xref: /openbmc/linux/fs/btrfs/transaction.c (revision 3bbb24b2)
16cbd5570SChris Mason /*
26cbd5570SChris Mason  * Copyright (C) 2007 Oracle.  All rights reserved.
36cbd5570SChris Mason  *
46cbd5570SChris Mason  * This program is free software; you can redistribute it and/or
56cbd5570SChris Mason  * modify it under the terms of the GNU General Public
66cbd5570SChris Mason  * License v2 as published by the Free Software Foundation.
76cbd5570SChris Mason  *
86cbd5570SChris Mason  * This program is distributed in the hope that it will be useful,
96cbd5570SChris Mason  * but WITHOUT ANY WARRANTY; without even the implied warranty of
106cbd5570SChris Mason  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
116cbd5570SChris Mason  * General Public License for more details.
126cbd5570SChris Mason  *
136cbd5570SChris Mason  * You should have received a copy of the GNU General Public
146cbd5570SChris Mason  * License along with this program; if not, write to the
156cbd5570SChris Mason  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
166cbd5570SChris Mason  * Boston, MA 021110-1307, USA.
176cbd5570SChris Mason  */
186cbd5570SChris Mason 
1979154b1bSChris Mason #include <linux/fs.h>
205a0e3ad6STejun Heo #include <linux/slab.h>
2134088780SChris Mason #include <linux/sched.h>
22d3c2fdcfSChris Mason #include <linux/writeback.h>
235f39d397SChris Mason #include <linux/pagemap.h>
245f2cc086SChris Mason #include <linux/blkdev.h>
258ea05e3aSAlexander Block #include <linux/uuid.h>
2679154b1bSChris Mason #include "ctree.h"
2779154b1bSChris Mason #include "disk-io.h"
2879154b1bSChris Mason #include "transaction.h"
29925baeddSChris Mason #include "locking.h"
30e02119d5SChris Mason #include "tree-log.h"
31581bb050SLi Zefan #include "inode-map.h"
32733f4fbbSStefan Behrens #include "volumes.h"
338dabb742SStefan Behrens #include "dev-replace.h"
3479154b1bSChris Mason 
350f7d52f4SChris Mason #define BTRFS_ROOT_TRANS_TAG 0
360f7d52f4SChris Mason 
374a9d8bdeSMiao Xie static unsigned int btrfs_blocked_trans_types[TRANS_STATE_MAX] = {
384a9d8bdeSMiao Xie 	[TRANS_STATE_RUNNING]		= 0U,
394a9d8bdeSMiao Xie 	[TRANS_STATE_BLOCKED]		= (__TRANS_USERSPACE |
404a9d8bdeSMiao Xie 					   __TRANS_START),
414a9d8bdeSMiao Xie 	[TRANS_STATE_COMMIT_START]	= (__TRANS_USERSPACE |
424a9d8bdeSMiao Xie 					   __TRANS_START |
434a9d8bdeSMiao Xie 					   __TRANS_ATTACH),
444a9d8bdeSMiao Xie 	[TRANS_STATE_COMMIT_DOING]	= (__TRANS_USERSPACE |
454a9d8bdeSMiao Xie 					   __TRANS_START |
464a9d8bdeSMiao Xie 					   __TRANS_ATTACH |
474a9d8bdeSMiao Xie 					   __TRANS_JOIN),
484a9d8bdeSMiao Xie 	[TRANS_STATE_UNBLOCKED]		= (__TRANS_USERSPACE |
494a9d8bdeSMiao Xie 					   __TRANS_START |
504a9d8bdeSMiao Xie 					   __TRANS_ATTACH |
514a9d8bdeSMiao Xie 					   __TRANS_JOIN |
524a9d8bdeSMiao Xie 					   __TRANS_JOIN_NOLOCK),
534a9d8bdeSMiao Xie 	[TRANS_STATE_COMPLETED]		= (__TRANS_USERSPACE |
544a9d8bdeSMiao Xie 					   __TRANS_START |
554a9d8bdeSMiao Xie 					   __TRANS_ATTACH |
564a9d8bdeSMiao Xie 					   __TRANS_JOIN |
574a9d8bdeSMiao Xie 					   __TRANS_JOIN_NOLOCK),
584a9d8bdeSMiao Xie };
594a9d8bdeSMiao Xie 
60724e2315SJosef Bacik void btrfs_put_transaction(struct btrfs_transaction *transaction)
6179154b1bSChris Mason {
6213c5a93eSJosef Bacik 	WARN_ON(atomic_read(&transaction->use_count) == 0);
6313c5a93eSJosef Bacik 	if (atomic_dec_and_test(&transaction->use_count)) {
64a4abeea4SJosef Bacik 		BUG_ON(!list_empty(&transaction->list));
65c46effa6SLiu Bo 		WARN_ON(!RB_EMPTY_ROOT(&transaction->delayed_refs.href_root));
666df9a95eSJosef Bacik 		while (!list_empty(&transaction->pending_chunks)) {
676df9a95eSJosef Bacik 			struct extent_map *em;
686df9a95eSJosef Bacik 
696df9a95eSJosef Bacik 			em = list_first_entry(&transaction->pending_chunks,
706df9a95eSJosef Bacik 					      struct extent_map, list);
716df9a95eSJosef Bacik 			list_del_init(&em->list);
726df9a95eSJosef Bacik 			free_extent_map(em);
736df9a95eSJosef Bacik 		}
742c90e5d6SChris Mason 		kmem_cache_free(btrfs_transaction_cachep, transaction);
7579154b1bSChris Mason 	}
7678fae27eSChris Mason }
7779154b1bSChris Mason 
78817d52f8SJosef Bacik static noinline void switch_commit_root(struct btrfs_root *root)
79817d52f8SJosef Bacik {
80817d52f8SJosef Bacik 	free_extent_buffer(root->commit_root);
81817d52f8SJosef Bacik 	root->commit_root = btrfs_root_node(root);
82817d52f8SJosef Bacik }
83817d52f8SJosef Bacik 
840860adfdSMiao Xie static inline void extwriter_counter_inc(struct btrfs_transaction *trans,
850860adfdSMiao Xie 					 unsigned int type)
860860adfdSMiao Xie {
870860adfdSMiao Xie 	if (type & TRANS_EXTWRITERS)
880860adfdSMiao Xie 		atomic_inc(&trans->num_extwriters);
890860adfdSMiao Xie }
900860adfdSMiao Xie 
910860adfdSMiao Xie static inline void extwriter_counter_dec(struct btrfs_transaction *trans,
920860adfdSMiao Xie 					 unsigned int type)
930860adfdSMiao Xie {
940860adfdSMiao Xie 	if (type & TRANS_EXTWRITERS)
950860adfdSMiao Xie 		atomic_dec(&trans->num_extwriters);
960860adfdSMiao Xie }
970860adfdSMiao Xie 
980860adfdSMiao Xie static inline void extwriter_counter_init(struct btrfs_transaction *trans,
990860adfdSMiao Xie 					  unsigned int type)
1000860adfdSMiao Xie {
1010860adfdSMiao Xie 	atomic_set(&trans->num_extwriters, ((type & TRANS_EXTWRITERS) ? 1 : 0));
1020860adfdSMiao Xie }
1030860adfdSMiao Xie 
1040860adfdSMiao Xie static inline int extwriter_counter_read(struct btrfs_transaction *trans)
1050860adfdSMiao Xie {
1060860adfdSMiao Xie 	return atomic_read(&trans->num_extwriters);
107178260b2SMiao Xie }
108178260b2SMiao Xie 
109d352ac68SChris Mason /*
110d352ac68SChris Mason  * either allocate a new transaction or hop into the existing one
111d352ac68SChris Mason  */
1120860adfdSMiao Xie static noinline int join_transaction(struct btrfs_root *root, unsigned int type)
11379154b1bSChris Mason {
11479154b1bSChris Mason 	struct btrfs_transaction *cur_trans;
11519ae4e81SJan Schmidt 	struct btrfs_fs_info *fs_info = root->fs_info;
116a4abeea4SJosef Bacik 
11719ae4e81SJan Schmidt 	spin_lock(&fs_info->trans_lock);
118d43317dcSChris Mason loop:
11949b25e05SJeff Mahoney 	/* The file system has been taken offline. No new transactions. */
12087533c47SMiao Xie 	if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) {
12119ae4e81SJan Schmidt 		spin_unlock(&fs_info->trans_lock);
12249b25e05SJeff Mahoney 		return -EROFS;
12349b25e05SJeff Mahoney 	}
12449b25e05SJeff Mahoney 
12519ae4e81SJan Schmidt 	cur_trans = fs_info->running_transaction;
126a4abeea4SJosef Bacik 	if (cur_trans) {
127871383beSDavid Sterba 		if (cur_trans->aborted) {
12819ae4e81SJan Schmidt 			spin_unlock(&fs_info->trans_lock);
12949b25e05SJeff Mahoney 			return cur_trans->aborted;
130871383beSDavid Sterba 		}
1314a9d8bdeSMiao Xie 		if (btrfs_blocked_trans_types[cur_trans->state] & type) {
132178260b2SMiao Xie 			spin_unlock(&fs_info->trans_lock);
133178260b2SMiao Xie 			return -EBUSY;
134178260b2SMiao Xie 		}
135a4abeea4SJosef Bacik 		atomic_inc(&cur_trans->use_count);
136a4abeea4SJosef Bacik 		atomic_inc(&cur_trans->num_writers);
1370860adfdSMiao Xie 		extwriter_counter_inc(cur_trans, type);
13819ae4e81SJan Schmidt 		spin_unlock(&fs_info->trans_lock);
139a4abeea4SJosef Bacik 		return 0;
140a4abeea4SJosef Bacik 	}
14119ae4e81SJan Schmidt 	spin_unlock(&fs_info->trans_lock);
142a4abeea4SJosef Bacik 
143354aa0fbSMiao Xie 	/*
144354aa0fbSMiao Xie 	 * If we are ATTACH, we just want to catch the current transaction,
145354aa0fbSMiao Xie 	 * and commit it. If there is no transaction, just return ENOENT.
146354aa0fbSMiao Xie 	 */
147354aa0fbSMiao Xie 	if (type == TRANS_ATTACH)
148354aa0fbSMiao Xie 		return -ENOENT;
149354aa0fbSMiao Xie 
1504a9d8bdeSMiao Xie 	/*
1514a9d8bdeSMiao Xie 	 * JOIN_NOLOCK only happens during the transaction commit, so
1524a9d8bdeSMiao Xie 	 * it is impossible that ->running_transaction is NULL
1534a9d8bdeSMiao Xie 	 */
1544a9d8bdeSMiao Xie 	BUG_ON(type == TRANS_JOIN_NOLOCK);
1554a9d8bdeSMiao Xie 
156a4abeea4SJosef Bacik 	cur_trans = kmem_cache_alloc(btrfs_transaction_cachep, GFP_NOFS);
157db5b493aSTsutomu Itoh 	if (!cur_trans)
158db5b493aSTsutomu Itoh 		return -ENOMEM;
159d43317dcSChris Mason 
16019ae4e81SJan Schmidt 	spin_lock(&fs_info->trans_lock);
16119ae4e81SJan Schmidt 	if (fs_info->running_transaction) {
162d43317dcSChris Mason 		/*
163d43317dcSChris Mason 		 * someone started a transaction after we unlocked.  Make sure
1644a9d8bdeSMiao Xie 		 * to redo the checks above
165d43317dcSChris Mason 		 */
166a4abeea4SJosef Bacik 		kmem_cache_free(btrfs_transaction_cachep, cur_trans);
167d43317dcSChris Mason 		goto loop;
16887533c47SMiao Xie 	} else if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) {
169e4b50e14SDan Carpenter 		spin_unlock(&fs_info->trans_lock);
1707b8b92afSJosef Bacik 		kmem_cache_free(btrfs_transaction_cachep, cur_trans);
1717b8b92afSJosef Bacik 		return -EROFS;
172a4abeea4SJosef Bacik 	}
173d43317dcSChris Mason 
17413c5a93eSJosef Bacik 	atomic_set(&cur_trans->num_writers, 1);
1750860adfdSMiao Xie 	extwriter_counter_init(cur_trans, type);
17679154b1bSChris Mason 	init_waitqueue_head(&cur_trans->writer_wait);
17779154b1bSChris Mason 	init_waitqueue_head(&cur_trans->commit_wait);
1784a9d8bdeSMiao Xie 	cur_trans->state = TRANS_STATE_RUNNING;
179a4abeea4SJosef Bacik 	/*
180a4abeea4SJosef Bacik 	 * One for this trans handle, one so it will live on until we
181a4abeea4SJosef Bacik 	 * commit the transaction.
182a4abeea4SJosef Bacik 	 */
183a4abeea4SJosef Bacik 	atomic_set(&cur_trans->use_count, 2);
18408607c1bSChris Mason 	cur_trans->start_time = get_seconds();
18556bec294SChris Mason 
186c46effa6SLiu Bo 	cur_trans->delayed_refs.href_root = RB_ROOT;
187d7df2c79SJosef Bacik 	atomic_set(&cur_trans->delayed_refs.num_entries, 0);
188c3e69d58SChris Mason 	cur_trans->delayed_refs.num_heads_ready = 0;
189c3e69d58SChris Mason 	cur_trans->delayed_refs.num_heads = 0;
19056bec294SChris Mason 	cur_trans->delayed_refs.flushing = 0;
191c3e69d58SChris Mason 	cur_trans->delayed_refs.run_delayed_start = 0;
19220b297d6SJan Schmidt 
19320b297d6SJan Schmidt 	/*
19420b297d6SJan Schmidt 	 * although the tree mod log is per file system and not per transaction,
19520b297d6SJan Schmidt 	 * the log must never go across transaction boundaries.
19620b297d6SJan Schmidt 	 */
19720b297d6SJan Schmidt 	smp_mb();
19831b1a2bdSJulia Lawall 	if (!list_empty(&fs_info->tree_mod_seq_list))
199efe120a0SFrank Holton 		WARN(1, KERN_ERR "BTRFS: tree_mod_seq_list not empty when "
20020b297d6SJan Schmidt 			"creating a fresh transaction\n");
20131b1a2bdSJulia Lawall 	if (!RB_EMPTY_ROOT(&fs_info->tree_mod_log))
202efe120a0SFrank Holton 		WARN(1, KERN_ERR "BTRFS: tree_mod_log rb tree not empty when "
20320b297d6SJan Schmidt 			"creating a fresh transaction\n");
204fc36ed7eSJan Schmidt 	atomic64_set(&fs_info->tree_mod_seq, 0);
20520b297d6SJan Schmidt 
20656bec294SChris Mason 	spin_lock_init(&cur_trans->delayed_refs.lock);
20756bec294SChris Mason 
2083063d29fSChris Mason 	INIT_LIST_HEAD(&cur_trans->pending_snapshots);
209569e0f35SJosef Bacik 	INIT_LIST_HEAD(&cur_trans->ordered_operations);
2106df9a95eSJosef Bacik 	INIT_LIST_HEAD(&cur_trans->pending_chunks);
21119ae4e81SJan Schmidt 	list_add_tail(&cur_trans->list, &fs_info->trans_list);
212d1310b2eSChris Mason 	extent_io_tree_init(&cur_trans->dirty_pages,
21319ae4e81SJan Schmidt 			     fs_info->btree_inode->i_mapping);
21419ae4e81SJan Schmidt 	fs_info->generation++;
21519ae4e81SJan Schmidt 	cur_trans->transid = fs_info->generation;
21619ae4e81SJan Schmidt 	fs_info->running_transaction = cur_trans;
21749b25e05SJeff Mahoney 	cur_trans->aborted = 0;
21819ae4e81SJan Schmidt 	spin_unlock(&fs_info->trans_lock);
21915ee9bc7SJosef Bacik 
22079154b1bSChris Mason 	return 0;
22179154b1bSChris Mason }
22279154b1bSChris Mason 
223d352ac68SChris Mason /*
224d397712bSChris Mason  * this does all the record keeping required to make sure that a reference
225d397712bSChris Mason  * counted root is properly recorded in a given transaction.  This is required
226d397712bSChris Mason  * to make sure the old root from before we joined the transaction is deleted
227d397712bSChris Mason  * when the transaction commits
228d352ac68SChris Mason  */
2297585717fSChris Mason static int record_root_in_trans(struct btrfs_trans_handle *trans,
2305d4f98a2SYan Zheng 			       struct btrfs_root *root)
2316702ed49SChris Mason {
2325d4f98a2SYan Zheng 	if (root->ref_cows && root->last_trans < trans->transid) {
2336702ed49SChris Mason 		WARN_ON(root == root->fs_info->extent_root);
2345d4f98a2SYan Zheng 		WARN_ON(root->commit_root != root->node);
2355d4f98a2SYan Zheng 
2367585717fSChris Mason 		/*
2377585717fSChris Mason 		 * see below for in_trans_setup usage rules
2387585717fSChris Mason 		 * we have the reloc mutex held now, so there
2397585717fSChris Mason 		 * is only one writer in this function
2407585717fSChris Mason 		 */
2417585717fSChris Mason 		root->in_trans_setup = 1;
2427585717fSChris Mason 
2437585717fSChris Mason 		/* make sure readers find in_trans_setup before
2447585717fSChris Mason 		 * they find our root->last_trans update
2457585717fSChris Mason 		 */
2467585717fSChris Mason 		smp_wmb();
2477585717fSChris Mason 
248a4abeea4SJosef Bacik 		spin_lock(&root->fs_info->fs_roots_radix_lock);
249a4abeea4SJosef Bacik 		if (root->last_trans == trans->transid) {
250a4abeea4SJosef Bacik 			spin_unlock(&root->fs_info->fs_roots_radix_lock);
251a4abeea4SJosef Bacik 			return 0;
252a4abeea4SJosef Bacik 		}
2536702ed49SChris Mason 		radix_tree_tag_set(&root->fs_info->fs_roots_radix,
2546702ed49SChris Mason 			   (unsigned long)root->root_key.objectid,
2556702ed49SChris Mason 			   BTRFS_ROOT_TRANS_TAG);
256a4abeea4SJosef Bacik 		spin_unlock(&root->fs_info->fs_roots_radix_lock);
2577585717fSChris Mason 		root->last_trans = trans->transid;
2587585717fSChris Mason 
2597585717fSChris Mason 		/* this is pretty tricky.  We don't want to
2607585717fSChris Mason 		 * take the relocation lock in btrfs_record_root_in_trans
2617585717fSChris Mason 		 * unless we're really doing the first setup for this root in
2627585717fSChris Mason 		 * this transaction.
2637585717fSChris Mason 		 *
2647585717fSChris Mason 		 * Normally we'd use root->last_trans as a flag to decide
2657585717fSChris Mason 		 * if we want to take the expensive mutex.
2667585717fSChris Mason 		 *
2677585717fSChris Mason 		 * But, we have to set root->last_trans before we
2687585717fSChris Mason 		 * init the relocation root, otherwise, we trip over warnings
2697585717fSChris Mason 		 * in ctree.c.  The solution used here is to flag ourselves
2707585717fSChris Mason 		 * with root->in_trans_setup.  When this is 1, we're still
2717585717fSChris Mason 		 * fixing up the reloc trees and everyone must wait.
2727585717fSChris Mason 		 *
2737585717fSChris Mason 		 * When this is zero, they can trust root->last_trans and fly
2747585717fSChris Mason 		 * through btrfs_record_root_in_trans without having to take the
2757585717fSChris Mason 		 * lock.  smp_wmb() makes sure that all the writes above are
2767585717fSChris Mason 		 * done before we pop in the zero below
2777585717fSChris Mason 		 */
2785d4f98a2SYan Zheng 		btrfs_init_reloc_root(trans, root);
2797585717fSChris Mason 		smp_wmb();
2807585717fSChris Mason 		root->in_trans_setup = 0;
2816702ed49SChris Mason 	}
2825d4f98a2SYan Zheng 	return 0;
2836702ed49SChris Mason }
2845d4f98a2SYan Zheng 
2857585717fSChris Mason 
2867585717fSChris Mason int btrfs_record_root_in_trans(struct btrfs_trans_handle *trans,
2877585717fSChris Mason 			       struct btrfs_root *root)
2887585717fSChris Mason {
2897585717fSChris Mason 	if (!root->ref_cows)
2907585717fSChris Mason 		return 0;
2917585717fSChris Mason 
2927585717fSChris Mason 	/*
2937585717fSChris Mason 	 * see record_root_in_trans for comments about in_trans_setup usage
2947585717fSChris Mason 	 * and barriers
2957585717fSChris Mason 	 */
2967585717fSChris Mason 	smp_rmb();
2977585717fSChris Mason 	if (root->last_trans == trans->transid &&
2987585717fSChris Mason 	    !root->in_trans_setup)
2997585717fSChris Mason 		return 0;
3007585717fSChris Mason 
3017585717fSChris Mason 	mutex_lock(&root->fs_info->reloc_mutex);
3027585717fSChris Mason 	record_root_in_trans(trans, root);
3037585717fSChris Mason 	mutex_unlock(&root->fs_info->reloc_mutex);
3047585717fSChris Mason 
3057585717fSChris Mason 	return 0;
3067585717fSChris Mason }
3077585717fSChris Mason 
3084a9d8bdeSMiao Xie static inline int is_transaction_blocked(struct btrfs_transaction *trans)
3094a9d8bdeSMiao Xie {
3104a9d8bdeSMiao Xie 	return (trans->state >= TRANS_STATE_BLOCKED &&
311501407aaSJosef Bacik 		trans->state < TRANS_STATE_UNBLOCKED &&
312501407aaSJosef Bacik 		!trans->aborted);
3134a9d8bdeSMiao Xie }
3144a9d8bdeSMiao Xie 
315d352ac68SChris Mason /* wait for commit against the current transaction to become unblocked
316d352ac68SChris Mason  * when this is done, it is safe to start a new transaction, but the current
317d352ac68SChris Mason  * transaction might not be fully on disk.
318d352ac68SChris Mason  */
31937d1aeeeSChris Mason static void wait_current_trans(struct btrfs_root *root)
32079154b1bSChris Mason {
321f9295749SChris Mason 	struct btrfs_transaction *cur_trans;
32279154b1bSChris Mason 
323a4abeea4SJosef Bacik 	spin_lock(&root->fs_info->trans_lock);
324f9295749SChris Mason 	cur_trans = root->fs_info->running_transaction;
3254a9d8bdeSMiao Xie 	if (cur_trans && is_transaction_blocked(cur_trans)) {
32613c5a93eSJosef Bacik 		atomic_inc(&cur_trans->use_count);
327a4abeea4SJosef Bacik 		spin_unlock(&root->fs_info->trans_lock);
32872d63ed6SLi Zefan 
32972d63ed6SLi Zefan 		wait_event(root->fs_info->transaction_wait,
330501407aaSJosef Bacik 			   cur_trans->state >= TRANS_STATE_UNBLOCKED ||
331501407aaSJosef Bacik 			   cur_trans->aborted);
332724e2315SJosef Bacik 		btrfs_put_transaction(cur_trans);
333a4abeea4SJosef Bacik 	} else {
334a4abeea4SJosef Bacik 		spin_unlock(&root->fs_info->trans_lock);
335f9295749SChris Mason 	}
33637d1aeeeSChris Mason }
33737d1aeeeSChris Mason 
338a22285a6SYan, Zheng static int may_wait_transaction(struct btrfs_root *root, int type)
33937d1aeeeSChris Mason {
340a4abeea4SJosef Bacik 	if (root->fs_info->log_root_recovering)
341a4abeea4SJosef Bacik 		return 0;
342a4abeea4SJosef Bacik 
343a4abeea4SJosef Bacik 	if (type == TRANS_USERSPACE)
344a22285a6SYan, Zheng 		return 1;
345a4abeea4SJosef Bacik 
346a4abeea4SJosef Bacik 	if (type == TRANS_START &&
347a4abeea4SJosef Bacik 	    !atomic_read(&root->fs_info->open_ioctl_trans))
348a4abeea4SJosef Bacik 		return 1;
349a4abeea4SJosef Bacik 
350a22285a6SYan, Zheng 	return 0;
351a22285a6SYan, Zheng }
352a22285a6SYan, Zheng 
35320dd2cbfSMiao Xie static inline bool need_reserve_reloc_root(struct btrfs_root *root)
35420dd2cbfSMiao Xie {
35520dd2cbfSMiao Xie 	if (!root->fs_info->reloc_ctl ||
35620dd2cbfSMiao Xie 	    !root->ref_cows ||
35720dd2cbfSMiao Xie 	    root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID ||
35820dd2cbfSMiao Xie 	    root->reloc_root)
35920dd2cbfSMiao Xie 		return false;
36020dd2cbfSMiao Xie 
36120dd2cbfSMiao Xie 	return true;
36220dd2cbfSMiao Xie }
36320dd2cbfSMiao Xie 
36408e007d2SMiao Xie static struct btrfs_trans_handle *
3650860adfdSMiao Xie start_transaction(struct btrfs_root *root, u64 num_items, unsigned int type,
36608e007d2SMiao Xie 		  enum btrfs_reserve_flush_enum flush)
367a22285a6SYan, Zheng {
368a22285a6SYan, Zheng 	struct btrfs_trans_handle *h;
369a22285a6SYan, Zheng 	struct btrfs_transaction *cur_trans;
370b5009945SJosef Bacik 	u64 num_bytes = 0;
371c5567237SArne Jansen 	u64 qgroup_reserved = 0;
37220dd2cbfSMiao Xie 	bool reloc_reserved = false;
37320dd2cbfSMiao Xie 	int ret;
374acce952bSliubo 
37587533c47SMiao Xie 	if (test_bit(BTRFS_FS_STATE_ERROR, &root->fs_info->fs_state))
376acce952bSliubo 		return ERR_PTR(-EROFS);
3772a1eb461SJosef Bacik 
3782a1eb461SJosef Bacik 	if (current->journal_info) {
3790860adfdSMiao Xie 		WARN_ON(type & TRANS_EXTWRITERS);
3802a1eb461SJosef Bacik 		h = current->journal_info;
3812a1eb461SJosef Bacik 		h->use_count++;
382b7d5b0a8SMiao Xie 		WARN_ON(h->use_count > 2);
3832a1eb461SJosef Bacik 		h->orig_rsv = h->block_rsv;
3842a1eb461SJosef Bacik 		h->block_rsv = NULL;
3852a1eb461SJosef Bacik 		goto got_it;
3862a1eb461SJosef Bacik 	}
387b5009945SJosef Bacik 
388b5009945SJosef Bacik 	/*
389b5009945SJosef Bacik 	 * Do the reservation before we join the transaction so we can do all
390b5009945SJosef Bacik 	 * the appropriate flushing if need be.
391b5009945SJosef Bacik 	 */
392b5009945SJosef Bacik 	if (num_items > 0 && root != root->fs_info->chunk_root) {
393c5567237SArne Jansen 		if (root->fs_info->quota_enabled &&
394c5567237SArne Jansen 		    is_fstree(root->root_key.objectid)) {
395c5567237SArne Jansen 			qgroup_reserved = num_items * root->leafsize;
396c5567237SArne Jansen 			ret = btrfs_qgroup_reserve(root, qgroup_reserved);
397c5567237SArne Jansen 			if (ret)
398c5567237SArne Jansen 				return ERR_PTR(ret);
399c5567237SArne Jansen 		}
400c5567237SArne Jansen 
401b5009945SJosef Bacik 		num_bytes = btrfs_calc_trans_metadata_size(root, num_items);
40220dd2cbfSMiao Xie 		/*
40320dd2cbfSMiao Xie 		 * Do the reservation for the relocation root creation
40420dd2cbfSMiao Xie 		 */
40520dd2cbfSMiao Xie 		if (unlikely(need_reserve_reloc_root(root))) {
40620dd2cbfSMiao Xie 			num_bytes += root->nodesize;
40720dd2cbfSMiao Xie 			reloc_reserved = true;
40820dd2cbfSMiao Xie 		}
40920dd2cbfSMiao Xie 
4104a92b1b8SJosef Bacik 		ret = btrfs_block_rsv_add(root,
411b5009945SJosef Bacik 					  &root->fs_info->trans_block_rsv,
41208e007d2SMiao Xie 					  num_bytes, flush);
413b5009945SJosef Bacik 		if (ret)
414843fcf35SMiao Xie 			goto reserve_fail;
415b5009945SJosef Bacik 	}
416a22285a6SYan, Zheng again:
417a22285a6SYan, Zheng 	h = kmem_cache_alloc(btrfs_trans_handle_cachep, GFP_NOFS);
418843fcf35SMiao Xie 	if (!h) {
419843fcf35SMiao Xie 		ret = -ENOMEM;
420843fcf35SMiao Xie 		goto alloc_fail;
421843fcf35SMiao Xie 	}
422a22285a6SYan, Zheng 
42398114659SJosef Bacik 	/*
42498114659SJosef Bacik 	 * If we are JOIN_NOLOCK we're already committing a transaction and
42598114659SJosef Bacik 	 * waiting on this guy, so we don't need to do the sb_start_intwrite
42698114659SJosef Bacik 	 * because we're already holding a ref.  We need this because we could
42798114659SJosef Bacik 	 * have raced in and did an fsync() on a file which can kick a commit
42898114659SJosef Bacik 	 * and then we deadlock with somebody doing a freeze.
429354aa0fbSMiao Xie 	 *
430354aa0fbSMiao Xie 	 * If we are ATTACH, it means we just want to catch the current
431354aa0fbSMiao Xie 	 * transaction and commit it, so we needn't do sb_start_intwrite().
43298114659SJosef Bacik 	 */
4330860adfdSMiao Xie 	if (type & __TRANS_FREEZABLE)
434b2b5ef5cSJan Kara 		sb_start_intwrite(root->fs_info->sb);
435b2b5ef5cSJan Kara 
436a22285a6SYan, Zheng 	if (may_wait_transaction(root, type))
43737d1aeeeSChris Mason 		wait_current_trans(root);
438a22285a6SYan, Zheng 
439a4abeea4SJosef Bacik 	do {
440354aa0fbSMiao Xie 		ret = join_transaction(root, type);
441178260b2SMiao Xie 		if (ret == -EBUSY) {
442a4abeea4SJosef Bacik 			wait_current_trans(root);
443178260b2SMiao Xie 			if (unlikely(type == TRANS_ATTACH))
444178260b2SMiao Xie 				ret = -ENOENT;
445178260b2SMiao Xie 		}
446a4abeea4SJosef Bacik 	} while (ret == -EBUSY);
447a4abeea4SJosef Bacik 
448db5b493aSTsutomu Itoh 	if (ret < 0) {
449354aa0fbSMiao Xie 		/* We must get the transaction if we are JOIN_NOLOCK. */
450354aa0fbSMiao Xie 		BUG_ON(type == TRANS_JOIN_NOLOCK);
451843fcf35SMiao Xie 		goto join_fail;
452db5b493aSTsutomu Itoh 	}
4530f7d52f4SChris Mason 
454a22285a6SYan, Zheng 	cur_trans = root->fs_info->running_transaction;
455a22285a6SYan, Zheng 
456a22285a6SYan, Zheng 	h->transid = cur_trans->transid;
457a22285a6SYan, Zheng 	h->transaction = cur_trans;
45879154b1bSChris Mason 	h->blocks_used = 0;
459a22285a6SYan, Zheng 	h->bytes_reserved = 0;
460d13603efSArne Jansen 	h->root = root;
46156bec294SChris Mason 	h->delayed_ref_updates = 0;
4622a1eb461SJosef Bacik 	h->use_count = 1;
4630e721106SJosef Bacik 	h->adding_csums = 0;
464f0486c68SYan, Zheng 	h->block_rsv = NULL;
4652a1eb461SJosef Bacik 	h->orig_rsv = NULL;
46649b25e05SJeff Mahoney 	h->aborted = 0;
4674b824906SMiao Xie 	h->qgroup_reserved = 0;
468bed92eaeSArne Jansen 	h->delayed_ref_elem.seq = 0;
469a698d075SMiao Xie 	h->type = type;
470c6b305a8SJosef Bacik 	h->allocating_chunk = false;
47120dd2cbfSMiao Xie 	h->reloc_reserved = false;
4725039eddcSJosef Bacik 	h->sync = false;
473bed92eaeSArne Jansen 	INIT_LIST_HEAD(&h->qgroup_ref_list);
474ea658badSJosef Bacik 	INIT_LIST_HEAD(&h->new_bgs);
475b7ec40d7SChris Mason 
476a22285a6SYan, Zheng 	smp_mb();
4774a9d8bdeSMiao Xie 	if (cur_trans->state >= TRANS_STATE_BLOCKED &&
4784a9d8bdeSMiao Xie 	    may_wait_transaction(root, type)) {
479a22285a6SYan, Zheng 		btrfs_commit_transaction(h, root);
480a22285a6SYan, Zheng 		goto again;
481a22285a6SYan, Zheng 	}
4829ed74f2dSJosef Bacik 
483b5009945SJosef Bacik 	if (num_bytes) {
4848c2a3ca2SJosef Bacik 		trace_btrfs_space_reservation(root->fs_info, "transaction",
4852bcc0328SLiu Bo 					      h->transid, num_bytes, 1);
486b5009945SJosef Bacik 		h->block_rsv = &root->fs_info->trans_block_rsv;
487b5009945SJosef Bacik 		h->bytes_reserved = num_bytes;
48820dd2cbfSMiao Xie 		h->reloc_reserved = reloc_reserved;
489a22285a6SYan, Zheng 	}
4904b824906SMiao Xie 	h->qgroup_reserved = qgroup_reserved;
491a22285a6SYan, Zheng 
4922a1eb461SJosef Bacik got_it:
493a4abeea4SJosef Bacik 	btrfs_record_root_in_trans(h, root);
494a22285a6SYan, Zheng 
495a22285a6SYan, Zheng 	if (!current->journal_info && type != TRANS_USERSPACE)
496a22285a6SYan, Zheng 		current->journal_info = h;
49779154b1bSChris Mason 	return h;
498843fcf35SMiao Xie 
499843fcf35SMiao Xie join_fail:
5000860adfdSMiao Xie 	if (type & __TRANS_FREEZABLE)
501843fcf35SMiao Xie 		sb_end_intwrite(root->fs_info->sb);
502843fcf35SMiao Xie 	kmem_cache_free(btrfs_trans_handle_cachep, h);
503843fcf35SMiao Xie alloc_fail:
504843fcf35SMiao Xie 	if (num_bytes)
505843fcf35SMiao Xie 		btrfs_block_rsv_release(root, &root->fs_info->trans_block_rsv,
506843fcf35SMiao Xie 					num_bytes);
507843fcf35SMiao Xie reserve_fail:
508843fcf35SMiao Xie 	if (qgroup_reserved)
509843fcf35SMiao Xie 		btrfs_qgroup_free(root, qgroup_reserved);
510843fcf35SMiao Xie 	return ERR_PTR(ret);
51179154b1bSChris Mason }
51279154b1bSChris Mason 
513f9295749SChris Mason struct btrfs_trans_handle *btrfs_start_transaction(struct btrfs_root *root,
514a22285a6SYan, Zheng 						   int num_items)
515f9295749SChris Mason {
51608e007d2SMiao Xie 	return start_transaction(root, num_items, TRANS_START,
51708e007d2SMiao Xie 				 BTRFS_RESERVE_FLUSH_ALL);
518f9295749SChris Mason }
5198407aa46SMiao Xie 
52008e007d2SMiao Xie struct btrfs_trans_handle *btrfs_start_transaction_lflush(
5218407aa46SMiao Xie 					struct btrfs_root *root, int num_items)
5228407aa46SMiao Xie {
52308e007d2SMiao Xie 	return start_transaction(root, num_items, TRANS_START,
52408e007d2SMiao Xie 				 BTRFS_RESERVE_FLUSH_LIMIT);
5258407aa46SMiao Xie }
5268407aa46SMiao Xie 
5277a7eaa40SJosef Bacik struct btrfs_trans_handle *btrfs_join_transaction(struct btrfs_root *root)
528f9295749SChris Mason {
5298407aa46SMiao Xie 	return start_transaction(root, 0, TRANS_JOIN, 0);
530f9295749SChris Mason }
531f9295749SChris Mason 
5327a7eaa40SJosef Bacik struct btrfs_trans_handle *btrfs_join_transaction_nolock(struct btrfs_root *root)
5330af3d00bSJosef Bacik {
5348407aa46SMiao Xie 	return start_transaction(root, 0, TRANS_JOIN_NOLOCK, 0);
5350af3d00bSJosef Bacik }
5360af3d00bSJosef Bacik 
5377a7eaa40SJosef Bacik struct btrfs_trans_handle *btrfs_start_ioctl_transaction(struct btrfs_root *root)
5389ca9ee09SSage Weil {
5398407aa46SMiao Xie 	return start_transaction(root, 0, TRANS_USERSPACE, 0);
5409ca9ee09SSage Weil }
5419ca9ee09SSage Weil 
542d4edf39bSMiao Xie /*
543d4edf39bSMiao Xie  * btrfs_attach_transaction() - catch the running transaction
544d4edf39bSMiao Xie  *
545d4edf39bSMiao Xie  * It is used when we want to commit the current the transaction, but
546d4edf39bSMiao Xie  * don't want to start a new one.
547d4edf39bSMiao Xie  *
548d4edf39bSMiao Xie  * Note: If this function return -ENOENT, it just means there is no
549d4edf39bSMiao Xie  * running transaction. But it is possible that the inactive transaction
550d4edf39bSMiao Xie  * is still in the memory, not fully on disk. If you hope there is no
551d4edf39bSMiao Xie  * inactive transaction in the fs when -ENOENT is returned, you should
552d4edf39bSMiao Xie  * invoke
553d4edf39bSMiao Xie  *     btrfs_attach_transaction_barrier()
554d4edf39bSMiao Xie  */
555354aa0fbSMiao Xie struct btrfs_trans_handle *btrfs_attach_transaction(struct btrfs_root *root)
55660376ce4SJosef Bacik {
557354aa0fbSMiao Xie 	return start_transaction(root, 0, TRANS_ATTACH, 0);
55860376ce4SJosef Bacik }
55960376ce4SJosef Bacik 
560d4edf39bSMiao Xie /*
56190b6d283SWang Sheng-Hui  * btrfs_attach_transaction_barrier() - catch the running transaction
562d4edf39bSMiao Xie  *
563d4edf39bSMiao Xie  * It is similar to the above function, the differentia is this one
564d4edf39bSMiao Xie  * will wait for all the inactive transactions until they fully
565d4edf39bSMiao Xie  * complete.
566d4edf39bSMiao Xie  */
567d4edf39bSMiao Xie struct btrfs_trans_handle *
568d4edf39bSMiao Xie btrfs_attach_transaction_barrier(struct btrfs_root *root)
569d4edf39bSMiao Xie {
570d4edf39bSMiao Xie 	struct btrfs_trans_handle *trans;
571d4edf39bSMiao Xie 
572d4edf39bSMiao Xie 	trans = start_transaction(root, 0, TRANS_ATTACH, 0);
573d4edf39bSMiao Xie 	if (IS_ERR(trans) && PTR_ERR(trans) == -ENOENT)
574d4edf39bSMiao Xie 		btrfs_wait_for_commit(root, 0);
575d4edf39bSMiao Xie 
576d4edf39bSMiao Xie 	return trans;
577d4edf39bSMiao Xie }
578d4edf39bSMiao Xie 
579d352ac68SChris Mason /* wait for a transaction commit to be fully complete */
580b9c8300cSLi Zefan static noinline void wait_for_commit(struct btrfs_root *root,
58189ce8a63SChris Mason 				    struct btrfs_transaction *commit)
58289ce8a63SChris Mason {
5834a9d8bdeSMiao Xie 	wait_event(commit->commit_wait, commit->state == TRANS_STATE_COMPLETED);
58489ce8a63SChris Mason }
58589ce8a63SChris Mason 
58646204592SSage Weil int btrfs_wait_for_commit(struct btrfs_root *root, u64 transid)
58746204592SSage Weil {
58846204592SSage Weil 	struct btrfs_transaction *cur_trans = NULL, *t;
5898cd2807fSMiao Xie 	int ret = 0;
59046204592SSage Weil 
59146204592SSage Weil 	if (transid) {
59246204592SSage Weil 		if (transid <= root->fs_info->last_trans_committed)
593a4abeea4SJosef Bacik 			goto out;
59446204592SSage Weil 
5958cd2807fSMiao Xie 		ret = -EINVAL;
59646204592SSage Weil 		/* find specified transaction */
597a4abeea4SJosef Bacik 		spin_lock(&root->fs_info->trans_lock);
59846204592SSage Weil 		list_for_each_entry(t, &root->fs_info->trans_list, list) {
59946204592SSage Weil 			if (t->transid == transid) {
60046204592SSage Weil 				cur_trans = t;
601a4abeea4SJosef Bacik 				atomic_inc(&cur_trans->use_count);
6028cd2807fSMiao Xie 				ret = 0;
60346204592SSage Weil 				break;
60446204592SSage Weil 			}
6058cd2807fSMiao Xie 			if (t->transid > transid) {
6068cd2807fSMiao Xie 				ret = 0;
60746204592SSage Weil 				break;
60846204592SSage Weil 			}
6098cd2807fSMiao Xie 		}
610a4abeea4SJosef Bacik 		spin_unlock(&root->fs_info->trans_lock);
6118cd2807fSMiao Xie 		/* The specified transaction doesn't exist */
61246204592SSage Weil 		if (!cur_trans)
6138cd2807fSMiao Xie 			goto out;
61446204592SSage Weil 	} else {
61546204592SSage Weil 		/* find newest transaction that is committing | committed */
616a4abeea4SJosef Bacik 		spin_lock(&root->fs_info->trans_lock);
61746204592SSage Weil 		list_for_each_entry_reverse(t, &root->fs_info->trans_list,
61846204592SSage Weil 					    list) {
6194a9d8bdeSMiao Xie 			if (t->state >= TRANS_STATE_COMMIT_START) {
6204a9d8bdeSMiao Xie 				if (t->state == TRANS_STATE_COMPLETED)
6213473f3c0SJosef Bacik 					break;
62246204592SSage Weil 				cur_trans = t;
623a4abeea4SJosef Bacik 				atomic_inc(&cur_trans->use_count);
62446204592SSage Weil 				break;
62546204592SSage Weil 			}
62646204592SSage Weil 		}
627a4abeea4SJosef Bacik 		spin_unlock(&root->fs_info->trans_lock);
62846204592SSage Weil 		if (!cur_trans)
629a4abeea4SJosef Bacik 			goto out;  /* nothing committing|committed */
63046204592SSage Weil 	}
63146204592SSage Weil 
63246204592SSage Weil 	wait_for_commit(root, cur_trans);
633724e2315SJosef Bacik 	btrfs_put_transaction(cur_trans);
634a4abeea4SJosef Bacik out:
63546204592SSage Weil 	return ret;
63646204592SSage Weil }
63746204592SSage Weil 
63837d1aeeeSChris Mason void btrfs_throttle(struct btrfs_root *root)
63937d1aeeeSChris Mason {
640a4abeea4SJosef Bacik 	if (!atomic_read(&root->fs_info->open_ioctl_trans))
64137d1aeeeSChris Mason 		wait_current_trans(root);
64237d1aeeeSChris Mason }
64337d1aeeeSChris Mason 
6448929ecfaSYan, Zheng static int should_end_transaction(struct btrfs_trans_handle *trans,
6458929ecfaSYan, Zheng 				  struct btrfs_root *root)
6468929ecfaSYan, Zheng {
6471be41b78SJosef Bacik 	if (root->fs_info->global_block_rsv.space_info->full &&
6480a2b2a84SJosef Bacik 	    btrfs_check_space_for_delayed_refs(trans, root))
6491be41b78SJosef Bacik 		return 1;
65036ba022aSJosef Bacik 
6511be41b78SJosef Bacik 	return !!btrfs_block_rsv_check(root, &root->fs_info->global_block_rsv, 5);
6528929ecfaSYan, Zheng }
6538929ecfaSYan, Zheng 
6548929ecfaSYan, Zheng int btrfs_should_end_transaction(struct btrfs_trans_handle *trans,
6558929ecfaSYan, Zheng 				 struct btrfs_root *root)
6568929ecfaSYan, Zheng {
6578929ecfaSYan, Zheng 	struct btrfs_transaction *cur_trans = trans->transaction;
6588929ecfaSYan, Zheng 	int updates;
65949b25e05SJeff Mahoney 	int err;
6608929ecfaSYan, Zheng 
661a4abeea4SJosef Bacik 	smp_mb();
6624a9d8bdeSMiao Xie 	if (cur_trans->state >= TRANS_STATE_BLOCKED ||
6634a9d8bdeSMiao Xie 	    cur_trans->delayed_refs.flushing)
6648929ecfaSYan, Zheng 		return 1;
6658929ecfaSYan, Zheng 
6668929ecfaSYan, Zheng 	updates = trans->delayed_ref_updates;
6678929ecfaSYan, Zheng 	trans->delayed_ref_updates = 0;
66849b25e05SJeff Mahoney 	if (updates) {
66949b25e05SJeff Mahoney 		err = btrfs_run_delayed_refs(trans, root, updates);
67049b25e05SJeff Mahoney 		if (err) /* Error code will also eval true */
67149b25e05SJeff Mahoney 			return err;
67249b25e05SJeff Mahoney 	}
6738929ecfaSYan, Zheng 
6748929ecfaSYan, Zheng 	return should_end_transaction(trans, root);
6758929ecfaSYan, Zheng }
6768929ecfaSYan, Zheng 
67789ce8a63SChris Mason static int __btrfs_end_transaction(struct btrfs_trans_handle *trans,
678a698d075SMiao Xie 			  struct btrfs_root *root, int throttle)
67979154b1bSChris Mason {
6808929ecfaSYan, Zheng 	struct btrfs_transaction *cur_trans = trans->transaction;
681ab78c84dSChris Mason 	struct btrfs_fs_info *info = root->fs_info;
6821be41b78SJosef Bacik 	unsigned long cur = trans->delayed_ref_updates;
683a698d075SMiao Xie 	int lock = (trans->type != TRANS_JOIN_NOLOCK);
6844edc2ca3SDave Jones 	int err = 0;
685d6e4a428SChris Mason 
6863bbb24b2SJosef Bacik 	if (trans->use_count > 1) {
6873bbb24b2SJosef Bacik 		trans->use_count--;
6882a1eb461SJosef Bacik 		trans->block_rsv = trans->orig_rsv;
6892a1eb461SJosef Bacik 		return 0;
6902a1eb461SJosef Bacik 	}
6912a1eb461SJosef Bacik 
692edf39272SJan Schmidt 	/*
693edf39272SJan Schmidt 	 * do the qgroup accounting as early as possible
694edf39272SJan Schmidt 	 */
695edf39272SJan Schmidt 	err = btrfs_delayed_refs_qgroup_accounting(trans, info);
696edf39272SJan Schmidt 
697b24e03dbSJosef Bacik 	btrfs_trans_release_metadata(trans, root);
6984c13d758SJosef Bacik 	trans->block_rsv = NULL;
699c5567237SArne Jansen 
700c5567237SArne Jansen 	if (trans->qgroup_reserved) {
7017c2ec3f0SLiu Bo 		/*
7027c2ec3f0SLiu Bo 		 * the same root has to be passed here between start_transaction
7037c2ec3f0SLiu Bo 		 * and end_transaction. Subvolume quota depends on this.
7047c2ec3f0SLiu Bo 		 */
7057c2ec3f0SLiu Bo 		btrfs_qgroup_free(trans->root, trans->qgroup_reserved);
706c5567237SArne Jansen 		trans->qgroup_reserved = 0;
707c5567237SArne Jansen 	}
708c5567237SArne Jansen 
709ea658badSJosef Bacik 	if (!list_empty(&trans->new_bgs))
710ea658badSJosef Bacik 		btrfs_create_pending_block_groups(trans, root);
711ea658badSJosef Bacik 
712c3e69d58SChris Mason 	trans->delayed_ref_updates = 0;
7135039eddcSJosef Bacik 	if (!trans->sync && btrfs_should_throttle_delayed_refs(trans, root)) {
7140a2b2a84SJosef Bacik 		cur = max_t(unsigned long, cur, 32);
715c3e69d58SChris Mason 		trans->delayed_ref_updates = 0;
716c3e69d58SChris Mason 		btrfs_run_delayed_refs(trans, root, cur);
71756bec294SChris Mason 	}
718bb721703SChris Mason 
7190e721106SJosef Bacik 	btrfs_trans_release_metadata(trans, root);
7200e721106SJosef Bacik 	trans->block_rsv = NULL;
72156bec294SChris Mason 
722ea658badSJosef Bacik 	if (!list_empty(&trans->new_bgs))
723ea658badSJosef Bacik 		btrfs_create_pending_block_groups(trans, root);
724ea658badSJosef Bacik 
725a4abeea4SJosef Bacik 	if (lock && !atomic_read(&root->fs_info->open_ioctl_trans) &&
7264a9d8bdeSMiao Xie 	    should_end_transaction(trans, root) &&
7274a9d8bdeSMiao Xie 	    ACCESS_ONCE(cur_trans->state) == TRANS_STATE_RUNNING) {
7284a9d8bdeSMiao Xie 		spin_lock(&info->trans_lock);
7294a9d8bdeSMiao Xie 		if (cur_trans->state == TRANS_STATE_RUNNING)
7304a9d8bdeSMiao Xie 			cur_trans->state = TRANS_STATE_BLOCKED;
7314a9d8bdeSMiao Xie 		spin_unlock(&info->trans_lock);
732a4abeea4SJosef Bacik 	}
7338929ecfaSYan, Zheng 
7344a9d8bdeSMiao Xie 	if (lock && ACCESS_ONCE(cur_trans->state) == TRANS_STATE_BLOCKED) {
7353bbb24b2SJosef Bacik 		if (throttle)
7368929ecfaSYan, Zheng 			return btrfs_commit_transaction(trans, root);
7373bbb24b2SJosef Bacik 		else
7388929ecfaSYan, Zheng 			wake_up_process(info->transaction_kthread);
7398929ecfaSYan, Zheng 	}
7408929ecfaSYan, Zheng 
7410860adfdSMiao Xie 	if (trans->type & __TRANS_FREEZABLE)
7426df7881aSJosef Bacik 		sb_end_intwrite(root->fs_info->sb);
7436df7881aSJosef Bacik 
7448929ecfaSYan, Zheng 	WARN_ON(cur_trans != info->running_transaction);
74513c5a93eSJosef Bacik 	WARN_ON(atomic_read(&cur_trans->num_writers) < 1);
74613c5a93eSJosef Bacik 	atomic_dec(&cur_trans->num_writers);
7470860adfdSMiao Xie 	extwriter_counter_dec(cur_trans, trans->type);
74889ce8a63SChris Mason 
74999d16cbcSSage Weil 	smp_mb();
75079154b1bSChris Mason 	if (waitqueue_active(&cur_trans->writer_wait))
75179154b1bSChris Mason 		wake_up(&cur_trans->writer_wait);
752724e2315SJosef Bacik 	btrfs_put_transaction(cur_trans);
7539ed74f2dSJosef Bacik 
7549ed74f2dSJosef Bacik 	if (current->journal_info == trans)
7559ed74f2dSJosef Bacik 		current->journal_info = NULL;
756ab78c84dSChris Mason 
75724bbcf04SYan, Zheng 	if (throttle)
75824bbcf04SYan, Zheng 		btrfs_run_delayed_iputs(root);
75924bbcf04SYan, Zheng 
76049b25e05SJeff Mahoney 	if (trans->aborted ||
7614e121c06SJosef Bacik 	    test_bit(BTRFS_FS_STATE_ERROR, &root->fs_info->fs_state)) {
7624e121c06SJosef Bacik 		wake_up_process(info->transaction_kthread);
7634edc2ca3SDave Jones 		err = -EIO;
7644e121c06SJosef Bacik 	}
765edf39272SJan Schmidt 	assert_qgroups_uptodate(trans);
76649b25e05SJeff Mahoney 
7674edc2ca3SDave Jones 	kmem_cache_free(btrfs_trans_handle_cachep, trans);
7684edc2ca3SDave Jones 	return err;
76979154b1bSChris Mason }
77079154b1bSChris Mason 
77189ce8a63SChris Mason int btrfs_end_transaction(struct btrfs_trans_handle *trans,
77289ce8a63SChris Mason 			  struct btrfs_root *root)
77389ce8a63SChris Mason {
77498ad43beSWang Shilong 	return __btrfs_end_transaction(trans, root, 0);
77589ce8a63SChris Mason }
77689ce8a63SChris Mason 
77789ce8a63SChris Mason int btrfs_end_transaction_throttle(struct btrfs_trans_handle *trans,
77889ce8a63SChris Mason 				   struct btrfs_root *root)
77989ce8a63SChris Mason {
78098ad43beSWang Shilong 	return __btrfs_end_transaction(trans, root, 1);
78116cdcec7SMiao Xie }
78216cdcec7SMiao Xie 
783d352ac68SChris Mason /*
784d352ac68SChris Mason  * when btree blocks are allocated, they have some corresponding bits set for
785d352ac68SChris Mason  * them in one of two extent_io trees.  This is used to make sure all of
786690587d1SChris Mason  * those extents are sent to disk but does not wait on them
787d352ac68SChris Mason  */
788690587d1SChris Mason int btrfs_write_marked_extents(struct btrfs_root *root,
7898cef4e16SYan, Zheng 			       struct extent_io_tree *dirty_pages, int mark)
79079154b1bSChris Mason {
791777e6bd7SChris Mason 	int err = 0;
7927c4452b9SChris Mason 	int werr = 0;
7931728366eSJosef Bacik 	struct address_space *mapping = root->fs_info->btree_inode->i_mapping;
794e6138876SJosef Bacik 	struct extent_state *cached_state = NULL;
795777e6bd7SChris Mason 	u64 start = 0;
7965f39d397SChris Mason 	u64 end;
7977c4452b9SChris Mason 
7981728366eSJosef Bacik 	while (!find_first_extent_bit(dirty_pages, start, &start, &end,
799e6138876SJosef Bacik 				      mark, &cached_state)) {
800e6138876SJosef Bacik 		convert_extent_bit(dirty_pages, start, end, EXTENT_NEED_WAIT,
801e6138876SJosef Bacik 				   mark, &cached_state, GFP_NOFS);
802e6138876SJosef Bacik 		cached_state = NULL;
8031728366eSJosef Bacik 		err = filemap_fdatawrite_range(mapping, start, end);
8047c4452b9SChris Mason 		if (err)
8057c4452b9SChris Mason 			werr = err;
8061728366eSJosef Bacik 		cond_resched();
8071728366eSJosef Bacik 		start = end + 1;
8087c4452b9SChris Mason 	}
809690587d1SChris Mason 	if (err)
810690587d1SChris Mason 		werr = err;
811690587d1SChris Mason 	return werr;
812690587d1SChris Mason }
813690587d1SChris Mason 
814690587d1SChris Mason /*
815690587d1SChris Mason  * when btree blocks are allocated, they have some corresponding bits set for
816690587d1SChris Mason  * them in one of two extent_io trees.  This is used to make sure all of
817690587d1SChris Mason  * those extents are on disk for transaction or log commit.  We wait
818690587d1SChris Mason  * on all the pages and clear them from the dirty pages state tree
819690587d1SChris Mason  */
820690587d1SChris Mason int btrfs_wait_marked_extents(struct btrfs_root *root,
8218cef4e16SYan, Zheng 			      struct extent_io_tree *dirty_pages, int mark)
822690587d1SChris Mason {
823690587d1SChris Mason 	int err = 0;
824690587d1SChris Mason 	int werr = 0;
8251728366eSJosef Bacik 	struct address_space *mapping = root->fs_info->btree_inode->i_mapping;
826e6138876SJosef Bacik 	struct extent_state *cached_state = NULL;
827690587d1SChris Mason 	u64 start = 0;
828690587d1SChris Mason 	u64 end;
829690587d1SChris Mason 
8301728366eSJosef Bacik 	while (!find_first_extent_bit(dirty_pages, start, &start, &end,
831e6138876SJosef Bacik 				      EXTENT_NEED_WAIT, &cached_state)) {
832e6138876SJosef Bacik 		clear_extent_bit(dirty_pages, start, end, EXTENT_NEED_WAIT,
833e6138876SJosef Bacik 				 0, 0, &cached_state, GFP_NOFS);
8341728366eSJosef Bacik 		err = filemap_fdatawait_range(mapping, start, end);
835777e6bd7SChris Mason 		if (err)
836777e6bd7SChris Mason 			werr = err;
837777e6bd7SChris Mason 		cond_resched();
8381728366eSJosef Bacik 		start = end + 1;
839777e6bd7SChris Mason 	}
8407c4452b9SChris Mason 	if (err)
8417c4452b9SChris Mason 		werr = err;
8427c4452b9SChris Mason 	return werr;
84379154b1bSChris Mason }
84479154b1bSChris Mason 
845690587d1SChris Mason /*
846690587d1SChris Mason  * when btree blocks are allocated, they have some corresponding bits set for
847690587d1SChris Mason  * them in one of two extent_io trees.  This is used to make sure all of
848690587d1SChris Mason  * those extents are on disk for transaction or log commit
849690587d1SChris Mason  */
850171170c1SSergei Trofimovich static int btrfs_write_and_wait_marked_extents(struct btrfs_root *root,
8518cef4e16SYan, Zheng 				struct extent_io_tree *dirty_pages, int mark)
852690587d1SChris Mason {
853690587d1SChris Mason 	int ret;
854690587d1SChris Mason 	int ret2;
855c6adc9ccSMiao Xie 	struct blk_plug plug;
856690587d1SChris Mason 
857c6adc9ccSMiao Xie 	blk_start_plug(&plug);
8588cef4e16SYan, Zheng 	ret = btrfs_write_marked_extents(root, dirty_pages, mark);
859c6adc9ccSMiao Xie 	blk_finish_plug(&plug);
8608cef4e16SYan, Zheng 	ret2 = btrfs_wait_marked_extents(root, dirty_pages, mark);
861bf0da8c1SChris Mason 
862bf0da8c1SChris Mason 	if (ret)
863bf0da8c1SChris Mason 		return ret;
864bf0da8c1SChris Mason 	if (ret2)
865bf0da8c1SChris Mason 		return ret2;
866bf0da8c1SChris Mason 	return 0;
867690587d1SChris Mason }
868690587d1SChris Mason 
869d0c803c4SChris Mason int btrfs_write_and_wait_transaction(struct btrfs_trans_handle *trans,
870d0c803c4SChris Mason 				     struct btrfs_root *root)
871d0c803c4SChris Mason {
872d0c803c4SChris Mason 	if (!trans || !trans->transaction) {
873d0c803c4SChris Mason 		struct inode *btree_inode;
874d0c803c4SChris Mason 		btree_inode = root->fs_info->btree_inode;
875d0c803c4SChris Mason 		return filemap_write_and_wait(btree_inode->i_mapping);
876d0c803c4SChris Mason 	}
877d0c803c4SChris Mason 	return btrfs_write_and_wait_marked_extents(root,
8788cef4e16SYan, Zheng 					   &trans->transaction->dirty_pages,
8798cef4e16SYan, Zheng 					   EXTENT_DIRTY);
880d0c803c4SChris Mason }
881d0c803c4SChris Mason 
882d352ac68SChris Mason /*
883d352ac68SChris Mason  * this is used to update the root pointer in the tree of tree roots.
884d352ac68SChris Mason  *
885d352ac68SChris Mason  * But, in the case of the extent allocation tree, updating the root
886d352ac68SChris Mason  * pointer may allocate blocks which may change the root of the extent
887d352ac68SChris Mason  * allocation tree.
888d352ac68SChris Mason  *
889d352ac68SChris Mason  * So, this loops and repeats and makes sure the cowonly root didn't
890d352ac68SChris Mason  * change while the root pointer was being updated in the metadata.
891d352ac68SChris Mason  */
8920b86a832SChris Mason static int update_cowonly_root(struct btrfs_trans_handle *trans,
89379154b1bSChris Mason 			       struct btrfs_root *root)
89479154b1bSChris Mason {
89579154b1bSChris Mason 	int ret;
8960b86a832SChris Mason 	u64 old_root_bytenr;
89786b9f2ecSYan, Zheng 	u64 old_root_used;
8980b86a832SChris Mason 	struct btrfs_root *tree_root = root->fs_info->tree_root;
89979154b1bSChris Mason 
90086b9f2ecSYan, Zheng 	old_root_used = btrfs_root_used(&root->root_item);
9010b86a832SChris Mason 	btrfs_write_dirty_block_groups(trans, root);
90256bec294SChris Mason 
90379154b1bSChris Mason 	while (1) {
9040b86a832SChris Mason 		old_root_bytenr = btrfs_root_bytenr(&root->root_item);
90586b9f2ecSYan, Zheng 		if (old_root_bytenr == root->node->start &&
90686b9f2ecSYan, Zheng 		    old_root_used == btrfs_root_used(&root->root_item))
90779154b1bSChris Mason 			break;
90887ef2bb4SChris Mason 
9095d4f98a2SYan Zheng 		btrfs_set_root_node(&root->root_item, root->node);
91079154b1bSChris Mason 		ret = btrfs_update_root(trans, tree_root,
9110b86a832SChris Mason 					&root->root_key,
9120b86a832SChris Mason 					&root->root_item);
91349b25e05SJeff Mahoney 		if (ret)
91449b25e05SJeff Mahoney 			return ret;
91556bec294SChris Mason 
91686b9f2ecSYan, Zheng 		old_root_used = btrfs_root_used(&root->root_item);
9174a8c9a62SYan Zheng 		ret = btrfs_write_dirty_block_groups(trans, root);
91849b25e05SJeff Mahoney 		if (ret)
91949b25e05SJeff Mahoney 			return ret;
9200b86a832SChris Mason 	}
921276e680dSYan Zheng 
922276e680dSYan Zheng 	if (root != root->fs_info->extent_root)
923817d52f8SJosef Bacik 		switch_commit_root(root);
924276e680dSYan Zheng 
9250b86a832SChris Mason 	return 0;
9260b86a832SChris Mason }
9270b86a832SChris Mason 
928d352ac68SChris Mason /*
929d352ac68SChris Mason  * update all the cowonly tree roots on disk
93049b25e05SJeff Mahoney  *
93149b25e05SJeff Mahoney  * The error handling in this function may not be obvious. Any of the
93249b25e05SJeff Mahoney  * failures will cause the file system to go offline. We still need
93349b25e05SJeff Mahoney  * to clean up the delayed refs.
934d352ac68SChris Mason  */
9355d4f98a2SYan Zheng static noinline int commit_cowonly_roots(struct btrfs_trans_handle *trans,
9360b86a832SChris Mason 					 struct btrfs_root *root)
9370b86a832SChris Mason {
9380b86a832SChris Mason 	struct btrfs_fs_info *fs_info = root->fs_info;
9390b86a832SChris Mason 	struct list_head *next;
94084234f3aSYan Zheng 	struct extent_buffer *eb;
94156bec294SChris Mason 	int ret;
94284234f3aSYan Zheng 
94356bec294SChris Mason 	ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
94449b25e05SJeff Mahoney 	if (ret)
94549b25e05SJeff Mahoney 		return ret;
94687ef2bb4SChris Mason 
94784234f3aSYan Zheng 	eb = btrfs_lock_root_node(fs_info->tree_root);
94849b25e05SJeff Mahoney 	ret = btrfs_cow_block(trans, fs_info->tree_root, eb, NULL,
94949b25e05SJeff Mahoney 			      0, &eb);
95084234f3aSYan Zheng 	btrfs_tree_unlock(eb);
95184234f3aSYan Zheng 	free_extent_buffer(eb);
9520b86a832SChris Mason 
95349b25e05SJeff Mahoney 	if (ret)
95449b25e05SJeff Mahoney 		return ret;
95549b25e05SJeff Mahoney 
95656bec294SChris Mason 	ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
95749b25e05SJeff Mahoney 	if (ret)
95849b25e05SJeff Mahoney 		return ret;
95987ef2bb4SChris Mason 
960733f4fbbSStefan Behrens 	ret = btrfs_run_dev_stats(trans, root->fs_info);
961c16ce190SJosef Bacik 	if (ret)
962c16ce190SJosef Bacik 		return ret;
9638dabb742SStefan Behrens 	ret = btrfs_run_dev_replace(trans, root->fs_info);
964c16ce190SJosef Bacik 	if (ret)
965c16ce190SJosef Bacik 		return ret;
966546adb0dSJan Schmidt 	ret = btrfs_run_qgroups(trans, root->fs_info);
967c16ce190SJosef Bacik 	if (ret)
968c16ce190SJosef Bacik 		return ret;
969546adb0dSJan Schmidt 
970546adb0dSJan Schmidt 	/* run_qgroups might have added some more refs */
971546adb0dSJan Schmidt 	ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
972c16ce190SJosef Bacik 	if (ret)
973c16ce190SJosef Bacik 		return ret;
974546adb0dSJan Schmidt 
9750b86a832SChris Mason 	while (!list_empty(&fs_info->dirty_cowonly_roots)) {
9760b86a832SChris Mason 		next = fs_info->dirty_cowonly_roots.next;
9770b86a832SChris Mason 		list_del_init(next);
9780b86a832SChris Mason 		root = list_entry(next, struct btrfs_root, dirty_list);
97987ef2bb4SChris Mason 
98049b25e05SJeff Mahoney 		ret = update_cowonly_root(trans, root);
98149b25e05SJeff Mahoney 		if (ret)
98249b25e05SJeff Mahoney 			return ret;
98379154b1bSChris Mason 	}
984276e680dSYan Zheng 
985276e680dSYan Zheng 	down_write(&fs_info->extent_commit_sem);
986276e680dSYan Zheng 	switch_commit_root(fs_info->extent_root);
987276e680dSYan Zheng 	up_write(&fs_info->extent_commit_sem);
988276e680dSYan Zheng 
9898dabb742SStefan Behrens 	btrfs_after_dev_replace_commit(fs_info);
9908dabb742SStefan Behrens 
99179154b1bSChris Mason 	return 0;
99279154b1bSChris Mason }
99379154b1bSChris Mason 
994d352ac68SChris Mason /*
995d352ac68SChris Mason  * dead roots are old snapshots that need to be deleted.  This allocates
996d352ac68SChris Mason  * a dirty root struct and adds it into the list of dead roots that need to
997d352ac68SChris Mason  * be deleted
998d352ac68SChris Mason  */
999cfad392bSJosef Bacik void btrfs_add_dead_root(struct btrfs_root *root)
10005eda7b5eSChris Mason {
1001a4abeea4SJosef Bacik 	spin_lock(&root->fs_info->trans_lock);
1002cfad392bSJosef Bacik 	if (list_empty(&root->root_list))
10039d1a2a3aSDavid Sterba 		list_add_tail(&root->root_list, &root->fs_info->dead_roots);
1004a4abeea4SJosef Bacik 	spin_unlock(&root->fs_info->trans_lock);
10055eda7b5eSChris Mason }
10065eda7b5eSChris Mason 
1007d352ac68SChris Mason /*
10085d4f98a2SYan Zheng  * update all the cowonly tree roots on disk
1009d352ac68SChris Mason  */
10105d4f98a2SYan Zheng static noinline int commit_fs_roots(struct btrfs_trans_handle *trans,
10115d4f98a2SYan Zheng 				    struct btrfs_root *root)
10120f7d52f4SChris Mason {
10130f7d52f4SChris Mason 	struct btrfs_root *gang[8];
10145d4f98a2SYan Zheng 	struct btrfs_fs_info *fs_info = root->fs_info;
10150f7d52f4SChris Mason 	int i;
10160f7d52f4SChris Mason 	int ret;
101754aa1f4dSChris Mason 	int err = 0;
101854aa1f4dSChris Mason 
1019a4abeea4SJosef Bacik 	spin_lock(&fs_info->fs_roots_radix_lock);
10200f7d52f4SChris Mason 	while (1) {
10215d4f98a2SYan Zheng 		ret = radix_tree_gang_lookup_tag(&fs_info->fs_roots_radix,
10225d4f98a2SYan Zheng 						 (void **)gang, 0,
10230f7d52f4SChris Mason 						 ARRAY_SIZE(gang),
10240f7d52f4SChris Mason 						 BTRFS_ROOT_TRANS_TAG);
10250f7d52f4SChris Mason 		if (ret == 0)
10260f7d52f4SChris Mason 			break;
10270f7d52f4SChris Mason 		for (i = 0; i < ret; i++) {
10280f7d52f4SChris Mason 			root = gang[i];
10295d4f98a2SYan Zheng 			radix_tree_tag_clear(&fs_info->fs_roots_radix,
10302619ba1fSChris Mason 					(unsigned long)root->root_key.objectid,
10310f7d52f4SChris Mason 					BTRFS_ROOT_TRANS_TAG);
1032a4abeea4SJosef Bacik 			spin_unlock(&fs_info->fs_roots_radix_lock);
103331153d81SYan Zheng 
1034e02119d5SChris Mason 			btrfs_free_log(trans, root);
10355d4f98a2SYan Zheng 			btrfs_update_reloc_root(trans, root);
1036d68fc57bSYan, Zheng 			btrfs_orphan_commit_root(trans, root);
1037e02119d5SChris Mason 
103882d5902dSLi Zefan 			btrfs_save_ino_cache(root, trans);
103982d5902dSLi Zefan 
1040f1ebcc74SLiu Bo 			/* see comments in should_cow_block() */
1041f1ebcc74SLiu Bo 			root->force_cow = 0;
1042f1ebcc74SLiu Bo 			smp_wmb();
1043f1ebcc74SLiu Bo 
1044978d910dSYan Zheng 			if (root->commit_root != root->node) {
1045581bb050SLi Zefan 				mutex_lock(&root->fs_commit_mutex);
1046817d52f8SJosef Bacik 				switch_commit_root(root);
1047581bb050SLi Zefan 				btrfs_unpin_free_ino(root);
1048581bb050SLi Zefan 				mutex_unlock(&root->fs_commit_mutex);
1049581bb050SLi Zefan 
1050978d910dSYan Zheng 				btrfs_set_root_node(&root->root_item,
1051978d910dSYan Zheng 						    root->node);
1052978d910dSYan Zheng 			}
105331153d81SYan Zheng 
10545d4f98a2SYan Zheng 			err = btrfs_update_root(trans, fs_info->tree_root,
10550f7d52f4SChris Mason 						&root->root_key,
10560f7d52f4SChris Mason 						&root->root_item);
1057a4abeea4SJosef Bacik 			spin_lock(&fs_info->fs_roots_radix_lock);
105854aa1f4dSChris Mason 			if (err)
105954aa1f4dSChris Mason 				break;
10600f7d52f4SChris Mason 		}
10619f3a7427SChris Mason 	}
1062a4abeea4SJosef Bacik 	spin_unlock(&fs_info->fs_roots_radix_lock);
106354aa1f4dSChris Mason 	return err;
10640f7d52f4SChris Mason }
10650f7d52f4SChris Mason 
1066d352ac68SChris Mason /*
1067de78b51aSEric Sandeen  * defrag a given btree.
1068de78b51aSEric Sandeen  * Every leaf in the btree is read and defragged.
1069d352ac68SChris Mason  */
1070de78b51aSEric Sandeen int btrfs_defrag_root(struct btrfs_root *root)
1071e9d0b13bSChris Mason {
1072e9d0b13bSChris Mason 	struct btrfs_fs_info *info = root->fs_info;
1073e9d0b13bSChris Mason 	struct btrfs_trans_handle *trans;
10748929ecfaSYan, Zheng 	int ret;
1075e9d0b13bSChris Mason 
10768929ecfaSYan, Zheng 	if (xchg(&root->defrag_running, 1))
1077e9d0b13bSChris Mason 		return 0;
10788929ecfaSYan, Zheng 
10796b80053dSChris Mason 	while (1) {
10808929ecfaSYan, Zheng 		trans = btrfs_start_transaction(root, 0);
10818929ecfaSYan, Zheng 		if (IS_ERR(trans))
10828929ecfaSYan, Zheng 			return PTR_ERR(trans);
10838929ecfaSYan, Zheng 
1084de78b51aSEric Sandeen 		ret = btrfs_defrag_leaves(trans, root);
10858929ecfaSYan, Zheng 
1086e9d0b13bSChris Mason 		btrfs_end_transaction(trans, root);
1087b53d3f5dSLiu Bo 		btrfs_btree_balance_dirty(info->tree_root);
1088e9d0b13bSChris Mason 		cond_resched();
1089e9d0b13bSChris Mason 
10907841cb28SDavid Sterba 		if (btrfs_fs_closing(root->fs_info) || ret != -EAGAIN)
1091e9d0b13bSChris Mason 			break;
1092210549ebSDavid Sterba 
1093210549ebSDavid Sterba 		if (btrfs_defrag_cancelled(root->fs_info)) {
1094efe120a0SFrank Holton 			pr_debug("BTRFS: defrag_root cancelled\n");
1095210549ebSDavid Sterba 			ret = -EAGAIN;
1096210549ebSDavid Sterba 			break;
1097210549ebSDavid Sterba 		}
1098e9d0b13bSChris Mason 	}
1099e9d0b13bSChris Mason 	root->defrag_running = 0;
11008929ecfaSYan, Zheng 	return ret;
1101e9d0b13bSChris Mason }
1102e9d0b13bSChris Mason 
1103d352ac68SChris Mason /*
1104d352ac68SChris Mason  * new snapshots need to be created at a very specific time in the
1105aec8030aSMiao Xie  * transaction commit.  This does the actual creation.
1106aec8030aSMiao Xie  *
1107aec8030aSMiao Xie  * Note:
1108aec8030aSMiao Xie  * If the error which may affect the commitment of the current transaction
1109aec8030aSMiao Xie  * happens, we should return the error number. If the error which just affect
1110aec8030aSMiao Xie  * the creation of the pending snapshots, just return 0.
1111d352ac68SChris Mason  */
111280b6794dSChris Mason static noinline int create_pending_snapshot(struct btrfs_trans_handle *trans,
11133063d29fSChris Mason 				   struct btrfs_fs_info *fs_info,
11143063d29fSChris Mason 				   struct btrfs_pending_snapshot *pending)
11153063d29fSChris Mason {
11163063d29fSChris Mason 	struct btrfs_key key;
111780b6794dSChris Mason 	struct btrfs_root_item *new_root_item;
11183063d29fSChris Mason 	struct btrfs_root *tree_root = fs_info->tree_root;
11193063d29fSChris Mason 	struct btrfs_root *root = pending->root;
11206bdb72deSSage Weil 	struct btrfs_root *parent_root;
112198c9942aSLiu Bo 	struct btrfs_block_rsv *rsv;
11226bdb72deSSage Weil 	struct inode *parent_inode;
112342874b3dSMiao Xie 	struct btrfs_path *path;
112442874b3dSMiao Xie 	struct btrfs_dir_item *dir_item;
1125a22285a6SYan, Zheng 	struct dentry *dentry;
11263063d29fSChris Mason 	struct extent_buffer *tmp;
1127925baeddSChris Mason 	struct extent_buffer *old;
11288ea05e3aSAlexander Block 	struct timespec cur_time = CURRENT_TIME;
1129aec8030aSMiao Xie 	int ret = 0;
1130d68fc57bSYan, Zheng 	u64 to_reserve = 0;
11316bdb72deSSage Weil 	u64 index = 0;
1132a22285a6SYan, Zheng 	u64 objectid;
1133b83cc969SLi Zefan 	u64 root_flags;
11348ea05e3aSAlexander Block 	uuid_le new_uuid;
11353063d29fSChris Mason 
113642874b3dSMiao Xie 	path = btrfs_alloc_path();
113742874b3dSMiao Xie 	if (!path) {
1138aec8030aSMiao Xie 		pending->error = -ENOMEM;
1139aec8030aSMiao Xie 		return 0;
114042874b3dSMiao Xie 	}
114142874b3dSMiao Xie 
114280b6794dSChris Mason 	new_root_item = kmalloc(sizeof(*new_root_item), GFP_NOFS);
114380b6794dSChris Mason 	if (!new_root_item) {
1144aec8030aSMiao Xie 		pending->error = -ENOMEM;
11456fa9700eSMiao Xie 		goto root_item_alloc_fail;
114680b6794dSChris Mason 	}
1147a22285a6SYan, Zheng 
1148aec8030aSMiao Xie 	pending->error = btrfs_find_free_objectid(tree_root, &objectid);
1149aec8030aSMiao Xie 	if (pending->error)
11506fa9700eSMiao Xie 		goto no_free_objectid;
11513063d29fSChris Mason 
11523fd0a558SYan, Zheng 	btrfs_reloc_pre_snapshot(trans, pending, &to_reserve);
1153d68fc57bSYan, Zheng 
1154d68fc57bSYan, Zheng 	if (to_reserve > 0) {
1155aec8030aSMiao Xie 		pending->error = btrfs_block_rsv_add(root,
1156aec8030aSMiao Xie 						     &pending->block_rsv,
115708e007d2SMiao Xie 						     to_reserve,
115808e007d2SMiao Xie 						     BTRFS_RESERVE_NO_FLUSH);
1159aec8030aSMiao Xie 		if (pending->error)
11606fa9700eSMiao Xie 			goto no_free_objectid;
1161d68fc57bSYan, Zheng 	}
1162d68fc57bSYan, Zheng 
1163aec8030aSMiao Xie 	pending->error = btrfs_qgroup_inherit(trans, fs_info,
1164aec8030aSMiao Xie 					      root->root_key.objectid,
11656f72c7e2SArne Jansen 					      objectid, pending->inherit);
1166aec8030aSMiao Xie 	if (pending->error)
11676fa9700eSMiao Xie 		goto no_free_objectid;
11686f72c7e2SArne Jansen 
11693063d29fSChris Mason 	key.objectid = objectid;
1170a22285a6SYan, Zheng 	key.offset = (u64)-1;
1171a22285a6SYan, Zheng 	key.type = BTRFS_ROOT_ITEM_KEY;
11723063d29fSChris Mason 
11736fa9700eSMiao Xie 	rsv = trans->block_rsv;
1174a22285a6SYan, Zheng 	trans->block_rsv = &pending->block_rsv;
11752382c5ccSLiu Bo 	trans->bytes_reserved = trans->block_rsv->reserved;
11766bdb72deSSage Weil 
1177a22285a6SYan, Zheng 	dentry = pending->dentry;
1178e9662f70SMiao Xie 	parent_inode = pending->dir;
1179a22285a6SYan, Zheng 	parent_root = BTRFS_I(parent_inode)->root;
11807585717fSChris Mason 	record_root_in_trans(trans, parent_root);
1181a22285a6SYan, Zheng 
11826bdb72deSSage Weil 	/*
11836bdb72deSSage Weil 	 * insert the directory item
11846bdb72deSSage Weil 	 */
11856bdb72deSSage Weil 	ret = btrfs_set_inode_index(parent_inode, &index);
118649b25e05SJeff Mahoney 	BUG_ON(ret); /* -ENOMEM */
118742874b3dSMiao Xie 
118842874b3dSMiao Xie 	/* check if there is a file/dir which has the same name. */
118942874b3dSMiao Xie 	dir_item = btrfs_lookup_dir_item(NULL, parent_root, path,
119042874b3dSMiao Xie 					 btrfs_ino(parent_inode),
119142874b3dSMiao Xie 					 dentry->d_name.name,
119242874b3dSMiao Xie 					 dentry->d_name.len, 0);
119342874b3dSMiao Xie 	if (dir_item != NULL && !IS_ERR(dir_item)) {
1194fe66a05aSChris Mason 		pending->error = -EEXIST;
1195aec8030aSMiao Xie 		goto dir_item_existed;
119642874b3dSMiao Xie 	} else if (IS_ERR(dir_item)) {
119742874b3dSMiao Xie 		ret = PTR_ERR(dir_item);
11988732d44fSMiao Xie 		btrfs_abort_transaction(trans, root, ret);
11998732d44fSMiao Xie 		goto fail;
120079787eaaSJeff Mahoney 	}
120142874b3dSMiao Xie 	btrfs_release_path(path);
12026bdb72deSSage Weil 
1203e999376fSChris Mason 	/*
1204e999376fSChris Mason 	 * pull in the delayed directory update
1205e999376fSChris Mason 	 * and the delayed inode item
1206e999376fSChris Mason 	 * otherwise we corrupt the FS during
1207e999376fSChris Mason 	 * snapshot
1208e999376fSChris Mason 	 */
1209e999376fSChris Mason 	ret = btrfs_run_delayed_items(trans, root);
12108732d44fSMiao Xie 	if (ret) {	/* Transaction aborted */
12118732d44fSMiao Xie 		btrfs_abort_transaction(trans, root, ret);
12128732d44fSMiao Xie 		goto fail;
12138732d44fSMiao Xie 	}
1214e999376fSChris Mason 
12157585717fSChris Mason 	record_root_in_trans(trans, root);
12166bdb72deSSage Weil 	btrfs_set_root_last_snapshot(&root->root_item, trans->transid);
12176bdb72deSSage Weil 	memcpy(new_root_item, &root->root_item, sizeof(*new_root_item));
121808fe4db1SLi Zefan 	btrfs_check_and_init_root_item(new_root_item);
12196bdb72deSSage Weil 
1220b83cc969SLi Zefan 	root_flags = btrfs_root_flags(new_root_item);
1221b83cc969SLi Zefan 	if (pending->readonly)
1222b83cc969SLi Zefan 		root_flags |= BTRFS_ROOT_SUBVOL_RDONLY;
1223b83cc969SLi Zefan 	else
1224b83cc969SLi Zefan 		root_flags &= ~BTRFS_ROOT_SUBVOL_RDONLY;
1225b83cc969SLi Zefan 	btrfs_set_root_flags(new_root_item, root_flags);
1226b83cc969SLi Zefan 
12278ea05e3aSAlexander Block 	btrfs_set_root_generation_v2(new_root_item,
12288ea05e3aSAlexander Block 			trans->transid);
12298ea05e3aSAlexander Block 	uuid_le_gen(&new_uuid);
12308ea05e3aSAlexander Block 	memcpy(new_root_item->uuid, new_uuid.b, BTRFS_UUID_SIZE);
12318ea05e3aSAlexander Block 	memcpy(new_root_item->parent_uuid, root->root_item.uuid,
12328ea05e3aSAlexander Block 			BTRFS_UUID_SIZE);
123370023da2SStefan Behrens 	if (!(root_flags & BTRFS_ROOT_SUBVOL_RDONLY)) {
123470023da2SStefan Behrens 		memset(new_root_item->received_uuid, 0,
123570023da2SStefan Behrens 		       sizeof(new_root_item->received_uuid));
12368ea05e3aSAlexander Block 		memset(&new_root_item->stime, 0, sizeof(new_root_item->stime));
12378ea05e3aSAlexander Block 		memset(&new_root_item->rtime, 0, sizeof(new_root_item->rtime));
12388ea05e3aSAlexander Block 		btrfs_set_root_stransid(new_root_item, 0);
12398ea05e3aSAlexander Block 		btrfs_set_root_rtransid(new_root_item, 0);
124070023da2SStefan Behrens 	}
12413cae210fSQu Wenruo 	btrfs_set_stack_timespec_sec(&new_root_item->otime, cur_time.tv_sec);
12423cae210fSQu Wenruo 	btrfs_set_stack_timespec_nsec(&new_root_item->otime, cur_time.tv_nsec);
124370023da2SStefan Behrens 	btrfs_set_root_otransid(new_root_item, trans->transid);
12448ea05e3aSAlexander Block 
1245925baeddSChris Mason 	old = btrfs_lock_root_node(root);
124649b25e05SJeff Mahoney 	ret = btrfs_cow_block(trans, root, old, NULL, 0, &old);
124779787eaaSJeff Mahoney 	if (ret) {
124879787eaaSJeff Mahoney 		btrfs_tree_unlock(old);
124979787eaaSJeff Mahoney 		free_extent_buffer(old);
12508732d44fSMiao Xie 		btrfs_abort_transaction(trans, root, ret);
12518732d44fSMiao Xie 		goto fail;
125279787eaaSJeff Mahoney 	}
125349b25e05SJeff Mahoney 
12545d4f98a2SYan Zheng 	btrfs_set_lock_blocking(old);
12553063d29fSChris Mason 
125649b25e05SJeff Mahoney 	ret = btrfs_copy_root(trans, root, old, &tmp, objectid);
125779787eaaSJeff Mahoney 	/* clean up in any case */
1258925baeddSChris Mason 	btrfs_tree_unlock(old);
1259925baeddSChris Mason 	free_extent_buffer(old);
12608732d44fSMiao Xie 	if (ret) {
12618732d44fSMiao Xie 		btrfs_abort_transaction(trans, root, ret);
12628732d44fSMiao Xie 		goto fail;
12638732d44fSMiao Xie 	}
12643063d29fSChris Mason 
1265f1ebcc74SLiu Bo 	/* see comments in should_cow_block() */
1266f1ebcc74SLiu Bo 	root->force_cow = 1;
1267f1ebcc74SLiu Bo 	smp_wmb();
1268f1ebcc74SLiu Bo 
12695d4f98a2SYan Zheng 	btrfs_set_root_node(new_root_item, tmp);
1270a22285a6SYan, Zheng 	/* record when the snapshot was created in key.offset */
1271a22285a6SYan, Zheng 	key.offset = trans->transid;
1272a22285a6SYan, Zheng 	ret = btrfs_insert_root(trans, tree_root, &key, new_root_item);
1273925baeddSChris Mason 	btrfs_tree_unlock(tmp);
12743063d29fSChris Mason 	free_extent_buffer(tmp);
12758732d44fSMiao Xie 	if (ret) {
12768732d44fSMiao Xie 		btrfs_abort_transaction(trans, root, ret);
12778732d44fSMiao Xie 		goto fail;
12788732d44fSMiao Xie 	}
12790660b5afSChris Mason 
1280a22285a6SYan, Zheng 	/*
1281a22285a6SYan, Zheng 	 * insert root back/forward references
1282a22285a6SYan, Zheng 	 */
1283a22285a6SYan, Zheng 	ret = btrfs_add_root_ref(trans, tree_root, objectid,
1284a22285a6SYan, Zheng 				 parent_root->root_key.objectid,
128533345d01SLi Zefan 				 btrfs_ino(parent_inode), index,
1286a22285a6SYan, Zheng 				 dentry->d_name.name, dentry->d_name.len);
12878732d44fSMiao Xie 	if (ret) {
12888732d44fSMiao Xie 		btrfs_abort_transaction(trans, root, ret);
12898732d44fSMiao Xie 		goto fail;
12908732d44fSMiao Xie 	}
1291a22285a6SYan, Zheng 
1292a22285a6SYan, Zheng 	key.offset = (u64)-1;
1293a22285a6SYan, Zheng 	pending->snap = btrfs_read_fs_root_no_name(root->fs_info, &key);
129479787eaaSJeff Mahoney 	if (IS_ERR(pending->snap)) {
129579787eaaSJeff Mahoney 		ret = PTR_ERR(pending->snap);
12968732d44fSMiao Xie 		btrfs_abort_transaction(trans, root, ret);
12978732d44fSMiao Xie 		goto fail;
129879787eaaSJeff Mahoney 	}
1299d68fc57bSYan, Zheng 
130049b25e05SJeff Mahoney 	ret = btrfs_reloc_post_snapshot(trans, pending);
13018732d44fSMiao Xie 	if (ret) {
13028732d44fSMiao Xie 		btrfs_abort_transaction(trans, root, ret);
13038732d44fSMiao Xie 		goto fail;
13048732d44fSMiao Xie 	}
1305361048f5SMiao Xie 
1306361048f5SMiao Xie 	ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
13078732d44fSMiao Xie 	if (ret) {
13088732d44fSMiao Xie 		btrfs_abort_transaction(trans, root, ret);
13098732d44fSMiao Xie 		goto fail;
13108732d44fSMiao Xie 	}
131142874b3dSMiao Xie 
131242874b3dSMiao Xie 	ret = btrfs_insert_dir_item(trans, parent_root,
131342874b3dSMiao Xie 				    dentry->d_name.name, dentry->d_name.len,
131442874b3dSMiao Xie 				    parent_inode, &key,
131542874b3dSMiao Xie 				    BTRFS_FT_DIR, index);
131642874b3dSMiao Xie 	/* We have check then name at the beginning, so it is impossible. */
13179c52057cSChris Mason 	BUG_ON(ret == -EEXIST || ret == -EOVERFLOW);
13188732d44fSMiao Xie 	if (ret) {
13198732d44fSMiao Xie 		btrfs_abort_transaction(trans, root, ret);
13208732d44fSMiao Xie 		goto fail;
13218732d44fSMiao Xie 	}
132242874b3dSMiao Xie 
132342874b3dSMiao Xie 	btrfs_i_size_write(parent_inode, parent_inode->i_size +
132442874b3dSMiao Xie 					 dentry->d_name.len * 2);
132542874b3dSMiao Xie 	parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
1326be6aef60SJosef Bacik 	ret = btrfs_update_inode_fallback(trans, parent_root, parent_inode);
1327dd5f9615SStefan Behrens 	if (ret) {
13288732d44fSMiao Xie 		btrfs_abort_transaction(trans, root, ret);
1329dd5f9615SStefan Behrens 		goto fail;
1330dd5f9615SStefan Behrens 	}
1331dd5f9615SStefan Behrens 	ret = btrfs_uuid_tree_add(trans, fs_info->uuid_root, new_uuid.b,
1332dd5f9615SStefan Behrens 				  BTRFS_UUID_KEY_SUBVOL, objectid);
1333dd5f9615SStefan Behrens 	if (ret) {
1334dd5f9615SStefan Behrens 		btrfs_abort_transaction(trans, root, ret);
1335dd5f9615SStefan Behrens 		goto fail;
1336dd5f9615SStefan Behrens 	}
1337dd5f9615SStefan Behrens 	if (!btrfs_is_empty_uuid(new_root_item->received_uuid)) {
1338dd5f9615SStefan Behrens 		ret = btrfs_uuid_tree_add(trans, fs_info->uuid_root,
1339dd5f9615SStefan Behrens 					  new_root_item->received_uuid,
1340dd5f9615SStefan Behrens 					  BTRFS_UUID_KEY_RECEIVED_SUBVOL,
1341dd5f9615SStefan Behrens 					  objectid);
1342dd5f9615SStefan Behrens 		if (ret && ret != -EEXIST) {
1343dd5f9615SStefan Behrens 			btrfs_abort_transaction(trans, root, ret);
1344dd5f9615SStefan Behrens 			goto fail;
1345dd5f9615SStefan Behrens 		}
1346dd5f9615SStefan Behrens 	}
13473063d29fSChris Mason fail:
1348aec8030aSMiao Xie 	pending->error = ret;
1349aec8030aSMiao Xie dir_item_existed:
135098c9942aSLiu Bo 	trans->block_rsv = rsv;
13512382c5ccSLiu Bo 	trans->bytes_reserved = 0;
13526fa9700eSMiao Xie no_free_objectid:
13536fa9700eSMiao Xie 	kfree(new_root_item);
13546fa9700eSMiao Xie root_item_alloc_fail:
135542874b3dSMiao Xie 	btrfs_free_path(path);
135649b25e05SJeff Mahoney 	return ret;
13573063d29fSChris Mason }
13583063d29fSChris Mason 
1359d352ac68SChris Mason /*
1360d352ac68SChris Mason  * create all the snapshots we've scheduled for creation
1361d352ac68SChris Mason  */
136280b6794dSChris Mason static noinline int create_pending_snapshots(struct btrfs_trans_handle *trans,
13633063d29fSChris Mason 					     struct btrfs_fs_info *fs_info)
13643063d29fSChris Mason {
1365aec8030aSMiao Xie 	struct btrfs_pending_snapshot *pending, *next;
13663063d29fSChris Mason 	struct list_head *head = &trans->transaction->pending_snapshots;
1367aec8030aSMiao Xie 	int ret = 0;
13683de4586cSChris Mason 
1369aec8030aSMiao Xie 	list_for_each_entry_safe(pending, next, head, list) {
1370aec8030aSMiao Xie 		list_del(&pending->list);
1371aec8030aSMiao Xie 		ret = create_pending_snapshot(trans, fs_info, pending);
1372aec8030aSMiao Xie 		if (ret)
1373aec8030aSMiao Xie 			break;
1374aec8030aSMiao Xie 	}
1375aec8030aSMiao Xie 	return ret;
13763de4586cSChris Mason }
13773de4586cSChris Mason 
13785d4f98a2SYan Zheng static void update_super_roots(struct btrfs_root *root)
13795d4f98a2SYan Zheng {
13805d4f98a2SYan Zheng 	struct btrfs_root_item *root_item;
13815d4f98a2SYan Zheng 	struct btrfs_super_block *super;
13825d4f98a2SYan Zheng 
13836c41761fSDavid Sterba 	super = root->fs_info->super_copy;
13845d4f98a2SYan Zheng 
13855d4f98a2SYan Zheng 	root_item = &root->fs_info->chunk_root->root_item;
13865d4f98a2SYan Zheng 	super->chunk_root = root_item->bytenr;
13875d4f98a2SYan Zheng 	super->chunk_root_generation = root_item->generation;
13885d4f98a2SYan Zheng 	super->chunk_root_level = root_item->level;
13895d4f98a2SYan Zheng 
13905d4f98a2SYan Zheng 	root_item = &root->fs_info->tree_root->root_item;
13915d4f98a2SYan Zheng 	super->root = root_item->bytenr;
13925d4f98a2SYan Zheng 	super->generation = root_item->generation;
13935d4f98a2SYan Zheng 	super->root_level = root_item->level;
139473bc1876SJosef Bacik 	if (btrfs_test_opt(root, SPACE_CACHE))
13950af3d00bSJosef Bacik 		super->cache_generation = root_item->generation;
139670f80175SStefan Behrens 	if (root->fs_info->update_uuid_tree_gen)
139726432799SStefan Behrens 		super->uuid_tree_generation = root_item->generation;
13985d4f98a2SYan Zheng }
13995d4f98a2SYan Zheng 
1400f36f3042SChris Mason int btrfs_transaction_in_commit(struct btrfs_fs_info *info)
1401f36f3042SChris Mason {
14024a9d8bdeSMiao Xie 	struct btrfs_transaction *trans;
1403f36f3042SChris Mason 	int ret = 0;
14044a9d8bdeSMiao Xie 
1405a4abeea4SJosef Bacik 	spin_lock(&info->trans_lock);
14064a9d8bdeSMiao Xie 	trans = info->running_transaction;
14074a9d8bdeSMiao Xie 	if (trans)
14084a9d8bdeSMiao Xie 		ret = (trans->state >= TRANS_STATE_COMMIT_START);
1409a4abeea4SJosef Bacik 	spin_unlock(&info->trans_lock);
1410f36f3042SChris Mason 	return ret;
1411f36f3042SChris Mason }
1412f36f3042SChris Mason 
14138929ecfaSYan, Zheng int btrfs_transaction_blocked(struct btrfs_fs_info *info)
14148929ecfaSYan, Zheng {
14154a9d8bdeSMiao Xie 	struct btrfs_transaction *trans;
14168929ecfaSYan, Zheng 	int ret = 0;
14174a9d8bdeSMiao Xie 
1418a4abeea4SJosef Bacik 	spin_lock(&info->trans_lock);
14194a9d8bdeSMiao Xie 	trans = info->running_transaction;
14204a9d8bdeSMiao Xie 	if (trans)
14214a9d8bdeSMiao Xie 		ret = is_transaction_blocked(trans);
1422a4abeea4SJosef Bacik 	spin_unlock(&info->trans_lock);
14238929ecfaSYan, Zheng 	return ret;
14248929ecfaSYan, Zheng }
14258929ecfaSYan, Zheng 
1426bb9c12c9SSage Weil /*
1427bb9c12c9SSage Weil  * wait for the current transaction commit to start and block subsequent
1428bb9c12c9SSage Weil  * transaction joins
1429bb9c12c9SSage Weil  */
1430bb9c12c9SSage Weil static void wait_current_trans_commit_start(struct btrfs_root *root,
1431bb9c12c9SSage Weil 					    struct btrfs_transaction *trans)
1432bb9c12c9SSage Weil {
14334a9d8bdeSMiao Xie 	wait_event(root->fs_info->transaction_blocked_wait,
1434501407aaSJosef Bacik 		   trans->state >= TRANS_STATE_COMMIT_START ||
1435501407aaSJosef Bacik 		   trans->aborted);
1436bb9c12c9SSage Weil }
1437bb9c12c9SSage Weil 
1438bb9c12c9SSage Weil /*
1439bb9c12c9SSage Weil  * wait for the current transaction to start and then become unblocked.
1440bb9c12c9SSage Weil  * caller holds ref.
1441bb9c12c9SSage Weil  */
1442bb9c12c9SSage Weil static void wait_current_trans_commit_start_and_unblock(struct btrfs_root *root,
1443bb9c12c9SSage Weil 					 struct btrfs_transaction *trans)
1444bb9c12c9SSage Weil {
144572d63ed6SLi Zefan 	wait_event(root->fs_info->transaction_wait,
1446501407aaSJosef Bacik 		   trans->state >= TRANS_STATE_UNBLOCKED ||
1447501407aaSJosef Bacik 		   trans->aborted);
1448bb9c12c9SSage Weil }
1449bb9c12c9SSage Weil 
1450bb9c12c9SSage Weil /*
1451bb9c12c9SSage Weil  * commit transactions asynchronously. once btrfs_commit_transaction_async
1452bb9c12c9SSage Weil  * returns, any subsequent transaction will not be allowed to join.
1453bb9c12c9SSage Weil  */
1454bb9c12c9SSage Weil struct btrfs_async_commit {
1455bb9c12c9SSage Weil 	struct btrfs_trans_handle *newtrans;
1456bb9c12c9SSage Weil 	struct btrfs_root *root;
14577892b5afSMiao Xie 	struct work_struct work;
1458bb9c12c9SSage Weil };
1459bb9c12c9SSage Weil 
1460bb9c12c9SSage Weil static void do_async_commit(struct work_struct *work)
1461bb9c12c9SSage Weil {
1462bb9c12c9SSage Weil 	struct btrfs_async_commit *ac =
14637892b5afSMiao Xie 		container_of(work, struct btrfs_async_commit, work);
1464bb9c12c9SSage Weil 
14656fc4e354SSage Weil 	/*
14666fc4e354SSage Weil 	 * We've got freeze protection passed with the transaction.
14676fc4e354SSage Weil 	 * Tell lockdep about it.
14686fc4e354SSage Weil 	 */
1469b1a06a4bSLiu Bo 	if (ac->newtrans->type & __TRANS_FREEZABLE)
14706fc4e354SSage Weil 		rwsem_acquire_read(
14716fc4e354SSage Weil 		     &ac->root->fs_info->sb->s_writers.lock_map[SB_FREEZE_FS-1],
14726fc4e354SSage Weil 		     0, 1, _THIS_IP_);
14736fc4e354SSage Weil 
1474e209db7aSSage Weil 	current->journal_info = ac->newtrans;
1475e209db7aSSage Weil 
1476bb9c12c9SSage Weil 	btrfs_commit_transaction(ac->newtrans, ac->root);
1477bb9c12c9SSage Weil 	kfree(ac);
1478bb9c12c9SSage Weil }
1479bb9c12c9SSage Weil 
1480bb9c12c9SSage Weil int btrfs_commit_transaction_async(struct btrfs_trans_handle *trans,
1481bb9c12c9SSage Weil 				   struct btrfs_root *root,
1482bb9c12c9SSage Weil 				   int wait_for_unblock)
1483bb9c12c9SSage Weil {
1484bb9c12c9SSage Weil 	struct btrfs_async_commit *ac;
1485bb9c12c9SSage Weil 	struct btrfs_transaction *cur_trans;
1486bb9c12c9SSage Weil 
1487bb9c12c9SSage Weil 	ac = kmalloc(sizeof(*ac), GFP_NOFS);
1488db5b493aSTsutomu Itoh 	if (!ac)
1489db5b493aSTsutomu Itoh 		return -ENOMEM;
1490bb9c12c9SSage Weil 
14917892b5afSMiao Xie 	INIT_WORK(&ac->work, do_async_commit);
1492bb9c12c9SSage Weil 	ac->root = root;
14937a7eaa40SJosef Bacik 	ac->newtrans = btrfs_join_transaction(root);
14943612b495STsutomu Itoh 	if (IS_ERR(ac->newtrans)) {
14953612b495STsutomu Itoh 		int err = PTR_ERR(ac->newtrans);
14963612b495STsutomu Itoh 		kfree(ac);
14973612b495STsutomu Itoh 		return err;
14983612b495STsutomu Itoh 	}
1499bb9c12c9SSage Weil 
1500bb9c12c9SSage Weil 	/* take transaction reference */
1501bb9c12c9SSage Weil 	cur_trans = trans->transaction;
150213c5a93eSJosef Bacik 	atomic_inc(&cur_trans->use_count);
1503bb9c12c9SSage Weil 
1504bb9c12c9SSage Weil 	btrfs_end_transaction(trans, root);
15056fc4e354SSage Weil 
15066fc4e354SSage Weil 	/*
15076fc4e354SSage Weil 	 * Tell lockdep we've released the freeze rwsem, since the
15086fc4e354SSage Weil 	 * async commit thread will be the one to unlock it.
15096fc4e354SSage Weil 	 */
1510b1a06a4bSLiu Bo 	if (ac->newtrans->type & __TRANS_FREEZABLE)
1511ff7c1d33SMiao Xie 		rwsem_release(
1512ff7c1d33SMiao Xie 			&root->fs_info->sb->s_writers.lock_map[SB_FREEZE_FS-1],
15136fc4e354SSage Weil 			1, _THIS_IP_);
15146fc4e354SSage Weil 
15157892b5afSMiao Xie 	schedule_work(&ac->work);
1516bb9c12c9SSage Weil 
1517bb9c12c9SSage Weil 	/* wait for transaction to start and unblock */
1518bb9c12c9SSage Weil 	if (wait_for_unblock)
1519bb9c12c9SSage Weil 		wait_current_trans_commit_start_and_unblock(root, cur_trans);
1520bb9c12c9SSage Weil 	else
1521bb9c12c9SSage Weil 		wait_current_trans_commit_start(root, cur_trans);
1522bb9c12c9SSage Weil 
152338e88054SSage Weil 	if (current->journal_info == trans)
152438e88054SSage Weil 		current->journal_info = NULL;
152538e88054SSage Weil 
1526724e2315SJosef Bacik 	btrfs_put_transaction(cur_trans);
1527bb9c12c9SSage Weil 	return 0;
1528bb9c12c9SSage Weil }
1529bb9c12c9SSage Weil 
153049b25e05SJeff Mahoney 
153149b25e05SJeff Mahoney static void cleanup_transaction(struct btrfs_trans_handle *trans,
15327b8b92afSJosef Bacik 				struct btrfs_root *root, int err)
153349b25e05SJeff Mahoney {
153449b25e05SJeff Mahoney 	struct btrfs_transaction *cur_trans = trans->transaction;
1535f094ac32SLiu Bo 	DEFINE_WAIT(wait);
153649b25e05SJeff Mahoney 
153749b25e05SJeff Mahoney 	WARN_ON(trans->use_count > 1);
153849b25e05SJeff Mahoney 
15397b8b92afSJosef Bacik 	btrfs_abort_transaction(trans, root, err);
15407b8b92afSJosef Bacik 
154149b25e05SJeff Mahoney 	spin_lock(&root->fs_info->trans_lock);
154266b6135bSLiu Bo 
154325d8c284SMiao Xie 	/*
154425d8c284SMiao Xie 	 * If the transaction is removed from the list, it means this
154525d8c284SMiao Xie 	 * transaction has been committed successfully, so it is impossible
154625d8c284SMiao Xie 	 * to call the cleanup function.
154725d8c284SMiao Xie 	 */
154825d8c284SMiao Xie 	BUG_ON(list_empty(&cur_trans->list));
154966b6135bSLiu Bo 
155049b25e05SJeff Mahoney 	list_del_init(&cur_trans->list);
1551d7096fc3SJosef Bacik 	if (cur_trans == root->fs_info->running_transaction) {
15524a9d8bdeSMiao Xie 		cur_trans->state = TRANS_STATE_COMMIT_DOING;
1553f094ac32SLiu Bo 		spin_unlock(&root->fs_info->trans_lock);
1554f094ac32SLiu Bo 		wait_event(cur_trans->writer_wait,
1555f094ac32SLiu Bo 			   atomic_read(&cur_trans->num_writers) == 1);
1556f094ac32SLiu Bo 
1557f094ac32SLiu Bo 		spin_lock(&root->fs_info->trans_lock);
1558d7096fc3SJosef Bacik 	}
155949b25e05SJeff Mahoney 	spin_unlock(&root->fs_info->trans_lock);
156049b25e05SJeff Mahoney 
156149b25e05SJeff Mahoney 	btrfs_cleanup_one_transaction(trans->transaction, root);
156249b25e05SJeff Mahoney 
15634a9d8bdeSMiao Xie 	spin_lock(&root->fs_info->trans_lock);
15644a9d8bdeSMiao Xie 	if (cur_trans == root->fs_info->running_transaction)
15654a9d8bdeSMiao Xie 		root->fs_info->running_transaction = NULL;
15664a9d8bdeSMiao Xie 	spin_unlock(&root->fs_info->trans_lock);
15674a9d8bdeSMiao Xie 
1568e0228285SJosef Bacik 	if (trans->type & __TRANS_FREEZABLE)
1569e0228285SJosef Bacik 		sb_end_intwrite(root->fs_info->sb);
1570724e2315SJosef Bacik 	btrfs_put_transaction(cur_trans);
1571724e2315SJosef Bacik 	btrfs_put_transaction(cur_trans);
157249b25e05SJeff Mahoney 
157349b25e05SJeff Mahoney 	trace_btrfs_transaction_commit(root);
157449b25e05SJeff Mahoney 
157549b25e05SJeff Mahoney 	if (current->journal_info == trans)
157649b25e05SJeff Mahoney 		current->journal_info = NULL;
1577c0af8f0bSWang Shilong 	btrfs_scrub_cancel(root->fs_info);
157849b25e05SJeff Mahoney 
157949b25e05SJeff Mahoney 	kmem_cache_free(btrfs_trans_handle_cachep, trans);
158049b25e05SJeff Mahoney }
158149b25e05SJeff Mahoney 
1582ca469637SMiao Xie static int btrfs_flush_all_pending_stuffs(struct btrfs_trans_handle *trans,
1583ca469637SMiao Xie 					  struct btrfs_root *root)
1584ca469637SMiao Xie {
1585ca469637SMiao Xie 	int ret;
1586ca469637SMiao Xie 
1587ca469637SMiao Xie 	ret = btrfs_run_delayed_items(trans, root);
1588ca469637SMiao Xie 	/*
1589ca469637SMiao Xie 	 * running the delayed items may have added new refs. account
1590ca469637SMiao Xie 	 * them now so that they hinder processing of more delayed refs
1591ca469637SMiao Xie 	 * as little as possible.
1592ca469637SMiao Xie 	 */
159380d94fb3SFilipe David Borba Manana 	if (ret) {
1594ca469637SMiao Xie 		btrfs_delayed_refs_qgroup_accounting(trans, root->fs_info);
159580d94fb3SFilipe David Borba Manana 		return ret;
159680d94fb3SFilipe David Borba Manana 	}
159780d94fb3SFilipe David Borba Manana 
159880d94fb3SFilipe David Borba Manana 	ret = btrfs_delayed_refs_qgroup_accounting(trans, root->fs_info);
159980d94fb3SFilipe David Borba Manana 	if (ret)
160080d94fb3SFilipe David Borba Manana 		return ret;
1601ca469637SMiao Xie 
1602ca469637SMiao Xie 	/*
1603ca469637SMiao Xie 	 * rename don't use btrfs_join_transaction, so, once we
1604ca469637SMiao Xie 	 * set the transaction to blocked above, we aren't going
1605ca469637SMiao Xie 	 * to get any new ordered operations.  We can safely run
1606ca469637SMiao Xie 	 * it here and no for sure that nothing new will be added
1607ca469637SMiao Xie 	 * to the list
1608ca469637SMiao Xie 	 */
1609569e0f35SJosef Bacik 	ret = btrfs_run_ordered_operations(trans, root, 1);
1610ca469637SMiao Xie 
1611eebc6084SMiao Xie 	return ret;
1612ca469637SMiao Xie }
1613ca469637SMiao Xie 
161482436617SMiao Xie static inline int btrfs_start_delalloc_flush(struct btrfs_fs_info *fs_info)
161582436617SMiao Xie {
161682436617SMiao Xie 	if (btrfs_test_opt(fs_info->tree_root, FLUSHONCOMMIT))
16176c255e67SMiao Xie 		return btrfs_start_delalloc_roots(fs_info, 1, -1);
161882436617SMiao Xie 	return 0;
161982436617SMiao Xie }
162082436617SMiao Xie 
162182436617SMiao Xie static inline void btrfs_wait_delalloc_flush(struct btrfs_fs_info *fs_info)
162282436617SMiao Xie {
162382436617SMiao Xie 	if (btrfs_test_opt(fs_info->tree_root, FLUSHONCOMMIT))
1624b0244199SMiao Xie 		btrfs_wait_ordered_roots(fs_info, -1);
162582436617SMiao Xie }
162682436617SMiao Xie 
162779154b1bSChris Mason int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
162879154b1bSChris Mason 			     struct btrfs_root *root)
162979154b1bSChris Mason {
163049b25e05SJeff Mahoney 	struct btrfs_transaction *cur_trans = trans->transaction;
16318fd17795SChris Mason 	struct btrfs_transaction *prev_trans = NULL;
163225287e0aSMiao Xie 	int ret;
163379154b1bSChris Mason 
1634569e0f35SJosef Bacik 	ret = btrfs_run_ordered_operations(trans, root, 0);
163525287e0aSMiao Xie 	if (ret) {
163625287e0aSMiao Xie 		btrfs_abort_transaction(trans, root, ret);
1637e4a2bcacSJosef Bacik 		btrfs_end_transaction(trans, root);
1638e4a2bcacSJosef Bacik 		return ret;
163925287e0aSMiao Xie 	}
164025287e0aSMiao Xie 
16418d25a086SMiao Xie 	/* Stop the commit early if ->aborted is set */
16428d25a086SMiao Xie 	if (unlikely(ACCESS_ONCE(cur_trans->aborted))) {
164325287e0aSMiao Xie 		ret = cur_trans->aborted;
1644e4a2bcacSJosef Bacik 		btrfs_end_transaction(trans, root);
1645e4a2bcacSJosef Bacik 		return ret;
164625287e0aSMiao Xie 	}
164749b25e05SJeff Mahoney 
164856bec294SChris Mason 	/* make a pass through all the delayed refs we have so far
164956bec294SChris Mason 	 * any runnings procs may add more while we are here
165056bec294SChris Mason 	 */
165156bec294SChris Mason 	ret = btrfs_run_delayed_refs(trans, root, 0);
1652e4a2bcacSJosef Bacik 	if (ret) {
1653e4a2bcacSJosef Bacik 		btrfs_end_transaction(trans, root);
1654e4a2bcacSJosef Bacik 		return ret;
1655e4a2bcacSJosef Bacik 	}
165656bec294SChris Mason 
16570e721106SJosef Bacik 	btrfs_trans_release_metadata(trans, root);
16580e721106SJosef Bacik 	trans->block_rsv = NULL;
1659272d26d0SMiao Xie 	if (trans->qgroup_reserved) {
1660272d26d0SMiao Xie 		btrfs_qgroup_free(root, trans->qgroup_reserved);
1661272d26d0SMiao Xie 		trans->qgroup_reserved = 0;
1662272d26d0SMiao Xie 	}
16630e721106SJosef Bacik 
1664b7ec40d7SChris Mason 	cur_trans = trans->transaction;
166549b25e05SJeff Mahoney 
166656bec294SChris Mason 	/*
166756bec294SChris Mason 	 * set the flushing flag so procs in this transaction have to
166856bec294SChris Mason 	 * start sending their work down.
166956bec294SChris Mason 	 */
1670b7ec40d7SChris Mason 	cur_trans->delayed_refs.flushing = 1;
16711be41b78SJosef Bacik 	smp_wmb();
167256bec294SChris Mason 
1673ea658badSJosef Bacik 	if (!list_empty(&trans->new_bgs))
1674ea658badSJosef Bacik 		btrfs_create_pending_block_groups(trans, root);
1675ea658badSJosef Bacik 
1676c3e69d58SChris Mason 	ret = btrfs_run_delayed_refs(trans, root, 0);
1677e4a2bcacSJosef Bacik 	if (ret) {
1678e4a2bcacSJosef Bacik 		btrfs_end_transaction(trans, root);
1679e4a2bcacSJosef Bacik 		return ret;
1680e4a2bcacSJosef Bacik 	}
168156bec294SChris Mason 
16824a9d8bdeSMiao Xie 	spin_lock(&root->fs_info->trans_lock);
16834a9d8bdeSMiao Xie 	if (cur_trans->state >= TRANS_STATE_COMMIT_START) {
16844a9d8bdeSMiao Xie 		spin_unlock(&root->fs_info->trans_lock);
168513c5a93eSJosef Bacik 		atomic_inc(&cur_trans->use_count);
168649b25e05SJeff Mahoney 		ret = btrfs_end_transaction(trans, root);
1687ccd467d6SChris Mason 
1688b9c8300cSLi Zefan 		wait_for_commit(root, cur_trans);
168915ee9bc7SJosef Bacik 
1690724e2315SJosef Bacik 		btrfs_put_transaction(cur_trans);
169115ee9bc7SJosef Bacik 
169249b25e05SJeff Mahoney 		return ret;
169379154b1bSChris Mason 	}
16944313b399SChris Mason 
16954a9d8bdeSMiao Xie 	cur_trans->state = TRANS_STATE_COMMIT_START;
1696bb9c12c9SSage Weil 	wake_up(&root->fs_info->transaction_blocked_wait);
1697bb9c12c9SSage Weil 
1698ccd467d6SChris Mason 	if (cur_trans->list.prev != &root->fs_info->trans_list) {
1699ccd467d6SChris Mason 		prev_trans = list_entry(cur_trans->list.prev,
1700ccd467d6SChris Mason 					struct btrfs_transaction, list);
17014a9d8bdeSMiao Xie 		if (prev_trans->state != TRANS_STATE_COMPLETED) {
170213c5a93eSJosef Bacik 			atomic_inc(&prev_trans->use_count);
1703a4abeea4SJosef Bacik 			spin_unlock(&root->fs_info->trans_lock);
1704ccd467d6SChris Mason 
1705ccd467d6SChris Mason 			wait_for_commit(root, prev_trans);
1706ccd467d6SChris Mason 
1707724e2315SJosef Bacik 			btrfs_put_transaction(prev_trans);
1708a4abeea4SJosef Bacik 		} else {
1709a4abeea4SJosef Bacik 			spin_unlock(&root->fs_info->trans_lock);
1710ccd467d6SChris Mason 		}
1711a4abeea4SJosef Bacik 	} else {
1712a4abeea4SJosef Bacik 		spin_unlock(&root->fs_info->trans_lock);
1713ccd467d6SChris Mason 	}
171415ee9bc7SJosef Bacik 
17150860adfdSMiao Xie 	extwriter_counter_dec(cur_trans, trans->type);
17160860adfdSMiao Xie 
171782436617SMiao Xie 	ret = btrfs_start_delalloc_flush(root->fs_info);
171882436617SMiao Xie 	if (ret)
171982436617SMiao Xie 		goto cleanup_transaction;
172082436617SMiao Xie 
1721ca469637SMiao Xie 	ret = btrfs_flush_all_pending_stuffs(trans, root);
172249b25e05SJeff Mahoney 	if (ret)
172349b25e05SJeff Mahoney 		goto cleanup_transaction;
172416cdcec7SMiao Xie 
1725581227d0SMiao Xie 	wait_event(cur_trans->writer_wait,
1726581227d0SMiao Xie 		   extwriter_counter_read(cur_trans) == 0);
1727ed3b3d31SChris Mason 
1728581227d0SMiao Xie 	/* some pending stuffs might be added after the previous flush. */
1729ca469637SMiao Xie 	ret = btrfs_flush_all_pending_stuffs(trans, root);
1730ca469637SMiao Xie 	if (ret)
1731ca469637SMiao Xie 		goto cleanup_transaction;
1732ca469637SMiao Xie 
173382436617SMiao Xie 	btrfs_wait_delalloc_flush(root->fs_info);
1734cb7ab021SWang Shilong 
1735cb7ab021SWang Shilong 	btrfs_scrub_pause(root);
1736ed0ca140SJosef Bacik 	/*
1737ed0ca140SJosef Bacik 	 * Ok now we need to make sure to block out any other joins while we
1738ed0ca140SJosef Bacik 	 * commit the transaction.  We could have started a join before setting
17394a9d8bdeSMiao Xie 	 * COMMIT_DOING so make sure to wait for num_writers to == 1 again.
1740ed0ca140SJosef Bacik 	 */
1741a4abeea4SJosef Bacik 	spin_lock(&root->fs_info->trans_lock);
17424a9d8bdeSMiao Xie 	cur_trans->state = TRANS_STATE_COMMIT_DOING;
1743a4abeea4SJosef Bacik 	spin_unlock(&root->fs_info->trans_lock);
1744ed0ca140SJosef Bacik 	wait_event(cur_trans->writer_wait,
1745ed0ca140SJosef Bacik 		   atomic_read(&cur_trans->num_writers) == 1);
174615ee9bc7SJosef Bacik 
17472cba30f1SMiao Xie 	/* ->aborted might be set after the previous check, so check it */
17482cba30f1SMiao Xie 	if (unlikely(ACCESS_ONCE(cur_trans->aborted))) {
17492cba30f1SMiao Xie 		ret = cur_trans->aborted;
17506cf7f77eSWang Shilong 		goto scrub_continue;
17512cba30f1SMiao Xie 	}
17527585717fSChris Mason 	/*
17537585717fSChris Mason 	 * the reloc mutex makes sure that we stop
17547585717fSChris Mason 	 * the balancing code from coming in and moving
17557585717fSChris Mason 	 * extents around in the middle of the commit
17567585717fSChris Mason 	 */
17577585717fSChris Mason 	mutex_lock(&root->fs_info->reloc_mutex);
17587585717fSChris Mason 
175942874b3dSMiao Xie 	/*
176042874b3dSMiao Xie 	 * We needn't worry about the delayed items because we will
176142874b3dSMiao Xie 	 * deal with them in create_pending_snapshot(), which is the
176242874b3dSMiao Xie 	 * core function of the snapshot creation.
176342874b3dSMiao Xie 	 */
176442874b3dSMiao Xie 	ret = create_pending_snapshots(trans, root->fs_info);
176549b25e05SJeff Mahoney 	if (ret) {
176649b25e05SJeff Mahoney 		mutex_unlock(&root->fs_info->reloc_mutex);
17676cf7f77eSWang Shilong 		goto scrub_continue;
176849b25e05SJeff Mahoney 	}
17693063d29fSChris Mason 
177042874b3dSMiao Xie 	/*
177142874b3dSMiao Xie 	 * We insert the dir indexes of the snapshots and update the inode
177242874b3dSMiao Xie 	 * of the snapshots' parents after the snapshot creation, so there
177342874b3dSMiao Xie 	 * are some delayed items which are not dealt with. Now deal with
177442874b3dSMiao Xie 	 * them.
177542874b3dSMiao Xie 	 *
177642874b3dSMiao Xie 	 * We needn't worry that this operation will corrupt the snapshots,
177742874b3dSMiao Xie 	 * because all the tree which are snapshoted will be forced to COW
177842874b3dSMiao Xie 	 * the nodes and leaves.
177942874b3dSMiao Xie 	 */
178042874b3dSMiao Xie 	ret = btrfs_run_delayed_items(trans, root);
178149b25e05SJeff Mahoney 	if (ret) {
178249b25e05SJeff Mahoney 		mutex_unlock(&root->fs_info->reloc_mutex);
17836cf7f77eSWang Shilong 		goto scrub_continue;
178449b25e05SJeff Mahoney 	}
178516cdcec7SMiao Xie 
178656bec294SChris Mason 	ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
178749b25e05SJeff Mahoney 	if (ret) {
178849b25e05SJeff Mahoney 		mutex_unlock(&root->fs_info->reloc_mutex);
17896cf7f77eSWang Shilong 		goto scrub_continue;
179049b25e05SJeff Mahoney 	}
179156bec294SChris Mason 
1792e999376fSChris Mason 	/*
1793e999376fSChris Mason 	 * make sure none of the code above managed to slip in a
1794e999376fSChris Mason 	 * delayed item
1795e999376fSChris Mason 	 */
1796e999376fSChris Mason 	btrfs_assert_delayed_root_empty(root);
1797e999376fSChris Mason 
17982c90e5d6SChris Mason 	WARN_ON(cur_trans != trans->transaction);
1799dc17ff8fSChris Mason 
1800e02119d5SChris Mason 	/* btrfs_commit_tree_roots is responsible for getting the
1801e02119d5SChris Mason 	 * various roots consistent with each other.  Every pointer
1802e02119d5SChris Mason 	 * in the tree of tree roots has to point to the most up to date
1803e02119d5SChris Mason 	 * root for every subvolume and other tree.  So, we have to keep
1804e02119d5SChris Mason 	 * the tree logging code from jumping in and changing any
1805e02119d5SChris Mason 	 * of the trees.
1806e02119d5SChris Mason 	 *
1807e02119d5SChris Mason 	 * At this point in the commit, there can't be any tree-log
1808e02119d5SChris Mason 	 * writers, but a little lower down we drop the trans mutex
1809e02119d5SChris Mason 	 * and let new people in.  By holding the tree_log_mutex
1810e02119d5SChris Mason 	 * from now until after the super is written, we avoid races
1811e02119d5SChris Mason 	 * with the tree-log code.
1812e02119d5SChris Mason 	 */
1813e02119d5SChris Mason 	mutex_lock(&root->fs_info->tree_log_mutex);
18141a40e23bSZheng Yan 
18155d4f98a2SYan Zheng 	ret = commit_fs_roots(trans, root);
181649b25e05SJeff Mahoney 	if (ret) {
181749b25e05SJeff Mahoney 		mutex_unlock(&root->fs_info->tree_log_mutex);
1818871383beSDavid Sterba 		mutex_unlock(&root->fs_info->reloc_mutex);
18196cf7f77eSWang Shilong 		goto scrub_continue;
182049b25e05SJeff Mahoney 	}
182154aa1f4dSChris Mason 
18223818aea2SQu Wenruo 	/*
18233818aea2SQu Wenruo 	 * Since the transaction is done, we should set the inode map cache flag
18243818aea2SQu Wenruo 	 * before any other comming transaction.
18253818aea2SQu Wenruo 	 */
18263818aea2SQu Wenruo 	if (btrfs_test_opt(root, CHANGE_INODE_CACHE))
18273818aea2SQu Wenruo 		btrfs_set_opt(root->fs_info->mount_opt, INODE_MAP_CACHE);
18283818aea2SQu Wenruo 	else
18293818aea2SQu Wenruo 		btrfs_clear_opt(root->fs_info->mount_opt, INODE_MAP_CACHE);
18303818aea2SQu Wenruo 
18315d4f98a2SYan Zheng 	/* commit_fs_roots gets rid of all the tree log roots, it is now
1832e02119d5SChris Mason 	 * safe to free the root of tree log roots
1833e02119d5SChris Mason 	 */
1834e02119d5SChris Mason 	btrfs_free_log_root_tree(trans, root->fs_info);
1835e02119d5SChris Mason 
18365d4f98a2SYan Zheng 	ret = commit_cowonly_roots(trans, root);
183749b25e05SJeff Mahoney 	if (ret) {
183849b25e05SJeff Mahoney 		mutex_unlock(&root->fs_info->tree_log_mutex);
1839871383beSDavid Sterba 		mutex_unlock(&root->fs_info->reloc_mutex);
18406cf7f77eSWang Shilong 		goto scrub_continue;
184149b25e05SJeff Mahoney 	}
184254aa1f4dSChris Mason 
18432cba30f1SMiao Xie 	/*
18442cba30f1SMiao Xie 	 * The tasks which save the space cache and inode cache may also
18452cba30f1SMiao Xie 	 * update ->aborted, check it.
18462cba30f1SMiao Xie 	 */
18472cba30f1SMiao Xie 	if (unlikely(ACCESS_ONCE(cur_trans->aborted))) {
18482cba30f1SMiao Xie 		ret = cur_trans->aborted;
18492cba30f1SMiao Xie 		mutex_unlock(&root->fs_info->tree_log_mutex);
18502cba30f1SMiao Xie 		mutex_unlock(&root->fs_info->reloc_mutex);
18516cf7f77eSWang Shilong 		goto scrub_continue;
18522cba30f1SMiao Xie 	}
18532cba30f1SMiao Xie 
185411833d66SYan Zheng 	btrfs_prepare_extent_commit(trans, root);
185511833d66SYan Zheng 
185678fae27eSChris Mason 	cur_trans = root->fs_info->running_transaction;
18575f39d397SChris Mason 
18585d4f98a2SYan Zheng 	btrfs_set_root_node(&root->fs_info->tree_root->root_item,
18595d4f98a2SYan Zheng 			    root->fs_info->tree_root->node);
1860817d52f8SJosef Bacik 	switch_commit_root(root->fs_info->tree_root);
18615d4f98a2SYan Zheng 
18625d4f98a2SYan Zheng 	btrfs_set_root_node(&root->fs_info->chunk_root->root_item,
18635d4f98a2SYan Zheng 			    root->fs_info->chunk_root->node);
1864817d52f8SJosef Bacik 	switch_commit_root(root->fs_info->chunk_root);
18655d4f98a2SYan Zheng 
1866edf39272SJan Schmidt 	assert_qgroups_uptodate(trans);
18675d4f98a2SYan Zheng 	update_super_roots(root);
1868e02119d5SChris Mason 
18696c41761fSDavid Sterba 	btrfs_set_super_log_root(root->fs_info->super_copy, 0);
18706c41761fSDavid Sterba 	btrfs_set_super_log_root_level(root->fs_info->super_copy, 0);
18716c41761fSDavid Sterba 	memcpy(root->fs_info->super_for_commit, root->fs_info->super_copy,
18726c41761fSDavid Sterba 	       sizeof(*root->fs_info->super_copy));
1873ccd467d6SChris Mason 
1874a4abeea4SJosef Bacik 	spin_lock(&root->fs_info->trans_lock);
18754a9d8bdeSMiao Xie 	cur_trans->state = TRANS_STATE_UNBLOCKED;
1876a4abeea4SJosef Bacik 	root->fs_info->running_transaction = NULL;
1877a4abeea4SJosef Bacik 	spin_unlock(&root->fs_info->trans_lock);
18787585717fSChris Mason 	mutex_unlock(&root->fs_info->reloc_mutex);
1879b7ec40d7SChris Mason 
1880f9295749SChris Mason 	wake_up(&root->fs_info->transaction_wait);
1881e6dcd2dcSChris Mason 
188279154b1bSChris Mason 	ret = btrfs_write_and_wait_transaction(trans, root);
188349b25e05SJeff Mahoney 	if (ret) {
188449b25e05SJeff Mahoney 		btrfs_error(root->fs_info, ret,
188508748810SDavid Sterba 			    "Error while writing out transaction");
188649b25e05SJeff Mahoney 		mutex_unlock(&root->fs_info->tree_log_mutex);
18876cf7f77eSWang Shilong 		goto scrub_continue;
188849b25e05SJeff Mahoney 	}
188949b25e05SJeff Mahoney 
189049b25e05SJeff Mahoney 	ret = write_ctree_super(trans, root, 0);
189149b25e05SJeff Mahoney 	if (ret) {
189249b25e05SJeff Mahoney 		mutex_unlock(&root->fs_info->tree_log_mutex);
18936cf7f77eSWang Shilong 		goto scrub_continue;
189449b25e05SJeff Mahoney 	}
18954313b399SChris Mason 
1896e02119d5SChris Mason 	/*
1897e02119d5SChris Mason 	 * the super is written, we can safely allow the tree-loggers
1898e02119d5SChris Mason 	 * to go about their business
1899e02119d5SChris Mason 	 */
1900e02119d5SChris Mason 	mutex_unlock(&root->fs_info->tree_log_mutex);
1901e02119d5SChris Mason 
190211833d66SYan Zheng 	btrfs_finish_extent_commit(trans, root);
19034313b399SChris Mason 
190415ee9bc7SJosef Bacik 	root->fs_info->last_trans_committed = cur_trans->transid;
19054a9d8bdeSMiao Xie 	/*
19064a9d8bdeSMiao Xie 	 * We needn't acquire the lock here because there is no other task
19074a9d8bdeSMiao Xie 	 * which can change it.
19084a9d8bdeSMiao Xie 	 */
19094a9d8bdeSMiao Xie 	cur_trans->state = TRANS_STATE_COMPLETED;
19102c90e5d6SChris Mason 	wake_up(&cur_trans->commit_wait);
19113de4586cSChris Mason 
1912a4abeea4SJosef Bacik 	spin_lock(&root->fs_info->trans_lock);
191313c5a93eSJosef Bacik 	list_del_init(&cur_trans->list);
1914a4abeea4SJosef Bacik 	spin_unlock(&root->fs_info->trans_lock);
1915a4abeea4SJosef Bacik 
1916724e2315SJosef Bacik 	btrfs_put_transaction(cur_trans);
1917724e2315SJosef Bacik 	btrfs_put_transaction(cur_trans);
191858176a96SJosef Bacik 
19190860adfdSMiao Xie 	if (trans->type & __TRANS_FREEZABLE)
1920b2b5ef5cSJan Kara 		sb_end_intwrite(root->fs_info->sb);
1921b2b5ef5cSJan Kara 
19221abe9b8aSliubo 	trace_btrfs_transaction_commit(root);
19231abe9b8aSliubo 
1924a2de733cSArne Jansen 	btrfs_scrub_continue(root);
1925a2de733cSArne Jansen 
19269ed74f2dSJosef Bacik 	if (current->journal_info == trans)
19279ed74f2dSJosef Bacik 		current->journal_info = NULL;
19289ed74f2dSJosef Bacik 
19292c90e5d6SChris Mason 	kmem_cache_free(btrfs_trans_handle_cachep, trans);
193024bbcf04SYan, Zheng 
193124bbcf04SYan, Zheng 	if (current != root->fs_info->transaction_kthread)
193224bbcf04SYan, Zheng 		btrfs_run_delayed_iputs(root);
193324bbcf04SYan, Zheng 
193479154b1bSChris Mason 	return ret;
193549b25e05SJeff Mahoney 
19366cf7f77eSWang Shilong scrub_continue:
19376cf7f77eSWang Shilong 	btrfs_scrub_continue(root);
193849b25e05SJeff Mahoney cleanup_transaction:
19390e721106SJosef Bacik 	btrfs_trans_release_metadata(trans, root);
19400e721106SJosef Bacik 	trans->block_rsv = NULL;
1941272d26d0SMiao Xie 	if (trans->qgroup_reserved) {
1942272d26d0SMiao Xie 		btrfs_qgroup_free(root, trans->qgroup_reserved);
1943272d26d0SMiao Xie 		trans->qgroup_reserved = 0;
1944272d26d0SMiao Xie 	}
1945c2cf52ebSSimon Kirby 	btrfs_warn(root->fs_info, "Skipping commit of aborted transaction.");
194649b25e05SJeff Mahoney 	if (current->journal_info == trans)
194749b25e05SJeff Mahoney 		current->journal_info = NULL;
19487b8b92afSJosef Bacik 	cleanup_transaction(trans, root, ret);
194949b25e05SJeff Mahoney 
195049b25e05SJeff Mahoney 	return ret;
195179154b1bSChris Mason }
195279154b1bSChris Mason 
1953d352ac68SChris Mason /*
19549d1a2a3aSDavid Sterba  * return < 0 if error
19559d1a2a3aSDavid Sterba  * 0 if there are no more dead_roots at the time of call
19569d1a2a3aSDavid Sterba  * 1 there are more to be processed, call me again
19579d1a2a3aSDavid Sterba  *
19589d1a2a3aSDavid Sterba  * The return value indicates there are certainly more snapshots to delete, but
19599d1a2a3aSDavid Sterba  * if there comes a new one during processing, it may return 0. We don't mind,
19609d1a2a3aSDavid Sterba  * because btrfs_commit_super will poke cleaner thread and it will process it a
19619d1a2a3aSDavid Sterba  * few seconds later.
1962d352ac68SChris Mason  */
19639d1a2a3aSDavid Sterba int btrfs_clean_one_deleted_snapshot(struct btrfs_root *root)
1964e9d0b13bSChris Mason {
19659d1a2a3aSDavid Sterba 	int ret;
19665d4f98a2SYan Zheng 	struct btrfs_fs_info *fs_info = root->fs_info;
1967e9d0b13bSChris Mason 
1968a4abeea4SJosef Bacik 	spin_lock(&fs_info->trans_lock);
19699d1a2a3aSDavid Sterba 	if (list_empty(&fs_info->dead_roots)) {
19709d1a2a3aSDavid Sterba 		spin_unlock(&fs_info->trans_lock);
19719d1a2a3aSDavid Sterba 		return 0;
19729d1a2a3aSDavid Sterba 	}
19739d1a2a3aSDavid Sterba 	root = list_first_entry(&fs_info->dead_roots,
19749d1a2a3aSDavid Sterba 			struct btrfs_root, root_list);
197518f687d5SWang Shilong 	/*
197618f687d5SWang Shilong 	 * Make sure root is not involved in send,
197718f687d5SWang Shilong 	 * if we fail with first root, we return
197818f687d5SWang Shilong 	 * directly rather than continue.
197918f687d5SWang Shilong 	 */
198018f687d5SWang Shilong 	spin_lock(&root->root_item_lock);
198118f687d5SWang Shilong 	if (root->send_in_progress) {
198218f687d5SWang Shilong 		spin_unlock(&fs_info->trans_lock);
198318f687d5SWang Shilong 		spin_unlock(&root->root_item_lock);
198418f687d5SWang Shilong 		return 0;
198518f687d5SWang Shilong 	}
198618f687d5SWang Shilong 	spin_unlock(&root->root_item_lock);
198718f687d5SWang Shilong 
1988cfad392bSJosef Bacik 	list_del_init(&root->root_list);
1989a4abeea4SJosef Bacik 	spin_unlock(&fs_info->trans_lock);
19905d4f98a2SYan Zheng 
1991efe120a0SFrank Holton 	pr_debug("BTRFS: cleaner removing %llu\n", root->objectid);
199276dda93cSYan, Zheng 
199316cdcec7SMiao Xie 	btrfs_kill_all_delayed_nodes(root);
199416cdcec7SMiao Xie 
199576dda93cSYan, Zheng 	if (btrfs_header_backref_rev(root->node) <
199676dda93cSYan, Zheng 			BTRFS_MIXED_BACKREF_REV)
19972c536799SJeff Mahoney 		ret = btrfs_drop_snapshot(root, NULL, 0, 0);
199876dda93cSYan, Zheng 	else
19992c536799SJeff Mahoney 		ret = btrfs_drop_snapshot(root, NULL, 1, 0);
20009d1a2a3aSDavid Sterba 	/*
20019d1a2a3aSDavid Sterba 	 * If we encounter a transaction abort during snapshot cleaning, we
20029d1a2a3aSDavid Sterba 	 * don't want to crash here
20039d1a2a3aSDavid Sterba 	 */
20046596a928SJosef Bacik 	return (ret < 0) ? 0 : 1;
2005e9d0b13bSChris Mason }
2006