xref: /openbmc/linux/fs/btrfs/transaction.c (revision 2b9dbef2)
16cbd5570SChris Mason /*
26cbd5570SChris Mason  * Copyright (C) 2007 Oracle.  All rights reserved.
36cbd5570SChris Mason  *
46cbd5570SChris Mason  * This program is free software; you can redistribute it and/or
56cbd5570SChris Mason  * modify it under the terms of the GNU General Public
66cbd5570SChris Mason  * License v2 as published by the Free Software Foundation.
76cbd5570SChris Mason  *
86cbd5570SChris Mason  * This program is distributed in the hope that it will be useful,
96cbd5570SChris Mason  * but WITHOUT ANY WARRANTY; without even the implied warranty of
106cbd5570SChris Mason  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
116cbd5570SChris Mason  * General Public License for more details.
126cbd5570SChris Mason  *
136cbd5570SChris Mason  * You should have received a copy of the GNU General Public
146cbd5570SChris Mason  * License along with this program; if not, write to the
156cbd5570SChris Mason  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
166cbd5570SChris Mason  * Boston, MA 021110-1307, USA.
176cbd5570SChris Mason  */
186cbd5570SChris Mason 
1979154b1bSChris Mason #include <linux/fs.h>
205a0e3ad6STejun Heo #include <linux/slab.h>
2134088780SChris Mason #include <linux/sched.h>
22d3c2fdcfSChris Mason #include <linux/writeback.h>
235f39d397SChris Mason #include <linux/pagemap.h>
245f2cc086SChris Mason #include <linux/blkdev.h>
258ea05e3aSAlexander Block #include <linux/uuid.h>
2679154b1bSChris Mason #include "ctree.h"
2779154b1bSChris Mason #include "disk-io.h"
2879154b1bSChris Mason #include "transaction.h"
29925baeddSChris Mason #include "locking.h"
30e02119d5SChris Mason #include "tree-log.h"
31581bb050SLi Zefan #include "inode-map.h"
32733f4fbbSStefan Behrens #include "volumes.h"
338dabb742SStefan Behrens #include "dev-replace.h"
34fcebe456SJosef Bacik #include "qgroup.h"
3579154b1bSChris Mason 
360f7d52f4SChris Mason #define BTRFS_ROOT_TRANS_TAG 0
370f7d52f4SChris Mason 
38e8c9f186SDavid Sterba static const unsigned int btrfs_blocked_trans_types[TRANS_STATE_MAX] = {
394a9d8bdeSMiao Xie 	[TRANS_STATE_RUNNING]		= 0U,
404a9d8bdeSMiao Xie 	[TRANS_STATE_BLOCKED]		= (__TRANS_USERSPACE |
414a9d8bdeSMiao Xie 					   __TRANS_START),
424a9d8bdeSMiao Xie 	[TRANS_STATE_COMMIT_START]	= (__TRANS_USERSPACE |
434a9d8bdeSMiao Xie 					   __TRANS_START |
444a9d8bdeSMiao Xie 					   __TRANS_ATTACH),
454a9d8bdeSMiao Xie 	[TRANS_STATE_COMMIT_DOING]	= (__TRANS_USERSPACE |
464a9d8bdeSMiao Xie 					   __TRANS_START |
474a9d8bdeSMiao Xie 					   __TRANS_ATTACH |
484a9d8bdeSMiao Xie 					   __TRANS_JOIN),
494a9d8bdeSMiao Xie 	[TRANS_STATE_UNBLOCKED]		= (__TRANS_USERSPACE |
504a9d8bdeSMiao Xie 					   __TRANS_START |
514a9d8bdeSMiao Xie 					   __TRANS_ATTACH |
524a9d8bdeSMiao Xie 					   __TRANS_JOIN |
534a9d8bdeSMiao Xie 					   __TRANS_JOIN_NOLOCK),
544a9d8bdeSMiao Xie 	[TRANS_STATE_COMPLETED]		= (__TRANS_USERSPACE |
554a9d8bdeSMiao Xie 					   __TRANS_START |
564a9d8bdeSMiao Xie 					   __TRANS_ATTACH |
574a9d8bdeSMiao Xie 					   __TRANS_JOIN |
584a9d8bdeSMiao Xie 					   __TRANS_JOIN_NOLOCK),
594a9d8bdeSMiao Xie };
604a9d8bdeSMiao Xie 
61724e2315SJosef Bacik void btrfs_put_transaction(struct btrfs_transaction *transaction)
6279154b1bSChris Mason {
6313c5a93eSJosef Bacik 	WARN_ON(atomic_read(&transaction->use_count) == 0);
6413c5a93eSJosef Bacik 	if (atomic_dec_and_test(&transaction->use_count)) {
65a4abeea4SJosef Bacik 		BUG_ON(!list_empty(&transaction->list));
66c46effa6SLiu Bo 		WARN_ON(!RB_EMPTY_ROOT(&transaction->delayed_refs.href_root));
671262133bSJosef Bacik 		if (transaction->delayed_refs.pending_csums)
681262133bSJosef Bacik 			printk(KERN_ERR "pending csums is %llu\n",
691262133bSJosef Bacik 			       transaction->delayed_refs.pending_csums);
706df9a95eSJosef Bacik 		while (!list_empty(&transaction->pending_chunks)) {
716df9a95eSJosef Bacik 			struct extent_map *em;
726df9a95eSJosef Bacik 
736df9a95eSJosef Bacik 			em = list_first_entry(&transaction->pending_chunks,
746df9a95eSJosef Bacik 					      struct extent_map, list);
756df9a95eSJosef Bacik 			list_del_init(&em->list);
766df9a95eSJosef Bacik 			free_extent_map(em);
776df9a95eSJosef Bacik 		}
782c90e5d6SChris Mason 		kmem_cache_free(btrfs_transaction_cachep, transaction);
7979154b1bSChris Mason 	}
8078fae27eSChris Mason }
8179154b1bSChris Mason 
82663dfbb0SFilipe Manana static void clear_btree_io_tree(struct extent_io_tree *tree)
83663dfbb0SFilipe Manana {
84663dfbb0SFilipe Manana 	spin_lock(&tree->lock);
85663dfbb0SFilipe Manana 	while (!RB_EMPTY_ROOT(&tree->state)) {
86663dfbb0SFilipe Manana 		struct rb_node *node;
87663dfbb0SFilipe Manana 		struct extent_state *state;
88663dfbb0SFilipe Manana 
89663dfbb0SFilipe Manana 		node = rb_first(&tree->state);
90663dfbb0SFilipe Manana 		state = rb_entry(node, struct extent_state, rb_node);
91663dfbb0SFilipe Manana 		rb_erase(&state->rb_node, &tree->state);
92663dfbb0SFilipe Manana 		RB_CLEAR_NODE(&state->rb_node);
93663dfbb0SFilipe Manana 		/*
94663dfbb0SFilipe Manana 		 * btree io trees aren't supposed to have tasks waiting for
95663dfbb0SFilipe Manana 		 * changes in the flags of extent states ever.
96663dfbb0SFilipe Manana 		 */
97663dfbb0SFilipe Manana 		ASSERT(!waitqueue_active(&state->wq));
98663dfbb0SFilipe Manana 		free_extent_state(state);
99351810c1SDavid Sterba 
100351810c1SDavid Sterba 		cond_resched_lock(&tree->lock);
101663dfbb0SFilipe Manana 	}
102663dfbb0SFilipe Manana 	spin_unlock(&tree->lock);
103663dfbb0SFilipe Manana }
104663dfbb0SFilipe Manana 
1059e351cc8SJosef Bacik static noinline void switch_commit_roots(struct btrfs_transaction *trans,
1069e351cc8SJosef Bacik 					 struct btrfs_fs_info *fs_info)
107817d52f8SJosef Bacik {
1089e351cc8SJosef Bacik 	struct btrfs_root *root, *tmp;
1099e351cc8SJosef Bacik 
1109e351cc8SJosef Bacik 	down_write(&fs_info->commit_root_sem);
1119e351cc8SJosef Bacik 	list_for_each_entry_safe(root, tmp, &trans->switch_commits,
1129e351cc8SJosef Bacik 				 dirty_list) {
1139e351cc8SJosef Bacik 		list_del_init(&root->dirty_list);
114817d52f8SJosef Bacik 		free_extent_buffer(root->commit_root);
115817d52f8SJosef Bacik 		root->commit_root = btrfs_root_node(root);
1169e351cc8SJosef Bacik 		if (is_fstree(root->objectid))
1179e351cc8SJosef Bacik 			btrfs_unpin_free_ino(root);
118663dfbb0SFilipe Manana 		clear_btree_io_tree(&root->dirty_log_pages);
1199e351cc8SJosef Bacik 	}
1202b9dbef2SJosef Bacik 
1212b9dbef2SJosef Bacik 	/* We can free old roots now. */
1222b9dbef2SJosef Bacik 	spin_lock(&trans->dropped_roots_lock);
1232b9dbef2SJosef Bacik 	while (!list_empty(&trans->dropped_roots)) {
1242b9dbef2SJosef Bacik 		root = list_first_entry(&trans->dropped_roots,
1252b9dbef2SJosef Bacik 					struct btrfs_root, root_list);
1262b9dbef2SJosef Bacik 		list_del_init(&root->root_list);
1272b9dbef2SJosef Bacik 		spin_unlock(&trans->dropped_roots_lock);
1282b9dbef2SJosef Bacik 		btrfs_drop_and_free_fs_root(fs_info, root);
1292b9dbef2SJosef Bacik 		spin_lock(&trans->dropped_roots_lock);
1302b9dbef2SJosef Bacik 	}
1312b9dbef2SJosef Bacik 	spin_unlock(&trans->dropped_roots_lock);
1329e351cc8SJosef Bacik 	up_write(&fs_info->commit_root_sem);
133817d52f8SJosef Bacik }
134817d52f8SJosef Bacik 
1350860adfdSMiao Xie static inline void extwriter_counter_inc(struct btrfs_transaction *trans,
1360860adfdSMiao Xie 					 unsigned int type)
1370860adfdSMiao Xie {
1380860adfdSMiao Xie 	if (type & TRANS_EXTWRITERS)
1390860adfdSMiao Xie 		atomic_inc(&trans->num_extwriters);
1400860adfdSMiao Xie }
1410860adfdSMiao Xie 
1420860adfdSMiao Xie static inline void extwriter_counter_dec(struct btrfs_transaction *trans,
1430860adfdSMiao Xie 					 unsigned int type)
1440860adfdSMiao Xie {
1450860adfdSMiao Xie 	if (type & TRANS_EXTWRITERS)
1460860adfdSMiao Xie 		atomic_dec(&trans->num_extwriters);
1470860adfdSMiao Xie }
1480860adfdSMiao Xie 
1490860adfdSMiao Xie static inline void extwriter_counter_init(struct btrfs_transaction *trans,
1500860adfdSMiao Xie 					  unsigned int type)
1510860adfdSMiao Xie {
1520860adfdSMiao Xie 	atomic_set(&trans->num_extwriters, ((type & TRANS_EXTWRITERS) ? 1 : 0));
1530860adfdSMiao Xie }
1540860adfdSMiao Xie 
1550860adfdSMiao Xie static inline int extwriter_counter_read(struct btrfs_transaction *trans)
1560860adfdSMiao Xie {
1570860adfdSMiao Xie 	return atomic_read(&trans->num_extwriters);
158178260b2SMiao Xie }
159178260b2SMiao Xie 
160d352ac68SChris Mason /*
161d352ac68SChris Mason  * either allocate a new transaction or hop into the existing one
162d352ac68SChris Mason  */
1630860adfdSMiao Xie static noinline int join_transaction(struct btrfs_root *root, unsigned int type)
16479154b1bSChris Mason {
16579154b1bSChris Mason 	struct btrfs_transaction *cur_trans;
16619ae4e81SJan Schmidt 	struct btrfs_fs_info *fs_info = root->fs_info;
167a4abeea4SJosef Bacik 
16819ae4e81SJan Schmidt 	spin_lock(&fs_info->trans_lock);
169d43317dcSChris Mason loop:
17049b25e05SJeff Mahoney 	/* The file system has been taken offline. No new transactions. */
17187533c47SMiao Xie 	if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) {
17219ae4e81SJan Schmidt 		spin_unlock(&fs_info->trans_lock);
17349b25e05SJeff Mahoney 		return -EROFS;
17449b25e05SJeff Mahoney 	}
17549b25e05SJeff Mahoney 
17619ae4e81SJan Schmidt 	cur_trans = fs_info->running_transaction;
177a4abeea4SJosef Bacik 	if (cur_trans) {
178871383beSDavid Sterba 		if (cur_trans->aborted) {
17919ae4e81SJan Schmidt 			spin_unlock(&fs_info->trans_lock);
18049b25e05SJeff Mahoney 			return cur_trans->aborted;
181871383beSDavid Sterba 		}
1824a9d8bdeSMiao Xie 		if (btrfs_blocked_trans_types[cur_trans->state] & type) {
183178260b2SMiao Xie 			spin_unlock(&fs_info->trans_lock);
184178260b2SMiao Xie 			return -EBUSY;
185178260b2SMiao Xie 		}
186a4abeea4SJosef Bacik 		atomic_inc(&cur_trans->use_count);
187a4abeea4SJosef Bacik 		atomic_inc(&cur_trans->num_writers);
1880860adfdSMiao Xie 		extwriter_counter_inc(cur_trans, type);
18919ae4e81SJan Schmidt 		spin_unlock(&fs_info->trans_lock);
190a4abeea4SJosef Bacik 		return 0;
191a4abeea4SJosef Bacik 	}
19219ae4e81SJan Schmidt 	spin_unlock(&fs_info->trans_lock);
193a4abeea4SJosef Bacik 
194354aa0fbSMiao Xie 	/*
195354aa0fbSMiao Xie 	 * If we are ATTACH, we just want to catch the current transaction,
196354aa0fbSMiao Xie 	 * and commit it. If there is no transaction, just return ENOENT.
197354aa0fbSMiao Xie 	 */
198354aa0fbSMiao Xie 	if (type == TRANS_ATTACH)
199354aa0fbSMiao Xie 		return -ENOENT;
200354aa0fbSMiao Xie 
2014a9d8bdeSMiao Xie 	/*
2024a9d8bdeSMiao Xie 	 * JOIN_NOLOCK only happens during the transaction commit, so
2034a9d8bdeSMiao Xie 	 * it is impossible that ->running_transaction is NULL
2044a9d8bdeSMiao Xie 	 */
2054a9d8bdeSMiao Xie 	BUG_ON(type == TRANS_JOIN_NOLOCK);
2064a9d8bdeSMiao Xie 
207a4abeea4SJosef Bacik 	cur_trans = kmem_cache_alloc(btrfs_transaction_cachep, GFP_NOFS);
208db5b493aSTsutomu Itoh 	if (!cur_trans)
209db5b493aSTsutomu Itoh 		return -ENOMEM;
210d43317dcSChris Mason 
21119ae4e81SJan Schmidt 	spin_lock(&fs_info->trans_lock);
21219ae4e81SJan Schmidt 	if (fs_info->running_transaction) {
213d43317dcSChris Mason 		/*
214d43317dcSChris Mason 		 * someone started a transaction after we unlocked.  Make sure
2154a9d8bdeSMiao Xie 		 * to redo the checks above
216d43317dcSChris Mason 		 */
217a4abeea4SJosef Bacik 		kmem_cache_free(btrfs_transaction_cachep, cur_trans);
218d43317dcSChris Mason 		goto loop;
21987533c47SMiao Xie 	} else if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) {
220e4b50e14SDan Carpenter 		spin_unlock(&fs_info->trans_lock);
2217b8b92afSJosef Bacik 		kmem_cache_free(btrfs_transaction_cachep, cur_trans);
2227b8b92afSJosef Bacik 		return -EROFS;
223a4abeea4SJosef Bacik 	}
224d43317dcSChris Mason 
22513c5a93eSJosef Bacik 	atomic_set(&cur_trans->num_writers, 1);
2260860adfdSMiao Xie 	extwriter_counter_init(cur_trans, type);
22779154b1bSChris Mason 	init_waitqueue_head(&cur_trans->writer_wait);
22879154b1bSChris Mason 	init_waitqueue_head(&cur_trans->commit_wait);
2294a9d8bdeSMiao Xie 	cur_trans->state = TRANS_STATE_RUNNING;
230a4abeea4SJosef Bacik 	/*
231a4abeea4SJosef Bacik 	 * One for this trans handle, one so it will live on until we
232a4abeea4SJosef Bacik 	 * commit the transaction.
233a4abeea4SJosef Bacik 	 */
234a4abeea4SJosef Bacik 	atomic_set(&cur_trans->use_count, 2);
23513212b54SZhao Lei 	cur_trans->have_free_bgs = 0;
23608607c1bSChris Mason 	cur_trans->start_time = get_seconds();
2371bbc621eSChris Mason 	cur_trans->dirty_bg_run = 0;
23856bec294SChris Mason 
239c46effa6SLiu Bo 	cur_trans->delayed_refs.href_root = RB_ROOT;
2403368d001SQu Wenruo 	cur_trans->delayed_refs.dirty_extent_root = RB_ROOT;
241d7df2c79SJosef Bacik 	atomic_set(&cur_trans->delayed_refs.num_entries, 0);
242c3e69d58SChris Mason 	cur_trans->delayed_refs.num_heads_ready = 0;
2431262133bSJosef Bacik 	cur_trans->delayed_refs.pending_csums = 0;
244c3e69d58SChris Mason 	cur_trans->delayed_refs.num_heads = 0;
24556bec294SChris Mason 	cur_trans->delayed_refs.flushing = 0;
246c3e69d58SChris Mason 	cur_trans->delayed_refs.run_delayed_start = 0;
2479086db86SQu Wenruo 	cur_trans->delayed_refs.qgroup_to_skip = 0;
24820b297d6SJan Schmidt 
24920b297d6SJan Schmidt 	/*
25020b297d6SJan Schmidt 	 * although the tree mod log is per file system and not per transaction,
25120b297d6SJan Schmidt 	 * the log must never go across transaction boundaries.
25220b297d6SJan Schmidt 	 */
25320b297d6SJan Schmidt 	smp_mb();
25431b1a2bdSJulia Lawall 	if (!list_empty(&fs_info->tree_mod_seq_list))
255efe120a0SFrank Holton 		WARN(1, KERN_ERR "BTRFS: tree_mod_seq_list not empty when "
25620b297d6SJan Schmidt 			"creating a fresh transaction\n");
25731b1a2bdSJulia Lawall 	if (!RB_EMPTY_ROOT(&fs_info->tree_mod_log))
258efe120a0SFrank Holton 		WARN(1, KERN_ERR "BTRFS: tree_mod_log rb tree not empty when "
25920b297d6SJan Schmidt 			"creating a fresh transaction\n");
260fc36ed7eSJan Schmidt 	atomic64_set(&fs_info->tree_mod_seq, 0);
26120b297d6SJan Schmidt 
26256bec294SChris Mason 	spin_lock_init(&cur_trans->delayed_refs.lock);
26356bec294SChris Mason 
2643063d29fSChris Mason 	INIT_LIST_HEAD(&cur_trans->pending_snapshots);
2656df9a95eSJosef Bacik 	INIT_LIST_HEAD(&cur_trans->pending_chunks);
2669e351cc8SJosef Bacik 	INIT_LIST_HEAD(&cur_trans->switch_commits);
26750d9aa99SJosef Bacik 	INIT_LIST_HEAD(&cur_trans->pending_ordered);
268ce93ec54SJosef Bacik 	INIT_LIST_HEAD(&cur_trans->dirty_bgs);
2691bbc621eSChris Mason 	INIT_LIST_HEAD(&cur_trans->io_bgs);
2702b9dbef2SJosef Bacik 	INIT_LIST_HEAD(&cur_trans->dropped_roots);
2711bbc621eSChris Mason 	mutex_init(&cur_trans->cache_write_mutex);
272cb723e49SJosef Bacik 	cur_trans->num_dirty_bgs = 0;
273ce93ec54SJosef Bacik 	spin_lock_init(&cur_trans->dirty_bgs_lock);
274e33e17eeSJeff Mahoney 	INIT_LIST_HEAD(&cur_trans->deleted_bgs);
275e33e17eeSJeff Mahoney 	spin_lock_init(&cur_trans->deleted_bgs_lock);
2762b9dbef2SJosef Bacik 	spin_lock_init(&cur_trans->dropped_roots_lock);
27719ae4e81SJan Schmidt 	list_add_tail(&cur_trans->list, &fs_info->trans_list);
278d1310b2eSChris Mason 	extent_io_tree_init(&cur_trans->dirty_pages,
27919ae4e81SJan Schmidt 			     fs_info->btree_inode->i_mapping);
28019ae4e81SJan Schmidt 	fs_info->generation++;
28119ae4e81SJan Schmidt 	cur_trans->transid = fs_info->generation;
28219ae4e81SJan Schmidt 	fs_info->running_transaction = cur_trans;
28349b25e05SJeff Mahoney 	cur_trans->aborted = 0;
28419ae4e81SJan Schmidt 	spin_unlock(&fs_info->trans_lock);
28515ee9bc7SJosef Bacik 
28679154b1bSChris Mason 	return 0;
28779154b1bSChris Mason }
28879154b1bSChris Mason 
289d352ac68SChris Mason /*
290d397712bSChris Mason  * this does all the record keeping required to make sure that a reference
291d397712bSChris Mason  * counted root is properly recorded in a given transaction.  This is required
292d397712bSChris Mason  * to make sure the old root from before we joined the transaction is deleted
293d397712bSChris Mason  * when the transaction commits
294d352ac68SChris Mason  */
2957585717fSChris Mason static int record_root_in_trans(struct btrfs_trans_handle *trans,
2965d4f98a2SYan Zheng 			       struct btrfs_root *root)
2976702ed49SChris Mason {
29827cdeb70SMiao Xie 	if (test_bit(BTRFS_ROOT_REF_COWS, &root->state) &&
29927cdeb70SMiao Xie 	    root->last_trans < trans->transid) {
3006702ed49SChris Mason 		WARN_ON(root == root->fs_info->extent_root);
3015d4f98a2SYan Zheng 		WARN_ON(root->commit_root != root->node);
3025d4f98a2SYan Zheng 
3037585717fSChris Mason 		/*
30427cdeb70SMiao Xie 		 * see below for IN_TRANS_SETUP usage rules
3057585717fSChris Mason 		 * we have the reloc mutex held now, so there
3067585717fSChris Mason 		 * is only one writer in this function
3077585717fSChris Mason 		 */
30827cdeb70SMiao Xie 		set_bit(BTRFS_ROOT_IN_TRANS_SETUP, &root->state);
3097585717fSChris Mason 
31027cdeb70SMiao Xie 		/* make sure readers find IN_TRANS_SETUP before
3117585717fSChris Mason 		 * they find our root->last_trans update
3127585717fSChris Mason 		 */
3137585717fSChris Mason 		smp_wmb();
3147585717fSChris Mason 
315a4abeea4SJosef Bacik 		spin_lock(&root->fs_info->fs_roots_radix_lock);
316a4abeea4SJosef Bacik 		if (root->last_trans == trans->transid) {
317a4abeea4SJosef Bacik 			spin_unlock(&root->fs_info->fs_roots_radix_lock);
318a4abeea4SJosef Bacik 			return 0;
319a4abeea4SJosef Bacik 		}
3206702ed49SChris Mason 		radix_tree_tag_set(&root->fs_info->fs_roots_radix,
3216702ed49SChris Mason 			   (unsigned long)root->root_key.objectid,
3226702ed49SChris Mason 			   BTRFS_ROOT_TRANS_TAG);
323a4abeea4SJosef Bacik 		spin_unlock(&root->fs_info->fs_roots_radix_lock);
3247585717fSChris Mason 		root->last_trans = trans->transid;
3257585717fSChris Mason 
3267585717fSChris Mason 		/* this is pretty tricky.  We don't want to
3277585717fSChris Mason 		 * take the relocation lock in btrfs_record_root_in_trans
3287585717fSChris Mason 		 * unless we're really doing the first setup for this root in
3297585717fSChris Mason 		 * this transaction.
3307585717fSChris Mason 		 *
3317585717fSChris Mason 		 * Normally we'd use root->last_trans as a flag to decide
3327585717fSChris Mason 		 * if we want to take the expensive mutex.
3337585717fSChris Mason 		 *
3347585717fSChris Mason 		 * But, we have to set root->last_trans before we
3357585717fSChris Mason 		 * init the relocation root, otherwise, we trip over warnings
3367585717fSChris Mason 		 * in ctree.c.  The solution used here is to flag ourselves
33727cdeb70SMiao Xie 		 * with root IN_TRANS_SETUP.  When this is 1, we're still
3387585717fSChris Mason 		 * fixing up the reloc trees and everyone must wait.
3397585717fSChris Mason 		 *
3407585717fSChris Mason 		 * When this is zero, they can trust root->last_trans and fly
3417585717fSChris Mason 		 * through btrfs_record_root_in_trans without having to take the
3427585717fSChris Mason 		 * lock.  smp_wmb() makes sure that all the writes above are
3437585717fSChris Mason 		 * done before we pop in the zero below
3447585717fSChris Mason 		 */
3455d4f98a2SYan Zheng 		btrfs_init_reloc_root(trans, root);
346c7548af6SChris Mason 		smp_mb__before_atomic();
34727cdeb70SMiao Xie 		clear_bit(BTRFS_ROOT_IN_TRANS_SETUP, &root->state);
3486702ed49SChris Mason 	}
3495d4f98a2SYan Zheng 	return 0;
3506702ed49SChris Mason }
3515d4f98a2SYan Zheng 
3527585717fSChris Mason 
3532b9dbef2SJosef Bacik void btrfs_add_dropped_root(struct btrfs_trans_handle *trans,
3542b9dbef2SJosef Bacik 			    struct btrfs_root *root)
3552b9dbef2SJosef Bacik {
3562b9dbef2SJosef Bacik 	struct btrfs_transaction *cur_trans = trans->transaction;
3572b9dbef2SJosef Bacik 
3582b9dbef2SJosef Bacik 	/* Add ourselves to the transaction dropped list */
3592b9dbef2SJosef Bacik 	spin_lock(&cur_trans->dropped_roots_lock);
3602b9dbef2SJosef Bacik 	list_add_tail(&root->root_list, &cur_trans->dropped_roots);
3612b9dbef2SJosef Bacik 	spin_unlock(&cur_trans->dropped_roots_lock);
3622b9dbef2SJosef Bacik 
3632b9dbef2SJosef Bacik 	/* Make sure we don't try to update the root at commit time */
3642b9dbef2SJosef Bacik 	spin_lock(&root->fs_info->fs_roots_radix_lock);
3652b9dbef2SJosef Bacik 	radix_tree_tag_clear(&root->fs_info->fs_roots_radix,
3662b9dbef2SJosef Bacik 			     (unsigned long)root->root_key.objectid,
3672b9dbef2SJosef Bacik 			     BTRFS_ROOT_TRANS_TAG);
3682b9dbef2SJosef Bacik 	spin_unlock(&root->fs_info->fs_roots_radix_lock);
3692b9dbef2SJosef Bacik }
3702b9dbef2SJosef Bacik 
3717585717fSChris Mason int btrfs_record_root_in_trans(struct btrfs_trans_handle *trans,
3727585717fSChris Mason 			       struct btrfs_root *root)
3737585717fSChris Mason {
37427cdeb70SMiao Xie 	if (!test_bit(BTRFS_ROOT_REF_COWS, &root->state))
3757585717fSChris Mason 		return 0;
3767585717fSChris Mason 
3777585717fSChris Mason 	/*
37827cdeb70SMiao Xie 	 * see record_root_in_trans for comments about IN_TRANS_SETUP usage
3797585717fSChris Mason 	 * and barriers
3807585717fSChris Mason 	 */
3817585717fSChris Mason 	smp_rmb();
3827585717fSChris Mason 	if (root->last_trans == trans->transid &&
38327cdeb70SMiao Xie 	    !test_bit(BTRFS_ROOT_IN_TRANS_SETUP, &root->state))
3847585717fSChris Mason 		return 0;
3857585717fSChris Mason 
3867585717fSChris Mason 	mutex_lock(&root->fs_info->reloc_mutex);
3877585717fSChris Mason 	record_root_in_trans(trans, root);
3887585717fSChris Mason 	mutex_unlock(&root->fs_info->reloc_mutex);
3897585717fSChris Mason 
3907585717fSChris Mason 	return 0;
3917585717fSChris Mason }
3927585717fSChris Mason 
3934a9d8bdeSMiao Xie static inline int is_transaction_blocked(struct btrfs_transaction *trans)
3944a9d8bdeSMiao Xie {
3954a9d8bdeSMiao Xie 	return (trans->state >= TRANS_STATE_BLOCKED &&
396501407aaSJosef Bacik 		trans->state < TRANS_STATE_UNBLOCKED &&
397501407aaSJosef Bacik 		!trans->aborted);
3984a9d8bdeSMiao Xie }
3994a9d8bdeSMiao Xie 
400d352ac68SChris Mason /* wait for commit against the current transaction to become unblocked
401d352ac68SChris Mason  * when this is done, it is safe to start a new transaction, but the current
402d352ac68SChris Mason  * transaction might not be fully on disk.
403d352ac68SChris Mason  */
40437d1aeeeSChris Mason static void wait_current_trans(struct btrfs_root *root)
40579154b1bSChris Mason {
406f9295749SChris Mason 	struct btrfs_transaction *cur_trans;
40779154b1bSChris Mason 
408a4abeea4SJosef Bacik 	spin_lock(&root->fs_info->trans_lock);
409f9295749SChris Mason 	cur_trans = root->fs_info->running_transaction;
4104a9d8bdeSMiao Xie 	if (cur_trans && is_transaction_blocked(cur_trans)) {
41113c5a93eSJosef Bacik 		atomic_inc(&cur_trans->use_count);
412a4abeea4SJosef Bacik 		spin_unlock(&root->fs_info->trans_lock);
41372d63ed6SLi Zefan 
41472d63ed6SLi Zefan 		wait_event(root->fs_info->transaction_wait,
415501407aaSJosef Bacik 			   cur_trans->state >= TRANS_STATE_UNBLOCKED ||
416501407aaSJosef Bacik 			   cur_trans->aborted);
417724e2315SJosef Bacik 		btrfs_put_transaction(cur_trans);
418a4abeea4SJosef Bacik 	} else {
419a4abeea4SJosef Bacik 		spin_unlock(&root->fs_info->trans_lock);
420f9295749SChris Mason 	}
42137d1aeeeSChris Mason }
42237d1aeeeSChris Mason 
423a22285a6SYan, Zheng static int may_wait_transaction(struct btrfs_root *root, int type)
42437d1aeeeSChris Mason {
425a4abeea4SJosef Bacik 	if (root->fs_info->log_root_recovering)
426a4abeea4SJosef Bacik 		return 0;
427a4abeea4SJosef Bacik 
428a4abeea4SJosef Bacik 	if (type == TRANS_USERSPACE)
429a22285a6SYan, Zheng 		return 1;
430a4abeea4SJosef Bacik 
431a4abeea4SJosef Bacik 	if (type == TRANS_START &&
432a4abeea4SJosef Bacik 	    !atomic_read(&root->fs_info->open_ioctl_trans))
433a4abeea4SJosef Bacik 		return 1;
434a4abeea4SJosef Bacik 
435a22285a6SYan, Zheng 	return 0;
436a22285a6SYan, Zheng }
437a22285a6SYan, Zheng 
43820dd2cbfSMiao Xie static inline bool need_reserve_reloc_root(struct btrfs_root *root)
43920dd2cbfSMiao Xie {
44020dd2cbfSMiao Xie 	if (!root->fs_info->reloc_ctl ||
44127cdeb70SMiao Xie 	    !test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
44220dd2cbfSMiao Xie 	    root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID ||
44320dd2cbfSMiao Xie 	    root->reloc_root)
44420dd2cbfSMiao Xie 		return false;
44520dd2cbfSMiao Xie 
44620dd2cbfSMiao Xie 	return true;
44720dd2cbfSMiao Xie }
44820dd2cbfSMiao Xie 
44908e007d2SMiao Xie static struct btrfs_trans_handle *
4500860adfdSMiao Xie start_transaction(struct btrfs_root *root, u64 num_items, unsigned int type,
45108e007d2SMiao Xie 		  enum btrfs_reserve_flush_enum flush)
452a22285a6SYan, Zheng {
453a22285a6SYan, Zheng 	struct btrfs_trans_handle *h;
454a22285a6SYan, Zheng 	struct btrfs_transaction *cur_trans;
455b5009945SJosef Bacik 	u64 num_bytes = 0;
456c5567237SArne Jansen 	u64 qgroup_reserved = 0;
45720dd2cbfSMiao Xie 	bool reloc_reserved = false;
45820dd2cbfSMiao Xie 	int ret;
459acce952bSliubo 
46046c4e71eSFilipe Manana 	/* Send isn't supposed to start transactions. */
4612755a0deSDavid Sterba 	ASSERT(current->journal_info != BTRFS_SEND_TRANS_STUB);
46246c4e71eSFilipe Manana 
46387533c47SMiao Xie 	if (test_bit(BTRFS_FS_STATE_ERROR, &root->fs_info->fs_state))
464acce952bSliubo 		return ERR_PTR(-EROFS);
4652a1eb461SJosef Bacik 
46646c4e71eSFilipe Manana 	if (current->journal_info) {
4670860adfdSMiao Xie 		WARN_ON(type & TRANS_EXTWRITERS);
4682a1eb461SJosef Bacik 		h = current->journal_info;
4692a1eb461SJosef Bacik 		h->use_count++;
470b7d5b0a8SMiao Xie 		WARN_ON(h->use_count > 2);
4712a1eb461SJosef Bacik 		h->orig_rsv = h->block_rsv;
4722a1eb461SJosef Bacik 		h->block_rsv = NULL;
4732a1eb461SJosef Bacik 		goto got_it;
4742a1eb461SJosef Bacik 	}
475b5009945SJosef Bacik 
476b5009945SJosef Bacik 	/*
477b5009945SJosef Bacik 	 * Do the reservation before we join the transaction so we can do all
478b5009945SJosef Bacik 	 * the appropriate flushing if need be.
479b5009945SJosef Bacik 	 */
480b5009945SJosef Bacik 	if (num_items > 0 && root != root->fs_info->chunk_root) {
481c5567237SArne Jansen 		if (root->fs_info->quota_enabled &&
482c5567237SArne Jansen 		    is_fstree(root->root_key.objectid)) {
483707e8a07SDavid Sterba 			qgroup_reserved = num_items * root->nodesize;
484c5567237SArne Jansen 			ret = btrfs_qgroup_reserve(root, qgroup_reserved);
485c5567237SArne Jansen 			if (ret)
486c5567237SArne Jansen 				return ERR_PTR(ret);
487c5567237SArne Jansen 		}
488c5567237SArne Jansen 
489b5009945SJosef Bacik 		num_bytes = btrfs_calc_trans_metadata_size(root, num_items);
49020dd2cbfSMiao Xie 		/*
49120dd2cbfSMiao Xie 		 * Do the reservation for the relocation root creation
49220dd2cbfSMiao Xie 		 */
493ee39b432SDavid Sterba 		if (need_reserve_reloc_root(root)) {
49420dd2cbfSMiao Xie 			num_bytes += root->nodesize;
49520dd2cbfSMiao Xie 			reloc_reserved = true;
49620dd2cbfSMiao Xie 		}
49720dd2cbfSMiao Xie 
4984a92b1b8SJosef Bacik 		ret = btrfs_block_rsv_add(root,
499b5009945SJosef Bacik 					  &root->fs_info->trans_block_rsv,
50008e007d2SMiao Xie 					  num_bytes, flush);
501b5009945SJosef Bacik 		if (ret)
502843fcf35SMiao Xie 			goto reserve_fail;
503b5009945SJosef Bacik 	}
504a22285a6SYan, Zheng again:
505a22285a6SYan, Zheng 	h = kmem_cache_alloc(btrfs_trans_handle_cachep, GFP_NOFS);
506843fcf35SMiao Xie 	if (!h) {
507843fcf35SMiao Xie 		ret = -ENOMEM;
508843fcf35SMiao Xie 		goto alloc_fail;
509843fcf35SMiao Xie 	}
510a22285a6SYan, Zheng 
51198114659SJosef Bacik 	/*
51298114659SJosef Bacik 	 * If we are JOIN_NOLOCK we're already committing a transaction and
51398114659SJosef Bacik 	 * waiting on this guy, so we don't need to do the sb_start_intwrite
51498114659SJosef Bacik 	 * because we're already holding a ref.  We need this because we could
51598114659SJosef Bacik 	 * have raced in and did an fsync() on a file which can kick a commit
51698114659SJosef Bacik 	 * and then we deadlock with somebody doing a freeze.
517354aa0fbSMiao Xie 	 *
518354aa0fbSMiao Xie 	 * If we are ATTACH, it means we just want to catch the current
519354aa0fbSMiao Xie 	 * transaction and commit it, so we needn't do sb_start_intwrite().
52098114659SJosef Bacik 	 */
5210860adfdSMiao Xie 	if (type & __TRANS_FREEZABLE)
522b2b5ef5cSJan Kara 		sb_start_intwrite(root->fs_info->sb);
523b2b5ef5cSJan Kara 
524a22285a6SYan, Zheng 	if (may_wait_transaction(root, type))
52537d1aeeeSChris Mason 		wait_current_trans(root);
526a22285a6SYan, Zheng 
527a4abeea4SJosef Bacik 	do {
528354aa0fbSMiao Xie 		ret = join_transaction(root, type);
529178260b2SMiao Xie 		if (ret == -EBUSY) {
530a4abeea4SJosef Bacik 			wait_current_trans(root);
531178260b2SMiao Xie 			if (unlikely(type == TRANS_ATTACH))
532178260b2SMiao Xie 				ret = -ENOENT;
533178260b2SMiao Xie 		}
534a4abeea4SJosef Bacik 	} while (ret == -EBUSY);
535a4abeea4SJosef Bacik 
536db5b493aSTsutomu Itoh 	if (ret < 0) {
537354aa0fbSMiao Xie 		/* We must get the transaction if we are JOIN_NOLOCK. */
538354aa0fbSMiao Xie 		BUG_ON(type == TRANS_JOIN_NOLOCK);
539843fcf35SMiao Xie 		goto join_fail;
540db5b493aSTsutomu Itoh 	}
5410f7d52f4SChris Mason 
542a22285a6SYan, Zheng 	cur_trans = root->fs_info->running_transaction;
543a22285a6SYan, Zheng 
544a22285a6SYan, Zheng 	h->transid = cur_trans->transid;
545a22285a6SYan, Zheng 	h->transaction = cur_trans;
54679154b1bSChris Mason 	h->blocks_used = 0;
547a22285a6SYan, Zheng 	h->bytes_reserved = 0;
5484fbcdf66SFilipe Manana 	h->chunk_bytes_reserved = 0;
549d13603efSArne Jansen 	h->root = root;
55056bec294SChris Mason 	h->delayed_ref_updates = 0;
5512a1eb461SJosef Bacik 	h->use_count = 1;
5520e721106SJosef Bacik 	h->adding_csums = 0;
553f0486c68SYan, Zheng 	h->block_rsv = NULL;
5542a1eb461SJosef Bacik 	h->orig_rsv = NULL;
55549b25e05SJeff Mahoney 	h->aborted = 0;
5564b824906SMiao Xie 	h->qgroup_reserved = 0;
557bed92eaeSArne Jansen 	h->delayed_ref_elem.seq = 0;
558a698d075SMiao Xie 	h->type = type;
559c6b305a8SJosef Bacik 	h->allocating_chunk = false;
56020dd2cbfSMiao Xie 	h->reloc_reserved = false;
5615039eddcSJosef Bacik 	h->sync = false;
562bed92eaeSArne Jansen 	INIT_LIST_HEAD(&h->qgroup_ref_list);
563ea658badSJosef Bacik 	INIT_LIST_HEAD(&h->new_bgs);
56450d9aa99SJosef Bacik 	INIT_LIST_HEAD(&h->ordered);
565b7ec40d7SChris Mason 
566a22285a6SYan, Zheng 	smp_mb();
5674a9d8bdeSMiao Xie 	if (cur_trans->state >= TRANS_STATE_BLOCKED &&
5684a9d8bdeSMiao Xie 	    may_wait_transaction(root, type)) {
569abdd2e80SFilipe Manana 		current->journal_info = h;
570a22285a6SYan, Zheng 		btrfs_commit_transaction(h, root);
571a22285a6SYan, Zheng 		goto again;
572a22285a6SYan, Zheng 	}
5739ed74f2dSJosef Bacik 
574b5009945SJosef Bacik 	if (num_bytes) {
5758c2a3ca2SJosef Bacik 		trace_btrfs_space_reservation(root->fs_info, "transaction",
5762bcc0328SLiu Bo 					      h->transid, num_bytes, 1);
577b5009945SJosef Bacik 		h->block_rsv = &root->fs_info->trans_block_rsv;
578b5009945SJosef Bacik 		h->bytes_reserved = num_bytes;
57920dd2cbfSMiao Xie 		h->reloc_reserved = reloc_reserved;
580a22285a6SYan, Zheng 	}
5814b824906SMiao Xie 	h->qgroup_reserved = qgroup_reserved;
582a22285a6SYan, Zheng 
5832a1eb461SJosef Bacik got_it:
584a4abeea4SJosef Bacik 	btrfs_record_root_in_trans(h, root);
585a22285a6SYan, Zheng 
586a22285a6SYan, Zheng 	if (!current->journal_info && type != TRANS_USERSPACE)
587a22285a6SYan, Zheng 		current->journal_info = h;
58879154b1bSChris Mason 	return h;
589843fcf35SMiao Xie 
590843fcf35SMiao Xie join_fail:
5910860adfdSMiao Xie 	if (type & __TRANS_FREEZABLE)
592843fcf35SMiao Xie 		sb_end_intwrite(root->fs_info->sb);
593843fcf35SMiao Xie 	kmem_cache_free(btrfs_trans_handle_cachep, h);
594843fcf35SMiao Xie alloc_fail:
595843fcf35SMiao Xie 	if (num_bytes)
596843fcf35SMiao Xie 		btrfs_block_rsv_release(root, &root->fs_info->trans_block_rsv,
597843fcf35SMiao Xie 					num_bytes);
598843fcf35SMiao Xie reserve_fail:
599843fcf35SMiao Xie 	if (qgroup_reserved)
600843fcf35SMiao Xie 		btrfs_qgroup_free(root, qgroup_reserved);
601843fcf35SMiao Xie 	return ERR_PTR(ret);
60279154b1bSChris Mason }
60379154b1bSChris Mason 
604f9295749SChris Mason struct btrfs_trans_handle *btrfs_start_transaction(struct btrfs_root *root,
605a22285a6SYan, Zheng 						   int num_items)
606f9295749SChris Mason {
60708e007d2SMiao Xie 	return start_transaction(root, num_items, TRANS_START,
60808e007d2SMiao Xie 				 BTRFS_RESERVE_FLUSH_ALL);
609f9295749SChris Mason }
6108407aa46SMiao Xie 
61108e007d2SMiao Xie struct btrfs_trans_handle *btrfs_start_transaction_lflush(
6128407aa46SMiao Xie 					struct btrfs_root *root, int num_items)
6138407aa46SMiao Xie {
61408e007d2SMiao Xie 	return start_transaction(root, num_items, TRANS_START,
61508e007d2SMiao Xie 				 BTRFS_RESERVE_FLUSH_LIMIT);
6168407aa46SMiao Xie }
6178407aa46SMiao Xie 
6187a7eaa40SJosef Bacik struct btrfs_trans_handle *btrfs_join_transaction(struct btrfs_root *root)
619f9295749SChris Mason {
6208407aa46SMiao Xie 	return start_transaction(root, 0, TRANS_JOIN, 0);
621f9295749SChris Mason }
622f9295749SChris Mason 
6237a7eaa40SJosef Bacik struct btrfs_trans_handle *btrfs_join_transaction_nolock(struct btrfs_root *root)
6240af3d00bSJosef Bacik {
6258407aa46SMiao Xie 	return start_transaction(root, 0, TRANS_JOIN_NOLOCK, 0);
6260af3d00bSJosef Bacik }
6270af3d00bSJosef Bacik 
6287a7eaa40SJosef Bacik struct btrfs_trans_handle *btrfs_start_ioctl_transaction(struct btrfs_root *root)
6299ca9ee09SSage Weil {
6308407aa46SMiao Xie 	return start_transaction(root, 0, TRANS_USERSPACE, 0);
6319ca9ee09SSage Weil }
6329ca9ee09SSage Weil 
633d4edf39bSMiao Xie /*
634d4edf39bSMiao Xie  * btrfs_attach_transaction() - catch the running transaction
635d4edf39bSMiao Xie  *
636d4edf39bSMiao Xie  * It is used when we want to commit the current the transaction, but
637d4edf39bSMiao Xie  * don't want to start a new one.
638d4edf39bSMiao Xie  *
639d4edf39bSMiao Xie  * Note: If this function return -ENOENT, it just means there is no
640d4edf39bSMiao Xie  * running transaction. But it is possible that the inactive transaction
641d4edf39bSMiao Xie  * is still in the memory, not fully on disk. If you hope there is no
642d4edf39bSMiao Xie  * inactive transaction in the fs when -ENOENT is returned, you should
643d4edf39bSMiao Xie  * invoke
644d4edf39bSMiao Xie  *     btrfs_attach_transaction_barrier()
645d4edf39bSMiao Xie  */
646354aa0fbSMiao Xie struct btrfs_trans_handle *btrfs_attach_transaction(struct btrfs_root *root)
64760376ce4SJosef Bacik {
648354aa0fbSMiao Xie 	return start_transaction(root, 0, TRANS_ATTACH, 0);
64960376ce4SJosef Bacik }
65060376ce4SJosef Bacik 
651d4edf39bSMiao Xie /*
65290b6d283SWang Sheng-Hui  * btrfs_attach_transaction_barrier() - catch the running transaction
653d4edf39bSMiao Xie  *
654d4edf39bSMiao Xie  * It is similar to the above function, the differentia is this one
655d4edf39bSMiao Xie  * will wait for all the inactive transactions until they fully
656d4edf39bSMiao Xie  * complete.
657d4edf39bSMiao Xie  */
658d4edf39bSMiao Xie struct btrfs_trans_handle *
659d4edf39bSMiao Xie btrfs_attach_transaction_barrier(struct btrfs_root *root)
660d4edf39bSMiao Xie {
661d4edf39bSMiao Xie 	struct btrfs_trans_handle *trans;
662d4edf39bSMiao Xie 
663d4edf39bSMiao Xie 	trans = start_transaction(root, 0, TRANS_ATTACH, 0);
664d4edf39bSMiao Xie 	if (IS_ERR(trans) && PTR_ERR(trans) == -ENOENT)
665d4edf39bSMiao Xie 		btrfs_wait_for_commit(root, 0);
666d4edf39bSMiao Xie 
667d4edf39bSMiao Xie 	return trans;
668d4edf39bSMiao Xie }
669d4edf39bSMiao Xie 
670d352ac68SChris Mason /* wait for a transaction commit to be fully complete */
671b9c8300cSLi Zefan static noinline void wait_for_commit(struct btrfs_root *root,
67289ce8a63SChris Mason 				    struct btrfs_transaction *commit)
67389ce8a63SChris Mason {
6744a9d8bdeSMiao Xie 	wait_event(commit->commit_wait, commit->state == TRANS_STATE_COMPLETED);
67589ce8a63SChris Mason }
67689ce8a63SChris Mason 
67746204592SSage Weil int btrfs_wait_for_commit(struct btrfs_root *root, u64 transid)
67846204592SSage Weil {
67946204592SSage Weil 	struct btrfs_transaction *cur_trans = NULL, *t;
6808cd2807fSMiao Xie 	int ret = 0;
68146204592SSage Weil 
68246204592SSage Weil 	if (transid) {
68346204592SSage Weil 		if (transid <= root->fs_info->last_trans_committed)
684a4abeea4SJosef Bacik 			goto out;
68546204592SSage Weil 
68646204592SSage Weil 		/* find specified transaction */
687a4abeea4SJosef Bacik 		spin_lock(&root->fs_info->trans_lock);
68846204592SSage Weil 		list_for_each_entry(t, &root->fs_info->trans_list, list) {
68946204592SSage Weil 			if (t->transid == transid) {
69046204592SSage Weil 				cur_trans = t;
691a4abeea4SJosef Bacik 				atomic_inc(&cur_trans->use_count);
6928cd2807fSMiao Xie 				ret = 0;
69346204592SSage Weil 				break;
69446204592SSage Weil 			}
6958cd2807fSMiao Xie 			if (t->transid > transid) {
6968cd2807fSMiao Xie 				ret = 0;
69746204592SSage Weil 				break;
69846204592SSage Weil 			}
6998cd2807fSMiao Xie 		}
700a4abeea4SJosef Bacik 		spin_unlock(&root->fs_info->trans_lock);
70142383020SSage Weil 
70242383020SSage Weil 		/*
70342383020SSage Weil 		 * The specified transaction doesn't exist, or we
70442383020SSage Weil 		 * raced with btrfs_commit_transaction
70542383020SSage Weil 		 */
70642383020SSage Weil 		if (!cur_trans) {
70742383020SSage Weil 			if (transid > root->fs_info->last_trans_committed)
70842383020SSage Weil 				ret = -EINVAL;
7098cd2807fSMiao Xie 			goto out;
71042383020SSage Weil 		}
71146204592SSage Weil 	} else {
71246204592SSage Weil 		/* find newest transaction that is committing | committed */
713a4abeea4SJosef Bacik 		spin_lock(&root->fs_info->trans_lock);
71446204592SSage Weil 		list_for_each_entry_reverse(t, &root->fs_info->trans_list,
71546204592SSage Weil 					    list) {
7164a9d8bdeSMiao Xie 			if (t->state >= TRANS_STATE_COMMIT_START) {
7174a9d8bdeSMiao Xie 				if (t->state == TRANS_STATE_COMPLETED)
7183473f3c0SJosef Bacik 					break;
71946204592SSage Weil 				cur_trans = t;
720a4abeea4SJosef Bacik 				atomic_inc(&cur_trans->use_count);
72146204592SSage Weil 				break;
72246204592SSage Weil 			}
72346204592SSage Weil 		}
724a4abeea4SJosef Bacik 		spin_unlock(&root->fs_info->trans_lock);
72546204592SSage Weil 		if (!cur_trans)
726a4abeea4SJosef Bacik 			goto out;  /* nothing committing|committed */
72746204592SSage Weil 	}
72846204592SSage Weil 
72946204592SSage Weil 	wait_for_commit(root, cur_trans);
730724e2315SJosef Bacik 	btrfs_put_transaction(cur_trans);
731a4abeea4SJosef Bacik out:
73246204592SSage Weil 	return ret;
73346204592SSage Weil }
73446204592SSage Weil 
73537d1aeeeSChris Mason void btrfs_throttle(struct btrfs_root *root)
73637d1aeeeSChris Mason {
737a4abeea4SJosef Bacik 	if (!atomic_read(&root->fs_info->open_ioctl_trans))
73837d1aeeeSChris Mason 		wait_current_trans(root);
73937d1aeeeSChris Mason }
74037d1aeeeSChris Mason 
7418929ecfaSYan, Zheng static int should_end_transaction(struct btrfs_trans_handle *trans,
7428929ecfaSYan, Zheng 				  struct btrfs_root *root)
7438929ecfaSYan, Zheng {
7441be41b78SJosef Bacik 	if (root->fs_info->global_block_rsv.space_info->full &&
7450a2b2a84SJosef Bacik 	    btrfs_check_space_for_delayed_refs(trans, root))
7461be41b78SJosef Bacik 		return 1;
74736ba022aSJosef Bacik 
7481be41b78SJosef Bacik 	return !!btrfs_block_rsv_check(root, &root->fs_info->global_block_rsv, 5);
7498929ecfaSYan, Zheng }
7508929ecfaSYan, Zheng 
7518929ecfaSYan, Zheng int btrfs_should_end_transaction(struct btrfs_trans_handle *trans,
7528929ecfaSYan, Zheng 				 struct btrfs_root *root)
7538929ecfaSYan, Zheng {
7548929ecfaSYan, Zheng 	struct btrfs_transaction *cur_trans = trans->transaction;
7558929ecfaSYan, Zheng 	int updates;
75649b25e05SJeff Mahoney 	int err;
7578929ecfaSYan, Zheng 
758a4abeea4SJosef Bacik 	smp_mb();
7594a9d8bdeSMiao Xie 	if (cur_trans->state >= TRANS_STATE_BLOCKED ||
7604a9d8bdeSMiao Xie 	    cur_trans->delayed_refs.flushing)
7618929ecfaSYan, Zheng 		return 1;
7628929ecfaSYan, Zheng 
7638929ecfaSYan, Zheng 	updates = trans->delayed_ref_updates;
7648929ecfaSYan, Zheng 	trans->delayed_ref_updates = 0;
76549b25e05SJeff Mahoney 	if (updates) {
76628ed1345SChris Mason 		err = btrfs_run_delayed_refs(trans, root, updates * 2);
76749b25e05SJeff Mahoney 		if (err) /* Error code will also eval true */
76849b25e05SJeff Mahoney 			return err;
76949b25e05SJeff Mahoney 	}
7708929ecfaSYan, Zheng 
7718929ecfaSYan, Zheng 	return should_end_transaction(trans, root);
7728929ecfaSYan, Zheng }
7738929ecfaSYan, Zheng 
77489ce8a63SChris Mason static int __btrfs_end_transaction(struct btrfs_trans_handle *trans,
775a698d075SMiao Xie 			  struct btrfs_root *root, int throttle)
77679154b1bSChris Mason {
7778929ecfaSYan, Zheng 	struct btrfs_transaction *cur_trans = trans->transaction;
778ab78c84dSChris Mason 	struct btrfs_fs_info *info = root->fs_info;
7791be41b78SJosef Bacik 	unsigned long cur = trans->delayed_ref_updates;
780a698d075SMiao Xie 	int lock = (trans->type != TRANS_JOIN_NOLOCK);
7814edc2ca3SDave Jones 	int err = 0;
782a79b7d4bSChris Mason 	int must_run_delayed_refs = 0;
783d6e4a428SChris Mason 
7843bbb24b2SJosef Bacik 	if (trans->use_count > 1) {
7853bbb24b2SJosef Bacik 		trans->use_count--;
7862a1eb461SJosef Bacik 		trans->block_rsv = trans->orig_rsv;
7872a1eb461SJosef Bacik 		return 0;
7882a1eb461SJosef Bacik 	}
7892a1eb461SJosef Bacik 
790b24e03dbSJosef Bacik 	btrfs_trans_release_metadata(trans, root);
7914c13d758SJosef Bacik 	trans->block_rsv = NULL;
792c5567237SArne Jansen 
793ea658badSJosef Bacik 	if (!list_empty(&trans->new_bgs))
794ea658badSJosef Bacik 		btrfs_create_pending_block_groups(trans, root);
795ea658badSJosef Bacik 
79650d9aa99SJosef Bacik 	if (!list_empty(&trans->ordered)) {
79750d9aa99SJosef Bacik 		spin_lock(&info->trans_lock);
798d3efe084SFilipe Manana 		list_splice_init(&trans->ordered, &cur_trans->pending_ordered);
79950d9aa99SJosef Bacik 		spin_unlock(&info->trans_lock);
80050d9aa99SJosef Bacik 	}
80150d9aa99SJosef Bacik 
802c3e69d58SChris Mason 	trans->delayed_ref_updates = 0;
803a79b7d4bSChris Mason 	if (!trans->sync) {
804a79b7d4bSChris Mason 		must_run_delayed_refs =
805a79b7d4bSChris Mason 			btrfs_should_throttle_delayed_refs(trans, root);
8060a2b2a84SJosef Bacik 		cur = max_t(unsigned long, cur, 32);
807a79b7d4bSChris Mason 
808a79b7d4bSChris Mason 		/*
809a79b7d4bSChris Mason 		 * don't make the caller wait if they are from a NOLOCK
810a79b7d4bSChris Mason 		 * or ATTACH transaction, it will deadlock with commit
811a79b7d4bSChris Mason 		 */
812a79b7d4bSChris Mason 		if (must_run_delayed_refs == 1 &&
813a79b7d4bSChris Mason 		    (trans->type & (__TRANS_JOIN_NOLOCK | __TRANS_ATTACH)))
814a79b7d4bSChris Mason 			must_run_delayed_refs = 2;
81556bec294SChris Mason 	}
816bb721703SChris Mason 
817fcebe456SJosef Bacik 	if (trans->qgroup_reserved) {
818fcebe456SJosef Bacik 		/*
819fcebe456SJosef Bacik 		 * the same root has to be passed here between start_transaction
820fcebe456SJosef Bacik 		 * and end_transaction. Subvolume quota depends on this.
821fcebe456SJosef Bacik 		 */
822fcebe456SJosef Bacik 		btrfs_qgroup_free(trans->root, trans->qgroup_reserved);
823fcebe456SJosef Bacik 		trans->qgroup_reserved = 0;
824fcebe456SJosef Bacik 	}
825fcebe456SJosef Bacik 
8260e721106SJosef Bacik 	btrfs_trans_release_metadata(trans, root);
8270e721106SJosef Bacik 	trans->block_rsv = NULL;
82856bec294SChris Mason 
829ea658badSJosef Bacik 	if (!list_empty(&trans->new_bgs))
830ea658badSJosef Bacik 		btrfs_create_pending_block_groups(trans, root);
831ea658badSJosef Bacik 
8324fbcdf66SFilipe Manana 	btrfs_trans_release_chunk_metadata(trans);
8334fbcdf66SFilipe Manana 
834a4abeea4SJosef Bacik 	if (lock && !atomic_read(&root->fs_info->open_ioctl_trans) &&
8354a9d8bdeSMiao Xie 	    should_end_transaction(trans, root) &&
8364a9d8bdeSMiao Xie 	    ACCESS_ONCE(cur_trans->state) == TRANS_STATE_RUNNING) {
8374a9d8bdeSMiao Xie 		spin_lock(&info->trans_lock);
8384a9d8bdeSMiao Xie 		if (cur_trans->state == TRANS_STATE_RUNNING)
8394a9d8bdeSMiao Xie 			cur_trans->state = TRANS_STATE_BLOCKED;
8404a9d8bdeSMiao Xie 		spin_unlock(&info->trans_lock);
841a4abeea4SJosef Bacik 	}
8428929ecfaSYan, Zheng 
8434a9d8bdeSMiao Xie 	if (lock && ACCESS_ONCE(cur_trans->state) == TRANS_STATE_BLOCKED) {
8443bbb24b2SJosef Bacik 		if (throttle)
8458929ecfaSYan, Zheng 			return btrfs_commit_transaction(trans, root);
8463bbb24b2SJosef Bacik 		else
8478929ecfaSYan, Zheng 			wake_up_process(info->transaction_kthread);
8488929ecfaSYan, Zheng 	}
8498929ecfaSYan, Zheng 
8500860adfdSMiao Xie 	if (trans->type & __TRANS_FREEZABLE)
8516df7881aSJosef Bacik 		sb_end_intwrite(root->fs_info->sb);
8526df7881aSJosef Bacik 
8538929ecfaSYan, Zheng 	WARN_ON(cur_trans != info->running_transaction);
85413c5a93eSJosef Bacik 	WARN_ON(atomic_read(&cur_trans->num_writers) < 1);
85513c5a93eSJosef Bacik 	atomic_dec(&cur_trans->num_writers);
8560860adfdSMiao Xie 	extwriter_counter_dec(cur_trans, trans->type);
85789ce8a63SChris Mason 
85899d16cbcSSage Weil 	smp_mb();
85979154b1bSChris Mason 	if (waitqueue_active(&cur_trans->writer_wait))
86079154b1bSChris Mason 		wake_up(&cur_trans->writer_wait);
861724e2315SJosef Bacik 	btrfs_put_transaction(cur_trans);
8629ed74f2dSJosef Bacik 
8639ed74f2dSJosef Bacik 	if (current->journal_info == trans)
8649ed74f2dSJosef Bacik 		current->journal_info = NULL;
865ab78c84dSChris Mason 
86624bbcf04SYan, Zheng 	if (throttle)
86724bbcf04SYan, Zheng 		btrfs_run_delayed_iputs(root);
86824bbcf04SYan, Zheng 
86949b25e05SJeff Mahoney 	if (trans->aborted ||
8704e121c06SJosef Bacik 	    test_bit(BTRFS_FS_STATE_ERROR, &root->fs_info->fs_state)) {
8714e121c06SJosef Bacik 		wake_up_process(info->transaction_kthread);
8724edc2ca3SDave Jones 		err = -EIO;
8734e121c06SJosef Bacik 	}
874edf39272SJan Schmidt 	assert_qgroups_uptodate(trans);
87549b25e05SJeff Mahoney 
8764edc2ca3SDave Jones 	kmem_cache_free(btrfs_trans_handle_cachep, trans);
877a79b7d4bSChris Mason 	if (must_run_delayed_refs) {
878a79b7d4bSChris Mason 		btrfs_async_run_delayed_refs(root, cur,
879a79b7d4bSChris Mason 					     must_run_delayed_refs == 1);
880a79b7d4bSChris Mason 	}
8814edc2ca3SDave Jones 	return err;
88279154b1bSChris Mason }
88379154b1bSChris Mason 
88489ce8a63SChris Mason int btrfs_end_transaction(struct btrfs_trans_handle *trans,
88589ce8a63SChris Mason 			  struct btrfs_root *root)
88689ce8a63SChris Mason {
88798ad43beSWang Shilong 	return __btrfs_end_transaction(trans, root, 0);
88889ce8a63SChris Mason }
88989ce8a63SChris Mason 
89089ce8a63SChris Mason int btrfs_end_transaction_throttle(struct btrfs_trans_handle *trans,
89189ce8a63SChris Mason 				   struct btrfs_root *root)
89289ce8a63SChris Mason {
89398ad43beSWang Shilong 	return __btrfs_end_transaction(trans, root, 1);
89416cdcec7SMiao Xie }
89516cdcec7SMiao Xie 
896d352ac68SChris Mason /*
897d352ac68SChris Mason  * when btree blocks are allocated, they have some corresponding bits set for
898d352ac68SChris Mason  * them in one of two extent_io trees.  This is used to make sure all of
899690587d1SChris Mason  * those extents are sent to disk but does not wait on them
900d352ac68SChris Mason  */
901690587d1SChris Mason int btrfs_write_marked_extents(struct btrfs_root *root,
9028cef4e16SYan, Zheng 			       struct extent_io_tree *dirty_pages, int mark)
90379154b1bSChris Mason {
904777e6bd7SChris Mason 	int err = 0;
9057c4452b9SChris Mason 	int werr = 0;
9061728366eSJosef Bacik 	struct address_space *mapping = root->fs_info->btree_inode->i_mapping;
907e6138876SJosef Bacik 	struct extent_state *cached_state = NULL;
908777e6bd7SChris Mason 	u64 start = 0;
9095f39d397SChris Mason 	u64 end;
9107c4452b9SChris Mason 
9111728366eSJosef Bacik 	while (!find_first_extent_bit(dirty_pages, start, &start, &end,
912e6138876SJosef Bacik 				      mark, &cached_state)) {
913663dfbb0SFilipe Manana 		bool wait_writeback = false;
914663dfbb0SFilipe Manana 
915663dfbb0SFilipe Manana 		err = convert_extent_bit(dirty_pages, start, end,
916663dfbb0SFilipe Manana 					 EXTENT_NEED_WAIT,
917e6138876SJosef Bacik 					 mark, &cached_state, GFP_NOFS);
918663dfbb0SFilipe Manana 		/*
919663dfbb0SFilipe Manana 		 * convert_extent_bit can return -ENOMEM, which is most of the
920663dfbb0SFilipe Manana 		 * time a temporary error. So when it happens, ignore the error
921663dfbb0SFilipe Manana 		 * and wait for writeback of this range to finish - because we
922663dfbb0SFilipe Manana 		 * failed to set the bit EXTENT_NEED_WAIT for the range, a call
923663dfbb0SFilipe Manana 		 * to btrfs_wait_marked_extents() would not know that writeback
924663dfbb0SFilipe Manana 		 * for this range started and therefore wouldn't wait for it to
925663dfbb0SFilipe Manana 		 * finish - we don't want to commit a superblock that points to
926663dfbb0SFilipe Manana 		 * btree nodes/leafs for which writeback hasn't finished yet
927663dfbb0SFilipe Manana 		 * (and without errors).
928663dfbb0SFilipe Manana 		 * We cleanup any entries left in the io tree when committing
929663dfbb0SFilipe Manana 		 * the transaction (through clear_btree_io_tree()).
930663dfbb0SFilipe Manana 		 */
931663dfbb0SFilipe Manana 		if (err == -ENOMEM) {
932663dfbb0SFilipe Manana 			err = 0;
933663dfbb0SFilipe Manana 			wait_writeback = true;
934663dfbb0SFilipe Manana 		}
935663dfbb0SFilipe Manana 		if (!err)
9361728366eSJosef Bacik 			err = filemap_fdatawrite_range(mapping, start, end);
9377c4452b9SChris Mason 		if (err)
9387c4452b9SChris Mason 			werr = err;
939663dfbb0SFilipe Manana 		else if (wait_writeback)
940663dfbb0SFilipe Manana 			werr = filemap_fdatawait_range(mapping, start, end);
941e38e2ed7SFilipe Manana 		free_extent_state(cached_state);
942663dfbb0SFilipe Manana 		cached_state = NULL;
9431728366eSJosef Bacik 		cond_resched();
9441728366eSJosef Bacik 		start = end + 1;
9457c4452b9SChris Mason 	}
946690587d1SChris Mason 	return werr;
947690587d1SChris Mason }
948690587d1SChris Mason 
949690587d1SChris Mason /*
950690587d1SChris Mason  * when btree blocks are allocated, they have some corresponding bits set for
951690587d1SChris Mason  * them in one of two extent_io trees.  This is used to make sure all of
952690587d1SChris Mason  * those extents are on disk for transaction or log commit.  We wait
953690587d1SChris Mason  * on all the pages and clear them from the dirty pages state tree
954690587d1SChris Mason  */
955690587d1SChris Mason int btrfs_wait_marked_extents(struct btrfs_root *root,
9568cef4e16SYan, Zheng 			      struct extent_io_tree *dirty_pages, int mark)
957690587d1SChris Mason {
958690587d1SChris Mason 	int err = 0;
959690587d1SChris Mason 	int werr = 0;
9601728366eSJosef Bacik 	struct address_space *mapping = root->fs_info->btree_inode->i_mapping;
961e6138876SJosef Bacik 	struct extent_state *cached_state = NULL;
962690587d1SChris Mason 	u64 start = 0;
963690587d1SChris Mason 	u64 end;
964656f30dbSFilipe Manana 	struct btrfs_inode *btree_ino = BTRFS_I(root->fs_info->btree_inode);
965656f30dbSFilipe Manana 	bool errors = false;
966690587d1SChris Mason 
9671728366eSJosef Bacik 	while (!find_first_extent_bit(dirty_pages, start, &start, &end,
968e6138876SJosef Bacik 				      EXTENT_NEED_WAIT, &cached_state)) {
969663dfbb0SFilipe Manana 		/*
970663dfbb0SFilipe Manana 		 * Ignore -ENOMEM errors returned by clear_extent_bit().
971663dfbb0SFilipe Manana 		 * When committing the transaction, we'll remove any entries
972663dfbb0SFilipe Manana 		 * left in the io tree. For a log commit, we don't remove them
973663dfbb0SFilipe Manana 		 * after committing the log because the tree can be accessed
974663dfbb0SFilipe Manana 		 * concurrently - we do it only at transaction commit time when
975663dfbb0SFilipe Manana 		 * it's safe to do it (through clear_btree_io_tree()).
976663dfbb0SFilipe Manana 		 */
977663dfbb0SFilipe Manana 		err = clear_extent_bit(dirty_pages, start, end,
978663dfbb0SFilipe Manana 				       EXTENT_NEED_WAIT,
979e6138876SJosef Bacik 				       0, 0, &cached_state, GFP_NOFS);
980663dfbb0SFilipe Manana 		if (err == -ENOMEM)
981663dfbb0SFilipe Manana 			err = 0;
982663dfbb0SFilipe Manana 		if (!err)
9831728366eSJosef Bacik 			err = filemap_fdatawait_range(mapping, start, end);
984777e6bd7SChris Mason 		if (err)
985777e6bd7SChris Mason 			werr = err;
986e38e2ed7SFilipe Manana 		free_extent_state(cached_state);
987e38e2ed7SFilipe Manana 		cached_state = NULL;
988777e6bd7SChris Mason 		cond_resched();
9891728366eSJosef Bacik 		start = end + 1;
990777e6bd7SChris Mason 	}
9917c4452b9SChris Mason 	if (err)
9927c4452b9SChris Mason 		werr = err;
993656f30dbSFilipe Manana 
994656f30dbSFilipe Manana 	if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) {
995656f30dbSFilipe Manana 		if ((mark & EXTENT_DIRTY) &&
996656f30dbSFilipe Manana 		    test_and_clear_bit(BTRFS_INODE_BTREE_LOG1_ERR,
997656f30dbSFilipe Manana 				       &btree_ino->runtime_flags))
998656f30dbSFilipe Manana 			errors = true;
999656f30dbSFilipe Manana 
1000656f30dbSFilipe Manana 		if ((mark & EXTENT_NEW) &&
1001656f30dbSFilipe Manana 		    test_and_clear_bit(BTRFS_INODE_BTREE_LOG2_ERR,
1002656f30dbSFilipe Manana 				       &btree_ino->runtime_flags))
1003656f30dbSFilipe Manana 			errors = true;
1004656f30dbSFilipe Manana 	} else {
1005656f30dbSFilipe Manana 		if (test_and_clear_bit(BTRFS_INODE_BTREE_ERR,
1006656f30dbSFilipe Manana 				       &btree_ino->runtime_flags))
1007656f30dbSFilipe Manana 			errors = true;
1008656f30dbSFilipe Manana 	}
1009656f30dbSFilipe Manana 
1010656f30dbSFilipe Manana 	if (errors && !werr)
1011656f30dbSFilipe Manana 		werr = -EIO;
1012656f30dbSFilipe Manana 
10137c4452b9SChris Mason 	return werr;
101479154b1bSChris Mason }
101579154b1bSChris Mason 
1016690587d1SChris Mason /*
1017690587d1SChris Mason  * when btree blocks are allocated, they have some corresponding bits set for
1018690587d1SChris Mason  * them in one of two extent_io trees.  This is used to make sure all of
1019690587d1SChris Mason  * those extents are on disk for transaction or log commit
1020690587d1SChris Mason  */
1021171170c1SSergei Trofimovich static int btrfs_write_and_wait_marked_extents(struct btrfs_root *root,
10228cef4e16SYan, Zheng 				struct extent_io_tree *dirty_pages, int mark)
1023690587d1SChris Mason {
1024690587d1SChris Mason 	int ret;
1025690587d1SChris Mason 	int ret2;
1026c6adc9ccSMiao Xie 	struct blk_plug plug;
1027690587d1SChris Mason 
1028c6adc9ccSMiao Xie 	blk_start_plug(&plug);
10298cef4e16SYan, Zheng 	ret = btrfs_write_marked_extents(root, dirty_pages, mark);
1030c6adc9ccSMiao Xie 	blk_finish_plug(&plug);
10318cef4e16SYan, Zheng 	ret2 = btrfs_wait_marked_extents(root, dirty_pages, mark);
1032bf0da8c1SChris Mason 
1033bf0da8c1SChris Mason 	if (ret)
1034bf0da8c1SChris Mason 		return ret;
1035bf0da8c1SChris Mason 	if (ret2)
1036bf0da8c1SChris Mason 		return ret2;
1037bf0da8c1SChris Mason 	return 0;
1038690587d1SChris Mason }
1039690587d1SChris Mason 
1040663dfbb0SFilipe Manana static int btrfs_write_and_wait_transaction(struct btrfs_trans_handle *trans,
1041d0c803c4SChris Mason 				     struct btrfs_root *root)
1042d0c803c4SChris Mason {
1043663dfbb0SFilipe Manana 	int ret;
1044663dfbb0SFilipe Manana 
1045663dfbb0SFilipe Manana 	ret = btrfs_write_and_wait_marked_extents(root,
10468cef4e16SYan, Zheng 					   &trans->transaction->dirty_pages,
10478cef4e16SYan, Zheng 					   EXTENT_DIRTY);
1048663dfbb0SFilipe Manana 	clear_btree_io_tree(&trans->transaction->dirty_pages);
1049663dfbb0SFilipe Manana 
1050663dfbb0SFilipe Manana 	return ret;
1051d0c803c4SChris Mason }
1052d0c803c4SChris Mason 
1053d352ac68SChris Mason /*
1054d352ac68SChris Mason  * this is used to update the root pointer in the tree of tree roots.
1055d352ac68SChris Mason  *
1056d352ac68SChris Mason  * But, in the case of the extent allocation tree, updating the root
1057d352ac68SChris Mason  * pointer may allocate blocks which may change the root of the extent
1058d352ac68SChris Mason  * allocation tree.
1059d352ac68SChris Mason  *
1060d352ac68SChris Mason  * So, this loops and repeats and makes sure the cowonly root didn't
1061d352ac68SChris Mason  * change while the root pointer was being updated in the metadata.
1062d352ac68SChris Mason  */
10630b86a832SChris Mason static int update_cowonly_root(struct btrfs_trans_handle *trans,
106479154b1bSChris Mason 			       struct btrfs_root *root)
106579154b1bSChris Mason {
106679154b1bSChris Mason 	int ret;
10670b86a832SChris Mason 	u64 old_root_bytenr;
106886b9f2ecSYan, Zheng 	u64 old_root_used;
10690b86a832SChris Mason 	struct btrfs_root *tree_root = root->fs_info->tree_root;
107079154b1bSChris Mason 
107186b9f2ecSYan, Zheng 	old_root_used = btrfs_root_used(&root->root_item);
107256bec294SChris Mason 
107379154b1bSChris Mason 	while (1) {
10740b86a832SChris Mason 		old_root_bytenr = btrfs_root_bytenr(&root->root_item);
107586b9f2ecSYan, Zheng 		if (old_root_bytenr == root->node->start &&
1076ea526d18SJosef Bacik 		    old_root_used == btrfs_root_used(&root->root_item))
107779154b1bSChris Mason 			break;
107887ef2bb4SChris Mason 
10795d4f98a2SYan Zheng 		btrfs_set_root_node(&root->root_item, root->node);
108079154b1bSChris Mason 		ret = btrfs_update_root(trans, tree_root,
10810b86a832SChris Mason 					&root->root_key,
10820b86a832SChris Mason 					&root->root_item);
108349b25e05SJeff Mahoney 		if (ret)
108449b25e05SJeff Mahoney 			return ret;
108556bec294SChris Mason 
108686b9f2ecSYan, Zheng 		old_root_used = btrfs_root_used(&root->root_item);
1087e7070be1SJosef Bacik 	}
1088276e680dSYan Zheng 
10890b86a832SChris Mason 	return 0;
10900b86a832SChris Mason }
10910b86a832SChris Mason 
1092d352ac68SChris Mason /*
1093d352ac68SChris Mason  * update all the cowonly tree roots on disk
109449b25e05SJeff Mahoney  *
109549b25e05SJeff Mahoney  * The error handling in this function may not be obvious. Any of the
109649b25e05SJeff Mahoney  * failures will cause the file system to go offline. We still need
109749b25e05SJeff Mahoney  * to clean up the delayed refs.
1098d352ac68SChris Mason  */
10995d4f98a2SYan Zheng static noinline int commit_cowonly_roots(struct btrfs_trans_handle *trans,
11000b86a832SChris Mason 					 struct btrfs_root *root)
11010b86a832SChris Mason {
11020b86a832SChris Mason 	struct btrfs_fs_info *fs_info = root->fs_info;
1103ea526d18SJosef Bacik 	struct list_head *dirty_bgs = &trans->transaction->dirty_bgs;
11041bbc621eSChris Mason 	struct list_head *io_bgs = &trans->transaction->io_bgs;
11050b86a832SChris Mason 	struct list_head *next;
110684234f3aSYan Zheng 	struct extent_buffer *eb;
110756bec294SChris Mason 	int ret;
110884234f3aSYan Zheng 
110984234f3aSYan Zheng 	eb = btrfs_lock_root_node(fs_info->tree_root);
111049b25e05SJeff Mahoney 	ret = btrfs_cow_block(trans, fs_info->tree_root, eb, NULL,
111149b25e05SJeff Mahoney 			      0, &eb);
111284234f3aSYan Zheng 	btrfs_tree_unlock(eb);
111384234f3aSYan Zheng 	free_extent_buffer(eb);
11140b86a832SChris Mason 
111549b25e05SJeff Mahoney 	if (ret)
111649b25e05SJeff Mahoney 		return ret;
111749b25e05SJeff Mahoney 
111856bec294SChris Mason 	ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
111949b25e05SJeff Mahoney 	if (ret)
112049b25e05SJeff Mahoney 		return ret;
112187ef2bb4SChris Mason 
1122733f4fbbSStefan Behrens 	ret = btrfs_run_dev_stats(trans, root->fs_info);
1123c16ce190SJosef Bacik 	if (ret)
1124c16ce190SJosef Bacik 		return ret;
11258dabb742SStefan Behrens 	ret = btrfs_run_dev_replace(trans, root->fs_info);
1126c16ce190SJosef Bacik 	if (ret)
1127c16ce190SJosef Bacik 		return ret;
1128546adb0dSJan Schmidt 	ret = btrfs_run_qgroups(trans, root->fs_info);
1129c16ce190SJosef Bacik 	if (ret)
1130c16ce190SJosef Bacik 		return ret;
1131546adb0dSJan Schmidt 
1132dcdf7f6dSJosef Bacik 	ret = btrfs_setup_space_cache(trans, root);
1133dcdf7f6dSJosef Bacik 	if (ret)
1134dcdf7f6dSJosef Bacik 		return ret;
1135dcdf7f6dSJosef Bacik 
1136546adb0dSJan Schmidt 	/* run_qgroups might have added some more refs */
1137546adb0dSJan Schmidt 	ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
1138c16ce190SJosef Bacik 	if (ret)
1139c16ce190SJosef Bacik 		return ret;
1140ea526d18SJosef Bacik again:
11410b86a832SChris Mason 	while (!list_empty(&fs_info->dirty_cowonly_roots)) {
11420b86a832SChris Mason 		next = fs_info->dirty_cowonly_roots.next;
11430b86a832SChris Mason 		list_del_init(next);
11440b86a832SChris Mason 		root = list_entry(next, struct btrfs_root, dirty_list);
1145e7070be1SJosef Bacik 		clear_bit(BTRFS_ROOT_DIRTY, &root->state);
114687ef2bb4SChris Mason 
11479e351cc8SJosef Bacik 		if (root != fs_info->extent_root)
11489e351cc8SJosef Bacik 			list_add_tail(&root->dirty_list,
11499e351cc8SJosef Bacik 				      &trans->transaction->switch_commits);
115049b25e05SJeff Mahoney 		ret = update_cowonly_root(trans, root);
115149b25e05SJeff Mahoney 		if (ret)
115249b25e05SJeff Mahoney 			return ret;
1153ea526d18SJosef Bacik 		ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
1154ea526d18SJosef Bacik 		if (ret)
1155ea526d18SJosef Bacik 			return ret;
115679154b1bSChris Mason 	}
1157276e680dSYan Zheng 
11581bbc621eSChris Mason 	while (!list_empty(dirty_bgs) || !list_empty(io_bgs)) {
1159ea526d18SJosef Bacik 		ret = btrfs_write_dirty_block_groups(trans, root);
1160ea526d18SJosef Bacik 		if (ret)
1161ea526d18SJosef Bacik 			return ret;
1162ea526d18SJosef Bacik 		ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
1163ea526d18SJosef Bacik 		if (ret)
1164ea526d18SJosef Bacik 			return ret;
1165ea526d18SJosef Bacik 	}
1166ea526d18SJosef Bacik 
1167ea526d18SJosef Bacik 	if (!list_empty(&fs_info->dirty_cowonly_roots))
1168ea526d18SJosef Bacik 		goto again;
1169ea526d18SJosef Bacik 
11709e351cc8SJosef Bacik 	list_add_tail(&fs_info->extent_root->dirty_list,
11719e351cc8SJosef Bacik 		      &trans->transaction->switch_commits);
11728dabb742SStefan Behrens 	btrfs_after_dev_replace_commit(fs_info);
11738dabb742SStefan Behrens 
117479154b1bSChris Mason 	return 0;
117579154b1bSChris Mason }
117679154b1bSChris Mason 
1177d352ac68SChris Mason /*
1178d352ac68SChris Mason  * dead roots are old snapshots that need to be deleted.  This allocates
1179d352ac68SChris Mason  * a dirty root struct and adds it into the list of dead roots that need to
1180d352ac68SChris Mason  * be deleted
1181d352ac68SChris Mason  */
1182cfad392bSJosef Bacik void btrfs_add_dead_root(struct btrfs_root *root)
11835eda7b5eSChris Mason {
1184a4abeea4SJosef Bacik 	spin_lock(&root->fs_info->trans_lock);
1185cfad392bSJosef Bacik 	if (list_empty(&root->root_list))
11869d1a2a3aSDavid Sterba 		list_add_tail(&root->root_list, &root->fs_info->dead_roots);
1187a4abeea4SJosef Bacik 	spin_unlock(&root->fs_info->trans_lock);
11885eda7b5eSChris Mason }
11895eda7b5eSChris Mason 
1190d352ac68SChris Mason /*
11915d4f98a2SYan Zheng  * update all the cowonly tree roots on disk
1192d352ac68SChris Mason  */
11935d4f98a2SYan Zheng static noinline int commit_fs_roots(struct btrfs_trans_handle *trans,
11945d4f98a2SYan Zheng 				    struct btrfs_root *root)
11950f7d52f4SChris Mason {
11960f7d52f4SChris Mason 	struct btrfs_root *gang[8];
11975d4f98a2SYan Zheng 	struct btrfs_fs_info *fs_info = root->fs_info;
11980f7d52f4SChris Mason 	int i;
11990f7d52f4SChris Mason 	int ret;
120054aa1f4dSChris Mason 	int err = 0;
120154aa1f4dSChris Mason 
1202a4abeea4SJosef Bacik 	spin_lock(&fs_info->fs_roots_radix_lock);
12030f7d52f4SChris Mason 	while (1) {
12045d4f98a2SYan Zheng 		ret = radix_tree_gang_lookup_tag(&fs_info->fs_roots_radix,
12055d4f98a2SYan Zheng 						 (void **)gang, 0,
12060f7d52f4SChris Mason 						 ARRAY_SIZE(gang),
12070f7d52f4SChris Mason 						 BTRFS_ROOT_TRANS_TAG);
12080f7d52f4SChris Mason 		if (ret == 0)
12090f7d52f4SChris Mason 			break;
12100f7d52f4SChris Mason 		for (i = 0; i < ret; i++) {
12110f7d52f4SChris Mason 			root = gang[i];
12125d4f98a2SYan Zheng 			radix_tree_tag_clear(&fs_info->fs_roots_radix,
12132619ba1fSChris Mason 					(unsigned long)root->root_key.objectid,
12140f7d52f4SChris Mason 					BTRFS_ROOT_TRANS_TAG);
1215a4abeea4SJosef Bacik 			spin_unlock(&fs_info->fs_roots_radix_lock);
121631153d81SYan Zheng 
1217e02119d5SChris Mason 			btrfs_free_log(trans, root);
12185d4f98a2SYan Zheng 			btrfs_update_reloc_root(trans, root);
1219d68fc57bSYan, Zheng 			btrfs_orphan_commit_root(trans, root);
1220e02119d5SChris Mason 
122182d5902dSLi Zefan 			btrfs_save_ino_cache(root, trans);
122282d5902dSLi Zefan 
1223f1ebcc74SLiu Bo 			/* see comments in should_cow_block() */
122427cdeb70SMiao Xie 			clear_bit(BTRFS_ROOT_FORCE_COW, &root->state);
1225c7548af6SChris Mason 			smp_mb__after_atomic();
1226f1ebcc74SLiu Bo 
1227978d910dSYan Zheng 			if (root->commit_root != root->node) {
12289e351cc8SJosef Bacik 				list_add_tail(&root->dirty_list,
12299e351cc8SJosef Bacik 					&trans->transaction->switch_commits);
1230978d910dSYan Zheng 				btrfs_set_root_node(&root->root_item,
1231978d910dSYan Zheng 						    root->node);
1232978d910dSYan Zheng 			}
123331153d81SYan Zheng 
12345d4f98a2SYan Zheng 			err = btrfs_update_root(trans, fs_info->tree_root,
12350f7d52f4SChris Mason 						&root->root_key,
12360f7d52f4SChris Mason 						&root->root_item);
1237a4abeea4SJosef Bacik 			spin_lock(&fs_info->fs_roots_radix_lock);
123854aa1f4dSChris Mason 			if (err)
123954aa1f4dSChris Mason 				break;
12400f7d52f4SChris Mason 		}
12419f3a7427SChris Mason 	}
1242a4abeea4SJosef Bacik 	spin_unlock(&fs_info->fs_roots_radix_lock);
124354aa1f4dSChris Mason 	return err;
12440f7d52f4SChris Mason }
12450f7d52f4SChris Mason 
1246d352ac68SChris Mason /*
1247de78b51aSEric Sandeen  * defrag a given btree.
1248de78b51aSEric Sandeen  * Every leaf in the btree is read and defragged.
1249d352ac68SChris Mason  */
1250de78b51aSEric Sandeen int btrfs_defrag_root(struct btrfs_root *root)
1251e9d0b13bSChris Mason {
1252e9d0b13bSChris Mason 	struct btrfs_fs_info *info = root->fs_info;
1253e9d0b13bSChris Mason 	struct btrfs_trans_handle *trans;
12548929ecfaSYan, Zheng 	int ret;
1255e9d0b13bSChris Mason 
125627cdeb70SMiao Xie 	if (test_and_set_bit(BTRFS_ROOT_DEFRAG_RUNNING, &root->state))
1257e9d0b13bSChris Mason 		return 0;
12588929ecfaSYan, Zheng 
12596b80053dSChris Mason 	while (1) {
12608929ecfaSYan, Zheng 		trans = btrfs_start_transaction(root, 0);
12618929ecfaSYan, Zheng 		if (IS_ERR(trans))
12628929ecfaSYan, Zheng 			return PTR_ERR(trans);
12638929ecfaSYan, Zheng 
1264de78b51aSEric Sandeen 		ret = btrfs_defrag_leaves(trans, root);
12658929ecfaSYan, Zheng 
1266e9d0b13bSChris Mason 		btrfs_end_transaction(trans, root);
1267b53d3f5dSLiu Bo 		btrfs_btree_balance_dirty(info->tree_root);
1268e9d0b13bSChris Mason 		cond_resched();
1269e9d0b13bSChris Mason 
12707841cb28SDavid Sterba 		if (btrfs_fs_closing(root->fs_info) || ret != -EAGAIN)
1271e9d0b13bSChris Mason 			break;
1272210549ebSDavid Sterba 
1273210549ebSDavid Sterba 		if (btrfs_defrag_cancelled(root->fs_info)) {
1274efe120a0SFrank Holton 			pr_debug("BTRFS: defrag_root cancelled\n");
1275210549ebSDavid Sterba 			ret = -EAGAIN;
1276210549ebSDavid Sterba 			break;
1277210549ebSDavid Sterba 		}
1278e9d0b13bSChris Mason 	}
127927cdeb70SMiao Xie 	clear_bit(BTRFS_ROOT_DEFRAG_RUNNING, &root->state);
12808929ecfaSYan, Zheng 	return ret;
1281e9d0b13bSChris Mason }
1282e9d0b13bSChris Mason 
1283d352ac68SChris Mason /*
1284d352ac68SChris Mason  * new snapshots need to be created at a very specific time in the
1285aec8030aSMiao Xie  * transaction commit.  This does the actual creation.
1286aec8030aSMiao Xie  *
1287aec8030aSMiao Xie  * Note:
1288aec8030aSMiao Xie  * If the error which may affect the commitment of the current transaction
1289aec8030aSMiao Xie  * happens, we should return the error number. If the error which just affect
1290aec8030aSMiao Xie  * the creation of the pending snapshots, just return 0.
1291d352ac68SChris Mason  */
129280b6794dSChris Mason static noinline int create_pending_snapshot(struct btrfs_trans_handle *trans,
12933063d29fSChris Mason 				   struct btrfs_fs_info *fs_info,
12943063d29fSChris Mason 				   struct btrfs_pending_snapshot *pending)
12953063d29fSChris Mason {
12963063d29fSChris Mason 	struct btrfs_key key;
129780b6794dSChris Mason 	struct btrfs_root_item *new_root_item;
12983063d29fSChris Mason 	struct btrfs_root *tree_root = fs_info->tree_root;
12993063d29fSChris Mason 	struct btrfs_root *root = pending->root;
13006bdb72deSSage Weil 	struct btrfs_root *parent_root;
130198c9942aSLiu Bo 	struct btrfs_block_rsv *rsv;
13026bdb72deSSage Weil 	struct inode *parent_inode;
130342874b3dSMiao Xie 	struct btrfs_path *path;
130442874b3dSMiao Xie 	struct btrfs_dir_item *dir_item;
1305a22285a6SYan, Zheng 	struct dentry *dentry;
13063063d29fSChris Mason 	struct extent_buffer *tmp;
1307925baeddSChris Mason 	struct extent_buffer *old;
13088ea05e3aSAlexander Block 	struct timespec cur_time = CURRENT_TIME;
1309aec8030aSMiao Xie 	int ret = 0;
1310d68fc57bSYan, Zheng 	u64 to_reserve = 0;
13116bdb72deSSage Weil 	u64 index = 0;
1312a22285a6SYan, Zheng 	u64 objectid;
1313b83cc969SLi Zefan 	u64 root_flags;
13148ea05e3aSAlexander Block 	uuid_le new_uuid;
13153063d29fSChris Mason 
131642874b3dSMiao Xie 	path = btrfs_alloc_path();
131742874b3dSMiao Xie 	if (!path) {
1318aec8030aSMiao Xie 		pending->error = -ENOMEM;
1319aec8030aSMiao Xie 		return 0;
132042874b3dSMiao Xie 	}
132142874b3dSMiao Xie 
132280b6794dSChris Mason 	new_root_item = kmalloc(sizeof(*new_root_item), GFP_NOFS);
132380b6794dSChris Mason 	if (!new_root_item) {
1324aec8030aSMiao Xie 		pending->error = -ENOMEM;
13256fa9700eSMiao Xie 		goto root_item_alloc_fail;
132680b6794dSChris Mason 	}
1327a22285a6SYan, Zheng 
1328aec8030aSMiao Xie 	pending->error = btrfs_find_free_objectid(tree_root, &objectid);
1329aec8030aSMiao Xie 	if (pending->error)
13306fa9700eSMiao Xie 		goto no_free_objectid;
13313063d29fSChris Mason 
1332d6726335SQu Wenruo 	/*
1333d6726335SQu Wenruo 	 * Make qgroup to skip current new snapshot's qgroupid, as it is
1334d6726335SQu Wenruo 	 * accounted by later btrfs_qgroup_inherit().
1335d6726335SQu Wenruo 	 */
1336d6726335SQu Wenruo 	btrfs_set_skip_qgroup(trans, objectid);
1337d6726335SQu Wenruo 
1338147d256eSZhaolei 	btrfs_reloc_pre_snapshot(pending, &to_reserve);
1339d68fc57bSYan, Zheng 
1340d68fc57bSYan, Zheng 	if (to_reserve > 0) {
1341aec8030aSMiao Xie 		pending->error = btrfs_block_rsv_add(root,
1342aec8030aSMiao Xie 						     &pending->block_rsv,
134308e007d2SMiao Xie 						     to_reserve,
134408e007d2SMiao Xie 						     BTRFS_RESERVE_NO_FLUSH);
1345aec8030aSMiao Xie 		if (pending->error)
1346d6726335SQu Wenruo 			goto clear_skip_qgroup;
1347d68fc57bSYan, Zheng 	}
1348d68fc57bSYan, Zheng 
13493063d29fSChris Mason 	key.objectid = objectid;
1350a22285a6SYan, Zheng 	key.offset = (u64)-1;
1351a22285a6SYan, Zheng 	key.type = BTRFS_ROOT_ITEM_KEY;
13523063d29fSChris Mason 
13536fa9700eSMiao Xie 	rsv = trans->block_rsv;
1354a22285a6SYan, Zheng 	trans->block_rsv = &pending->block_rsv;
13552382c5ccSLiu Bo 	trans->bytes_reserved = trans->block_rsv->reserved;
13566bdb72deSSage Weil 
1357a22285a6SYan, Zheng 	dentry = pending->dentry;
1358e9662f70SMiao Xie 	parent_inode = pending->dir;
1359a22285a6SYan, Zheng 	parent_root = BTRFS_I(parent_inode)->root;
13607585717fSChris Mason 	record_root_in_trans(trans, parent_root);
1361a22285a6SYan, Zheng 
13626bdb72deSSage Weil 	/*
13636bdb72deSSage Weil 	 * insert the directory item
13646bdb72deSSage Weil 	 */
13656bdb72deSSage Weil 	ret = btrfs_set_inode_index(parent_inode, &index);
136649b25e05SJeff Mahoney 	BUG_ON(ret); /* -ENOMEM */
136742874b3dSMiao Xie 
136842874b3dSMiao Xie 	/* check if there is a file/dir which has the same name. */
136942874b3dSMiao Xie 	dir_item = btrfs_lookup_dir_item(NULL, parent_root, path,
137042874b3dSMiao Xie 					 btrfs_ino(parent_inode),
137142874b3dSMiao Xie 					 dentry->d_name.name,
137242874b3dSMiao Xie 					 dentry->d_name.len, 0);
137342874b3dSMiao Xie 	if (dir_item != NULL && !IS_ERR(dir_item)) {
1374fe66a05aSChris Mason 		pending->error = -EEXIST;
1375aec8030aSMiao Xie 		goto dir_item_existed;
137642874b3dSMiao Xie 	} else if (IS_ERR(dir_item)) {
137742874b3dSMiao Xie 		ret = PTR_ERR(dir_item);
13788732d44fSMiao Xie 		btrfs_abort_transaction(trans, root, ret);
13798732d44fSMiao Xie 		goto fail;
138079787eaaSJeff Mahoney 	}
138142874b3dSMiao Xie 	btrfs_release_path(path);
13826bdb72deSSage Weil 
1383e999376fSChris Mason 	/*
1384e999376fSChris Mason 	 * pull in the delayed directory update
1385e999376fSChris Mason 	 * and the delayed inode item
1386e999376fSChris Mason 	 * otherwise we corrupt the FS during
1387e999376fSChris Mason 	 * snapshot
1388e999376fSChris Mason 	 */
1389e999376fSChris Mason 	ret = btrfs_run_delayed_items(trans, root);
13908732d44fSMiao Xie 	if (ret) {	/* Transaction aborted */
13918732d44fSMiao Xie 		btrfs_abort_transaction(trans, root, ret);
13928732d44fSMiao Xie 		goto fail;
13938732d44fSMiao Xie 	}
1394e999376fSChris Mason 
13957585717fSChris Mason 	record_root_in_trans(trans, root);
13966bdb72deSSage Weil 	btrfs_set_root_last_snapshot(&root->root_item, trans->transid);
13976bdb72deSSage Weil 	memcpy(new_root_item, &root->root_item, sizeof(*new_root_item));
139808fe4db1SLi Zefan 	btrfs_check_and_init_root_item(new_root_item);
13996bdb72deSSage Weil 
1400b83cc969SLi Zefan 	root_flags = btrfs_root_flags(new_root_item);
1401b83cc969SLi Zefan 	if (pending->readonly)
1402b83cc969SLi Zefan 		root_flags |= BTRFS_ROOT_SUBVOL_RDONLY;
1403b83cc969SLi Zefan 	else
1404b83cc969SLi Zefan 		root_flags &= ~BTRFS_ROOT_SUBVOL_RDONLY;
1405b83cc969SLi Zefan 	btrfs_set_root_flags(new_root_item, root_flags);
1406b83cc969SLi Zefan 
14078ea05e3aSAlexander Block 	btrfs_set_root_generation_v2(new_root_item,
14088ea05e3aSAlexander Block 			trans->transid);
14098ea05e3aSAlexander Block 	uuid_le_gen(&new_uuid);
14108ea05e3aSAlexander Block 	memcpy(new_root_item->uuid, new_uuid.b, BTRFS_UUID_SIZE);
14118ea05e3aSAlexander Block 	memcpy(new_root_item->parent_uuid, root->root_item.uuid,
14128ea05e3aSAlexander Block 			BTRFS_UUID_SIZE);
141370023da2SStefan Behrens 	if (!(root_flags & BTRFS_ROOT_SUBVOL_RDONLY)) {
141470023da2SStefan Behrens 		memset(new_root_item->received_uuid, 0,
141570023da2SStefan Behrens 		       sizeof(new_root_item->received_uuid));
14168ea05e3aSAlexander Block 		memset(&new_root_item->stime, 0, sizeof(new_root_item->stime));
14178ea05e3aSAlexander Block 		memset(&new_root_item->rtime, 0, sizeof(new_root_item->rtime));
14188ea05e3aSAlexander Block 		btrfs_set_root_stransid(new_root_item, 0);
14198ea05e3aSAlexander Block 		btrfs_set_root_rtransid(new_root_item, 0);
142070023da2SStefan Behrens 	}
14213cae210fSQu Wenruo 	btrfs_set_stack_timespec_sec(&new_root_item->otime, cur_time.tv_sec);
14223cae210fSQu Wenruo 	btrfs_set_stack_timespec_nsec(&new_root_item->otime, cur_time.tv_nsec);
142370023da2SStefan Behrens 	btrfs_set_root_otransid(new_root_item, trans->transid);
14248ea05e3aSAlexander Block 
1425925baeddSChris Mason 	old = btrfs_lock_root_node(root);
142649b25e05SJeff Mahoney 	ret = btrfs_cow_block(trans, root, old, NULL, 0, &old);
142779787eaaSJeff Mahoney 	if (ret) {
142879787eaaSJeff Mahoney 		btrfs_tree_unlock(old);
142979787eaaSJeff Mahoney 		free_extent_buffer(old);
14308732d44fSMiao Xie 		btrfs_abort_transaction(trans, root, ret);
14318732d44fSMiao Xie 		goto fail;
143279787eaaSJeff Mahoney 	}
143349b25e05SJeff Mahoney 
14345d4f98a2SYan Zheng 	btrfs_set_lock_blocking(old);
14353063d29fSChris Mason 
143649b25e05SJeff Mahoney 	ret = btrfs_copy_root(trans, root, old, &tmp, objectid);
143779787eaaSJeff Mahoney 	/* clean up in any case */
1438925baeddSChris Mason 	btrfs_tree_unlock(old);
1439925baeddSChris Mason 	free_extent_buffer(old);
14408732d44fSMiao Xie 	if (ret) {
14418732d44fSMiao Xie 		btrfs_abort_transaction(trans, root, ret);
14428732d44fSMiao Xie 		goto fail;
14438732d44fSMiao Xie 	}
1444f1ebcc74SLiu Bo 	/* see comments in should_cow_block() */
144527cdeb70SMiao Xie 	set_bit(BTRFS_ROOT_FORCE_COW, &root->state);
1446f1ebcc74SLiu Bo 	smp_wmb();
1447f1ebcc74SLiu Bo 
14485d4f98a2SYan Zheng 	btrfs_set_root_node(new_root_item, tmp);
1449a22285a6SYan, Zheng 	/* record when the snapshot was created in key.offset */
1450a22285a6SYan, Zheng 	key.offset = trans->transid;
1451a22285a6SYan, Zheng 	ret = btrfs_insert_root(trans, tree_root, &key, new_root_item);
1452925baeddSChris Mason 	btrfs_tree_unlock(tmp);
14533063d29fSChris Mason 	free_extent_buffer(tmp);
14548732d44fSMiao Xie 	if (ret) {
14558732d44fSMiao Xie 		btrfs_abort_transaction(trans, root, ret);
14568732d44fSMiao Xie 		goto fail;
14578732d44fSMiao Xie 	}
14580660b5afSChris Mason 
1459a22285a6SYan, Zheng 	/*
1460a22285a6SYan, Zheng 	 * insert root back/forward references
1461a22285a6SYan, Zheng 	 */
1462a22285a6SYan, Zheng 	ret = btrfs_add_root_ref(trans, tree_root, objectid,
1463a22285a6SYan, Zheng 				 parent_root->root_key.objectid,
146433345d01SLi Zefan 				 btrfs_ino(parent_inode), index,
1465a22285a6SYan, Zheng 				 dentry->d_name.name, dentry->d_name.len);
14668732d44fSMiao Xie 	if (ret) {
14678732d44fSMiao Xie 		btrfs_abort_transaction(trans, root, ret);
14688732d44fSMiao Xie 		goto fail;
14698732d44fSMiao Xie 	}
1470a22285a6SYan, Zheng 
1471a22285a6SYan, Zheng 	key.offset = (u64)-1;
1472a22285a6SYan, Zheng 	pending->snap = btrfs_read_fs_root_no_name(root->fs_info, &key);
147379787eaaSJeff Mahoney 	if (IS_ERR(pending->snap)) {
147479787eaaSJeff Mahoney 		ret = PTR_ERR(pending->snap);
14758732d44fSMiao Xie 		btrfs_abort_transaction(trans, root, ret);
14768732d44fSMiao Xie 		goto fail;
147779787eaaSJeff Mahoney 	}
1478d68fc57bSYan, Zheng 
147949b25e05SJeff Mahoney 	ret = btrfs_reloc_post_snapshot(trans, pending);
14808732d44fSMiao Xie 	if (ret) {
14818732d44fSMiao Xie 		btrfs_abort_transaction(trans, root, ret);
14828732d44fSMiao Xie 		goto fail;
14838732d44fSMiao Xie 	}
1484361048f5SMiao Xie 
1485361048f5SMiao Xie 	ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
14868732d44fSMiao Xie 	if (ret) {
14878732d44fSMiao Xie 		btrfs_abort_transaction(trans, root, ret);
14888732d44fSMiao Xie 		goto fail;
14898732d44fSMiao Xie 	}
149042874b3dSMiao Xie 
149142874b3dSMiao Xie 	ret = btrfs_insert_dir_item(trans, parent_root,
149242874b3dSMiao Xie 				    dentry->d_name.name, dentry->d_name.len,
149342874b3dSMiao Xie 				    parent_inode, &key,
149442874b3dSMiao Xie 				    BTRFS_FT_DIR, index);
149542874b3dSMiao Xie 	/* We have check then name at the beginning, so it is impossible. */
14969c52057cSChris Mason 	BUG_ON(ret == -EEXIST || ret == -EOVERFLOW);
14978732d44fSMiao Xie 	if (ret) {
14988732d44fSMiao Xie 		btrfs_abort_transaction(trans, root, ret);
14998732d44fSMiao Xie 		goto fail;
15008732d44fSMiao Xie 	}
150142874b3dSMiao Xie 
150242874b3dSMiao Xie 	btrfs_i_size_write(parent_inode, parent_inode->i_size +
150342874b3dSMiao Xie 					 dentry->d_name.len * 2);
150442874b3dSMiao Xie 	parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
1505be6aef60SJosef Bacik 	ret = btrfs_update_inode_fallback(trans, parent_root, parent_inode);
1506dd5f9615SStefan Behrens 	if (ret) {
15078732d44fSMiao Xie 		btrfs_abort_transaction(trans, root, ret);
1508dd5f9615SStefan Behrens 		goto fail;
1509dd5f9615SStefan Behrens 	}
1510dd5f9615SStefan Behrens 	ret = btrfs_uuid_tree_add(trans, fs_info->uuid_root, new_uuid.b,
1511dd5f9615SStefan Behrens 				  BTRFS_UUID_KEY_SUBVOL, objectid);
1512dd5f9615SStefan Behrens 	if (ret) {
1513dd5f9615SStefan Behrens 		btrfs_abort_transaction(trans, root, ret);
1514dd5f9615SStefan Behrens 		goto fail;
1515dd5f9615SStefan Behrens 	}
1516dd5f9615SStefan Behrens 	if (!btrfs_is_empty_uuid(new_root_item->received_uuid)) {
1517dd5f9615SStefan Behrens 		ret = btrfs_uuid_tree_add(trans, fs_info->uuid_root,
1518dd5f9615SStefan Behrens 					  new_root_item->received_uuid,
1519dd5f9615SStefan Behrens 					  BTRFS_UUID_KEY_RECEIVED_SUBVOL,
1520dd5f9615SStefan Behrens 					  objectid);
1521dd5f9615SStefan Behrens 		if (ret && ret != -EEXIST) {
1522dd5f9615SStefan Behrens 			btrfs_abort_transaction(trans, root, ret);
1523dd5f9615SStefan Behrens 			goto fail;
1524dd5f9615SStefan Behrens 		}
1525dd5f9615SStefan Behrens 	}
1526d6726335SQu Wenruo 
1527d6726335SQu Wenruo 	ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
1528d6726335SQu Wenruo 	if (ret) {
1529d6726335SQu Wenruo 		btrfs_abort_transaction(trans, root, ret);
1530d6726335SQu Wenruo 		goto fail;
1531d6726335SQu Wenruo 	}
1532d6726335SQu Wenruo 
1533d6726335SQu Wenruo 	/*
1534d6726335SQu Wenruo 	 * account qgroup counters before qgroup_inherit()
1535d6726335SQu Wenruo 	 */
1536d6726335SQu Wenruo 	ret = btrfs_qgroup_prepare_account_extents(trans, fs_info);
1537d6726335SQu Wenruo 	if (ret)
1538d6726335SQu Wenruo 		goto fail;
1539d6726335SQu Wenruo 	ret = btrfs_qgroup_account_extents(trans, fs_info);
1540d6726335SQu Wenruo 	if (ret)
1541d6726335SQu Wenruo 		goto fail;
1542d6726335SQu Wenruo 	ret = btrfs_qgroup_inherit(trans, fs_info,
1543d6726335SQu Wenruo 				   root->root_key.objectid,
1544d6726335SQu Wenruo 				   objectid, pending->inherit);
1545d6726335SQu Wenruo 	if (ret) {
1546d6726335SQu Wenruo 		btrfs_abort_transaction(trans, root, ret);
1547d6726335SQu Wenruo 		goto fail;
1548d6726335SQu Wenruo 	}
1549d6726335SQu Wenruo 
15503063d29fSChris Mason fail:
1551aec8030aSMiao Xie 	pending->error = ret;
1552aec8030aSMiao Xie dir_item_existed:
155398c9942aSLiu Bo 	trans->block_rsv = rsv;
15542382c5ccSLiu Bo 	trans->bytes_reserved = 0;
1555d6726335SQu Wenruo clear_skip_qgroup:
1556d6726335SQu Wenruo 	btrfs_clear_skip_qgroup(trans);
15576fa9700eSMiao Xie no_free_objectid:
15586fa9700eSMiao Xie 	kfree(new_root_item);
15596fa9700eSMiao Xie root_item_alloc_fail:
156042874b3dSMiao Xie 	btrfs_free_path(path);
156149b25e05SJeff Mahoney 	return ret;
15623063d29fSChris Mason }
15633063d29fSChris Mason 
1564d352ac68SChris Mason /*
1565d352ac68SChris Mason  * create all the snapshots we've scheduled for creation
1566d352ac68SChris Mason  */
156780b6794dSChris Mason static noinline int create_pending_snapshots(struct btrfs_trans_handle *trans,
15683063d29fSChris Mason 					     struct btrfs_fs_info *fs_info)
15693063d29fSChris Mason {
1570aec8030aSMiao Xie 	struct btrfs_pending_snapshot *pending, *next;
15713063d29fSChris Mason 	struct list_head *head = &trans->transaction->pending_snapshots;
1572aec8030aSMiao Xie 	int ret = 0;
15733de4586cSChris Mason 
1574aec8030aSMiao Xie 	list_for_each_entry_safe(pending, next, head, list) {
1575aec8030aSMiao Xie 		list_del(&pending->list);
1576aec8030aSMiao Xie 		ret = create_pending_snapshot(trans, fs_info, pending);
1577aec8030aSMiao Xie 		if (ret)
1578aec8030aSMiao Xie 			break;
1579aec8030aSMiao Xie 	}
1580aec8030aSMiao Xie 	return ret;
15813de4586cSChris Mason }
15823de4586cSChris Mason 
15835d4f98a2SYan Zheng static void update_super_roots(struct btrfs_root *root)
15845d4f98a2SYan Zheng {
15855d4f98a2SYan Zheng 	struct btrfs_root_item *root_item;
15865d4f98a2SYan Zheng 	struct btrfs_super_block *super;
15875d4f98a2SYan Zheng 
15886c41761fSDavid Sterba 	super = root->fs_info->super_copy;
15895d4f98a2SYan Zheng 
15905d4f98a2SYan Zheng 	root_item = &root->fs_info->chunk_root->root_item;
15915d4f98a2SYan Zheng 	super->chunk_root = root_item->bytenr;
15925d4f98a2SYan Zheng 	super->chunk_root_generation = root_item->generation;
15935d4f98a2SYan Zheng 	super->chunk_root_level = root_item->level;
15945d4f98a2SYan Zheng 
15955d4f98a2SYan Zheng 	root_item = &root->fs_info->tree_root->root_item;
15965d4f98a2SYan Zheng 	super->root = root_item->bytenr;
15975d4f98a2SYan Zheng 	super->generation = root_item->generation;
15985d4f98a2SYan Zheng 	super->root_level = root_item->level;
159973bc1876SJosef Bacik 	if (btrfs_test_opt(root, SPACE_CACHE))
16000af3d00bSJosef Bacik 		super->cache_generation = root_item->generation;
160170f80175SStefan Behrens 	if (root->fs_info->update_uuid_tree_gen)
160226432799SStefan Behrens 		super->uuid_tree_generation = root_item->generation;
16035d4f98a2SYan Zheng }
16045d4f98a2SYan Zheng 
1605f36f3042SChris Mason int btrfs_transaction_in_commit(struct btrfs_fs_info *info)
1606f36f3042SChris Mason {
16074a9d8bdeSMiao Xie 	struct btrfs_transaction *trans;
1608f36f3042SChris Mason 	int ret = 0;
16094a9d8bdeSMiao Xie 
1610a4abeea4SJosef Bacik 	spin_lock(&info->trans_lock);
16114a9d8bdeSMiao Xie 	trans = info->running_transaction;
16124a9d8bdeSMiao Xie 	if (trans)
16134a9d8bdeSMiao Xie 		ret = (trans->state >= TRANS_STATE_COMMIT_START);
1614a4abeea4SJosef Bacik 	spin_unlock(&info->trans_lock);
1615f36f3042SChris Mason 	return ret;
1616f36f3042SChris Mason }
1617f36f3042SChris Mason 
16188929ecfaSYan, Zheng int btrfs_transaction_blocked(struct btrfs_fs_info *info)
16198929ecfaSYan, Zheng {
16204a9d8bdeSMiao Xie 	struct btrfs_transaction *trans;
16218929ecfaSYan, Zheng 	int ret = 0;
16224a9d8bdeSMiao Xie 
1623a4abeea4SJosef Bacik 	spin_lock(&info->trans_lock);
16244a9d8bdeSMiao Xie 	trans = info->running_transaction;
16254a9d8bdeSMiao Xie 	if (trans)
16264a9d8bdeSMiao Xie 		ret = is_transaction_blocked(trans);
1627a4abeea4SJosef Bacik 	spin_unlock(&info->trans_lock);
16288929ecfaSYan, Zheng 	return ret;
16298929ecfaSYan, Zheng }
16308929ecfaSYan, Zheng 
1631bb9c12c9SSage Weil /*
1632bb9c12c9SSage Weil  * wait for the current transaction commit to start and block subsequent
1633bb9c12c9SSage Weil  * transaction joins
1634bb9c12c9SSage Weil  */
1635bb9c12c9SSage Weil static void wait_current_trans_commit_start(struct btrfs_root *root,
1636bb9c12c9SSage Weil 					    struct btrfs_transaction *trans)
1637bb9c12c9SSage Weil {
16384a9d8bdeSMiao Xie 	wait_event(root->fs_info->transaction_blocked_wait,
1639501407aaSJosef Bacik 		   trans->state >= TRANS_STATE_COMMIT_START ||
1640501407aaSJosef Bacik 		   trans->aborted);
1641bb9c12c9SSage Weil }
1642bb9c12c9SSage Weil 
1643bb9c12c9SSage Weil /*
1644bb9c12c9SSage Weil  * wait for the current transaction to start and then become unblocked.
1645bb9c12c9SSage Weil  * caller holds ref.
1646bb9c12c9SSage Weil  */
1647bb9c12c9SSage Weil static void wait_current_trans_commit_start_and_unblock(struct btrfs_root *root,
1648bb9c12c9SSage Weil 					 struct btrfs_transaction *trans)
1649bb9c12c9SSage Weil {
165072d63ed6SLi Zefan 	wait_event(root->fs_info->transaction_wait,
1651501407aaSJosef Bacik 		   trans->state >= TRANS_STATE_UNBLOCKED ||
1652501407aaSJosef Bacik 		   trans->aborted);
1653bb9c12c9SSage Weil }
1654bb9c12c9SSage Weil 
1655bb9c12c9SSage Weil /*
1656bb9c12c9SSage Weil  * commit transactions asynchronously. once btrfs_commit_transaction_async
1657bb9c12c9SSage Weil  * returns, any subsequent transaction will not be allowed to join.
1658bb9c12c9SSage Weil  */
1659bb9c12c9SSage Weil struct btrfs_async_commit {
1660bb9c12c9SSage Weil 	struct btrfs_trans_handle *newtrans;
1661bb9c12c9SSage Weil 	struct btrfs_root *root;
16627892b5afSMiao Xie 	struct work_struct work;
1663bb9c12c9SSage Weil };
1664bb9c12c9SSage Weil 
1665bb9c12c9SSage Weil static void do_async_commit(struct work_struct *work)
1666bb9c12c9SSage Weil {
1667bb9c12c9SSage Weil 	struct btrfs_async_commit *ac =
16687892b5afSMiao Xie 		container_of(work, struct btrfs_async_commit, work);
1669bb9c12c9SSage Weil 
16706fc4e354SSage Weil 	/*
16716fc4e354SSage Weil 	 * We've got freeze protection passed with the transaction.
16726fc4e354SSage Weil 	 * Tell lockdep about it.
16736fc4e354SSage Weil 	 */
1674b1a06a4bSLiu Bo 	if (ac->newtrans->type & __TRANS_FREEZABLE)
16756fc4e354SSage Weil 		rwsem_acquire_read(
16766fc4e354SSage Weil 		     &ac->root->fs_info->sb->s_writers.lock_map[SB_FREEZE_FS-1],
16776fc4e354SSage Weil 		     0, 1, _THIS_IP_);
16786fc4e354SSage Weil 
1679e209db7aSSage Weil 	current->journal_info = ac->newtrans;
1680e209db7aSSage Weil 
1681bb9c12c9SSage Weil 	btrfs_commit_transaction(ac->newtrans, ac->root);
1682bb9c12c9SSage Weil 	kfree(ac);
1683bb9c12c9SSage Weil }
1684bb9c12c9SSage Weil 
1685bb9c12c9SSage Weil int btrfs_commit_transaction_async(struct btrfs_trans_handle *trans,
1686bb9c12c9SSage Weil 				   struct btrfs_root *root,
1687bb9c12c9SSage Weil 				   int wait_for_unblock)
1688bb9c12c9SSage Weil {
1689bb9c12c9SSage Weil 	struct btrfs_async_commit *ac;
1690bb9c12c9SSage Weil 	struct btrfs_transaction *cur_trans;
1691bb9c12c9SSage Weil 
1692bb9c12c9SSage Weil 	ac = kmalloc(sizeof(*ac), GFP_NOFS);
1693db5b493aSTsutomu Itoh 	if (!ac)
1694db5b493aSTsutomu Itoh 		return -ENOMEM;
1695bb9c12c9SSage Weil 
16967892b5afSMiao Xie 	INIT_WORK(&ac->work, do_async_commit);
1697bb9c12c9SSage Weil 	ac->root = root;
16987a7eaa40SJosef Bacik 	ac->newtrans = btrfs_join_transaction(root);
16993612b495STsutomu Itoh 	if (IS_ERR(ac->newtrans)) {
17003612b495STsutomu Itoh 		int err = PTR_ERR(ac->newtrans);
17013612b495STsutomu Itoh 		kfree(ac);
17023612b495STsutomu Itoh 		return err;
17033612b495STsutomu Itoh 	}
1704bb9c12c9SSage Weil 
1705bb9c12c9SSage Weil 	/* take transaction reference */
1706bb9c12c9SSage Weil 	cur_trans = trans->transaction;
170713c5a93eSJosef Bacik 	atomic_inc(&cur_trans->use_count);
1708bb9c12c9SSage Weil 
1709bb9c12c9SSage Weil 	btrfs_end_transaction(trans, root);
17106fc4e354SSage Weil 
17116fc4e354SSage Weil 	/*
17126fc4e354SSage Weil 	 * Tell lockdep we've released the freeze rwsem, since the
17136fc4e354SSage Weil 	 * async commit thread will be the one to unlock it.
17146fc4e354SSage Weil 	 */
1715b1a06a4bSLiu Bo 	if (ac->newtrans->type & __TRANS_FREEZABLE)
1716ff7c1d33SMiao Xie 		rwsem_release(
1717ff7c1d33SMiao Xie 			&root->fs_info->sb->s_writers.lock_map[SB_FREEZE_FS-1],
17186fc4e354SSage Weil 			1, _THIS_IP_);
17196fc4e354SSage Weil 
17207892b5afSMiao Xie 	schedule_work(&ac->work);
1721bb9c12c9SSage Weil 
1722bb9c12c9SSage Weil 	/* wait for transaction to start and unblock */
1723bb9c12c9SSage Weil 	if (wait_for_unblock)
1724bb9c12c9SSage Weil 		wait_current_trans_commit_start_and_unblock(root, cur_trans);
1725bb9c12c9SSage Weil 	else
1726bb9c12c9SSage Weil 		wait_current_trans_commit_start(root, cur_trans);
1727bb9c12c9SSage Weil 
172838e88054SSage Weil 	if (current->journal_info == trans)
172938e88054SSage Weil 		current->journal_info = NULL;
173038e88054SSage Weil 
1731724e2315SJosef Bacik 	btrfs_put_transaction(cur_trans);
1732bb9c12c9SSage Weil 	return 0;
1733bb9c12c9SSage Weil }
1734bb9c12c9SSage Weil 
173549b25e05SJeff Mahoney 
173649b25e05SJeff Mahoney static void cleanup_transaction(struct btrfs_trans_handle *trans,
17377b8b92afSJosef Bacik 				struct btrfs_root *root, int err)
173849b25e05SJeff Mahoney {
173949b25e05SJeff Mahoney 	struct btrfs_transaction *cur_trans = trans->transaction;
1740f094ac32SLiu Bo 	DEFINE_WAIT(wait);
174149b25e05SJeff Mahoney 
174249b25e05SJeff Mahoney 	WARN_ON(trans->use_count > 1);
174349b25e05SJeff Mahoney 
17447b8b92afSJosef Bacik 	btrfs_abort_transaction(trans, root, err);
17457b8b92afSJosef Bacik 
174649b25e05SJeff Mahoney 	spin_lock(&root->fs_info->trans_lock);
174766b6135bSLiu Bo 
174825d8c284SMiao Xie 	/*
174925d8c284SMiao Xie 	 * If the transaction is removed from the list, it means this
175025d8c284SMiao Xie 	 * transaction has been committed successfully, so it is impossible
175125d8c284SMiao Xie 	 * to call the cleanup function.
175225d8c284SMiao Xie 	 */
175325d8c284SMiao Xie 	BUG_ON(list_empty(&cur_trans->list));
175466b6135bSLiu Bo 
175549b25e05SJeff Mahoney 	list_del_init(&cur_trans->list);
1756d7096fc3SJosef Bacik 	if (cur_trans == root->fs_info->running_transaction) {
17574a9d8bdeSMiao Xie 		cur_trans->state = TRANS_STATE_COMMIT_DOING;
1758f094ac32SLiu Bo 		spin_unlock(&root->fs_info->trans_lock);
1759f094ac32SLiu Bo 		wait_event(cur_trans->writer_wait,
1760f094ac32SLiu Bo 			   atomic_read(&cur_trans->num_writers) == 1);
1761f094ac32SLiu Bo 
1762f094ac32SLiu Bo 		spin_lock(&root->fs_info->trans_lock);
1763d7096fc3SJosef Bacik 	}
176449b25e05SJeff Mahoney 	spin_unlock(&root->fs_info->trans_lock);
176549b25e05SJeff Mahoney 
176649b25e05SJeff Mahoney 	btrfs_cleanup_one_transaction(trans->transaction, root);
176749b25e05SJeff Mahoney 
17684a9d8bdeSMiao Xie 	spin_lock(&root->fs_info->trans_lock);
17694a9d8bdeSMiao Xie 	if (cur_trans == root->fs_info->running_transaction)
17704a9d8bdeSMiao Xie 		root->fs_info->running_transaction = NULL;
17714a9d8bdeSMiao Xie 	spin_unlock(&root->fs_info->trans_lock);
17724a9d8bdeSMiao Xie 
1773e0228285SJosef Bacik 	if (trans->type & __TRANS_FREEZABLE)
1774e0228285SJosef Bacik 		sb_end_intwrite(root->fs_info->sb);
1775724e2315SJosef Bacik 	btrfs_put_transaction(cur_trans);
1776724e2315SJosef Bacik 	btrfs_put_transaction(cur_trans);
177749b25e05SJeff Mahoney 
177849b25e05SJeff Mahoney 	trace_btrfs_transaction_commit(root);
177949b25e05SJeff Mahoney 
178049b25e05SJeff Mahoney 	if (current->journal_info == trans)
178149b25e05SJeff Mahoney 		current->journal_info = NULL;
1782c0af8f0bSWang Shilong 	btrfs_scrub_cancel(root->fs_info);
178349b25e05SJeff Mahoney 
178449b25e05SJeff Mahoney 	kmem_cache_free(btrfs_trans_handle_cachep, trans);
178549b25e05SJeff Mahoney }
178649b25e05SJeff Mahoney 
178782436617SMiao Xie static inline int btrfs_start_delalloc_flush(struct btrfs_fs_info *fs_info)
178882436617SMiao Xie {
178982436617SMiao Xie 	if (btrfs_test_opt(fs_info->tree_root, FLUSHONCOMMIT))
17906c255e67SMiao Xie 		return btrfs_start_delalloc_roots(fs_info, 1, -1);
179182436617SMiao Xie 	return 0;
179282436617SMiao Xie }
179382436617SMiao Xie 
179482436617SMiao Xie static inline void btrfs_wait_delalloc_flush(struct btrfs_fs_info *fs_info)
179582436617SMiao Xie {
179682436617SMiao Xie 	if (btrfs_test_opt(fs_info->tree_root, FLUSHONCOMMIT))
1797b0244199SMiao Xie 		btrfs_wait_ordered_roots(fs_info, -1);
179882436617SMiao Xie }
179982436617SMiao Xie 
180050d9aa99SJosef Bacik static inline void
180150d9aa99SJosef Bacik btrfs_wait_pending_ordered(struct btrfs_transaction *cur_trans,
180250d9aa99SJosef Bacik 			   struct btrfs_fs_info *fs_info)
180350d9aa99SJosef Bacik {
180450d9aa99SJosef Bacik 	struct btrfs_ordered_extent *ordered;
180550d9aa99SJosef Bacik 
180650d9aa99SJosef Bacik 	spin_lock(&fs_info->trans_lock);
180750d9aa99SJosef Bacik 	while (!list_empty(&cur_trans->pending_ordered)) {
180850d9aa99SJosef Bacik 		ordered = list_first_entry(&cur_trans->pending_ordered,
180950d9aa99SJosef Bacik 					   struct btrfs_ordered_extent,
181050d9aa99SJosef Bacik 					   trans_list);
181150d9aa99SJosef Bacik 		list_del_init(&ordered->trans_list);
181250d9aa99SJosef Bacik 		spin_unlock(&fs_info->trans_lock);
181350d9aa99SJosef Bacik 
181450d9aa99SJosef Bacik 		wait_event(ordered->wait, test_bit(BTRFS_ORDERED_COMPLETE,
181550d9aa99SJosef Bacik 						   &ordered->flags));
181650d9aa99SJosef Bacik 		btrfs_put_ordered_extent(ordered);
181750d9aa99SJosef Bacik 		spin_lock(&fs_info->trans_lock);
181850d9aa99SJosef Bacik 	}
181950d9aa99SJosef Bacik 	spin_unlock(&fs_info->trans_lock);
182050d9aa99SJosef Bacik }
182150d9aa99SJosef Bacik 
182279154b1bSChris Mason int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
182379154b1bSChris Mason 			     struct btrfs_root *root)
182479154b1bSChris Mason {
182549b25e05SJeff Mahoney 	struct btrfs_transaction *cur_trans = trans->transaction;
18268fd17795SChris Mason 	struct btrfs_transaction *prev_trans = NULL;
1827656f30dbSFilipe Manana 	struct btrfs_inode *btree_ino = BTRFS_I(root->fs_info->btree_inode);
182825287e0aSMiao Xie 	int ret;
182979154b1bSChris Mason 
18308d25a086SMiao Xie 	/* Stop the commit early if ->aborted is set */
18318d25a086SMiao Xie 	if (unlikely(ACCESS_ONCE(cur_trans->aborted))) {
183225287e0aSMiao Xie 		ret = cur_trans->aborted;
1833e4a2bcacSJosef Bacik 		btrfs_end_transaction(trans, root);
1834e4a2bcacSJosef Bacik 		return ret;
183525287e0aSMiao Xie 	}
183649b25e05SJeff Mahoney 
183756bec294SChris Mason 	/* make a pass through all the delayed refs we have so far
183856bec294SChris Mason 	 * any runnings procs may add more while we are here
183956bec294SChris Mason 	 */
184056bec294SChris Mason 	ret = btrfs_run_delayed_refs(trans, root, 0);
1841e4a2bcacSJosef Bacik 	if (ret) {
1842e4a2bcacSJosef Bacik 		btrfs_end_transaction(trans, root);
1843e4a2bcacSJosef Bacik 		return ret;
1844e4a2bcacSJosef Bacik 	}
184556bec294SChris Mason 
18460e721106SJosef Bacik 	btrfs_trans_release_metadata(trans, root);
18470e721106SJosef Bacik 	trans->block_rsv = NULL;
1848272d26d0SMiao Xie 	if (trans->qgroup_reserved) {
1849272d26d0SMiao Xie 		btrfs_qgroup_free(root, trans->qgroup_reserved);
1850272d26d0SMiao Xie 		trans->qgroup_reserved = 0;
1851272d26d0SMiao Xie 	}
18520e721106SJosef Bacik 
1853b7ec40d7SChris Mason 	cur_trans = trans->transaction;
185449b25e05SJeff Mahoney 
185556bec294SChris Mason 	/*
185656bec294SChris Mason 	 * set the flushing flag so procs in this transaction have to
185756bec294SChris Mason 	 * start sending their work down.
185856bec294SChris Mason 	 */
1859b7ec40d7SChris Mason 	cur_trans->delayed_refs.flushing = 1;
18601be41b78SJosef Bacik 	smp_wmb();
186156bec294SChris Mason 
1862ea658badSJosef Bacik 	if (!list_empty(&trans->new_bgs))
1863ea658badSJosef Bacik 		btrfs_create_pending_block_groups(trans, root);
1864ea658badSJosef Bacik 
1865c3e69d58SChris Mason 	ret = btrfs_run_delayed_refs(trans, root, 0);
1866e4a2bcacSJosef Bacik 	if (ret) {
1867e4a2bcacSJosef Bacik 		btrfs_end_transaction(trans, root);
1868e4a2bcacSJosef Bacik 		return ret;
1869e4a2bcacSJosef Bacik 	}
187056bec294SChris Mason 
18711bbc621eSChris Mason 	if (!cur_trans->dirty_bg_run) {
18721bbc621eSChris Mason 		int run_it = 0;
18731bbc621eSChris Mason 
18741bbc621eSChris Mason 		/* this mutex is also taken before trying to set
18751bbc621eSChris Mason 		 * block groups readonly.  We need to make sure
18761bbc621eSChris Mason 		 * that nobody has set a block group readonly
18771bbc621eSChris Mason 		 * after a extents from that block group have been
18781bbc621eSChris Mason 		 * allocated for cache files.  btrfs_set_block_group_ro
18791bbc621eSChris Mason 		 * will wait for the transaction to commit if it
18801bbc621eSChris Mason 		 * finds dirty_bg_run = 1
18811bbc621eSChris Mason 		 *
18821bbc621eSChris Mason 		 * The dirty_bg_run flag is also used to make sure only
18831bbc621eSChris Mason 		 * one process starts all the block group IO.  It wouldn't
18841bbc621eSChris Mason 		 * hurt to have more than one go through, but there's no
18851bbc621eSChris Mason 		 * real advantage to it either.
18861bbc621eSChris Mason 		 */
18871bbc621eSChris Mason 		mutex_lock(&root->fs_info->ro_block_group_mutex);
18881bbc621eSChris Mason 		if (!cur_trans->dirty_bg_run) {
18891bbc621eSChris Mason 			run_it = 1;
18901bbc621eSChris Mason 			cur_trans->dirty_bg_run = 1;
18911bbc621eSChris Mason 		}
18921bbc621eSChris Mason 		mutex_unlock(&root->fs_info->ro_block_group_mutex);
18931bbc621eSChris Mason 
18941bbc621eSChris Mason 		if (run_it)
18951bbc621eSChris Mason 			ret = btrfs_start_dirty_block_groups(trans, root);
18961bbc621eSChris Mason 	}
18971bbc621eSChris Mason 	if (ret) {
18981bbc621eSChris Mason 		btrfs_end_transaction(trans, root);
18991bbc621eSChris Mason 		return ret;
19001bbc621eSChris Mason 	}
19011bbc621eSChris Mason 
19024a9d8bdeSMiao Xie 	spin_lock(&root->fs_info->trans_lock);
1903d3efe084SFilipe Manana 	list_splice_init(&trans->ordered, &cur_trans->pending_ordered);
19044a9d8bdeSMiao Xie 	if (cur_trans->state >= TRANS_STATE_COMMIT_START) {
19054a9d8bdeSMiao Xie 		spin_unlock(&root->fs_info->trans_lock);
190613c5a93eSJosef Bacik 		atomic_inc(&cur_trans->use_count);
190749b25e05SJeff Mahoney 		ret = btrfs_end_transaction(trans, root);
1908ccd467d6SChris Mason 
1909b9c8300cSLi Zefan 		wait_for_commit(root, cur_trans);
191015ee9bc7SJosef Bacik 
1911b4924a0fSLiu Bo 		if (unlikely(cur_trans->aborted))
1912b4924a0fSLiu Bo 			ret = cur_trans->aborted;
1913b4924a0fSLiu Bo 
1914724e2315SJosef Bacik 		btrfs_put_transaction(cur_trans);
191515ee9bc7SJosef Bacik 
191649b25e05SJeff Mahoney 		return ret;
191779154b1bSChris Mason 	}
19184313b399SChris Mason 
19194a9d8bdeSMiao Xie 	cur_trans->state = TRANS_STATE_COMMIT_START;
1920bb9c12c9SSage Weil 	wake_up(&root->fs_info->transaction_blocked_wait);
1921bb9c12c9SSage Weil 
1922ccd467d6SChris Mason 	if (cur_trans->list.prev != &root->fs_info->trans_list) {
1923ccd467d6SChris Mason 		prev_trans = list_entry(cur_trans->list.prev,
1924ccd467d6SChris Mason 					struct btrfs_transaction, list);
19254a9d8bdeSMiao Xie 		if (prev_trans->state != TRANS_STATE_COMPLETED) {
192613c5a93eSJosef Bacik 			atomic_inc(&prev_trans->use_count);
1927a4abeea4SJosef Bacik 			spin_unlock(&root->fs_info->trans_lock);
1928ccd467d6SChris Mason 
1929ccd467d6SChris Mason 			wait_for_commit(root, prev_trans);
19301f9b8c8fSFilipe Manana 			ret = prev_trans->aborted;
1931ccd467d6SChris Mason 
1932724e2315SJosef Bacik 			btrfs_put_transaction(prev_trans);
19331f9b8c8fSFilipe Manana 			if (ret)
19341f9b8c8fSFilipe Manana 				goto cleanup_transaction;
1935a4abeea4SJosef Bacik 		} else {
1936a4abeea4SJosef Bacik 			spin_unlock(&root->fs_info->trans_lock);
1937ccd467d6SChris Mason 		}
1938a4abeea4SJosef Bacik 	} else {
1939a4abeea4SJosef Bacik 		spin_unlock(&root->fs_info->trans_lock);
1940ccd467d6SChris Mason 	}
194115ee9bc7SJosef Bacik 
19420860adfdSMiao Xie 	extwriter_counter_dec(cur_trans, trans->type);
19430860adfdSMiao Xie 
194482436617SMiao Xie 	ret = btrfs_start_delalloc_flush(root->fs_info);
194582436617SMiao Xie 	if (ret)
194682436617SMiao Xie 		goto cleanup_transaction;
194782436617SMiao Xie 
19488d875f95SChris Mason 	ret = btrfs_run_delayed_items(trans, root);
194949b25e05SJeff Mahoney 	if (ret)
195049b25e05SJeff Mahoney 		goto cleanup_transaction;
195116cdcec7SMiao Xie 
1952581227d0SMiao Xie 	wait_event(cur_trans->writer_wait,
1953581227d0SMiao Xie 		   extwriter_counter_read(cur_trans) == 0);
1954ed3b3d31SChris Mason 
1955581227d0SMiao Xie 	/* some pending stuffs might be added after the previous flush. */
19568d875f95SChris Mason 	ret = btrfs_run_delayed_items(trans, root);
1957ca469637SMiao Xie 	if (ret)
1958ca469637SMiao Xie 		goto cleanup_transaction;
1959ca469637SMiao Xie 
196082436617SMiao Xie 	btrfs_wait_delalloc_flush(root->fs_info);
1961cb7ab021SWang Shilong 
196250d9aa99SJosef Bacik 	btrfs_wait_pending_ordered(cur_trans, root->fs_info);
196350d9aa99SJosef Bacik 
1964cb7ab021SWang Shilong 	btrfs_scrub_pause(root);
1965ed0ca140SJosef Bacik 	/*
1966ed0ca140SJosef Bacik 	 * Ok now we need to make sure to block out any other joins while we
1967ed0ca140SJosef Bacik 	 * commit the transaction.  We could have started a join before setting
19684a9d8bdeSMiao Xie 	 * COMMIT_DOING so make sure to wait for num_writers to == 1 again.
1969ed0ca140SJosef Bacik 	 */
1970a4abeea4SJosef Bacik 	spin_lock(&root->fs_info->trans_lock);
19714a9d8bdeSMiao Xie 	cur_trans->state = TRANS_STATE_COMMIT_DOING;
1972a4abeea4SJosef Bacik 	spin_unlock(&root->fs_info->trans_lock);
1973ed0ca140SJosef Bacik 	wait_event(cur_trans->writer_wait,
1974ed0ca140SJosef Bacik 		   atomic_read(&cur_trans->num_writers) == 1);
197515ee9bc7SJosef Bacik 
19762cba30f1SMiao Xie 	/* ->aborted might be set after the previous check, so check it */
19772cba30f1SMiao Xie 	if (unlikely(ACCESS_ONCE(cur_trans->aborted))) {
19782cba30f1SMiao Xie 		ret = cur_trans->aborted;
19796cf7f77eSWang Shilong 		goto scrub_continue;
19802cba30f1SMiao Xie 	}
19817585717fSChris Mason 	/*
19827585717fSChris Mason 	 * the reloc mutex makes sure that we stop
19837585717fSChris Mason 	 * the balancing code from coming in and moving
19847585717fSChris Mason 	 * extents around in the middle of the commit
19857585717fSChris Mason 	 */
19867585717fSChris Mason 	mutex_lock(&root->fs_info->reloc_mutex);
19877585717fSChris Mason 
198842874b3dSMiao Xie 	/*
198942874b3dSMiao Xie 	 * We needn't worry about the delayed items because we will
199042874b3dSMiao Xie 	 * deal with them in create_pending_snapshot(), which is the
199142874b3dSMiao Xie 	 * core function of the snapshot creation.
199242874b3dSMiao Xie 	 */
199342874b3dSMiao Xie 	ret = create_pending_snapshots(trans, root->fs_info);
199449b25e05SJeff Mahoney 	if (ret) {
199549b25e05SJeff Mahoney 		mutex_unlock(&root->fs_info->reloc_mutex);
19966cf7f77eSWang Shilong 		goto scrub_continue;
199749b25e05SJeff Mahoney 	}
19983063d29fSChris Mason 
199942874b3dSMiao Xie 	/*
200042874b3dSMiao Xie 	 * We insert the dir indexes of the snapshots and update the inode
200142874b3dSMiao Xie 	 * of the snapshots' parents after the snapshot creation, so there
200242874b3dSMiao Xie 	 * are some delayed items which are not dealt with. Now deal with
200342874b3dSMiao Xie 	 * them.
200442874b3dSMiao Xie 	 *
200542874b3dSMiao Xie 	 * We needn't worry that this operation will corrupt the snapshots,
200642874b3dSMiao Xie 	 * because all the tree which are snapshoted will be forced to COW
200742874b3dSMiao Xie 	 * the nodes and leaves.
200842874b3dSMiao Xie 	 */
200942874b3dSMiao Xie 	ret = btrfs_run_delayed_items(trans, root);
201049b25e05SJeff Mahoney 	if (ret) {
201149b25e05SJeff Mahoney 		mutex_unlock(&root->fs_info->reloc_mutex);
20126cf7f77eSWang Shilong 		goto scrub_continue;
201349b25e05SJeff Mahoney 	}
201416cdcec7SMiao Xie 
201556bec294SChris Mason 	ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
201649b25e05SJeff Mahoney 	if (ret) {
201749b25e05SJeff Mahoney 		mutex_unlock(&root->fs_info->reloc_mutex);
20186cf7f77eSWang Shilong 		goto scrub_continue;
201949b25e05SJeff Mahoney 	}
202056bec294SChris Mason 
20210ed4792aSQu Wenruo 	/* Reocrd old roots for later qgroup accounting */
20220ed4792aSQu Wenruo 	ret = btrfs_qgroup_prepare_account_extents(trans, root->fs_info);
20230ed4792aSQu Wenruo 	if (ret) {
20240ed4792aSQu Wenruo 		mutex_unlock(&root->fs_info->reloc_mutex);
20250ed4792aSQu Wenruo 		goto scrub_continue;
20260ed4792aSQu Wenruo 	}
20270ed4792aSQu Wenruo 
2028e999376fSChris Mason 	/*
2029e999376fSChris Mason 	 * make sure none of the code above managed to slip in a
2030e999376fSChris Mason 	 * delayed item
2031e999376fSChris Mason 	 */
2032e999376fSChris Mason 	btrfs_assert_delayed_root_empty(root);
2033e999376fSChris Mason 
20342c90e5d6SChris Mason 	WARN_ON(cur_trans != trans->transaction);
2035dc17ff8fSChris Mason 
2036e02119d5SChris Mason 	/* btrfs_commit_tree_roots is responsible for getting the
2037e02119d5SChris Mason 	 * various roots consistent with each other.  Every pointer
2038e02119d5SChris Mason 	 * in the tree of tree roots has to point to the most up to date
2039e02119d5SChris Mason 	 * root for every subvolume and other tree.  So, we have to keep
2040e02119d5SChris Mason 	 * the tree logging code from jumping in and changing any
2041e02119d5SChris Mason 	 * of the trees.
2042e02119d5SChris Mason 	 *
2043e02119d5SChris Mason 	 * At this point in the commit, there can't be any tree-log
2044e02119d5SChris Mason 	 * writers, but a little lower down we drop the trans mutex
2045e02119d5SChris Mason 	 * and let new people in.  By holding the tree_log_mutex
2046e02119d5SChris Mason 	 * from now until after the super is written, we avoid races
2047e02119d5SChris Mason 	 * with the tree-log code.
2048e02119d5SChris Mason 	 */
2049e02119d5SChris Mason 	mutex_lock(&root->fs_info->tree_log_mutex);
20501a40e23bSZheng Yan 
20515d4f98a2SYan Zheng 	ret = commit_fs_roots(trans, root);
205249b25e05SJeff Mahoney 	if (ret) {
205349b25e05SJeff Mahoney 		mutex_unlock(&root->fs_info->tree_log_mutex);
2054871383beSDavid Sterba 		mutex_unlock(&root->fs_info->reloc_mutex);
20556cf7f77eSWang Shilong 		goto scrub_continue;
205649b25e05SJeff Mahoney 	}
205754aa1f4dSChris Mason 
20583818aea2SQu Wenruo 	/*
20597e1876acSDavid Sterba 	 * Since the transaction is done, we can apply the pending changes
20607e1876acSDavid Sterba 	 * before the next transaction.
20613818aea2SQu Wenruo 	 */
2062572d9ab7SDavid Sterba 	btrfs_apply_pending_changes(root->fs_info);
20633818aea2SQu Wenruo 
20645d4f98a2SYan Zheng 	/* commit_fs_roots gets rid of all the tree log roots, it is now
2065e02119d5SChris Mason 	 * safe to free the root of tree log roots
2066e02119d5SChris Mason 	 */
2067e02119d5SChris Mason 	btrfs_free_log_root_tree(trans, root->fs_info);
2068e02119d5SChris Mason 
20690ed4792aSQu Wenruo 	/*
20700ed4792aSQu Wenruo 	 * Since fs roots are all committed, we can get a quite accurate
20710ed4792aSQu Wenruo 	 * new_roots. So let's do quota accounting.
20720ed4792aSQu Wenruo 	 */
20730ed4792aSQu Wenruo 	ret = btrfs_qgroup_account_extents(trans, root->fs_info);
20740ed4792aSQu Wenruo 	if (ret < 0) {
20750ed4792aSQu Wenruo 		mutex_unlock(&root->fs_info->tree_log_mutex);
20760ed4792aSQu Wenruo 		mutex_unlock(&root->fs_info->reloc_mutex);
20770ed4792aSQu Wenruo 		goto scrub_continue;
20780ed4792aSQu Wenruo 	}
20790ed4792aSQu Wenruo 
20805d4f98a2SYan Zheng 	ret = commit_cowonly_roots(trans, root);
208149b25e05SJeff Mahoney 	if (ret) {
208249b25e05SJeff Mahoney 		mutex_unlock(&root->fs_info->tree_log_mutex);
2083871383beSDavid Sterba 		mutex_unlock(&root->fs_info->reloc_mutex);
20846cf7f77eSWang Shilong 		goto scrub_continue;
208549b25e05SJeff Mahoney 	}
208654aa1f4dSChris Mason 
20872cba30f1SMiao Xie 	/*
20882cba30f1SMiao Xie 	 * The tasks which save the space cache and inode cache may also
20892cba30f1SMiao Xie 	 * update ->aborted, check it.
20902cba30f1SMiao Xie 	 */
20912cba30f1SMiao Xie 	if (unlikely(ACCESS_ONCE(cur_trans->aborted))) {
20922cba30f1SMiao Xie 		ret = cur_trans->aborted;
20932cba30f1SMiao Xie 		mutex_unlock(&root->fs_info->tree_log_mutex);
20942cba30f1SMiao Xie 		mutex_unlock(&root->fs_info->reloc_mutex);
20956cf7f77eSWang Shilong 		goto scrub_continue;
20962cba30f1SMiao Xie 	}
20972cba30f1SMiao Xie 
209811833d66SYan Zheng 	btrfs_prepare_extent_commit(trans, root);
209911833d66SYan Zheng 
210078fae27eSChris Mason 	cur_trans = root->fs_info->running_transaction;
21015f39d397SChris Mason 
21025d4f98a2SYan Zheng 	btrfs_set_root_node(&root->fs_info->tree_root->root_item,
21035d4f98a2SYan Zheng 			    root->fs_info->tree_root->node);
21049e351cc8SJosef Bacik 	list_add_tail(&root->fs_info->tree_root->dirty_list,
21059e351cc8SJosef Bacik 		      &cur_trans->switch_commits);
21065d4f98a2SYan Zheng 
21075d4f98a2SYan Zheng 	btrfs_set_root_node(&root->fs_info->chunk_root->root_item,
21085d4f98a2SYan Zheng 			    root->fs_info->chunk_root->node);
21099e351cc8SJosef Bacik 	list_add_tail(&root->fs_info->chunk_root->dirty_list,
21109e351cc8SJosef Bacik 		      &cur_trans->switch_commits);
21119e351cc8SJosef Bacik 
21129e351cc8SJosef Bacik 	switch_commit_roots(cur_trans, root->fs_info);
21135d4f98a2SYan Zheng 
2114edf39272SJan Schmidt 	assert_qgroups_uptodate(trans);
2115ce93ec54SJosef Bacik 	ASSERT(list_empty(&cur_trans->dirty_bgs));
21161bbc621eSChris Mason 	ASSERT(list_empty(&cur_trans->io_bgs));
21175d4f98a2SYan Zheng 	update_super_roots(root);
2118e02119d5SChris Mason 
21196c41761fSDavid Sterba 	btrfs_set_super_log_root(root->fs_info->super_copy, 0);
21206c41761fSDavid Sterba 	btrfs_set_super_log_root_level(root->fs_info->super_copy, 0);
21216c41761fSDavid Sterba 	memcpy(root->fs_info->super_for_commit, root->fs_info->super_copy,
21226c41761fSDavid Sterba 	       sizeof(*root->fs_info->super_copy));
2123ccd467d6SChris Mason 
2124935e5cc9SMiao Xie 	btrfs_update_commit_device_size(root->fs_info);
2125ce7213c7SMiao Xie 	btrfs_update_commit_device_bytes_used(root, cur_trans);
2126935e5cc9SMiao Xie 
2127656f30dbSFilipe Manana 	clear_bit(BTRFS_INODE_BTREE_LOG1_ERR, &btree_ino->runtime_flags);
2128656f30dbSFilipe Manana 	clear_bit(BTRFS_INODE_BTREE_LOG2_ERR, &btree_ino->runtime_flags);
2129656f30dbSFilipe Manana 
21304fbcdf66SFilipe Manana 	btrfs_trans_release_chunk_metadata(trans);
21314fbcdf66SFilipe Manana 
2132a4abeea4SJosef Bacik 	spin_lock(&root->fs_info->trans_lock);
21334a9d8bdeSMiao Xie 	cur_trans->state = TRANS_STATE_UNBLOCKED;
2134a4abeea4SJosef Bacik 	root->fs_info->running_transaction = NULL;
2135a4abeea4SJosef Bacik 	spin_unlock(&root->fs_info->trans_lock);
21367585717fSChris Mason 	mutex_unlock(&root->fs_info->reloc_mutex);
2137b7ec40d7SChris Mason 
2138f9295749SChris Mason 	wake_up(&root->fs_info->transaction_wait);
2139e6dcd2dcSChris Mason 
214079154b1bSChris Mason 	ret = btrfs_write_and_wait_transaction(trans, root);
214149b25e05SJeff Mahoney 	if (ret) {
214249b25e05SJeff Mahoney 		btrfs_error(root->fs_info, ret,
214308748810SDavid Sterba 			    "Error while writing out transaction");
214449b25e05SJeff Mahoney 		mutex_unlock(&root->fs_info->tree_log_mutex);
21456cf7f77eSWang Shilong 		goto scrub_continue;
214649b25e05SJeff Mahoney 	}
214749b25e05SJeff Mahoney 
214849b25e05SJeff Mahoney 	ret = write_ctree_super(trans, root, 0);
214949b25e05SJeff Mahoney 	if (ret) {
215049b25e05SJeff Mahoney 		mutex_unlock(&root->fs_info->tree_log_mutex);
21516cf7f77eSWang Shilong 		goto scrub_continue;
215249b25e05SJeff Mahoney 	}
21534313b399SChris Mason 
2154e02119d5SChris Mason 	/*
2155e02119d5SChris Mason 	 * the super is written, we can safely allow the tree-loggers
2156e02119d5SChris Mason 	 * to go about their business
2157e02119d5SChris Mason 	 */
2158e02119d5SChris Mason 	mutex_unlock(&root->fs_info->tree_log_mutex);
2159e02119d5SChris Mason 
216011833d66SYan Zheng 	btrfs_finish_extent_commit(trans, root);
21614313b399SChris Mason 
216213212b54SZhao Lei 	if (cur_trans->have_free_bgs)
216313212b54SZhao Lei 		btrfs_clear_space_info_full(root->fs_info);
216413212b54SZhao Lei 
216515ee9bc7SJosef Bacik 	root->fs_info->last_trans_committed = cur_trans->transid;
21664a9d8bdeSMiao Xie 	/*
21674a9d8bdeSMiao Xie 	 * We needn't acquire the lock here because there is no other task
21684a9d8bdeSMiao Xie 	 * which can change it.
21694a9d8bdeSMiao Xie 	 */
21704a9d8bdeSMiao Xie 	cur_trans->state = TRANS_STATE_COMPLETED;
21712c90e5d6SChris Mason 	wake_up(&cur_trans->commit_wait);
21723de4586cSChris Mason 
2173a4abeea4SJosef Bacik 	spin_lock(&root->fs_info->trans_lock);
217413c5a93eSJosef Bacik 	list_del_init(&cur_trans->list);
2175a4abeea4SJosef Bacik 	spin_unlock(&root->fs_info->trans_lock);
2176a4abeea4SJosef Bacik 
2177724e2315SJosef Bacik 	btrfs_put_transaction(cur_trans);
2178724e2315SJosef Bacik 	btrfs_put_transaction(cur_trans);
217958176a96SJosef Bacik 
21800860adfdSMiao Xie 	if (trans->type & __TRANS_FREEZABLE)
2181b2b5ef5cSJan Kara 		sb_end_intwrite(root->fs_info->sb);
2182b2b5ef5cSJan Kara 
21831abe9b8aSliubo 	trace_btrfs_transaction_commit(root);
21841abe9b8aSliubo 
2185a2de733cSArne Jansen 	btrfs_scrub_continue(root);
2186a2de733cSArne Jansen 
21879ed74f2dSJosef Bacik 	if (current->journal_info == trans)
21889ed74f2dSJosef Bacik 		current->journal_info = NULL;
21899ed74f2dSJosef Bacik 
21902c90e5d6SChris Mason 	kmem_cache_free(btrfs_trans_handle_cachep, trans);
219124bbcf04SYan, Zheng 
21928a733013SZhao Lei 	if (current != root->fs_info->transaction_kthread &&
21938a733013SZhao Lei 	    current != root->fs_info->cleaner_kthread)
219424bbcf04SYan, Zheng 		btrfs_run_delayed_iputs(root);
219524bbcf04SYan, Zheng 
219679154b1bSChris Mason 	return ret;
219749b25e05SJeff Mahoney 
21986cf7f77eSWang Shilong scrub_continue:
21996cf7f77eSWang Shilong 	btrfs_scrub_continue(root);
220049b25e05SJeff Mahoney cleanup_transaction:
22010e721106SJosef Bacik 	btrfs_trans_release_metadata(trans, root);
22024fbcdf66SFilipe Manana 	btrfs_trans_release_chunk_metadata(trans);
22030e721106SJosef Bacik 	trans->block_rsv = NULL;
2204272d26d0SMiao Xie 	if (trans->qgroup_reserved) {
2205272d26d0SMiao Xie 		btrfs_qgroup_free(root, trans->qgroup_reserved);
2206272d26d0SMiao Xie 		trans->qgroup_reserved = 0;
2207272d26d0SMiao Xie 	}
2208c2cf52ebSSimon Kirby 	btrfs_warn(root->fs_info, "Skipping commit of aborted transaction.");
220949b25e05SJeff Mahoney 	if (current->journal_info == trans)
221049b25e05SJeff Mahoney 		current->journal_info = NULL;
22117b8b92afSJosef Bacik 	cleanup_transaction(trans, root, ret);
221249b25e05SJeff Mahoney 
221349b25e05SJeff Mahoney 	return ret;
221479154b1bSChris Mason }
221579154b1bSChris Mason 
2216d352ac68SChris Mason /*
22179d1a2a3aSDavid Sterba  * return < 0 if error
22189d1a2a3aSDavid Sterba  * 0 if there are no more dead_roots at the time of call
22199d1a2a3aSDavid Sterba  * 1 there are more to be processed, call me again
22209d1a2a3aSDavid Sterba  *
22219d1a2a3aSDavid Sterba  * The return value indicates there are certainly more snapshots to delete, but
22229d1a2a3aSDavid Sterba  * if there comes a new one during processing, it may return 0. We don't mind,
22239d1a2a3aSDavid Sterba  * because btrfs_commit_super will poke cleaner thread and it will process it a
22249d1a2a3aSDavid Sterba  * few seconds later.
2225d352ac68SChris Mason  */
22269d1a2a3aSDavid Sterba int btrfs_clean_one_deleted_snapshot(struct btrfs_root *root)
2227e9d0b13bSChris Mason {
22289d1a2a3aSDavid Sterba 	int ret;
22295d4f98a2SYan Zheng 	struct btrfs_fs_info *fs_info = root->fs_info;
2230e9d0b13bSChris Mason 
2231a4abeea4SJosef Bacik 	spin_lock(&fs_info->trans_lock);
22329d1a2a3aSDavid Sterba 	if (list_empty(&fs_info->dead_roots)) {
22339d1a2a3aSDavid Sterba 		spin_unlock(&fs_info->trans_lock);
22349d1a2a3aSDavid Sterba 		return 0;
22359d1a2a3aSDavid Sterba 	}
22369d1a2a3aSDavid Sterba 	root = list_first_entry(&fs_info->dead_roots,
22379d1a2a3aSDavid Sterba 			struct btrfs_root, root_list);
2238cfad392bSJosef Bacik 	list_del_init(&root->root_list);
2239a4abeea4SJosef Bacik 	spin_unlock(&fs_info->trans_lock);
22405d4f98a2SYan Zheng 
2241efe120a0SFrank Holton 	pr_debug("BTRFS: cleaner removing %llu\n", root->objectid);
224276dda93cSYan, Zheng 
224316cdcec7SMiao Xie 	btrfs_kill_all_delayed_nodes(root);
224416cdcec7SMiao Xie 
224576dda93cSYan, Zheng 	if (btrfs_header_backref_rev(root->node) <
224676dda93cSYan, Zheng 			BTRFS_MIXED_BACKREF_REV)
22472c536799SJeff Mahoney 		ret = btrfs_drop_snapshot(root, NULL, 0, 0);
224876dda93cSYan, Zheng 	else
22492c536799SJeff Mahoney 		ret = btrfs_drop_snapshot(root, NULL, 1, 0);
225032471dc2SDavid Sterba 
22516596a928SJosef Bacik 	return (ret < 0) ? 0 : 1;
2252e9d0b13bSChris Mason }
2253572d9ab7SDavid Sterba 
2254572d9ab7SDavid Sterba void btrfs_apply_pending_changes(struct btrfs_fs_info *fs_info)
2255572d9ab7SDavid Sterba {
2256572d9ab7SDavid Sterba 	unsigned long prev;
2257572d9ab7SDavid Sterba 	unsigned long bit;
2258572d9ab7SDavid Sterba 
22596c9fe14fSQu Wenruo 	prev = xchg(&fs_info->pending_changes, 0);
2260572d9ab7SDavid Sterba 	if (!prev)
2261572d9ab7SDavid Sterba 		return;
2262572d9ab7SDavid Sterba 
22637e1876acSDavid Sterba 	bit = 1 << BTRFS_PENDING_SET_INODE_MAP_CACHE;
22647e1876acSDavid Sterba 	if (prev & bit)
22657e1876acSDavid Sterba 		btrfs_set_opt(fs_info->mount_opt, INODE_MAP_CACHE);
22667e1876acSDavid Sterba 	prev &= ~bit;
22677e1876acSDavid Sterba 
22687e1876acSDavid Sterba 	bit = 1 << BTRFS_PENDING_CLEAR_INODE_MAP_CACHE;
22697e1876acSDavid Sterba 	if (prev & bit)
22707e1876acSDavid Sterba 		btrfs_clear_opt(fs_info->mount_opt, INODE_MAP_CACHE);
22717e1876acSDavid Sterba 	prev &= ~bit;
22727e1876acSDavid Sterba 
2273d51033d0SDavid Sterba 	bit = 1 << BTRFS_PENDING_COMMIT;
2274d51033d0SDavid Sterba 	if (prev & bit)
2275d51033d0SDavid Sterba 		btrfs_debug(fs_info, "pending commit done");
2276d51033d0SDavid Sterba 	prev &= ~bit;
2277d51033d0SDavid Sterba 
2278572d9ab7SDavid Sterba 	if (prev)
2279572d9ab7SDavid Sterba 		btrfs_warn(fs_info,
2280572d9ab7SDavid Sterba 			"unknown pending changes left 0x%lx, ignoring", prev);
2281572d9ab7SDavid Sterba }
2282