xref: /openbmc/linux/fs/btrfs/transaction.c (revision 1bbc621e)
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 
38e8c9f186SDavid Sterba static const 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));
671262133bSJosef Bacik 		if (transaction->delayed_refs.pending_csums)
681262133bSJosef Bacik 			printk(KERN_ERR "pending csums is %llu\n",
691262133bSJosef Bacik 			       transaction->delayed_refs.pending_csums);
706df9a95eSJosef Bacik 		while (!list_empty(&transaction->pending_chunks)) {
716df9a95eSJosef Bacik 			struct extent_map *em;
726df9a95eSJosef Bacik 
736df9a95eSJosef Bacik 			em = list_first_entry(&transaction->pending_chunks,
746df9a95eSJosef Bacik 					      struct extent_map, list);
756df9a95eSJosef Bacik 			list_del_init(&em->list);
766df9a95eSJosef Bacik 			free_extent_map(em);
776df9a95eSJosef Bacik 		}
782c90e5d6SChris Mason 		kmem_cache_free(btrfs_transaction_cachep, transaction);
7979154b1bSChris Mason 	}
8078fae27eSChris Mason }
8179154b1bSChris Mason 
82663dfbb0SFilipe Manana static void clear_btree_io_tree(struct extent_io_tree *tree)
83663dfbb0SFilipe Manana {
84663dfbb0SFilipe Manana 	spin_lock(&tree->lock);
85663dfbb0SFilipe Manana 	while (!RB_EMPTY_ROOT(&tree->state)) {
86663dfbb0SFilipe Manana 		struct rb_node *node;
87663dfbb0SFilipe Manana 		struct extent_state *state;
88663dfbb0SFilipe Manana 
89663dfbb0SFilipe Manana 		node = rb_first(&tree->state);
90663dfbb0SFilipe Manana 		state = rb_entry(node, struct extent_state, rb_node);
91663dfbb0SFilipe Manana 		rb_erase(&state->rb_node, &tree->state);
92663dfbb0SFilipe Manana 		RB_CLEAR_NODE(&state->rb_node);
93663dfbb0SFilipe Manana 		/*
94663dfbb0SFilipe Manana 		 * btree io trees aren't supposed to have tasks waiting for
95663dfbb0SFilipe Manana 		 * changes in the flags of extent states ever.
96663dfbb0SFilipe Manana 		 */
97663dfbb0SFilipe Manana 		ASSERT(!waitqueue_active(&state->wq));
98663dfbb0SFilipe Manana 		free_extent_state(state);
99351810c1SDavid Sterba 
100351810c1SDavid Sterba 		cond_resched_lock(&tree->lock);
101663dfbb0SFilipe Manana 	}
102663dfbb0SFilipe Manana 	spin_unlock(&tree->lock);
103663dfbb0SFilipe Manana }
104663dfbb0SFilipe Manana 
1059e351cc8SJosef Bacik static noinline void switch_commit_roots(struct btrfs_transaction *trans,
1069e351cc8SJosef Bacik 					 struct btrfs_fs_info *fs_info)
107817d52f8SJosef Bacik {
1089e351cc8SJosef Bacik 	struct btrfs_root *root, *tmp;
1099e351cc8SJosef Bacik 
1109e351cc8SJosef Bacik 	down_write(&fs_info->commit_root_sem);
1119e351cc8SJosef Bacik 	list_for_each_entry_safe(root, tmp, &trans->switch_commits,
1129e351cc8SJosef Bacik 				 dirty_list) {
1139e351cc8SJosef Bacik 		list_del_init(&root->dirty_list);
114817d52f8SJosef Bacik 		free_extent_buffer(root->commit_root);
115817d52f8SJosef Bacik 		root->commit_root = btrfs_root_node(root);
1169e351cc8SJosef Bacik 		if (is_fstree(root->objectid))
1179e351cc8SJosef Bacik 			btrfs_unpin_free_ino(root);
118663dfbb0SFilipe Manana 		clear_btree_io_tree(&root->dirty_log_pages);
1199e351cc8SJosef Bacik 	}
1209e351cc8SJosef Bacik 	up_write(&fs_info->commit_root_sem);
121817d52f8SJosef Bacik }
122817d52f8SJosef Bacik 
1230860adfdSMiao Xie static inline void extwriter_counter_inc(struct btrfs_transaction *trans,
1240860adfdSMiao Xie 					 unsigned int type)
1250860adfdSMiao Xie {
1260860adfdSMiao Xie 	if (type & TRANS_EXTWRITERS)
1270860adfdSMiao Xie 		atomic_inc(&trans->num_extwriters);
1280860adfdSMiao Xie }
1290860adfdSMiao Xie 
1300860adfdSMiao Xie static inline void extwriter_counter_dec(struct btrfs_transaction *trans,
1310860adfdSMiao Xie 					 unsigned int type)
1320860adfdSMiao Xie {
1330860adfdSMiao Xie 	if (type & TRANS_EXTWRITERS)
1340860adfdSMiao Xie 		atomic_dec(&trans->num_extwriters);
1350860adfdSMiao Xie }
1360860adfdSMiao Xie 
1370860adfdSMiao Xie static inline void extwriter_counter_init(struct btrfs_transaction *trans,
1380860adfdSMiao Xie 					  unsigned int type)
1390860adfdSMiao Xie {
1400860adfdSMiao Xie 	atomic_set(&trans->num_extwriters, ((type & TRANS_EXTWRITERS) ? 1 : 0));
1410860adfdSMiao Xie }
1420860adfdSMiao Xie 
1430860adfdSMiao Xie static inline int extwriter_counter_read(struct btrfs_transaction *trans)
1440860adfdSMiao Xie {
1450860adfdSMiao Xie 	return atomic_read(&trans->num_extwriters);
146178260b2SMiao Xie }
147178260b2SMiao Xie 
148d352ac68SChris Mason /*
149d352ac68SChris Mason  * either allocate a new transaction or hop into the existing one
150d352ac68SChris Mason  */
1510860adfdSMiao Xie static noinline int join_transaction(struct btrfs_root *root, unsigned int type)
15279154b1bSChris Mason {
15379154b1bSChris Mason 	struct btrfs_transaction *cur_trans;
15419ae4e81SJan Schmidt 	struct btrfs_fs_info *fs_info = root->fs_info;
155a4abeea4SJosef Bacik 
15619ae4e81SJan Schmidt 	spin_lock(&fs_info->trans_lock);
157d43317dcSChris Mason loop:
15849b25e05SJeff Mahoney 	/* The file system has been taken offline. No new transactions. */
15987533c47SMiao Xie 	if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) {
16019ae4e81SJan Schmidt 		spin_unlock(&fs_info->trans_lock);
16149b25e05SJeff Mahoney 		return -EROFS;
16249b25e05SJeff Mahoney 	}
16349b25e05SJeff Mahoney 
16419ae4e81SJan Schmidt 	cur_trans = fs_info->running_transaction;
165a4abeea4SJosef Bacik 	if (cur_trans) {
166871383beSDavid Sterba 		if (cur_trans->aborted) {
16719ae4e81SJan Schmidt 			spin_unlock(&fs_info->trans_lock);
16849b25e05SJeff Mahoney 			return cur_trans->aborted;
169871383beSDavid Sterba 		}
1704a9d8bdeSMiao Xie 		if (btrfs_blocked_trans_types[cur_trans->state] & type) {
171178260b2SMiao Xie 			spin_unlock(&fs_info->trans_lock);
172178260b2SMiao Xie 			return -EBUSY;
173178260b2SMiao Xie 		}
174a4abeea4SJosef Bacik 		atomic_inc(&cur_trans->use_count);
175a4abeea4SJosef Bacik 		atomic_inc(&cur_trans->num_writers);
1760860adfdSMiao Xie 		extwriter_counter_inc(cur_trans, type);
17719ae4e81SJan Schmidt 		spin_unlock(&fs_info->trans_lock);
178a4abeea4SJosef Bacik 		return 0;
179a4abeea4SJosef Bacik 	}
18019ae4e81SJan Schmidt 	spin_unlock(&fs_info->trans_lock);
181a4abeea4SJosef Bacik 
182354aa0fbSMiao Xie 	/*
183354aa0fbSMiao Xie 	 * If we are ATTACH, we just want to catch the current transaction,
184354aa0fbSMiao Xie 	 * and commit it. If there is no transaction, just return ENOENT.
185354aa0fbSMiao Xie 	 */
186354aa0fbSMiao Xie 	if (type == TRANS_ATTACH)
187354aa0fbSMiao Xie 		return -ENOENT;
188354aa0fbSMiao Xie 
1894a9d8bdeSMiao Xie 	/*
1904a9d8bdeSMiao Xie 	 * JOIN_NOLOCK only happens during the transaction commit, so
1914a9d8bdeSMiao Xie 	 * it is impossible that ->running_transaction is NULL
1924a9d8bdeSMiao Xie 	 */
1934a9d8bdeSMiao Xie 	BUG_ON(type == TRANS_JOIN_NOLOCK);
1944a9d8bdeSMiao Xie 
195a4abeea4SJosef Bacik 	cur_trans = kmem_cache_alloc(btrfs_transaction_cachep, GFP_NOFS);
196db5b493aSTsutomu Itoh 	if (!cur_trans)
197db5b493aSTsutomu Itoh 		return -ENOMEM;
198d43317dcSChris Mason 
19919ae4e81SJan Schmidt 	spin_lock(&fs_info->trans_lock);
20019ae4e81SJan Schmidt 	if (fs_info->running_transaction) {
201d43317dcSChris Mason 		/*
202d43317dcSChris Mason 		 * someone started a transaction after we unlocked.  Make sure
2034a9d8bdeSMiao Xie 		 * to redo the checks above
204d43317dcSChris Mason 		 */
205a4abeea4SJosef Bacik 		kmem_cache_free(btrfs_transaction_cachep, cur_trans);
206d43317dcSChris Mason 		goto loop;
20787533c47SMiao Xie 	} else if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) {
208e4b50e14SDan Carpenter 		spin_unlock(&fs_info->trans_lock);
2097b8b92afSJosef Bacik 		kmem_cache_free(btrfs_transaction_cachep, cur_trans);
2107b8b92afSJosef Bacik 		return -EROFS;
211a4abeea4SJosef Bacik 	}
212d43317dcSChris Mason 
21313c5a93eSJosef Bacik 	atomic_set(&cur_trans->num_writers, 1);
2140860adfdSMiao Xie 	extwriter_counter_init(cur_trans, type);
21579154b1bSChris Mason 	init_waitqueue_head(&cur_trans->writer_wait);
21679154b1bSChris Mason 	init_waitqueue_head(&cur_trans->commit_wait);
2174a9d8bdeSMiao Xie 	cur_trans->state = TRANS_STATE_RUNNING;
218a4abeea4SJosef Bacik 	/*
219a4abeea4SJosef Bacik 	 * One for this trans handle, one so it will live on until we
220a4abeea4SJosef Bacik 	 * commit the transaction.
221a4abeea4SJosef Bacik 	 */
222a4abeea4SJosef Bacik 	atomic_set(&cur_trans->use_count, 2);
22313212b54SZhao Lei 	cur_trans->have_free_bgs = 0;
22408607c1bSChris Mason 	cur_trans->start_time = get_seconds();
2251bbc621eSChris Mason 	cur_trans->dirty_bg_run = 0;
22656bec294SChris Mason 
227c46effa6SLiu Bo 	cur_trans->delayed_refs.href_root = RB_ROOT;
228d7df2c79SJosef Bacik 	atomic_set(&cur_trans->delayed_refs.num_entries, 0);
229c3e69d58SChris Mason 	cur_trans->delayed_refs.num_heads_ready = 0;
2301262133bSJosef Bacik 	cur_trans->delayed_refs.pending_csums = 0;
231c3e69d58SChris Mason 	cur_trans->delayed_refs.num_heads = 0;
23256bec294SChris Mason 	cur_trans->delayed_refs.flushing = 0;
233c3e69d58SChris Mason 	cur_trans->delayed_refs.run_delayed_start = 0;
23420b297d6SJan Schmidt 
23520b297d6SJan Schmidt 	/*
23620b297d6SJan Schmidt 	 * although the tree mod log is per file system and not per transaction,
23720b297d6SJan Schmidt 	 * the log must never go across transaction boundaries.
23820b297d6SJan Schmidt 	 */
23920b297d6SJan Schmidt 	smp_mb();
24031b1a2bdSJulia Lawall 	if (!list_empty(&fs_info->tree_mod_seq_list))
241efe120a0SFrank Holton 		WARN(1, KERN_ERR "BTRFS: tree_mod_seq_list not empty when "
24220b297d6SJan Schmidt 			"creating a fresh transaction\n");
24331b1a2bdSJulia Lawall 	if (!RB_EMPTY_ROOT(&fs_info->tree_mod_log))
244efe120a0SFrank Holton 		WARN(1, KERN_ERR "BTRFS: tree_mod_log rb tree not empty when "
24520b297d6SJan Schmidt 			"creating a fresh transaction\n");
246fc36ed7eSJan Schmidt 	atomic64_set(&fs_info->tree_mod_seq, 0);
24720b297d6SJan Schmidt 
24856bec294SChris Mason 	spin_lock_init(&cur_trans->delayed_refs.lock);
24956bec294SChris Mason 
2503063d29fSChris Mason 	INIT_LIST_HEAD(&cur_trans->pending_snapshots);
2516df9a95eSJosef Bacik 	INIT_LIST_HEAD(&cur_trans->pending_chunks);
2529e351cc8SJosef Bacik 	INIT_LIST_HEAD(&cur_trans->switch_commits);
25350d9aa99SJosef Bacik 	INIT_LIST_HEAD(&cur_trans->pending_ordered);
254ce93ec54SJosef Bacik 	INIT_LIST_HEAD(&cur_trans->dirty_bgs);
2551bbc621eSChris Mason 	INIT_LIST_HEAD(&cur_trans->io_bgs);
2561bbc621eSChris Mason 	mutex_init(&cur_trans->cache_write_mutex);
257cb723e49SJosef Bacik 	cur_trans->num_dirty_bgs = 0;
258ce93ec54SJosef Bacik 	spin_lock_init(&cur_trans->dirty_bgs_lock);
25919ae4e81SJan Schmidt 	list_add_tail(&cur_trans->list, &fs_info->trans_list);
260d1310b2eSChris Mason 	extent_io_tree_init(&cur_trans->dirty_pages,
26119ae4e81SJan Schmidt 			     fs_info->btree_inode->i_mapping);
26219ae4e81SJan Schmidt 	fs_info->generation++;
26319ae4e81SJan Schmidt 	cur_trans->transid = fs_info->generation;
26419ae4e81SJan Schmidt 	fs_info->running_transaction = cur_trans;
26549b25e05SJeff Mahoney 	cur_trans->aborted = 0;
26619ae4e81SJan Schmidt 	spin_unlock(&fs_info->trans_lock);
26715ee9bc7SJosef Bacik 
26879154b1bSChris Mason 	return 0;
26979154b1bSChris Mason }
27079154b1bSChris Mason 
271d352ac68SChris Mason /*
272d397712bSChris Mason  * this does all the record keeping required to make sure that a reference
273d397712bSChris Mason  * counted root is properly recorded in a given transaction.  This is required
274d397712bSChris Mason  * to make sure the old root from before we joined the transaction is deleted
275d397712bSChris Mason  * when the transaction commits
276d352ac68SChris Mason  */
2777585717fSChris Mason static int record_root_in_trans(struct btrfs_trans_handle *trans,
2785d4f98a2SYan Zheng 			       struct btrfs_root *root)
2796702ed49SChris Mason {
28027cdeb70SMiao Xie 	if (test_bit(BTRFS_ROOT_REF_COWS, &root->state) &&
28127cdeb70SMiao Xie 	    root->last_trans < trans->transid) {
2826702ed49SChris Mason 		WARN_ON(root == root->fs_info->extent_root);
2835d4f98a2SYan Zheng 		WARN_ON(root->commit_root != root->node);
2845d4f98a2SYan Zheng 
2857585717fSChris Mason 		/*
28627cdeb70SMiao Xie 		 * see below for IN_TRANS_SETUP usage rules
2877585717fSChris Mason 		 * we have the reloc mutex held now, so there
2887585717fSChris Mason 		 * is only one writer in this function
2897585717fSChris Mason 		 */
29027cdeb70SMiao Xie 		set_bit(BTRFS_ROOT_IN_TRANS_SETUP, &root->state);
2917585717fSChris Mason 
29227cdeb70SMiao Xie 		/* make sure readers find IN_TRANS_SETUP before
2937585717fSChris Mason 		 * they find our root->last_trans update
2947585717fSChris Mason 		 */
2957585717fSChris Mason 		smp_wmb();
2967585717fSChris Mason 
297a4abeea4SJosef Bacik 		spin_lock(&root->fs_info->fs_roots_radix_lock);
298a4abeea4SJosef Bacik 		if (root->last_trans == trans->transid) {
299a4abeea4SJosef Bacik 			spin_unlock(&root->fs_info->fs_roots_radix_lock);
300a4abeea4SJosef Bacik 			return 0;
301a4abeea4SJosef Bacik 		}
3026702ed49SChris Mason 		radix_tree_tag_set(&root->fs_info->fs_roots_radix,
3036702ed49SChris Mason 			   (unsigned long)root->root_key.objectid,
3046702ed49SChris Mason 			   BTRFS_ROOT_TRANS_TAG);
305a4abeea4SJosef Bacik 		spin_unlock(&root->fs_info->fs_roots_radix_lock);
3067585717fSChris Mason 		root->last_trans = trans->transid;
3077585717fSChris Mason 
3087585717fSChris Mason 		/* this is pretty tricky.  We don't want to
3097585717fSChris Mason 		 * take the relocation lock in btrfs_record_root_in_trans
3107585717fSChris Mason 		 * unless we're really doing the first setup for this root in
3117585717fSChris Mason 		 * this transaction.
3127585717fSChris Mason 		 *
3137585717fSChris Mason 		 * Normally we'd use root->last_trans as a flag to decide
3147585717fSChris Mason 		 * if we want to take the expensive mutex.
3157585717fSChris Mason 		 *
3167585717fSChris Mason 		 * But, we have to set root->last_trans before we
3177585717fSChris Mason 		 * init the relocation root, otherwise, we trip over warnings
3187585717fSChris Mason 		 * in ctree.c.  The solution used here is to flag ourselves
31927cdeb70SMiao Xie 		 * with root IN_TRANS_SETUP.  When this is 1, we're still
3207585717fSChris Mason 		 * fixing up the reloc trees and everyone must wait.
3217585717fSChris Mason 		 *
3227585717fSChris Mason 		 * When this is zero, they can trust root->last_trans and fly
3237585717fSChris Mason 		 * through btrfs_record_root_in_trans without having to take the
3247585717fSChris Mason 		 * lock.  smp_wmb() makes sure that all the writes above are
3257585717fSChris Mason 		 * done before we pop in the zero below
3267585717fSChris Mason 		 */
3275d4f98a2SYan Zheng 		btrfs_init_reloc_root(trans, root);
328c7548af6SChris Mason 		smp_mb__before_atomic();
32927cdeb70SMiao Xie 		clear_bit(BTRFS_ROOT_IN_TRANS_SETUP, &root->state);
3306702ed49SChris Mason 	}
3315d4f98a2SYan Zheng 	return 0;
3326702ed49SChris Mason }
3335d4f98a2SYan Zheng 
3347585717fSChris Mason 
3357585717fSChris Mason int btrfs_record_root_in_trans(struct btrfs_trans_handle *trans,
3367585717fSChris Mason 			       struct btrfs_root *root)
3377585717fSChris Mason {
33827cdeb70SMiao Xie 	if (!test_bit(BTRFS_ROOT_REF_COWS, &root->state))
3397585717fSChris Mason 		return 0;
3407585717fSChris Mason 
3417585717fSChris Mason 	/*
34227cdeb70SMiao Xie 	 * see record_root_in_trans for comments about IN_TRANS_SETUP usage
3437585717fSChris Mason 	 * and barriers
3447585717fSChris Mason 	 */
3457585717fSChris Mason 	smp_rmb();
3467585717fSChris Mason 	if (root->last_trans == trans->transid &&
34727cdeb70SMiao Xie 	    !test_bit(BTRFS_ROOT_IN_TRANS_SETUP, &root->state))
3487585717fSChris Mason 		return 0;
3497585717fSChris Mason 
3507585717fSChris Mason 	mutex_lock(&root->fs_info->reloc_mutex);
3517585717fSChris Mason 	record_root_in_trans(trans, root);
3527585717fSChris Mason 	mutex_unlock(&root->fs_info->reloc_mutex);
3537585717fSChris Mason 
3547585717fSChris Mason 	return 0;
3557585717fSChris Mason }
3567585717fSChris Mason 
3574a9d8bdeSMiao Xie static inline int is_transaction_blocked(struct btrfs_transaction *trans)
3584a9d8bdeSMiao Xie {
3594a9d8bdeSMiao Xie 	return (trans->state >= TRANS_STATE_BLOCKED &&
360501407aaSJosef Bacik 		trans->state < TRANS_STATE_UNBLOCKED &&
361501407aaSJosef Bacik 		!trans->aborted);
3624a9d8bdeSMiao Xie }
3634a9d8bdeSMiao Xie 
364d352ac68SChris Mason /* wait for commit against the current transaction to become unblocked
365d352ac68SChris Mason  * when this is done, it is safe to start a new transaction, but the current
366d352ac68SChris Mason  * transaction might not be fully on disk.
367d352ac68SChris Mason  */
36837d1aeeeSChris Mason static void wait_current_trans(struct btrfs_root *root)
36979154b1bSChris Mason {
370f9295749SChris Mason 	struct btrfs_transaction *cur_trans;
37179154b1bSChris Mason 
372a4abeea4SJosef Bacik 	spin_lock(&root->fs_info->trans_lock);
373f9295749SChris Mason 	cur_trans = root->fs_info->running_transaction;
3744a9d8bdeSMiao Xie 	if (cur_trans && is_transaction_blocked(cur_trans)) {
37513c5a93eSJosef Bacik 		atomic_inc(&cur_trans->use_count);
376a4abeea4SJosef Bacik 		spin_unlock(&root->fs_info->trans_lock);
37772d63ed6SLi Zefan 
37872d63ed6SLi Zefan 		wait_event(root->fs_info->transaction_wait,
379501407aaSJosef Bacik 			   cur_trans->state >= TRANS_STATE_UNBLOCKED ||
380501407aaSJosef Bacik 			   cur_trans->aborted);
381724e2315SJosef Bacik 		btrfs_put_transaction(cur_trans);
382a4abeea4SJosef Bacik 	} else {
383a4abeea4SJosef Bacik 		spin_unlock(&root->fs_info->trans_lock);
384f9295749SChris Mason 	}
38537d1aeeeSChris Mason }
38637d1aeeeSChris Mason 
387a22285a6SYan, Zheng static int may_wait_transaction(struct btrfs_root *root, int type)
38837d1aeeeSChris Mason {
389a4abeea4SJosef Bacik 	if (root->fs_info->log_root_recovering)
390a4abeea4SJosef Bacik 		return 0;
391a4abeea4SJosef Bacik 
392a4abeea4SJosef Bacik 	if (type == TRANS_USERSPACE)
393a22285a6SYan, Zheng 		return 1;
394a4abeea4SJosef Bacik 
395a4abeea4SJosef Bacik 	if (type == TRANS_START &&
396a4abeea4SJosef Bacik 	    !atomic_read(&root->fs_info->open_ioctl_trans))
397a4abeea4SJosef Bacik 		return 1;
398a4abeea4SJosef Bacik 
399a22285a6SYan, Zheng 	return 0;
400a22285a6SYan, Zheng }
401a22285a6SYan, Zheng 
40220dd2cbfSMiao Xie static inline bool need_reserve_reloc_root(struct btrfs_root *root)
40320dd2cbfSMiao Xie {
40420dd2cbfSMiao Xie 	if (!root->fs_info->reloc_ctl ||
40527cdeb70SMiao Xie 	    !test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
40620dd2cbfSMiao Xie 	    root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID ||
40720dd2cbfSMiao Xie 	    root->reloc_root)
40820dd2cbfSMiao Xie 		return false;
40920dd2cbfSMiao Xie 
41020dd2cbfSMiao Xie 	return true;
41120dd2cbfSMiao Xie }
41220dd2cbfSMiao Xie 
41308e007d2SMiao Xie static struct btrfs_trans_handle *
4140860adfdSMiao Xie start_transaction(struct btrfs_root *root, u64 num_items, unsigned int type,
41508e007d2SMiao Xie 		  enum btrfs_reserve_flush_enum flush)
416a22285a6SYan, Zheng {
417a22285a6SYan, Zheng 	struct btrfs_trans_handle *h;
418a22285a6SYan, Zheng 	struct btrfs_transaction *cur_trans;
419b5009945SJosef Bacik 	u64 num_bytes = 0;
420c5567237SArne Jansen 	u64 qgroup_reserved = 0;
42120dd2cbfSMiao Xie 	bool reloc_reserved = false;
42220dd2cbfSMiao Xie 	int ret;
423acce952bSliubo 
42446c4e71eSFilipe Manana 	/* Send isn't supposed to start transactions. */
4252755a0deSDavid Sterba 	ASSERT(current->journal_info != BTRFS_SEND_TRANS_STUB);
42646c4e71eSFilipe Manana 
42787533c47SMiao Xie 	if (test_bit(BTRFS_FS_STATE_ERROR, &root->fs_info->fs_state))
428acce952bSliubo 		return ERR_PTR(-EROFS);
4292a1eb461SJosef Bacik 
43046c4e71eSFilipe Manana 	if (current->journal_info) {
4310860adfdSMiao Xie 		WARN_ON(type & TRANS_EXTWRITERS);
4322a1eb461SJosef Bacik 		h = current->journal_info;
4332a1eb461SJosef Bacik 		h->use_count++;
434b7d5b0a8SMiao Xie 		WARN_ON(h->use_count > 2);
4352a1eb461SJosef Bacik 		h->orig_rsv = h->block_rsv;
4362a1eb461SJosef Bacik 		h->block_rsv = NULL;
4372a1eb461SJosef Bacik 		goto got_it;
4382a1eb461SJosef Bacik 	}
439b5009945SJosef Bacik 
440b5009945SJosef Bacik 	/*
441b5009945SJosef Bacik 	 * Do the reservation before we join the transaction so we can do all
442b5009945SJosef Bacik 	 * the appropriate flushing if need be.
443b5009945SJosef Bacik 	 */
444b5009945SJosef Bacik 	if (num_items > 0 && root != root->fs_info->chunk_root) {
445c5567237SArne Jansen 		if (root->fs_info->quota_enabled &&
446c5567237SArne Jansen 		    is_fstree(root->root_key.objectid)) {
447707e8a07SDavid Sterba 			qgroup_reserved = num_items * root->nodesize;
448c5567237SArne Jansen 			ret = btrfs_qgroup_reserve(root, qgroup_reserved);
449c5567237SArne Jansen 			if (ret)
450c5567237SArne Jansen 				return ERR_PTR(ret);
451c5567237SArne Jansen 		}
452c5567237SArne Jansen 
453b5009945SJosef Bacik 		num_bytes = btrfs_calc_trans_metadata_size(root, num_items);
45420dd2cbfSMiao Xie 		/*
45520dd2cbfSMiao Xie 		 * Do the reservation for the relocation root creation
45620dd2cbfSMiao Xie 		 */
457ee39b432SDavid Sterba 		if (need_reserve_reloc_root(root)) {
45820dd2cbfSMiao Xie 			num_bytes += root->nodesize;
45920dd2cbfSMiao Xie 			reloc_reserved = true;
46020dd2cbfSMiao Xie 		}
46120dd2cbfSMiao Xie 
4624a92b1b8SJosef Bacik 		ret = btrfs_block_rsv_add(root,
463b5009945SJosef Bacik 					  &root->fs_info->trans_block_rsv,
46408e007d2SMiao Xie 					  num_bytes, flush);
465b5009945SJosef Bacik 		if (ret)
466843fcf35SMiao Xie 			goto reserve_fail;
467b5009945SJosef Bacik 	}
468a22285a6SYan, Zheng again:
469a22285a6SYan, Zheng 	h = kmem_cache_alloc(btrfs_trans_handle_cachep, GFP_NOFS);
470843fcf35SMiao Xie 	if (!h) {
471843fcf35SMiao Xie 		ret = -ENOMEM;
472843fcf35SMiao Xie 		goto alloc_fail;
473843fcf35SMiao Xie 	}
474a22285a6SYan, Zheng 
47598114659SJosef Bacik 	/*
47698114659SJosef Bacik 	 * If we are JOIN_NOLOCK we're already committing a transaction and
47798114659SJosef Bacik 	 * waiting on this guy, so we don't need to do the sb_start_intwrite
47898114659SJosef Bacik 	 * because we're already holding a ref.  We need this because we could
47998114659SJosef Bacik 	 * have raced in and did an fsync() on a file which can kick a commit
48098114659SJosef Bacik 	 * and then we deadlock with somebody doing a freeze.
481354aa0fbSMiao Xie 	 *
482354aa0fbSMiao Xie 	 * If we are ATTACH, it means we just want to catch the current
483354aa0fbSMiao Xie 	 * transaction and commit it, so we needn't do sb_start_intwrite().
48498114659SJosef Bacik 	 */
4850860adfdSMiao Xie 	if (type & __TRANS_FREEZABLE)
486b2b5ef5cSJan Kara 		sb_start_intwrite(root->fs_info->sb);
487b2b5ef5cSJan Kara 
488a22285a6SYan, Zheng 	if (may_wait_transaction(root, type))
48937d1aeeeSChris Mason 		wait_current_trans(root);
490a22285a6SYan, Zheng 
491a4abeea4SJosef Bacik 	do {
492354aa0fbSMiao Xie 		ret = join_transaction(root, type);
493178260b2SMiao Xie 		if (ret == -EBUSY) {
494a4abeea4SJosef Bacik 			wait_current_trans(root);
495178260b2SMiao Xie 			if (unlikely(type == TRANS_ATTACH))
496178260b2SMiao Xie 				ret = -ENOENT;
497178260b2SMiao Xie 		}
498a4abeea4SJosef Bacik 	} while (ret == -EBUSY);
499a4abeea4SJosef Bacik 
500db5b493aSTsutomu Itoh 	if (ret < 0) {
501354aa0fbSMiao Xie 		/* We must get the transaction if we are JOIN_NOLOCK. */
502354aa0fbSMiao Xie 		BUG_ON(type == TRANS_JOIN_NOLOCK);
503843fcf35SMiao Xie 		goto join_fail;
504db5b493aSTsutomu Itoh 	}
5050f7d52f4SChris Mason 
506a22285a6SYan, Zheng 	cur_trans = root->fs_info->running_transaction;
507a22285a6SYan, Zheng 
508a22285a6SYan, Zheng 	h->transid = cur_trans->transid;
509a22285a6SYan, Zheng 	h->transaction = cur_trans;
51079154b1bSChris Mason 	h->blocks_used = 0;
511a22285a6SYan, Zheng 	h->bytes_reserved = 0;
512d13603efSArne Jansen 	h->root = root;
51356bec294SChris Mason 	h->delayed_ref_updates = 0;
5142a1eb461SJosef Bacik 	h->use_count = 1;
5150e721106SJosef Bacik 	h->adding_csums = 0;
516f0486c68SYan, Zheng 	h->block_rsv = NULL;
5172a1eb461SJosef Bacik 	h->orig_rsv = NULL;
51849b25e05SJeff Mahoney 	h->aborted = 0;
5194b824906SMiao Xie 	h->qgroup_reserved = 0;
520bed92eaeSArne Jansen 	h->delayed_ref_elem.seq = 0;
521a698d075SMiao Xie 	h->type = type;
522c6b305a8SJosef Bacik 	h->allocating_chunk = false;
52320dd2cbfSMiao Xie 	h->reloc_reserved = false;
5245039eddcSJosef Bacik 	h->sync = false;
525bed92eaeSArne Jansen 	INIT_LIST_HEAD(&h->qgroup_ref_list);
526ea658badSJosef Bacik 	INIT_LIST_HEAD(&h->new_bgs);
52750d9aa99SJosef Bacik 	INIT_LIST_HEAD(&h->ordered);
528b7ec40d7SChris Mason 
529a22285a6SYan, Zheng 	smp_mb();
5304a9d8bdeSMiao Xie 	if (cur_trans->state >= TRANS_STATE_BLOCKED &&
5314a9d8bdeSMiao Xie 	    may_wait_transaction(root, type)) {
532abdd2e80SFilipe Manana 		current->journal_info = h;
533a22285a6SYan, Zheng 		btrfs_commit_transaction(h, root);
534a22285a6SYan, Zheng 		goto again;
535a22285a6SYan, Zheng 	}
5369ed74f2dSJosef Bacik 
537b5009945SJosef Bacik 	if (num_bytes) {
5388c2a3ca2SJosef Bacik 		trace_btrfs_space_reservation(root->fs_info, "transaction",
5392bcc0328SLiu Bo 					      h->transid, num_bytes, 1);
540b5009945SJosef Bacik 		h->block_rsv = &root->fs_info->trans_block_rsv;
541b5009945SJosef Bacik 		h->bytes_reserved = num_bytes;
54220dd2cbfSMiao Xie 		h->reloc_reserved = reloc_reserved;
543a22285a6SYan, Zheng 	}
5444b824906SMiao Xie 	h->qgroup_reserved = qgroup_reserved;
545a22285a6SYan, Zheng 
5462a1eb461SJosef Bacik got_it:
547a4abeea4SJosef Bacik 	btrfs_record_root_in_trans(h, root);
548a22285a6SYan, Zheng 
549a22285a6SYan, Zheng 	if (!current->journal_info && type != TRANS_USERSPACE)
550a22285a6SYan, Zheng 		current->journal_info = h;
55179154b1bSChris Mason 	return h;
552843fcf35SMiao Xie 
553843fcf35SMiao Xie join_fail:
5540860adfdSMiao Xie 	if (type & __TRANS_FREEZABLE)
555843fcf35SMiao Xie 		sb_end_intwrite(root->fs_info->sb);
556843fcf35SMiao Xie 	kmem_cache_free(btrfs_trans_handle_cachep, h);
557843fcf35SMiao Xie alloc_fail:
558843fcf35SMiao Xie 	if (num_bytes)
559843fcf35SMiao Xie 		btrfs_block_rsv_release(root, &root->fs_info->trans_block_rsv,
560843fcf35SMiao Xie 					num_bytes);
561843fcf35SMiao Xie reserve_fail:
562843fcf35SMiao Xie 	if (qgroup_reserved)
563843fcf35SMiao Xie 		btrfs_qgroup_free(root, qgroup_reserved);
564843fcf35SMiao Xie 	return ERR_PTR(ret);
56579154b1bSChris Mason }
56679154b1bSChris Mason 
567f9295749SChris Mason struct btrfs_trans_handle *btrfs_start_transaction(struct btrfs_root *root,
568a22285a6SYan, Zheng 						   int num_items)
569f9295749SChris Mason {
57008e007d2SMiao Xie 	return start_transaction(root, num_items, TRANS_START,
57108e007d2SMiao Xie 				 BTRFS_RESERVE_FLUSH_ALL);
572f9295749SChris Mason }
5738407aa46SMiao Xie 
57408e007d2SMiao Xie struct btrfs_trans_handle *btrfs_start_transaction_lflush(
5758407aa46SMiao Xie 					struct btrfs_root *root, int num_items)
5768407aa46SMiao Xie {
57708e007d2SMiao Xie 	return start_transaction(root, num_items, TRANS_START,
57808e007d2SMiao Xie 				 BTRFS_RESERVE_FLUSH_LIMIT);
5798407aa46SMiao Xie }
5808407aa46SMiao Xie 
5817a7eaa40SJosef Bacik struct btrfs_trans_handle *btrfs_join_transaction(struct btrfs_root *root)
582f9295749SChris Mason {
5838407aa46SMiao Xie 	return start_transaction(root, 0, TRANS_JOIN, 0);
584f9295749SChris Mason }
585f9295749SChris Mason 
5867a7eaa40SJosef Bacik struct btrfs_trans_handle *btrfs_join_transaction_nolock(struct btrfs_root *root)
5870af3d00bSJosef Bacik {
5888407aa46SMiao Xie 	return start_transaction(root, 0, TRANS_JOIN_NOLOCK, 0);
5890af3d00bSJosef Bacik }
5900af3d00bSJosef Bacik 
5917a7eaa40SJosef Bacik struct btrfs_trans_handle *btrfs_start_ioctl_transaction(struct btrfs_root *root)
5929ca9ee09SSage Weil {
5938407aa46SMiao Xie 	return start_transaction(root, 0, TRANS_USERSPACE, 0);
5949ca9ee09SSage Weil }
5959ca9ee09SSage Weil 
596d4edf39bSMiao Xie /*
597d4edf39bSMiao Xie  * btrfs_attach_transaction() - catch the running transaction
598d4edf39bSMiao Xie  *
599d4edf39bSMiao Xie  * It is used when we want to commit the current the transaction, but
600d4edf39bSMiao Xie  * don't want to start a new one.
601d4edf39bSMiao Xie  *
602d4edf39bSMiao Xie  * Note: If this function return -ENOENT, it just means there is no
603d4edf39bSMiao Xie  * running transaction. But it is possible that the inactive transaction
604d4edf39bSMiao Xie  * is still in the memory, not fully on disk. If you hope there is no
605d4edf39bSMiao Xie  * inactive transaction in the fs when -ENOENT is returned, you should
606d4edf39bSMiao Xie  * invoke
607d4edf39bSMiao Xie  *     btrfs_attach_transaction_barrier()
608d4edf39bSMiao Xie  */
609354aa0fbSMiao Xie struct btrfs_trans_handle *btrfs_attach_transaction(struct btrfs_root *root)
61060376ce4SJosef Bacik {
611354aa0fbSMiao Xie 	return start_transaction(root, 0, TRANS_ATTACH, 0);
61260376ce4SJosef Bacik }
61360376ce4SJosef Bacik 
614d4edf39bSMiao Xie /*
61590b6d283SWang Sheng-Hui  * btrfs_attach_transaction_barrier() - catch the running transaction
616d4edf39bSMiao Xie  *
617d4edf39bSMiao Xie  * It is similar to the above function, the differentia is this one
618d4edf39bSMiao Xie  * will wait for all the inactive transactions until they fully
619d4edf39bSMiao Xie  * complete.
620d4edf39bSMiao Xie  */
621d4edf39bSMiao Xie struct btrfs_trans_handle *
622d4edf39bSMiao Xie btrfs_attach_transaction_barrier(struct btrfs_root *root)
623d4edf39bSMiao Xie {
624d4edf39bSMiao Xie 	struct btrfs_trans_handle *trans;
625d4edf39bSMiao Xie 
626d4edf39bSMiao Xie 	trans = start_transaction(root, 0, TRANS_ATTACH, 0);
627d4edf39bSMiao Xie 	if (IS_ERR(trans) && PTR_ERR(trans) == -ENOENT)
628d4edf39bSMiao Xie 		btrfs_wait_for_commit(root, 0);
629d4edf39bSMiao Xie 
630d4edf39bSMiao Xie 	return trans;
631d4edf39bSMiao Xie }
632d4edf39bSMiao Xie 
633d352ac68SChris Mason /* wait for a transaction commit to be fully complete */
634b9c8300cSLi Zefan static noinline void wait_for_commit(struct btrfs_root *root,
63589ce8a63SChris Mason 				    struct btrfs_transaction *commit)
63689ce8a63SChris Mason {
6374a9d8bdeSMiao Xie 	wait_event(commit->commit_wait, commit->state == TRANS_STATE_COMPLETED);
63889ce8a63SChris Mason }
63989ce8a63SChris Mason 
64046204592SSage Weil int btrfs_wait_for_commit(struct btrfs_root *root, u64 transid)
64146204592SSage Weil {
64246204592SSage Weil 	struct btrfs_transaction *cur_trans = NULL, *t;
6438cd2807fSMiao Xie 	int ret = 0;
64446204592SSage Weil 
64546204592SSage Weil 	if (transid) {
64646204592SSage Weil 		if (transid <= root->fs_info->last_trans_committed)
647a4abeea4SJosef Bacik 			goto out;
64846204592SSage Weil 
64946204592SSage Weil 		/* find specified transaction */
650a4abeea4SJosef Bacik 		spin_lock(&root->fs_info->trans_lock);
65146204592SSage Weil 		list_for_each_entry(t, &root->fs_info->trans_list, list) {
65246204592SSage Weil 			if (t->transid == transid) {
65346204592SSage Weil 				cur_trans = t;
654a4abeea4SJosef Bacik 				atomic_inc(&cur_trans->use_count);
6558cd2807fSMiao Xie 				ret = 0;
65646204592SSage Weil 				break;
65746204592SSage Weil 			}
6588cd2807fSMiao Xie 			if (t->transid > transid) {
6598cd2807fSMiao Xie 				ret = 0;
66046204592SSage Weil 				break;
66146204592SSage Weil 			}
6628cd2807fSMiao Xie 		}
663a4abeea4SJosef Bacik 		spin_unlock(&root->fs_info->trans_lock);
66442383020SSage Weil 
66542383020SSage Weil 		/*
66642383020SSage Weil 		 * The specified transaction doesn't exist, or we
66742383020SSage Weil 		 * raced with btrfs_commit_transaction
66842383020SSage Weil 		 */
66942383020SSage Weil 		if (!cur_trans) {
67042383020SSage Weil 			if (transid > root->fs_info->last_trans_committed)
67142383020SSage Weil 				ret = -EINVAL;
6728cd2807fSMiao Xie 			goto out;
67342383020SSage Weil 		}
67446204592SSage Weil 	} else {
67546204592SSage Weil 		/* find newest transaction that is committing | committed */
676a4abeea4SJosef Bacik 		spin_lock(&root->fs_info->trans_lock);
67746204592SSage Weil 		list_for_each_entry_reverse(t, &root->fs_info->trans_list,
67846204592SSage Weil 					    list) {
6794a9d8bdeSMiao Xie 			if (t->state >= TRANS_STATE_COMMIT_START) {
6804a9d8bdeSMiao Xie 				if (t->state == TRANS_STATE_COMPLETED)
6813473f3c0SJosef Bacik 					break;
68246204592SSage Weil 				cur_trans = t;
683a4abeea4SJosef Bacik 				atomic_inc(&cur_trans->use_count);
68446204592SSage Weil 				break;
68546204592SSage Weil 			}
68646204592SSage Weil 		}
687a4abeea4SJosef Bacik 		spin_unlock(&root->fs_info->trans_lock);
68846204592SSage Weil 		if (!cur_trans)
689a4abeea4SJosef Bacik 			goto out;  /* nothing committing|committed */
69046204592SSage Weil 	}
69146204592SSage Weil 
69246204592SSage Weil 	wait_for_commit(root, cur_trans);
693724e2315SJosef Bacik 	btrfs_put_transaction(cur_trans);
694a4abeea4SJosef Bacik out:
69546204592SSage Weil 	return ret;
69646204592SSage Weil }
69746204592SSage Weil 
69837d1aeeeSChris Mason void btrfs_throttle(struct btrfs_root *root)
69937d1aeeeSChris Mason {
700a4abeea4SJosef Bacik 	if (!atomic_read(&root->fs_info->open_ioctl_trans))
70137d1aeeeSChris Mason 		wait_current_trans(root);
70237d1aeeeSChris Mason }
70337d1aeeeSChris Mason 
7048929ecfaSYan, Zheng static int should_end_transaction(struct btrfs_trans_handle *trans,
7058929ecfaSYan, Zheng 				  struct btrfs_root *root)
7068929ecfaSYan, Zheng {
7071be41b78SJosef Bacik 	if (root->fs_info->global_block_rsv.space_info->full &&
7080a2b2a84SJosef Bacik 	    btrfs_check_space_for_delayed_refs(trans, root))
7091be41b78SJosef Bacik 		return 1;
71036ba022aSJosef Bacik 
7111be41b78SJosef Bacik 	return !!btrfs_block_rsv_check(root, &root->fs_info->global_block_rsv, 5);
7128929ecfaSYan, Zheng }
7138929ecfaSYan, Zheng 
7148929ecfaSYan, Zheng int btrfs_should_end_transaction(struct btrfs_trans_handle *trans,
7158929ecfaSYan, Zheng 				 struct btrfs_root *root)
7168929ecfaSYan, Zheng {
7178929ecfaSYan, Zheng 	struct btrfs_transaction *cur_trans = trans->transaction;
7188929ecfaSYan, Zheng 	int updates;
71949b25e05SJeff Mahoney 	int err;
7208929ecfaSYan, Zheng 
721a4abeea4SJosef Bacik 	smp_mb();
7224a9d8bdeSMiao Xie 	if (cur_trans->state >= TRANS_STATE_BLOCKED ||
7234a9d8bdeSMiao Xie 	    cur_trans->delayed_refs.flushing)
7248929ecfaSYan, Zheng 		return 1;
7258929ecfaSYan, Zheng 
7268929ecfaSYan, Zheng 	updates = trans->delayed_ref_updates;
7278929ecfaSYan, Zheng 	trans->delayed_ref_updates = 0;
72849b25e05SJeff Mahoney 	if (updates) {
72928ed1345SChris Mason 		err = btrfs_run_delayed_refs(trans, root, updates * 2);
73049b25e05SJeff Mahoney 		if (err) /* Error code will also eval true */
73149b25e05SJeff Mahoney 			return err;
73249b25e05SJeff Mahoney 	}
7338929ecfaSYan, Zheng 
7348929ecfaSYan, Zheng 	return should_end_transaction(trans, root);
7358929ecfaSYan, Zheng }
7368929ecfaSYan, Zheng 
73789ce8a63SChris Mason static int __btrfs_end_transaction(struct btrfs_trans_handle *trans,
738a698d075SMiao Xie 			  struct btrfs_root *root, int throttle)
73979154b1bSChris Mason {
7408929ecfaSYan, Zheng 	struct btrfs_transaction *cur_trans = trans->transaction;
741ab78c84dSChris Mason 	struct btrfs_fs_info *info = root->fs_info;
7421be41b78SJosef Bacik 	unsigned long cur = trans->delayed_ref_updates;
743a698d075SMiao Xie 	int lock = (trans->type != TRANS_JOIN_NOLOCK);
7444edc2ca3SDave Jones 	int err = 0;
745a79b7d4bSChris Mason 	int must_run_delayed_refs = 0;
746d6e4a428SChris Mason 
7473bbb24b2SJosef Bacik 	if (trans->use_count > 1) {
7483bbb24b2SJosef Bacik 		trans->use_count--;
7492a1eb461SJosef Bacik 		trans->block_rsv = trans->orig_rsv;
7502a1eb461SJosef Bacik 		return 0;
7512a1eb461SJosef Bacik 	}
7522a1eb461SJosef Bacik 
753b24e03dbSJosef Bacik 	btrfs_trans_release_metadata(trans, root);
7544c13d758SJosef Bacik 	trans->block_rsv = NULL;
755c5567237SArne Jansen 
756ea658badSJosef Bacik 	if (!list_empty(&trans->new_bgs))
757ea658badSJosef Bacik 		btrfs_create_pending_block_groups(trans, root);
758ea658badSJosef Bacik 
75950d9aa99SJosef Bacik 	if (!list_empty(&trans->ordered)) {
76050d9aa99SJosef Bacik 		spin_lock(&info->trans_lock);
76150d9aa99SJosef Bacik 		list_splice(&trans->ordered, &cur_trans->pending_ordered);
76250d9aa99SJosef Bacik 		spin_unlock(&info->trans_lock);
76350d9aa99SJosef Bacik 	}
76450d9aa99SJosef Bacik 
765c3e69d58SChris Mason 	trans->delayed_ref_updates = 0;
766a79b7d4bSChris Mason 	if (!trans->sync) {
767a79b7d4bSChris Mason 		must_run_delayed_refs =
768a79b7d4bSChris Mason 			btrfs_should_throttle_delayed_refs(trans, root);
7690a2b2a84SJosef Bacik 		cur = max_t(unsigned long, cur, 32);
770a79b7d4bSChris Mason 
771a79b7d4bSChris Mason 		/*
772a79b7d4bSChris Mason 		 * don't make the caller wait if they are from a NOLOCK
773a79b7d4bSChris Mason 		 * or ATTACH transaction, it will deadlock with commit
774a79b7d4bSChris Mason 		 */
775a79b7d4bSChris Mason 		if (must_run_delayed_refs == 1 &&
776a79b7d4bSChris Mason 		    (trans->type & (__TRANS_JOIN_NOLOCK | __TRANS_ATTACH)))
777a79b7d4bSChris Mason 			must_run_delayed_refs = 2;
77856bec294SChris Mason 	}
779bb721703SChris Mason 
780fcebe456SJosef Bacik 	if (trans->qgroup_reserved) {
781fcebe456SJosef Bacik 		/*
782fcebe456SJosef Bacik 		 * the same root has to be passed here between start_transaction
783fcebe456SJosef Bacik 		 * and end_transaction. Subvolume quota depends on this.
784fcebe456SJosef Bacik 		 */
785fcebe456SJosef Bacik 		btrfs_qgroup_free(trans->root, trans->qgroup_reserved);
786fcebe456SJosef Bacik 		trans->qgroup_reserved = 0;
787fcebe456SJosef Bacik 	}
788fcebe456SJosef Bacik 
7890e721106SJosef Bacik 	btrfs_trans_release_metadata(trans, root);
7900e721106SJosef Bacik 	trans->block_rsv = NULL;
79156bec294SChris Mason 
792ea658badSJosef Bacik 	if (!list_empty(&trans->new_bgs))
793ea658badSJosef Bacik 		btrfs_create_pending_block_groups(trans, root);
794ea658badSJosef Bacik 
795a4abeea4SJosef Bacik 	if (lock && !atomic_read(&root->fs_info->open_ioctl_trans) &&
7964a9d8bdeSMiao Xie 	    should_end_transaction(trans, root) &&
7974a9d8bdeSMiao Xie 	    ACCESS_ONCE(cur_trans->state) == TRANS_STATE_RUNNING) {
7984a9d8bdeSMiao Xie 		spin_lock(&info->trans_lock);
7994a9d8bdeSMiao Xie 		if (cur_trans->state == TRANS_STATE_RUNNING)
8004a9d8bdeSMiao Xie 			cur_trans->state = TRANS_STATE_BLOCKED;
8014a9d8bdeSMiao Xie 		spin_unlock(&info->trans_lock);
802a4abeea4SJosef Bacik 	}
8038929ecfaSYan, Zheng 
8044a9d8bdeSMiao Xie 	if (lock && ACCESS_ONCE(cur_trans->state) == TRANS_STATE_BLOCKED) {
8053bbb24b2SJosef Bacik 		if (throttle)
8068929ecfaSYan, Zheng 			return btrfs_commit_transaction(trans, root);
8073bbb24b2SJosef Bacik 		else
8088929ecfaSYan, Zheng 			wake_up_process(info->transaction_kthread);
8098929ecfaSYan, Zheng 	}
8108929ecfaSYan, Zheng 
8110860adfdSMiao Xie 	if (trans->type & __TRANS_FREEZABLE)
8126df7881aSJosef Bacik 		sb_end_intwrite(root->fs_info->sb);
8136df7881aSJosef Bacik 
8148929ecfaSYan, Zheng 	WARN_ON(cur_trans != info->running_transaction);
81513c5a93eSJosef Bacik 	WARN_ON(atomic_read(&cur_trans->num_writers) < 1);
81613c5a93eSJosef Bacik 	atomic_dec(&cur_trans->num_writers);
8170860adfdSMiao Xie 	extwriter_counter_dec(cur_trans, trans->type);
81889ce8a63SChris Mason 
81999d16cbcSSage Weil 	smp_mb();
82079154b1bSChris Mason 	if (waitqueue_active(&cur_trans->writer_wait))
82179154b1bSChris Mason 		wake_up(&cur_trans->writer_wait);
822724e2315SJosef Bacik 	btrfs_put_transaction(cur_trans);
8239ed74f2dSJosef Bacik 
8249ed74f2dSJosef Bacik 	if (current->journal_info == trans)
8259ed74f2dSJosef Bacik 		current->journal_info = NULL;
826ab78c84dSChris Mason 
82724bbcf04SYan, Zheng 	if (throttle)
82824bbcf04SYan, Zheng 		btrfs_run_delayed_iputs(root);
82924bbcf04SYan, Zheng 
83049b25e05SJeff Mahoney 	if (trans->aborted ||
8314e121c06SJosef Bacik 	    test_bit(BTRFS_FS_STATE_ERROR, &root->fs_info->fs_state)) {
8324e121c06SJosef Bacik 		wake_up_process(info->transaction_kthread);
8334edc2ca3SDave Jones 		err = -EIO;
8344e121c06SJosef Bacik 	}
835edf39272SJan Schmidt 	assert_qgroups_uptodate(trans);
83649b25e05SJeff Mahoney 
8374edc2ca3SDave Jones 	kmem_cache_free(btrfs_trans_handle_cachep, trans);
838a79b7d4bSChris Mason 	if (must_run_delayed_refs) {
839a79b7d4bSChris Mason 		btrfs_async_run_delayed_refs(root, cur,
840a79b7d4bSChris Mason 					     must_run_delayed_refs == 1);
841a79b7d4bSChris Mason 	}
8424edc2ca3SDave Jones 	return err;
84379154b1bSChris Mason }
84479154b1bSChris Mason 
84589ce8a63SChris Mason int btrfs_end_transaction(struct btrfs_trans_handle *trans,
84689ce8a63SChris Mason 			  struct btrfs_root *root)
84789ce8a63SChris Mason {
84898ad43beSWang Shilong 	return __btrfs_end_transaction(trans, root, 0);
84989ce8a63SChris Mason }
85089ce8a63SChris Mason 
85189ce8a63SChris Mason int btrfs_end_transaction_throttle(struct btrfs_trans_handle *trans,
85289ce8a63SChris Mason 				   struct btrfs_root *root)
85389ce8a63SChris Mason {
85498ad43beSWang Shilong 	return __btrfs_end_transaction(trans, root, 1);
85516cdcec7SMiao Xie }
85616cdcec7SMiao Xie 
857d352ac68SChris Mason /*
858d352ac68SChris Mason  * when btree blocks are allocated, they have some corresponding bits set for
859d352ac68SChris Mason  * them in one of two extent_io trees.  This is used to make sure all of
860690587d1SChris Mason  * those extents are sent to disk but does not wait on them
861d352ac68SChris Mason  */
862690587d1SChris Mason int btrfs_write_marked_extents(struct btrfs_root *root,
8638cef4e16SYan, Zheng 			       struct extent_io_tree *dirty_pages, int mark)
86479154b1bSChris Mason {
865777e6bd7SChris Mason 	int err = 0;
8667c4452b9SChris Mason 	int werr = 0;
8671728366eSJosef Bacik 	struct address_space *mapping = root->fs_info->btree_inode->i_mapping;
868e6138876SJosef Bacik 	struct extent_state *cached_state = NULL;
869777e6bd7SChris Mason 	u64 start = 0;
8705f39d397SChris Mason 	u64 end;
8717c4452b9SChris Mason 
8721728366eSJosef Bacik 	while (!find_first_extent_bit(dirty_pages, start, &start, &end,
873e6138876SJosef Bacik 				      mark, &cached_state)) {
874663dfbb0SFilipe Manana 		bool wait_writeback = false;
875663dfbb0SFilipe Manana 
876663dfbb0SFilipe Manana 		err = convert_extent_bit(dirty_pages, start, end,
877663dfbb0SFilipe Manana 					 EXTENT_NEED_WAIT,
878e6138876SJosef Bacik 					 mark, &cached_state, GFP_NOFS);
879663dfbb0SFilipe Manana 		/*
880663dfbb0SFilipe Manana 		 * convert_extent_bit can return -ENOMEM, which is most of the
881663dfbb0SFilipe Manana 		 * time a temporary error. So when it happens, ignore the error
882663dfbb0SFilipe Manana 		 * and wait for writeback of this range to finish - because we
883663dfbb0SFilipe Manana 		 * failed to set the bit EXTENT_NEED_WAIT for the range, a call
884663dfbb0SFilipe Manana 		 * to btrfs_wait_marked_extents() would not know that writeback
885663dfbb0SFilipe Manana 		 * for this range started and therefore wouldn't wait for it to
886663dfbb0SFilipe Manana 		 * finish - we don't want to commit a superblock that points to
887663dfbb0SFilipe Manana 		 * btree nodes/leafs for which writeback hasn't finished yet
888663dfbb0SFilipe Manana 		 * (and without errors).
889663dfbb0SFilipe Manana 		 * We cleanup any entries left in the io tree when committing
890663dfbb0SFilipe Manana 		 * the transaction (through clear_btree_io_tree()).
891663dfbb0SFilipe Manana 		 */
892663dfbb0SFilipe Manana 		if (err == -ENOMEM) {
893663dfbb0SFilipe Manana 			err = 0;
894663dfbb0SFilipe Manana 			wait_writeback = true;
895663dfbb0SFilipe Manana 		}
896663dfbb0SFilipe Manana 		if (!err)
8971728366eSJosef Bacik 			err = filemap_fdatawrite_range(mapping, start, end);
8987c4452b9SChris Mason 		if (err)
8997c4452b9SChris Mason 			werr = err;
900663dfbb0SFilipe Manana 		else if (wait_writeback)
901663dfbb0SFilipe Manana 			werr = filemap_fdatawait_range(mapping, start, end);
902e38e2ed7SFilipe Manana 		free_extent_state(cached_state);
903663dfbb0SFilipe Manana 		cached_state = NULL;
9041728366eSJosef Bacik 		cond_resched();
9051728366eSJosef Bacik 		start = end + 1;
9067c4452b9SChris Mason 	}
907690587d1SChris Mason 	return werr;
908690587d1SChris Mason }
909690587d1SChris Mason 
910690587d1SChris Mason /*
911690587d1SChris Mason  * when btree blocks are allocated, they have some corresponding bits set for
912690587d1SChris Mason  * them in one of two extent_io trees.  This is used to make sure all of
913690587d1SChris Mason  * those extents are on disk for transaction or log commit.  We wait
914690587d1SChris Mason  * on all the pages and clear them from the dirty pages state tree
915690587d1SChris Mason  */
916690587d1SChris Mason int btrfs_wait_marked_extents(struct btrfs_root *root,
9178cef4e16SYan, Zheng 			      struct extent_io_tree *dirty_pages, int mark)
918690587d1SChris Mason {
919690587d1SChris Mason 	int err = 0;
920690587d1SChris Mason 	int werr = 0;
9211728366eSJosef Bacik 	struct address_space *mapping = root->fs_info->btree_inode->i_mapping;
922e6138876SJosef Bacik 	struct extent_state *cached_state = NULL;
923690587d1SChris Mason 	u64 start = 0;
924690587d1SChris Mason 	u64 end;
925656f30dbSFilipe Manana 	struct btrfs_inode *btree_ino = BTRFS_I(root->fs_info->btree_inode);
926656f30dbSFilipe Manana 	bool errors = false;
927690587d1SChris Mason 
9281728366eSJosef Bacik 	while (!find_first_extent_bit(dirty_pages, start, &start, &end,
929e6138876SJosef Bacik 				      EXTENT_NEED_WAIT, &cached_state)) {
930663dfbb0SFilipe Manana 		/*
931663dfbb0SFilipe Manana 		 * Ignore -ENOMEM errors returned by clear_extent_bit().
932663dfbb0SFilipe Manana 		 * When committing the transaction, we'll remove any entries
933663dfbb0SFilipe Manana 		 * left in the io tree. For a log commit, we don't remove them
934663dfbb0SFilipe Manana 		 * after committing the log because the tree can be accessed
935663dfbb0SFilipe Manana 		 * concurrently - we do it only at transaction commit time when
936663dfbb0SFilipe Manana 		 * it's safe to do it (through clear_btree_io_tree()).
937663dfbb0SFilipe Manana 		 */
938663dfbb0SFilipe Manana 		err = clear_extent_bit(dirty_pages, start, end,
939663dfbb0SFilipe Manana 				       EXTENT_NEED_WAIT,
940e6138876SJosef Bacik 				       0, 0, &cached_state, GFP_NOFS);
941663dfbb0SFilipe Manana 		if (err == -ENOMEM)
942663dfbb0SFilipe Manana 			err = 0;
943663dfbb0SFilipe Manana 		if (!err)
9441728366eSJosef Bacik 			err = filemap_fdatawait_range(mapping, start, end);
945777e6bd7SChris Mason 		if (err)
946777e6bd7SChris Mason 			werr = err;
947e38e2ed7SFilipe Manana 		free_extent_state(cached_state);
948e38e2ed7SFilipe Manana 		cached_state = NULL;
949777e6bd7SChris Mason 		cond_resched();
9501728366eSJosef Bacik 		start = end + 1;
951777e6bd7SChris Mason 	}
9527c4452b9SChris Mason 	if (err)
9537c4452b9SChris Mason 		werr = err;
954656f30dbSFilipe Manana 
955656f30dbSFilipe Manana 	if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) {
956656f30dbSFilipe Manana 		if ((mark & EXTENT_DIRTY) &&
957656f30dbSFilipe Manana 		    test_and_clear_bit(BTRFS_INODE_BTREE_LOG1_ERR,
958656f30dbSFilipe Manana 				       &btree_ino->runtime_flags))
959656f30dbSFilipe Manana 			errors = true;
960656f30dbSFilipe Manana 
961656f30dbSFilipe Manana 		if ((mark & EXTENT_NEW) &&
962656f30dbSFilipe Manana 		    test_and_clear_bit(BTRFS_INODE_BTREE_LOG2_ERR,
963656f30dbSFilipe Manana 				       &btree_ino->runtime_flags))
964656f30dbSFilipe Manana 			errors = true;
965656f30dbSFilipe Manana 	} else {
966656f30dbSFilipe Manana 		if (test_and_clear_bit(BTRFS_INODE_BTREE_ERR,
967656f30dbSFilipe Manana 				       &btree_ino->runtime_flags))
968656f30dbSFilipe Manana 			errors = true;
969656f30dbSFilipe Manana 	}
970656f30dbSFilipe Manana 
971656f30dbSFilipe Manana 	if (errors && !werr)
972656f30dbSFilipe Manana 		werr = -EIO;
973656f30dbSFilipe Manana 
9747c4452b9SChris Mason 	return werr;
97579154b1bSChris Mason }
97679154b1bSChris Mason 
977690587d1SChris Mason /*
978690587d1SChris Mason  * when btree blocks are allocated, they have some corresponding bits set for
979690587d1SChris Mason  * them in one of two extent_io trees.  This is used to make sure all of
980690587d1SChris Mason  * those extents are on disk for transaction or log commit
981690587d1SChris Mason  */
982171170c1SSergei Trofimovich static int btrfs_write_and_wait_marked_extents(struct btrfs_root *root,
9838cef4e16SYan, Zheng 				struct extent_io_tree *dirty_pages, int mark)
984690587d1SChris Mason {
985690587d1SChris Mason 	int ret;
986690587d1SChris Mason 	int ret2;
987c6adc9ccSMiao Xie 	struct blk_plug plug;
988690587d1SChris Mason 
989c6adc9ccSMiao Xie 	blk_start_plug(&plug);
9908cef4e16SYan, Zheng 	ret = btrfs_write_marked_extents(root, dirty_pages, mark);
991c6adc9ccSMiao Xie 	blk_finish_plug(&plug);
9928cef4e16SYan, Zheng 	ret2 = btrfs_wait_marked_extents(root, dirty_pages, mark);
993bf0da8c1SChris Mason 
994bf0da8c1SChris Mason 	if (ret)
995bf0da8c1SChris Mason 		return ret;
996bf0da8c1SChris Mason 	if (ret2)
997bf0da8c1SChris Mason 		return ret2;
998bf0da8c1SChris Mason 	return 0;
999690587d1SChris Mason }
1000690587d1SChris Mason 
1001663dfbb0SFilipe Manana static int btrfs_write_and_wait_transaction(struct btrfs_trans_handle *trans,
1002d0c803c4SChris Mason 				     struct btrfs_root *root)
1003d0c803c4SChris Mason {
1004663dfbb0SFilipe Manana 	int ret;
1005663dfbb0SFilipe Manana 
1006663dfbb0SFilipe Manana 	ret = btrfs_write_and_wait_marked_extents(root,
10078cef4e16SYan, Zheng 					   &trans->transaction->dirty_pages,
10088cef4e16SYan, Zheng 					   EXTENT_DIRTY);
1009663dfbb0SFilipe Manana 	clear_btree_io_tree(&trans->transaction->dirty_pages);
1010663dfbb0SFilipe Manana 
1011663dfbb0SFilipe Manana 	return ret;
1012d0c803c4SChris Mason }
1013d0c803c4SChris Mason 
1014d352ac68SChris Mason /*
1015d352ac68SChris Mason  * this is used to update the root pointer in the tree of tree roots.
1016d352ac68SChris Mason  *
1017d352ac68SChris Mason  * But, in the case of the extent allocation tree, updating the root
1018d352ac68SChris Mason  * pointer may allocate blocks which may change the root of the extent
1019d352ac68SChris Mason  * allocation tree.
1020d352ac68SChris Mason  *
1021d352ac68SChris Mason  * So, this loops and repeats and makes sure the cowonly root didn't
1022d352ac68SChris Mason  * change while the root pointer was being updated in the metadata.
1023d352ac68SChris Mason  */
10240b86a832SChris Mason static int update_cowonly_root(struct btrfs_trans_handle *trans,
102579154b1bSChris Mason 			       struct btrfs_root *root)
102679154b1bSChris Mason {
102779154b1bSChris Mason 	int ret;
10280b86a832SChris Mason 	u64 old_root_bytenr;
102986b9f2ecSYan, Zheng 	u64 old_root_used;
10300b86a832SChris Mason 	struct btrfs_root *tree_root = root->fs_info->tree_root;
103179154b1bSChris Mason 
103286b9f2ecSYan, Zheng 	old_root_used = btrfs_root_used(&root->root_item);
103356bec294SChris Mason 
103479154b1bSChris Mason 	while (1) {
10350b86a832SChris Mason 		old_root_bytenr = btrfs_root_bytenr(&root->root_item);
103686b9f2ecSYan, Zheng 		if (old_root_bytenr == root->node->start &&
1037ea526d18SJosef Bacik 		    old_root_used == btrfs_root_used(&root->root_item))
103879154b1bSChris Mason 			break;
103987ef2bb4SChris Mason 
10405d4f98a2SYan Zheng 		btrfs_set_root_node(&root->root_item, root->node);
104179154b1bSChris Mason 		ret = btrfs_update_root(trans, tree_root,
10420b86a832SChris Mason 					&root->root_key,
10430b86a832SChris Mason 					&root->root_item);
104449b25e05SJeff Mahoney 		if (ret)
104549b25e05SJeff Mahoney 			return ret;
104656bec294SChris Mason 
104786b9f2ecSYan, Zheng 		old_root_used = btrfs_root_used(&root->root_item);
1048e7070be1SJosef Bacik 	}
1049276e680dSYan Zheng 
10500b86a832SChris Mason 	return 0;
10510b86a832SChris Mason }
10520b86a832SChris Mason 
1053d352ac68SChris Mason /*
1054d352ac68SChris Mason  * update all the cowonly tree roots on disk
105549b25e05SJeff Mahoney  *
105649b25e05SJeff Mahoney  * The error handling in this function may not be obvious. Any of the
105749b25e05SJeff Mahoney  * failures will cause the file system to go offline. We still need
105849b25e05SJeff Mahoney  * to clean up the delayed refs.
1059d352ac68SChris Mason  */
10605d4f98a2SYan Zheng static noinline int commit_cowonly_roots(struct btrfs_trans_handle *trans,
10610b86a832SChris Mason 					 struct btrfs_root *root)
10620b86a832SChris Mason {
10630b86a832SChris Mason 	struct btrfs_fs_info *fs_info = root->fs_info;
1064ea526d18SJosef Bacik 	struct list_head *dirty_bgs = &trans->transaction->dirty_bgs;
10651bbc621eSChris Mason 	struct list_head *io_bgs = &trans->transaction->io_bgs;
10660b86a832SChris Mason 	struct list_head *next;
106784234f3aSYan Zheng 	struct extent_buffer *eb;
106856bec294SChris Mason 	int ret;
106984234f3aSYan Zheng 
107084234f3aSYan Zheng 	eb = btrfs_lock_root_node(fs_info->tree_root);
107149b25e05SJeff Mahoney 	ret = btrfs_cow_block(trans, fs_info->tree_root, eb, NULL,
107249b25e05SJeff Mahoney 			      0, &eb);
107384234f3aSYan Zheng 	btrfs_tree_unlock(eb);
107484234f3aSYan Zheng 	free_extent_buffer(eb);
10750b86a832SChris Mason 
107649b25e05SJeff Mahoney 	if (ret)
107749b25e05SJeff Mahoney 		return ret;
107849b25e05SJeff Mahoney 
107956bec294SChris Mason 	ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
108049b25e05SJeff Mahoney 	if (ret)
108149b25e05SJeff Mahoney 		return ret;
108287ef2bb4SChris Mason 
1083733f4fbbSStefan Behrens 	ret = btrfs_run_dev_stats(trans, root->fs_info);
1084c16ce190SJosef Bacik 	if (ret)
1085c16ce190SJosef Bacik 		return ret;
10868dabb742SStefan Behrens 	ret = btrfs_run_dev_replace(trans, root->fs_info);
1087c16ce190SJosef Bacik 	if (ret)
1088c16ce190SJosef Bacik 		return ret;
1089546adb0dSJan Schmidt 	ret = btrfs_run_qgroups(trans, root->fs_info);
1090c16ce190SJosef Bacik 	if (ret)
1091c16ce190SJosef Bacik 		return ret;
1092546adb0dSJan Schmidt 
1093dcdf7f6dSJosef Bacik 	ret = btrfs_setup_space_cache(trans, root);
1094dcdf7f6dSJosef Bacik 	if (ret)
1095dcdf7f6dSJosef Bacik 		return ret;
1096dcdf7f6dSJosef Bacik 
1097546adb0dSJan Schmidt 	/* run_qgroups might have added some more refs */
1098546adb0dSJan Schmidt 	ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
1099c16ce190SJosef Bacik 	if (ret)
1100c16ce190SJosef Bacik 		return ret;
1101ea526d18SJosef Bacik again:
11020b86a832SChris Mason 	while (!list_empty(&fs_info->dirty_cowonly_roots)) {
11030b86a832SChris Mason 		next = fs_info->dirty_cowonly_roots.next;
11040b86a832SChris Mason 		list_del_init(next);
11050b86a832SChris Mason 		root = list_entry(next, struct btrfs_root, dirty_list);
1106e7070be1SJosef Bacik 		clear_bit(BTRFS_ROOT_DIRTY, &root->state);
110787ef2bb4SChris Mason 
11089e351cc8SJosef Bacik 		if (root != fs_info->extent_root)
11099e351cc8SJosef Bacik 			list_add_tail(&root->dirty_list,
11109e351cc8SJosef Bacik 				      &trans->transaction->switch_commits);
111149b25e05SJeff Mahoney 		ret = update_cowonly_root(trans, root);
111249b25e05SJeff Mahoney 		if (ret)
111349b25e05SJeff Mahoney 			return ret;
1114ea526d18SJosef Bacik 		ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
1115ea526d18SJosef Bacik 		if (ret)
1116ea526d18SJosef Bacik 			return ret;
111779154b1bSChris Mason 	}
1118276e680dSYan Zheng 
11191bbc621eSChris Mason 	while (!list_empty(dirty_bgs) || !list_empty(io_bgs)) {
1120ea526d18SJosef Bacik 		ret = btrfs_write_dirty_block_groups(trans, root);
1121ea526d18SJosef Bacik 		if (ret)
1122ea526d18SJosef Bacik 			return ret;
1123ea526d18SJosef Bacik 		ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
1124ea526d18SJosef Bacik 		if (ret)
1125ea526d18SJosef Bacik 			return ret;
1126ea526d18SJosef Bacik 	}
1127ea526d18SJosef Bacik 
1128ea526d18SJosef Bacik 	if (!list_empty(&fs_info->dirty_cowonly_roots))
1129ea526d18SJosef Bacik 		goto again;
1130ea526d18SJosef Bacik 
11319e351cc8SJosef Bacik 	list_add_tail(&fs_info->extent_root->dirty_list,
11329e351cc8SJosef Bacik 		      &trans->transaction->switch_commits);
11338dabb742SStefan Behrens 	btrfs_after_dev_replace_commit(fs_info);
11348dabb742SStefan Behrens 
113579154b1bSChris Mason 	return 0;
113679154b1bSChris Mason }
113779154b1bSChris Mason 
1138d352ac68SChris Mason /*
1139d352ac68SChris Mason  * dead roots are old snapshots that need to be deleted.  This allocates
1140d352ac68SChris Mason  * a dirty root struct and adds it into the list of dead roots that need to
1141d352ac68SChris Mason  * be deleted
1142d352ac68SChris Mason  */
1143cfad392bSJosef Bacik void btrfs_add_dead_root(struct btrfs_root *root)
11445eda7b5eSChris Mason {
1145a4abeea4SJosef Bacik 	spin_lock(&root->fs_info->trans_lock);
1146cfad392bSJosef Bacik 	if (list_empty(&root->root_list))
11479d1a2a3aSDavid Sterba 		list_add_tail(&root->root_list, &root->fs_info->dead_roots);
1148a4abeea4SJosef Bacik 	spin_unlock(&root->fs_info->trans_lock);
11495eda7b5eSChris Mason }
11505eda7b5eSChris Mason 
1151d352ac68SChris Mason /*
11525d4f98a2SYan Zheng  * update all the cowonly tree roots on disk
1153d352ac68SChris Mason  */
11545d4f98a2SYan Zheng static noinline int commit_fs_roots(struct btrfs_trans_handle *trans,
11555d4f98a2SYan Zheng 				    struct btrfs_root *root)
11560f7d52f4SChris Mason {
11570f7d52f4SChris Mason 	struct btrfs_root *gang[8];
11585d4f98a2SYan Zheng 	struct btrfs_fs_info *fs_info = root->fs_info;
11590f7d52f4SChris Mason 	int i;
11600f7d52f4SChris Mason 	int ret;
116154aa1f4dSChris Mason 	int err = 0;
116254aa1f4dSChris Mason 
1163a4abeea4SJosef Bacik 	spin_lock(&fs_info->fs_roots_radix_lock);
11640f7d52f4SChris Mason 	while (1) {
11655d4f98a2SYan Zheng 		ret = radix_tree_gang_lookup_tag(&fs_info->fs_roots_radix,
11665d4f98a2SYan Zheng 						 (void **)gang, 0,
11670f7d52f4SChris Mason 						 ARRAY_SIZE(gang),
11680f7d52f4SChris Mason 						 BTRFS_ROOT_TRANS_TAG);
11690f7d52f4SChris Mason 		if (ret == 0)
11700f7d52f4SChris Mason 			break;
11710f7d52f4SChris Mason 		for (i = 0; i < ret; i++) {
11720f7d52f4SChris Mason 			root = gang[i];
11735d4f98a2SYan Zheng 			radix_tree_tag_clear(&fs_info->fs_roots_radix,
11742619ba1fSChris Mason 					(unsigned long)root->root_key.objectid,
11750f7d52f4SChris Mason 					BTRFS_ROOT_TRANS_TAG);
1176a4abeea4SJosef Bacik 			spin_unlock(&fs_info->fs_roots_radix_lock);
117731153d81SYan Zheng 
1178e02119d5SChris Mason 			btrfs_free_log(trans, root);
11795d4f98a2SYan Zheng 			btrfs_update_reloc_root(trans, root);
1180d68fc57bSYan, Zheng 			btrfs_orphan_commit_root(trans, root);
1181e02119d5SChris Mason 
118282d5902dSLi Zefan 			btrfs_save_ino_cache(root, trans);
118382d5902dSLi Zefan 
1184f1ebcc74SLiu Bo 			/* see comments in should_cow_block() */
118527cdeb70SMiao Xie 			clear_bit(BTRFS_ROOT_FORCE_COW, &root->state);
1186c7548af6SChris Mason 			smp_mb__after_atomic();
1187f1ebcc74SLiu Bo 
1188978d910dSYan Zheng 			if (root->commit_root != root->node) {
11899e351cc8SJosef Bacik 				list_add_tail(&root->dirty_list,
11909e351cc8SJosef Bacik 					&trans->transaction->switch_commits);
1191978d910dSYan Zheng 				btrfs_set_root_node(&root->root_item,
1192978d910dSYan Zheng 						    root->node);
1193978d910dSYan Zheng 			}
119431153d81SYan Zheng 
11955d4f98a2SYan Zheng 			err = btrfs_update_root(trans, fs_info->tree_root,
11960f7d52f4SChris Mason 						&root->root_key,
11970f7d52f4SChris Mason 						&root->root_item);
1198a4abeea4SJosef Bacik 			spin_lock(&fs_info->fs_roots_radix_lock);
119954aa1f4dSChris Mason 			if (err)
120054aa1f4dSChris Mason 				break;
12010f7d52f4SChris Mason 		}
12029f3a7427SChris Mason 	}
1203a4abeea4SJosef Bacik 	spin_unlock(&fs_info->fs_roots_radix_lock);
120454aa1f4dSChris Mason 	return err;
12050f7d52f4SChris Mason }
12060f7d52f4SChris Mason 
1207d352ac68SChris Mason /*
1208de78b51aSEric Sandeen  * defrag a given btree.
1209de78b51aSEric Sandeen  * Every leaf in the btree is read and defragged.
1210d352ac68SChris Mason  */
1211de78b51aSEric Sandeen int btrfs_defrag_root(struct btrfs_root *root)
1212e9d0b13bSChris Mason {
1213e9d0b13bSChris Mason 	struct btrfs_fs_info *info = root->fs_info;
1214e9d0b13bSChris Mason 	struct btrfs_trans_handle *trans;
12158929ecfaSYan, Zheng 	int ret;
1216e9d0b13bSChris Mason 
121727cdeb70SMiao Xie 	if (test_and_set_bit(BTRFS_ROOT_DEFRAG_RUNNING, &root->state))
1218e9d0b13bSChris Mason 		return 0;
12198929ecfaSYan, Zheng 
12206b80053dSChris Mason 	while (1) {
12218929ecfaSYan, Zheng 		trans = btrfs_start_transaction(root, 0);
12228929ecfaSYan, Zheng 		if (IS_ERR(trans))
12238929ecfaSYan, Zheng 			return PTR_ERR(trans);
12248929ecfaSYan, Zheng 
1225de78b51aSEric Sandeen 		ret = btrfs_defrag_leaves(trans, root);
12268929ecfaSYan, Zheng 
1227e9d0b13bSChris Mason 		btrfs_end_transaction(trans, root);
1228b53d3f5dSLiu Bo 		btrfs_btree_balance_dirty(info->tree_root);
1229e9d0b13bSChris Mason 		cond_resched();
1230e9d0b13bSChris Mason 
12317841cb28SDavid Sterba 		if (btrfs_fs_closing(root->fs_info) || ret != -EAGAIN)
1232e9d0b13bSChris Mason 			break;
1233210549ebSDavid Sterba 
1234210549ebSDavid Sterba 		if (btrfs_defrag_cancelled(root->fs_info)) {
1235efe120a0SFrank Holton 			pr_debug("BTRFS: defrag_root cancelled\n");
1236210549ebSDavid Sterba 			ret = -EAGAIN;
1237210549ebSDavid Sterba 			break;
1238210549ebSDavid Sterba 		}
1239e9d0b13bSChris Mason 	}
124027cdeb70SMiao Xie 	clear_bit(BTRFS_ROOT_DEFRAG_RUNNING, &root->state);
12418929ecfaSYan, Zheng 	return ret;
1242e9d0b13bSChris Mason }
1243e9d0b13bSChris Mason 
1244d352ac68SChris Mason /*
1245d352ac68SChris Mason  * new snapshots need to be created at a very specific time in the
1246aec8030aSMiao Xie  * transaction commit.  This does the actual creation.
1247aec8030aSMiao Xie  *
1248aec8030aSMiao Xie  * Note:
1249aec8030aSMiao Xie  * If the error which may affect the commitment of the current transaction
1250aec8030aSMiao Xie  * happens, we should return the error number. If the error which just affect
1251aec8030aSMiao Xie  * the creation of the pending snapshots, just return 0.
1252d352ac68SChris Mason  */
125380b6794dSChris Mason static noinline int create_pending_snapshot(struct btrfs_trans_handle *trans,
12543063d29fSChris Mason 				   struct btrfs_fs_info *fs_info,
12553063d29fSChris Mason 				   struct btrfs_pending_snapshot *pending)
12563063d29fSChris Mason {
12573063d29fSChris Mason 	struct btrfs_key key;
125880b6794dSChris Mason 	struct btrfs_root_item *new_root_item;
12593063d29fSChris Mason 	struct btrfs_root *tree_root = fs_info->tree_root;
12603063d29fSChris Mason 	struct btrfs_root *root = pending->root;
12616bdb72deSSage Weil 	struct btrfs_root *parent_root;
126298c9942aSLiu Bo 	struct btrfs_block_rsv *rsv;
12636bdb72deSSage Weil 	struct inode *parent_inode;
126442874b3dSMiao Xie 	struct btrfs_path *path;
126542874b3dSMiao Xie 	struct btrfs_dir_item *dir_item;
1266a22285a6SYan, Zheng 	struct dentry *dentry;
12673063d29fSChris Mason 	struct extent_buffer *tmp;
1268925baeddSChris Mason 	struct extent_buffer *old;
12698ea05e3aSAlexander Block 	struct timespec cur_time = CURRENT_TIME;
1270aec8030aSMiao Xie 	int ret = 0;
1271d68fc57bSYan, Zheng 	u64 to_reserve = 0;
12726bdb72deSSage Weil 	u64 index = 0;
1273a22285a6SYan, Zheng 	u64 objectid;
1274b83cc969SLi Zefan 	u64 root_flags;
12758ea05e3aSAlexander Block 	uuid_le new_uuid;
12763063d29fSChris Mason 
127742874b3dSMiao Xie 	path = btrfs_alloc_path();
127842874b3dSMiao Xie 	if (!path) {
1279aec8030aSMiao Xie 		pending->error = -ENOMEM;
1280aec8030aSMiao Xie 		return 0;
128142874b3dSMiao Xie 	}
128242874b3dSMiao Xie 
128380b6794dSChris Mason 	new_root_item = kmalloc(sizeof(*new_root_item), GFP_NOFS);
128480b6794dSChris Mason 	if (!new_root_item) {
1285aec8030aSMiao Xie 		pending->error = -ENOMEM;
12866fa9700eSMiao Xie 		goto root_item_alloc_fail;
128780b6794dSChris Mason 	}
1288a22285a6SYan, Zheng 
1289aec8030aSMiao Xie 	pending->error = btrfs_find_free_objectid(tree_root, &objectid);
1290aec8030aSMiao Xie 	if (pending->error)
12916fa9700eSMiao Xie 		goto no_free_objectid;
12923063d29fSChris Mason 
12933fd0a558SYan, Zheng 	btrfs_reloc_pre_snapshot(trans, pending, &to_reserve);
1294d68fc57bSYan, Zheng 
1295d68fc57bSYan, Zheng 	if (to_reserve > 0) {
1296aec8030aSMiao Xie 		pending->error = btrfs_block_rsv_add(root,
1297aec8030aSMiao Xie 						     &pending->block_rsv,
129808e007d2SMiao Xie 						     to_reserve,
129908e007d2SMiao Xie 						     BTRFS_RESERVE_NO_FLUSH);
1300aec8030aSMiao Xie 		if (pending->error)
13016fa9700eSMiao Xie 			goto no_free_objectid;
1302d68fc57bSYan, Zheng 	}
1303d68fc57bSYan, Zheng 
13043063d29fSChris Mason 	key.objectid = objectid;
1305a22285a6SYan, Zheng 	key.offset = (u64)-1;
1306a22285a6SYan, Zheng 	key.type = BTRFS_ROOT_ITEM_KEY;
13073063d29fSChris Mason 
13086fa9700eSMiao Xie 	rsv = trans->block_rsv;
1309a22285a6SYan, Zheng 	trans->block_rsv = &pending->block_rsv;
13102382c5ccSLiu Bo 	trans->bytes_reserved = trans->block_rsv->reserved;
13116bdb72deSSage Weil 
1312a22285a6SYan, Zheng 	dentry = pending->dentry;
1313e9662f70SMiao Xie 	parent_inode = pending->dir;
1314a22285a6SYan, Zheng 	parent_root = BTRFS_I(parent_inode)->root;
13157585717fSChris Mason 	record_root_in_trans(trans, parent_root);
1316a22285a6SYan, Zheng 
13176bdb72deSSage Weil 	/*
13186bdb72deSSage Weil 	 * insert the directory item
13196bdb72deSSage Weil 	 */
13206bdb72deSSage Weil 	ret = btrfs_set_inode_index(parent_inode, &index);
132149b25e05SJeff Mahoney 	BUG_ON(ret); /* -ENOMEM */
132242874b3dSMiao Xie 
132342874b3dSMiao Xie 	/* check if there is a file/dir which has the same name. */
132442874b3dSMiao Xie 	dir_item = btrfs_lookup_dir_item(NULL, parent_root, path,
132542874b3dSMiao Xie 					 btrfs_ino(parent_inode),
132642874b3dSMiao Xie 					 dentry->d_name.name,
132742874b3dSMiao Xie 					 dentry->d_name.len, 0);
132842874b3dSMiao Xie 	if (dir_item != NULL && !IS_ERR(dir_item)) {
1329fe66a05aSChris Mason 		pending->error = -EEXIST;
1330aec8030aSMiao Xie 		goto dir_item_existed;
133142874b3dSMiao Xie 	} else if (IS_ERR(dir_item)) {
133242874b3dSMiao Xie 		ret = PTR_ERR(dir_item);
13338732d44fSMiao Xie 		btrfs_abort_transaction(trans, root, ret);
13348732d44fSMiao Xie 		goto fail;
133579787eaaSJeff Mahoney 	}
133642874b3dSMiao Xie 	btrfs_release_path(path);
13376bdb72deSSage Weil 
1338e999376fSChris Mason 	/*
1339e999376fSChris Mason 	 * pull in the delayed directory update
1340e999376fSChris Mason 	 * and the delayed inode item
1341e999376fSChris Mason 	 * otherwise we corrupt the FS during
1342e999376fSChris Mason 	 * snapshot
1343e999376fSChris Mason 	 */
1344e999376fSChris Mason 	ret = btrfs_run_delayed_items(trans, root);
13458732d44fSMiao Xie 	if (ret) {	/* Transaction aborted */
13468732d44fSMiao Xie 		btrfs_abort_transaction(trans, root, ret);
13478732d44fSMiao Xie 		goto fail;
13488732d44fSMiao Xie 	}
1349e999376fSChris Mason 
13507585717fSChris Mason 	record_root_in_trans(trans, root);
13516bdb72deSSage Weil 	btrfs_set_root_last_snapshot(&root->root_item, trans->transid);
13526bdb72deSSage Weil 	memcpy(new_root_item, &root->root_item, sizeof(*new_root_item));
135308fe4db1SLi Zefan 	btrfs_check_and_init_root_item(new_root_item);
13546bdb72deSSage Weil 
1355b83cc969SLi Zefan 	root_flags = btrfs_root_flags(new_root_item);
1356b83cc969SLi Zefan 	if (pending->readonly)
1357b83cc969SLi Zefan 		root_flags |= BTRFS_ROOT_SUBVOL_RDONLY;
1358b83cc969SLi Zefan 	else
1359b83cc969SLi Zefan 		root_flags &= ~BTRFS_ROOT_SUBVOL_RDONLY;
1360b83cc969SLi Zefan 	btrfs_set_root_flags(new_root_item, root_flags);
1361b83cc969SLi Zefan 
13628ea05e3aSAlexander Block 	btrfs_set_root_generation_v2(new_root_item,
13638ea05e3aSAlexander Block 			trans->transid);
13648ea05e3aSAlexander Block 	uuid_le_gen(&new_uuid);
13658ea05e3aSAlexander Block 	memcpy(new_root_item->uuid, new_uuid.b, BTRFS_UUID_SIZE);
13668ea05e3aSAlexander Block 	memcpy(new_root_item->parent_uuid, root->root_item.uuid,
13678ea05e3aSAlexander Block 			BTRFS_UUID_SIZE);
136870023da2SStefan Behrens 	if (!(root_flags & BTRFS_ROOT_SUBVOL_RDONLY)) {
136970023da2SStefan Behrens 		memset(new_root_item->received_uuid, 0,
137070023da2SStefan Behrens 		       sizeof(new_root_item->received_uuid));
13718ea05e3aSAlexander Block 		memset(&new_root_item->stime, 0, sizeof(new_root_item->stime));
13728ea05e3aSAlexander Block 		memset(&new_root_item->rtime, 0, sizeof(new_root_item->rtime));
13738ea05e3aSAlexander Block 		btrfs_set_root_stransid(new_root_item, 0);
13748ea05e3aSAlexander Block 		btrfs_set_root_rtransid(new_root_item, 0);
137570023da2SStefan Behrens 	}
13763cae210fSQu Wenruo 	btrfs_set_stack_timespec_sec(&new_root_item->otime, cur_time.tv_sec);
13773cae210fSQu Wenruo 	btrfs_set_stack_timespec_nsec(&new_root_item->otime, cur_time.tv_nsec);
137870023da2SStefan Behrens 	btrfs_set_root_otransid(new_root_item, trans->transid);
13798ea05e3aSAlexander Block 
1380925baeddSChris Mason 	old = btrfs_lock_root_node(root);
138149b25e05SJeff Mahoney 	ret = btrfs_cow_block(trans, root, old, NULL, 0, &old);
138279787eaaSJeff Mahoney 	if (ret) {
138379787eaaSJeff Mahoney 		btrfs_tree_unlock(old);
138479787eaaSJeff Mahoney 		free_extent_buffer(old);
13858732d44fSMiao Xie 		btrfs_abort_transaction(trans, root, ret);
13868732d44fSMiao Xie 		goto fail;
138779787eaaSJeff Mahoney 	}
138849b25e05SJeff Mahoney 
13895d4f98a2SYan Zheng 	btrfs_set_lock_blocking(old);
13903063d29fSChris Mason 
139149b25e05SJeff Mahoney 	ret = btrfs_copy_root(trans, root, old, &tmp, objectid);
139279787eaaSJeff Mahoney 	/* clean up in any case */
1393925baeddSChris Mason 	btrfs_tree_unlock(old);
1394925baeddSChris Mason 	free_extent_buffer(old);
13958732d44fSMiao Xie 	if (ret) {
13968732d44fSMiao Xie 		btrfs_abort_transaction(trans, root, ret);
13978732d44fSMiao Xie 		goto fail;
13988732d44fSMiao Xie 	}
13993063d29fSChris Mason 
1400fcebe456SJosef Bacik 	/*
1401fcebe456SJosef Bacik 	 * We need to flush delayed refs in order to make sure all of our quota
1402fcebe456SJosef Bacik 	 * operations have been done before we call btrfs_qgroup_inherit.
1403fcebe456SJosef Bacik 	 */
1404fcebe456SJosef Bacik 	ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
1405fcebe456SJosef Bacik 	if (ret) {
1406fcebe456SJosef Bacik 		btrfs_abort_transaction(trans, root, ret);
1407fcebe456SJosef Bacik 		goto fail;
1408fcebe456SJosef Bacik 	}
1409fcebe456SJosef Bacik 
141047a306a7SEric Sandeen 	ret = btrfs_qgroup_inherit(trans, fs_info,
1411fcebe456SJosef Bacik 				   root->root_key.objectid,
1412fcebe456SJosef Bacik 				   objectid, pending->inherit);
141347a306a7SEric Sandeen 	if (ret) {
141447a306a7SEric Sandeen 		btrfs_abort_transaction(trans, root, ret);
141547a306a7SEric Sandeen 		goto fail;
141647a306a7SEric Sandeen 	}
1417fcebe456SJosef Bacik 
1418f1ebcc74SLiu Bo 	/* see comments in should_cow_block() */
141927cdeb70SMiao Xie 	set_bit(BTRFS_ROOT_FORCE_COW, &root->state);
1420f1ebcc74SLiu Bo 	smp_wmb();
1421f1ebcc74SLiu Bo 
14225d4f98a2SYan Zheng 	btrfs_set_root_node(new_root_item, tmp);
1423a22285a6SYan, Zheng 	/* record when the snapshot was created in key.offset */
1424a22285a6SYan, Zheng 	key.offset = trans->transid;
1425a22285a6SYan, Zheng 	ret = btrfs_insert_root(trans, tree_root, &key, new_root_item);
1426925baeddSChris Mason 	btrfs_tree_unlock(tmp);
14273063d29fSChris Mason 	free_extent_buffer(tmp);
14288732d44fSMiao Xie 	if (ret) {
14298732d44fSMiao Xie 		btrfs_abort_transaction(trans, root, ret);
14308732d44fSMiao Xie 		goto fail;
14318732d44fSMiao Xie 	}
14320660b5afSChris Mason 
1433a22285a6SYan, Zheng 	/*
1434a22285a6SYan, Zheng 	 * insert root back/forward references
1435a22285a6SYan, Zheng 	 */
1436a22285a6SYan, Zheng 	ret = btrfs_add_root_ref(trans, tree_root, objectid,
1437a22285a6SYan, Zheng 				 parent_root->root_key.objectid,
143833345d01SLi Zefan 				 btrfs_ino(parent_inode), index,
1439a22285a6SYan, Zheng 				 dentry->d_name.name, dentry->d_name.len);
14408732d44fSMiao Xie 	if (ret) {
14418732d44fSMiao Xie 		btrfs_abort_transaction(trans, root, ret);
14428732d44fSMiao Xie 		goto fail;
14438732d44fSMiao Xie 	}
1444a22285a6SYan, Zheng 
1445a22285a6SYan, Zheng 	key.offset = (u64)-1;
1446a22285a6SYan, Zheng 	pending->snap = btrfs_read_fs_root_no_name(root->fs_info, &key);
144779787eaaSJeff Mahoney 	if (IS_ERR(pending->snap)) {
144879787eaaSJeff Mahoney 		ret = PTR_ERR(pending->snap);
14498732d44fSMiao Xie 		btrfs_abort_transaction(trans, root, ret);
14508732d44fSMiao Xie 		goto fail;
145179787eaaSJeff Mahoney 	}
1452d68fc57bSYan, Zheng 
145349b25e05SJeff Mahoney 	ret = btrfs_reloc_post_snapshot(trans, pending);
14548732d44fSMiao Xie 	if (ret) {
14558732d44fSMiao Xie 		btrfs_abort_transaction(trans, root, ret);
14568732d44fSMiao Xie 		goto fail;
14578732d44fSMiao Xie 	}
1458361048f5SMiao Xie 
1459361048f5SMiao Xie 	ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
14608732d44fSMiao Xie 	if (ret) {
14618732d44fSMiao Xie 		btrfs_abort_transaction(trans, root, ret);
14628732d44fSMiao Xie 		goto fail;
14638732d44fSMiao Xie 	}
146442874b3dSMiao Xie 
146542874b3dSMiao Xie 	ret = btrfs_insert_dir_item(trans, parent_root,
146642874b3dSMiao Xie 				    dentry->d_name.name, dentry->d_name.len,
146742874b3dSMiao Xie 				    parent_inode, &key,
146842874b3dSMiao Xie 				    BTRFS_FT_DIR, index);
146942874b3dSMiao Xie 	/* We have check then name at the beginning, so it is impossible. */
14709c52057cSChris Mason 	BUG_ON(ret == -EEXIST || ret == -EOVERFLOW);
14718732d44fSMiao Xie 	if (ret) {
14728732d44fSMiao Xie 		btrfs_abort_transaction(trans, root, ret);
14738732d44fSMiao Xie 		goto fail;
14748732d44fSMiao Xie 	}
147542874b3dSMiao Xie 
147642874b3dSMiao Xie 	btrfs_i_size_write(parent_inode, parent_inode->i_size +
147742874b3dSMiao Xie 					 dentry->d_name.len * 2);
147842874b3dSMiao Xie 	parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
1479be6aef60SJosef Bacik 	ret = btrfs_update_inode_fallback(trans, parent_root, parent_inode);
1480dd5f9615SStefan Behrens 	if (ret) {
14818732d44fSMiao Xie 		btrfs_abort_transaction(trans, root, ret);
1482dd5f9615SStefan Behrens 		goto fail;
1483dd5f9615SStefan Behrens 	}
1484dd5f9615SStefan Behrens 	ret = btrfs_uuid_tree_add(trans, fs_info->uuid_root, new_uuid.b,
1485dd5f9615SStefan Behrens 				  BTRFS_UUID_KEY_SUBVOL, objectid);
1486dd5f9615SStefan Behrens 	if (ret) {
1487dd5f9615SStefan Behrens 		btrfs_abort_transaction(trans, root, ret);
1488dd5f9615SStefan Behrens 		goto fail;
1489dd5f9615SStefan Behrens 	}
1490dd5f9615SStefan Behrens 	if (!btrfs_is_empty_uuid(new_root_item->received_uuid)) {
1491dd5f9615SStefan Behrens 		ret = btrfs_uuid_tree_add(trans, fs_info->uuid_root,
1492dd5f9615SStefan Behrens 					  new_root_item->received_uuid,
1493dd5f9615SStefan Behrens 					  BTRFS_UUID_KEY_RECEIVED_SUBVOL,
1494dd5f9615SStefan Behrens 					  objectid);
1495dd5f9615SStefan Behrens 		if (ret && ret != -EEXIST) {
1496dd5f9615SStefan Behrens 			btrfs_abort_transaction(trans, root, ret);
1497dd5f9615SStefan Behrens 			goto fail;
1498dd5f9615SStefan Behrens 		}
1499dd5f9615SStefan Behrens 	}
15003063d29fSChris Mason fail:
1501aec8030aSMiao Xie 	pending->error = ret;
1502aec8030aSMiao Xie dir_item_existed:
150398c9942aSLiu Bo 	trans->block_rsv = rsv;
15042382c5ccSLiu Bo 	trans->bytes_reserved = 0;
15056fa9700eSMiao Xie no_free_objectid:
15066fa9700eSMiao Xie 	kfree(new_root_item);
15076fa9700eSMiao Xie root_item_alloc_fail:
150842874b3dSMiao Xie 	btrfs_free_path(path);
150949b25e05SJeff Mahoney 	return ret;
15103063d29fSChris Mason }
15113063d29fSChris Mason 
1512d352ac68SChris Mason /*
1513d352ac68SChris Mason  * create all the snapshots we've scheduled for creation
1514d352ac68SChris Mason  */
151580b6794dSChris Mason static noinline int create_pending_snapshots(struct btrfs_trans_handle *trans,
15163063d29fSChris Mason 					     struct btrfs_fs_info *fs_info)
15173063d29fSChris Mason {
1518aec8030aSMiao Xie 	struct btrfs_pending_snapshot *pending, *next;
15193063d29fSChris Mason 	struct list_head *head = &trans->transaction->pending_snapshots;
1520aec8030aSMiao Xie 	int ret = 0;
15213de4586cSChris Mason 
1522aec8030aSMiao Xie 	list_for_each_entry_safe(pending, next, head, list) {
1523aec8030aSMiao Xie 		list_del(&pending->list);
1524aec8030aSMiao Xie 		ret = create_pending_snapshot(trans, fs_info, pending);
1525aec8030aSMiao Xie 		if (ret)
1526aec8030aSMiao Xie 			break;
1527aec8030aSMiao Xie 	}
1528aec8030aSMiao Xie 	return ret;
15293de4586cSChris Mason }
15303de4586cSChris Mason 
15315d4f98a2SYan Zheng static void update_super_roots(struct btrfs_root *root)
15325d4f98a2SYan Zheng {
15335d4f98a2SYan Zheng 	struct btrfs_root_item *root_item;
15345d4f98a2SYan Zheng 	struct btrfs_super_block *super;
15355d4f98a2SYan Zheng 
15366c41761fSDavid Sterba 	super = root->fs_info->super_copy;
15375d4f98a2SYan Zheng 
15385d4f98a2SYan Zheng 	root_item = &root->fs_info->chunk_root->root_item;
15395d4f98a2SYan Zheng 	super->chunk_root = root_item->bytenr;
15405d4f98a2SYan Zheng 	super->chunk_root_generation = root_item->generation;
15415d4f98a2SYan Zheng 	super->chunk_root_level = root_item->level;
15425d4f98a2SYan Zheng 
15435d4f98a2SYan Zheng 	root_item = &root->fs_info->tree_root->root_item;
15445d4f98a2SYan Zheng 	super->root = root_item->bytenr;
15455d4f98a2SYan Zheng 	super->generation = root_item->generation;
15465d4f98a2SYan Zheng 	super->root_level = root_item->level;
154773bc1876SJosef Bacik 	if (btrfs_test_opt(root, SPACE_CACHE))
15480af3d00bSJosef Bacik 		super->cache_generation = root_item->generation;
154970f80175SStefan Behrens 	if (root->fs_info->update_uuid_tree_gen)
155026432799SStefan Behrens 		super->uuid_tree_generation = root_item->generation;
15515d4f98a2SYan Zheng }
15525d4f98a2SYan Zheng 
1553f36f3042SChris Mason int btrfs_transaction_in_commit(struct btrfs_fs_info *info)
1554f36f3042SChris Mason {
15554a9d8bdeSMiao Xie 	struct btrfs_transaction *trans;
1556f36f3042SChris Mason 	int ret = 0;
15574a9d8bdeSMiao Xie 
1558a4abeea4SJosef Bacik 	spin_lock(&info->trans_lock);
15594a9d8bdeSMiao Xie 	trans = info->running_transaction;
15604a9d8bdeSMiao Xie 	if (trans)
15614a9d8bdeSMiao Xie 		ret = (trans->state >= TRANS_STATE_COMMIT_START);
1562a4abeea4SJosef Bacik 	spin_unlock(&info->trans_lock);
1563f36f3042SChris Mason 	return ret;
1564f36f3042SChris Mason }
1565f36f3042SChris Mason 
15668929ecfaSYan, Zheng int btrfs_transaction_blocked(struct btrfs_fs_info *info)
15678929ecfaSYan, Zheng {
15684a9d8bdeSMiao Xie 	struct btrfs_transaction *trans;
15698929ecfaSYan, Zheng 	int ret = 0;
15704a9d8bdeSMiao Xie 
1571a4abeea4SJosef Bacik 	spin_lock(&info->trans_lock);
15724a9d8bdeSMiao Xie 	trans = info->running_transaction;
15734a9d8bdeSMiao Xie 	if (trans)
15744a9d8bdeSMiao Xie 		ret = is_transaction_blocked(trans);
1575a4abeea4SJosef Bacik 	spin_unlock(&info->trans_lock);
15768929ecfaSYan, Zheng 	return ret;
15778929ecfaSYan, Zheng }
15788929ecfaSYan, Zheng 
1579bb9c12c9SSage Weil /*
1580bb9c12c9SSage Weil  * wait for the current transaction commit to start and block subsequent
1581bb9c12c9SSage Weil  * transaction joins
1582bb9c12c9SSage Weil  */
1583bb9c12c9SSage Weil static void wait_current_trans_commit_start(struct btrfs_root *root,
1584bb9c12c9SSage Weil 					    struct btrfs_transaction *trans)
1585bb9c12c9SSage Weil {
15864a9d8bdeSMiao Xie 	wait_event(root->fs_info->transaction_blocked_wait,
1587501407aaSJosef Bacik 		   trans->state >= TRANS_STATE_COMMIT_START ||
1588501407aaSJosef Bacik 		   trans->aborted);
1589bb9c12c9SSage Weil }
1590bb9c12c9SSage Weil 
1591bb9c12c9SSage Weil /*
1592bb9c12c9SSage Weil  * wait for the current transaction to start and then become unblocked.
1593bb9c12c9SSage Weil  * caller holds ref.
1594bb9c12c9SSage Weil  */
1595bb9c12c9SSage Weil static void wait_current_trans_commit_start_and_unblock(struct btrfs_root *root,
1596bb9c12c9SSage Weil 					 struct btrfs_transaction *trans)
1597bb9c12c9SSage Weil {
159872d63ed6SLi Zefan 	wait_event(root->fs_info->transaction_wait,
1599501407aaSJosef Bacik 		   trans->state >= TRANS_STATE_UNBLOCKED ||
1600501407aaSJosef Bacik 		   trans->aborted);
1601bb9c12c9SSage Weil }
1602bb9c12c9SSage Weil 
1603bb9c12c9SSage Weil /*
1604bb9c12c9SSage Weil  * commit transactions asynchronously. once btrfs_commit_transaction_async
1605bb9c12c9SSage Weil  * returns, any subsequent transaction will not be allowed to join.
1606bb9c12c9SSage Weil  */
1607bb9c12c9SSage Weil struct btrfs_async_commit {
1608bb9c12c9SSage Weil 	struct btrfs_trans_handle *newtrans;
1609bb9c12c9SSage Weil 	struct btrfs_root *root;
16107892b5afSMiao Xie 	struct work_struct work;
1611bb9c12c9SSage Weil };
1612bb9c12c9SSage Weil 
1613bb9c12c9SSage Weil static void do_async_commit(struct work_struct *work)
1614bb9c12c9SSage Weil {
1615bb9c12c9SSage Weil 	struct btrfs_async_commit *ac =
16167892b5afSMiao Xie 		container_of(work, struct btrfs_async_commit, work);
1617bb9c12c9SSage Weil 
16186fc4e354SSage Weil 	/*
16196fc4e354SSage Weil 	 * We've got freeze protection passed with the transaction.
16206fc4e354SSage Weil 	 * Tell lockdep about it.
16216fc4e354SSage Weil 	 */
1622b1a06a4bSLiu Bo 	if (ac->newtrans->type & __TRANS_FREEZABLE)
16236fc4e354SSage Weil 		rwsem_acquire_read(
16246fc4e354SSage Weil 		     &ac->root->fs_info->sb->s_writers.lock_map[SB_FREEZE_FS-1],
16256fc4e354SSage Weil 		     0, 1, _THIS_IP_);
16266fc4e354SSage Weil 
1627e209db7aSSage Weil 	current->journal_info = ac->newtrans;
1628e209db7aSSage Weil 
1629bb9c12c9SSage Weil 	btrfs_commit_transaction(ac->newtrans, ac->root);
1630bb9c12c9SSage Weil 	kfree(ac);
1631bb9c12c9SSage Weil }
1632bb9c12c9SSage Weil 
1633bb9c12c9SSage Weil int btrfs_commit_transaction_async(struct btrfs_trans_handle *trans,
1634bb9c12c9SSage Weil 				   struct btrfs_root *root,
1635bb9c12c9SSage Weil 				   int wait_for_unblock)
1636bb9c12c9SSage Weil {
1637bb9c12c9SSage Weil 	struct btrfs_async_commit *ac;
1638bb9c12c9SSage Weil 	struct btrfs_transaction *cur_trans;
1639bb9c12c9SSage Weil 
1640bb9c12c9SSage Weil 	ac = kmalloc(sizeof(*ac), GFP_NOFS);
1641db5b493aSTsutomu Itoh 	if (!ac)
1642db5b493aSTsutomu Itoh 		return -ENOMEM;
1643bb9c12c9SSage Weil 
16447892b5afSMiao Xie 	INIT_WORK(&ac->work, do_async_commit);
1645bb9c12c9SSage Weil 	ac->root = root;
16467a7eaa40SJosef Bacik 	ac->newtrans = btrfs_join_transaction(root);
16473612b495STsutomu Itoh 	if (IS_ERR(ac->newtrans)) {
16483612b495STsutomu Itoh 		int err = PTR_ERR(ac->newtrans);
16493612b495STsutomu Itoh 		kfree(ac);
16503612b495STsutomu Itoh 		return err;
16513612b495STsutomu Itoh 	}
1652bb9c12c9SSage Weil 
1653bb9c12c9SSage Weil 	/* take transaction reference */
1654bb9c12c9SSage Weil 	cur_trans = trans->transaction;
165513c5a93eSJosef Bacik 	atomic_inc(&cur_trans->use_count);
1656bb9c12c9SSage Weil 
1657bb9c12c9SSage Weil 	btrfs_end_transaction(trans, root);
16586fc4e354SSage Weil 
16596fc4e354SSage Weil 	/*
16606fc4e354SSage Weil 	 * Tell lockdep we've released the freeze rwsem, since the
16616fc4e354SSage Weil 	 * async commit thread will be the one to unlock it.
16626fc4e354SSage Weil 	 */
1663b1a06a4bSLiu Bo 	if (ac->newtrans->type & __TRANS_FREEZABLE)
1664ff7c1d33SMiao Xie 		rwsem_release(
1665ff7c1d33SMiao Xie 			&root->fs_info->sb->s_writers.lock_map[SB_FREEZE_FS-1],
16666fc4e354SSage Weil 			1, _THIS_IP_);
16676fc4e354SSage Weil 
16687892b5afSMiao Xie 	schedule_work(&ac->work);
1669bb9c12c9SSage Weil 
1670bb9c12c9SSage Weil 	/* wait for transaction to start and unblock */
1671bb9c12c9SSage Weil 	if (wait_for_unblock)
1672bb9c12c9SSage Weil 		wait_current_trans_commit_start_and_unblock(root, cur_trans);
1673bb9c12c9SSage Weil 	else
1674bb9c12c9SSage Weil 		wait_current_trans_commit_start(root, cur_trans);
1675bb9c12c9SSage Weil 
167638e88054SSage Weil 	if (current->journal_info == trans)
167738e88054SSage Weil 		current->journal_info = NULL;
167838e88054SSage Weil 
1679724e2315SJosef Bacik 	btrfs_put_transaction(cur_trans);
1680bb9c12c9SSage Weil 	return 0;
1681bb9c12c9SSage Weil }
1682bb9c12c9SSage Weil 
168349b25e05SJeff Mahoney 
168449b25e05SJeff Mahoney static void cleanup_transaction(struct btrfs_trans_handle *trans,
16857b8b92afSJosef Bacik 				struct btrfs_root *root, int err)
168649b25e05SJeff Mahoney {
168749b25e05SJeff Mahoney 	struct btrfs_transaction *cur_trans = trans->transaction;
1688f094ac32SLiu Bo 	DEFINE_WAIT(wait);
168949b25e05SJeff Mahoney 
169049b25e05SJeff Mahoney 	WARN_ON(trans->use_count > 1);
169149b25e05SJeff Mahoney 
16927b8b92afSJosef Bacik 	btrfs_abort_transaction(trans, root, err);
16937b8b92afSJosef Bacik 
169449b25e05SJeff Mahoney 	spin_lock(&root->fs_info->trans_lock);
169566b6135bSLiu Bo 
169625d8c284SMiao Xie 	/*
169725d8c284SMiao Xie 	 * If the transaction is removed from the list, it means this
169825d8c284SMiao Xie 	 * transaction has been committed successfully, so it is impossible
169925d8c284SMiao Xie 	 * to call the cleanup function.
170025d8c284SMiao Xie 	 */
170125d8c284SMiao Xie 	BUG_ON(list_empty(&cur_trans->list));
170266b6135bSLiu Bo 
170349b25e05SJeff Mahoney 	list_del_init(&cur_trans->list);
1704d7096fc3SJosef Bacik 	if (cur_trans == root->fs_info->running_transaction) {
17054a9d8bdeSMiao Xie 		cur_trans->state = TRANS_STATE_COMMIT_DOING;
1706f094ac32SLiu Bo 		spin_unlock(&root->fs_info->trans_lock);
1707f094ac32SLiu Bo 		wait_event(cur_trans->writer_wait,
1708f094ac32SLiu Bo 			   atomic_read(&cur_trans->num_writers) == 1);
1709f094ac32SLiu Bo 
1710f094ac32SLiu Bo 		spin_lock(&root->fs_info->trans_lock);
1711d7096fc3SJosef Bacik 	}
171249b25e05SJeff Mahoney 	spin_unlock(&root->fs_info->trans_lock);
171349b25e05SJeff Mahoney 
171449b25e05SJeff Mahoney 	btrfs_cleanup_one_transaction(trans->transaction, root);
171549b25e05SJeff Mahoney 
17164a9d8bdeSMiao Xie 	spin_lock(&root->fs_info->trans_lock);
17174a9d8bdeSMiao Xie 	if (cur_trans == root->fs_info->running_transaction)
17184a9d8bdeSMiao Xie 		root->fs_info->running_transaction = NULL;
17194a9d8bdeSMiao Xie 	spin_unlock(&root->fs_info->trans_lock);
17204a9d8bdeSMiao Xie 
1721e0228285SJosef Bacik 	if (trans->type & __TRANS_FREEZABLE)
1722e0228285SJosef Bacik 		sb_end_intwrite(root->fs_info->sb);
1723724e2315SJosef Bacik 	btrfs_put_transaction(cur_trans);
1724724e2315SJosef Bacik 	btrfs_put_transaction(cur_trans);
172549b25e05SJeff Mahoney 
172649b25e05SJeff Mahoney 	trace_btrfs_transaction_commit(root);
172749b25e05SJeff Mahoney 
172849b25e05SJeff Mahoney 	if (current->journal_info == trans)
172949b25e05SJeff Mahoney 		current->journal_info = NULL;
1730c0af8f0bSWang Shilong 	btrfs_scrub_cancel(root->fs_info);
173149b25e05SJeff Mahoney 
173249b25e05SJeff Mahoney 	kmem_cache_free(btrfs_trans_handle_cachep, trans);
173349b25e05SJeff Mahoney }
173449b25e05SJeff Mahoney 
173582436617SMiao Xie static inline int btrfs_start_delalloc_flush(struct btrfs_fs_info *fs_info)
173682436617SMiao Xie {
173782436617SMiao Xie 	if (btrfs_test_opt(fs_info->tree_root, FLUSHONCOMMIT))
17386c255e67SMiao Xie 		return btrfs_start_delalloc_roots(fs_info, 1, -1);
173982436617SMiao Xie 	return 0;
174082436617SMiao Xie }
174182436617SMiao Xie 
174282436617SMiao Xie static inline void btrfs_wait_delalloc_flush(struct btrfs_fs_info *fs_info)
174382436617SMiao Xie {
174482436617SMiao Xie 	if (btrfs_test_opt(fs_info->tree_root, FLUSHONCOMMIT))
1745b0244199SMiao Xie 		btrfs_wait_ordered_roots(fs_info, -1);
174682436617SMiao Xie }
174782436617SMiao Xie 
174850d9aa99SJosef Bacik static inline void
174950d9aa99SJosef Bacik btrfs_wait_pending_ordered(struct btrfs_transaction *cur_trans,
175050d9aa99SJosef Bacik 			   struct btrfs_fs_info *fs_info)
175150d9aa99SJosef Bacik {
175250d9aa99SJosef Bacik 	struct btrfs_ordered_extent *ordered;
175350d9aa99SJosef Bacik 
175450d9aa99SJosef Bacik 	spin_lock(&fs_info->trans_lock);
175550d9aa99SJosef Bacik 	while (!list_empty(&cur_trans->pending_ordered)) {
175650d9aa99SJosef Bacik 		ordered = list_first_entry(&cur_trans->pending_ordered,
175750d9aa99SJosef Bacik 					   struct btrfs_ordered_extent,
175850d9aa99SJosef Bacik 					   trans_list);
175950d9aa99SJosef Bacik 		list_del_init(&ordered->trans_list);
176050d9aa99SJosef Bacik 		spin_unlock(&fs_info->trans_lock);
176150d9aa99SJosef Bacik 
176250d9aa99SJosef Bacik 		wait_event(ordered->wait, test_bit(BTRFS_ORDERED_COMPLETE,
176350d9aa99SJosef Bacik 						   &ordered->flags));
176450d9aa99SJosef Bacik 		btrfs_put_ordered_extent(ordered);
176550d9aa99SJosef Bacik 		spin_lock(&fs_info->trans_lock);
176650d9aa99SJosef Bacik 	}
176750d9aa99SJosef Bacik 	spin_unlock(&fs_info->trans_lock);
176850d9aa99SJosef Bacik }
176950d9aa99SJosef Bacik 
177079154b1bSChris Mason int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
177179154b1bSChris Mason 			     struct btrfs_root *root)
177279154b1bSChris Mason {
177349b25e05SJeff Mahoney 	struct btrfs_transaction *cur_trans = trans->transaction;
17748fd17795SChris Mason 	struct btrfs_transaction *prev_trans = NULL;
1775656f30dbSFilipe Manana 	struct btrfs_inode *btree_ino = BTRFS_I(root->fs_info->btree_inode);
177625287e0aSMiao Xie 	int ret;
177779154b1bSChris Mason 
17788d25a086SMiao Xie 	/* Stop the commit early if ->aborted is set */
17798d25a086SMiao Xie 	if (unlikely(ACCESS_ONCE(cur_trans->aborted))) {
178025287e0aSMiao Xie 		ret = cur_trans->aborted;
1781e4a2bcacSJosef Bacik 		btrfs_end_transaction(trans, root);
1782e4a2bcacSJosef Bacik 		return ret;
178325287e0aSMiao Xie 	}
178449b25e05SJeff Mahoney 
178556bec294SChris Mason 	/* make a pass through all the delayed refs we have so far
178656bec294SChris Mason 	 * any runnings procs may add more while we are here
178756bec294SChris Mason 	 */
178856bec294SChris Mason 	ret = btrfs_run_delayed_refs(trans, root, 0);
1789e4a2bcacSJosef Bacik 	if (ret) {
1790e4a2bcacSJosef Bacik 		btrfs_end_transaction(trans, root);
1791e4a2bcacSJosef Bacik 		return ret;
1792e4a2bcacSJosef Bacik 	}
179356bec294SChris Mason 
17940e721106SJosef Bacik 	btrfs_trans_release_metadata(trans, root);
17950e721106SJosef Bacik 	trans->block_rsv = NULL;
1796272d26d0SMiao Xie 	if (trans->qgroup_reserved) {
1797272d26d0SMiao Xie 		btrfs_qgroup_free(root, trans->qgroup_reserved);
1798272d26d0SMiao Xie 		trans->qgroup_reserved = 0;
1799272d26d0SMiao Xie 	}
18000e721106SJosef Bacik 
1801b7ec40d7SChris Mason 	cur_trans = trans->transaction;
180249b25e05SJeff Mahoney 
180356bec294SChris Mason 	/*
180456bec294SChris Mason 	 * set the flushing flag so procs in this transaction have to
180556bec294SChris Mason 	 * start sending their work down.
180656bec294SChris Mason 	 */
1807b7ec40d7SChris Mason 	cur_trans->delayed_refs.flushing = 1;
18081be41b78SJosef Bacik 	smp_wmb();
180956bec294SChris Mason 
1810ea658badSJosef Bacik 	if (!list_empty(&trans->new_bgs))
1811ea658badSJosef Bacik 		btrfs_create_pending_block_groups(trans, root);
1812ea658badSJosef Bacik 
1813c3e69d58SChris Mason 	ret = btrfs_run_delayed_refs(trans, root, 0);
1814e4a2bcacSJosef Bacik 	if (ret) {
1815e4a2bcacSJosef Bacik 		btrfs_end_transaction(trans, root);
1816e4a2bcacSJosef Bacik 		return ret;
1817e4a2bcacSJosef Bacik 	}
181856bec294SChris Mason 
18191bbc621eSChris Mason 	if (!cur_trans->dirty_bg_run) {
18201bbc621eSChris Mason 		int run_it = 0;
18211bbc621eSChris Mason 
18221bbc621eSChris Mason 		/* this mutex is also taken before trying to set
18231bbc621eSChris Mason 		 * block groups readonly.  We need to make sure
18241bbc621eSChris Mason 		 * that nobody has set a block group readonly
18251bbc621eSChris Mason 		 * after a extents from that block group have been
18261bbc621eSChris Mason 		 * allocated for cache files.  btrfs_set_block_group_ro
18271bbc621eSChris Mason 		 * will wait for the transaction to commit if it
18281bbc621eSChris Mason 		 * finds dirty_bg_run = 1
18291bbc621eSChris Mason 		 *
18301bbc621eSChris Mason 		 * The dirty_bg_run flag is also used to make sure only
18311bbc621eSChris Mason 		 * one process starts all the block group IO.  It wouldn't
18321bbc621eSChris Mason 		 * hurt to have more than one go through, but there's no
18331bbc621eSChris Mason 		 * real advantage to it either.
18341bbc621eSChris Mason 		 */
18351bbc621eSChris Mason 		mutex_lock(&root->fs_info->ro_block_group_mutex);
18361bbc621eSChris Mason 		if (!cur_trans->dirty_bg_run) {
18371bbc621eSChris Mason 			run_it = 1;
18381bbc621eSChris Mason 			cur_trans->dirty_bg_run = 1;
18391bbc621eSChris Mason 		}
18401bbc621eSChris Mason 		mutex_unlock(&root->fs_info->ro_block_group_mutex);
18411bbc621eSChris Mason 
18421bbc621eSChris Mason 		if (run_it)
18431bbc621eSChris Mason 			ret = btrfs_start_dirty_block_groups(trans, root);
18441bbc621eSChris Mason 	}
18451bbc621eSChris Mason 	if (ret) {
18461bbc621eSChris Mason 		btrfs_end_transaction(trans, root);
18471bbc621eSChris Mason 		return ret;
18481bbc621eSChris Mason 	}
18491bbc621eSChris Mason 
18504a9d8bdeSMiao Xie 	spin_lock(&root->fs_info->trans_lock);
185150d9aa99SJosef Bacik 	list_splice(&trans->ordered, &cur_trans->pending_ordered);
18524a9d8bdeSMiao Xie 	if (cur_trans->state >= TRANS_STATE_COMMIT_START) {
18534a9d8bdeSMiao Xie 		spin_unlock(&root->fs_info->trans_lock);
185413c5a93eSJosef Bacik 		atomic_inc(&cur_trans->use_count);
185549b25e05SJeff Mahoney 		ret = btrfs_end_transaction(trans, root);
1856ccd467d6SChris Mason 
1857b9c8300cSLi Zefan 		wait_for_commit(root, cur_trans);
185815ee9bc7SJosef Bacik 
1859b4924a0fSLiu Bo 		if (unlikely(cur_trans->aborted))
1860b4924a0fSLiu Bo 			ret = cur_trans->aborted;
1861b4924a0fSLiu Bo 
1862724e2315SJosef Bacik 		btrfs_put_transaction(cur_trans);
186315ee9bc7SJosef Bacik 
186449b25e05SJeff Mahoney 		return ret;
186579154b1bSChris Mason 	}
18664313b399SChris Mason 
18674a9d8bdeSMiao Xie 	cur_trans->state = TRANS_STATE_COMMIT_START;
1868bb9c12c9SSage Weil 	wake_up(&root->fs_info->transaction_blocked_wait);
1869bb9c12c9SSage Weil 
1870ccd467d6SChris Mason 	if (cur_trans->list.prev != &root->fs_info->trans_list) {
1871ccd467d6SChris Mason 		prev_trans = list_entry(cur_trans->list.prev,
1872ccd467d6SChris Mason 					struct btrfs_transaction, list);
18734a9d8bdeSMiao Xie 		if (prev_trans->state != TRANS_STATE_COMPLETED) {
187413c5a93eSJosef Bacik 			atomic_inc(&prev_trans->use_count);
1875a4abeea4SJosef Bacik 			spin_unlock(&root->fs_info->trans_lock);
1876ccd467d6SChris Mason 
1877ccd467d6SChris Mason 			wait_for_commit(root, prev_trans);
1878ccd467d6SChris Mason 
1879724e2315SJosef Bacik 			btrfs_put_transaction(prev_trans);
1880a4abeea4SJosef Bacik 		} else {
1881a4abeea4SJosef Bacik 			spin_unlock(&root->fs_info->trans_lock);
1882ccd467d6SChris Mason 		}
1883a4abeea4SJosef Bacik 	} else {
1884a4abeea4SJosef Bacik 		spin_unlock(&root->fs_info->trans_lock);
1885ccd467d6SChris Mason 	}
188615ee9bc7SJosef Bacik 
18870860adfdSMiao Xie 	extwriter_counter_dec(cur_trans, trans->type);
18880860adfdSMiao Xie 
188982436617SMiao Xie 	ret = btrfs_start_delalloc_flush(root->fs_info);
189082436617SMiao Xie 	if (ret)
189182436617SMiao Xie 		goto cleanup_transaction;
189282436617SMiao Xie 
18938d875f95SChris Mason 	ret = btrfs_run_delayed_items(trans, root);
189449b25e05SJeff Mahoney 	if (ret)
189549b25e05SJeff Mahoney 		goto cleanup_transaction;
189616cdcec7SMiao Xie 
1897581227d0SMiao Xie 	wait_event(cur_trans->writer_wait,
1898581227d0SMiao Xie 		   extwriter_counter_read(cur_trans) == 0);
1899ed3b3d31SChris Mason 
1900581227d0SMiao Xie 	/* some pending stuffs might be added after the previous flush. */
19018d875f95SChris Mason 	ret = btrfs_run_delayed_items(trans, root);
1902ca469637SMiao Xie 	if (ret)
1903ca469637SMiao Xie 		goto cleanup_transaction;
1904ca469637SMiao Xie 
190582436617SMiao Xie 	btrfs_wait_delalloc_flush(root->fs_info);
1906cb7ab021SWang Shilong 
190750d9aa99SJosef Bacik 	btrfs_wait_pending_ordered(cur_trans, root->fs_info);
190850d9aa99SJosef Bacik 
1909cb7ab021SWang Shilong 	btrfs_scrub_pause(root);
1910ed0ca140SJosef Bacik 	/*
1911ed0ca140SJosef Bacik 	 * Ok now we need to make sure to block out any other joins while we
1912ed0ca140SJosef Bacik 	 * commit the transaction.  We could have started a join before setting
19134a9d8bdeSMiao Xie 	 * COMMIT_DOING so make sure to wait for num_writers to == 1 again.
1914ed0ca140SJosef Bacik 	 */
1915a4abeea4SJosef Bacik 	spin_lock(&root->fs_info->trans_lock);
19164a9d8bdeSMiao Xie 	cur_trans->state = TRANS_STATE_COMMIT_DOING;
1917a4abeea4SJosef Bacik 	spin_unlock(&root->fs_info->trans_lock);
1918ed0ca140SJosef Bacik 	wait_event(cur_trans->writer_wait,
1919ed0ca140SJosef Bacik 		   atomic_read(&cur_trans->num_writers) == 1);
192015ee9bc7SJosef Bacik 
19212cba30f1SMiao Xie 	/* ->aborted might be set after the previous check, so check it */
19222cba30f1SMiao Xie 	if (unlikely(ACCESS_ONCE(cur_trans->aborted))) {
19232cba30f1SMiao Xie 		ret = cur_trans->aborted;
19246cf7f77eSWang Shilong 		goto scrub_continue;
19252cba30f1SMiao Xie 	}
19267585717fSChris Mason 	/*
19277585717fSChris Mason 	 * the reloc mutex makes sure that we stop
19287585717fSChris Mason 	 * the balancing code from coming in and moving
19297585717fSChris Mason 	 * extents around in the middle of the commit
19307585717fSChris Mason 	 */
19317585717fSChris Mason 	mutex_lock(&root->fs_info->reloc_mutex);
19327585717fSChris Mason 
193342874b3dSMiao Xie 	/*
193442874b3dSMiao Xie 	 * We needn't worry about the delayed items because we will
193542874b3dSMiao Xie 	 * deal with them in create_pending_snapshot(), which is the
193642874b3dSMiao Xie 	 * core function of the snapshot creation.
193742874b3dSMiao Xie 	 */
193842874b3dSMiao Xie 	ret = create_pending_snapshots(trans, root->fs_info);
193949b25e05SJeff Mahoney 	if (ret) {
194049b25e05SJeff Mahoney 		mutex_unlock(&root->fs_info->reloc_mutex);
19416cf7f77eSWang Shilong 		goto scrub_continue;
194249b25e05SJeff Mahoney 	}
19433063d29fSChris Mason 
194442874b3dSMiao Xie 	/*
194542874b3dSMiao Xie 	 * We insert the dir indexes of the snapshots and update the inode
194642874b3dSMiao Xie 	 * of the snapshots' parents after the snapshot creation, so there
194742874b3dSMiao Xie 	 * are some delayed items which are not dealt with. Now deal with
194842874b3dSMiao Xie 	 * them.
194942874b3dSMiao Xie 	 *
195042874b3dSMiao Xie 	 * We needn't worry that this operation will corrupt the snapshots,
195142874b3dSMiao Xie 	 * because all the tree which are snapshoted will be forced to COW
195242874b3dSMiao Xie 	 * the nodes and leaves.
195342874b3dSMiao Xie 	 */
195442874b3dSMiao Xie 	ret = btrfs_run_delayed_items(trans, root);
195549b25e05SJeff Mahoney 	if (ret) {
195649b25e05SJeff Mahoney 		mutex_unlock(&root->fs_info->reloc_mutex);
19576cf7f77eSWang Shilong 		goto scrub_continue;
195849b25e05SJeff Mahoney 	}
195916cdcec7SMiao Xie 
196056bec294SChris Mason 	ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
196149b25e05SJeff Mahoney 	if (ret) {
196249b25e05SJeff Mahoney 		mutex_unlock(&root->fs_info->reloc_mutex);
19636cf7f77eSWang Shilong 		goto scrub_continue;
196449b25e05SJeff Mahoney 	}
196556bec294SChris Mason 
1966e999376fSChris Mason 	/*
1967e999376fSChris Mason 	 * make sure none of the code above managed to slip in a
1968e999376fSChris Mason 	 * delayed item
1969e999376fSChris Mason 	 */
1970e999376fSChris Mason 	btrfs_assert_delayed_root_empty(root);
1971e999376fSChris Mason 
19722c90e5d6SChris Mason 	WARN_ON(cur_trans != trans->transaction);
1973dc17ff8fSChris Mason 
1974e02119d5SChris Mason 	/* btrfs_commit_tree_roots is responsible for getting the
1975e02119d5SChris Mason 	 * various roots consistent with each other.  Every pointer
1976e02119d5SChris Mason 	 * in the tree of tree roots has to point to the most up to date
1977e02119d5SChris Mason 	 * root for every subvolume and other tree.  So, we have to keep
1978e02119d5SChris Mason 	 * the tree logging code from jumping in and changing any
1979e02119d5SChris Mason 	 * of the trees.
1980e02119d5SChris Mason 	 *
1981e02119d5SChris Mason 	 * At this point in the commit, there can't be any tree-log
1982e02119d5SChris Mason 	 * writers, but a little lower down we drop the trans mutex
1983e02119d5SChris Mason 	 * and let new people in.  By holding the tree_log_mutex
1984e02119d5SChris Mason 	 * from now until after the super is written, we avoid races
1985e02119d5SChris Mason 	 * with the tree-log code.
1986e02119d5SChris Mason 	 */
1987e02119d5SChris Mason 	mutex_lock(&root->fs_info->tree_log_mutex);
19881a40e23bSZheng Yan 
19895d4f98a2SYan Zheng 	ret = commit_fs_roots(trans, root);
199049b25e05SJeff Mahoney 	if (ret) {
199149b25e05SJeff Mahoney 		mutex_unlock(&root->fs_info->tree_log_mutex);
1992871383beSDavid Sterba 		mutex_unlock(&root->fs_info->reloc_mutex);
19936cf7f77eSWang Shilong 		goto scrub_continue;
199449b25e05SJeff Mahoney 	}
199554aa1f4dSChris Mason 
19963818aea2SQu Wenruo 	/*
19977e1876acSDavid Sterba 	 * Since the transaction is done, we can apply the pending changes
19987e1876acSDavid Sterba 	 * before the next transaction.
19993818aea2SQu Wenruo 	 */
2000572d9ab7SDavid Sterba 	btrfs_apply_pending_changes(root->fs_info);
20013818aea2SQu Wenruo 
20025d4f98a2SYan Zheng 	/* commit_fs_roots gets rid of all the tree log roots, it is now
2003e02119d5SChris Mason 	 * safe to free the root of tree log roots
2004e02119d5SChris Mason 	 */
2005e02119d5SChris Mason 	btrfs_free_log_root_tree(trans, root->fs_info);
2006e02119d5SChris Mason 
20075d4f98a2SYan Zheng 	ret = commit_cowonly_roots(trans, root);
200849b25e05SJeff Mahoney 	if (ret) {
200949b25e05SJeff Mahoney 		mutex_unlock(&root->fs_info->tree_log_mutex);
2010871383beSDavid Sterba 		mutex_unlock(&root->fs_info->reloc_mutex);
20116cf7f77eSWang Shilong 		goto scrub_continue;
201249b25e05SJeff Mahoney 	}
201354aa1f4dSChris Mason 
20142cba30f1SMiao Xie 	/*
20152cba30f1SMiao Xie 	 * The tasks which save the space cache and inode cache may also
20162cba30f1SMiao Xie 	 * update ->aborted, check it.
20172cba30f1SMiao Xie 	 */
20182cba30f1SMiao Xie 	if (unlikely(ACCESS_ONCE(cur_trans->aborted))) {
20192cba30f1SMiao Xie 		ret = cur_trans->aborted;
20202cba30f1SMiao Xie 		mutex_unlock(&root->fs_info->tree_log_mutex);
20212cba30f1SMiao Xie 		mutex_unlock(&root->fs_info->reloc_mutex);
20226cf7f77eSWang Shilong 		goto scrub_continue;
20232cba30f1SMiao Xie 	}
20242cba30f1SMiao Xie 
202511833d66SYan Zheng 	btrfs_prepare_extent_commit(trans, root);
202611833d66SYan Zheng 
202778fae27eSChris Mason 	cur_trans = root->fs_info->running_transaction;
20285f39d397SChris Mason 
20295d4f98a2SYan Zheng 	btrfs_set_root_node(&root->fs_info->tree_root->root_item,
20305d4f98a2SYan Zheng 			    root->fs_info->tree_root->node);
20319e351cc8SJosef Bacik 	list_add_tail(&root->fs_info->tree_root->dirty_list,
20329e351cc8SJosef Bacik 		      &cur_trans->switch_commits);
20335d4f98a2SYan Zheng 
20345d4f98a2SYan Zheng 	btrfs_set_root_node(&root->fs_info->chunk_root->root_item,
20355d4f98a2SYan Zheng 			    root->fs_info->chunk_root->node);
20369e351cc8SJosef Bacik 	list_add_tail(&root->fs_info->chunk_root->dirty_list,
20379e351cc8SJosef Bacik 		      &cur_trans->switch_commits);
20389e351cc8SJosef Bacik 
20399e351cc8SJosef Bacik 	switch_commit_roots(cur_trans, root->fs_info);
20405d4f98a2SYan Zheng 
2041edf39272SJan Schmidt 	assert_qgroups_uptodate(trans);
2042ce93ec54SJosef Bacik 	ASSERT(list_empty(&cur_trans->dirty_bgs));
20431bbc621eSChris Mason 	ASSERT(list_empty(&cur_trans->io_bgs));
20445d4f98a2SYan Zheng 	update_super_roots(root);
2045e02119d5SChris Mason 
20466c41761fSDavid Sterba 	btrfs_set_super_log_root(root->fs_info->super_copy, 0);
20476c41761fSDavid Sterba 	btrfs_set_super_log_root_level(root->fs_info->super_copy, 0);
20486c41761fSDavid Sterba 	memcpy(root->fs_info->super_for_commit, root->fs_info->super_copy,
20496c41761fSDavid Sterba 	       sizeof(*root->fs_info->super_copy));
2050ccd467d6SChris Mason 
2051935e5cc9SMiao Xie 	btrfs_update_commit_device_size(root->fs_info);
2052ce7213c7SMiao Xie 	btrfs_update_commit_device_bytes_used(root, cur_trans);
2053935e5cc9SMiao Xie 
2054656f30dbSFilipe Manana 	clear_bit(BTRFS_INODE_BTREE_LOG1_ERR, &btree_ino->runtime_flags);
2055656f30dbSFilipe Manana 	clear_bit(BTRFS_INODE_BTREE_LOG2_ERR, &btree_ino->runtime_flags);
2056656f30dbSFilipe Manana 
2057a4abeea4SJosef Bacik 	spin_lock(&root->fs_info->trans_lock);
20584a9d8bdeSMiao Xie 	cur_trans->state = TRANS_STATE_UNBLOCKED;
2059a4abeea4SJosef Bacik 	root->fs_info->running_transaction = NULL;
2060a4abeea4SJosef Bacik 	spin_unlock(&root->fs_info->trans_lock);
20617585717fSChris Mason 	mutex_unlock(&root->fs_info->reloc_mutex);
2062b7ec40d7SChris Mason 
2063f9295749SChris Mason 	wake_up(&root->fs_info->transaction_wait);
2064e6dcd2dcSChris Mason 
206579154b1bSChris Mason 	ret = btrfs_write_and_wait_transaction(trans, root);
206649b25e05SJeff Mahoney 	if (ret) {
206749b25e05SJeff Mahoney 		btrfs_error(root->fs_info, ret,
206808748810SDavid Sterba 			    "Error while writing out transaction");
206949b25e05SJeff Mahoney 		mutex_unlock(&root->fs_info->tree_log_mutex);
20706cf7f77eSWang Shilong 		goto scrub_continue;
207149b25e05SJeff Mahoney 	}
207249b25e05SJeff Mahoney 
207349b25e05SJeff Mahoney 	ret = write_ctree_super(trans, root, 0);
207449b25e05SJeff Mahoney 	if (ret) {
207549b25e05SJeff Mahoney 		mutex_unlock(&root->fs_info->tree_log_mutex);
20766cf7f77eSWang Shilong 		goto scrub_continue;
207749b25e05SJeff Mahoney 	}
20784313b399SChris Mason 
2079e02119d5SChris Mason 	/*
2080e02119d5SChris Mason 	 * the super is written, we can safely allow the tree-loggers
2081e02119d5SChris Mason 	 * to go about their business
2082e02119d5SChris Mason 	 */
2083e02119d5SChris Mason 	mutex_unlock(&root->fs_info->tree_log_mutex);
2084e02119d5SChris Mason 
208511833d66SYan Zheng 	btrfs_finish_extent_commit(trans, root);
20864313b399SChris Mason 
208713212b54SZhao Lei 	if (cur_trans->have_free_bgs)
208813212b54SZhao Lei 		btrfs_clear_space_info_full(root->fs_info);
208913212b54SZhao Lei 
209015ee9bc7SJosef Bacik 	root->fs_info->last_trans_committed = cur_trans->transid;
20914a9d8bdeSMiao Xie 	/*
20924a9d8bdeSMiao Xie 	 * We needn't acquire the lock here because there is no other task
20934a9d8bdeSMiao Xie 	 * which can change it.
20944a9d8bdeSMiao Xie 	 */
20954a9d8bdeSMiao Xie 	cur_trans->state = TRANS_STATE_COMPLETED;
20962c90e5d6SChris Mason 	wake_up(&cur_trans->commit_wait);
20973de4586cSChris Mason 
2098a4abeea4SJosef Bacik 	spin_lock(&root->fs_info->trans_lock);
209913c5a93eSJosef Bacik 	list_del_init(&cur_trans->list);
2100a4abeea4SJosef Bacik 	spin_unlock(&root->fs_info->trans_lock);
2101a4abeea4SJosef Bacik 
2102724e2315SJosef Bacik 	btrfs_put_transaction(cur_trans);
2103724e2315SJosef Bacik 	btrfs_put_transaction(cur_trans);
210458176a96SJosef Bacik 
21050860adfdSMiao Xie 	if (trans->type & __TRANS_FREEZABLE)
2106b2b5ef5cSJan Kara 		sb_end_intwrite(root->fs_info->sb);
2107b2b5ef5cSJan Kara 
21081abe9b8aSliubo 	trace_btrfs_transaction_commit(root);
21091abe9b8aSliubo 
2110a2de733cSArne Jansen 	btrfs_scrub_continue(root);
2111a2de733cSArne Jansen 
21129ed74f2dSJosef Bacik 	if (current->journal_info == trans)
21139ed74f2dSJosef Bacik 		current->journal_info = NULL;
21149ed74f2dSJosef Bacik 
21152c90e5d6SChris Mason 	kmem_cache_free(btrfs_trans_handle_cachep, trans);
211624bbcf04SYan, Zheng 
211724bbcf04SYan, Zheng 	if (current != root->fs_info->transaction_kthread)
211824bbcf04SYan, Zheng 		btrfs_run_delayed_iputs(root);
211924bbcf04SYan, Zheng 
212079154b1bSChris Mason 	return ret;
212149b25e05SJeff Mahoney 
21226cf7f77eSWang Shilong scrub_continue:
21236cf7f77eSWang Shilong 	btrfs_scrub_continue(root);
212449b25e05SJeff Mahoney cleanup_transaction:
21250e721106SJosef Bacik 	btrfs_trans_release_metadata(trans, root);
21260e721106SJosef Bacik 	trans->block_rsv = NULL;
2127272d26d0SMiao Xie 	if (trans->qgroup_reserved) {
2128272d26d0SMiao Xie 		btrfs_qgroup_free(root, trans->qgroup_reserved);
2129272d26d0SMiao Xie 		trans->qgroup_reserved = 0;
2130272d26d0SMiao Xie 	}
2131c2cf52ebSSimon Kirby 	btrfs_warn(root->fs_info, "Skipping commit of aborted transaction.");
213249b25e05SJeff Mahoney 	if (current->journal_info == trans)
213349b25e05SJeff Mahoney 		current->journal_info = NULL;
21347b8b92afSJosef Bacik 	cleanup_transaction(trans, root, ret);
213549b25e05SJeff Mahoney 
213649b25e05SJeff Mahoney 	return ret;
213779154b1bSChris Mason }
213879154b1bSChris Mason 
2139d352ac68SChris Mason /*
21409d1a2a3aSDavid Sterba  * return < 0 if error
21419d1a2a3aSDavid Sterba  * 0 if there are no more dead_roots at the time of call
21429d1a2a3aSDavid Sterba  * 1 there are more to be processed, call me again
21439d1a2a3aSDavid Sterba  *
21449d1a2a3aSDavid Sterba  * The return value indicates there are certainly more snapshots to delete, but
21459d1a2a3aSDavid Sterba  * if there comes a new one during processing, it may return 0. We don't mind,
21469d1a2a3aSDavid Sterba  * because btrfs_commit_super will poke cleaner thread and it will process it a
21479d1a2a3aSDavid Sterba  * few seconds later.
2148d352ac68SChris Mason  */
21499d1a2a3aSDavid Sterba int btrfs_clean_one_deleted_snapshot(struct btrfs_root *root)
2150e9d0b13bSChris Mason {
21519d1a2a3aSDavid Sterba 	int ret;
21525d4f98a2SYan Zheng 	struct btrfs_fs_info *fs_info = root->fs_info;
2153e9d0b13bSChris Mason 
2154a4abeea4SJosef Bacik 	spin_lock(&fs_info->trans_lock);
21559d1a2a3aSDavid Sterba 	if (list_empty(&fs_info->dead_roots)) {
21569d1a2a3aSDavid Sterba 		spin_unlock(&fs_info->trans_lock);
21579d1a2a3aSDavid Sterba 		return 0;
21589d1a2a3aSDavid Sterba 	}
21599d1a2a3aSDavid Sterba 	root = list_first_entry(&fs_info->dead_roots,
21609d1a2a3aSDavid Sterba 			struct btrfs_root, root_list);
2161cfad392bSJosef Bacik 	list_del_init(&root->root_list);
2162a4abeea4SJosef Bacik 	spin_unlock(&fs_info->trans_lock);
21635d4f98a2SYan Zheng 
2164efe120a0SFrank Holton 	pr_debug("BTRFS: cleaner removing %llu\n", root->objectid);
216576dda93cSYan, Zheng 
216616cdcec7SMiao Xie 	btrfs_kill_all_delayed_nodes(root);
216716cdcec7SMiao Xie 
216876dda93cSYan, Zheng 	if (btrfs_header_backref_rev(root->node) <
216976dda93cSYan, Zheng 			BTRFS_MIXED_BACKREF_REV)
21702c536799SJeff Mahoney 		ret = btrfs_drop_snapshot(root, NULL, 0, 0);
217176dda93cSYan, Zheng 	else
21722c536799SJeff Mahoney 		ret = btrfs_drop_snapshot(root, NULL, 1, 0);
217332471dc2SDavid Sterba 
21746596a928SJosef Bacik 	return (ret < 0) ? 0 : 1;
2175e9d0b13bSChris Mason }
2176572d9ab7SDavid Sterba 
2177572d9ab7SDavid Sterba void btrfs_apply_pending_changes(struct btrfs_fs_info *fs_info)
2178572d9ab7SDavid Sterba {
2179572d9ab7SDavid Sterba 	unsigned long prev;
2180572d9ab7SDavid Sterba 	unsigned long bit;
2181572d9ab7SDavid Sterba 
21826c9fe14fSQu Wenruo 	prev = xchg(&fs_info->pending_changes, 0);
2183572d9ab7SDavid Sterba 	if (!prev)
2184572d9ab7SDavid Sterba 		return;
2185572d9ab7SDavid Sterba 
21867e1876acSDavid Sterba 	bit = 1 << BTRFS_PENDING_SET_INODE_MAP_CACHE;
21877e1876acSDavid Sterba 	if (prev & bit)
21887e1876acSDavid Sterba 		btrfs_set_opt(fs_info->mount_opt, INODE_MAP_CACHE);
21897e1876acSDavid Sterba 	prev &= ~bit;
21907e1876acSDavid Sterba 
21917e1876acSDavid Sterba 	bit = 1 << BTRFS_PENDING_CLEAR_INODE_MAP_CACHE;
21927e1876acSDavid Sterba 	if (prev & bit)
21937e1876acSDavid Sterba 		btrfs_clear_opt(fs_info->mount_opt, INODE_MAP_CACHE);
21947e1876acSDavid Sterba 	prev &= ~bit;
21957e1876acSDavid Sterba 
2196d51033d0SDavid Sterba 	bit = 1 << BTRFS_PENDING_COMMIT;
2197d51033d0SDavid Sterba 	if (prev & bit)
2198d51033d0SDavid Sterba 		btrfs_debug(fs_info, "pending commit done");
2199d51033d0SDavid Sterba 	prev &= ~bit;
2200d51033d0SDavid Sterba 
2201572d9ab7SDavid Sterba 	if (prev)
2202572d9ab7SDavid Sterba 		btrfs_warn(fs_info,
2203572d9ab7SDavid Sterba 			"unknown pending changes left 0x%lx, ignoring", prev);
2204572d9ab7SDavid Sterba }
2205