xref: /openbmc/linux/fs/btrfs/transaction.c (revision fcebe456)
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"
34fcebe456SJosef Bacik #include "qgroup.h"
3579154b1bSChris Mason 
360f7d52f4SChris Mason #define BTRFS_ROOT_TRANS_TAG 0
370f7d52f4SChris Mason 
384a9d8bdeSMiao Xie static unsigned int btrfs_blocked_trans_types[TRANS_STATE_MAX] = {
394a9d8bdeSMiao Xie 	[TRANS_STATE_RUNNING]		= 0U,
404a9d8bdeSMiao Xie 	[TRANS_STATE_BLOCKED]		= (__TRANS_USERSPACE |
414a9d8bdeSMiao Xie 					   __TRANS_START),
424a9d8bdeSMiao Xie 	[TRANS_STATE_COMMIT_START]	= (__TRANS_USERSPACE |
434a9d8bdeSMiao Xie 					   __TRANS_START |
444a9d8bdeSMiao Xie 					   __TRANS_ATTACH),
454a9d8bdeSMiao Xie 	[TRANS_STATE_COMMIT_DOING]	= (__TRANS_USERSPACE |
464a9d8bdeSMiao Xie 					   __TRANS_START |
474a9d8bdeSMiao Xie 					   __TRANS_ATTACH |
484a9d8bdeSMiao Xie 					   __TRANS_JOIN),
494a9d8bdeSMiao Xie 	[TRANS_STATE_UNBLOCKED]		= (__TRANS_USERSPACE |
504a9d8bdeSMiao Xie 					   __TRANS_START |
514a9d8bdeSMiao Xie 					   __TRANS_ATTACH |
524a9d8bdeSMiao Xie 					   __TRANS_JOIN |
534a9d8bdeSMiao Xie 					   __TRANS_JOIN_NOLOCK),
544a9d8bdeSMiao Xie 	[TRANS_STATE_COMPLETED]		= (__TRANS_USERSPACE |
554a9d8bdeSMiao Xie 					   __TRANS_START |
564a9d8bdeSMiao Xie 					   __TRANS_ATTACH |
574a9d8bdeSMiao Xie 					   __TRANS_JOIN |
584a9d8bdeSMiao Xie 					   __TRANS_JOIN_NOLOCK),
594a9d8bdeSMiao Xie };
604a9d8bdeSMiao Xie 
61724e2315SJosef Bacik void btrfs_put_transaction(struct btrfs_transaction *transaction)
6279154b1bSChris Mason {
6313c5a93eSJosef Bacik 	WARN_ON(atomic_read(&transaction->use_count) == 0);
6413c5a93eSJosef Bacik 	if (atomic_dec_and_test(&transaction->use_count)) {
65a4abeea4SJosef Bacik 		BUG_ON(!list_empty(&transaction->list));
66c46effa6SLiu Bo 		WARN_ON(!RB_EMPTY_ROOT(&transaction->delayed_refs.href_root));
676df9a95eSJosef Bacik 		while (!list_empty(&transaction->pending_chunks)) {
686df9a95eSJosef Bacik 			struct extent_map *em;
696df9a95eSJosef Bacik 
706df9a95eSJosef Bacik 			em = list_first_entry(&transaction->pending_chunks,
716df9a95eSJosef Bacik 					      struct extent_map, list);
726df9a95eSJosef Bacik 			list_del_init(&em->list);
736df9a95eSJosef Bacik 			free_extent_map(em);
746df9a95eSJosef Bacik 		}
752c90e5d6SChris Mason 		kmem_cache_free(btrfs_transaction_cachep, transaction);
7679154b1bSChris Mason 	}
7778fae27eSChris Mason }
7879154b1bSChris Mason 
799e351cc8SJosef Bacik static noinline void switch_commit_roots(struct btrfs_transaction *trans,
809e351cc8SJosef Bacik 					 struct btrfs_fs_info *fs_info)
81817d52f8SJosef Bacik {
829e351cc8SJosef Bacik 	struct btrfs_root *root, *tmp;
839e351cc8SJosef Bacik 
849e351cc8SJosef Bacik 	down_write(&fs_info->commit_root_sem);
859e351cc8SJosef Bacik 	list_for_each_entry_safe(root, tmp, &trans->switch_commits,
869e351cc8SJosef Bacik 				 dirty_list) {
879e351cc8SJosef Bacik 		list_del_init(&root->dirty_list);
88817d52f8SJosef Bacik 		free_extent_buffer(root->commit_root);
89817d52f8SJosef Bacik 		root->commit_root = btrfs_root_node(root);
909e351cc8SJosef Bacik 		if (is_fstree(root->objectid))
919e351cc8SJosef Bacik 			btrfs_unpin_free_ino(root);
929e351cc8SJosef Bacik 	}
939e351cc8SJosef Bacik 	up_write(&fs_info->commit_root_sem);
94817d52f8SJosef Bacik }
95817d52f8SJosef Bacik 
960860adfdSMiao Xie static inline void extwriter_counter_inc(struct btrfs_transaction *trans,
970860adfdSMiao Xie 					 unsigned int type)
980860adfdSMiao Xie {
990860adfdSMiao Xie 	if (type & TRANS_EXTWRITERS)
1000860adfdSMiao Xie 		atomic_inc(&trans->num_extwriters);
1010860adfdSMiao Xie }
1020860adfdSMiao Xie 
1030860adfdSMiao Xie static inline void extwriter_counter_dec(struct btrfs_transaction *trans,
1040860adfdSMiao Xie 					 unsigned int type)
1050860adfdSMiao Xie {
1060860adfdSMiao Xie 	if (type & TRANS_EXTWRITERS)
1070860adfdSMiao Xie 		atomic_dec(&trans->num_extwriters);
1080860adfdSMiao Xie }
1090860adfdSMiao Xie 
1100860adfdSMiao Xie static inline void extwriter_counter_init(struct btrfs_transaction *trans,
1110860adfdSMiao Xie 					  unsigned int type)
1120860adfdSMiao Xie {
1130860adfdSMiao Xie 	atomic_set(&trans->num_extwriters, ((type & TRANS_EXTWRITERS) ? 1 : 0));
1140860adfdSMiao Xie }
1150860adfdSMiao Xie 
1160860adfdSMiao Xie static inline int extwriter_counter_read(struct btrfs_transaction *trans)
1170860adfdSMiao Xie {
1180860adfdSMiao Xie 	return atomic_read(&trans->num_extwriters);
119178260b2SMiao Xie }
120178260b2SMiao Xie 
121d352ac68SChris Mason /*
122d352ac68SChris Mason  * either allocate a new transaction or hop into the existing one
123d352ac68SChris Mason  */
1240860adfdSMiao Xie static noinline int join_transaction(struct btrfs_root *root, unsigned int type)
12579154b1bSChris Mason {
12679154b1bSChris Mason 	struct btrfs_transaction *cur_trans;
12719ae4e81SJan Schmidt 	struct btrfs_fs_info *fs_info = root->fs_info;
128a4abeea4SJosef Bacik 
12919ae4e81SJan Schmidt 	spin_lock(&fs_info->trans_lock);
130d43317dcSChris Mason loop:
13149b25e05SJeff Mahoney 	/* The file system has been taken offline. No new transactions. */
13287533c47SMiao Xie 	if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) {
13319ae4e81SJan Schmidt 		spin_unlock(&fs_info->trans_lock);
13449b25e05SJeff Mahoney 		return -EROFS;
13549b25e05SJeff Mahoney 	}
13649b25e05SJeff Mahoney 
13719ae4e81SJan Schmidt 	cur_trans = fs_info->running_transaction;
138a4abeea4SJosef Bacik 	if (cur_trans) {
139871383beSDavid Sterba 		if (cur_trans->aborted) {
14019ae4e81SJan Schmidt 			spin_unlock(&fs_info->trans_lock);
14149b25e05SJeff Mahoney 			return cur_trans->aborted;
142871383beSDavid Sterba 		}
1434a9d8bdeSMiao Xie 		if (btrfs_blocked_trans_types[cur_trans->state] & type) {
144178260b2SMiao Xie 			spin_unlock(&fs_info->trans_lock);
145178260b2SMiao Xie 			return -EBUSY;
146178260b2SMiao Xie 		}
147a4abeea4SJosef Bacik 		atomic_inc(&cur_trans->use_count);
148a4abeea4SJosef Bacik 		atomic_inc(&cur_trans->num_writers);
1490860adfdSMiao Xie 		extwriter_counter_inc(cur_trans, type);
15019ae4e81SJan Schmidt 		spin_unlock(&fs_info->trans_lock);
151a4abeea4SJosef Bacik 		return 0;
152a4abeea4SJosef Bacik 	}
15319ae4e81SJan Schmidt 	spin_unlock(&fs_info->trans_lock);
154a4abeea4SJosef Bacik 
155354aa0fbSMiao Xie 	/*
156354aa0fbSMiao Xie 	 * If we are ATTACH, we just want to catch the current transaction,
157354aa0fbSMiao Xie 	 * and commit it. If there is no transaction, just return ENOENT.
158354aa0fbSMiao Xie 	 */
159354aa0fbSMiao Xie 	if (type == TRANS_ATTACH)
160354aa0fbSMiao Xie 		return -ENOENT;
161354aa0fbSMiao Xie 
1624a9d8bdeSMiao Xie 	/*
1634a9d8bdeSMiao Xie 	 * JOIN_NOLOCK only happens during the transaction commit, so
1644a9d8bdeSMiao Xie 	 * it is impossible that ->running_transaction is NULL
1654a9d8bdeSMiao Xie 	 */
1664a9d8bdeSMiao Xie 	BUG_ON(type == TRANS_JOIN_NOLOCK);
1674a9d8bdeSMiao Xie 
168a4abeea4SJosef Bacik 	cur_trans = kmem_cache_alloc(btrfs_transaction_cachep, GFP_NOFS);
169db5b493aSTsutomu Itoh 	if (!cur_trans)
170db5b493aSTsutomu Itoh 		return -ENOMEM;
171d43317dcSChris Mason 
17219ae4e81SJan Schmidt 	spin_lock(&fs_info->trans_lock);
17319ae4e81SJan Schmidt 	if (fs_info->running_transaction) {
174d43317dcSChris Mason 		/*
175d43317dcSChris Mason 		 * someone started a transaction after we unlocked.  Make sure
1764a9d8bdeSMiao Xie 		 * to redo the checks above
177d43317dcSChris Mason 		 */
178a4abeea4SJosef Bacik 		kmem_cache_free(btrfs_transaction_cachep, cur_trans);
179d43317dcSChris Mason 		goto loop;
18087533c47SMiao Xie 	} else if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) {
181e4b50e14SDan Carpenter 		spin_unlock(&fs_info->trans_lock);
1827b8b92afSJosef Bacik 		kmem_cache_free(btrfs_transaction_cachep, cur_trans);
1837b8b92afSJosef Bacik 		return -EROFS;
184a4abeea4SJosef Bacik 	}
185d43317dcSChris Mason 
18613c5a93eSJosef Bacik 	atomic_set(&cur_trans->num_writers, 1);
1870860adfdSMiao Xie 	extwriter_counter_init(cur_trans, type);
18879154b1bSChris Mason 	init_waitqueue_head(&cur_trans->writer_wait);
18979154b1bSChris Mason 	init_waitqueue_head(&cur_trans->commit_wait);
1904a9d8bdeSMiao Xie 	cur_trans->state = TRANS_STATE_RUNNING;
191a4abeea4SJosef Bacik 	/*
192a4abeea4SJosef Bacik 	 * One for this trans handle, one so it will live on until we
193a4abeea4SJosef Bacik 	 * commit the transaction.
194a4abeea4SJosef Bacik 	 */
195a4abeea4SJosef Bacik 	atomic_set(&cur_trans->use_count, 2);
19608607c1bSChris Mason 	cur_trans->start_time = get_seconds();
19756bec294SChris Mason 
198c46effa6SLiu Bo 	cur_trans->delayed_refs.href_root = RB_ROOT;
199d7df2c79SJosef Bacik 	atomic_set(&cur_trans->delayed_refs.num_entries, 0);
200c3e69d58SChris Mason 	cur_trans->delayed_refs.num_heads_ready = 0;
201c3e69d58SChris Mason 	cur_trans->delayed_refs.num_heads = 0;
20256bec294SChris Mason 	cur_trans->delayed_refs.flushing = 0;
203c3e69d58SChris Mason 	cur_trans->delayed_refs.run_delayed_start = 0;
20420b297d6SJan Schmidt 
20520b297d6SJan Schmidt 	/*
20620b297d6SJan Schmidt 	 * although the tree mod log is per file system and not per transaction,
20720b297d6SJan Schmidt 	 * the log must never go across transaction boundaries.
20820b297d6SJan Schmidt 	 */
20920b297d6SJan Schmidt 	smp_mb();
21031b1a2bdSJulia Lawall 	if (!list_empty(&fs_info->tree_mod_seq_list))
211efe120a0SFrank Holton 		WARN(1, KERN_ERR "BTRFS: tree_mod_seq_list not empty when "
21220b297d6SJan Schmidt 			"creating a fresh transaction\n");
21331b1a2bdSJulia Lawall 	if (!RB_EMPTY_ROOT(&fs_info->tree_mod_log))
214efe120a0SFrank Holton 		WARN(1, KERN_ERR "BTRFS: tree_mod_log rb tree not empty when "
21520b297d6SJan Schmidt 			"creating a fresh transaction\n");
216fc36ed7eSJan Schmidt 	atomic64_set(&fs_info->tree_mod_seq, 0);
21720b297d6SJan Schmidt 
21856bec294SChris Mason 	spin_lock_init(&cur_trans->delayed_refs.lock);
21956bec294SChris Mason 
2203063d29fSChris Mason 	INIT_LIST_HEAD(&cur_trans->pending_snapshots);
221569e0f35SJosef Bacik 	INIT_LIST_HEAD(&cur_trans->ordered_operations);
2226df9a95eSJosef Bacik 	INIT_LIST_HEAD(&cur_trans->pending_chunks);
2239e351cc8SJosef Bacik 	INIT_LIST_HEAD(&cur_trans->switch_commits);
22419ae4e81SJan Schmidt 	list_add_tail(&cur_trans->list, &fs_info->trans_list);
225d1310b2eSChris Mason 	extent_io_tree_init(&cur_trans->dirty_pages,
22619ae4e81SJan Schmidt 			     fs_info->btree_inode->i_mapping);
22719ae4e81SJan Schmidt 	fs_info->generation++;
22819ae4e81SJan Schmidt 	cur_trans->transid = fs_info->generation;
22919ae4e81SJan Schmidt 	fs_info->running_transaction = cur_trans;
23049b25e05SJeff Mahoney 	cur_trans->aborted = 0;
23119ae4e81SJan Schmidt 	spin_unlock(&fs_info->trans_lock);
23215ee9bc7SJosef Bacik 
23379154b1bSChris Mason 	return 0;
23479154b1bSChris Mason }
23579154b1bSChris Mason 
236d352ac68SChris Mason /*
237d397712bSChris Mason  * this does all the record keeping required to make sure that a reference
238d397712bSChris Mason  * counted root is properly recorded in a given transaction.  This is required
239d397712bSChris Mason  * to make sure the old root from before we joined the transaction is deleted
240d397712bSChris Mason  * when the transaction commits
241d352ac68SChris Mason  */
2427585717fSChris Mason static int record_root_in_trans(struct btrfs_trans_handle *trans,
2435d4f98a2SYan Zheng 			       struct btrfs_root *root)
2446702ed49SChris Mason {
24527cdeb70SMiao Xie 	if (test_bit(BTRFS_ROOT_REF_COWS, &root->state) &&
24627cdeb70SMiao Xie 	    root->last_trans < trans->transid) {
2476702ed49SChris Mason 		WARN_ON(root == root->fs_info->extent_root);
2485d4f98a2SYan Zheng 		WARN_ON(root->commit_root != root->node);
2495d4f98a2SYan Zheng 
2507585717fSChris Mason 		/*
25127cdeb70SMiao Xie 		 * see below for IN_TRANS_SETUP usage rules
2527585717fSChris Mason 		 * we have the reloc mutex held now, so there
2537585717fSChris Mason 		 * is only one writer in this function
2547585717fSChris Mason 		 */
25527cdeb70SMiao Xie 		set_bit(BTRFS_ROOT_IN_TRANS_SETUP, &root->state);
2567585717fSChris Mason 
25727cdeb70SMiao Xie 		/* make sure readers find IN_TRANS_SETUP before
2587585717fSChris Mason 		 * they find our root->last_trans update
2597585717fSChris Mason 		 */
2607585717fSChris Mason 		smp_wmb();
2617585717fSChris Mason 
262a4abeea4SJosef Bacik 		spin_lock(&root->fs_info->fs_roots_radix_lock);
263a4abeea4SJosef Bacik 		if (root->last_trans == trans->transid) {
264a4abeea4SJosef Bacik 			spin_unlock(&root->fs_info->fs_roots_radix_lock);
265a4abeea4SJosef Bacik 			return 0;
266a4abeea4SJosef Bacik 		}
2676702ed49SChris Mason 		radix_tree_tag_set(&root->fs_info->fs_roots_radix,
2686702ed49SChris Mason 			   (unsigned long)root->root_key.objectid,
2696702ed49SChris Mason 			   BTRFS_ROOT_TRANS_TAG);
270a4abeea4SJosef Bacik 		spin_unlock(&root->fs_info->fs_roots_radix_lock);
2717585717fSChris Mason 		root->last_trans = trans->transid;
2727585717fSChris Mason 
2737585717fSChris Mason 		/* this is pretty tricky.  We don't want to
2747585717fSChris Mason 		 * take the relocation lock in btrfs_record_root_in_trans
2757585717fSChris Mason 		 * unless we're really doing the first setup for this root in
2767585717fSChris Mason 		 * this transaction.
2777585717fSChris Mason 		 *
2787585717fSChris Mason 		 * Normally we'd use root->last_trans as a flag to decide
2797585717fSChris Mason 		 * if we want to take the expensive mutex.
2807585717fSChris Mason 		 *
2817585717fSChris Mason 		 * But, we have to set root->last_trans before we
2827585717fSChris Mason 		 * init the relocation root, otherwise, we trip over warnings
2837585717fSChris Mason 		 * in ctree.c.  The solution used here is to flag ourselves
28427cdeb70SMiao Xie 		 * with root IN_TRANS_SETUP.  When this is 1, we're still
2857585717fSChris Mason 		 * fixing up the reloc trees and everyone must wait.
2867585717fSChris Mason 		 *
2877585717fSChris Mason 		 * When this is zero, they can trust root->last_trans and fly
2887585717fSChris Mason 		 * through btrfs_record_root_in_trans without having to take the
2897585717fSChris Mason 		 * lock.  smp_wmb() makes sure that all the writes above are
2907585717fSChris Mason 		 * done before we pop in the zero below
2917585717fSChris Mason 		 */
2925d4f98a2SYan Zheng 		btrfs_init_reloc_root(trans, root);
29327cdeb70SMiao Xie 		smp_mb__before_clear_bit();
29427cdeb70SMiao Xie 		clear_bit(BTRFS_ROOT_IN_TRANS_SETUP, &root->state);
2956702ed49SChris Mason 	}
2965d4f98a2SYan Zheng 	return 0;
2976702ed49SChris Mason }
2985d4f98a2SYan Zheng 
2997585717fSChris Mason 
3007585717fSChris Mason int btrfs_record_root_in_trans(struct btrfs_trans_handle *trans,
3017585717fSChris Mason 			       struct btrfs_root *root)
3027585717fSChris Mason {
30327cdeb70SMiao Xie 	if (!test_bit(BTRFS_ROOT_REF_COWS, &root->state))
3047585717fSChris Mason 		return 0;
3057585717fSChris Mason 
3067585717fSChris Mason 	/*
30727cdeb70SMiao Xie 	 * see record_root_in_trans for comments about IN_TRANS_SETUP usage
3087585717fSChris Mason 	 * and barriers
3097585717fSChris Mason 	 */
3107585717fSChris Mason 	smp_rmb();
3117585717fSChris Mason 	if (root->last_trans == trans->transid &&
31227cdeb70SMiao Xie 	    !test_bit(BTRFS_ROOT_IN_TRANS_SETUP, &root->state))
3137585717fSChris Mason 		return 0;
3147585717fSChris Mason 
3157585717fSChris Mason 	mutex_lock(&root->fs_info->reloc_mutex);
3167585717fSChris Mason 	record_root_in_trans(trans, root);
3177585717fSChris Mason 	mutex_unlock(&root->fs_info->reloc_mutex);
3187585717fSChris Mason 
3197585717fSChris Mason 	return 0;
3207585717fSChris Mason }
3217585717fSChris Mason 
3224a9d8bdeSMiao Xie static inline int is_transaction_blocked(struct btrfs_transaction *trans)
3234a9d8bdeSMiao Xie {
3244a9d8bdeSMiao Xie 	return (trans->state >= TRANS_STATE_BLOCKED &&
325501407aaSJosef Bacik 		trans->state < TRANS_STATE_UNBLOCKED &&
326501407aaSJosef Bacik 		!trans->aborted);
3274a9d8bdeSMiao Xie }
3284a9d8bdeSMiao Xie 
329d352ac68SChris Mason /* wait for commit against the current transaction to become unblocked
330d352ac68SChris Mason  * when this is done, it is safe to start a new transaction, but the current
331d352ac68SChris Mason  * transaction might not be fully on disk.
332d352ac68SChris Mason  */
33337d1aeeeSChris Mason static void wait_current_trans(struct btrfs_root *root)
33479154b1bSChris Mason {
335f9295749SChris Mason 	struct btrfs_transaction *cur_trans;
33679154b1bSChris Mason 
337a4abeea4SJosef Bacik 	spin_lock(&root->fs_info->trans_lock);
338f9295749SChris Mason 	cur_trans = root->fs_info->running_transaction;
3394a9d8bdeSMiao Xie 	if (cur_trans && is_transaction_blocked(cur_trans)) {
34013c5a93eSJosef Bacik 		atomic_inc(&cur_trans->use_count);
341a4abeea4SJosef Bacik 		spin_unlock(&root->fs_info->trans_lock);
34272d63ed6SLi Zefan 
34372d63ed6SLi Zefan 		wait_event(root->fs_info->transaction_wait,
344501407aaSJosef Bacik 			   cur_trans->state >= TRANS_STATE_UNBLOCKED ||
345501407aaSJosef Bacik 			   cur_trans->aborted);
346724e2315SJosef Bacik 		btrfs_put_transaction(cur_trans);
347a4abeea4SJosef Bacik 	} else {
348a4abeea4SJosef Bacik 		spin_unlock(&root->fs_info->trans_lock);
349f9295749SChris Mason 	}
35037d1aeeeSChris Mason }
35137d1aeeeSChris Mason 
352a22285a6SYan, Zheng static int may_wait_transaction(struct btrfs_root *root, int type)
35337d1aeeeSChris Mason {
354a4abeea4SJosef Bacik 	if (root->fs_info->log_root_recovering)
355a4abeea4SJosef Bacik 		return 0;
356a4abeea4SJosef Bacik 
357a4abeea4SJosef Bacik 	if (type == TRANS_USERSPACE)
358a22285a6SYan, Zheng 		return 1;
359a4abeea4SJosef Bacik 
360a4abeea4SJosef Bacik 	if (type == TRANS_START &&
361a4abeea4SJosef Bacik 	    !atomic_read(&root->fs_info->open_ioctl_trans))
362a4abeea4SJosef Bacik 		return 1;
363a4abeea4SJosef Bacik 
364a22285a6SYan, Zheng 	return 0;
365a22285a6SYan, Zheng }
366a22285a6SYan, Zheng 
36720dd2cbfSMiao Xie static inline bool need_reserve_reloc_root(struct btrfs_root *root)
36820dd2cbfSMiao Xie {
36920dd2cbfSMiao Xie 	if (!root->fs_info->reloc_ctl ||
37027cdeb70SMiao Xie 	    !test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
37120dd2cbfSMiao Xie 	    root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID ||
37220dd2cbfSMiao Xie 	    root->reloc_root)
37320dd2cbfSMiao Xie 		return false;
37420dd2cbfSMiao Xie 
37520dd2cbfSMiao Xie 	return true;
37620dd2cbfSMiao Xie }
37720dd2cbfSMiao Xie 
37808e007d2SMiao Xie static struct btrfs_trans_handle *
3790860adfdSMiao Xie start_transaction(struct btrfs_root *root, u64 num_items, unsigned int type,
38008e007d2SMiao Xie 		  enum btrfs_reserve_flush_enum flush)
381a22285a6SYan, Zheng {
382a22285a6SYan, Zheng 	struct btrfs_trans_handle *h;
383a22285a6SYan, Zheng 	struct btrfs_transaction *cur_trans;
384b5009945SJosef Bacik 	u64 num_bytes = 0;
385c5567237SArne Jansen 	u64 qgroup_reserved = 0;
38620dd2cbfSMiao Xie 	bool reloc_reserved = false;
38720dd2cbfSMiao Xie 	int ret;
388acce952bSliubo 
38987533c47SMiao Xie 	if (test_bit(BTRFS_FS_STATE_ERROR, &root->fs_info->fs_state))
390acce952bSliubo 		return ERR_PTR(-EROFS);
3912a1eb461SJosef Bacik 
392a26e8c9fSJosef Bacik 	if (current->journal_info &&
393a26e8c9fSJosef Bacik 	    current->journal_info != (void *)BTRFS_SEND_TRANS_STUB) {
3940860adfdSMiao Xie 		WARN_ON(type & TRANS_EXTWRITERS);
3952a1eb461SJosef Bacik 		h = current->journal_info;
3962a1eb461SJosef Bacik 		h->use_count++;
397b7d5b0a8SMiao Xie 		WARN_ON(h->use_count > 2);
3982a1eb461SJosef Bacik 		h->orig_rsv = h->block_rsv;
3992a1eb461SJosef Bacik 		h->block_rsv = NULL;
4002a1eb461SJosef Bacik 		goto got_it;
4012a1eb461SJosef Bacik 	}
402b5009945SJosef Bacik 
403b5009945SJosef Bacik 	/*
404b5009945SJosef Bacik 	 * Do the reservation before we join the transaction so we can do all
405b5009945SJosef Bacik 	 * the appropriate flushing if need be.
406b5009945SJosef Bacik 	 */
407b5009945SJosef Bacik 	if (num_items > 0 && root != root->fs_info->chunk_root) {
408c5567237SArne Jansen 		if (root->fs_info->quota_enabled &&
409c5567237SArne Jansen 		    is_fstree(root->root_key.objectid)) {
410c5567237SArne Jansen 			qgroup_reserved = num_items * root->leafsize;
411c5567237SArne Jansen 			ret = btrfs_qgroup_reserve(root, qgroup_reserved);
412c5567237SArne Jansen 			if (ret)
413c5567237SArne Jansen 				return ERR_PTR(ret);
414c5567237SArne Jansen 		}
415c5567237SArne Jansen 
416b5009945SJosef Bacik 		num_bytes = btrfs_calc_trans_metadata_size(root, num_items);
41720dd2cbfSMiao Xie 		/*
41820dd2cbfSMiao Xie 		 * Do the reservation for the relocation root creation
41920dd2cbfSMiao Xie 		 */
42020dd2cbfSMiao Xie 		if (unlikely(need_reserve_reloc_root(root))) {
42120dd2cbfSMiao Xie 			num_bytes += root->nodesize;
42220dd2cbfSMiao Xie 			reloc_reserved = true;
42320dd2cbfSMiao Xie 		}
42420dd2cbfSMiao Xie 
4254a92b1b8SJosef Bacik 		ret = btrfs_block_rsv_add(root,
426b5009945SJosef Bacik 					  &root->fs_info->trans_block_rsv,
42708e007d2SMiao Xie 					  num_bytes, flush);
428b5009945SJosef Bacik 		if (ret)
429843fcf35SMiao Xie 			goto reserve_fail;
430b5009945SJosef Bacik 	}
431a22285a6SYan, Zheng again:
432a22285a6SYan, Zheng 	h = kmem_cache_alloc(btrfs_trans_handle_cachep, GFP_NOFS);
433843fcf35SMiao Xie 	if (!h) {
434843fcf35SMiao Xie 		ret = -ENOMEM;
435843fcf35SMiao Xie 		goto alloc_fail;
436843fcf35SMiao Xie 	}
437a22285a6SYan, Zheng 
43898114659SJosef Bacik 	/*
43998114659SJosef Bacik 	 * If we are JOIN_NOLOCK we're already committing a transaction and
44098114659SJosef Bacik 	 * waiting on this guy, so we don't need to do the sb_start_intwrite
44198114659SJosef Bacik 	 * because we're already holding a ref.  We need this because we could
44298114659SJosef Bacik 	 * have raced in and did an fsync() on a file which can kick a commit
44398114659SJosef Bacik 	 * and then we deadlock with somebody doing a freeze.
444354aa0fbSMiao Xie 	 *
445354aa0fbSMiao Xie 	 * If we are ATTACH, it means we just want to catch the current
446354aa0fbSMiao Xie 	 * transaction and commit it, so we needn't do sb_start_intwrite().
44798114659SJosef Bacik 	 */
4480860adfdSMiao Xie 	if (type & __TRANS_FREEZABLE)
449b2b5ef5cSJan Kara 		sb_start_intwrite(root->fs_info->sb);
450b2b5ef5cSJan Kara 
451a22285a6SYan, Zheng 	if (may_wait_transaction(root, type))
45237d1aeeeSChris Mason 		wait_current_trans(root);
453a22285a6SYan, Zheng 
454a4abeea4SJosef Bacik 	do {
455354aa0fbSMiao Xie 		ret = join_transaction(root, type);
456178260b2SMiao Xie 		if (ret == -EBUSY) {
457a4abeea4SJosef Bacik 			wait_current_trans(root);
458178260b2SMiao Xie 			if (unlikely(type == TRANS_ATTACH))
459178260b2SMiao Xie 				ret = -ENOENT;
460178260b2SMiao Xie 		}
461a4abeea4SJosef Bacik 	} while (ret == -EBUSY);
462a4abeea4SJosef Bacik 
463db5b493aSTsutomu Itoh 	if (ret < 0) {
464354aa0fbSMiao Xie 		/* We must get the transaction if we are JOIN_NOLOCK. */
465354aa0fbSMiao Xie 		BUG_ON(type == TRANS_JOIN_NOLOCK);
466843fcf35SMiao Xie 		goto join_fail;
467db5b493aSTsutomu Itoh 	}
4680f7d52f4SChris Mason 
469a22285a6SYan, Zheng 	cur_trans = root->fs_info->running_transaction;
470a22285a6SYan, Zheng 
471a22285a6SYan, Zheng 	h->transid = cur_trans->transid;
472a22285a6SYan, Zheng 	h->transaction = cur_trans;
47379154b1bSChris Mason 	h->blocks_used = 0;
474a22285a6SYan, Zheng 	h->bytes_reserved = 0;
475d13603efSArne Jansen 	h->root = root;
47656bec294SChris Mason 	h->delayed_ref_updates = 0;
4772a1eb461SJosef Bacik 	h->use_count = 1;
4780e721106SJosef Bacik 	h->adding_csums = 0;
479f0486c68SYan, Zheng 	h->block_rsv = NULL;
4802a1eb461SJosef Bacik 	h->orig_rsv = NULL;
48149b25e05SJeff Mahoney 	h->aborted = 0;
4824b824906SMiao Xie 	h->qgroup_reserved = 0;
483bed92eaeSArne Jansen 	h->delayed_ref_elem.seq = 0;
484a698d075SMiao Xie 	h->type = type;
485c6b305a8SJosef Bacik 	h->allocating_chunk = false;
48620dd2cbfSMiao Xie 	h->reloc_reserved = false;
4875039eddcSJosef Bacik 	h->sync = false;
488bed92eaeSArne Jansen 	INIT_LIST_HEAD(&h->qgroup_ref_list);
489ea658badSJosef Bacik 	INIT_LIST_HEAD(&h->new_bgs);
490b7ec40d7SChris Mason 
491a22285a6SYan, Zheng 	smp_mb();
4924a9d8bdeSMiao Xie 	if (cur_trans->state >= TRANS_STATE_BLOCKED &&
4934a9d8bdeSMiao Xie 	    may_wait_transaction(root, type)) {
494a22285a6SYan, Zheng 		btrfs_commit_transaction(h, root);
495a22285a6SYan, Zheng 		goto again;
496a22285a6SYan, Zheng 	}
4979ed74f2dSJosef Bacik 
498b5009945SJosef Bacik 	if (num_bytes) {
4998c2a3ca2SJosef Bacik 		trace_btrfs_space_reservation(root->fs_info, "transaction",
5002bcc0328SLiu Bo 					      h->transid, num_bytes, 1);
501b5009945SJosef Bacik 		h->block_rsv = &root->fs_info->trans_block_rsv;
502b5009945SJosef Bacik 		h->bytes_reserved = num_bytes;
50320dd2cbfSMiao Xie 		h->reloc_reserved = reloc_reserved;
504a22285a6SYan, Zheng 	}
5054b824906SMiao Xie 	h->qgroup_reserved = qgroup_reserved;
506a22285a6SYan, Zheng 
5072a1eb461SJosef Bacik got_it:
508a4abeea4SJosef Bacik 	btrfs_record_root_in_trans(h, root);
509a22285a6SYan, Zheng 
510a22285a6SYan, Zheng 	if (!current->journal_info && type != TRANS_USERSPACE)
511a22285a6SYan, Zheng 		current->journal_info = h;
51279154b1bSChris Mason 	return h;
513843fcf35SMiao Xie 
514843fcf35SMiao Xie join_fail:
5150860adfdSMiao Xie 	if (type & __TRANS_FREEZABLE)
516843fcf35SMiao Xie 		sb_end_intwrite(root->fs_info->sb);
517843fcf35SMiao Xie 	kmem_cache_free(btrfs_trans_handle_cachep, h);
518843fcf35SMiao Xie alloc_fail:
519843fcf35SMiao Xie 	if (num_bytes)
520843fcf35SMiao Xie 		btrfs_block_rsv_release(root, &root->fs_info->trans_block_rsv,
521843fcf35SMiao Xie 					num_bytes);
522843fcf35SMiao Xie reserve_fail:
523843fcf35SMiao Xie 	if (qgroup_reserved)
524843fcf35SMiao Xie 		btrfs_qgroup_free(root, qgroup_reserved);
525843fcf35SMiao Xie 	return ERR_PTR(ret);
52679154b1bSChris Mason }
52779154b1bSChris Mason 
528f9295749SChris Mason struct btrfs_trans_handle *btrfs_start_transaction(struct btrfs_root *root,
529a22285a6SYan, Zheng 						   int num_items)
530f9295749SChris Mason {
53108e007d2SMiao Xie 	return start_transaction(root, num_items, TRANS_START,
53208e007d2SMiao Xie 				 BTRFS_RESERVE_FLUSH_ALL);
533f9295749SChris Mason }
5348407aa46SMiao Xie 
53508e007d2SMiao Xie struct btrfs_trans_handle *btrfs_start_transaction_lflush(
5368407aa46SMiao Xie 					struct btrfs_root *root, int num_items)
5378407aa46SMiao Xie {
53808e007d2SMiao Xie 	return start_transaction(root, num_items, TRANS_START,
53908e007d2SMiao Xie 				 BTRFS_RESERVE_FLUSH_LIMIT);
5408407aa46SMiao Xie }
5418407aa46SMiao Xie 
5427a7eaa40SJosef Bacik struct btrfs_trans_handle *btrfs_join_transaction(struct btrfs_root *root)
543f9295749SChris Mason {
5448407aa46SMiao Xie 	return start_transaction(root, 0, TRANS_JOIN, 0);
545f9295749SChris Mason }
546f9295749SChris Mason 
5477a7eaa40SJosef Bacik struct btrfs_trans_handle *btrfs_join_transaction_nolock(struct btrfs_root *root)
5480af3d00bSJosef Bacik {
5498407aa46SMiao Xie 	return start_transaction(root, 0, TRANS_JOIN_NOLOCK, 0);
5500af3d00bSJosef Bacik }
5510af3d00bSJosef Bacik 
5527a7eaa40SJosef Bacik struct btrfs_trans_handle *btrfs_start_ioctl_transaction(struct btrfs_root *root)
5539ca9ee09SSage Weil {
5548407aa46SMiao Xie 	return start_transaction(root, 0, TRANS_USERSPACE, 0);
5559ca9ee09SSage Weil }
5569ca9ee09SSage Weil 
557d4edf39bSMiao Xie /*
558d4edf39bSMiao Xie  * btrfs_attach_transaction() - catch the running transaction
559d4edf39bSMiao Xie  *
560d4edf39bSMiao Xie  * It is used when we want to commit the current the transaction, but
561d4edf39bSMiao Xie  * don't want to start a new one.
562d4edf39bSMiao Xie  *
563d4edf39bSMiao Xie  * Note: If this function return -ENOENT, it just means there is no
564d4edf39bSMiao Xie  * running transaction. But it is possible that the inactive transaction
565d4edf39bSMiao Xie  * is still in the memory, not fully on disk. If you hope there is no
566d4edf39bSMiao Xie  * inactive transaction in the fs when -ENOENT is returned, you should
567d4edf39bSMiao Xie  * invoke
568d4edf39bSMiao Xie  *     btrfs_attach_transaction_barrier()
569d4edf39bSMiao Xie  */
570354aa0fbSMiao Xie struct btrfs_trans_handle *btrfs_attach_transaction(struct btrfs_root *root)
57160376ce4SJosef Bacik {
572354aa0fbSMiao Xie 	return start_transaction(root, 0, TRANS_ATTACH, 0);
57360376ce4SJosef Bacik }
57460376ce4SJosef Bacik 
575d4edf39bSMiao Xie /*
57690b6d283SWang Sheng-Hui  * btrfs_attach_transaction_barrier() - catch the running transaction
577d4edf39bSMiao Xie  *
578d4edf39bSMiao Xie  * It is similar to the above function, the differentia is this one
579d4edf39bSMiao Xie  * will wait for all the inactive transactions until they fully
580d4edf39bSMiao Xie  * complete.
581d4edf39bSMiao Xie  */
582d4edf39bSMiao Xie struct btrfs_trans_handle *
583d4edf39bSMiao Xie btrfs_attach_transaction_barrier(struct btrfs_root *root)
584d4edf39bSMiao Xie {
585d4edf39bSMiao Xie 	struct btrfs_trans_handle *trans;
586d4edf39bSMiao Xie 
587d4edf39bSMiao Xie 	trans = start_transaction(root, 0, TRANS_ATTACH, 0);
588d4edf39bSMiao Xie 	if (IS_ERR(trans) && PTR_ERR(trans) == -ENOENT)
589d4edf39bSMiao Xie 		btrfs_wait_for_commit(root, 0);
590d4edf39bSMiao Xie 
591d4edf39bSMiao Xie 	return trans;
592d4edf39bSMiao Xie }
593d4edf39bSMiao Xie 
594d352ac68SChris Mason /* wait for a transaction commit to be fully complete */
595b9c8300cSLi Zefan static noinline void wait_for_commit(struct btrfs_root *root,
59689ce8a63SChris Mason 				    struct btrfs_transaction *commit)
59789ce8a63SChris Mason {
5984a9d8bdeSMiao Xie 	wait_event(commit->commit_wait, commit->state == TRANS_STATE_COMPLETED);
59989ce8a63SChris Mason }
60089ce8a63SChris Mason 
60146204592SSage Weil int btrfs_wait_for_commit(struct btrfs_root *root, u64 transid)
60246204592SSage Weil {
60346204592SSage Weil 	struct btrfs_transaction *cur_trans = NULL, *t;
6048cd2807fSMiao Xie 	int ret = 0;
60546204592SSage Weil 
60646204592SSage Weil 	if (transid) {
60746204592SSage Weil 		if (transid <= root->fs_info->last_trans_committed)
608a4abeea4SJosef Bacik 			goto out;
60946204592SSage Weil 
6108cd2807fSMiao Xie 		ret = -EINVAL;
61146204592SSage Weil 		/* find specified transaction */
612a4abeea4SJosef Bacik 		spin_lock(&root->fs_info->trans_lock);
61346204592SSage Weil 		list_for_each_entry(t, &root->fs_info->trans_list, list) {
61446204592SSage Weil 			if (t->transid == transid) {
61546204592SSage Weil 				cur_trans = t;
616a4abeea4SJosef Bacik 				atomic_inc(&cur_trans->use_count);
6178cd2807fSMiao Xie 				ret = 0;
61846204592SSage Weil 				break;
61946204592SSage Weil 			}
6208cd2807fSMiao Xie 			if (t->transid > transid) {
6218cd2807fSMiao Xie 				ret = 0;
62246204592SSage Weil 				break;
62346204592SSage Weil 			}
6248cd2807fSMiao Xie 		}
625a4abeea4SJosef Bacik 		spin_unlock(&root->fs_info->trans_lock);
6268cd2807fSMiao Xie 		/* The specified transaction doesn't exist */
62746204592SSage Weil 		if (!cur_trans)
6288cd2807fSMiao Xie 			goto out;
62946204592SSage Weil 	} else {
63046204592SSage Weil 		/* find newest transaction that is committing | committed */
631a4abeea4SJosef Bacik 		spin_lock(&root->fs_info->trans_lock);
63246204592SSage Weil 		list_for_each_entry_reverse(t, &root->fs_info->trans_list,
63346204592SSage Weil 					    list) {
6344a9d8bdeSMiao Xie 			if (t->state >= TRANS_STATE_COMMIT_START) {
6354a9d8bdeSMiao Xie 				if (t->state == TRANS_STATE_COMPLETED)
6363473f3c0SJosef Bacik 					break;
63746204592SSage Weil 				cur_trans = t;
638a4abeea4SJosef Bacik 				atomic_inc(&cur_trans->use_count);
63946204592SSage Weil 				break;
64046204592SSage Weil 			}
64146204592SSage Weil 		}
642a4abeea4SJosef Bacik 		spin_unlock(&root->fs_info->trans_lock);
64346204592SSage Weil 		if (!cur_trans)
644a4abeea4SJosef Bacik 			goto out;  /* nothing committing|committed */
64546204592SSage Weil 	}
64646204592SSage Weil 
64746204592SSage Weil 	wait_for_commit(root, cur_trans);
648724e2315SJosef Bacik 	btrfs_put_transaction(cur_trans);
649a4abeea4SJosef Bacik out:
65046204592SSage Weil 	return ret;
65146204592SSage Weil }
65246204592SSage Weil 
65337d1aeeeSChris Mason void btrfs_throttle(struct btrfs_root *root)
65437d1aeeeSChris Mason {
655a4abeea4SJosef Bacik 	if (!atomic_read(&root->fs_info->open_ioctl_trans))
65637d1aeeeSChris Mason 		wait_current_trans(root);
65737d1aeeeSChris Mason }
65837d1aeeeSChris Mason 
6598929ecfaSYan, Zheng static int should_end_transaction(struct btrfs_trans_handle *trans,
6608929ecfaSYan, Zheng 				  struct btrfs_root *root)
6618929ecfaSYan, Zheng {
6621be41b78SJosef Bacik 	if (root->fs_info->global_block_rsv.space_info->full &&
6630a2b2a84SJosef Bacik 	    btrfs_check_space_for_delayed_refs(trans, root))
6641be41b78SJosef Bacik 		return 1;
66536ba022aSJosef Bacik 
6661be41b78SJosef Bacik 	return !!btrfs_block_rsv_check(root, &root->fs_info->global_block_rsv, 5);
6678929ecfaSYan, Zheng }
6688929ecfaSYan, Zheng 
6698929ecfaSYan, Zheng int btrfs_should_end_transaction(struct btrfs_trans_handle *trans,
6708929ecfaSYan, Zheng 				 struct btrfs_root *root)
6718929ecfaSYan, Zheng {
6728929ecfaSYan, Zheng 	struct btrfs_transaction *cur_trans = trans->transaction;
6738929ecfaSYan, Zheng 	int updates;
67449b25e05SJeff Mahoney 	int err;
6758929ecfaSYan, Zheng 
676a4abeea4SJosef Bacik 	smp_mb();
6774a9d8bdeSMiao Xie 	if (cur_trans->state >= TRANS_STATE_BLOCKED ||
6784a9d8bdeSMiao Xie 	    cur_trans->delayed_refs.flushing)
6798929ecfaSYan, Zheng 		return 1;
6808929ecfaSYan, Zheng 
6818929ecfaSYan, Zheng 	updates = trans->delayed_ref_updates;
6828929ecfaSYan, Zheng 	trans->delayed_ref_updates = 0;
68349b25e05SJeff Mahoney 	if (updates) {
68449b25e05SJeff Mahoney 		err = btrfs_run_delayed_refs(trans, root, updates);
68549b25e05SJeff Mahoney 		if (err) /* Error code will also eval true */
68649b25e05SJeff Mahoney 			return err;
68749b25e05SJeff Mahoney 	}
6888929ecfaSYan, Zheng 
6898929ecfaSYan, Zheng 	return should_end_transaction(trans, root);
6908929ecfaSYan, Zheng }
6918929ecfaSYan, Zheng 
69289ce8a63SChris Mason static int __btrfs_end_transaction(struct btrfs_trans_handle *trans,
693a698d075SMiao Xie 			  struct btrfs_root *root, int throttle)
69479154b1bSChris Mason {
6958929ecfaSYan, Zheng 	struct btrfs_transaction *cur_trans = trans->transaction;
696ab78c84dSChris Mason 	struct btrfs_fs_info *info = root->fs_info;
6971be41b78SJosef Bacik 	unsigned long cur = trans->delayed_ref_updates;
698a698d075SMiao Xie 	int lock = (trans->type != TRANS_JOIN_NOLOCK);
6994edc2ca3SDave Jones 	int err = 0;
700d6e4a428SChris Mason 
7013bbb24b2SJosef Bacik 	if (trans->use_count > 1) {
7023bbb24b2SJosef Bacik 		trans->use_count--;
7032a1eb461SJosef Bacik 		trans->block_rsv = trans->orig_rsv;
7042a1eb461SJosef Bacik 		return 0;
7052a1eb461SJosef Bacik 	}
7062a1eb461SJosef Bacik 
707b24e03dbSJosef Bacik 	btrfs_trans_release_metadata(trans, root);
7084c13d758SJosef Bacik 	trans->block_rsv = NULL;
709c5567237SArne Jansen 
710ea658badSJosef Bacik 	if (!list_empty(&trans->new_bgs))
711ea658badSJosef Bacik 		btrfs_create_pending_block_groups(trans, root);
712ea658badSJosef Bacik 
713c3e69d58SChris Mason 	trans->delayed_ref_updates = 0;
7145039eddcSJosef Bacik 	if (!trans->sync && btrfs_should_throttle_delayed_refs(trans, root)) {
7150a2b2a84SJosef Bacik 		cur = max_t(unsigned long, cur, 32);
716c3e69d58SChris Mason 		trans->delayed_ref_updates = 0;
717c3e69d58SChris Mason 		btrfs_run_delayed_refs(trans, root, cur);
71856bec294SChris Mason 	}
719bb721703SChris Mason 
720fcebe456SJosef Bacik 	if (trans->qgroup_reserved) {
721fcebe456SJosef Bacik 		/*
722fcebe456SJosef Bacik 		 * the same root has to be passed here between start_transaction
723fcebe456SJosef Bacik 		 * and end_transaction. Subvolume quota depends on this.
724fcebe456SJosef Bacik 		 */
725fcebe456SJosef Bacik 		btrfs_qgroup_free(trans->root, trans->qgroup_reserved);
726fcebe456SJosef Bacik 		trans->qgroup_reserved = 0;
727fcebe456SJosef Bacik 	}
728fcebe456SJosef Bacik 
7290e721106SJosef Bacik 	btrfs_trans_release_metadata(trans, root);
7300e721106SJosef Bacik 	trans->block_rsv = NULL;
73156bec294SChris Mason 
732ea658badSJosef Bacik 	if (!list_empty(&trans->new_bgs))
733ea658badSJosef Bacik 		btrfs_create_pending_block_groups(trans, root);
734ea658badSJosef Bacik 
735a4abeea4SJosef Bacik 	if (lock && !atomic_read(&root->fs_info->open_ioctl_trans) &&
7364a9d8bdeSMiao Xie 	    should_end_transaction(trans, root) &&
7374a9d8bdeSMiao Xie 	    ACCESS_ONCE(cur_trans->state) == TRANS_STATE_RUNNING) {
7384a9d8bdeSMiao Xie 		spin_lock(&info->trans_lock);
7394a9d8bdeSMiao Xie 		if (cur_trans->state == TRANS_STATE_RUNNING)
7404a9d8bdeSMiao Xie 			cur_trans->state = TRANS_STATE_BLOCKED;
7414a9d8bdeSMiao Xie 		spin_unlock(&info->trans_lock);
742a4abeea4SJosef Bacik 	}
7438929ecfaSYan, Zheng 
7444a9d8bdeSMiao Xie 	if (lock && ACCESS_ONCE(cur_trans->state) == TRANS_STATE_BLOCKED) {
7453bbb24b2SJosef Bacik 		if (throttle)
7468929ecfaSYan, Zheng 			return btrfs_commit_transaction(trans, root);
7473bbb24b2SJosef Bacik 		else
7488929ecfaSYan, Zheng 			wake_up_process(info->transaction_kthread);
7498929ecfaSYan, Zheng 	}
7508929ecfaSYan, Zheng 
7510860adfdSMiao Xie 	if (trans->type & __TRANS_FREEZABLE)
7526df7881aSJosef Bacik 		sb_end_intwrite(root->fs_info->sb);
7536df7881aSJosef Bacik 
7548929ecfaSYan, Zheng 	WARN_ON(cur_trans != info->running_transaction);
75513c5a93eSJosef Bacik 	WARN_ON(atomic_read(&cur_trans->num_writers) < 1);
75613c5a93eSJosef Bacik 	atomic_dec(&cur_trans->num_writers);
7570860adfdSMiao Xie 	extwriter_counter_dec(cur_trans, trans->type);
75889ce8a63SChris Mason 
75999d16cbcSSage Weil 	smp_mb();
76079154b1bSChris Mason 	if (waitqueue_active(&cur_trans->writer_wait))
76179154b1bSChris Mason 		wake_up(&cur_trans->writer_wait);
762724e2315SJosef Bacik 	btrfs_put_transaction(cur_trans);
7639ed74f2dSJosef Bacik 
7649ed74f2dSJosef Bacik 	if (current->journal_info == trans)
7659ed74f2dSJosef Bacik 		current->journal_info = NULL;
766ab78c84dSChris Mason 
76724bbcf04SYan, Zheng 	if (throttle)
76824bbcf04SYan, Zheng 		btrfs_run_delayed_iputs(root);
76924bbcf04SYan, Zheng 
77049b25e05SJeff Mahoney 	if (trans->aborted ||
7714e121c06SJosef Bacik 	    test_bit(BTRFS_FS_STATE_ERROR, &root->fs_info->fs_state)) {
7724e121c06SJosef Bacik 		wake_up_process(info->transaction_kthread);
7734edc2ca3SDave Jones 		err = -EIO;
7744e121c06SJosef Bacik 	}
775edf39272SJan Schmidt 	assert_qgroups_uptodate(trans);
77649b25e05SJeff Mahoney 
7774edc2ca3SDave Jones 	kmem_cache_free(btrfs_trans_handle_cachep, trans);
7784edc2ca3SDave Jones 	return err;
77979154b1bSChris Mason }
78079154b1bSChris Mason 
78189ce8a63SChris Mason int btrfs_end_transaction(struct btrfs_trans_handle *trans,
78289ce8a63SChris Mason 			  struct btrfs_root *root)
78389ce8a63SChris Mason {
78498ad43beSWang Shilong 	return __btrfs_end_transaction(trans, root, 0);
78589ce8a63SChris Mason }
78689ce8a63SChris Mason 
78789ce8a63SChris Mason int btrfs_end_transaction_throttle(struct btrfs_trans_handle *trans,
78889ce8a63SChris Mason 				   struct btrfs_root *root)
78989ce8a63SChris Mason {
79098ad43beSWang Shilong 	return __btrfs_end_transaction(trans, root, 1);
79116cdcec7SMiao Xie }
79216cdcec7SMiao Xie 
793d352ac68SChris Mason /*
794d352ac68SChris Mason  * when btree blocks are allocated, they have some corresponding bits set for
795d352ac68SChris Mason  * them in one of two extent_io trees.  This is used to make sure all of
796690587d1SChris Mason  * those extents are sent to disk but does not wait on them
797d352ac68SChris Mason  */
798690587d1SChris Mason int btrfs_write_marked_extents(struct btrfs_root *root,
7998cef4e16SYan, Zheng 			       struct extent_io_tree *dirty_pages, int mark)
80079154b1bSChris Mason {
801777e6bd7SChris Mason 	int err = 0;
8027c4452b9SChris Mason 	int werr = 0;
8031728366eSJosef Bacik 	struct address_space *mapping = root->fs_info->btree_inode->i_mapping;
804e6138876SJosef Bacik 	struct extent_state *cached_state = NULL;
805777e6bd7SChris Mason 	u64 start = 0;
8065f39d397SChris Mason 	u64 end;
8077c4452b9SChris Mason 
8081728366eSJosef Bacik 	while (!find_first_extent_bit(dirty_pages, start, &start, &end,
809e6138876SJosef Bacik 				      mark, &cached_state)) {
810e6138876SJosef Bacik 		convert_extent_bit(dirty_pages, start, end, EXTENT_NEED_WAIT,
811e6138876SJosef Bacik 				   mark, &cached_state, GFP_NOFS);
812e6138876SJosef Bacik 		cached_state = NULL;
8131728366eSJosef Bacik 		err = filemap_fdatawrite_range(mapping, start, end);
8147c4452b9SChris Mason 		if (err)
8157c4452b9SChris Mason 			werr = err;
8161728366eSJosef Bacik 		cond_resched();
8171728366eSJosef Bacik 		start = end + 1;
8187c4452b9SChris Mason 	}
819690587d1SChris Mason 	if (err)
820690587d1SChris Mason 		werr = err;
821690587d1SChris Mason 	return werr;
822690587d1SChris Mason }
823690587d1SChris Mason 
824690587d1SChris Mason /*
825690587d1SChris Mason  * when btree blocks are allocated, they have some corresponding bits set for
826690587d1SChris Mason  * them in one of two extent_io trees.  This is used to make sure all of
827690587d1SChris Mason  * those extents are on disk for transaction or log commit.  We wait
828690587d1SChris Mason  * on all the pages and clear them from the dirty pages state tree
829690587d1SChris Mason  */
830690587d1SChris Mason int btrfs_wait_marked_extents(struct btrfs_root *root,
8318cef4e16SYan, Zheng 			      struct extent_io_tree *dirty_pages, int mark)
832690587d1SChris Mason {
833690587d1SChris Mason 	int err = 0;
834690587d1SChris Mason 	int werr = 0;
8351728366eSJosef Bacik 	struct address_space *mapping = root->fs_info->btree_inode->i_mapping;
836e6138876SJosef Bacik 	struct extent_state *cached_state = NULL;
837690587d1SChris Mason 	u64 start = 0;
838690587d1SChris Mason 	u64 end;
839690587d1SChris Mason 
8401728366eSJosef Bacik 	while (!find_first_extent_bit(dirty_pages, start, &start, &end,
841e6138876SJosef Bacik 				      EXTENT_NEED_WAIT, &cached_state)) {
842e6138876SJosef Bacik 		clear_extent_bit(dirty_pages, start, end, EXTENT_NEED_WAIT,
843e6138876SJosef Bacik 				 0, 0, &cached_state, GFP_NOFS);
8441728366eSJosef Bacik 		err = filemap_fdatawait_range(mapping, start, end);
845777e6bd7SChris Mason 		if (err)
846777e6bd7SChris Mason 			werr = err;
847777e6bd7SChris Mason 		cond_resched();
8481728366eSJosef Bacik 		start = end + 1;
849777e6bd7SChris Mason 	}
8507c4452b9SChris Mason 	if (err)
8517c4452b9SChris Mason 		werr = err;
8527c4452b9SChris Mason 	return werr;
85379154b1bSChris Mason }
85479154b1bSChris Mason 
855690587d1SChris Mason /*
856690587d1SChris Mason  * when btree blocks are allocated, they have some corresponding bits set for
857690587d1SChris Mason  * them in one of two extent_io trees.  This is used to make sure all of
858690587d1SChris Mason  * those extents are on disk for transaction or log commit
859690587d1SChris Mason  */
860171170c1SSergei Trofimovich static int btrfs_write_and_wait_marked_extents(struct btrfs_root *root,
8618cef4e16SYan, Zheng 				struct extent_io_tree *dirty_pages, int mark)
862690587d1SChris Mason {
863690587d1SChris Mason 	int ret;
864690587d1SChris Mason 	int ret2;
865c6adc9ccSMiao Xie 	struct blk_plug plug;
866690587d1SChris Mason 
867c6adc9ccSMiao Xie 	blk_start_plug(&plug);
8688cef4e16SYan, Zheng 	ret = btrfs_write_marked_extents(root, dirty_pages, mark);
869c6adc9ccSMiao Xie 	blk_finish_plug(&plug);
8708cef4e16SYan, Zheng 	ret2 = btrfs_wait_marked_extents(root, dirty_pages, mark);
871bf0da8c1SChris Mason 
872bf0da8c1SChris Mason 	if (ret)
873bf0da8c1SChris Mason 		return ret;
874bf0da8c1SChris Mason 	if (ret2)
875bf0da8c1SChris Mason 		return ret2;
876bf0da8c1SChris Mason 	return 0;
877690587d1SChris Mason }
878690587d1SChris Mason 
879d0c803c4SChris Mason int btrfs_write_and_wait_transaction(struct btrfs_trans_handle *trans,
880d0c803c4SChris Mason 				     struct btrfs_root *root)
881d0c803c4SChris Mason {
882d0c803c4SChris Mason 	if (!trans || !trans->transaction) {
883d0c803c4SChris Mason 		struct inode *btree_inode;
884d0c803c4SChris Mason 		btree_inode = root->fs_info->btree_inode;
885d0c803c4SChris Mason 		return filemap_write_and_wait(btree_inode->i_mapping);
886d0c803c4SChris Mason 	}
887d0c803c4SChris Mason 	return btrfs_write_and_wait_marked_extents(root,
8888cef4e16SYan, Zheng 					   &trans->transaction->dirty_pages,
8898cef4e16SYan, Zheng 					   EXTENT_DIRTY);
890d0c803c4SChris Mason }
891d0c803c4SChris Mason 
892d352ac68SChris Mason /*
893d352ac68SChris Mason  * this is used to update the root pointer in the tree of tree roots.
894d352ac68SChris Mason  *
895d352ac68SChris Mason  * But, in the case of the extent allocation tree, updating the root
896d352ac68SChris Mason  * pointer may allocate blocks which may change the root of the extent
897d352ac68SChris Mason  * allocation tree.
898d352ac68SChris Mason  *
899d352ac68SChris Mason  * So, this loops and repeats and makes sure the cowonly root didn't
900d352ac68SChris Mason  * change while the root pointer was being updated in the metadata.
901d352ac68SChris Mason  */
9020b86a832SChris Mason static int update_cowonly_root(struct btrfs_trans_handle *trans,
90379154b1bSChris Mason 			       struct btrfs_root *root)
90479154b1bSChris Mason {
90579154b1bSChris Mason 	int ret;
9060b86a832SChris Mason 	u64 old_root_bytenr;
90786b9f2ecSYan, Zheng 	u64 old_root_used;
9080b86a832SChris Mason 	struct btrfs_root *tree_root = root->fs_info->tree_root;
90979154b1bSChris Mason 
91086b9f2ecSYan, Zheng 	old_root_used = btrfs_root_used(&root->root_item);
9110b86a832SChris Mason 	btrfs_write_dirty_block_groups(trans, root);
91256bec294SChris Mason 
91379154b1bSChris Mason 	while (1) {
9140b86a832SChris Mason 		old_root_bytenr = btrfs_root_bytenr(&root->root_item);
91586b9f2ecSYan, Zheng 		if (old_root_bytenr == root->node->start &&
91686b9f2ecSYan, Zheng 		    old_root_used == btrfs_root_used(&root->root_item))
91779154b1bSChris Mason 			break;
91887ef2bb4SChris Mason 
9195d4f98a2SYan Zheng 		btrfs_set_root_node(&root->root_item, root->node);
92079154b1bSChris Mason 		ret = btrfs_update_root(trans, tree_root,
9210b86a832SChris Mason 					&root->root_key,
9220b86a832SChris Mason 					&root->root_item);
92349b25e05SJeff Mahoney 		if (ret)
92449b25e05SJeff Mahoney 			return ret;
92556bec294SChris Mason 
92686b9f2ecSYan, Zheng 		old_root_used = btrfs_root_used(&root->root_item);
9274a8c9a62SYan Zheng 		ret = btrfs_write_dirty_block_groups(trans, root);
92849b25e05SJeff Mahoney 		if (ret)
92949b25e05SJeff Mahoney 			return ret;
9300b86a832SChris Mason 	}
931276e680dSYan Zheng 
9320b86a832SChris Mason 	return 0;
9330b86a832SChris Mason }
9340b86a832SChris Mason 
935d352ac68SChris Mason /*
936d352ac68SChris Mason  * update all the cowonly tree roots on disk
93749b25e05SJeff Mahoney  *
93849b25e05SJeff Mahoney  * The error handling in this function may not be obvious. Any of the
93949b25e05SJeff Mahoney  * failures will cause the file system to go offline. We still need
94049b25e05SJeff Mahoney  * to clean up the delayed refs.
941d352ac68SChris Mason  */
9425d4f98a2SYan Zheng static noinline int commit_cowonly_roots(struct btrfs_trans_handle *trans,
9430b86a832SChris Mason 					 struct btrfs_root *root)
9440b86a832SChris Mason {
9450b86a832SChris Mason 	struct btrfs_fs_info *fs_info = root->fs_info;
9460b86a832SChris Mason 	struct list_head *next;
94784234f3aSYan Zheng 	struct extent_buffer *eb;
94856bec294SChris Mason 	int ret;
94984234f3aSYan Zheng 
95056bec294SChris Mason 	ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
95149b25e05SJeff Mahoney 	if (ret)
95249b25e05SJeff Mahoney 		return ret;
95387ef2bb4SChris Mason 
95484234f3aSYan Zheng 	eb = btrfs_lock_root_node(fs_info->tree_root);
95549b25e05SJeff Mahoney 	ret = btrfs_cow_block(trans, fs_info->tree_root, eb, NULL,
95649b25e05SJeff Mahoney 			      0, &eb);
95784234f3aSYan Zheng 	btrfs_tree_unlock(eb);
95884234f3aSYan Zheng 	free_extent_buffer(eb);
9590b86a832SChris Mason 
96049b25e05SJeff Mahoney 	if (ret)
96149b25e05SJeff Mahoney 		return ret;
96249b25e05SJeff Mahoney 
96356bec294SChris Mason 	ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
96449b25e05SJeff Mahoney 	if (ret)
96549b25e05SJeff Mahoney 		return ret;
96687ef2bb4SChris Mason 
967733f4fbbSStefan Behrens 	ret = btrfs_run_dev_stats(trans, root->fs_info);
968c16ce190SJosef Bacik 	if (ret)
969c16ce190SJosef Bacik 		return ret;
9708dabb742SStefan Behrens 	ret = btrfs_run_dev_replace(trans, root->fs_info);
971c16ce190SJosef Bacik 	if (ret)
972c16ce190SJosef Bacik 		return ret;
973546adb0dSJan Schmidt 	ret = btrfs_run_qgroups(trans, root->fs_info);
974c16ce190SJosef Bacik 	if (ret)
975c16ce190SJosef Bacik 		return ret;
976546adb0dSJan Schmidt 
977546adb0dSJan Schmidt 	/* run_qgroups might have added some more refs */
978546adb0dSJan Schmidt 	ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
979c16ce190SJosef Bacik 	if (ret)
980c16ce190SJosef Bacik 		return ret;
981546adb0dSJan Schmidt 
9820b86a832SChris Mason 	while (!list_empty(&fs_info->dirty_cowonly_roots)) {
9830b86a832SChris Mason 		next = fs_info->dirty_cowonly_roots.next;
9840b86a832SChris Mason 		list_del_init(next);
9850b86a832SChris Mason 		root = list_entry(next, struct btrfs_root, dirty_list);
98687ef2bb4SChris Mason 
9879e351cc8SJosef Bacik 		if (root != fs_info->extent_root)
9889e351cc8SJosef Bacik 			list_add_tail(&root->dirty_list,
9899e351cc8SJosef Bacik 				      &trans->transaction->switch_commits);
99049b25e05SJeff Mahoney 		ret = update_cowonly_root(trans, root);
99149b25e05SJeff Mahoney 		if (ret)
99249b25e05SJeff Mahoney 			return ret;
99379154b1bSChris Mason 	}
994276e680dSYan Zheng 
9959e351cc8SJosef Bacik 	list_add_tail(&fs_info->extent_root->dirty_list,
9969e351cc8SJosef Bacik 		      &trans->transaction->switch_commits);
9978dabb742SStefan Behrens 	btrfs_after_dev_replace_commit(fs_info);
9988dabb742SStefan Behrens 
99979154b1bSChris Mason 	return 0;
100079154b1bSChris Mason }
100179154b1bSChris Mason 
1002d352ac68SChris Mason /*
1003d352ac68SChris Mason  * dead roots are old snapshots that need to be deleted.  This allocates
1004d352ac68SChris Mason  * a dirty root struct and adds it into the list of dead roots that need to
1005d352ac68SChris Mason  * be deleted
1006d352ac68SChris Mason  */
1007cfad392bSJosef Bacik void btrfs_add_dead_root(struct btrfs_root *root)
10085eda7b5eSChris Mason {
1009a4abeea4SJosef Bacik 	spin_lock(&root->fs_info->trans_lock);
1010cfad392bSJosef Bacik 	if (list_empty(&root->root_list))
10119d1a2a3aSDavid Sterba 		list_add_tail(&root->root_list, &root->fs_info->dead_roots);
1012a4abeea4SJosef Bacik 	spin_unlock(&root->fs_info->trans_lock);
10135eda7b5eSChris Mason }
10145eda7b5eSChris Mason 
1015d352ac68SChris Mason /*
10165d4f98a2SYan Zheng  * update all the cowonly tree roots on disk
1017d352ac68SChris Mason  */
10185d4f98a2SYan Zheng static noinline int commit_fs_roots(struct btrfs_trans_handle *trans,
10195d4f98a2SYan Zheng 				    struct btrfs_root *root)
10200f7d52f4SChris Mason {
10210f7d52f4SChris Mason 	struct btrfs_root *gang[8];
10225d4f98a2SYan Zheng 	struct btrfs_fs_info *fs_info = root->fs_info;
10230f7d52f4SChris Mason 	int i;
10240f7d52f4SChris Mason 	int ret;
102554aa1f4dSChris Mason 	int err = 0;
102654aa1f4dSChris Mason 
1027a4abeea4SJosef Bacik 	spin_lock(&fs_info->fs_roots_radix_lock);
10280f7d52f4SChris Mason 	while (1) {
10295d4f98a2SYan Zheng 		ret = radix_tree_gang_lookup_tag(&fs_info->fs_roots_radix,
10305d4f98a2SYan Zheng 						 (void **)gang, 0,
10310f7d52f4SChris Mason 						 ARRAY_SIZE(gang),
10320f7d52f4SChris Mason 						 BTRFS_ROOT_TRANS_TAG);
10330f7d52f4SChris Mason 		if (ret == 0)
10340f7d52f4SChris Mason 			break;
10350f7d52f4SChris Mason 		for (i = 0; i < ret; i++) {
10360f7d52f4SChris Mason 			root = gang[i];
10375d4f98a2SYan Zheng 			radix_tree_tag_clear(&fs_info->fs_roots_radix,
10382619ba1fSChris Mason 					(unsigned long)root->root_key.objectid,
10390f7d52f4SChris Mason 					BTRFS_ROOT_TRANS_TAG);
1040a4abeea4SJosef Bacik 			spin_unlock(&fs_info->fs_roots_radix_lock);
104131153d81SYan Zheng 
1042e02119d5SChris Mason 			btrfs_free_log(trans, root);
10435d4f98a2SYan Zheng 			btrfs_update_reloc_root(trans, root);
1044d68fc57bSYan, Zheng 			btrfs_orphan_commit_root(trans, root);
1045e02119d5SChris Mason 
104682d5902dSLi Zefan 			btrfs_save_ino_cache(root, trans);
104782d5902dSLi Zefan 
1048f1ebcc74SLiu Bo 			/* see comments in should_cow_block() */
104927cdeb70SMiao Xie 			clear_bit(BTRFS_ROOT_FORCE_COW, &root->state);
105027cdeb70SMiao Xie 			smp_mb__after_clear_bit();
1051f1ebcc74SLiu Bo 
1052978d910dSYan Zheng 			if (root->commit_root != root->node) {
10539e351cc8SJosef Bacik 				list_add_tail(&root->dirty_list,
10549e351cc8SJosef Bacik 					&trans->transaction->switch_commits);
1055978d910dSYan Zheng 				btrfs_set_root_node(&root->root_item,
1056978d910dSYan Zheng 						    root->node);
1057978d910dSYan Zheng 			}
105831153d81SYan Zheng 
10595d4f98a2SYan Zheng 			err = btrfs_update_root(trans, fs_info->tree_root,
10600f7d52f4SChris Mason 						&root->root_key,
10610f7d52f4SChris Mason 						&root->root_item);
1062a4abeea4SJosef Bacik 			spin_lock(&fs_info->fs_roots_radix_lock);
106354aa1f4dSChris Mason 			if (err)
106454aa1f4dSChris Mason 				break;
10650f7d52f4SChris Mason 		}
10669f3a7427SChris Mason 	}
1067a4abeea4SJosef Bacik 	spin_unlock(&fs_info->fs_roots_radix_lock);
106854aa1f4dSChris Mason 	return err;
10690f7d52f4SChris Mason }
10700f7d52f4SChris Mason 
1071d352ac68SChris Mason /*
1072de78b51aSEric Sandeen  * defrag a given btree.
1073de78b51aSEric Sandeen  * Every leaf in the btree is read and defragged.
1074d352ac68SChris Mason  */
1075de78b51aSEric Sandeen int btrfs_defrag_root(struct btrfs_root *root)
1076e9d0b13bSChris Mason {
1077e9d0b13bSChris Mason 	struct btrfs_fs_info *info = root->fs_info;
1078e9d0b13bSChris Mason 	struct btrfs_trans_handle *trans;
10798929ecfaSYan, Zheng 	int ret;
1080e9d0b13bSChris Mason 
108127cdeb70SMiao Xie 	if (test_and_set_bit(BTRFS_ROOT_DEFRAG_RUNNING, &root->state))
1082e9d0b13bSChris Mason 		return 0;
10838929ecfaSYan, Zheng 
10846b80053dSChris Mason 	while (1) {
10858929ecfaSYan, Zheng 		trans = btrfs_start_transaction(root, 0);
10868929ecfaSYan, Zheng 		if (IS_ERR(trans))
10878929ecfaSYan, Zheng 			return PTR_ERR(trans);
10888929ecfaSYan, Zheng 
1089de78b51aSEric Sandeen 		ret = btrfs_defrag_leaves(trans, root);
10908929ecfaSYan, Zheng 
1091e9d0b13bSChris Mason 		btrfs_end_transaction(trans, root);
1092b53d3f5dSLiu Bo 		btrfs_btree_balance_dirty(info->tree_root);
1093e9d0b13bSChris Mason 		cond_resched();
1094e9d0b13bSChris Mason 
10957841cb28SDavid Sterba 		if (btrfs_fs_closing(root->fs_info) || ret != -EAGAIN)
1096e9d0b13bSChris Mason 			break;
1097210549ebSDavid Sterba 
1098210549ebSDavid Sterba 		if (btrfs_defrag_cancelled(root->fs_info)) {
1099efe120a0SFrank Holton 			pr_debug("BTRFS: defrag_root cancelled\n");
1100210549ebSDavid Sterba 			ret = -EAGAIN;
1101210549ebSDavid Sterba 			break;
1102210549ebSDavid Sterba 		}
1103e9d0b13bSChris Mason 	}
110427cdeb70SMiao Xie 	clear_bit(BTRFS_ROOT_DEFRAG_RUNNING, &root->state);
11058929ecfaSYan, Zheng 	return ret;
1106e9d0b13bSChris Mason }
1107e9d0b13bSChris Mason 
1108d352ac68SChris Mason /*
1109d352ac68SChris Mason  * new snapshots need to be created at a very specific time in the
1110aec8030aSMiao Xie  * transaction commit.  This does the actual creation.
1111aec8030aSMiao Xie  *
1112aec8030aSMiao Xie  * Note:
1113aec8030aSMiao Xie  * If the error which may affect the commitment of the current transaction
1114aec8030aSMiao Xie  * happens, we should return the error number. If the error which just affect
1115aec8030aSMiao Xie  * the creation of the pending snapshots, just return 0.
1116d352ac68SChris Mason  */
111780b6794dSChris Mason static noinline int create_pending_snapshot(struct btrfs_trans_handle *trans,
11183063d29fSChris Mason 				   struct btrfs_fs_info *fs_info,
11193063d29fSChris Mason 				   struct btrfs_pending_snapshot *pending)
11203063d29fSChris Mason {
11213063d29fSChris Mason 	struct btrfs_key key;
112280b6794dSChris Mason 	struct btrfs_root_item *new_root_item;
11233063d29fSChris Mason 	struct btrfs_root *tree_root = fs_info->tree_root;
11243063d29fSChris Mason 	struct btrfs_root *root = pending->root;
11256bdb72deSSage Weil 	struct btrfs_root *parent_root;
112698c9942aSLiu Bo 	struct btrfs_block_rsv *rsv;
11276bdb72deSSage Weil 	struct inode *parent_inode;
112842874b3dSMiao Xie 	struct btrfs_path *path;
112942874b3dSMiao Xie 	struct btrfs_dir_item *dir_item;
1130a22285a6SYan, Zheng 	struct dentry *dentry;
11313063d29fSChris Mason 	struct extent_buffer *tmp;
1132925baeddSChris Mason 	struct extent_buffer *old;
11338ea05e3aSAlexander Block 	struct timespec cur_time = CURRENT_TIME;
1134aec8030aSMiao Xie 	int ret = 0;
1135d68fc57bSYan, Zheng 	u64 to_reserve = 0;
11366bdb72deSSage Weil 	u64 index = 0;
1137a22285a6SYan, Zheng 	u64 objectid;
1138b83cc969SLi Zefan 	u64 root_flags;
11398ea05e3aSAlexander Block 	uuid_le new_uuid;
11403063d29fSChris Mason 
114142874b3dSMiao Xie 	path = btrfs_alloc_path();
114242874b3dSMiao Xie 	if (!path) {
1143aec8030aSMiao Xie 		pending->error = -ENOMEM;
1144aec8030aSMiao Xie 		return 0;
114542874b3dSMiao Xie 	}
114642874b3dSMiao Xie 
114780b6794dSChris Mason 	new_root_item = kmalloc(sizeof(*new_root_item), GFP_NOFS);
114880b6794dSChris Mason 	if (!new_root_item) {
1149aec8030aSMiao Xie 		pending->error = -ENOMEM;
11506fa9700eSMiao Xie 		goto root_item_alloc_fail;
115180b6794dSChris Mason 	}
1152a22285a6SYan, Zheng 
1153aec8030aSMiao Xie 	pending->error = btrfs_find_free_objectid(tree_root, &objectid);
1154aec8030aSMiao Xie 	if (pending->error)
11556fa9700eSMiao Xie 		goto no_free_objectid;
11563063d29fSChris Mason 
11573fd0a558SYan, Zheng 	btrfs_reloc_pre_snapshot(trans, pending, &to_reserve);
1158d68fc57bSYan, Zheng 
1159d68fc57bSYan, Zheng 	if (to_reserve > 0) {
1160aec8030aSMiao Xie 		pending->error = btrfs_block_rsv_add(root,
1161aec8030aSMiao Xie 						     &pending->block_rsv,
116208e007d2SMiao Xie 						     to_reserve,
116308e007d2SMiao Xie 						     BTRFS_RESERVE_NO_FLUSH);
1164aec8030aSMiao Xie 		if (pending->error)
11656fa9700eSMiao Xie 			goto no_free_objectid;
1166d68fc57bSYan, Zheng 	}
1167d68fc57bSYan, Zheng 
11683063d29fSChris Mason 	key.objectid = objectid;
1169a22285a6SYan, Zheng 	key.offset = (u64)-1;
1170a22285a6SYan, Zheng 	key.type = BTRFS_ROOT_ITEM_KEY;
11713063d29fSChris Mason 
11726fa9700eSMiao Xie 	rsv = trans->block_rsv;
1173a22285a6SYan, Zheng 	trans->block_rsv = &pending->block_rsv;
11742382c5ccSLiu Bo 	trans->bytes_reserved = trans->block_rsv->reserved;
11756bdb72deSSage Weil 
1176a22285a6SYan, Zheng 	dentry = pending->dentry;
1177e9662f70SMiao Xie 	parent_inode = pending->dir;
1178a22285a6SYan, Zheng 	parent_root = BTRFS_I(parent_inode)->root;
11797585717fSChris Mason 	record_root_in_trans(trans, parent_root);
1180a22285a6SYan, Zheng 
11816bdb72deSSage Weil 	/*
11826bdb72deSSage Weil 	 * insert the directory item
11836bdb72deSSage Weil 	 */
11846bdb72deSSage Weil 	ret = btrfs_set_inode_index(parent_inode, &index);
118549b25e05SJeff Mahoney 	BUG_ON(ret); /* -ENOMEM */
118642874b3dSMiao Xie 
118742874b3dSMiao Xie 	/* check if there is a file/dir which has the same name. */
118842874b3dSMiao Xie 	dir_item = btrfs_lookup_dir_item(NULL, parent_root, path,
118942874b3dSMiao Xie 					 btrfs_ino(parent_inode),
119042874b3dSMiao Xie 					 dentry->d_name.name,
119142874b3dSMiao Xie 					 dentry->d_name.len, 0);
119242874b3dSMiao Xie 	if (dir_item != NULL && !IS_ERR(dir_item)) {
1193fe66a05aSChris Mason 		pending->error = -EEXIST;
1194aec8030aSMiao Xie 		goto dir_item_existed;
119542874b3dSMiao Xie 	} else if (IS_ERR(dir_item)) {
119642874b3dSMiao Xie 		ret = PTR_ERR(dir_item);
11978732d44fSMiao Xie 		btrfs_abort_transaction(trans, root, ret);
11988732d44fSMiao Xie 		goto fail;
119979787eaaSJeff Mahoney 	}
120042874b3dSMiao Xie 	btrfs_release_path(path);
12016bdb72deSSage Weil 
1202e999376fSChris Mason 	/*
1203e999376fSChris Mason 	 * pull in the delayed directory update
1204e999376fSChris Mason 	 * and the delayed inode item
1205e999376fSChris Mason 	 * otherwise we corrupt the FS during
1206e999376fSChris Mason 	 * snapshot
1207e999376fSChris Mason 	 */
1208e999376fSChris Mason 	ret = btrfs_run_delayed_items(trans, root);
12098732d44fSMiao Xie 	if (ret) {	/* Transaction aborted */
12108732d44fSMiao Xie 		btrfs_abort_transaction(trans, root, ret);
12118732d44fSMiao Xie 		goto fail;
12128732d44fSMiao Xie 	}
1213e999376fSChris Mason 
12147585717fSChris Mason 	record_root_in_trans(trans, root);
12156bdb72deSSage Weil 	btrfs_set_root_last_snapshot(&root->root_item, trans->transid);
12166bdb72deSSage Weil 	memcpy(new_root_item, &root->root_item, sizeof(*new_root_item));
121708fe4db1SLi Zefan 	btrfs_check_and_init_root_item(new_root_item);
12186bdb72deSSage Weil 
1219b83cc969SLi Zefan 	root_flags = btrfs_root_flags(new_root_item);
1220b83cc969SLi Zefan 	if (pending->readonly)
1221b83cc969SLi Zefan 		root_flags |= BTRFS_ROOT_SUBVOL_RDONLY;
1222b83cc969SLi Zefan 	else
1223b83cc969SLi Zefan 		root_flags &= ~BTRFS_ROOT_SUBVOL_RDONLY;
1224b83cc969SLi Zefan 	btrfs_set_root_flags(new_root_item, root_flags);
1225b83cc969SLi Zefan 
12268ea05e3aSAlexander Block 	btrfs_set_root_generation_v2(new_root_item,
12278ea05e3aSAlexander Block 			trans->transid);
12288ea05e3aSAlexander Block 	uuid_le_gen(&new_uuid);
12298ea05e3aSAlexander Block 	memcpy(new_root_item->uuid, new_uuid.b, BTRFS_UUID_SIZE);
12308ea05e3aSAlexander Block 	memcpy(new_root_item->parent_uuid, root->root_item.uuid,
12318ea05e3aSAlexander Block 			BTRFS_UUID_SIZE);
123270023da2SStefan Behrens 	if (!(root_flags & BTRFS_ROOT_SUBVOL_RDONLY)) {
123370023da2SStefan Behrens 		memset(new_root_item->received_uuid, 0,
123470023da2SStefan Behrens 		       sizeof(new_root_item->received_uuid));
12358ea05e3aSAlexander Block 		memset(&new_root_item->stime, 0, sizeof(new_root_item->stime));
12368ea05e3aSAlexander Block 		memset(&new_root_item->rtime, 0, sizeof(new_root_item->rtime));
12378ea05e3aSAlexander Block 		btrfs_set_root_stransid(new_root_item, 0);
12388ea05e3aSAlexander Block 		btrfs_set_root_rtransid(new_root_item, 0);
123970023da2SStefan Behrens 	}
12403cae210fSQu Wenruo 	btrfs_set_stack_timespec_sec(&new_root_item->otime, cur_time.tv_sec);
12413cae210fSQu Wenruo 	btrfs_set_stack_timespec_nsec(&new_root_item->otime, cur_time.tv_nsec);
124270023da2SStefan Behrens 	btrfs_set_root_otransid(new_root_item, trans->transid);
12438ea05e3aSAlexander Block 
1244925baeddSChris Mason 	old = btrfs_lock_root_node(root);
124549b25e05SJeff Mahoney 	ret = btrfs_cow_block(trans, root, old, NULL, 0, &old);
124679787eaaSJeff Mahoney 	if (ret) {
124779787eaaSJeff Mahoney 		btrfs_tree_unlock(old);
124879787eaaSJeff Mahoney 		free_extent_buffer(old);
12498732d44fSMiao Xie 		btrfs_abort_transaction(trans, root, ret);
12508732d44fSMiao Xie 		goto fail;
125179787eaaSJeff Mahoney 	}
125249b25e05SJeff Mahoney 
12535d4f98a2SYan Zheng 	btrfs_set_lock_blocking(old);
12543063d29fSChris Mason 
125549b25e05SJeff Mahoney 	ret = btrfs_copy_root(trans, root, old, &tmp, objectid);
125679787eaaSJeff Mahoney 	/* clean up in any case */
1257925baeddSChris Mason 	btrfs_tree_unlock(old);
1258925baeddSChris Mason 	free_extent_buffer(old);
12598732d44fSMiao Xie 	if (ret) {
12608732d44fSMiao Xie 		btrfs_abort_transaction(trans, root, ret);
12618732d44fSMiao Xie 		goto fail;
12628732d44fSMiao Xie 	}
12633063d29fSChris Mason 
1264fcebe456SJosef Bacik 	/*
1265fcebe456SJosef Bacik 	 * We need to flush delayed refs in order to make sure all of our quota
1266fcebe456SJosef Bacik 	 * operations have been done before we call btrfs_qgroup_inherit.
1267fcebe456SJosef Bacik 	 */
1268fcebe456SJosef Bacik 	ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
1269fcebe456SJosef Bacik 	if (ret) {
1270fcebe456SJosef Bacik 		btrfs_abort_transaction(trans, root, ret);
1271fcebe456SJosef Bacik 		goto fail;
1272fcebe456SJosef Bacik 	}
1273fcebe456SJosef Bacik 
1274fcebe456SJosef Bacik 	pending->error = btrfs_qgroup_inherit(trans, fs_info,
1275fcebe456SJosef Bacik 					      root->root_key.objectid,
1276fcebe456SJosef Bacik 					      objectid, pending->inherit);
1277fcebe456SJosef Bacik 	if (pending->error)
1278fcebe456SJosef Bacik 		goto no_free_objectid;
1279fcebe456SJosef Bacik 
1280f1ebcc74SLiu Bo 	/* see comments in should_cow_block() */
128127cdeb70SMiao Xie 	set_bit(BTRFS_ROOT_FORCE_COW, &root->state);
1282f1ebcc74SLiu Bo 	smp_wmb();
1283f1ebcc74SLiu Bo 
12845d4f98a2SYan Zheng 	btrfs_set_root_node(new_root_item, tmp);
1285a22285a6SYan, Zheng 	/* record when the snapshot was created in key.offset */
1286a22285a6SYan, Zheng 	key.offset = trans->transid;
1287a22285a6SYan, Zheng 	ret = btrfs_insert_root(trans, tree_root, &key, new_root_item);
1288925baeddSChris Mason 	btrfs_tree_unlock(tmp);
12893063d29fSChris Mason 	free_extent_buffer(tmp);
12908732d44fSMiao Xie 	if (ret) {
12918732d44fSMiao Xie 		btrfs_abort_transaction(trans, root, ret);
12928732d44fSMiao Xie 		goto fail;
12938732d44fSMiao Xie 	}
12940660b5afSChris Mason 
1295a22285a6SYan, Zheng 	/*
1296a22285a6SYan, Zheng 	 * insert root back/forward references
1297a22285a6SYan, Zheng 	 */
1298a22285a6SYan, Zheng 	ret = btrfs_add_root_ref(trans, tree_root, objectid,
1299a22285a6SYan, Zheng 				 parent_root->root_key.objectid,
130033345d01SLi Zefan 				 btrfs_ino(parent_inode), index,
1301a22285a6SYan, Zheng 				 dentry->d_name.name, dentry->d_name.len);
13028732d44fSMiao Xie 	if (ret) {
13038732d44fSMiao Xie 		btrfs_abort_transaction(trans, root, ret);
13048732d44fSMiao Xie 		goto fail;
13058732d44fSMiao Xie 	}
1306a22285a6SYan, Zheng 
1307a22285a6SYan, Zheng 	key.offset = (u64)-1;
1308a22285a6SYan, Zheng 	pending->snap = btrfs_read_fs_root_no_name(root->fs_info, &key);
130979787eaaSJeff Mahoney 	if (IS_ERR(pending->snap)) {
131079787eaaSJeff Mahoney 		ret = PTR_ERR(pending->snap);
13118732d44fSMiao Xie 		btrfs_abort_transaction(trans, root, ret);
13128732d44fSMiao Xie 		goto fail;
131379787eaaSJeff Mahoney 	}
1314d68fc57bSYan, Zheng 
131549b25e05SJeff Mahoney 	ret = btrfs_reloc_post_snapshot(trans, pending);
13168732d44fSMiao Xie 	if (ret) {
13178732d44fSMiao Xie 		btrfs_abort_transaction(trans, root, ret);
13188732d44fSMiao Xie 		goto fail;
13198732d44fSMiao Xie 	}
1320361048f5SMiao Xie 
1321361048f5SMiao Xie 	ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
13228732d44fSMiao Xie 	if (ret) {
13238732d44fSMiao Xie 		btrfs_abort_transaction(trans, root, ret);
13248732d44fSMiao Xie 		goto fail;
13258732d44fSMiao Xie 	}
132642874b3dSMiao Xie 
132742874b3dSMiao Xie 	ret = btrfs_insert_dir_item(trans, parent_root,
132842874b3dSMiao Xie 				    dentry->d_name.name, dentry->d_name.len,
132942874b3dSMiao Xie 				    parent_inode, &key,
133042874b3dSMiao Xie 				    BTRFS_FT_DIR, index);
133142874b3dSMiao Xie 	/* We have check then name at the beginning, so it is impossible. */
13329c52057cSChris Mason 	BUG_ON(ret == -EEXIST || ret == -EOVERFLOW);
13338732d44fSMiao Xie 	if (ret) {
13348732d44fSMiao Xie 		btrfs_abort_transaction(trans, root, ret);
13358732d44fSMiao Xie 		goto fail;
13368732d44fSMiao Xie 	}
133742874b3dSMiao Xie 
133842874b3dSMiao Xie 	btrfs_i_size_write(parent_inode, parent_inode->i_size +
133942874b3dSMiao Xie 					 dentry->d_name.len * 2);
134042874b3dSMiao Xie 	parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
1341be6aef60SJosef Bacik 	ret = btrfs_update_inode_fallback(trans, parent_root, parent_inode);
1342dd5f9615SStefan Behrens 	if (ret) {
13438732d44fSMiao Xie 		btrfs_abort_transaction(trans, root, ret);
1344dd5f9615SStefan Behrens 		goto fail;
1345dd5f9615SStefan Behrens 	}
1346dd5f9615SStefan Behrens 	ret = btrfs_uuid_tree_add(trans, fs_info->uuid_root, new_uuid.b,
1347dd5f9615SStefan Behrens 				  BTRFS_UUID_KEY_SUBVOL, objectid);
1348dd5f9615SStefan Behrens 	if (ret) {
1349dd5f9615SStefan Behrens 		btrfs_abort_transaction(trans, root, ret);
1350dd5f9615SStefan Behrens 		goto fail;
1351dd5f9615SStefan Behrens 	}
1352dd5f9615SStefan Behrens 	if (!btrfs_is_empty_uuid(new_root_item->received_uuid)) {
1353dd5f9615SStefan Behrens 		ret = btrfs_uuid_tree_add(trans, fs_info->uuid_root,
1354dd5f9615SStefan Behrens 					  new_root_item->received_uuid,
1355dd5f9615SStefan Behrens 					  BTRFS_UUID_KEY_RECEIVED_SUBVOL,
1356dd5f9615SStefan Behrens 					  objectid);
1357dd5f9615SStefan Behrens 		if (ret && ret != -EEXIST) {
1358dd5f9615SStefan Behrens 			btrfs_abort_transaction(trans, root, ret);
1359dd5f9615SStefan Behrens 			goto fail;
1360dd5f9615SStefan Behrens 		}
1361dd5f9615SStefan Behrens 	}
13623063d29fSChris Mason fail:
1363aec8030aSMiao Xie 	pending->error = ret;
1364aec8030aSMiao Xie dir_item_existed:
136598c9942aSLiu Bo 	trans->block_rsv = rsv;
13662382c5ccSLiu Bo 	trans->bytes_reserved = 0;
13676fa9700eSMiao Xie no_free_objectid:
13686fa9700eSMiao Xie 	kfree(new_root_item);
13696fa9700eSMiao Xie root_item_alloc_fail:
137042874b3dSMiao Xie 	btrfs_free_path(path);
137149b25e05SJeff Mahoney 	return ret;
13723063d29fSChris Mason }
13733063d29fSChris Mason 
1374d352ac68SChris Mason /*
1375d352ac68SChris Mason  * create all the snapshots we've scheduled for creation
1376d352ac68SChris Mason  */
137780b6794dSChris Mason static noinline int create_pending_snapshots(struct btrfs_trans_handle *trans,
13783063d29fSChris Mason 					     struct btrfs_fs_info *fs_info)
13793063d29fSChris Mason {
1380aec8030aSMiao Xie 	struct btrfs_pending_snapshot *pending, *next;
13813063d29fSChris Mason 	struct list_head *head = &trans->transaction->pending_snapshots;
1382aec8030aSMiao Xie 	int ret = 0;
13833de4586cSChris Mason 
1384aec8030aSMiao Xie 	list_for_each_entry_safe(pending, next, head, list) {
1385aec8030aSMiao Xie 		list_del(&pending->list);
1386aec8030aSMiao Xie 		ret = create_pending_snapshot(trans, fs_info, pending);
1387aec8030aSMiao Xie 		if (ret)
1388aec8030aSMiao Xie 			break;
1389aec8030aSMiao Xie 	}
1390aec8030aSMiao Xie 	return ret;
13913de4586cSChris Mason }
13923de4586cSChris Mason 
13935d4f98a2SYan Zheng static void update_super_roots(struct btrfs_root *root)
13945d4f98a2SYan Zheng {
13955d4f98a2SYan Zheng 	struct btrfs_root_item *root_item;
13965d4f98a2SYan Zheng 	struct btrfs_super_block *super;
13975d4f98a2SYan Zheng 
13986c41761fSDavid Sterba 	super = root->fs_info->super_copy;
13995d4f98a2SYan Zheng 
14005d4f98a2SYan Zheng 	root_item = &root->fs_info->chunk_root->root_item;
14015d4f98a2SYan Zheng 	super->chunk_root = root_item->bytenr;
14025d4f98a2SYan Zheng 	super->chunk_root_generation = root_item->generation;
14035d4f98a2SYan Zheng 	super->chunk_root_level = root_item->level;
14045d4f98a2SYan Zheng 
14055d4f98a2SYan Zheng 	root_item = &root->fs_info->tree_root->root_item;
14065d4f98a2SYan Zheng 	super->root = root_item->bytenr;
14075d4f98a2SYan Zheng 	super->generation = root_item->generation;
14085d4f98a2SYan Zheng 	super->root_level = root_item->level;
140973bc1876SJosef Bacik 	if (btrfs_test_opt(root, SPACE_CACHE))
14100af3d00bSJosef Bacik 		super->cache_generation = root_item->generation;
141170f80175SStefan Behrens 	if (root->fs_info->update_uuid_tree_gen)
141226432799SStefan Behrens 		super->uuid_tree_generation = root_item->generation;
14135d4f98a2SYan Zheng }
14145d4f98a2SYan Zheng 
1415f36f3042SChris Mason int btrfs_transaction_in_commit(struct btrfs_fs_info *info)
1416f36f3042SChris Mason {
14174a9d8bdeSMiao Xie 	struct btrfs_transaction *trans;
1418f36f3042SChris Mason 	int ret = 0;
14194a9d8bdeSMiao Xie 
1420a4abeea4SJosef Bacik 	spin_lock(&info->trans_lock);
14214a9d8bdeSMiao Xie 	trans = info->running_transaction;
14224a9d8bdeSMiao Xie 	if (trans)
14234a9d8bdeSMiao Xie 		ret = (trans->state >= TRANS_STATE_COMMIT_START);
1424a4abeea4SJosef Bacik 	spin_unlock(&info->trans_lock);
1425f36f3042SChris Mason 	return ret;
1426f36f3042SChris Mason }
1427f36f3042SChris Mason 
14288929ecfaSYan, Zheng int btrfs_transaction_blocked(struct btrfs_fs_info *info)
14298929ecfaSYan, Zheng {
14304a9d8bdeSMiao Xie 	struct btrfs_transaction *trans;
14318929ecfaSYan, Zheng 	int ret = 0;
14324a9d8bdeSMiao Xie 
1433a4abeea4SJosef Bacik 	spin_lock(&info->trans_lock);
14344a9d8bdeSMiao Xie 	trans = info->running_transaction;
14354a9d8bdeSMiao Xie 	if (trans)
14364a9d8bdeSMiao Xie 		ret = is_transaction_blocked(trans);
1437a4abeea4SJosef Bacik 	spin_unlock(&info->trans_lock);
14388929ecfaSYan, Zheng 	return ret;
14398929ecfaSYan, Zheng }
14408929ecfaSYan, Zheng 
1441bb9c12c9SSage Weil /*
1442bb9c12c9SSage Weil  * wait for the current transaction commit to start and block subsequent
1443bb9c12c9SSage Weil  * transaction joins
1444bb9c12c9SSage Weil  */
1445bb9c12c9SSage Weil static void wait_current_trans_commit_start(struct btrfs_root *root,
1446bb9c12c9SSage Weil 					    struct btrfs_transaction *trans)
1447bb9c12c9SSage Weil {
14484a9d8bdeSMiao Xie 	wait_event(root->fs_info->transaction_blocked_wait,
1449501407aaSJosef Bacik 		   trans->state >= TRANS_STATE_COMMIT_START ||
1450501407aaSJosef Bacik 		   trans->aborted);
1451bb9c12c9SSage Weil }
1452bb9c12c9SSage Weil 
1453bb9c12c9SSage Weil /*
1454bb9c12c9SSage Weil  * wait for the current transaction to start and then become unblocked.
1455bb9c12c9SSage Weil  * caller holds ref.
1456bb9c12c9SSage Weil  */
1457bb9c12c9SSage Weil static void wait_current_trans_commit_start_and_unblock(struct btrfs_root *root,
1458bb9c12c9SSage Weil 					 struct btrfs_transaction *trans)
1459bb9c12c9SSage Weil {
146072d63ed6SLi Zefan 	wait_event(root->fs_info->transaction_wait,
1461501407aaSJosef Bacik 		   trans->state >= TRANS_STATE_UNBLOCKED ||
1462501407aaSJosef Bacik 		   trans->aborted);
1463bb9c12c9SSage Weil }
1464bb9c12c9SSage Weil 
1465bb9c12c9SSage Weil /*
1466bb9c12c9SSage Weil  * commit transactions asynchronously. once btrfs_commit_transaction_async
1467bb9c12c9SSage Weil  * returns, any subsequent transaction will not be allowed to join.
1468bb9c12c9SSage Weil  */
1469bb9c12c9SSage Weil struct btrfs_async_commit {
1470bb9c12c9SSage Weil 	struct btrfs_trans_handle *newtrans;
1471bb9c12c9SSage Weil 	struct btrfs_root *root;
14727892b5afSMiao Xie 	struct work_struct work;
1473bb9c12c9SSage Weil };
1474bb9c12c9SSage Weil 
1475bb9c12c9SSage Weil static void do_async_commit(struct work_struct *work)
1476bb9c12c9SSage Weil {
1477bb9c12c9SSage Weil 	struct btrfs_async_commit *ac =
14787892b5afSMiao Xie 		container_of(work, struct btrfs_async_commit, work);
1479bb9c12c9SSage Weil 
14806fc4e354SSage Weil 	/*
14816fc4e354SSage Weil 	 * We've got freeze protection passed with the transaction.
14826fc4e354SSage Weil 	 * Tell lockdep about it.
14836fc4e354SSage Weil 	 */
1484b1a06a4bSLiu Bo 	if (ac->newtrans->type & __TRANS_FREEZABLE)
14856fc4e354SSage Weil 		rwsem_acquire_read(
14866fc4e354SSage Weil 		     &ac->root->fs_info->sb->s_writers.lock_map[SB_FREEZE_FS-1],
14876fc4e354SSage Weil 		     0, 1, _THIS_IP_);
14886fc4e354SSage Weil 
1489e209db7aSSage Weil 	current->journal_info = ac->newtrans;
1490e209db7aSSage Weil 
1491bb9c12c9SSage Weil 	btrfs_commit_transaction(ac->newtrans, ac->root);
1492bb9c12c9SSage Weil 	kfree(ac);
1493bb9c12c9SSage Weil }
1494bb9c12c9SSage Weil 
1495bb9c12c9SSage Weil int btrfs_commit_transaction_async(struct btrfs_trans_handle *trans,
1496bb9c12c9SSage Weil 				   struct btrfs_root *root,
1497bb9c12c9SSage Weil 				   int wait_for_unblock)
1498bb9c12c9SSage Weil {
1499bb9c12c9SSage Weil 	struct btrfs_async_commit *ac;
1500bb9c12c9SSage Weil 	struct btrfs_transaction *cur_trans;
1501bb9c12c9SSage Weil 
1502bb9c12c9SSage Weil 	ac = kmalloc(sizeof(*ac), GFP_NOFS);
1503db5b493aSTsutomu Itoh 	if (!ac)
1504db5b493aSTsutomu Itoh 		return -ENOMEM;
1505bb9c12c9SSage Weil 
15067892b5afSMiao Xie 	INIT_WORK(&ac->work, do_async_commit);
1507bb9c12c9SSage Weil 	ac->root = root;
15087a7eaa40SJosef Bacik 	ac->newtrans = btrfs_join_transaction(root);
15093612b495STsutomu Itoh 	if (IS_ERR(ac->newtrans)) {
15103612b495STsutomu Itoh 		int err = PTR_ERR(ac->newtrans);
15113612b495STsutomu Itoh 		kfree(ac);
15123612b495STsutomu Itoh 		return err;
15133612b495STsutomu Itoh 	}
1514bb9c12c9SSage Weil 
1515bb9c12c9SSage Weil 	/* take transaction reference */
1516bb9c12c9SSage Weil 	cur_trans = trans->transaction;
151713c5a93eSJosef Bacik 	atomic_inc(&cur_trans->use_count);
1518bb9c12c9SSage Weil 
1519bb9c12c9SSage Weil 	btrfs_end_transaction(trans, root);
15206fc4e354SSage Weil 
15216fc4e354SSage Weil 	/*
15226fc4e354SSage Weil 	 * Tell lockdep we've released the freeze rwsem, since the
15236fc4e354SSage Weil 	 * async commit thread will be the one to unlock it.
15246fc4e354SSage Weil 	 */
1525b1a06a4bSLiu Bo 	if (ac->newtrans->type & __TRANS_FREEZABLE)
1526ff7c1d33SMiao Xie 		rwsem_release(
1527ff7c1d33SMiao Xie 			&root->fs_info->sb->s_writers.lock_map[SB_FREEZE_FS-1],
15286fc4e354SSage Weil 			1, _THIS_IP_);
15296fc4e354SSage Weil 
15307892b5afSMiao Xie 	schedule_work(&ac->work);
1531bb9c12c9SSage Weil 
1532bb9c12c9SSage Weil 	/* wait for transaction to start and unblock */
1533bb9c12c9SSage Weil 	if (wait_for_unblock)
1534bb9c12c9SSage Weil 		wait_current_trans_commit_start_and_unblock(root, cur_trans);
1535bb9c12c9SSage Weil 	else
1536bb9c12c9SSage Weil 		wait_current_trans_commit_start(root, cur_trans);
1537bb9c12c9SSage Weil 
153838e88054SSage Weil 	if (current->journal_info == trans)
153938e88054SSage Weil 		current->journal_info = NULL;
154038e88054SSage Weil 
1541724e2315SJosef Bacik 	btrfs_put_transaction(cur_trans);
1542bb9c12c9SSage Weil 	return 0;
1543bb9c12c9SSage Weil }
1544bb9c12c9SSage Weil 
154549b25e05SJeff Mahoney 
154649b25e05SJeff Mahoney static void cleanup_transaction(struct btrfs_trans_handle *trans,
15477b8b92afSJosef Bacik 				struct btrfs_root *root, int err)
154849b25e05SJeff Mahoney {
154949b25e05SJeff Mahoney 	struct btrfs_transaction *cur_trans = trans->transaction;
1550f094ac32SLiu Bo 	DEFINE_WAIT(wait);
155149b25e05SJeff Mahoney 
155249b25e05SJeff Mahoney 	WARN_ON(trans->use_count > 1);
155349b25e05SJeff Mahoney 
15547b8b92afSJosef Bacik 	btrfs_abort_transaction(trans, root, err);
15557b8b92afSJosef Bacik 
155649b25e05SJeff Mahoney 	spin_lock(&root->fs_info->trans_lock);
155766b6135bSLiu Bo 
155825d8c284SMiao Xie 	/*
155925d8c284SMiao Xie 	 * If the transaction is removed from the list, it means this
156025d8c284SMiao Xie 	 * transaction has been committed successfully, so it is impossible
156125d8c284SMiao Xie 	 * to call the cleanup function.
156225d8c284SMiao Xie 	 */
156325d8c284SMiao Xie 	BUG_ON(list_empty(&cur_trans->list));
156466b6135bSLiu Bo 
156549b25e05SJeff Mahoney 	list_del_init(&cur_trans->list);
1566d7096fc3SJosef Bacik 	if (cur_trans == root->fs_info->running_transaction) {
15674a9d8bdeSMiao Xie 		cur_trans->state = TRANS_STATE_COMMIT_DOING;
1568f094ac32SLiu Bo 		spin_unlock(&root->fs_info->trans_lock);
1569f094ac32SLiu Bo 		wait_event(cur_trans->writer_wait,
1570f094ac32SLiu Bo 			   atomic_read(&cur_trans->num_writers) == 1);
1571f094ac32SLiu Bo 
1572f094ac32SLiu Bo 		spin_lock(&root->fs_info->trans_lock);
1573d7096fc3SJosef Bacik 	}
157449b25e05SJeff Mahoney 	spin_unlock(&root->fs_info->trans_lock);
157549b25e05SJeff Mahoney 
157649b25e05SJeff Mahoney 	btrfs_cleanup_one_transaction(trans->transaction, root);
157749b25e05SJeff Mahoney 
15784a9d8bdeSMiao Xie 	spin_lock(&root->fs_info->trans_lock);
15794a9d8bdeSMiao Xie 	if (cur_trans == root->fs_info->running_transaction)
15804a9d8bdeSMiao Xie 		root->fs_info->running_transaction = NULL;
15814a9d8bdeSMiao Xie 	spin_unlock(&root->fs_info->trans_lock);
15824a9d8bdeSMiao Xie 
1583e0228285SJosef Bacik 	if (trans->type & __TRANS_FREEZABLE)
1584e0228285SJosef Bacik 		sb_end_intwrite(root->fs_info->sb);
1585724e2315SJosef Bacik 	btrfs_put_transaction(cur_trans);
1586724e2315SJosef Bacik 	btrfs_put_transaction(cur_trans);
158749b25e05SJeff Mahoney 
158849b25e05SJeff Mahoney 	trace_btrfs_transaction_commit(root);
158949b25e05SJeff Mahoney 
159049b25e05SJeff Mahoney 	if (current->journal_info == trans)
159149b25e05SJeff Mahoney 		current->journal_info = NULL;
1592c0af8f0bSWang Shilong 	btrfs_scrub_cancel(root->fs_info);
159349b25e05SJeff Mahoney 
159449b25e05SJeff Mahoney 	kmem_cache_free(btrfs_trans_handle_cachep, trans);
159549b25e05SJeff Mahoney }
159649b25e05SJeff Mahoney 
1597ca469637SMiao Xie static int btrfs_flush_all_pending_stuffs(struct btrfs_trans_handle *trans,
1598ca469637SMiao Xie 					  struct btrfs_root *root)
1599ca469637SMiao Xie {
1600ca469637SMiao Xie 	int ret;
1601ca469637SMiao Xie 
1602ca469637SMiao Xie 	ret = btrfs_run_delayed_items(trans, root);
1603ca469637SMiao Xie 	/*
1604ca469637SMiao Xie 	 * running the delayed items may have added new refs. account
1605ca469637SMiao Xie 	 * them now so that they hinder processing of more delayed refs
1606ca469637SMiao Xie 	 * as little as possible.
1607ca469637SMiao Xie 	 */
160880d94fb3SFilipe David Borba Manana 	if (ret)
160980d94fb3SFilipe David Borba Manana 		return ret;
1610ca469637SMiao Xie 
1611ca469637SMiao Xie 	/*
1612ca469637SMiao Xie 	 * rename don't use btrfs_join_transaction, so, once we
1613ca469637SMiao Xie 	 * set the transaction to blocked above, we aren't going
1614ca469637SMiao Xie 	 * to get any new ordered operations.  We can safely run
1615ca469637SMiao Xie 	 * it here and no for sure that nothing new will be added
1616ca469637SMiao Xie 	 * to the list
1617ca469637SMiao Xie 	 */
1618569e0f35SJosef Bacik 	ret = btrfs_run_ordered_operations(trans, root, 1);
1619ca469637SMiao Xie 
1620eebc6084SMiao Xie 	return ret;
1621ca469637SMiao Xie }
1622ca469637SMiao Xie 
162382436617SMiao Xie static inline int btrfs_start_delalloc_flush(struct btrfs_fs_info *fs_info)
162482436617SMiao Xie {
162582436617SMiao Xie 	if (btrfs_test_opt(fs_info->tree_root, FLUSHONCOMMIT))
16266c255e67SMiao Xie 		return btrfs_start_delalloc_roots(fs_info, 1, -1);
162782436617SMiao Xie 	return 0;
162882436617SMiao Xie }
162982436617SMiao Xie 
163082436617SMiao Xie static inline void btrfs_wait_delalloc_flush(struct btrfs_fs_info *fs_info)
163182436617SMiao Xie {
163282436617SMiao Xie 	if (btrfs_test_opt(fs_info->tree_root, FLUSHONCOMMIT))
1633b0244199SMiao Xie 		btrfs_wait_ordered_roots(fs_info, -1);
163482436617SMiao Xie }
163582436617SMiao Xie 
163679154b1bSChris Mason int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
163779154b1bSChris Mason 			     struct btrfs_root *root)
163879154b1bSChris Mason {
163949b25e05SJeff Mahoney 	struct btrfs_transaction *cur_trans = trans->transaction;
16408fd17795SChris Mason 	struct btrfs_transaction *prev_trans = NULL;
164125287e0aSMiao Xie 	int ret;
164279154b1bSChris Mason 
1643569e0f35SJosef Bacik 	ret = btrfs_run_ordered_operations(trans, root, 0);
164425287e0aSMiao Xie 	if (ret) {
164525287e0aSMiao Xie 		btrfs_abort_transaction(trans, root, ret);
1646e4a2bcacSJosef Bacik 		btrfs_end_transaction(trans, root);
1647e4a2bcacSJosef Bacik 		return ret;
164825287e0aSMiao Xie 	}
164925287e0aSMiao Xie 
16508d25a086SMiao Xie 	/* Stop the commit early if ->aborted is set */
16518d25a086SMiao Xie 	if (unlikely(ACCESS_ONCE(cur_trans->aborted))) {
165225287e0aSMiao Xie 		ret = cur_trans->aborted;
1653e4a2bcacSJosef Bacik 		btrfs_end_transaction(trans, root);
1654e4a2bcacSJosef Bacik 		return ret;
165525287e0aSMiao Xie 	}
165649b25e05SJeff Mahoney 
165756bec294SChris Mason 	/* make a pass through all the delayed refs we have so far
165856bec294SChris Mason 	 * any runnings procs may add more while we are here
165956bec294SChris Mason 	 */
166056bec294SChris Mason 	ret = btrfs_run_delayed_refs(trans, root, 0);
1661e4a2bcacSJosef Bacik 	if (ret) {
1662e4a2bcacSJosef Bacik 		btrfs_end_transaction(trans, root);
1663e4a2bcacSJosef Bacik 		return ret;
1664e4a2bcacSJosef Bacik 	}
166556bec294SChris Mason 
16660e721106SJosef Bacik 	btrfs_trans_release_metadata(trans, root);
16670e721106SJosef Bacik 	trans->block_rsv = NULL;
1668272d26d0SMiao Xie 	if (trans->qgroup_reserved) {
1669272d26d0SMiao Xie 		btrfs_qgroup_free(root, trans->qgroup_reserved);
1670272d26d0SMiao Xie 		trans->qgroup_reserved = 0;
1671272d26d0SMiao Xie 	}
16720e721106SJosef Bacik 
1673b7ec40d7SChris Mason 	cur_trans = trans->transaction;
167449b25e05SJeff Mahoney 
167556bec294SChris Mason 	/*
167656bec294SChris Mason 	 * set the flushing flag so procs in this transaction have to
167756bec294SChris Mason 	 * start sending their work down.
167856bec294SChris Mason 	 */
1679b7ec40d7SChris Mason 	cur_trans->delayed_refs.flushing = 1;
16801be41b78SJosef Bacik 	smp_wmb();
168156bec294SChris Mason 
1682ea658badSJosef Bacik 	if (!list_empty(&trans->new_bgs))
1683ea658badSJosef Bacik 		btrfs_create_pending_block_groups(trans, root);
1684ea658badSJosef Bacik 
1685c3e69d58SChris Mason 	ret = btrfs_run_delayed_refs(trans, root, 0);
1686e4a2bcacSJosef Bacik 	if (ret) {
1687e4a2bcacSJosef Bacik 		btrfs_end_transaction(trans, root);
1688e4a2bcacSJosef Bacik 		return ret;
1689e4a2bcacSJosef Bacik 	}
169056bec294SChris Mason 
16914a9d8bdeSMiao Xie 	spin_lock(&root->fs_info->trans_lock);
16924a9d8bdeSMiao Xie 	if (cur_trans->state >= TRANS_STATE_COMMIT_START) {
16934a9d8bdeSMiao Xie 		spin_unlock(&root->fs_info->trans_lock);
169413c5a93eSJosef Bacik 		atomic_inc(&cur_trans->use_count);
169549b25e05SJeff Mahoney 		ret = btrfs_end_transaction(trans, root);
1696ccd467d6SChris Mason 
1697b9c8300cSLi Zefan 		wait_for_commit(root, cur_trans);
169815ee9bc7SJosef Bacik 
1699724e2315SJosef Bacik 		btrfs_put_transaction(cur_trans);
170015ee9bc7SJosef Bacik 
170149b25e05SJeff Mahoney 		return ret;
170279154b1bSChris Mason 	}
17034313b399SChris Mason 
17044a9d8bdeSMiao Xie 	cur_trans->state = TRANS_STATE_COMMIT_START;
1705bb9c12c9SSage Weil 	wake_up(&root->fs_info->transaction_blocked_wait);
1706bb9c12c9SSage Weil 
1707ccd467d6SChris Mason 	if (cur_trans->list.prev != &root->fs_info->trans_list) {
1708ccd467d6SChris Mason 		prev_trans = list_entry(cur_trans->list.prev,
1709ccd467d6SChris Mason 					struct btrfs_transaction, list);
17104a9d8bdeSMiao Xie 		if (prev_trans->state != TRANS_STATE_COMPLETED) {
171113c5a93eSJosef Bacik 			atomic_inc(&prev_trans->use_count);
1712a4abeea4SJosef Bacik 			spin_unlock(&root->fs_info->trans_lock);
1713ccd467d6SChris Mason 
1714ccd467d6SChris Mason 			wait_for_commit(root, prev_trans);
1715ccd467d6SChris Mason 
1716724e2315SJosef Bacik 			btrfs_put_transaction(prev_trans);
1717a4abeea4SJosef Bacik 		} else {
1718a4abeea4SJosef Bacik 			spin_unlock(&root->fs_info->trans_lock);
1719ccd467d6SChris Mason 		}
1720a4abeea4SJosef Bacik 	} else {
1721a4abeea4SJosef Bacik 		spin_unlock(&root->fs_info->trans_lock);
1722ccd467d6SChris Mason 	}
172315ee9bc7SJosef Bacik 
17240860adfdSMiao Xie 	extwriter_counter_dec(cur_trans, trans->type);
17250860adfdSMiao Xie 
172682436617SMiao Xie 	ret = btrfs_start_delalloc_flush(root->fs_info);
172782436617SMiao Xie 	if (ret)
172882436617SMiao Xie 		goto cleanup_transaction;
172982436617SMiao Xie 
1730ca469637SMiao Xie 	ret = btrfs_flush_all_pending_stuffs(trans, root);
173149b25e05SJeff Mahoney 	if (ret)
173249b25e05SJeff Mahoney 		goto cleanup_transaction;
173316cdcec7SMiao Xie 
1734581227d0SMiao Xie 	wait_event(cur_trans->writer_wait,
1735581227d0SMiao Xie 		   extwriter_counter_read(cur_trans) == 0);
1736ed3b3d31SChris Mason 
1737581227d0SMiao Xie 	/* some pending stuffs might be added after the previous flush. */
1738ca469637SMiao Xie 	ret = btrfs_flush_all_pending_stuffs(trans, root);
1739ca469637SMiao Xie 	if (ret)
1740ca469637SMiao Xie 		goto cleanup_transaction;
1741ca469637SMiao Xie 
174282436617SMiao Xie 	btrfs_wait_delalloc_flush(root->fs_info);
1743cb7ab021SWang Shilong 
1744cb7ab021SWang Shilong 	btrfs_scrub_pause(root);
1745ed0ca140SJosef Bacik 	/*
1746ed0ca140SJosef Bacik 	 * Ok now we need to make sure to block out any other joins while we
1747ed0ca140SJosef Bacik 	 * commit the transaction.  We could have started a join before setting
17484a9d8bdeSMiao Xie 	 * COMMIT_DOING so make sure to wait for num_writers to == 1 again.
1749ed0ca140SJosef Bacik 	 */
1750a4abeea4SJosef Bacik 	spin_lock(&root->fs_info->trans_lock);
17514a9d8bdeSMiao Xie 	cur_trans->state = TRANS_STATE_COMMIT_DOING;
1752a4abeea4SJosef Bacik 	spin_unlock(&root->fs_info->trans_lock);
1753ed0ca140SJosef Bacik 	wait_event(cur_trans->writer_wait,
1754ed0ca140SJosef Bacik 		   atomic_read(&cur_trans->num_writers) == 1);
175515ee9bc7SJosef Bacik 
17562cba30f1SMiao Xie 	/* ->aborted might be set after the previous check, so check it */
17572cba30f1SMiao Xie 	if (unlikely(ACCESS_ONCE(cur_trans->aborted))) {
17582cba30f1SMiao Xie 		ret = cur_trans->aborted;
17596cf7f77eSWang Shilong 		goto scrub_continue;
17602cba30f1SMiao Xie 	}
17617585717fSChris Mason 	/*
17627585717fSChris Mason 	 * the reloc mutex makes sure that we stop
17637585717fSChris Mason 	 * the balancing code from coming in and moving
17647585717fSChris Mason 	 * extents around in the middle of the commit
17657585717fSChris Mason 	 */
17667585717fSChris Mason 	mutex_lock(&root->fs_info->reloc_mutex);
17677585717fSChris Mason 
176842874b3dSMiao Xie 	/*
176942874b3dSMiao Xie 	 * We needn't worry about the delayed items because we will
177042874b3dSMiao Xie 	 * deal with them in create_pending_snapshot(), which is the
177142874b3dSMiao Xie 	 * core function of the snapshot creation.
177242874b3dSMiao Xie 	 */
177342874b3dSMiao Xie 	ret = create_pending_snapshots(trans, root->fs_info);
177449b25e05SJeff Mahoney 	if (ret) {
177549b25e05SJeff Mahoney 		mutex_unlock(&root->fs_info->reloc_mutex);
17766cf7f77eSWang Shilong 		goto scrub_continue;
177749b25e05SJeff Mahoney 	}
17783063d29fSChris Mason 
177942874b3dSMiao Xie 	/*
178042874b3dSMiao Xie 	 * We insert the dir indexes of the snapshots and update the inode
178142874b3dSMiao Xie 	 * of the snapshots' parents after the snapshot creation, so there
178242874b3dSMiao Xie 	 * are some delayed items which are not dealt with. Now deal with
178342874b3dSMiao Xie 	 * them.
178442874b3dSMiao Xie 	 *
178542874b3dSMiao Xie 	 * We needn't worry that this operation will corrupt the snapshots,
178642874b3dSMiao Xie 	 * because all the tree which are snapshoted will be forced to COW
178742874b3dSMiao Xie 	 * the nodes and leaves.
178842874b3dSMiao Xie 	 */
178942874b3dSMiao Xie 	ret = btrfs_run_delayed_items(trans, root);
179049b25e05SJeff Mahoney 	if (ret) {
179149b25e05SJeff Mahoney 		mutex_unlock(&root->fs_info->reloc_mutex);
17926cf7f77eSWang Shilong 		goto scrub_continue;
179349b25e05SJeff Mahoney 	}
179416cdcec7SMiao Xie 
179556bec294SChris Mason 	ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
179649b25e05SJeff Mahoney 	if (ret) {
179749b25e05SJeff Mahoney 		mutex_unlock(&root->fs_info->reloc_mutex);
17986cf7f77eSWang Shilong 		goto scrub_continue;
179949b25e05SJeff Mahoney 	}
180056bec294SChris Mason 
1801e999376fSChris Mason 	/*
1802e999376fSChris Mason 	 * make sure none of the code above managed to slip in a
1803e999376fSChris Mason 	 * delayed item
1804e999376fSChris Mason 	 */
1805e999376fSChris Mason 	btrfs_assert_delayed_root_empty(root);
1806e999376fSChris Mason 
18072c90e5d6SChris Mason 	WARN_ON(cur_trans != trans->transaction);
1808dc17ff8fSChris Mason 
1809e02119d5SChris Mason 	/* btrfs_commit_tree_roots is responsible for getting the
1810e02119d5SChris Mason 	 * various roots consistent with each other.  Every pointer
1811e02119d5SChris Mason 	 * in the tree of tree roots has to point to the most up to date
1812e02119d5SChris Mason 	 * root for every subvolume and other tree.  So, we have to keep
1813e02119d5SChris Mason 	 * the tree logging code from jumping in and changing any
1814e02119d5SChris Mason 	 * of the trees.
1815e02119d5SChris Mason 	 *
1816e02119d5SChris Mason 	 * At this point in the commit, there can't be any tree-log
1817e02119d5SChris Mason 	 * writers, but a little lower down we drop the trans mutex
1818e02119d5SChris Mason 	 * and let new people in.  By holding the tree_log_mutex
1819e02119d5SChris Mason 	 * from now until after the super is written, we avoid races
1820e02119d5SChris Mason 	 * with the tree-log code.
1821e02119d5SChris Mason 	 */
1822e02119d5SChris Mason 	mutex_lock(&root->fs_info->tree_log_mutex);
18231a40e23bSZheng Yan 
18245d4f98a2SYan Zheng 	ret = commit_fs_roots(trans, root);
182549b25e05SJeff Mahoney 	if (ret) {
182649b25e05SJeff Mahoney 		mutex_unlock(&root->fs_info->tree_log_mutex);
1827871383beSDavid Sterba 		mutex_unlock(&root->fs_info->reloc_mutex);
18286cf7f77eSWang Shilong 		goto scrub_continue;
182949b25e05SJeff Mahoney 	}
183054aa1f4dSChris Mason 
18313818aea2SQu Wenruo 	/*
18323818aea2SQu Wenruo 	 * Since the transaction is done, we should set the inode map cache flag
18333818aea2SQu Wenruo 	 * before any other comming transaction.
18343818aea2SQu Wenruo 	 */
18353818aea2SQu Wenruo 	if (btrfs_test_opt(root, CHANGE_INODE_CACHE))
18363818aea2SQu Wenruo 		btrfs_set_opt(root->fs_info->mount_opt, INODE_MAP_CACHE);
18373818aea2SQu Wenruo 	else
18383818aea2SQu Wenruo 		btrfs_clear_opt(root->fs_info->mount_opt, INODE_MAP_CACHE);
18393818aea2SQu Wenruo 
18405d4f98a2SYan Zheng 	/* commit_fs_roots gets rid of all the tree log roots, it is now
1841e02119d5SChris Mason 	 * safe to free the root of tree log roots
1842e02119d5SChris Mason 	 */
1843e02119d5SChris Mason 	btrfs_free_log_root_tree(trans, root->fs_info);
1844e02119d5SChris Mason 
18455d4f98a2SYan Zheng 	ret = commit_cowonly_roots(trans, root);
184649b25e05SJeff Mahoney 	if (ret) {
184749b25e05SJeff Mahoney 		mutex_unlock(&root->fs_info->tree_log_mutex);
1848871383beSDavid Sterba 		mutex_unlock(&root->fs_info->reloc_mutex);
18496cf7f77eSWang Shilong 		goto scrub_continue;
185049b25e05SJeff Mahoney 	}
185154aa1f4dSChris Mason 
18522cba30f1SMiao Xie 	/*
18532cba30f1SMiao Xie 	 * The tasks which save the space cache and inode cache may also
18542cba30f1SMiao Xie 	 * update ->aborted, check it.
18552cba30f1SMiao Xie 	 */
18562cba30f1SMiao Xie 	if (unlikely(ACCESS_ONCE(cur_trans->aborted))) {
18572cba30f1SMiao Xie 		ret = cur_trans->aborted;
18582cba30f1SMiao Xie 		mutex_unlock(&root->fs_info->tree_log_mutex);
18592cba30f1SMiao Xie 		mutex_unlock(&root->fs_info->reloc_mutex);
18606cf7f77eSWang Shilong 		goto scrub_continue;
18612cba30f1SMiao Xie 	}
18622cba30f1SMiao Xie 
186311833d66SYan Zheng 	btrfs_prepare_extent_commit(trans, root);
186411833d66SYan Zheng 
186578fae27eSChris Mason 	cur_trans = root->fs_info->running_transaction;
18665f39d397SChris Mason 
18675d4f98a2SYan Zheng 	btrfs_set_root_node(&root->fs_info->tree_root->root_item,
18685d4f98a2SYan Zheng 			    root->fs_info->tree_root->node);
18699e351cc8SJosef Bacik 	list_add_tail(&root->fs_info->tree_root->dirty_list,
18709e351cc8SJosef Bacik 		      &cur_trans->switch_commits);
18715d4f98a2SYan Zheng 
18725d4f98a2SYan Zheng 	btrfs_set_root_node(&root->fs_info->chunk_root->root_item,
18735d4f98a2SYan Zheng 			    root->fs_info->chunk_root->node);
18749e351cc8SJosef Bacik 	list_add_tail(&root->fs_info->chunk_root->dirty_list,
18759e351cc8SJosef Bacik 		      &cur_trans->switch_commits);
18769e351cc8SJosef Bacik 
18779e351cc8SJosef Bacik 	switch_commit_roots(cur_trans, root->fs_info);
18785d4f98a2SYan Zheng 
1879edf39272SJan Schmidt 	assert_qgroups_uptodate(trans);
18805d4f98a2SYan Zheng 	update_super_roots(root);
1881e02119d5SChris Mason 
18826c41761fSDavid Sterba 	btrfs_set_super_log_root(root->fs_info->super_copy, 0);
18836c41761fSDavid Sterba 	btrfs_set_super_log_root_level(root->fs_info->super_copy, 0);
18846c41761fSDavid Sterba 	memcpy(root->fs_info->super_for_commit, root->fs_info->super_copy,
18856c41761fSDavid Sterba 	       sizeof(*root->fs_info->super_copy));
1886ccd467d6SChris Mason 
1887a4abeea4SJosef Bacik 	spin_lock(&root->fs_info->trans_lock);
18884a9d8bdeSMiao Xie 	cur_trans->state = TRANS_STATE_UNBLOCKED;
1889a4abeea4SJosef Bacik 	root->fs_info->running_transaction = NULL;
1890a4abeea4SJosef Bacik 	spin_unlock(&root->fs_info->trans_lock);
18917585717fSChris Mason 	mutex_unlock(&root->fs_info->reloc_mutex);
1892b7ec40d7SChris Mason 
1893f9295749SChris Mason 	wake_up(&root->fs_info->transaction_wait);
1894e6dcd2dcSChris Mason 
189579154b1bSChris Mason 	ret = btrfs_write_and_wait_transaction(trans, root);
189649b25e05SJeff Mahoney 	if (ret) {
189749b25e05SJeff Mahoney 		btrfs_error(root->fs_info, ret,
189808748810SDavid Sterba 			    "Error while writing out transaction");
189949b25e05SJeff Mahoney 		mutex_unlock(&root->fs_info->tree_log_mutex);
19006cf7f77eSWang Shilong 		goto scrub_continue;
190149b25e05SJeff Mahoney 	}
190249b25e05SJeff Mahoney 
190349b25e05SJeff Mahoney 	ret = write_ctree_super(trans, root, 0);
190449b25e05SJeff Mahoney 	if (ret) {
190549b25e05SJeff Mahoney 		mutex_unlock(&root->fs_info->tree_log_mutex);
19066cf7f77eSWang Shilong 		goto scrub_continue;
190749b25e05SJeff Mahoney 	}
19084313b399SChris Mason 
1909e02119d5SChris Mason 	/*
1910e02119d5SChris Mason 	 * the super is written, we can safely allow the tree-loggers
1911e02119d5SChris Mason 	 * to go about their business
1912e02119d5SChris Mason 	 */
1913e02119d5SChris Mason 	mutex_unlock(&root->fs_info->tree_log_mutex);
1914e02119d5SChris Mason 
191511833d66SYan Zheng 	btrfs_finish_extent_commit(trans, root);
19164313b399SChris Mason 
191715ee9bc7SJosef Bacik 	root->fs_info->last_trans_committed = cur_trans->transid;
19184a9d8bdeSMiao Xie 	/*
19194a9d8bdeSMiao Xie 	 * We needn't acquire the lock here because there is no other task
19204a9d8bdeSMiao Xie 	 * which can change it.
19214a9d8bdeSMiao Xie 	 */
19224a9d8bdeSMiao Xie 	cur_trans->state = TRANS_STATE_COMPLETED;
19232c90e5d6SChris Mason 	wake_up(&cur_trans->commit_wait);
19243de4586cSChris Mason 
1925a4abeea4SJosef Bacik 	spin_lock(&root->fs_info->trans_lock);
192613c5a93eSJosef Bacik 	list_del_init(&cur_trans->list);
1927a4abeea4SJosef Bacik 	spin_unlock(&root->fs_info->trans_lock);
1928a4abeea4SJosef Bacik 
1929724e2315SJosef Bacik 	btrfs_put_transaction(cur_trans);
1930724e2315SJosef Bacik 	btrfs_put_transaction(cur_trans);
193158176a96SJosef Bacik 
19320860adfdSMiao Xie 	if (trans->type & __TRANS_FREEZABLE)
1933b2b5ef5cSJan Kara 		sb_end_intwrite(root->fs_info->sb);
1934b2b5ef5cSJan Kara 
19351abe9b8aSliubo 	trace_btrfs_transaction_commit(root);
19361abe9b8aSliubo 
1937a2de733cSArne Jansen 	btrfs_scrub_continue(root);
1938a2de733cSArne Jansen 
19399ed74f2dSJosef Bacik 	if (current->journal_info == trans)
19409ed74f2dSJosef Bacik 		current->journal_info = NULL;
19419ed74f2dSJosef Bacik 
19422c90e5d6SChris Mason 	kmem_cache_free(btrfs_trans_handle_cachep, trans);
194324bbcf04SYan, Zheng 
194424bbcf04SYan, Zheng 	if (current != root->fs_info->transaction_kthread)
194524bbcf04SYan, Zheng 		btrfs_run_delayed_iputs(root);
194624bbcf04SYan, Zheng 
194779154b1bSChris Mason 	return ret;
194849b25e05SJeff Mahoney 
19496cf7f77eSWang Shilong scrub_continue:
19506cf7f77eSWang Shilong 	btrfs_scrub_continue(root);
195149b25e05SJeff Mahoney cleanup_transaction:
19520e721106SJosef Bacik 	btrfs_trans_release_metadata(trans, root);
19530e721106SJosef Bacik 	trans->block_rsv = NULL;
1954272d26d0SMiao Xie 	if (trans->qgroup_reserved) {
1955272d26d0SMiao Xie 		btrfs_qgroup_free(root, trans->qgroup_reserved);
1956272d26d0SMiao Xie 		trans->qgroup_reserved = 0;
1957272d26d0SMiao Xie 	}
1958c2cf52ebSSimon Kirby 	btrfs_warn(root->fs_info, "Skipping commit of aborted transaction.");
195949b25e05SJeff Mahoney 	if (current->journal_info == trans)
196049b25e05SJeff Mahoney 		current->journal_info = NULL;
19617b8b92afSJosef Bacik 	cleanup_transaction(trans, root, ret);
196249b25e05SJeff Mahoney 
196349b25e05SJeff Mahoney 	return ret;
196479154b1bSChris Mason }
196579154b1bSChris Mason 
1966d352ac68SChris Mason /*
19679d1a2a3aSDavid Sterba  * return < 0 if error
19689d1a2a3aSDavid Sterba  * 0 if there are no more dead_roots at the time of call
19699d1a2a3aSDavid Sterba  * 1 there are more to be processed, call me again
19709d1a2a3aSDavid Sterba  *
19719d1a2a3aSDavid Sterba  * The return value indicates there are certainly more snapshots to delete, but
19729d1a2a3aSDavid Sterba  * if there comes a new one during processing, it may return 0. We don't mind,
19739d1a2a3aSDavid Sterba  * because btrfs_commit_super will poke cleaner thread and it will process it a
19749d1a2a3aSDavid Sterba  * few seconds later.
1975d352ac68SChris Mason  */
19769d1a2a3aSDavid Sterba int btrfs_clean_one_deleted_snapshot(struct btrfs_root *root)
1977e9d0b13bSChris Mason {
19789d1a2a3aSDavid Sterba 	int ret;
19795d4f98a2SYan Zheng 	struct btrfs_fs_info *fs_info = root->fs_info;
1980e9d0b13bSChris Mason 
1981a4abeea4SJosef Bacik 	spin_lock(&fs_info->trans_lock);
19829d1a2a3aSDavid Sterba 	if (list_empty(&fs_info->dead_roots)) {
19839d1a2a3aSDavid Sterba 		spin_unlock(&fs_info->trans_lock);
19849d1a2a3aSDavid Sterba 		return 0;
19859d1a2a3aSDavid Sterba 	}
19869d1a2a3aSDavid Sterba 	root = list_first_entry(&fs_info->dead_roots,
19879d1a2a3aSDavid Sterba 			struct btrfs_root, root_list);
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