xref: /openbmc/linux/fs/btrfs/transaction.c (revision e8c9f186)
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));
676df9a95eSJosef Bacik 		while (!list_empty(&transaction->pending_chunks)) {
686df9a95eSJosef Bacik 			struct extent_map *em;
696df9a95eSJosef Bacik 
706df9a95eSJosef Bacik 			em = list_first_entry(&transaction->pending_chunks,
716df9a95eSJosef Bacik 					      struct extent_map, list);
726df9a95eSJosef Bacik 			list_del_init(&em->list);
736df9a95eSJosef Bacik 			free_extent_map(em);
746df9a95eSJosef Bacik 		}
752c90e5d6SChris Mason 		kmem_cache_free(btrfs_transaction_cachep, transaction);
7679154b1bSChris Mason 	}
7778fae27eSChris Mason }
7879154b1bSChris Mason 
79663dfbb0SFilipe Manana static void clear_btree_io_tree(struct extent_io_tree *tree)
80663dfbb0SFilipe Manana {
81663dfbb0SFilipe Manana 	spin_lock(&tree->lock);
82663dfbb0SFilipe Manana 	while (!RB_EMPTY_ROOT(&tree->state)) {
83663dfbb0SFilipe Manana 		struct rb_node *node;
84663dfbb0SFilipe Manana 		struct extent_state *state;
85663dfbb0SFilipe Manana 
86663dfbb0SFilipe Manana 		node = rb_first(&tree->state);
87663dfbb0SFilipe Manana 		state = rb_entry(node, struct extent_state, rb_node);
88663dfbb0SFilipe Manana 		rb_erase(&state->rb_node, &tree->state);
89663dfbb0SFilipe Manana 		RB_CLEAR_NODE(&state->rb_node);
90663dfbb0SFilipe Manana 		/*
91663dfbb0SFilipe Manana 		 * btree io trees aren't supposed to have tasks waiting for
92663dfbb0SFilipe Manana 		 * changes in the flags of extent states ever.
93663dfbb0SFilipe Manana 		 */
94663dfbb0SFilipe Manana 		ASSERT(!waitqueue_active(&state->wq));
95663dfbb0SFilipe Manana 		free_extent_state(state);
96663dfbb0SFilipe Manana 		if (need_resched()) {
97663dfbb0SFilipe Manana 			spin_unlock(&tree->lock);
98663dfbb0SFilipe Manana 			cond_resched();
99663dfbb0SFilipe Manana 			spin_lock(&tree->lock);
100663dfbb0SFilipe Manana 		}
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();
22556bec294SChris Mason 
226c46effa6SLiu Bo 	cur_trans->delayed_refs.href_root = RB_ROOT;
227d7df2c79SJosef Bacik 	atomic_set(&cur_trans->delayed_refs.num_entries, 0);
228c3e69d58SChris Mason 	cur_trans->delayed_refs.num_heads_ready = 0;
229c3e69d58SChris Mason 	cur_trans->delayed_refs.num_heads = 0;
23056bec294SChris Mason 	cur_trans->delayed_refs.flushing = 0;
231c3e69d58SChris Mason 	cur_trans->delayed_refs.run_delayed_start = 0;
23220b297d6SJan Schmidt 
23320b297d6SJan Schmidt 	/*
23420b297d6SJan Schmidt 	 * although the tree mod log is per file system and not per transaction,
23520b297d6SJan Schmidt 	 * the log must never go across transaction boundaries.
23620b297d6SJan Schmidt 	 */
23720b297d6SJan Schmidt 	smp_mb();
23831b1a2bdSJulia Lawall 	if (!list_empty(&fs_info->tree_mod_seq_list))
239efe120a0SFrank Holton 		WARN(1, KERN_ERR "BTRFS: tree_mod_seq_list not empty when "
24020b297d6SJan Schmidt 			"creating a fresh transaction\n");
24131b1a2bdSJulia Lawall 	if (!RB_EMPTY_ROOT(&fs_info->tree_mod_log))
242efe120a0SFrank Holton 		WARN(1, KERN_ERR "BTRFS: tree_mod_log rb tree not empty when "
24320b297d6SJan Schmidt 			"creating a fresh transaction\n");
244fc36ed7eSJan Schmidt 	atomic64_set(&fs_info->tree_mod_seq, 0);
24520b297d6SJan Schmidt 
24656bec294SChris Mason 	spin_lock_init(&cur_trans->delayed_refs.lock);
24756bec294SChris Mason 
2483063d29fSChris Mason 	INIT_LIST_HEAD(&cur_trans->pending_snapshots);
2496df9a95eSJosef Bacik 	INIT_LIST_HEAD(&cur_trans->pending_chunks);
2509e351cc8SJosef Bacik 	INIT_LIST_HEAD(&cur_trans->switch_commits);
25150d9aa99SJosef Bacik 	INIT_LIST_HEAD(&cur_trans->pending_ordered);
252ce93ec54SJosef Bacik 	INIT_LIST_HEAD(&cur_trans->dirty_bgs);
253ce93ec54SJosef Bacik 	spin_lock_init(&cur_trans->dirty_bgs_lock);
25419ae4e81SJan Schmidt 	list_add_tail(&cur_trans->list, &fs_info->trans_list);
255d1310b2eSChris Mason 	extent_io_tree_init(&cur_trans->dirty_pages,
25619ae4e81SJan Schmidt 			     fs_info->btree_inode->i_mapping);
25719ae4e81SJan Schmidt 	fs_info->generation++;
25819ae4e81SJan Schmidt 	cur_trans->transid = fs_info->generation;
25919ae4e81SJan Schmidt 	fs_info->running_transaction = cur_trans;
26049b25e05SJeff Mahoney 	cur_trans->aborted = 0;
26119ae4e81SJan Schmidt 	spin_unlock(&fs_info->trans_lock);
26215ee9bc7SJosef Bacik 
26379154b1bSChris Mason 	return 0;
26479154b1bSChris Mason }
26579154b1bSChris Mason 
266d352ac68SChris Mason /*
267d397712bSChris Mason  * this does all the record keeping required to make sure that a reference
268d397712bSChris Mason  * counted root is properly recorded in a given transaction.  This is required
269d397712bSChris Mason  * to make sure the old root from before we joined the transaction is deleted
270d397712bSChris Mason  * when the transaction commits
271d352ac68SChris Mason  */
2727585717fSChris Mason static int record_root_in_trans(struct btrfs_trans_handle *trans,
2735d4f98a2SYan Zheng 			       struct btrfs_root *root)
2746702ed49SChris Mason {
27527cdeb70SMiao Xie 	if (test_bit(BTRFS_ROOT_REF_COWS, &root->state) &&
27627cdeb70SMiao Xie 	    root->last_trans < trans->transid) {
2776702ed49SChris Mason 		WARN_ON(root == root->fs_info->extent_root);
2785d4f98a2SYan Zheng 		WARN_ON(root->commit_root != root->node);
2795d4f98a2SYan Zheng 
2807585717fSChris Mason 		/*
28127cdeb70SMiao Xie 		 * see below for IN_TRANS_SETUP usage rules
2827585717fSChris Mason 		 * we have the reloc mutex held now, so there
2837585717fSChris Mason 		 * is only one writer in this function
2847585717fSChris Mason 		 */
28527cdeb70SMiao Xie 		set_bit(BTRFS_ROOT_IN_TRANS_SETUP, &root->state);
2867585717fSChris Mason 
28727cdeb70SMiao Xie 		/* make sure readers find IN_TRANS_SETUP before
2887585717fSChris Mason 		 * they find our root->last_trans update
2897585717fSChris Mason 		 */
2907585717fSChris Mason 		smp_wmb();
2917585717fSChris Mason 
292a4abeea4SJosef Bacik 		spin_lock(&root->fs_info->fs_roots_radix_lock);
293a4abeea4SJosef Bacik 		if (root->last_trans == trans->transid) {
294a4abeea4SJosef Bacik 			spin_unlock(&root->fs_info->fs_roots_radix_lock);
295a4abeea4SJosef Bacik 			return 0;
296a4abeea4SJosef Bacik 		}
2976702ed49SChris Mason 		radix_tree_tag_set(&root->fs_info->fs_roots_radix,
2986702ed49SChris Mason 			   (unsigned long)root->root_key.objectid,
2996702ed49SChris Mason 			   BTRFS_ROOT_TRANS_TAG);
300a4abeea4SJosef Bacik 		spin_unlock(&root->fs_info->fs_roots_radix_lock);
3017585717fSChris Mason 		root->last_trans = trans->transid;
3027585717fSChris Mason 
3037585717fSChris Mason 		/* this is pretty tricky.  We don't want to
3047585717fSChris Mason 		 * take the relocation lock in btrfs_record_root_in_trans
3057585717fSChris Mason 		 * unless we're really doing the first setup for this root in
3067585717fSChris Mason 		 * this transaction.
3077585717fSChris Mason 		 *
3087585717fSChris Mason 		 * Normally we'd use root->last_trans as a flag to decide
3097585717fSChris Mason 		 * if we want to take the expensive mutex.
3107585717fSChris Mason 		 *
3117585717fSChris Mason 		 * But, we have to set root->last_trans before we
3127585717fSChris Mason 		 * init the relocation root, otherwise, we trip over warnings
3137585717fSChris Mason 		 * in ctree.c.  The solution used here is to flag ourselves
31427cdeb70SMiao Xie 		 * with root IN_TRANS_SETUP.  When this is 1, we're still
3157585717fSChris Mason 		 * fixing up the reloc trees and everyone must wait.
3167585717fSChris Mason 		 *
3177585717fSChris Mason 		 * When this is zero, they can trust root->last_trans and fly
3187585717fSChris Mason 		 * through btrfs_record_root_in_trans without having to take the
3197585717fSChris Mason 		 * lock.  smp_wmb() makes sure that all the writes above are
3207585717fSChris Mason 		 * done before we pop in the zero below
3217585717fSChris Mason 		 */
3225d4f98a2SYan Zheng 		btrfs_init_reloc_root(trans, root);
323c7548af6SChris Mason 		smp_mb__before_atomic();
32427cdeb70SMiao Xie 		clear_bit(BTRFS_ROOT_IN_TRANS_SETUP, &root->state);
3256702ed49SChris Mason 	}
3265d4f98a2SYan Zheng 	return 0;
3276702ed49SChris Mason }
3285d4f98a2SYan Zheng 
3297585717fSChris Mason 
3307585717fSChris Mason int btrfs_record_root_in_trans(struct btrfs_trans_handle *trans,
3317585717fSChris Mason 			       struct btrfs_root *root)
3327585717fSChris Mason {
33327cdeb70SMiao Xie 	if (!test_bit(BTRFS_ROOT_REF_COWS, &root->state))
3347585717fSChris Mason 		return 0;
3357585717fSChris Mason 
3367585717fSChris Mason 	/*
33727cdeb70SMiao Xie 	 * see record_root_in_trans for comments about IN_TRANS_SETUP usage
3387585717fSChris Mason 	 * and barriers
3397585717fSChris Mason 	 */
3407585717fSChris Mason 	smp_rmb();
3417585717fSChris Mason 	if (root->last_trans == trans->transid &&
34227cdeb70SMiao Xie 	    !test_bit(BTRFS_ROOT_IN_TRANS_SETUP, &root->state))
3437585717fSChris Mason 		return 0;
3447585717fSChris Mason 
3457585717fSChris Mason 	mutex_lock(&root->fs_info->reloc_mutex);
3467585717fSChris Mason 	record_root_in_trans(trans, root);
3477585717fSChris Mason 	mutex_unlock(&root->fs_info->reloc_mutex);
3487585717fSChris Mason 
3497585717fSChris Mason 	return 0;
3507585717fSChris Mason }
3517585717fSChris Mason 
3524a9d8bdeSMiao Xie static inline int is_transaction_blocked(struct btrfs_transaction *trans)
3534a9d8bdeSMiao Xie {
3544a9d8bdeSMiao Xie 	return (trans->state >= TRANS_STATE_BLOCKED &&
355501407aaSJosef Bacik 		trans->state < TRANS_STATE_UNBLOCKED &&
356501407aaSJosef Bacik 		!trans->aborted);
3574a9d8bdeSMiao Xie }
3584a9d8bdeSMiao Xie 
359d352ac68SChris Mason /* wait for commit against the current transaction to become unblocked
360d352ac68SChris Mason  * when this is done, it is safe to start a new transaction, but the current
361d352ac68SChris Mason  * transaction might not be fully on disk.
362d352ac68SChris Mason  */
36337d1aeeeSChris Mason static void wait_current_trans(struct btrfs_root *root)
36479154b1bSChris Mason {
365f9295749SChris Mason 	struct btrfs_transaction *cur_trans;
36679154b1bSChris Mason 
367a4abeea4SJosef Bacik 	spin_lock(&root->fs_info->trans_lock);
368f9295749SChris Mason 	cur_trans = root->fs_info->running_transaction;
3694a9d8bdeSMiao Xie 	if (cur_trans && is_transaction_blocked(cur_trans)) {
37013c5a93eSJosef Bacik 		atomic_inc(&cur_trans->use_count);
371a4abeea4SJosef Bacik 		spin_unlock(&root->fs_info->trans_lock);
37272d63ed6SLi Zefan 
37372d63ed6SLi Zefan 		wait_event(root->fs_info->transaction_wait,
374501407aaSJosef Bacik 			   cur_trans->state >= TRANS_STATE_UNBLOCKED ||
375501407aaSJosef Bacik 			   cur_trans->aborted);
376724e2315SJosef Bacik 		btrfs_put_transaction(cur_trans);
377a4abeea4SJosef Bacik 	} else {
378a4abeea4SJosef Bacik 		spin_unlock(&root->fs_info->trans_lock);
379f9295749SChris Mason 	}
38037d1aeeeSChris Mason }
38137d1aeeeSChris Mason 
382a22285a6SYan, Zheng static int may_wait_transaction(struct btrfs_root *root, int type)
38337d1aeeeSChris Mason {
384a4abeea4SJosef Bacik 	if (root->fs_info->log_root_recovering)
385a4abeea4SJosef Bacik 		return 0;
386a4abeea4SJosef Bacik 
387a4abeea4SJosef Bacik 	if (type == TRANS_USERSPACE)
388a22285a6SYan, Zheng 		return 1;
389a4abeea4SJosef Bacik 
390a4abeea4SJosef Bacik 	if (type == TRANS_START &&
391a4abeea4SJosef Bacik 	    !atomic_read(&root->fs_info->open_ioctl_trans))
392a4abeea4SJosef Bacik 		return 1;
393a4abeea4SJosef Bacik 
394a22285a6SYan, Zheng 	return 0;
395a22285a6SYan, Zheng }
396a22285a6SYan, Zheng 
39720dd2cbfSMiao Xie static inline bool need_reserve_reloc_root(struct btrfs_root *root)
39820dd2cbfSMiao Xie {
39920dd2cbfSMiao Xie 	if (!root->fs_info->reloc_ctl ||
40027cdeb70SMiao Xie 	    !test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
40120dd2cbfSMiao Xie 	    root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID ||
40220dd2cbfSMiao Xie 	    root->reloc_root)
40320dd2cbfSMiao Xie 		return false;
40420dd2cbfSMiao Xie 
40520dd2cbfSMiao Xie 	return true;
40620dd2cbfSMiao Xie }
40720dd2cbfSMiao Xie 
40808e007d2SMiao Xie static struct btrfs_trans_handle *
4090860adfdSMiao Xie start_transaction(struct btrfs_root *root, u64 num_items, unsigned int type,
41008e007d2SMiao Xie 		  enum btrfs_reserve_flush_enum flush)
411a22285a6SYan, Zheng {
412a22285a6SYan, Zheng 	struct btrfs_trans_handle *h;
413a22285a6SYan, Zheng 	struct btrfs_transaction *cur_trans;
414b5009945SJosef Bacik 	u64 num_bytes = 0;
415c5567237SArne Jansen 	u64 qgroup_reserved = 0;
41620dd2cbfSMiao Xie 	bool reloc_reserved = false;
41720dd2cbfSMiao Xie 	int ret;
418acce952bSliubo 
41946c4e71eSFilipe Manana 	/* Send isn't supposed to start transactions. */
4202755a0deSDavid Sterba 	ASSERT(current->journal_info != BTRFS_SEND_TRANS_STUB);
42146c4e71eSFilipe Manana 
42287533c47SMiao Xie 	if (test_bit(BTRFS_FS_STATE_ERROR, &root->fs_info->fs_state))
423acce952bSliubo 		return ERR_PTR(-EROFS);
4242a1eb461SJosef Bacik 
42546c4e71eSFilipe Manana 	if (current->journal_info) {
4260860adfdSMiao Xie 		WARN_ON(type & TRANS_EXTWRITERS);
4272a1eb461SJosef Bacik 		h = current->journal_info;
4282a1eb461SJosef Bacik 		h->use_count++;
429b7d5b0a8SMiao Xie 		WARN_ON(h->use_count > 2);
4302a1eb461SJosef Bacik 		h->orig_rsv = h->block_rsv;
4312a1eb461SJosef Bacik 		h->block_rsv = NULL;
4322a1eb461SJosef Bacik 		goto got_it;
4332a1eb461SJosef Bacik 	}
434b5009945SJosef Bacik 
435b5009945SJosef Bacik 	/*
436b5009945SJosef Bacik 	 * Do the reservation before we join the transaction so we can do all
437b5009945SJosef Bacik 	 * the appropriate flushing if need be.
438b5009945SJosef Bacik 	 */
439b5009945SJosef Bacik 	if (num_items > 0 && root != root->fs_info->chunk_root) {
440c5567237SArne Jansen 		if (root->fs_info->quota_enabled &&
441c5567237SArne Jansen 		    is_fstree(root->root_key.objectid)) {
442707e8a07SDavid Sterba 			qgroup_reserved = num_items * root->nodesize;
443c5567237SArne Jansen 			ret = btrfs_qgroup_reserve(root, qgroup_reserved);
444c5567237SArne Jansen 			if (ret)
445c5567237SArne Jansen 				return ERR_PTR(ret);
446c5567237SArne Jansen 		}
447c5567237SArne Jansen 
448b5009945SJosef Bacik 		num_bytes = btrfs_calc_trans_metadata_size(root, num_items);
44920dd2cbfSMiao Xie 		/*
45020dd2cbfSMiao Xie 		 * Do the reservation for the relocation root creation
45120dd2cbfSMiao Xie 		 */
452ee39b432SDavid Sterba 		if (need_reserve_reloc_root(root)) {
45320dd2cbfSMiao Xie 			num_bytes += root->nodesize;
45420dd2cbfSMiao Xie 			reloc_reserved = true;
45520dd2cbfSMiao Xie 		}
45620dd2cbfSMiao Xie 
4574a92b1b8SJosef Bacik 		ret = btrfs_block_rsv_add(root,
458b5009945SJosef Bacik 					  &root->fs_info->trans_block_rsv,
45908e007d2SMiao Xie 					  num_bytes, flush);
460b5009945SJosef Bacik 		if (ret)
461843fcf35SMiao Xie 			goto reserve_fail;
462b5009945SJosef Bacik 	}
463a22285a6SYan, Zheng again:
464a22285a6SYan, Zheng 	h = kmem_cache_alloc(btrfs_trans_handle_cachep, GFP_NOFS);
465843fcf35SMiao Xie 	if (!h) {
466843fcf35SMiao Xie 		ret = -ENOMEM;
467843fcf35SMiao Xie 		goto alloc_fail;
468843fcf35SMiao Xie 	}
469a22285a6SYan, Zheng 
47098114659SJosef Bacik 	/*
47198114659SJosef Bacik 	 * If we are JOIN_NOLOCK we're already committing a transaction and
47298114659SJosef Bacik 	 * waiting on this guy, so we don't need to do the sb_start_intwrite
47398114659SJosef Bacik 	 * because we're already holding a ref.  We need this because we could
47498114659SJosef Bacik 	 * have raced in and did an fsync() on a file which can kick a commit
47598114659SJosef Bacik 	 * and then we deadlock with somebody doing a freeze.
476354aa0fbSMiao Xie 	 *
477354aa0fbSMiao Xie 	 * If we are ATTACH, it means we just want to catch the current
478354aa0fbSMiao Xie 	 * transaction and commit it, so we needn't do sb_start_intwrite().
47998114659SJosef Bacik 	 */
4800860adfdSMiao Xie 	if (type & __TRANS_FREEZABLE)
481b2b5ef5cSJan Kara 		sb_start_intwrite(root->fs_info->sb);
482b2b5ef5cSJan Kara 
483a22285a6SYan, Zheng 	if (may_wait_transaction(root, type))
48437d1aeeeSChris Mason 		wait_current_trans(root);
485a22285a6SYan, Zheng 
486a4abeea4SJosef Bacik 	do {
487354aa0fbSMiao Xie 		ret = join_transaction(root, type);
488178260b2SMiao Xie 		if (ret == -EBUSY) {
489a4abeea4SJosef Bacik 			wait_current_trans(root);
490178260b2SMiao Xie 			if (unlikely(type == TRANS_ATTACH))
491178260b2SMiao Xie 				ret = -ENOENT;
492178260b2SMiao Xie 		}
493a4abeea4SJosef Bacik 	} while (ret == -EBUSY);
494a4abeea4SJosef Bacik 
495db5b493aSTsutomu Itoh 	if (ret < 0) {
496354aa0fbSMiao Xie 		/* We must get the transaction if we are JOIN_NOLOCK. */
497354aa0fbSMiao Xie 		BUG_ON(type == TRANS_JOIN_NOLOCK);
498843fcf35SMiao Xie 		goto join_fail;
499db5b493aSTsutomu Itoh 	}
5000f7d52f4SChris Mason 
501a22285a6SYan, Zheng 	cur_trans = root->fs_info->running_transaction;
502a22285a6SYan, Zheng 
503a22285a6SYan, Zheng 	h->transid = cur_trans->transid;
504a22285a6SYan, Zheng 	h->transaction = cur_trans;
50579154b1bSChris Mason 	h->blocks_used = 0;
506a22285a6SYan, Zheng 	h->bytes_reserved = 0;
507d13603efSArne Jansen 	h->root = root;
50856bec294SChris Mason 	h->delayed_ref_updates = 0;
5092a1eb461SJosef Bacik 	h->use_count = 1;
5100e721106SJosef Bacik 	h->adding_csums = 0;
511f0486c68SYan, Zheng 	h->block_rsv = NULL;
5122a1eb461SJosef Bacik 	h->orig_rsv = NULL;
51349b25e05SJeff Mahoney 	h->aborted = 0;
5144b824906SMiao Xie 	h->qgroup_reserved = 0;
515bed92eaeSArne Jansen 	h->delayed_ref_elem.seq = 0;
516a698d075SMiao Xie 	h->type = type;
517c6b305a8SJosef Bacik 	h->allocating_chunk = false;
51820dd2cbfSMiao Xie 	h->reloc_reserved = false;
5195039eddcSJosef Bacik 	h->sync = false;
520bed92eaeSArne Jansen 	INIT_LIST_HEAD(&h->qgroup_ref_list);
521ea658badSJosef Bacik 	INIT_LIST_HEAD(&h->new_bgs);
52250d9aa99SJosef Bacik 	INIT_LIST_HEAD(&h->ordered);
523b7ec40d7SChris Mason 
524a22285a6SYan, Zheng 	smp_mb();
5254a9d8bdeSMiao Xie 	if (cur_trans->state >= TRANS_STATE_BLOCKED &&
5264a9d8bdeSMiao Xie 	    may_wait_transaction(root, type)) {
527abdd2e80SFilipe Manana 		current->journal_info = h;
528a22285a6SYan, Zheng 		btrfs_commit_transaction(h, root);
529a22285a6SYan, Zheng 		goto again;
530a22285a6SYan, Zheng 	}
5319ed74f2dSJosef Bacik 
532b5009945SJosef Bacik 	if (num_bytes) {
5338c2a3ca2SJosef Bacik 		trace_btrfs_space_reservation(root->fs_info, "transaction",
5342bcc0328SLiu Bo 					      h->transid, num_bytes, 1);
535b5009945SJosef Bacik 		h->block_rsv = &root->fs_info->trans_block_rsv;
536b5009945SJosef Bacik 		h->bytes_reserved = num_bytes;
53720dd2cbfSMiao Xie 		h->reloc_reserved = reloc_reserved;
538a22285a6SYan, Zheng 	}
5394b824906SMiao Xie 	h->qgroup_reserved = qgroup_reserved;
540a22285a6SYan, Zheng 
5412a1eb461SJosef Bacik got_it:
542a4abeea4SJosef Bacik 	btrfs_record_root_in_trans(h, root);
543a22285a6SYan, Zheng 
544a22285a6SYan, Zheng 	if (!current->journal_info && type != TRANS_USERSPACE)
545a22285a6SYan, Zheng 		current->journal_info = h;
54679154b1bSChris Mason 	return h;
547843fcf35SMiao Xie 
548843fcf35SMiao Xie join_fail:
5490860adfdSMiao Xie 	if (type & __TRANS_FREEZABLE)
550843fcf35SMiao Xie 		sb_end_intwrite(root->fs_info->sb);
551843fcf35SMiao Xie 	kmem_cache_free(btrfs_trans_handle_cachep, h);
552843fcf35SMiao Xie alloc_fail:
553843fcf35SMiao Xie 	if (num_bytes)
554843fcf35SMiao Xie 		btrfs_block_rsv_release(root, &root->fs_info->trans_block_rsv,
555843fcf35SMiao Xie 					num_bytes);
556843fcf35SMiao Xie reserve_fail:
557843fcf35SMiao Xie 	if (qgroup_reserved)
558843fcf35SMiao Xie 		btrfs_qgroup_free(root, qgroup_reserved);
559843fcf35SMiao Xie 	return ERR_PTR(ret);
56079154b1bSChris Mason }
56179154b1bSChris Mason 
562f9295749SChris Mason struct btrfs_trans_handle *btrfs_start_transaction(struct btrfs_root *root,
563a22285a6SYan, Zheng 						   int num_items)
564f9295749SChris Mason {
56508e007d2SMiao Xie 	return start_transaction(root, num_items, TRANS_START,
56608e007d2SMiao Xie 				 BTRFS_RESERVE_FLUSH_ALL);
567f9295749SChris Mason }
5688407aa46SMiao Xie 
56908e007d2SMiao Xie struct btrfs_trans_handle *btrfs_start_transaction_lflush(
5708407aa46SMiao Xie 					struct btrfs_root *root, int num_items)
5718407aa46SMiao Xie {
57208e007d2SMiao Xie 	return start_transaction(root, num_items, TRANS_START,
57308e007d2SMiao Xie 				 BTRFS_RESERVE_FLUSH_LIMIT);
5748407aa46SMiao Xie }
5758407aa46SMiao Xie 
5767a7eaa40SJosef Bacik struct btrfs_trans_handle *btrfs_join_transaction(struct btrfs_root *root)
577f9295749SChris Mason {
5788407aa46SMiao Xie 	return start_transaction(root, 0, TRANS_JOIN, 0);
579f9295749SChris Mason }
580f9295749SChris Mason 
5817a7eaa40SJosef Bacik struct btrfs_trans_handle *btrfs_join_transaction_nolock(struct btrfs_root *root)
5820af3d00bSJosef Bacik {
5838407aa46SMiao Xie 	return start_transaction(root, 0, TRANS_JOIN_NOLOCK, 0);
5840af3d00bSJosef Bacik }
5850af3d00bSJosef Bacik 
5867a7eaa40SJosef Bacik struct btrfs_trans_handle *btrfs_start_ioctl_transaction(struct btrfs_root *root)
5879ca9ee09SSage Weil {
5888407aa46SMiao Xie 	return start_transaction(root, 0, TRANS_USERSPACE, 0);
5899ca9ee09SSage Weil }
5909ca9ee09SSage Weil 
591d4edf39bSMiao Xie /*
592d4edf39bSMiao Xie  * btrfs_attach_transaction() - catch the running transaction
593d4edf39bSMiao Xie  *
594d4edf39bSMiao Xie  * It is used when we want to commit the current the transaction, but
595d4edf39bSMiao Xie  * don't want to start a new one.
596d4edf39bSMiao Xie  *
597d4edf39bSMiao Xie  * Note: If this function return -ENOENT, it just means there is no
598d4edf39bSMiao Xie  * running transaction. But it is possible that the inactive transaction
599d4edf39bSMiao Xie  * is still in the memory, not fully on disk. If you hope there is no
600d4edf39bSMiao Xie  * inactive transaction in the fs when -ENOENT is returned, you should
601d4edf39bSMiao Xie  * invoke
602d4edf39bSMiao Xie  *     btrfs_attach_transaction_barrier()
603d4edf39bSMiao Xie  */
604354aa0fbSMiao Xie struct btrfs_trans_handle *btrfs_attach_transaction(struct btrfs_root *root)
60560376ce4SJosef Bacik {
606354aa0fbSMiao Xie 	return start_transaction(root, 0, TRANS_ATTACH, 0);
60760376ce4SJosef Bacik }
60860376ce4SJosef Bacik 
609d4edf39bSMiao Xie /*
61090b6d283SWang Sheng-Hui  * btrfs_attach_transaction_barrier() - catch the running transaction
611d4edf39bSMiao Xie  *
612d4edf39bSMiao Xie  * It is similar to the above function, the differentia is this one
613d4edf39bSMiao Xie  * will wait for all the inactive transactions until they fully
614d4edf39bSMiao Xie  * complete.
615d4edf39bSMiao Xie  */
616d4edf39bSMiao Xie struct btrfs_trans_handle *
617d4edf39bSMiao Xie btrfs_attach_transaction_barrier(struct btrfs_root *root)
618d4edf39bSMiao Xie {
619d4edf39bSMiao Xie 	struct btrfs_trans_handle *trans;
620d4edf39bSMiao Xie 
621d4edf39bSMiao Xie 	trans = start_transaction(root, 0, TRANS_ATTACH, 0);
622d4edf39bSMiao Xie 	if (IS_ERR(trans) && PTR_ERR(trans) == -ENOENT)
623d4edf39bSMiao Xie 		btrfs_wait_for_commit(root, 0);
624d4edf39bSMiao Xie 
625d4edf39bSMiao Xie 	return trans;
626d4edf39bSMiao Xie }
627d4edf39bSMiao Xie 
628d352ac68SChris Mason /* wait for a transaction commit to be fully complete */
629b9c8300cSLi Zefan static noinline void wait_for_commit(struct btrfs_root *root,
63089ce8a63SChris Mason 				    struct btrfs_transaction *commit)
63189ce8a63SChris Mason {
6324a9d8bdeSMiao Xie 	wait_event(commit->commit_wait, commit->state == TRANS_STATE_COMPLETED);
63389ce8a63SChris Mason }
63489ce8a63SChris Mason 
63546204592SSage Weil int btrfs_wait_for_commit(struct btrfs_root *root, u64 transid)
63646204592SSage Weil {
63746204592SSage Weil 	struct btrfs_transaction *cur_trans = NULL, *t;
6388cd2807fSMiao Xie 	int ret = 0;
63946204592SSage Weil 
64046204592SSage Weil 	if (transid) {
64146204592SSage Weil 		if (transid <= root->fs_info->last_trans_committed)
642a4abeea4SJosef Bacik 			goto out;
64346204592SSage Weil 
64446204592SSage Weil 		/* find specified transaction */
645a4abeea4SJosef Bacik 		spin_lock(&root->fs_info->trans_lock);
64646204592SSage Weil 		list_for_each_entry(t, &root->fs_info->trans_list, list) {
64746204592SSage Weil 			if (t->transid == transid) {
64846204592SSage Weil 				cur_trans = t;
649a4abeea4SJosef Bacik 				atomic_inc(&cur_trans->use_count);
6508cd2807fSMiao Xie 				ret = 0;
65146204592SSage Weil 				break;
65246204592SSage Weil 			}
6538cd2807fSMiao Xie 			if (t->transid > transid) {
6548cd2807fSMiao Xie 				ret = 0;
65546204592SSage Weil 				break;
65646204592SSage Weil 			}
6578cd2807fSMiao Xie 		}
658a4abeea4SJosef Bacik 		spin_unlock(&root->fs_info->trans_lock);
65942383020SSage Weil 
66042383020SSage Weil 		/*
66142383020SSage Weil 		 * The specified transaction doesn't exist, or we
66242383020SSage Weil 		 * raced with btrfs_commit_transaction
66342383020SSage Weil 		 */
66442383020SSage Weil 		if (!cur_trans) {
66542383020SSage Weil 			if (transid > root->fs_info->last_trans_committed)
66642383020SSage Weil 				ret = -EINVAL;
6678cd2807fSMiao Xie 			goto out;
66842383020SSage Weil 		}
66946204592SSage Weil 	} else {
67046204592SSage Weil 		/* find newest transaction that is committing | committed */
671a4abeea4SJosef Bacik 		spin_lock(&root->fs_info->trans_lock);
67246204592SSage Weil 		list_for_each_entry_reverse(t, &root->fs_info->trans_list,
67346204592SSage Weil 					    list) {
6744a9d8bdeSMiao Xie 			if (t->state >= TRANS_STATE_COMMIT_START) {
6754a9d8bdeSMiao Xie 				if (t->state == TRANS_STATE_COMPLETED)
6763473f3c0SJosef Bacik 					break;
67746204592SSage Weil 				cur_trans = t;
678a4abeea4SJosef Bacik 				atomic_inc(&cur_trans->use_count);
67946204592SSage Weil 				break;
68046204592SSage Weil 			}
68146204592SSage Weil 		}
682a4abeea4SJosef Bacik 		spin_unlock(&root->fs_info->trans_lock);
68346204592SSage Weil 		if (!cur_trans)
684a4abeea4SJosef Bacik 			goto out;  /* nothing committing|committed */
68546204592SSage Weil 	}
68646204592SSage Weil 
68746204592SSage Weil 	wait_for_commit(root, cur_trans);
688724e2315SJosef Bacik 	btrfs_put_transaction(cur_trans);
689a4abeea4SJosef Bacik out:
69046204592SSage Weil 	return ret;
69146204592SSage Weil }
69246204592SSage Weil 
69337d1aeeeSChris Mason void btrfs_throttle(struct btrfs_root *root)
69437d1aeeeSChris Mason {
695a4abeea4SJosef Bacik 	if (!atomic_read(&root->fs_info->open_ioctl_trans))
69637d1aeeeSChris Mason 		wait_current_trans(root);
69737d1aeeeSChris Mason }
69837d1aeeeSChris Mason 
6998929ecfaSYan, Zheng static int should_end_transaction(struct btrfs_trans_handle *trans,
7008929ecfaSYan, Zheng 				  struct btrfs_root *root)
7018929ecfaSYan, Zheng {
7021be41b78SJosef Bacik 	if (root->fs_info->global_block_rsv.space_info->full &&
7030a2b2a84SJosef Bacik 	    btrfs_check_space_for_delayed_refs(trans, root))
7041be41b78SJosef Bacik 		return 1;
70536ba022aSJosef Bacik 
7061be41b78SJosef Bacik 	return !!btrfs_block_rsv_check(root, &root->fs_info->global_block_rsv, 5);
7078929ecfaSYan, Zheng }
7088929ecfaSYan, Zheng 
7098929ecfaSYan, Zheng int btrfs_should_end_transaction(struct btrfs_trans_handle *trans,
7108929ecfaSYan, Zheng 				 struct btrfs_root *root)
7118929ecfaSYan, Zheng {
7128929ecfaSYan, Zheng 	struct btrfs_transaction *cur_trans = trans->transaction;
7138929ecfaSYan, Zheng 	int updates;
71449b25e05SJeff Mahoney 	int err;
7158929ecfaSYan, Zheng 
716a4abeea4SJosef Bacik 	smp_mb();
7174a9d8bdeSMiao Xie 	if (cur_trans->state >= TRANS_STATE_BLOCKED ||
7184a9d8bdeSMiao Xie 	    cur_trans->delayed_refs.flushing)
7198929ecfaSYan, Zheng 		return 1;
7208929ecfaSYan, Zheng 
7218929ecfaSYan, Zheng 	updates = trans->delayed_ref_updates;
7228929ecfaSYan, Zheng 	trans->delayed_ref_updates = 0;
72349b25e05SJeff Mahoney 	if (updates) {
72449b25e05SJeff Mahoney 		err = btrfs_run_delayed_refs(trans, root, updates);
72549b25e05SJeff Mahoney 		if (err) /* Error code will also eval true */
72649b25e05SJeff Mahoney 			return err;
72749b25e05SJeff Mahoney 	}
7288929ecfaSYan, Zheng 
7298929ecfaSYan, Zheng 	return should_end_transaction(trans, root);
7308929ecfaSYan, Zheng }
7318929ecfaSYan, Zheng 
73289ce8a63SChris Mason static int __btrfs_end_transaction(struct btrfs_trans_handle *trans,
733a698d075SMiao Xie 			  struct btrfs_root *root, int throttle)
73479154b1bSChris Mason {
7358929ecfaSYan, Zheng 	struct btrfs_transaction *cur_trans = trans->transaction;
736ab78c84dSChris Mason 	struct btrfs_fs_info *info = root->fs_info;
7371be41b78SJosef Bacik 	unsigned long cur = trans->delayed_ref_updates;
738a698d075SMiao Xie 	int lock = (trans->type != TRANS_JOIN_NOLOCK);
7394edc2ca3SDave Jones 	int err = 0;
740a79b7d4bSChris Mason 	int must_run_delayed_refs = 0;
741d6e4a428SChris Mason 
7423bbb24b2SJosef Bacik 	if (trans->use_count > 1) {
7433bbb24b2SJosef Bacik 		trans->use_count--;
7442a1eb461SJosef Bacik 		trans->block_rsv = trans->orig_rsv;
7452a1eb461SJosef Bacik 		return 0;
7462a1eb461SJosef Bacik 	}
7472a1eb461SJosef Bacik 
748b24e03dbSJosef Bacik 	btrfs_trans_release_metadata(trans, root);
7494c13d758SJosef Bacik 	trans->block_rsv = NULL;
750c5567237SArne Jansen 
751ea658badSJosef Bacik 	if (!list_empty(&trans->new_bgs))
752ea658badSJosef Bacik 		btrfs_create_pending_block_groups(trans, root);
753ea658badSJosef Bacik 
75450d9aa99SJosef Bacik 	if (!list_empty(&trans->ordered)) {
75550d9aa99SJosef Bacik 		spin_lock(&info->trans_lock);
75650d9aa99SJosef Bacik 		list_splice(&trans->ordered, &cur_trans->pending_ordered);
75750d9aa99SJosef Bacik 		spin_unlock(&info->trans_lock);
75850d9aa99SJosef Bacik 	}
75950d9aa99SJosef Bacik 
760c3e69d58SChris Mason 	trans->delayed_ref_updates = 0;
761a79b7d4bSChris Mason 	if (!trans->sync) {
762a79b7d4bSChris Mason 		must_run_delayed_refs =
763a79b7d4bSChris Mason 			btrfs_should_throttle_delayed_refs(trans, root);
7640a2b2a84SJosef Bacik 		cur = max_t(unsigned long, cur, 32);
765a79b7d4bSChris Mason 
766a79b7d4bSChris Mason 		/*
767a79b7d4bSChris Mason 		 * don't make the caller wait if they are from a NOLOCK
768a79b7d4bSChris Mason 		 * or ATTACH transaction, it will deadlock with commit
769a79b7d4bSChris Mason 		 */
770a79b7d4bSChris Mason 		if (must_run_delayed_refs == 1 &&
771a79b7d4bSChris Mason 		    (trans->type & (__TRANS_JOIN_NOLOCK | __TRANS_ATTACH)))
772a79b7d4bSChris Mason 			must_run_delayed_refs = 2;
77356bec294SChris Mason 	}
774bb721703SChris Mason 
775fcebe456SJosef Bacik 	if (trans->qgroup_reserved) {
776fcebe456SJosef Bacik 		/*
777fcebe456SJosef Bacik 		 * the same root has to be passed here between start_transaction
778fcebe456SJosef Bacik 		 * and end_transaction. Subvolume quota depends on this.
779fcebe456SJosef Bacik 		 */
780fcebe456SJosef Bacik 		btrfs_qgroup_free(trans->root, trans->qgroup_reserved);
781fcebe456SJosef Bacik 		trans->qgroup_reserved = 0;
782fcebe456SJosef Bacik 	}
783fcebe456SJosef Bacik 
7840e721106SJosef Bacik 	btrfs_trans_release_metadata(trans, root);
7850e721106SJosef Bacik 	trans->block_rsv = NULL;
78656bec294SChris Mason 
787ea658badSJosef Bacik 	if (!list_empty(&trans->new_bgs))
788ea658badSJosef Bacik 		btrfs_create_pending_block_groups(trans, root);
789ea658badSJosef Bacik 
790a4abeea4SJosef Bacik 	if (lock && !atomic_read(&root->fs_info->open_ioctl_trans) &&
7914a9d8bdeSMiao Xie 	    should_end_transaction(trans, root) &&
7924a9d8bdeSMiao Xie 	    ACCESS_ONCE(cur_trans->state) == TRANS_STATE_RUNNING) {
7934a9d8bdeSMiao Xie 		spin_lock(&info->trans_lock);
7944a9d8bdeSMiao Xie 		if (cur_trans->state == TRANS_STATE_RUNNING)
7954a9d8bdeSMiao Xie 			cur_trans->state = TRANS_STATE_BLOCKED;
7964a9d8bdeSMiao Xie 		spin_unlock(&info->trans_lock);
797a4abeea4SJosef Bacik 	}
7988929ecfaSYan, Zheng 
7994a9d8bdeSMiao Xie 	if (lock && ACCESS_ONCE(cur_trans->state) == TRANS_STATE_BLOCKED) {
8003bbb24b2SJosef Bacik 		if (throttle)
8018929ecfaSYan, Zheng 			return btrfs_commit_transaction(trans, root);
8023bbb24b2SJosef Bacik 		else
8038929ecfaSYan, Zheng 			wake_up_process(info->transaction_kthread);
8048929ecfaSYan, Zheng 	}
8058929ecfaSYan, Zheng 
8060860adfdSMiao Xie 	if (trans->type & __TRANS_FREEZABLE)
8076df7881aSJosef Bacik 		sb_end_intwrite(root->fs_info->sb);
8086df7881aSJosef Bacik 
8098929ecfaSYan, Zheng 	WARN_ON(cur_trans != info->running_transaction);
81013c5a93eSJosef Bacik 	WARN_ON(atomic_read(&cur_trans->num_writers) < 1);
81113c5a93eSJosef Bacik 	atomic_dec(&cur_trans->num_writers);
8120860adfdSMiao Xie 	extwriter_counter_dec(cur_trans, trans->type);
81389ce8a63SChris Mason 
81499d16cbcSSage Weil 	smp_mb();
81579154b1bSChris Mason 	if (waitqueue_active(&cur_trans->writer_wait))
81679154b1bSChris Mason 		wake_up(&cur_trans->writer_wait);
817724e2315SJosef Bacik 	btrfs_put_transaction(cur_trans);
8189ed74f2dSJosef Bacik 
8199ed74f2dSJosef Bacik 	if (current->journal_info == trans)
8209ed74f2dSJosef Bacik 		current->journal_info = NULL;
821ab78c84dSChris Mason 
82224bbcf04SYan, Zheng 	if (throttle)
82324bbcf04SYan, Zheng 		btrfs_run_delayed_iputs(root);
82424bbcf04SYan, Zheng 
82549b25e05SJeff Mahoney 	if (trans->aborted ||
8264e121c06SJosef Bacik 	    test_bit(BTRFS_FS_STATE_ERROR, &root->fs_info->fs_state)) {
8274e121c06SJosef Bacik 		wake_up_process(info->transaction_kthread);
8284edc2ca3SDave Jones 		err = -EIO;
8294e121c06SJosef Bacik 	}
830edf39272SJan Schmidt 	assert_qgroups_uptodate(trans);
83149b25e05SJeff Mahoney 
8324edc2ca3SDave Jones 	kmem_cache_free(btrfs_trans_handle_cachep, trans);
833a79b7d4bSChris Mason 	if (must_run_delayed_refs) {
834a79b7d4bSChris Mason 		btrfs_async_run_delayed_refs(root, cur,
835a79b7d4bSChris Mason 					     must_run_delayed_refs == 1);
836a79b7d4bSChris Mason 	}
8374edc2ca3SDave Jones 	return err;
83879154b1bSChris Mason }
83979154b1bSChris Mason 
84089ce8a63SChris Mason int btrfs_end_transaction(struct btrfs_trans_handle *trans,
84189ce8a63SChris Mason 			  struct btrfs_root *root)
84289ce8a63SChris Mason {
84398ad43beSWang Shilong 	return __btrfs_end_transaction(trans, root, 0);
84489ce8a63SChris Mason }
84589ce8a63SChris Mason 
84689ce8a63SChris Mason int btrfs_end_transaction_throttle(struct btrfs_trans_handle *trans,
84789ce8a63SChris Mason 				   struct btrfs_root *root)
84889ce8a63SChris Mason {
84998ad43beSWang Shilong 	return __btrfs_end_transaction(trans, root, 1);
85016cdcec7SMiao Xie }
85116cdcec7SMiao Xie 
852d352ac68SChris Mason /*
853d352ac68SChris Mason  * when btree blocks are allocated, they have some corresponding bits set for
854d352ac68SChris Mason  * them in one of two extent_io trees.  This is used to make sure all of
855690587d1SChris Mason  * those extents are sent to disk but does not wait on them
856d352ac68SChris Mason  */
857690587d1SChris Mason int btrfs_write_marked_extents(struct btrfs_root *root,
8588cef4e16SYan, Zheng 			       struct extent_io_tree *dirty_pages, int mark)
85979154b1bSChris Mason {
860777e6bd7SChris Mason 	int err = 0;
8617c4452b9SChris Mason 	int werr = 0;
8621728366eSJosef Bacik 	struct address_space *mapping = root->fs_info->btree_inode->i_mapping;
863e6138876SJosef Bacik 	struct extent_state *cached_state = NULL;
864777e6bd7SChris Mason 	u64 start = 0;
8655f39d397SChris Mason 	u64 end;
8667c4452b9SChris Mason 
8671728366eSJosef Bacik 	while (!find_first_extent_bit(dirty_pages, start, &start, &end,
868e6138876SJosef Bacik 				      mark, &cached_state)) {
869663dfbb0SFilipe Manana 		bool wait_writeback = false;
870663dfbb0SFilipe Manana 
871663dfbb0SFilipe Manana 		err = convert_extent_bit(dirty_pages, start, end,
872663dfbb0SFilipe Manana 					 EXTENT_NEED_WAIT,
873e6138876SJosef Bacik 					 mark, &cached_state, GFP_NOFS);
874663dfbb0SFilipe Manana 		/*
875663dfbb0SFilipe Manana 		 * convert_extent_bit can return -ENOMEM, which is most of the
876663dfbb0SFilipe Manana 		 * time a temporary error. So when it happens, ignore the error
877663dfbb0SFilipe Manana 		 * and wait for writeback of this range to finish - because we
878663dfbb0SFilipe Manana 		 * failed to set the bit EXTENT_NEED_WAIT for the range, a call
879663dfbb0SFilipe Manana 		 * to btrfs_wait_marked_extents() would not know that writeback
880663dfbb0SFilipe Manana 		 * for this range started and therefore wouldn't wait for it to
881663dfbb0SFilipe Manana 		 * finish - we don't want to commit a superblock that points to
882663dfbb0SFilipe Manana 		 * btree nodes/leafs for which writeback hasn't finished yet
883663dfbb0SFilipe Manana 		 * (and without errors).
884663dfbb0SFilipe Manana 		 * We cleanup any entries left in the io tree when committing
885663dfbb0SFilipe Manana 		 * the transaction (through clear_btree_io_tree()).
886663dfbb0SFilipe Manana 		 */
887663dfbb0SFilipe Manana 		if (err == -ENOMEM) {
888663dfbb0SFilipe Manana 			err = 0;
889663dfbb0SFilipe Manana 			wait_writeback = true;
890663dfbb0SFilipe Manana 		}
891663dfbb0SFilipe Manana 		if (!err)
8921728366eSJosef Bacik 			err = filemap_fdatawrite_range(mapping, start, end);
8937c4452b9SChris Mason 		if (err)
8947c4452b9SChris Mason 			werr = err;
895663dfbb0SFilipe Manana 		else if (wait_writeback)
896663dfbb0SFilipe Manana 			werr = filemap_fdatawait_range(mapping, start, end);
897e38e2ed7SFilipe Manana 		free_extent_state(cached_state);
898663dfbb0SFilipe Manana 		cached_state = NULL;
8991728366eSJosef Bacik 		cond_resched();
9001728366eSJosef Bacik 		start = end + 1;
9017c4452b9SChris Mason 	}
902690587d1SChris Mason 	return werr;
903690587d1SChris Mason }
904690587d1SChris Mason 
905690587d1SChris Mason /*
906690587d1SChris Mason  * when btree blocks are allocated, they have some corresponding bits set for
907690587d1SChris Mason  * them in one of two extent_io trees.  This is used to make sure all of
908690587d1SChris Mason  * those extents are on disk for transaction or log commit.  We wait
909690587d1SChris Mason  * on all the pages and clear them from the dirty pages state tree
910690587d1SChris Mason  */
911690587d1SChris Mason int btrfs_wait_marked_extents(struct btrfs_root *root,
9128cef4e16SYan, Zheng 			      struct extent_io_tree *dirty_pages, int mark)
913690587d1SChris Mason {
914690587d1SChris Mason 	int err = 0;
915690587d1SChris Mason 	int werr = 0;
9161728366eSJosef Bacik 	struct address_space *mapping = root->fs_info->btree_inode->i_mapping;
917e6138876SJosef Bacik 	struct extent_state *cached_state = NULL;
918690587d1SChris Mason 	u64 start = 0;
919690587d1SChris Mason 	u64 end;
920656f30dbSFilipe Manana 	struct btrfs_inode *btree_ino = BTRFS_I(root->fs_info->btree_inode);
921656f30dbSFilipe Manana 	bool errors = false;
922690587d1SChris Mason 
9231728366eSJosef Bacik 	while (!find_first_extent_bit(dirty_pages, start, &start, &end,
924e6138876SJosef Bacik 				      EXTENT_NEED_WAIT, &cached_state)) {
925663dfbb0SFilipe Manana 		/*
926663dfbb0SFilipe Manana 		 * Ignore -ENOMEM errors returned by clear_extent_bit().
927663dfbb0SFilipe Manana 		 * When committing the transaction, we'll remove any entries
928663dfbb0SFilipe Manana 		 * left in the io tree. For a log commit, we don't remove them
929663dfbb0SFilipe Manana 		 * after committing the log because the tree can be accessed
930663dfbb0SFilipe Manana 		 * concurrently - we do it only at transaction commit time when
931663dfbb0SFilipe Manana 		 * it's safe to do it (through clear_btree_io_tree()).
932663dfbb0SFilipe Manana 		 */
933663dfbb0SFilipe Manana 		err = clear_extent_bit(dirty_pages, start, end,
934663dfbb0SFilipe Manana 				       EXTENT_NEED_WAIT,
935e6138876SJosef Bacik 				       0, 0, &cached_state, GFP_NOFS);
936663dfbb0SFilipe Manana 		if (err == -ENOMEM)
937663dfbb0SFilipe Manana 			err = 0;
938663dfbb0SFilipe Manana 		if (!err)
9391728366eSJosef Bacik 			err = filemap_fdatawait_range(mapping, start, end);
940777e6bd7SChris Mason 		if (err)
941777e6bd7SChris Mason 			werr = err;
942e38e2ed7SFilipe Manana 		free_extent_state(cached_state);
943e38e2ed7SFilipe Manana 		cached_state = NULL;
944777e6bd7SChris Mason 		cond_resched();
9451728366eSJosef Bacik 		start = end + 1;
946777e6bd7SChris Mason 	}
9477c4452b9SChris Mason 	if (err)
9487c4452b9SChris Mason 		werr = err;
949656f30dbSFilipe Manana 
950656f30dbSFilipe Manana 	if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) {
951656f30dbSFilipe Manana 		if ((mark & EXTENT_DIRTY) &&
952656f30dbSFilipe Manana 		    test_and_clear_bit(BTRFS_INODE_BTREE_LOG1_ERR,
953656f30dbSFilipe Manana 				       &btree_ino->runtime_flags))
954656f30dbSFilipe Manana 			errors = true;
955656f30dbSFilipe Manana 
956656f30dbSFilipe Manana 		if ((mark & EXTENT_NEW) &&
957656f30dbSFilipe Manana 		    test_and_clear_bit(BTRFS_INODE_BTREE_LOG2_ERR,
958656f30dbSFilipe Manana 				       &btree_ino->runtime_flags))
959656f30dbSFilipe Manana 			errors = true;
960656f30dbSFilipe Manana 	} else {
961656f30dbSFilipe Manana 		if (test_and_clear_bit(BTRFS_INODE_BTREE_ERR,
962656f30dbSFilipe Manana 				       &btree_ino->runtime_flags))
963656f30dbSFilipe Manana 			errors = true;
964656f30dbSFilipe Manana 	}
965656f30dbSFilipe Manana 
966656f30dbSFilipe Manana 	if (errors && !werr)
967656f30dbSFilipe Manana 		werr = -EIO;
968656f30dbSFilipe Manana 
9697c4452b9SChris Mason 	return werr;
97079154b1bSChris Mason }
97179154b1bSChris Mason 
972690587d1SChris Mason /*
973690587d1SChris Mason  * when btree blocks are allocated, they have some corresponding bits set for
974690587d1SChris Mason  * them in one of two extent_io trees.  This is used to make sure all of
975690587d1SChris Mason  * those extents are on disk for transaction or log commit
976690587d1SChris Mason  */
977171170c1SSergei Trofimovich static int btrfs_write_and_wait_marked_extents(struct btrfs_root *root,
9788cef4e16SYan, Zheng 				struct extent_io_tree *dirty_pages, int mark)
979690587d1SChris Mason {
980690587d1SChris Mason 	int ret;
981690587d1SChris Mason 	int ret2;
982c6adc9ccSMiao Xie 	struct blk_plug plug;
983690587d1SChris Mason 
984c6adc9ccSMiao Xie 	blk_start_plug(&plug);
9858cef4e16SYan, Zheng 	ret = btrfs_write_marked_extents(root, dirty_pages, mark);
986c6adc9ccSMiao Xie 	blk_finish_plug(&plug);
9878cef4e16SYan, Zheng 	ret2 = btrfs_wait_marked_extents(root, dirty_pages, mark);
988bf0da8c1SChris Mason 
989bf0da8c1SChris Mason 	if (ret)
990bf0da8c1SChris Mason 		return ret;
991bf0da8c1SChris Mason 	if (ret2)
992bf0da8c1SChris Mason 		return ret2;
993bf0da8c1SChris Mason 	return 0;
994690587d1SChris Mason }
995690587d1SChris Mason 
996663dfbb0SFilipe Manana static int btrfs_write_and_wait_transaction(struct btrfs_trans_handle *trans,
997d0c803c4SChris Mason 				     struct btrfs_root *root)
998d0c803c4SChris Mason {
999663dfbb0SFilipe Manana 	int ret;
1000663dfbb0SFilipe Manana 
1001663dfbb0SFilipe Manana 	ret = btrfs_write_and_wait_marked_extents(root,
10028cef4e16SYan, Zheng 					   &trans->transaction->dirty_pages,
10038cef4e16SYan, Zheng 					   EXTENT_DIRTY);
1004663dfbb0SFilipe Manana 	clear_btree_io_tree(&trans->transaction->dirty_pages);
1005663dfbb0SFilipe Manana 
1006663dfbb0SFilipe Manana 	return ret;
1007d0c803c4SChris Mason }
1008d0c803c4SChris Mason 
1009d352ac68SChris Mason /*
1010d352ac68SChris Mason  * this is used to update the root pointer in the tree of tree roots.
1011d352ac68SChris Mason  *
1012d352ac68SChris Mason  * But, in the case of the extent allocation tree, updating the root
1013d352ac68SChris Mason  * pointer may allocate blocks which may change the root of the extent
1014d352ac68SChris Mason  * allocation tree.
1015d352ac68SChris Mason  *
1016d352ac68SChris Mason  * So, this loops and repeats and makes sure the cowonly root didn't
1017d352ac68SChris Mason  * change while the root pointer was being updated in the metadata.
1018d352ac68SChris Mason  */
10190b86a832SChris Mason static int update_cowonly_root(struct btrfs_trans_handle *trans,
102079154b1bSChris Mason 			       struct btrfs_root *root)
102179154b1bSChris Mason {
102279154b1bSChris Mason 	int ret;
10230b86a832SChris Mason 	u64 old_root_bytenr;
102486b9f2ecSYan, Zheng 	u64 old_root_used;
10250b86a832SChris Mason 	struct btrfs_root *tree_root = root->fs_info->tree_root;
1026e7070be1SJosef Bacik 	bool extent_root = (root->objectid == BTRFS_EXTENT_TREE_OBJECTID);
102779154b1bSChris Mason 
102886b9f2ecSYan, Zheng 	old_root_used = btrfs_root_used(&root->root_item);
10290b86a832SChris Mason 	btrfs_write_dirty_block_groups(trans, root);
103056bec294SChris Mason 
103179154b1bSChris Mason 	while (1) {
10320b86a832SChris Mason 		old_root_bytenr = btrfs_root_bytenr(&root->root_item);
103386b9f2ecSYan, Zheng 		if (old_root_bytenr == root->node->start &&
1034ce93ec54SJosef Bacik 		    old_root_used == btrfs_root_used(&root->root_item) &&
1035ce93ec54SJosef Bacik 		    (!extent_root ||
1036ce93ec54SJosef Bacik 		     list_empty(&trans->transaction->dirty_bgs)))
103779154b1bSChris Mason 			break;
103887ef2bb4SChris Mason 
10395d4f98a2SYan Zheng 		btrfs_set_root_node(&root->root_item, root->node);
104079154b1bSChris Mason 		ret = btrfs_update_root(trans, tree_root,
10410b86a832SChris Mason 					&root->root_key,
10420b86a832SChris Mason 					&root->root_item);
104349b25e05SJeff Mahoney 		if (ret)
104449b25e05SJeff Mahoney 			return ret;
104556bec294SChris Mason 
104686b9f2ecSYan, Zheng 		old_root_used = btrfs_root_used(&root->root_item);
1047e7070be1SJosef Bacik 		if (extent_root) {
10484a8c9a62SYan Zheng 			ret = btrfs_write_dirty_block_groups(trans, root);
104949b25e05SJeff Mahoney 			if (ret)
105049b25e05SJeff Mahoney 				return ret;
10510b86a832SChris Mason 		}
1052e7070be1SJosef Bacik 		ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
1053e7070be1SJosef Bacik 		if (ret)
1054e7070be1SJosef Bacik 			return ret;
1055ce93ec54SJosef Bacik 		ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
1056ce93ec54SJosef Bacik 		if (ret)
1057ce93ec54SJosef Bacik 			return ret;
1058e7070be1SJosef Bacik 	}
1059276e680dSYan Zheng 
10600b86a832SChris Mason 	return 0;
10610b86a832SChris Mason }
10620b86a832SChris Mason 
1063d352ac68SChris Mason /*
1064d352ac68SChris Mason  * update all the cowonly tree roots on disk
106549b25e05SJeff Mahoney  *
106649b25e05SJeff Mahoney  * The error handling in this function may not be obvious. Any of the
106749b25e05SJeff Mahoney  * failures will cause the file system to go offline. We still need
106849b25e05SJeff Mahoney  * to clean up the delayed refs.
1069d352ac68SChris Mason  */
10705d4f98a2SYan Zheng static noinline int commit_cowonly_roots(struct btrfs_trans_handle *trans,
10710b86a832SChris Mason 					 struct btrfs_root *root)
10720b86a832SChris Mason {
10730b86a832SChris Mason 	struct btrfs_fs_info *fs_info = root->fs_info;
10740b86a832SChris Mason 	struct list_head *next;
107584234f3aSYan Zheng 	struct extent_buffer *eb;
107656bec294SChris Mason 	int ret;
107784234f3aSYan Zheng 
107884234f3aSYan Zheng 	eb = btrfs_lock_root_node(fs_info->tree_root);
107949b25e05SJeff Mahoney 	ret = btrfs_cow_block(trans, fs_info->tree_root, eb, NULL,
108049b25e05SJeff Mahoney 			      0, &eb);
108184234f3aSYan Zheng 	btrfs_tree_unlock(eb);
108284234f3aSYan Zheng 	free_extent_buffer(eb);
10830b86a832SChris Mason 
108449b25e05SJeff Mahoney 	if (ret)
108549b25e05SJeff Mahoney 		return ret;
108649b25e05SJeff Mahoney 
108756bec294SChris Mason 	ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
108849b25e05SJeff Mahoney 	if (ret)
108949b25e05SJeff Mahoney 		return ret;
109087ef2bb4SChris Mason 
1091733f4fbbSStefan Behrens 	ret = btrfs_run_dev_stats(trans, root->fs_info);
1092c16ce190SJosef Bacik 	if (ret)
1093c16ce190SJosef Bacik 		return ret;
10948dabb742SStefan Behrens 	ret = btrfs_run_dev_replace(trans, root->fs_info);
1095c16ce190SJosef Bacik 	if (ret)
1096c16ce190SJosef Bacik 		return ret;
1097546adb0dSJan Schmidt 	ret = btrfs_run_qgroups(trans, root->fs_info);
1098c16ce190SJosef Bacik 	if (ret)
1099c16ce190SJosef Bacik 		return ret;
1100546adb0dSJan Schmidt 
1101546adb0dSJan Schmidt 	/* run_qgroups might have added some more refs */
1102546adb0dSJan Schmidt 	ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
1103c16ce190SJosef Bacik 	if (ret)
1104c16ce190SJosef Bacik 		return ret;
1105546adb0dSJan Schmidt 
11060b86a832SChris Mason 	while (!list_empty(&fs_info->dirty_cowonly_roots)) {
11070b86a832SChris Mason 		next = fs_info->dirty_cowonly_roots.next;
11080b86a832SChris Mason 		list_del_init(next);
11090b86a832SChris Mason 		root = list_entry(next, struct btrfs_root, dirty_list);
1110e7070be1SJosef Bacik 		clear_bit(BTRFS_ROOT_DIRTY, &root->state);
111187ef2bb4SChris Mason 
11129e351cc8SJosef Bacik 		if (root != fs_info->extent_root)
11139e351cc8SJosef Bacik 			list_add_tail(&root->dirty_list,
11149e351cc8SJosef Bacik 				      &trans->transaction->switch_commits);
111549b25e05SJeff Mahoney 		ret = update_cowonly_root(trans, root);
111649b25e05SJeff Mahoney 		if (ret)
111749b25e05SJeff Mahoney 			return ret;
111879154b1bSChris Mason 	}
1119276e680dSYan Zheng 
11209e351cc8SJosef Bacik 	list_add_tail(&fs_info->extent_root->dirty_list,
11219e351cc8SJosef Bacik 		      &trans->transaction->switch_commits);
11228dabb742SStefan Behrens 	btrfs_after_dev_replace_commit(fs_info);
11238dabb742SStefan Behrens 
112479154b1bSChris Mason 	return 0;
112579154b1bSChris Mason }
112679154b1bSChris Mason 
1127d352ac68SChris Mason /*
1128d352ac68SChris Mason  * dead roots are old snapshots that need to be deleted.  This allocates
1129d352ac68SChris Mason  * a dirty root struct and adds it into the list of dead roots that need to
1130d352ac68SChris Mason  * be deleted
1131d352ac68SChris Mason  */
1132cfad392bSJosef Bacik void btrfs_add_dead_root(struct btrfs_root *root)
11335eda7b5eSChris Mason {
1134a4abeea4SJosef Bacik 	spin_lock(&root->fs_info->trans_lock);
1135cfad392bSJosef Bacik 	if (list_empty(&root->root_list))
11369d1a2a3aSDavid Sterba 		list_add_tail(&root->root_list, &root->fs_info->dead_roots);
1137a4abeea4SJosef Bacik 	spin_unlock(&root->fs_info->trans_lock);
11385eda7b5eSChris Mason }
11395eda7b5eSChris Mason 
1140d352ac68SChris Mason /*
11415d4f98a2SYan Zheng  * update all the cowonly tree roots on disk
1142d352ac68SChris Mason  */
11435d4f98a2SYan Zheng static noinline int commit_fs_roots(struct btrfs_trans_handle *trans,
11445d4f98a2SYan Zheng 				    struct btrfs_root *root)
11450f7d52f4SChris Mason {
11460f7d52f4SChris Mason 	struct btrfs_root *gang[8];
11475d4f98a2SYan Zheng 	struct btrfs_fs_info *fs_info = root->fs_info;
11480f7d52f4SChris Mason 	int i;
11490f7d52f4SChris Mason 	int ret;
115054aa1f4dSChris Mason 	int err = 0;
115154aa1f4dSChris Mason 
1152a4abeea4SJosef Bacik 	spin_lock(&fs_info->fs_roots_radix_lock);
11530f7d52f4SChris Mason 	while (1) {
11545d4f98a2SYan Zheng 		ret = radix_tree_gang_lookup_tag(&fs_info->fs_roots_radix,
11555d4f98a2SYan Zheng 						 (void **)gang, 0,
11560f7d52f4SChris Mason 						 ARRAY_SIZE(gang),
11570f7d52f4SChris Mason 						 BTRFS_ROOT_TRANS_TAG);
11580f7d52f4SChris Mason 		if (ret == 0)
11590f7d52f4SChris Mason 			break;
11600f7d52f4SChris Mason 		for (i = 0; i < ret; i++) {
11610f7d52f4SChris Mason 			root = gang[i];
11625d4f98a2SYan Zheng 			radix_tree_tag_clear(&fs_info->fs_roots_radix,
11632619ba1fSChris Mason 					(unsigned long)root->root_key.objectid,
11640f7d52f4SChris Mason 					BTRFS_ROOT_TRANS_TAG);
1165a4abeea4SJosef Bacik 			spin_unlock(&fs_info->fs_roots_radix_lock);
116631153d81SYan Zheng 
1167e02119d5SChris Mason 			btrfs_free_log(trans, root);
11685d4f98a2SYan Zheng 			btrfs_update_reloc_root(trans, root);
1169d68fc57bSYan, Zheng 			btrfs_orphan_commit_root(trans, root);
1170e02119d5SChris Mason 
117182d5902dSLi Zefan 			btrfs_save_ino_cache(root, trans);
117282d5902dSLi Zefan 
1173f1ebcc74SLiu Bo 			/* see comments in should_cow_block() */
117427cdeb70SMiao Xie 			clear_bit(BTRFS_ROOT_FORCE_COW, &root->state);
1175c7548af6SChris Mason 			smp_mb__after_atomic();
1176f1ebcc74SLiu Bo 
1177978d910dSYan Zheng 			if (root->commit_root != root->node) {
11789e351cc8SJosef Bacik 				list_add_tail(&root->dirty_list,
11799e351cc8SJosef Bacik 					&trans->transaction->switch_commits);
1180978d910dSYan Zheng 				btrfs_set_root_node(&root->root_item,
1181978d910dSYan Zheng 						    root->node);
1182978d910dSYan Zheng 			}
118331153d81SYan Zheng 
11845d4f98a2SYan Zheng 			err = btrfs_update_root(trans, fs_info->tree_root,
11850f7d52f4SChris Mason 						&root->root_key,
11860f7d52f4SChris Mason 						&root->root_item);
1187a4abeea4SJosef Bacik 			spin_lock(&fs_info->fs_roots_radix_lock);
118854aa1f4dSChris Mason 			if (err)
118954aa1f4dSChris Mason 				break;
11900f7d52f4SChris Mason 		}
11919f3a7427SChris Mason 	}
1192a4abeea4SJosef Bacik 	spin_unlock(&fs_info->fs_roots_radix_lock);
119354aa1f4dSChris Mason 	return err;
11940f7d52f4SChris Mason }
11950f7d52f4SChris Mason 
1196d352ac68SChris Mason /*
1197de78b51aSEric Sandeen  * defrag a given btree.
1198de78b51aSEric Sandeen  * Every leaf in the btree is read and defragged.
1199d352ac68SChris Mason  */
1200de78b51aSEric Sandeen int btrfs_defrag_root(struct btrfs_root *root)
1201e9d0b13bSChris Mason {
1202e9d0b13bSChris Mason 	struct btrfs_fs_info *info = root->fs_info;
1203e9d0b13bSChris Mason 	struct btrfs_trans_handle *trans;
12048929ecfaSYan, Zheng 	int ret;
1205e9d0b13bSChris Mason 
120627cdeb70SMiao Xie 	if (test_and_set_bit(BTRFS_ROOT_DEFRAG_RUNNING, &root->state))
1207e9d0b13bSChris Mason 		return 0;
12088929ecfaSYan, Zheng 
12096b80053dSChris Mason 	while (1) {
12108929ecfaSYan, Zheng 		trans = btrfs_start_transaction(root, 0);
12118929ecfaSYan, Zheng 		if (IS_ERR(trans))
12128929ecfaSYan, Zheng 			return PTR_ERR(trans);
12138929ecfaSYan, Zheng 
1214de78b51aSEric Sandeen 		ret = btrfs_defrag_leaves(trans, root);
12158929ecfaSYan, Zheng 
1216e9d0b13bSChris Mason 		btrfs_end_transaction(trans, root);
1217b53d3f5dSLiu Bo 		btrfs_btree_balance_dirty(info->tree_root);
1218e9d0b13bSChris Mason 		cond_resched();
1219e9d0b13bSChris Mason 
12207841cb28SDavid Sterba 		if (btrfs_fs_closing(root->fs_info) || ret != -EAGAIN)
1221e9d0b13bSChris Mason 			break;
1222210549ebSDavid Sterba 
1223210549ebSDavid Sterba 		if (btrfs_defrag_cancelled(root->fs_info)) {
1224efe120a0SFrank Holton 			pr_debug("BTRFS: defrag_root cancelled\n");
1225210549ebSDavid Sterba 			ret = -EAGAIN;
1226210549ebSDavid Sterba 			break;
1227210549ebSDavid Sterba 		}
1228e9d0b13bSChris Mason 	}
122927cdeb70SMiao Xie 	clear_bit(BTRFS_ROOT_DEFRAG_RUNNING, &root->state);
12308929ecfaSYan, Zheng 	return ret;
1231e9d0b13bSChris Mason }
1232e9d0b13bSChris Mason 
1233d352ac68SChris Mason /*
1234d352ac68SChris Mason  * new snapshots need to be created at a very specific time in the
1235aec8030aSMiao Xie  * transaction commit.  This does the actual creation.
1236aec8030aSMiao Xie  *
1237aec8030aSMiao Xie  * Note:
1238aec8030aSMiao Xie  * If the error which may affect the commitment of the current transaction
1239aec8030aSMiao Xie  * happens, we should return the error number. If the error which just affect
1240aec8030aSMiao Xie  * the creation of the pending snapshots, just return 0.
1241d352ac68SChris Mason  */
124280b6794dSChris Mason static noinline int create_pending_snapshot(struct btrfs_trans_handle *trans,
12433063d29fSChris Mason 				   struct btrfs_fs_info *fs_info,
12443063d29fSChris Mason 				   struct btrfs_pending_snapshot *pending)
12453063d29fSChris Mason {
12463063d29fSChris Mason 	struct btrfs_key key;
124780b6794dSChris Mason 	struct btrfs_root_item *new_root_item;
12483063d29fSChris Mason 	struct btrfs_root *tree_root = fs_info->tree_root;
12493063d29fSChris Mason 	struct btrfs_root *root = pending->root;
12506bdb72deSSage Weil 	struct btrfs_root *parent_root;
125198c9942aSLiu Bo 	struct btrfs_block_rsv *rsv;
12526bdb72deSSage Weil 	struct inode *parent_inode;
125342874b3dSMiao Xie 	struct btrfs_path *path;
125442874b3dSMiao Xie 	struct btrfs_dir_item *dir_item;
1255a22285a6SYan, Zheng 	struct dentry *dentry;
12563063d29fSChris Mason 	struct extent_buffer *tmp;
1257925baeddSChris Mason 	struct extent_buffer *old;
12588ea05e3aSAlexander Block 	struct timespec cur_time = CURRENT_TIME;
1259aec8030aSMiao Xie 	int ret = 0;
1260d68fc57bSYan, Zheng 	u64 to_reserve = 0;
12616bdb72deSSage Weil 	u64 index = 0;
1262a22285a6SYan, Zheng 	u64 objectid;
1263b83cc969SLi Zefan 	u64 root_flags;
12648ea05e3aSAlexander Block 	uuid_le new_uuid;
12653063d29fSChris Mason 
126642874b3dSMiao Xie 	path = btrfs_alloc_path();
126742874b3dSMiao Xie 	if (!path) {
1268aec8030aSMiao Xie 		pending->error = -ENOMEM;
1269aec8030aSMiao Xie 		return 0;
127042874b3dSMiao Xie 	}
127142874b3dSMiao Xie 
127280b6794dSChris Mason 	new_root_item = kmalloc(sizeof(*new_root_item), GFP_NOFS);
127380b6794dSChris Mason 	if (!new_root_item) {
1274aec8030aSMiao Xie 		pending->error = -ENOMEM;
12756fa9700eSMiao Xie 		goto root_item_alloc_fail;
127680b6794dSChris Mason 	}
1277a22285a6SYan, Zheng 
1278aec8030aSMiao Xie 	pending->error = btrfs_find_free_objectid(tree_root, &objectid);
1279aec8030aSMiao Xie 	if (pending->error)
12806fa9700eSMiao Xie 		goto no_free_objectid;
12813063d29fSChris Mason 
12823fd0a558SYan, Zheng 	btrfs_reloc_pre_snapshot(trans, pending, &to_reserve);
1283d68fc57bSYan, Zheng 
1284d68fc57bSYan, Zheng 	if (to_reserve > 0) {
1285aec8030aSMiao Xie 		pending->error = btrfs_block_rsv_add(root,
1286aec8030aSMiao Xie 						     &pending->block_rsv,
128708e007d2SMiao Xie 						     to_reserve,
128808e007d2SMiao Xie 						     BTRFS_RESERVE_NO_FLUSH);
1289aec8030aSMiao Xie 		if (pending->error)
12906fa9700eSMiao Xie 			goto no_free_objectid;
1291d68fc57bSYan, Zheng 	}
1292d68fc57bSYan, Zheng 
12933063d29fSChris Mason 	key.objectid = objectid;
1294a22285a6SYan, Zheng 	key.offset = (u64)-1;
1295a22285a6SYan, Zheng 	key.type = BTRFS_ROOT_ITEM_KEY;
12963063d29fSChris Mason 
12976fa9700eSMiao Xie 	rsv = trans->block_rsv;
1298a22285a6SYan, Zheng 	trans->block_rsv = &pending->block_rsv;
12992382c5ccSLiu Bo 	trans->bytes_reserved = trans->block_rsv->reserved;
13006bdb72deSSage Weil 
1301a22285a6SYan, Zheng 	dentry = pending->dentry;
1302e9662f70SMiao Xie 	parent_inode = pending->dir;
1303a22285a6SYan, Zheng 	parent_root = BTRFS_I(parent_inode)->root;
13047585717fSChris Mason 	record_root_in_trans(trans, parent_root);
1305a22285a6SYan, Zheng 
13066bdb72deSSage Weil 	/*
13076bdb72deSSage Weil 	 * insert the directory item
13086bdb72deSSage Weil 	 */
13096bdb72deSSage Weil 	ret = btrfs_set_inode_index(parent_inode, &index);
131049b25e05SJeff Mahoney 	BUG_ON(ret); /* -ENOMEM */
131142874b3dSMiao Xie 
131242874b3dSMiao Xie 	/* check if there is a file/dir which has the same name. */
131342874b3dSMiao Xie 	dir_item = btrfs_lookup_dir_item(NULL, parent_root, path,
131442874b3dSMiao Xie 					 btrfs_ino(parent_inode),
131542874b3dSMiao Xie 					 dentry->d_name.name,
131642874b3dSMiao Xie 					 dentry->d_name.len, 0);
131742874b3dSMiao Xie 	if (dir_item != NULL && !IS_ERR(dir_item)) {
1318fe66a05aSChris Mason 		pending->error = -EEXIST;
1319aec8030aSMiao Xie 		goto dir_item_existed;
132042874b3dSMiao Xie 	} else if (IS_ERR(dir_item)) {
132142874b3dSMiao Xie 		ret = PTR_ERR(dir_item);
13228732d44fSMiao Xie 		btrfs_abort_transaction(trans, root, ret);
13238732d44fSMiao Xie 		goto fail;
132479787eaaSJeff Mahoney 	}
132542874b3dSMiao Xie 	btrfs_release_path(path);
13266bdb72deSSage Weil 
1327e999376fSChris Mason 	/*
1328e999376fSChris Mason 	 * pull in the delayed directory update
1329e999376fSChris Mason 	 * and the delayed inode item
1330e999376fSChris Mason 	 * otherwise we corrupt the FS during
1331e999376fSChris Mason 	 * snapshot
1332e999376fSChris Mason 	 */
1333e999376fSChris Mason 	ret = btrfs_run_delayed_items(trans, root);
13348732d44fSMiao Xie 	if (ret) {	/* Transaction aborted */
13358732d44fSMiao Xie 		btrfs_abort_transaction(trans, root, ret);
13368732d44fSMiao Xie 		goto fail;
13378732d44fSMiao Xie 	}
1338e999376fSChris Mason 
13397585717fSChris Mason 	record_root_in_trans(trans, root);
13406bdb72deSSage Weil 	btrfs_set_root_last_snapshot(&root->root_item, trans->transid);
13416bdb72deSSage Weil 	memcpy(new_root_item, &root->root_item, sizeof(*new_root_item));
134208fe4db1SLi Zefan 	btrfs_check_and_init_root_item(new_root_item);
13436bdb72deSSage Weil 
1344b83cc969SLi Zefan 	root_flags = btrfs_root_flags(new_root_item);
1345b83cc969SLi Zefan 	if (pending->readonly)
1346b83cc969SLi Zefan 		root_flags |= BTRFS_ROOT_SUBVOL_RDONLY;
1347b83cc969SLi Zefan 	else
1348b83cc969SLi Zefan 		root_flags &= ~BTRFS_ROOT_SUBVOL_RDONLY;
1349b83cc969SLi Zefan 	btrfs_set_root_flags(new_root_item, root_flags);
1350b83cc969SLi Zefan 
13518ea05e3aSAlexander Block 	btrfs_set_root_generation_v2(new_root_item,
13528ea05e3aSAlexander Block 			trans->transid);
13538ea05e3aSAlexander Block 	uuid_le_gen(&new_uuid);
13548ea05e3aSAlexander Block 	memcpy(new_root_item->uuid, new_uuid.b, BTRFS_UUID_SIZE);
13558ea05e3aSAlexander Block 	memcpy(new_root_item->parent_uuid, root->root_item.uuid,
13568ea05e3aSAlexander Block 			BTRFS_UUID_SIZE);
135770023da2SStefan Behrens 	if (!(root_flags & BTRFS_ROOT_SUBVOL_RDONLY)) {
135870023da2SStefan Behrens 		memset(new_root_item->received_uuid, 0,
135970023da2SStefan Behrens 		       sizeof(new_root_item->received_uuid));
13608ea05e3aSAlexander Block 		memset(&new_root_item->stime, 0, sizeof(new_root_item->stime));
13618ea05e3aSAlexander Block 		memset(&new_root_item->rtime, 0, sizeof(new_root_item->rtime));
13628ea05e3aSAlexander Block 		btrfs_set_root_stransid(new_root_item, 0);
13638ea05e3aSAlexander Block 		btrfs_set_root_rtransid(new_root_item, 0);
136470023da2SStefan Behrens 	}
13653cae210fSQu Wenruo 	btrfs_set_stack_timespec_sec(&new_root_item->otime, cur_time.tv_sec);
13663cae210fSQu Wenruo 	btrfs_set_stack_timespec_nsec(&new_root_item->otime, cur_time.tv_nsec);
136770023da2SStefan Behrens 	btrfs_set_root_otransid(new_root_item, trans->transid);
13688ea05e3aSAlexander Block 
1369925baeddSChris Mason 	old = btrfs_lock_root_node(root);
137049b25e05SJeff Mahoney 	ret = btrfs_cow_block(trans, root, old, NULL, 0, &old);
137179787eaaSJeff Mahoney 	if (ret) {
137279787eaaSJeff Mahoney 		btrfs_tree_unlock(old);
137379787eaaSJeff Mahoney 		free_extent_buffer(old);
13748732d44fSMiao Xie 		btrfs_abort_transaction(trans, root, ret);
13758732d44fSMiao Xie 		goto fail;
137679787eaaSJeff Mahoney 	}
137749b25e05SJeff Mahoney 
13785d4f98a2SYan Zheng 	btrfs_set_lock_blocking(old);
13793063d29fSChris Mason 
138049b25e05SJeff Mahoney 	ret = btrfs_copy_root(trans, root, old, &tmp, objectid);
138179787eaaSJeff Mahoney 	/* clean up in any case */
1382925baeddSChris Mason 	btrfs_tree_unlock(old);
1383925baeddSChris Mason 	free_extent_buffer(old);
13848732d44fSMiao Xie 	if (ret) {
13858732d44fSMiao Xie 		btrfs_abort_transaction(trans, root, ret);
13868732d44fSMiao Xie 		goto fail;
13878732d44fSMiao Xie 	}
13883063d29fSChris Mason 
1389fcebe456SJosef Bacik 	/*
1390fcebe456SJosef Bacik 	 * We need to flush delayed refs in order to make sure all of our quota
1391fcebe456SJosef Bacik 	 * operations have been done before we call btrfs_qgroup_inherit.
1392fcebe456SJosef Bacik 	 */
1393fcebe456SJosef Bacik 	ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
1394fcebe456SJosef Bacik 	if (ret) {
1395fcebe456SJosef Bacik 		btrfs_abort_transaction(trans, root, ret);
1396fcebe456SJosef Bacik 		goto fail;
1397fcebe456SJosef Bacik 	}
1398fcebe456SJosef Bacik 
139947a306a7SEric Sandeen 	ret = btrfs_qgroup_inherit(trans, fs_info,
1400fcebe456SJosef Bacik 				   root->root_key.objectid,
1401fcebe456SJosef Bacik 				   objectid, pending->inherit);
140247a306a7SEric Sandeen 	if (ret) {
140347a306a7SEric Sandeen 		btrfs_abort_transaction(trans, root, ret);
140447a306a7SEric Sandeen 		goto fail;
140547a306a7SEric Sandeen 	}
1406fcebe456SJosef Bacik 
1407f1ebcc74SLiu Bo 	/* see comments in should_cow_block() */
140827cdeb70SMiao Xie 	set_bit(BTRFS_ROOT_FORCE_COW, &root->state);
1409f1ebcc74SLiu Bo 	smp_wmb();
1410f1ebcc74SLiu Bo 
14115d4f98a2SYan Zheng 	btrfs_set_root_node(new_root_item, tmp);
1412a22285a6SYan, Zheng 	/* record when the snapshot was created in key.offset */
1413a22285a6SYan, Zheng 	key.offset = trans->transid;
1414a22285a6SYan, Zheng 	ret = btrfs_insert_root(trans, tree_root, &key, new_root_item);
1415925baeddSChris Mason 	btrfs_tree_unlock(tmp);
14163063d29fSChris Mason 	free_extent_buffer(tmp);
14178732d44fSMiao Xie 	if (ret) {
14188732d44fSMiao Xie 		btrfs_abort_transaction(trans, root, ret);
14198732d44fSMiao Xie 		goto fail;
14208732d44fSMiao Xie 	}
14210660b5afSChris Mason 
1422a22285a6SYan, Zheng 	/*
1423a22285a6SYan, Zheng 	 * insert root back/forward references
1424a22285a6SYan, Zheng 	 */
1425a22285a6SYan, Zheng 	ret = btrfs_add_root_ref(trans, tree_root, objectid,
1426a22285a6SYan, Zheng 				 parent_root->root_key.objectid,
142733345d01SLi Zefan 				 btrfs_ino(parent_inode), index,
1428a22285a6SYan, Zheng 				 dentry->d_name.name, dentry->d_name.len);
14298732d44fSMiao Xie 	if (ret) {
14308732d44fSMiao Xie 		btrfs_abort_transaction(trans, root, ret);
14318732d44fSMiao Xie 		goto fail;
14328732d44fSMiao Xie 	}
1433a22285a6SYan, Zheng 
1434a22285a6SYan, Zheng 	key.offset = (u64)-1;
1435a22285a6SYan, Zheng 	pending->snap = btrfs_read_fs_root_no_name(root->fs_info, &key);
143679787eaaSJeff Mahoney 	if (IS_ERR(pending->snap)) {
143779787eaaSJeff Mahoney 		ret = PTR_ERR(pending->snap);
14388732d44fSMiao Xie 		btrfs_abort_transaction(trans, root, ret);
14398732d44fSMiao Xie 		goto fail;
144079787eaaSJeff Mahoney 	}
1441d68fc57bSYan, Zheng 
144249b25e05SJeff Mahoney 	ret = btrfs_reloc_post_snapshot(trans, pending);
14438732d44fSMiao Xie 	if (ret) {
14448732d44fSMiao Xie 		btrfs_abort_transaction(trans, root, ret);
14458732d44fSMiao Xie 		goto fail;
14468732d44fSMiao Xie 	}
1447361048f5SMiao Xie 
1448361048f5SMiao Xie 	ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
14498732d44fSMiao Xie 	if (ret) {
14508732d44fSMiao Xie 		btrfs_abort_transaction(trans, root, ret);
14518732d44fSMiao Xie 		goto fail;
14528732d44fSMiao Xie 	}
145342874b3dSMiao Xie 
145442874b3dSMiao Xie 	ret = btrfs_insert_dir_item(trans, parent_root,
145542874b3dSMiao Xie 				    dentry->d_name.name, dentry->d_name.len,
145642874b3dSMiao Xie 				    parent_inode, &key,
145742874b3dSMiao Xie 				    BTRFS_FT_DIR, index);
145842874b3dSMiao Xie 	/* We have check then name at the beginning, so it is impossible. */
14599c52057cSChris Mason 	BUG_ON(ret == -EEXIST || ret == -EOVERFLOW);
14608732d44fSMiao Xie 	if (ret) {
14618732d44fSMiao Xie 		btrfs_abort_transaction(trans, root, ret);
14628732d44fSMiao Xie 		goto fail;
14638732d44fSMiao Xie 	}
146442874b3dSMiao Xie 
146542874b3dSMiao Xie 	btrfs_i_size_write(parent_inode, parent_inode->i_size +
146642874b3dSMiao Xie 					 dentry->d_name.len * 2);
146742874b3dSMiao Xie 	parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
1468be6aef60SJosef Bacik 	ret = btrfs_update_inode_fallback(trans, parent_root, parent_inode);
1469dd5f9615SStefan Behrens 	if (ret) {
14708732d44fSMiao Xie 		btrfs_abort_transaction(trans, root, ret);
1471dd5f9615SStefan Behrens 		goto fail;
1472dd5f9615SStefan Behrens 	}
1473dd5f9615SStefan Behrens 	ret = btrfs_uuid_tree_add(trans, fs_info->uuid_root, new_uuid.b,
1474dd5f9615SStefan Behrens 				  BTRFS_UUID_KEY_SUBVOL, objectid);
1475dd5f9615SStefan Behrens 	if (ret) {
1476dd5f9615SStefan Behrens 		btrfs_abort_transaction(trans, root, ret);
1477dd5f9615SStefan Behrens 		goto fail;
1478dd5f9615SStefan Behrens 	}
1479dd5f9615SStefan Behrens 	if (!btrfs_is_empty_uuid(new_root_item->received_uuid)) {
1480dd5f9615SStefan Behrens 		ret = btrfs_uuid_tree_add(trans, fs_info->uuid_root,
1481dd5f9615SStefan Behrens 					  new_root_item->received_uuid,
1482dd5f9615SStefan Behrens 					  BTRFS_UUID_KEY_RECEIVED_SUBVOL,
1483dd5f9615SStefan Behrens 					  objectid);
1484dd5f9615SStefan Behrens 		if (ret && ret != -EEXIST) {
1485dd5f9615SStefan Behrens 			btrfs_abort_transaction(trans, root, ret);
1486dd5f9615SStefan Behrens 			goto fail;
1487dd5f9615SStefan Behrens 		}
1488dd5f9615SStefan Behrens 	}
14893063d29fSChris Mason fail:
1490aec8030aSMiao Xie 	pending->error = ret;
1491aec8030aSMiao Xie dir_item_existed:
149298c9942aSLiu Bo 	trans->block_rsv = rsv;
14932382c5ccSLiu Bo 	trans->bytes_reserved = 0;
14946fa9700eSMiao Xie no_free_objectid:
14956fa9700eSMiao Xie 	kfree(new_root_item);
14966fa9700eSMiao Xie root_item_alloc_fail:
149742874b3dSMiao Xie 	btrfs_free_path(path);
149849b25e05SJeff Mahoney 	return ret;
14993063d29fSChris Mason }
15003063d29fSChris Mason 
1501d352ac68SChris Mason /*
1502d352ac68SChris Mason  * create all the snapshots we've scheduled for creation
1503d352ac68SChris Mason  */
150480b6794dSChris Mason static noinline int create_pending_snapshots(struct btrfs_trans_handle *trans,
15053063d29fSChris Mason 					     struct btrfs_fs_info *fs_info)
15063063d29fSChris Mason {
1507aec8030aSMiao Xie 	struct btrfs_pending_snapshot *pending, *next;
15083063d29fSChris Mason 	struct list_head *head = &trans->transaction->pending_snapshots;
1509aec8030aSMiao Xie 	int ret = 0;
15103de4586cSChris Mason 
1511aec8030aSMiao Xie 	list_for_each_entry_safe(pending, next, head, list) {
1512aec8030aSMiao Xie 		list_del(&pending->list);
1513aec8030aSMiao Xie 		ret = create_pending_snapshot(trans, fs_info, pending);
1514aec8030aSMiao Xie 		if (ret)
1515aec8030aSMiao Xie 			break;
1516aec8030aSMiao Xie 	}
1517aec8030aSMiao Xie 	return ret;
15183de4586cSChris Mason }
15193de4586cSChris Mason 
15205d4f98a2SYan Zheng static void update_super_roots(struct btrfs_root *root)
15215d4f98a2SYan Zheng {
15225d4f98a2SYan Zheng 	struct btrfs_root_item *root_item;
15235d4f98a2SYan Zheng 	struct btrfs_super_block *super;
15245d4f98a2SYan Zheng 
15256c41761fSDavid Sterba 	super = root->fs_info->super_copy;
15265d4f98a2SYan Zheng 
15275d4f98a2SYan Zheng 	root_item = &root->fs_info->chunk_root->root_item;
15285d4f98a2SYan Zheng 	super->chunk_root = root_item->bytenr;
15295d4f98a2SYan Zheng 	super->chunk_root_generation = root_item->generation;
15305d4f98a2SYan Zheng 	super->chunk_root_level = root_item->level;
15315d4f98a2SYan Zheng 
15325d4f98a2SYan Zheng 	root_item = &root->fs_info->tree_root->root_item;
15335d4f98a2SYan Zheng 	super->root = root_item->bytenr;
15345d4f98a2SYan Zheng 	super->generation = root_item->generation;
15355d4f98a2SYan Zheng 	super->root_level = root_item->level;
153673bc1876SJosef Bacik 	if (btrfs_test_opt(root, SPACE_CACHE))
15370af3d00bSJosef Bacik 		super->cache_generation = root_item->generation;
153870f80175SStefan Behrens 	if (root->fs_info->update_uuid_tree_gen)
153926432799SStefan Behrens 		super->uuid_tree_generation = root_item->generation;
15405d4f98a2SYan Zheng }
15415d4f98a2SYan Zheng 
1542f36f3042SChris Mason int btrfs_transaction_in_commit(struct btrfs_fs_info *info)
1543f36f3042SChris Mason {
15444a9d8bdeSMiao Xie 	struct btrfs_transaction *trans;
1545f36f3042SChris Mason 	int ret = 0;
15464a9d8bdeSMiao Xie 
1547a4abeea4SJosef Bacik 	spin_lock(&info->trans_lock);
15484a9d8bdeSMiao Xie 	trans = info->running_transaction;
15494a9d8bdeSMiao Xie 	if (trans)
15504a9d8bdeSMiao Xie 		ret = (trans->state >= TRANS_STATE_COMMIT_START);
1551a4abeea4SJosef Bacik 	spin_unlock(&info->trans_lock);
1552f36f3042SChris Mason 	return ret;
1553f36f3042SChris Mason }
1554f36f3042SChris Mason 
15558929ecfaSYan, Zheng int btrfs_transaction_blocked(struct btrfs_fs_info *info)
15568929ecfaSYan, Zheng {
15574a9d8bdeSMiao Xie 	struct btrfs_transaction *trans;
15588929ecfaSYan, Zheng 	int ret = 0;
15594a9d8bdeSMiao Xie 
1560a4abeea4SJosef Bacik 	spin_lock(&info->trans_lock);
15614a9d8bdeSMiao Xie 	trans = info->running_transaction;
15624a9d8bdeSMiao Xie 	if (trans)
15634a9d8bdeSMiao Xie 		ret = is_transaction_blocked(trans);
1564a4abeea4SJosef Bacik 	spin_unlock(&info->trans_lock);
15658929ecfaSYan, Zheng 	return ret;
15668929ecfaSYan, Zheng }
15678929ecfaSYan, Zheng 
1568bb9c12c9SSage Weil /*
1569bb9c12c9SSage Weil  * wait for the current transaction commit to start and block subsequent
1570bb9c12c9SSage Weil  * transaction joins
1571bb9c12c9SSage Weil  */
1572bb9c12c9SSage Weil static void wait_current_trans_commit_start(struct btrfs_root *root,
1573bb9c12c9SSage Weil 					    struct btrfs_transaction *trans)
1574bb9c12c9SSage Weil {
15754a9d8bdeSMiao Xie 	wait_event(root->fs_info->transaction_blocked_wait,
1576501407aaSJosef Bacik 		   trans->state >= TRANS_STATE_COMMIT_START ||
1577501407aaSJosef Bacik 		   trans->aborted);
1578bb9c12c9SSage Weil }
1579bb9c12c9SSage Weil 
1580bb9c12c9SSage Weil /*
1581bb9c12c9SSage Weil  * wait for the current transaction to start and then become unblocked.
1582bb9c12c9SSage Weil  * caller holds ref.
1583bb9c12c9SSage Weil  */
1584bb9c12c9SSage Weil static void wait_current_trans_commit_start_and_unblock(struct btrfs_root *root,
1585bb9c12c9SSage Weil 					 struct btrfs_transaction *trans)
1586bb9c12c9SSage Weil {
158772d63ed6SLi Zefan 	wait_event(root->fs_info->transaction_wait,
1588501407aaSJosef Bacik 		   trans->state >= TRANS_STATE_UNBLOCKED ||
1589501407aaSJosef Bacik 		   trans->aborted);
1590bb9c12c9SSage Weil }
1591bb9c12c9SSage Weil 
1592bb9c12c9SSage Weil /*
1593bb9c12c9SSage Weil  * commit transactions asynchronously. once btrfs_commit_transaction_async
1594bb9c12c9SSage Weil  * returns, any subsequent transaction will not be allowed to join.
1595bb9c12c9SSage Weil  */
1596bb9c12c9SSage Weil struct btrfs_async_commit {
1597bb9c12c9SSage Weil 	struct btrfs_trans_handle *newtrans;
1598bb9c12c9SSage Weil 	struct btrfs_root *root;
15997892b5afSMiao Xie 	struct work_struct work;
1600bb9c12c9SSage Weil };
1601bb9c12c9SSage Weil 
1602bb9c12c9SSage Weil static void do_async_commit(struct work_struct *work)
1603bb9c12c9SSage Weil {
1604bb9c12c9SSage Weil 	struct btrfs_async_commit *ac =
16057892b5afSMiao Xie 		container_of(work, struct btrfs_async_commit, work);
1606bb9c12c9SSage Weil 
16076fc4e354SSage Weil 	/*
16086fc4e354SSage Weil 	 * We've got freeze protection passed with the transaction.
16096fc4e354SSage Weil 	 * Tell lockdep about it.
16106fc4e354SSage Weil 	 */
1611b1a06a4bSLiu Bo 	if (ac->newtrans->type & __TRANS_FREEZABLE)
16126fc4e354SSage Weil 		rwsem_acquire_read(
16136fc4e354SSage Weil 		     &ac->root->fs_info->sb->s_writers.lock_map[SB_FREEZE_FS-1],
16146fc4e354SSage Weil 		     0, 1, _THIS_IP_);
16156fc4e354SSage Weil 
1616e209db7aSSage Weil 	current->journal_info = ac->newtrans;
1617e209db7aSSage Weil 
1618bb9c12c9SSage Weil 	btrfs_commit_transaction(ac->newtrans, ac->root);
1619bb9c12c9SSage Weil 	kfree(ac);
1620bb9c12c9SSage Weil }
1621bb9c12c9SSage Weil 
1622bb9c12c9SSage Weil int btrfs_commit_transaction_async(struct btrfs_trans_handle *trans,
1623bb9c12c9SSage Weil 				   struct btrfs_root *root,
1624bb9c12c9SSage Weil 				   int wait_for_unblock)
1625bb9c12c9SSage Weil {
1626bb9c12c9SSage Weil 	struct btrfs_async_commit *ac;
1627bb9c12c9SSage Weil 	struct btrfs_transaction *cur_trans;
1628bb9c12c9SSage Weil 
1629bb9c12c9SSage Weil 	ac = kmalloc(sizeof(*ac), GFP_NOFS);
1630db5b493aSTsutomu Itoh 	if (!ac)
1631db5b493aSTsutomu Itoh 		return -ENOMEM;
1632bb9c12c9SSage Weil 
16337892b5afSMiao Xie 	INIT_WORK(&ac->work, do_async_commit);
1634bb9c12c9SSage Weil 	ac->root = root;
16357a7eaa40SJosef Bacik 	ac->newtrans = btrfs_join_transaction(root);
16363612b495STsutomu Itoh 	if (IS_ERR(ac->newtrans)) {
16373612b495STsutomu Itoh 		int err = PTR_ERR(ac->newtrans);
16383612b495STsutomu Itoh 		kfree(ac);
16393612b495STsutomu Itoh 		return err;
16403612b495STsutomu Itoh 	}
1641bb9c12c9SSage Weil 
1642bb9c12c9SSage Weil 	/* take transaction reference */
1643bb9c12c9SSage Weil 	cur_trans = trans->transaction;
164413c5a93eSJosef Bacik 	atomic_inc(&cur_trans->use_count);
1645bb9c12c9SSage Weil 
1646bb9c12c9SSage Weil 	btrfs_end_transaction(trans, root);
16476fc4e354SSage Weil 
16486fc4e354SSage Weil 	/*
16496fc4e354SSage Weil 	 * Tell lockdep we've released the freeze rwsem, since the
16506fc4e354SSage Weil 	 * async commit thread will be the one to unlock it.
16516fc4e354SSage Weil 	 */
1652b1a06a4bSLiu Bo 	if (ac->newtrans->type & __TRANS_FREEZABLE)
1653ff7c1d33SMiao Xie 		rwsem_release(
1654ff7c1d33SMiao Xie 			&root->fs_info->sb->s_writers.lock_map[SB_FREEZE_FS-1],
16556fc4e354SSage Weil 			1, _THIS_IP_);
16566fc4e354SSage Weil 
16577892b5afSMiao Xie 	schedule_work(&ac->work);
1658bb9c12c9SSage Weil 
1659bb9c12c9SSage Weil 	/* wait for transaction to start and unblock */
1660bb9c12c9SSage Weil 	if (wait_for_unblock)
1661bb9c12c9SSage Weil 		wait_current_trans_commit_start_and_unblock(root, cur_trans);
1662bb9c12c9SSage Weil 	else
1663bb9c12c9SSage Weil 		wait_current_trans_commit_start(root, cur_trans);
1664bb9c12c9SSage Weil 
166538e88054SSage Weil 	if (current->journal_info == trans)
166638e88054SSage Weil 		current->journal_info = NULL;
166738e88054SSage Weil 
1668724e2315SJosef Bacik 	btrfs_put_transaction(cur_trans);
1669bb9c12c9SSage Weil 	return 0;
1670bb9c12c9SSage Weil }
1671bb9c12c9SSage Weil 
167249b25e05SJeff Mahoney 
167349b25e05SJeff Mahoney static void cleanup_transaction(struct btrfs_trans_handle *trans,
16747b8b92afSJosef Bacik 				struct btrfs_root *root, int err)
167549b25e05SJeff Mahoney {
167649b25e05SJeff Mahoney 	struct btrfs_transaction *cur_trans = trans->transaction;
1677f094ac32SLiu Bo 	DEFINE_WAIT(wait);
167849b25e05SJeff Mahoney 
167949b25e05SJeff Mahoney 	WARN_ON(trans->use_count > 1);
168049b25e05SJeff Mahoney 
16817b8b92afSJosef Bacik 	btrfs_abort_transaction(trans, root, err);
16827b8b92afSJosef Bacik 
168349b25e05SJeff Mahoney 	spin_lock(&root->fs_info->trans_lock);
168466b6135bSLiu Bo 
168525d8c284SMiao Xie 	/*
168625d8c284SMiao Xie 	 * If the transaction is removed from the list, it means this
168725d8c284SMiao Xie 	 * transaction has been committed successfully, so it is impossible
168825d8c284SMiao Xie 	 * to call the cleanup function.
168925d8c284SMiao Xie 	 */
169025d8c284SMiao Xie 	BUG_ON(list_empty(&cur_trans->list));
169166b6135bSLiu Bo 
169249b25e05SJeff Mahoney 	list_del_init(&cur_trans->list);
1693d7096fc3SJosef Bacik 	if (cur_trans == root->fs_info->running_transaction) {
16944a9d8bdeSMiao Xie 		cur_trans->state = TRANS_STATE_COMMIT_DOING;
1695f094ac32SLiu Bo 		spin_unlock(&root->fs_info->trans_lock);
1696f094ac32SLiu Bo 		wait_event(cur_trans->writer_wait,
1697f094ac32SLiu Bo 			   atomic_read(&cur_trans->num_writers) == 1);
1698f094ac32SLiu Bo 
1699f094ac32SLiu Bo 		spin_lock(&root->fs_info->trans_lock);
1700d7096fc3SJosef Bacik 	}
170149b25e05SJeff Mahoney 	spin_unlock(&root->fs_info->trans_lock);
170249b25e05SJeff Mahoney 
170349b25e05SJeff Mahoney 	btrfs_cleanup_one_transaction(trans->transaction, root);
170449b25e05SJeff Mahoney 
17054a9d8bdeSMiao Xie 	spin_lock(&root->fs_info->trans_lock);
17064a9d8bdeSMiao Xie 	if (cur_trans == root->fs_info->running_transaction)
17074a9d8bdeSMiao Xie 		root->fs_info->running_transaction = NULL;
17084a9d8bdeSMiao Xie 	spin_unlock(&root->fs_info->trans_lock);
17094a9d8bdeSMiao Xie 
1710e0228285SJosef Bacik 	if (trans->type & __TRANS_FREEZABLE)
1711e0228285SJosef Bacik 		sb_end_intwrite(root->fs_info->sb);
1712724e2315SJosef Bacik 	btrfs_put_transaction(cur_trans);
1713724e2315SJosef Bacik 	btrfs_put_transaction(cur_trans);
171449b25e05SJeff Mahoney 
171549b25e05SJeff Mahoney 	trace_btrfs_transaction_commit(root);
171649b25e05SJeff Mahoney 
171749b25e05SJeff Mahoney 	if (current->journal_info == trans)
171849b25e05SJeff Mahoney 		current->journal_info = NULL;
1719c0af8f0bSWang Shilong 	btrfs_scrub_cancel(root->fs_info);
172049b25e05SJeff Mahoney 
172149b25e05SJeff Mahoney 	kmem_cache_free(btrfs_trans_handle_cachep, trans);
172249b25e05SJeff Mahoney }
172349b25e05SJeff Mahoney 
172482436617SMiao Xie static inline int btrfs_start_delalloc_flush(struct btrfs_fs_info *fs_info)
172582436617SMiao Xie {
172682436617SMiao Xie 	if (btrfs_test_opt(fs_info->tree_root, FLUSHONCOMMIT))
17276c255e67SMiao Xie 		return btrfs_start_delalloc_roots(fs_info, 1, -1);
172882436617SMiao Xie 	return 0;
172982436617SMiao Xie }
173082436617SMiao Xie 
173182436617SMiao Xie static inline void btrfs_wait_delalloc_flush(struct btrfs_fs_info *fs_info)
173282436617SMiao Xie {
173382436617SMiao Xie 	if (btrfs_test_opt(fs_info->tree_root, FLUSHONCOMMIT))
1734b0244199SMiao Xie 		btrfs_wait_ordered_roots(fs_info, -1);
173582436617SMiao Xie }
173682436617SMiao Xie 
173750d9aa99SJosef Bacik static inline void
173850d9aa99SJosef Bacik btrfs_wait_pending_ordered(struct btrfs_transaction *cur_trans,
173950d9aa99SJosef Bacik 			   struct btrfs_fs_info *fs_info)
174050d9aa99SJosef Bacik {
174150d9aa99SJosef Bacik 	struct btrfs_ordered_extent *ordered;
174250d9aa99SJosef Bacik 
174350d9aa99SJosef Bacik 	spin_lock(&fs_info->trans_lock);
174450d9aa99SJosef Bacik 	while (!list_empty(&cur_trans->pending_ordered)) {
174550d9aa99SJosef Bacik 		ordered = list_first_entry(&cur_trans->pending_ordered,
174650d9aa99SJosef Bacik 					   struct btrfs_ordered_extent,
174750d9aa99SJosef Bacik 					   trans_list);
174850d9aa99SJosef Bacik 		list_del_init(&ordered->trans_list);
174950d9aa99SJosef Bacik 		spin_unlock(&fs_info->trans_lock);
175050d9aa99SJosef Bacik 
175150d9aa99SJosef Bacik 		wait_event(ordered->wait, test_bit(BTRFS_ORDERED_COMPLETE,
175250d9aa99SJosef Bacik 						   &ordered->flags));
175350d9aa99SJosef Bacik 		btrfs_put_ordered_extent(ordered);
175450d9aa99SJosef Bacik 		spin_lock(&fs_info->trans_lock);
175550d9aa99SJosef Bacik 	}
175650d9aa99SJosef Bacik 	spin_unlock(&fs_info->trans_lock);
175750d9aa99SJosef Bacik }
175850d9aa99SJosef Bacik 
175979154b1bSChris Mason int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
176079154b1bSChris Mason 			     struct btrfs_root *root)
176179154b1bSChris Mason {
176249b25e05SJeff Mahoney 	struct btrfs_transaction *cur_trans = trans->transaction;
17638fd17795SChris Mason 	struct btrfs_transaction *prev_trans = NULL;
1764656f30dbSFilipe Manana 	struct btrfs_inode *btree_ino = BTRFS_I(root->fs_info->btree_inode);
176525287e0aSMiao Xie 	int ret;
176679154b1bSChris Mason 
17678d25a086SMiao Xie 	/* Stop the commit early if ->aborted is set */
17688d25a086SMiao Xie 	if (unlikely(ACCESS_ONCE(cur_trans->aborted))) {
176925287e0aSMiao Xie 		ret = cur_trans->aborted;
1770e4a2bcacSJosef Bacik 		btrfs_end_transaction(trans, root);
1771e4a2bcacSJosef Bacik 		return ret;
177225287e0aSMiao Xie 	}
177349b25e05SJeff Mahoney 
177456bec294SChris Mason 	/* make a pass through all the delayed refs we have so far
177556bec294SChris Mason 	 * any runnings procs may add more while we are here
177656bec294SChris Mason 	 */
177756bec294SChris Mason 	ret = btrfs_run_delayed_refs(trans, root, 0);
1778e4a2bcacSJosef Bacik 	if (ret) {
1779e4a2bcacSJosef Bacik 		btrfs_end_transaction(trans, root);
1780e4a2bcacSJosef Bacik 		return ret;
1781e4a2bcacSJosef Bacik 	}
178256bec294SChris Mason 
17830e721106SJosef Bacik 	btrfs_trans_release_metadata(trans, root);
17840e721106SJosef Bacik 	trans->block_rsv = NULL;
1785272d26d0SMiao Xie 	if (trans->qgroup_reserved) {
1786272d26d0SMiao Xie 		btrfs_qgroup_free(root, trans->qgroup_reserved);
1787272d26d0SMiao Xie 		trans->qgroup_reserved = 0;
1788272d26d0SMiao Xie 	}
17890e721106SJosef Bacik 
1790b7ec40d7SChris Mason 	cur_trans = trans->transaction;
179149b25e05SJeff Mahoney 
179256bec294SChris Mason 	/*
179356bec294SChris Mason 	 * set the flushing flag so procs in this transaction have to
179456bec294SChris Mason 	 * start sending their work down.
179556bec294SChris Mason 	 */
1796b7ec40d7SChris Mason 	cur_trans->delayed_refs.flushing = 1;
17971be41b78SJosef Bacik 	smp_wmb();
179856bec294SChris Mason 
1799ea658badSJosef Bacik 	if (!list_empty(&trans->new_bgs))
1800ea658badSJosef Bacik 		btrfs_create_pending_block_groups(trans, root);
1801ea658badSJosef Bacik 
1802c3e69d58SChris Mason 	ret = btrfs_run_delayed_refs(trans, root, 0);
1803e4a2bcacSJosef Bacik 	if (ret) {
1804e4a2bcacSJosef Bacik 		btrfs_end_transaction(trans, root);
1805e4a2bcacSJosef Bacik 		return ret;
1806e4a2bcacSJosef Bacik 	}
180756bec294SChris Mason 
18084a9d8bdeSMiao Xie 	spin_lock(&root->fs_info->trans_lock);
180950d9aa99SJosef Bacik 	list_splice(&trans->ordered, &cur_trans->pending_ordered);
18104a9d8bdeSMiao Xie 	if (cur_trans->state >= TRANS_STATE_COMMIT_START) {
18114a9d8bdeSMiao Xie 		spin_unlock(&root->fs_info->trans_lock);
181213c5a93eSJosef Bacik 		atomic_inc(&cur_trans->use_count);
181349b25e05SJeff Mahoney 		ret = btrfs_end_transaction(trans, root);
1814ccd467d6SChris Mason 
1815b9c8300cSLi Zefan 		wait_for_commit(root, cur_trans);
181615ee9bc7SJosef Bacik 
1817724e2315SJosef Bacik 		btrfs_put_transaction(cur_trans);
181815ee9bc7SJosef Bacik 
181949b25e05SJeff Mahoney 		return ret;
182079154b1bSChris Mason 	}
18214313b399SChris Mason 
18224a9d8bdeSMiao Xie 	cur_trans->state = TRANS_STATE_COMMIT_START;
1823bb9c12c9SSage Weil 	wake_up(&root->fs_info->transaction_blocked_wait);
1824bb9c12c9SSage Weil 
1825ccd467d6SChris Mason 	if (cur_trans->list.prev != &root->fs_info->trans_list) {
1826ccd467d6SChris Mason 		prev_trans = list_entry(cur_trans->list.prev,
1827ccd467d6SChris Mason 					struct btrfs_transaction, list);
18284a9d8bdeSMiao Xie 		if (prev_trans->state != TRANS_STATE_COMPLETED) {
182913c5a93eSJosef Bacik 			atomic_inc(&prev_trans->use_count);
1830a4abeea4SJosef Bacik 			spin_unlock(&root->fs_info->trans_lock);
1831ccd467d6SChris Mason 
1832ccd467d6SChris Mason 			wait_for_commit(root, prev_trans);
1833ccd467d6SChris Mason 
1834724e2315SJosef Bacik 			btrfs_put_transaction(prev_trans);
1835a4abeea4SJosef Bacik 		} else {
1836a4abeea4SJosef Bacik 			spin_unlock(&root->fs_info->trans_lock);
1837ccd467d6SChris Mason 		}
1838a4abeea4SJosef Bacik 	} else {
1839a4abeea4SJosef Bacik 		spin_unlock(&root->fs_info->trans_lock);
1840ccd467d6SChris Mason 	}
184115ee9bc7SJosef Bacik 
18420860adfdSMiao Xie 	extwriter_counter_dec(cur_trans, trans->type);
18430860adfdSMiao Xie 
184482436617SMiao Xie 	ret = btrfs_start_delalloc_flush(root->fs_info);
184582436617SMiao Xie 	if (ret)
184682436617SMiao Xie 		goto cleanup_transaction;
184782436617SMiao Xie 
18488d875f95SChris Mason 	ret = btrfs_run_delayed_items(trans, root);
184949b25e05SJeff Mahoney 	if (ret)
185049b25e05SJeff Mahoney 		goto cleanup_transaction;
185116cdcec7SMiao Xie 
1852581227d0SMiao Xie 	wait_event(cur_trans->writer_wait,
1853581227d0SMiao Xie 		   extwriter_counter_read(cur_trans) == 0);
1854ed3b3d31SChris Mason 
1855581227d0SMiao Xie 	/* some pending stuffs might be added after the previous flush. */
18568d875f95SChris Mason 	ret = btrfs_run_delayed_items(trans, root);
1857ca469637SMiao Xie 	if (ret)
1858ca469637SMiao Xie 		goto cleanup_transaction;
1859ca469637SMiao Xie 
186082436617SMiao Xie 	btrfs_wait_delalloc_flush(root->fs_info);
1861cb7ab021SWang Shilong 
186250d9aa99SJosef Bacik 	btrfs_wait_pending_ordered(cur_trans, root->fs_info);
186350d9aa99SJosef Bacik 
1864cb7ab021SWang Shilong 	btrfs_scrub_pause(root);
1865ed0ca140SJosef Bacik 	/*
1866ed0ca140SJosef Bacik 	 * Ok now we need to make sure to block out any other joins while we
1867ed0ca140SJosef Bacik 	 * commit the transaction.  We could have started a join before setting
18684a9d8bdeSMiao Xie 	 * COMMIT_DOING so make sure to wait for num_writers to == 1 again.
1869ed0ca140SJosef Bacik 	 */
1870a4abeea4SJosef Bacik 	spin_lock(&root->fs_info->trans_lock);
18714a9d8bdeSMiao Xie 	cur_trans->state = TRANS_STATE_COMMIT_DOING;
1872a4abeea4SJosef Bacik 	spin_unlock(&root->fs_info->trans_lock);
1873ed0ca140SJosef Bacik 	wait_event(cur_trans->writer_wait,
1874ed0ca140SJosef Bacik 		   atomic_read(&cur_trans->num_writers) == 1);
187515ee9bc7SJosef Bacik 
18762cba30f1SMiao Xie 	/* ->aborted might be set after the previous check, so check it */
18772cba30f1SMiao Xie 	if (unlikely(ACCESS_ONCE(cur_trans->aborted))) {
18782cba30f1SMiao Xie 		ret = cur_trans->aborted;
18796cf7f77eSWang Shilong 		goto scrub_continue;
18802cba30f1SMiao Xie 	}
18817585717fSChris Mason 	/*
18827585717fSChris Mason 	 * the reloc mutex makes sure that we stop
18837585717fSChris Mason 	 * the balancing code from coming in and moving
18847585717fSChris Mason 	 * extents around in the middle of the commit
18857585717fSChris Mason 	 */
18867585717fSChris Mason 	mutex_lock(&root->fs_info->reloc_mutex);
18877585717fSChris Mason 
188842874b3dSMiao Xie 	/*
188942874b3dSMiao Xie 	 * We needn't worry about the delayed items because we will
189042874b3dSMiao Xie 	 * deal with them in create_pending_snapshot(), which is the
189142874b3dSMiao Xie 	 * core function of the snapshot creation.
189242874b3dSMiao Xie 	 */
189342874b3dSMiao Xie 	ret = create_pending_snapshots(trans, root->fs_info);
189449b25e05SJeff Mahoney 	if (ret) {
189549b25e05SJeff Mahoney 		mutex_unlock(&root->fs_info->reloc_mutex);
18966cf7f77eSWang Shilong 		goto scrub_continue;
189749b25e05SJeff Mahoney 	}
18983063d29fSChris Mason 
189942874b3dSMiao Xie 	/*
190042874b3dSMiao Xie 	 * We insert the dir indexes of the snapshots and update the inode
190142874b3dSMiao Xie 	 * of the snapshots' parents after the snapshot creation, so there
190242874b3dSMiao Xie 	 * are some delayed items which are not dealt with. Now deal with
190342874b3dSMiao Xie 	 * them.
190442874b3dSMiao Xie 	 *
190542874b3dSMiao Xie 	 * We needn't worry that this operation will corrupt the snapshots,
190642874b3dSMiao Xie 	 * because all the tree which are snapshoted will be forced to COW
190742874b3dSMiao Xie 	 * the nodes and leaves.
190842874b3dSMiao Xie 	 */
190942874b3dSMiao Xie 	ret = btrfs_run_delayed_items(trans, root);
191049b25e05SJeff Mahoney 	if (ret) {
191149b25e05SJeff Mahoney 		mutex_unlock(&root->fs_info->reloc_mutex);
19126cf7f77eSWang Shilong 		goto scrub_continue;
191349b25e05SJeff Mahoney 	}
191416cdcec7SMiao Xie 
191556bec294SChris Mason 	ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
191649b25e05SJeff Mahoney 	if (ret) {
191749b25e05SJeff Mahoney 		mutex_unlock(&root->fs_info->reloc_mutex);
19186cf7f77eSWang Shilong 		goto scrub_continue;
191949b25e05SJeff Mahoney 	}
192056bec294SChris Mason 
1921e999376fSChris Mason 	/*
1922e999376fSChris Mason 	 * make sure none of the code above managed to slip in a
1923e999376fSChris Mason 	 * delayed item
1924e999376fSChris Mason 	 */
1925e999376fSChris Mason 	btrfs_assert_delayed_root_empty(root);
1926e999376fSChris Mason 
19272c90e5d6SChris Mason 	WARN_ON(cur_trans != trans->transaction);
1928dc17ff8fSChris Mason 
1929e02119d5SChris Mason 	/* btrfs_commit_tree_roots is responsible for getting the
1930e02119d5SChris Mason 	 * various roots consistent with each other.  Every pointer
1931e02119d5SChris Mason 	 * in the tree of tree roots has to point to the most up to date
1932e02119d5SChris Mason 	 * root for every subvolume and other tree.  So, we have to keep
1933e02119d5SChris Mason 	 * the tree logging code from jumping in and changing any
1934e02119d5SChris Mason 	 * of the trees.
1935e02119d5SChris Mason 	 *
1936e02119d5SChris Mason 	 * At this point in the commit, there can't be any tree-log
1937e02119d5SChris Mason 	 * writers, but a little lower down we drop the trans mutex
1938e02119d5SChris Mason 	 * and let new people in.  By holding the tree_log_mutex
1939e02119d5SChris Mason 	 * from now until after the super is written, we avoid races
1940e02119d5SChris Mason 	 * with the tree-log code.
1941e02119d5SChris Mason 	 */
1942e02119d5SChris Mason 	mutex_lock(&root->fs_info->tree_log_mutex);
19431a40e23bSZheng Yan 
19445d4f98a2SYan Zheng 	ret = commit_fs_roots(trans, root);
194549b25e05SJeff Mahoney 	if (ret) {
194649b25e05SJeff Mahoney 		mutex_unlock(&root->fs_info->tree_log_mutex);
1947871383beSDavid Sterba 		mutex_unlock(&root->fs_info->reloc_mutex);
19486cf7f77eSWang Shilong 		goto scrub_continue;
194949b25e05SJeff Mahoney 	}
195054aa1f4dSChris Mason 
19513818aea2SQu Wenruo 	/*
19527e1876acSDavid Sterba 	 * Since the transaction is done, we can apply the pending changes
19537e1876acSDavid Sterba 	 * before the next transaction.
19543818aea2SQu Wenruo 	 */
1955572d9ab7SDavid Sterba 	btrfs_apply_pending_changes(root->fs_info);
19563818aea2SQu Wenruo 
19575d4f98a2SYan Zheng 	/* commit_fs_roots gets rid of all the tree log roots, it is now
1958e02119d5SChris Mason 	 * safe to free the root of tree log roots
1959e02119d5SChris Mason 	 */
1960e02119d5SChris Mason 	btrfs_free_log_root_tree(trans, root->fs_info);
1961e02119d5SChris Mason 
19625d4f98a2SYan Zheng 	ret = commit_cowonly_roots(trans, root);
196349b25e05SJeff Mahoney 	if (ret) {
196449b25e05SJeff Mahoney 		mutex_unlock(&root->fs_info->tree_log_mutex);
1965871383beSDavid Sterba 		mutex_unlock(&root->fs_info->reloc_mutex);
19666cf7f77eSWang Shilong 		goto scrub_continue;
196749b25e05SJeff Mahoney 	}
196854aa1f4dSChris Mason 
19692cba30f1SMiao Xie 	/*
19702cba30f1SMiao Xie 	 * The tasks which save the space cache and inode cache may also
19712cba30f1SMiao Xie 	 * update ->aborted, check it.
19722cba30f1SMiao Xie 	 */
19732cba30f1SMiao Xie 	if (unlikely(ACCESS_ONCE(cur_trans->aborted))) {
19742cba30f1SMiao Xie 		ret = cur_trans->aborted;
19752cba30f1SMiao Xie 		mutex_unlock(&root->fs_info->tree_log_mutex);
19762cba30f1SMiao Xie 		mutex_unlock(&root->fs_info->reloc_mutex);
19776cf7f77eSWang Shilong 		goto scrub_continue;
19782cba30f1SMiao Xie 	}
19792cba30f1SMiao Xie 
198011833d66SYan Zheng 	btrfs_prepare_extent_commit(trans, root);
198111833d66SYan Zheng 
198278fae27eSChris Mason 	cur_trans = root->fs_info->running_transaction;
19835f39d397SChris Mason 
19845d4f98a2SYan Zheng 	btrfs_set_root_node(&root->fs_info->tree_root->root_item,
19855d4f98a2SYan Zheng 			    root->fs_info->tree_root->node);
19869e351cc8SJosef Bacik 	list_add_tail(&root->fs_info->tree_root->dirty_list,
19879e351cc8SJosef Bacik 		      &cur_trans->switch_commits);
19885d4f98a2SYan Zheng 
19895d4f98a2SYan Zheng 	btrfs_set_root_node(&root->fs_info->chunk_root->root_item,
19905d4f98a2SYan Zheng 			    root->fs_info->chunk_root->node);
19919e351cc8SJosef Bacik 	list_add_tail(&root->fs_info->chunk_root->dirty_list,
19929e351cc8SJosef Bacik 		      &cur_trans->switch_commits);
19939e351cc8SJosef Bacik 
19949e351cc8SJosef Bacik 	switch_commit_roots(cur_trans, root->fs_info);
19955d4f98a2SYan Zheng 
1996edf39272SJan Schmidt 	assert_qgroups_uptodate(trans);
1997ce93ec54SJosef Bacik 	ASSERT(list_empty(&cur_trans->dirty_bgs));
19985d4f98a2SYan Zheng 	update_super_roots(root);
1999e02119d5SChris Mason 
20006c41761fSDavid Sterba 	btrfs_set_super_log_root(root->fs_info->super_copy, 0);
20016c41761fSDavid Sterba 	btrfs_set_super_log_root_level(root->fs_info->super_copy, 0);
20026c41761fSDavid Sterba 	memcpy(root->fs_info->super_for_commit, root->fs_info->super_copy,
20036c41761fSDavid Sterba 	       sizeof(*root->fs_info->super_copy));
2004ccd467d6SChris Mason 
2005935e5cc9SMiao Xie 	btrfs_update_commit_device_size(root->fs_info);
2006ce7213c7SMiao Xie 	btrfs_update_commit_device_bytes_used(root, cur_trans);
2007935e5cc9SMiao Xie 
2008656f30dbSFilipe Manana 	clear_bit(BTRFS_INODE_BTREE_LOG1_ERR, &btree_ino->runtime_flags);
2009656f30dbSFilipe Manana 	clear_bit(BTRFS_INODE_BTREE_LOG2_ERR, &btree_ino->runtime_flags);
2010656f30dbSFilipe Manana 
2011a4abeea4SJosef Bacik 	spin_lock(&root->fs_info->trans_lock);
20124a9d8bdeSMiao Xie 	cur_trans->state = TRANS_STATE_UNBLOCKED;
2013a4abeea4SJosef Bacik 	root->fs_info->running_transaction = NULL;
2014a4abeea4SJosef Bacik 	spin_unlock(&root->fs_info->trans_lock);
20157585717fSChris Mason 	mutex_unlock(&root->fs_info->reloc_mutex);
2016b7ec40d7SChris Mason 
2017f9295749SChris Mason 	wake_up(&root->fs_info->transaction_wait);
2018e6dcd2dcSChris Mason 
201979154b1bSChris Mason 	ret = btrfs_write_and_wait_transaction(trans, root);
202049b25e05SJeff Mahoney 	if (ret) {
202149b25e05SJeff Mahoney 		btrfs_error(root->fs_info, ret,
202208748810SDavid Sterba 			    "Error while writing out transaction");
202349b25e05SJeff Mahoney 		mutex_unlock(&root->fs_info->tree_log_mutex);
20246cf7f77eSWang Shilong 		goto scrub_continue;
202549b25e05SJeff Mahoney 	}
202649b25e05SJeff Mahoney 
202749b25e05SJeff Mahoney 	ret = write_ctree_super(trans, root, 0);
202849b25e05SJeff Mahoney 	if (ret) {
202949b25e05SJeff Mahoney 		mutex_unlock(&root->fs_info->tree_log_mutex);
20306cf7f77eSWang Shilong 		goto scrub_continue;
203149b25e05SJeff Mahoney 	}
20324313b399SChris Mason 
2033e02119d5SChris Mason 	/*
2034e02119d5SChris Mason 	 * the super is written, we can safely allow the tree-loggers
2035e02119d5SChris Mason 	 * to go about their business
2036e02119d5SChris Mason 	 */
2037e02119d5SChris Mason 	mutex_unlock(&root->fs_info->tree_log_mutex);
2038e02119d5SChris Mason 
203911833d66SYan Zheng 	btrfs_finish_extent_commit(trans, root);
20404313b399SChris Mason 
204113212b54SZhao Lei 	if (cur_trans->have_free_bgs)
204213212b54SZhao Lei 		btrfs_clear_space_info_full(root->fs_info);
204313212b54SZhao Lei 
204415ee9bc7SJosef Bacik 	root->fs_info->last_trans_committed = cur_trans->transid;
20454a9d8bdeSMiao Xie 	/*
20464a9d8bdeSMiao Xie 	 * We needn't acquire the lock here because there is no other task
20474a9d8bdeSMiao Xie 	 * which can change it.
20484a9d8bdeSMiao Xie 	 */
20494a9d8bdeSMiao Xie 	cur_trans->state = TRANS_STATE_COMPLETED;
20502c90e5d6SChris Mason 	wake_up(&cur_trans->commit_wait);
20513de4586cSChris Mason 
2052a4abeea4SJosef Bacik 	spin_lock(&root->fs_info->trans_lock);
205313c5a93eSJosef Bacik 	list_del_init(&cur_trans->list);
2054a4abeea4SJosef Bacik 	spin_unlock(&root->fs_info->trans_lock);
2055a4abeea4SJosef Bacik 
2056724e2315SJosef Bacik 	btrfs_put_transaction(cur_trans);
2057724e2315SJosef Bacik 	btrfs_put_transaction(cur_trans);
205858176a96SJosef Bacik 
20590860adfdSMiao Xie 	if (trans->type & __TRANS_FREEZABLE)
2060b2b5ef5cSJan Kara 		sb_end_intwrite(root->fs_info->sb);
2061b2b5ef5cSJan Kara 
20621abe9b8aSliubo 	trace_btrfs_transaction_commit(root);
20631abe9b8aSliubo 
2064a2de733cSArne Jansen 	btrfs_scrub_continue(root);
2065a2de733cSArne Jansen 
20669ed74f2dSJosef Bacik 	if (current->journal_info == trans)
20679ed74f2dSJosef Bacik 		current->journal_info = NULL;
20689ed74f2dSJosef Bacik 
20692c90e5d6SChris Mason 	kmem_cache_free(btrfs_trans_handle_cachep, trans);
207024bbcf04SYan, Zheng 
207124bbcf04SYan, Zheng 	if (current != root->fs_info->transaction_kthread)
207224bbcf04SYan, Zheng 		btrfs_run_delayed_iputs(root);
207324bbcf04SYan, Zheng 
207479154b1bSChris Mason 	return ret;
207549b25e05SJeff Mahoney 
20766cf7f77eSWang Shilong scrub_continue:
20776cf7f77eSWang Shilong 	btrfs_scrub_continue(root);
207849b25e05SJeff Mahoney cleanup_transaction:
20790e721106SJosef Bacik 	btrfs_trans_release_metadata(trans, root);
20800e721106SJosef Bacik 	trans->block_rsv = NULL;
2081272d26d0SMiao Xie 	if (trans->qgroup_reserved) {
2082272d26d0SMiao Xie 		btrfs_qgroup_free(root, trans->qgroup_reserved);
2083272d26d0SMiao Xie 		trans->qgroup_reserved = 0;
2084272d26d0SMiao Xie 	}
2085c2cf52ebSSimon Kirby 	btrfs_warn(root->fs_info, "Skipping commit of aborted transaction.");
208649b25e05SJeff Mahoney 	if (current->journal_info == trans)
208749b25e05SJeff Mahoney 		current->journal_info = NULL;
20887b8b92afSJosef Bacik 	cleanup_transaction(trans, root, ret);
208949b25e05SJeff Mahoney 
209049b25e05SJeff Mahoney 	return ret;
209179154b1bSChris Mason }
209279154b1bSChris Mason 
2093d352ac68SChris Mason /*
20949d1a2a3aSDavid Sterba  * return < 0 if error
20959d1a2a3aSDavid Sterba  * 0 if there are no more dead_roots at the time of call
20969d1a2a3aSDavid Sterba  * 1 there are more to be processed, call me again
20979d1a2a3aSDavid Sterba  *
20989d1a2a3aSDavid Sterba  * The return value indicates there are certainly more snapshots to delete, but
20999d1a2a3aSDavid Sterba  * if there comes a new one during processing, it may return 0. We don't mind,
21009d1a2a3aSDavid Sterba  * because btrfs_commit_super will poke cleaner thread and it will process it a
21019d1a2a3aSDavid Sterba  * few seconds later.
2102d352ac68SChris Mason  */
21039d1a2a3aSDavid Sterba int btrfs_clean_one_deleted_snapshot(struct btrfs_root *root)
2104e9d0b13bSChris Mason {
21059d1a2a3aSDavid Sterba 	int ret;
21065d4f98a2SYan Zheng 	struct btrfs_fs_info *fs_info = root->fs_info;
2107e9d0b13bSChris Mason 
2108a4abeea4SJosef Bacik 	spin_lock(&fs_info->trans_lock);
21099d1a2a3aSDavid Sterba 	if (list_empty(&fs_info->dead_roots)) {
21109d1a2a3aSDavid Sterba 		spin_unlock(&fs_info->trans_lock);
21119d1a2a3aSDavid Sterba 		return 0;
21129d1a2a3aSDavid Sterba 	}
21139d1a2a3aSDavid Sterba 	root = list_first_entry(&fs_info->dead_roots,
21149d1a2a3aSDavid Sterba 			struct btrfs_root, root_list);
2115cfad392bSJosef Bacik 	list_del_init(&root->root_list);
2116a4abeea4SJosef Bacik 	spin_unlock(&fs_info->trans_lock);
21175d4f98a2SYan Zheng 
2118efe120a0SFrank Holton 	pr_debug("BTRFS: cleaner removing %llu\n", root->objectid);
211976dda93cSYan, Zheng 
212016cdcec7SMiao Xie 	btrfs_kill_all_delayed_nodes(root);
212116cdcec7SMiao Xie 
212276dda93cSYan, Zheng 	if (btrfs_header_backref_rev(root->node) <
212376dda93cSYan, Zheng 			BTRFS_MIXED_BACKREF_REV)
21242c536799SJeff Mahoney 		ret = btrfs_drop_snapshot(root, NULL, 0, 0);
212576dda93cSYan, Zheng 	else
21262c536799SJeff Mahoney 		ret = btrfs_drop_snapshot(root, NULL, 1, 0);
212732471dc2SDavid Sterba 
21286596a928SJosef Bacik 	return (ret < 0) ? 0 : 1;
2129e9d0b13bSChris Mason }
2130572d9ab7SDavid Sterba 
2131572d9ab7SDavid Sterba void btrfs_apply_pending_changes(struct btrfs_fs_info *fs_info)
2132572d9ab7SDavid Sterba {
2133572d9ab7SDavid Sterba 	unsigned long prev;
2134572d9ab7SDavid Sterba 	unsigned long bit;
2135572d9ab7SDavid Sterba 
2136572d9ab7SDavid Sterba 	prev = cmpxchg(&fs_info->pending_changes, 0, 0);
2137572d9ab7SDavid Sterba 	if (!prev)
2138572d9ab7SDavid Sterba 		return;
2139572d9ab7SDavid Sterba 
21407e1876acSDavid Sterba 	bit = 1 << BTRFS_PENDING_SET_INODE_MAP_CACHE;
21417e1876acSDavid Sterba 	if (prev & bit)
21427e1876acSDavid Sterba 		btrfs_set_opt(fs_info->mount_opt, INODE_MAP_CACHE);
21437e1876acSDavid Sterba 	prev &= ~bit;
21447e1876acSDavid Sterba 
21457e1876acSDavid Sterba 	bit = 1 << BTRFS_PENDING_CLEAR_INODE_MAP_CACHE;
21467e1876acSDavid Sterba 	if (prev & bit)
21477e1876acSDavid Sterba 		btrfs_clear_opt(fs_info->mount_opt, INODE_MAP_CACHE);
21487e1876acSDavid Sterba 	prev &= ~bit;
21497e1876acSDavid Sterba 
2150d51033d0SDavid Sterba 	bit = 1 << BTRFS_PENDING_COMMIT;
2151d51033d0SDavid Sterba 	if (prev & bit)
2152d51033d0SDavid Sterba 		btrfs_debug(fs_info, "pending commit done");
2153d51033d0SDavid Sterba 	prev &= ~bit;
2154d51033d0SDavid Sterba 
2155572d9ab7SDavid Sterba 	if (prev)
2156572d9ab7SDavid Sterba 		btrfs_warn(fs_info,
2157572d9ab7SDavid Sterba 			"unknown pending changes left 0x%lx, ignoring", prev);
2158572d9ab7SDavid Sterba }
2159