xref: /openbmc/linux/fs/btrfs/transaction.c (revision a83342aa)
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);
85b666a9cdSDavid Sterba 	/*
86b666a9cdSDavid Sterba 	 * Do a single barrier for the waitqueue_active check here, the state
87b666a9cdSDavid Sterba 	 * of the waitqueue should not change once clear_btree_io_tree is
88b666a9cdSDavid Sterba 	 * called.
89b666a9cdSDavid Sterba 	 */
90b666a9cdSDavid Sterba 	smp_mb();
91663dfbb0SFilipe Manana 	while (!RB_EMPTY_ROOT(&tree->state)) {
92663dfbb0SFilipe Manana 		struct rb_node *node;
93663dfbb0SFilipe Manana 		struct extent_state *state;
94663dfbb0SFilipe Manana 
95663dfbb0SFilipe Manana 		node = rb_first(&tree->state);
96663dfbb0SFilipe Manana 		state = rb_entry(node, struct extent_state, rb_node);
97663dfbb0SFilipe Manana 		rb_erase(&state->rb_node, &tree->state);
98663dfbb0SFilipe Manana 		RB_CLEAR_NODE(&state->rb_node);
99663dfbb0SFilipe Manana 		/*
100663dfbb0SFilipe Manana 		 * btree io trees aren't supposed to have tasks waiting for
101663dfbb0SFilipe Manana 		 * changes in the flags of extent states ever.
102663dfbb0SFilipe Manana 		 */
103663dfbb0SFilipe Manana 		ASSERT(!waitqueue_active(&state->wq));
104663dfbb0SFilipe Manana 		free_extent_state(state);
105351810c1SDavid Sterba 
106351810c1SDavid Sterba 		cond_resched_lock(&tree->lock);
107663dfbb0SFilipe Manana 	}
108663dfbb0SFilipe Manana 	spin_unlock(&tree->lock);
109663dfbb0SFilipe Manana }
110663dfbb0SFilipe Manana 
1119e351cc8SJosef Bacik static noinline void switch_commit_roots(struct btrfs_transaction *trans,
1129e351cc8SJosef Bacik 					 struct btrfs_fs_info *fs_info)
113817d52f8SJosef Bacik {
1149e351cc8SJosef Bacik 	struct btrfs_root *root, *tmp;
1159e351cc8SJosef Bacik 
1169e351cc8SJosef Bacik 	down_write(&fs_info->commit_root_sem);
1179e351cc8SJosef Bacik 	list_for_each_entry_safe(root, tmp, &trans->switch_commits,
1189e351cc8SJosef Bacik 				 dirty_list) {
1199e351cc8SJosef Bacik 		list_del_init(&root->dirty_list);
120817d52f8SJosef Bacik 		free_extent_buffer(root->commit_root);
121817d52f8SJosef Bacik 		root->commit_root = btrfs_root_node(root);
1229e351cc8SJosef Bacik 		if (is_fstree(root->objectid))
1239e351cc8SJosef Bacik 			btrfs_unpin_free_ino(root);
124663dfbb0SFilipe Manana 		clear_btree_io_tree(&root->dirty_log_pages);
1259e351cc8SJosef Bacik 	}
1262b9dbef2SJosef Bacik 
1272b9dbef2SJosef Bacik 	/* We can free old roots now. */
1282b9dbef2SJosef Bacik 	spin_lock(&trans->dropped_roots_lock);
1292b9dbef2SJosef Bacik 	while (!list_empty(&trans->dropped_roots)) {
1302b9dbef2SJosef Bacik 		root = list_first_entry(&trans->dropped_roots,
1312b9dbef2SJosef Bacik 					struct btrfs_root, root_list);
1322b9dbef2SJosef Bacik 		list_del_init(&root->root_list);
1332b9dbef2SJosef Bacik 		spin_unlock(&trans->dropped_roots_lock);
1342b9dbef2SJosef Bacik 		btrfs_drop_and_free_fs_root(fs_info, root);
1352b9dbef2SJosef Bacik 		spin_lock(&trans->dropped_roots_lock);
1362b9dbef2SJosef Bacik 	}
1372b9dbef2SJosef Bacik 	spin_unlock(&trans->dropped_roots_lock);
1389e351cc8SJosef Bacik 	up_write(&fs_info->commit_root_sem);
139817d52f8SJosef Bacik }
140817d52f8SJosef Bacik 
1410860adfdSMiao Xie static inline void extwriter_counter_inc(struct btrfs_transaction *trans,
1420860adfdSMiao Xie 					 unsigned int type)
1430860adfdSMiao Xie {
1440860adfdSMiao Xie 	if (type & TRANS_EXTWRITERS)
1450860adfdSMiao Xie 		atomic_inc(&trans->num_extwriters);
1460860adfdSMiao Xie }
1470860adfdSMiao Xie 
1480860adfdSMiao Xie static inline void extwriter_counter_dec(struct btrfs_transaction *trans,
1490860adfdSMiao Xie 					 unsigned int type)
1500860adfdSMiao Xie {
1510860adfdSMiao Xie 	if (type & TRANS_EXTWRITERS)
1520860adfdSMiao Xie 		atomic_dec(&trans->num_extwriters);
1530860adfdSMiao Xie }
1540860adfdSMiao Xie 
1550860adfdSMiao Xie static inline void extwriter_counter_init(struct btrfs_transaction *trans,
1560860adfdSMiao Xie 					  unsigned int type)
1570860adfdSMiao Xie {
1580860adfdSMiao Xie 	atomic_set(&trans->num_extwriters, ((type & TRANS_EXTWRITERS) ? 1 : 0));
1590860adfdSMiao Xie }
1600860adfdSMiao Xie 
1610860adfdSMiao Xie static inline int extwriter_counter_read(struct btrfs_transaction *trans)
1620860adfdSMiao Xie {
1630860adfdSMiao Xie 	return atomic_read(&trans->num_extwriters);
164178260b2SMiao Xie }
165178260b2SMiao Xie 
166d352ac68SChris Mason /*
167d352ac68SChris Mason  * either allocate a new transaction or hop into the existing one
168d352ac68SChris Mason  */
1690860adfdSMiao Xie static noinline int join_transaction(struct btrfs_root *root, unsigned int type)
17079154b1bSChris Mason {
17179154b1bSChris Mason 	struct btrfs_transaction *cur_trans;
17219ae4e81SJan Schmidt 	struct btrfs_fs_info *fs_info = root->fs_info;
173a4abeea4SJosef Bacik 
17419ae4e81SJan Schmidt 	spin_lock(&fs_info->trans_lock);
175d43317dcSChris Mason loop:
17649b25e05SJeff Mahoney 	/* The file system has been taken offline. No new transactions. */
17787533c47SMiao Xie 	if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) {
17819ae4e81SJan Schmidt 		spin_unlock(&fs_info->trans_lock);
17949b25e05SJeff Mahoney 		return -EROFS;
18049b25e05SJeff Mahoney 	}
18149b25e05SJeff Mahoney 
18219ae4e81SJan Schmidt 	cur_trans = fs_info->running_transaction;
183a4abeea4SJosef Bacik 	if (cur_trans) {
184871383beSDavid Sterba 		if (cur_trans->aborted) {
18519ae4e81SJan Schmidt 			spin_unlock(&fs_info->trans_lock);
18649b25e05SJeff Mahoney 			return cur_trans->aborted;
187871383beSDavid Sterba 		}
1884a9d8bdeSMiao Xie 		if (btrfs_blocked_trans_types[cur_trans->state] & type) {
189178260b2SMiao Xie 			spin_unlock(&fs_info->trans_lock);
190178260b2SMiao Xie 			return -EBUSY;
191178260b2SMiao Xie 		}
192a4abeea4SJosef Bacik 		atomic_inc(&cur_trans->use_count);
193a4abeea4SJosef Bacik 		atomic_inc(&cur_trans->num_writers);
1940860adfdSMiao Xie 		extwriter_counter_inc(cur_trans, type);
19519ae4e81SJan Schmidt 		spin_unlock(&fs_info->trans_lock);
196a4abeea4SJosef Bacik 		return 0;
197a4abeea4SJosef Bacik 	}
19819ae4e81SJan Schmidt 	spin_unlock(&fs_info->trans_lock);
199a4abeea4SJosef Bacik 
200354aa0fbSMiao Xie 	/*
201354aa0fbSMiao Xie 	 * If we are ATTACH, we just want to catch the current transaction,
202354aa0fbSMiao Xie 	 * and commit it. If there is no transaction, just return ENOENT.
203354aa0fbSMiao Xie 	 */
204354aa0fbSMiao Xie 	if (type == TRANS_ATTACH)
205354aa0fbSMiao Xie 		return -ENOENT;
206354aa0fbSMiao Xie 
2074a9d8bdeSMiao Xie 	/*
2084a9d8bdeSMiao Xie 	 * JOIN_NOLOCK only happens during the transaction commit, so
2094a9d8bdeSMiao Xie 	 * it is impossible that ->running_transaction is NULL
2104a9d8bdeSMiao Xie 	 */
2114a9d8bdeSMiao Xie 	BUG_ON(type == TRANS_JOIN_NOLOCK);
2124a9d8bdeSMiao Xie 
213a4abeea4SJosef Bacik 	cur_trans = kmem_cache_alloc(btrfs_transaction_cachep, GFP_NOFS);
214db5b493aSTsutomu Itoh 	if (!cur_trans)
215db5b493aSTsutomu Itoh 		return -ENOMEM;
216d43317dcSChris Mason 
21719ae4e81SJan Schmidt 	spin_lock(&fs_info->trans_lock);
21819ae4e81SJan Schmidt 	if (fs_info->running_transaction) {
219d43317dcSChris Mason 		/*
220d43317dcSChris Mason 		 * someone started a transaction after we unlocked.  Make sure
2214a9d8bdeSMiao Xie 		 * to redo the checks above
222d43317dcSChris Mason 		 */
223a4abeea4SJosef Bacik 		kmem_cache_free(btrfs_transaction_cachep, cur_trans);
224d43317dcSChris Mason 		goto loop;
22587533c47SMiao Xie 	} else if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) {
226e4b50e14SDan Carpenter 		spin_unlock(&fs_info->trans_lock);
2277b8b92afSJosef Bacik 		kmem_cache_free(btrfs_transaction_cachep, cur_trans);
2287b8b92afSJosef Bacik 		return -EROFS;
229a4abeea4SJosef Bacik 	}
230d43317dcSChris Mason 
23113c5a93eSJosef Bacik 	atomic_set(&cur_trans->num_writers, 1);
2320860adfdSMiao Xie 	extwriter_counter_init(cur_trans, type);
23379154b1bSChris Mason 	init_waitqueue_head(&cur_trans->writer_wait);
23479154b1bSChris Mason 	init_waitqueue_head(&cur_trans->commit_wait);
2354a9d8bdeSMiao Xie 	cur_trans->state = TRANS_STATE_RUNNING;
236a4abeea4SJosef Bacik 	/*
237a4abeea4SJosef Bacik 	 * One for this trans handle, one so it will live on until we
238a4abeea4SJosef Bacik 	 * commit the transaction.
239a4abeea4SJosef Bacik 	 */
240a4abeea4SJosef Bacik 	atomic_set(&cur_trans->use_count, 2);
24113212b54SZhao Lei 	cur_trans->have_free_bgs = 0;
24208607c1bSChris Mason 	cur_trans->start_time = get_seconds();
2431bbc621eSChris Mason 	cur_trans->dirty_bg_run = 0;
24456bec294SChris Mason 
245c46effa6SLiu Bo 	cur_trans->delayed_refs.href_root = RB_ROOT;
2463368d001SQu Wenruo 	cur_trans->delayed_refs.dirty_extent_root = RB_ROOT;
247d7df2c79SJosef Bacik 	atomic_set(&cur_trans->delayed_refs.num_entries, 0);
248c3e69d58SChris Mason 	cur_trans->delayed_refs.num_heads_ready = 0;
2491262133bSJosef Bacik 	cur_trans->delayed_refs.pending_csums = 0;
250c3e69d58SChris Mason 	cur_trans->delayed_refs.num_heads = 0;
25156bec294SChris Mason 	cur_trans->delayed_refs.flushing = 0;
252c3e69d58SChris Mason 	cur_trans->delayed_refs.run_delayed_start = 0;
2539086db86SQu Wenruo 	cur_trans->delayed_refs.qgroup_to_skip = 0;
25420b297d6SJan Schmidt 
25520b297d6SJan Schmidt 	/*
25620b297d6SJan Schmidt 	 * although the tree mod log is per file system and not per transaction,
25720b297d6SJan Schmidt 	 * the log must never go across transaction boundaries.
25820b297d6SJan Schmidt 	 */
25920b297d6SJan Schmidt 	smp_mb();
26031b1a2bdSJulia Lawall 	if (!list_empty(&fs_info->tree_mod_seq_list))
261efe120a0SFrank Holton 		WARN(1, KERN_ERR "BTRFS: tree_mod_seq_list not empty when "
26220b297d6SJan Schmidt 			"creating a fresh transaction\n");
26331b1a2bdSJulia Lawall 	if (!RB_EMPTY_ROOT(&fs_info->tree_mod_log))
264efe120a0SFrank Holton 		WARN(1, KERN_ERR "BTRFS: tree_mod_log rb tree not empty when "
26520b297d6SJan Schmidt 			"creating a fresh transaction\n");
266fc36ed7eSJan Schmidt 	atomic64_set(&fs_info->tree_mod_seq, 0);
26720b297d6SJan Schmidt 
26856bec294SChris Mason 	spin_lock_init(&cur_trans->delayed_refs.lock);
26956bec294SChris Mason 
2703063d29fSChris Mason 	INIT_LIST_HEAD(&cur_trans->pending_snapshots);
2716df9a95eSJosef Bacik 	INIT_LIST_HEAD(&cur_trans->pending_chunks);
2729e351cc8SJosef Bacik 	INIT_LIST_HEAD(&cur_trans->switch_commits);
27350d9aa99SJosef Bacik 	INIT_LIST_HEAD(&cur_trans->pending_ordered);
274ce93ec54SJosef Bacik 	INIT_LIST_HEAD(&cur_trans->dirty_bgs);
2751bbc621eSChris Mason 	INIT_LIST_HEAD(&cur_trans->io_bgs);
2762b9dbef2SJosef Bacik 	INIT_LIST_HEAD(&cur_trans->dropped_roots);
2771bbc621eSChris Mason 	mutex_init(&cur_trans->cache_write_mutex);
278cb723e49SJosef Bacik 	cur_trans->num_dirty_bgs = 0;
279ce93ec54SJosef Bacik 	spin_lock_init(&cur_trans->dirty_bgs_lock);
280e33e17eeSJeff Mahoney 	INIT_LIST_HEAD(&cur_trans->deleted_bgs);
281e33e17eeSJeff Mahoney 	spin_lock_init(&cur_trans->deleted_bgs_lock);
2822b9dbef2SJosef Bacik 	spin_lock_init(&cur_trans->dropped_roots_lock);
28319ae4e81SJan Schmidt 	list_add_tail(&cur_trans->list, &fs_info->trans_list);
284d1310b2eSChris Mason 	extent_io_tree_init(&cur_trans->dirty_pages,
28519ae4e81SJan Schmidt 			     fs_info->btree_inode->i_mapping);
28619ae4e81SJan Schmidt 	fs_info->generation++;
28719ae4e81SJan Schmidt 	cur_trans->transid = fs_info->generation;
28819ae4e81SJan Schmidt 	fs_info->running_transaction = cur_trans;
28949b25e05SJeff Mahoney 	cur_trans->aborted = 0;
29019ae4e81SJan Schmidt 	spin_unlock(&fs_info->trans_lock);
29115ee9bc7SJosef Bacik 
29279154b1bSChris Mason 	return 0;
29379154b1bSChris Mason }
29479154b1bSChris Mason 
295d352ac68SChris Mason /*
296d397712bSChris Mason  * this does all the record keeping required to make sure that a reference
297d397712bSChris Mason  * counted root is properly recorded in a given transaction.  This is required
298d397712bSChris Mason  * to make sure the old root from before we joined the transaction is deleted
299d397712bSChris Mason  * when the transaction commits
300d352ac68SChris Mason  */
3017585717fSChris Mason static int record_root_in_trans(struct btrfs_trans_handle *trans,
3025d4f98a2SYan Zheng 			       struct btrfs_root *root)
3036702ed49SChris Mason {
30427cdeb70SMiao Xie 	if (test_bit(BTRFS_ROOT_REF_COWS, &root->state) &&
30527cdeb70SMiao Xie 	    root->last_trans < trans->transid) {
3066702ed49SChris Mason 		WARN_ON(root == root->fs_info->extent_root);
3075d4f98a2SYan Zheng 		WARN_ON(root->commit_root != root->node);
3085d4f98a2SYan Zheng 
3097585717fSChris Mason 		/*
31027cdeb70SMiao Xie 		 * see below for IN_TRANS_SETUP usage rules
3117585717fSChris Mason 		 * we have the reloc mutex held now, so there
3127585717fSChris Mason 		 * is only one writer in this function
3137585717fSChris Mason 		 */
31427cdeb70SMiao Xie 		set_bit(BTRFS_ROOT_IN_TRANS_SETUP, &root->state);
3157585717fSChris Mason 
31627cdeb70SMiao Xie 		/* make sure readers find IN_TRANS_SETUP before
3177585717fSChris Mason 		 * they find our root->last_trans update
3187585717fSChris Mason 		 */
3197585717fSChris Mason 		smp_wmb();
3207585717fSChris Mason 
321a4abeea4SJosef Bacik 		spin_lock(&root->fs_info->fs_roots_radix_lock);
322a4abeea4SJosef Bacik 		if (root->last_trans == trans->transid) {
323a4abeea4SJosef Bacik 			spin_unlock(&root->fs_info->fs_roots_radix_lock);
324a4abeea4SJosef Bacik 			return 0;
325a4abeea4SJosef Bacik 		}
3266702ed49SChris Mason 		radix_tree_tag_set(&root->fs_info->fs_roots_radix,
3276702ed49SChris Mason 			   (unsigned long)root->root_key.objectid,
3286702ed49SChris Mason 			   BTRFS_ROOT_TRANS_TAG);
329a4abeea4SJosef Bacik 		spin_unlock(&root->fs_info->fs_roots_radix_lock);
3307585717fSChris Mason 		root->last_trans = trans->transid;
3317585717fSChris Mason 
3327585717fSChris Mason 		/* this is pretty tricky.  We don't want to
3337585717fSChris Mason 		 * take the relocation lock in btrfs_record_root_in_trans
3347585717fSChris Mason 		 * unless we're really doing the first setup for this root in
3357585717fSChris Mason 		 * this transaction.
3367585717fSChris Mason 		 *
3377585717fSChris Mason 		 * Normally we'd use root->last_trans as a flag to decide
3387585717fSChris Mason 		 * if we want to take the expensive mutex.
3397585717fSChris Mason 		 *
3407585717fSChris Mason 		 * But, we have to set root->last_trans before we
3417585717fSChris Mason 		 * init the relocation root, otherwise, we trip over warnings
3427585717fSChris Mason 		 * in ctree.c.  The solution used here is to flag ourselves
34327cdeb70SMiao Xie 		 * with root IN_TRANS_SETUP.  When this is 1, we're still
3447585717fSChris Mason 		 * fixing up the reloc trees and everyone must wait.
3457585717fSChris Mason 		 *
3467585717fSChris Mason 		 * When this is zero, they can trust root->last_trans and fly
3477585717fSChris Mason 		 * through btrfs_record_root_in_trans without having to take the
3487585717fSChris Mason 		 * lock.  smp_wmb() makes sure that all the writes above are
3497585717fSChris Mason 		 * done before we pop in the zero below
3507585717fSChris Mason 		 */
3515d4f98a2SYan Zheng 		btrfs_init_reloc_root(trans, root);
352c7548af6SChris Mason 		smp_mb__before_atomic();
35327cdeb70SMiao Xie 		clear_bit(BTRFS_ROOT_IN_TRANS_SETUP, &root->state);
3546702ed49SChris Mason 	}
3555d4f98a2SYan Zheng 	return 0;
3566702ed49SChris Mason }
3575d4f98a2SYan Zheng 
3587585717fSChris Mason 
3592b9dbef2SJosef Bacik void btrfs_add_dropped_root(struct btrfs_trans_handle *trans,
3602b9dbef2SJosef Bacik 			    struct btrfs_root *root)
3612b9dbef2SJosef Bacik {
3622b9dbef2SJosef Bacik 	struct btrfs_transaction *cur_trans = trans->transaction;
3632b9dbef2SJosef Bacik 
3642b9dbef2SJosef Bacik 	/* Add ourselves to the transaction dropped list */
3652b9dbef2SJosef Bacik 	spin_lock(&cur_trans->dropped_roots_lock);
3662b9dbef2SJosef Bacik 	list_add_tail(&root->root_list, &cur_trans->dropped_roots);
3672b9dbef2SJosef Bacik 	spin_unlock(&cur_trans->dropped_roots_lock);
3682b9dbef2SJosef Bacik 
3692b9dbef2SJosef Bacik 	/* Make sure we don't try to update the root at commit time */
3702b9dbef2SJosef Bacik 	spin_lock(&root->fs_info->fs_roots_radix_lock);
3712b9dbef2SJosef Bacik 	radix_tree_tag_clear(&root->fs_info->fs_roots_radix,
3722b9dbef2SJosef Bacik 			     (unsigned long)root->root_key.objectid,
3732b9dbef2SJosef Bacik 			     BTRFS_ROOT_TRANS_TAG);
3742b9dbef2SJosef Bacik 	spin_unlock(&root->fs_info->fs_roots_radix_lock);
3752b9dbef2SJosef Bacik }
3762b9dbef2SJosef Bacik 
3777585717fSChris Mason int btrfs_record_root_in_trans(struct btrfs_trans_handle *trans,
3787585717fSChris Mason 			       struct btrfs_root *root)
3797585717fSChris Mason {
38027cdeb70SMiao Xie 	if (!test_bit(BTRFS_ROOT_REF_COWS, &root->state))
3817585717fSChris Mason 		return 0;
3827585717fSChris Mason 
3837585717fSChris Mason 	/*
38427cdeb70SMiao Xie 	 * see record_root_in_trans for comments about IN_TRANS_SETUP usage
3857585717fSChris Mason 	 * and barriers
3867585717fSChris Mason 	 */
3877585717fSChris Mason 	smp_rmb();
3887585717fSChris Mason 	if (root->last_trans == trans->transid &&
38927cdeb70SMiao Xie 	    !test_bit(BTRFS_ROOT_IN_TRANS_SETUP, &root->state))
3907585717fSChris Mason 		return 0;
3917585717fSChris Mason 
3927585717fSChris Mason 	mutex_lock(&root->fs_info->reloc_mutex);
3937585717fSChris Mason 	record_root_in_trans(trans, root);
3947585717fSChris Mason 	mutex_unlock(&root->fs_info->reloc_mutex);
3957585717fSChris Mason 
3967585717fSChris Mason 	return 0;
3977585717fSChris Mason }
3987585717fSChris Mason 
3994a9d8bdeSMiao Xie static inline int is_transaction_blocked(struct btrfs_transaction *trans)
4004a9d8bdeSMiao Xie {
4014a9d8bdeSMiao Xie 	return (trans->state >= TRANS_STATE_BLOCKED &&
402501407aaSJosef Bacik 		trans->state < TRANS_STATE_UNBLOCKED &&
403501407aaSJosef Bacik 		!trans->aborted);
4044a9d8bdeSMiao Xie }
4054a9d8bdeSMiao Xie 
406d352ac68SChris Mason /* wait for commit against the current transaction to become unblocked
407d352ac68SChris Mason  * when this is done, it is safe to start a new transaction, but the current
408d352ac68SChris Mason  * transaction might not be fully on disk.
409d352ac68SChris Mason  */
41037d1aeeeSChris Mason static void wait_current_trans(struct btrfs_root *root)
41179154b1bSChris Mason {
412f9295749SChris Mason 	struct btrfs_transaction *cur_trans;
41379154b1bSChris Mason 
414a4abeea4SJosef Bacik 	spin_lock(&root->fs_info->trans_lock);
415f9295749SChris Mason 	cur_trans = root->fs_info->running_transaction;
4164a9d8bdeSMiao Xie 	if (cur_trans && is_transaction_blocked(cur_trans)) {
41713c5a93eSJosef Bacik 		atomic_inc(&cur_trans->use_count);
418a4abeea4SJosef Bacik 		spin_unlock(&root->fs_info->trans_lock);
41972d63ed6SLi Zefan 
42072d63ed6SLi Zefan 		wait_event(root->fs_info->transaction_wait,
421501407aaSJosef Bacik 			   cur_trans->state >= TRANS_STATE_UNBLOCKED ||
422501407aaSJosef Bacik 			   cur_trans->aborted);
423724e2315SJosef Bacik 		btrfs_put_transaction(cur_trans);
424a4abeea4SJosef Bacik 	} else {
425a4abeea4SJosef Bacik 		spin_unlock(&root->fs_info->trans_lock);
426f9295749SChris Mason 	}
42737d1aeeeSChris Mason }
42837d1aeeeSChris Mason 
429a22285a6SYan, Zheng static int may_wait_transaction(struct btrfs_root *root, int type)
43037d1aeeeSChris Mason {
431a4abeea4SJosef Bacik 	if (root->fs_info->log_root_recovering)
432a4abeea4SJosef Bacik 		return 0;
433a4abeea4SJosef Bacik 
434a4abeea4SJosef Bacik 	if (type == TRANS_USERSPACE)
435a22285a6SYan, Zheng 		return 1;
436a4abeea4SJosef Bacik 
437a4abeea4SJosef Bacik 	if (type == TRANS_START &&
438a4abeea4SJosef Bacik 	    !atomic_read(&root->fs_info->open_ioctl_trans))
439a4abeea4SJosef Bacik 		return 1;
440a4abeea4SJosef Bacik 
441a22285a6SYan, Zheng 	return 0;
442a22285a6SYan, Zheng }
443a22285a6SYan, Zheng 
44420dd2cbfSMiao Xie static inline bool need_reserve_reloc_root(struct btrfs_root *root)
44520dd2cbfSMiao Xie {
44620dd2cbfSMiao Xie 	if (!root->fs_info->reloc_ctl ||
44727cdeb70SMiao Xie 	    !test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
44820dd2cbfSMiao Xie 	    root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID ||
44920dd2cbfSMiao Xie 	    root->reloc_root)
45020dd2cbfSMiao Xie 		return false;
45120dd2cbfSMiao Xie 
45220dd2cbfSMiao Xie 	return true;
45320dd2cbfSMiao Xie }
45420dd2cbfSMiao Xie 
45508e007d2SMiao Xie static struct btrfs_trans_handle *
4560860adfdSMiao Xie start_transaction(struct btrfs_root *root, u64 num_items, unsigned int type,
45708e007d2SMiao Xie 		  enum btrfs_reserve_flush_enum flush)
458a22285a6SYan, Zheng {
459a22285a6SYan, Zheng 	struct btrfs_trans_handle *h;
460a22285a6SYan, Zheng 	struct btrfs_transaction *cur_trans;
461b5009945SJosef Bacik 	u64 num_bytes = 0;
462c5567237SArne Jansen 	u64 qgroup_reserved = 0;
46320dd2cbfSMiao Xie 	bool reloc_reserved = false;
46420dd2cbfSMiao Xie 	int ret;
465acce952bSliubo 
46646c4e71eSFilipe Manana 	/* Send isn't supposed to start transactions. */
4672755a0deSDavid Sterba 	ASSERT(current->journal_info != BTRFS_SEND_TRANS_STUB);
46846c4e71eSFilipe Manana 
46987533c47SMiao Xie 	if (test_bit(BTRFS_FS_STATE_ERROR, &root->fs_info->fs_state))
470acce952bSliubo 		return ERR_PTR(-EROFS);
4712a1eb461SJosef Bacik 
47246c4e71eSFilipe Manana 	if (current->journal_info) {
4730860adfdSMiao Xie 		WARN_ON(type & TRANS_EXTWRITERS);
4742a1eb461SJosef Bacik 		h = current->journal_info;
4752a1eb461SJosef Bacik 		h->use_count++;
476b7d5b0a8SMiao Xie 		WARN_ON(h->use_count > 2);
4772a1eb461SJosef Bacik 		h->orig_rsv = h->block_rsv;
4782a1eb461SJosef Bacik 		h->block_rsv = NULL;
4792a1eb461SJosef Bacik 		goto got_it;
4802a1eb461SJosef Bacik 	}
481b5009945SJosef Bacik 
482b5009945SJosef Bacik 	/*
483b5009945SJosef Bacik 	 * Do the reservation before we join the transaction so we can do all
484b5009945SJosef Bacik 	 * the appropriate flushing if need be.
485b5009945SJosef Bacik 	 */
486b5009945SJosef Bacik 	if (num_items > 0 && root != root->fs_info->chunk_root) {
487c5567237SArne Jansen 		if (root->fs_info->quota_enabled &&
488c5567237SArne Jansen 		    is_fstree(root->root_key.objectid)) {
489707e8a07SDavid Sterba 			qgroup_reserved = num_items * root->nodesize;
490c5567237SArne Jansen 			ret = btrfs_qgroup_reserve(root, qgroup_reserved);
491c5567237SArne Jansen 			if (ret)
492c5567237SArne Jansen 				return ERR_PTR(ret);
493c5567237SArne Jansen 		}
494c5567237SArne Jansen 
495b5009945SJosef Bacik 		num_bytes = btrfs_calc_trans_metadata_size(root, num_items);
49620dd2cbfSMiao Xie 		/*
49720dd2cbfSMiao Xie 		 * Do the reservation for the relocation root creation
49820dd2cbfSMiao Xie 		 */
499ee39b432SDavid Sterba 		if (need_reserve_reloc_root(root)) {
50020dd2cbfSMiao Xie 			num_bytes += root->nodesize;
50120dd2cbfSMiao Xie 			reloc_reserved = true;
50220dd2cbfSMiao Xie 		}
50320dd2cbfSMiao Xie 
5044a92b1b8SJosef Bacik 		ret = btrfs_block_rsv_add(root,
505b5009945SJosef Bacik 					  &root->fs_info->trans_block_rsv,
50608e007d2SMiao Xie 					  num_bytes, flush);
507b5009945SJosef Bacik 		if (ret)
508843fcf35SMiao Xie 			goto reserve_fail;
509b5009945SJosef Bacik 	}
510a22285a6SYan, Zheng again:
511a22285a6SYan, Zheng 	h = kmem_cache_alloc(btrfs_trans_handle_cachep, GFP_NOFS);
512843fcf35SMiao Xie 	if (!h) {
513843fcf35SMiao Xie 		ret = -ENOMEM;
514843fcf35SMiao Xie 		goto alloc_fail;
515843fcf35SMiao Xie 	}
516a22285a6SYan, Zheng 
51798114659SJosef Bacik 	/*
51898114659SJosef Bacik 	 * If we are JOIN_NOLOCK we're already committing a transaction and
51998114659SJosef Bacik 	 * waiting on this guy, so we don't need to do the sb_start_intwrite
52098114659SJosef Bacik 	 * because we're already holding a ref.  We need this because we could
52198114659SJosef Bacik 	 * have raced in and did an fsync() on a file which can kick a commit
52298114659SJosef Bacik 	 * and then we deadlock with somebody doing a freeze.
523354aa0fbSMiao Xie 	 *
524354aa0fbSMiao Xie 	 * If we are ATTACH, it means we just want to catch the current
525354aa0fbSMiao Xie 	 * transaction and commit it, so we needn't do sb_start_intwrite().
52698114659SJosef Bacik 	 */
5270860adfdSMiao Xie 	if (type & __TRANS_FREEZABLE)
528b2b5ef5cSJan Kara 		sb_start_intwrite(root->fs_info->sb);
529b2b5ef5cSJan Kara 
530a22285a6SYan, Zheng 	if (may_wait_transaction(root, type))
53137d1aeeeSChris Mason 		wait_current_trans(root);
532a22285a6SYan, Zheng 
533a4abeea4SJosef Bacik 	do {
534354aa0fbSMiao Xie 		ret = join_transaction(root, type);
535178260b2SMiao Xie 		if (ret == -EBUSY) {
536a4abeea4SJosef Bacik 			wait_current_trans(root);
537178260b2SMiao Xie 			if (unlikely(type == TRANS_ATTACH))
538178260b2SMiao Xie 				ret = -ENOENT;
539178260b2SMiao Xie 		}
540a4abeea4SJosef Bacik 	} while (ret == -EBUSY);
541a4abeea4SJosef Bacik 
542db5b493aSTsutomu Itoh 	if (ret < 0) {
543354aa0fbSMiao Xie 		/* We must get the transaction if we are JOIN_NOLOCK. */
544354aa0fbSMiao Xie 		BUG_ON(type == TRANS_JOIN_NOLOCK);
545843fcf35SMiao Xie 		goto join_fail;
546db5b493aSTsutomu Itoh 	}
5470f7d52f4SChris Mason 
548a22285a6SYan, Zheng 	cur_trans = root->fs_info->running_transaction;
549a22285a6SYan, Zheng 
550a22285a6SYan, Zheng 	h->transid = cur_trans->transid;
551a22285a6SYan, Zheng 	h->transaction = cur_trans;
55279154b1bSChris Mason 	h->blocks_used = 0;
553a22285a6SYan, Zheng 	h->bytes_reserved = 0;
5544fbcdf66SFilipe Manana 	h->chunk_bytes_reserved = 0;
555d13603efSArne Jansen 	h->root = root;
55656bec294SChris Mason 	h->delayed_ref_updates = 0;
5572a1eb461SJosef Bacik 	h->use_count = 1;
5580e721106SJosef Bacik 	h->adding_csums = 0;
559f0486c68SYan, Zheng 	h->block_rsv = NULL;
5602a1eb461SJosef Bacik 	h->orig_rsv = NULL;
56149b25e05SJeff Mahoney 	h->aborted = 0;
5624b824906SMiao Xie 	h->qgroup_reserved = 0;
563bed92eaeSArne Jansen 	h->delayed_ref_elem.seq = 0;
564a698d075SMiao Xie 	h->type = type;
565c6b305a8SJosef Bacik 	h->allocating_chunk = false;
56620dd2cbfSMiao Xie 	h->reloc_reserved = false;
5675039eddcSJosef Bacik 	h->sync = false;
568bed92eaeSArne Jansen 	INIT_LIST_HEAD(&h->qgroup_ref_list);
569ea658badSJosef Bacik 	INIT_LIST_HEAD(&h->new_bgs);
57050d9aa99SJosef Bacik 	INIT_LIST_HEAD(&h->ordered);
571b7ec40d7SChris Mason 
572a22285a6SYan, Zheng 	smp_mb();
5734a9d8bdeSMiao Xie 	if (cur_trans->state >= TRANS_STATE_BLOCKED &&
5744a9d8bdeSMiao Xie 	    may_wait_transaction(root, type)) {
575abdd2e80SFilipe Manana 		current->journal_info = h;
576a22285a6SYan, Zheng 		btrfs_commit_transaction(h, root);
577a22285a6SYan, Zheng 		goto again;
578a22285a6SYan, Zheng 	}
5799ed74f2dSJosef Bacik 
580b5009945SJosef Bacik 	if (num_bytes) {
5818c2a3ca2SJosef Bacik 		trace_btrfs_space_reservation(root->fs_info, "transaction",
5822bcc0328SLiu Bo 					      h->transid, num_bytes, 1);
583b5009945SJosef Bacik 		h->block_rsv = &root->fs_info->trans_block_rsv;
584b5009945SJosef Bacik 		h->bytes_reserved = num_bytes;
58520dd2cbfSMiao Xie 		h->reloc_reserved = reloc_reserved;
586a22285a6SYan, Zheng 	}
5874b824906SMiao Xie 	h->qgroup_reserved = qgroup_reserved;
588a22285a6SYan, Zheng 
5892a1eb461SJosef Bacik got_it:
590a4abeea4SJosef Bacik 	btrfs_record_root_in_trans(h, root);
591a22285a6SYan, Zheng 
592a22285a6SYan, Zheng 	if (!current->journal_info && type != TRANS_USERSPACE)
593a22285a6SYan, Zheng 		current->journal_info = h;
59479154b1bSChris Mason 	return h;
595843fcf35SMiao Xie 
596843fcf35SMiao Xie join_fail:
5970860adfdSMiao Xie 	if (type & __TRANS_FREEZABLE)
598843fcf35SMiao Xie 		sb_end_intwrite(root->fs_info->sb);
599843fcf35SMiao Xie 	kmem_cache_free(btrfs_trans_handle_cachep, h);
600843fcf35SMiao Xie alloc_fail:
601843fcf35SMiao Xie 	if (num_bytes)
602843fcf35SMiao Xie 		btrfs_block_rsv_release(root, &root->fs_info->trans_block_rsv,
603843fcf35SMiao Xie 					num_bytes);
604843fcf35SMiao Xie reserve_fail:
605843fcf35SMiao Xie 	if (qgroup_reserved)
606843fcf35SMiao Xie 		btrfs_qgroup_free(root, qgroup_reserved);
607843fcf35SMiao Xie 	return ERR_PTR(ret);
60879154b1bSChris Mason }
60979154b1bSChris Mason 
610f9295749SChris Mason struct btrfs_trans_handle *btrfs_start_transaction(struct btrfs_root *root,
611a22285a6SYan, Zheng 						   int num_items)
612f9295749SChris Mason {
61308e007d2SMiao Xie 	return start_transaction(root, num_items, TRANS_START,
61408e007d2SMiao Xie 				 BTRFS_RESERVE_FLUSH_ALL);
615f9295749SChris Mason }
6168407aa46SMiao Xie 
61708e007d2SMiao Xie struct btrfs_trans_handle *btrfs_start_transaction_lflush(
6188407aa46SMiao Xie 					struct btrfs_root *root, int num_items)
6198407aa46SMiao Xie {
62008e007d2SMiao Xie 	return start_transaction(root, num_items, TRANS_START,
62108e007d2SMiao Xie 				 BTRFS_RESERVE_FLUSH_LIMIT);
6228407aa46SMiao Xie }
6238407aa46SMiao Xie 
6247a7eaa40SJosef Bacik struct btrfs_trans_handle *btrfs_join_transaction(struct btrfs_root *root)
625f9295749SChris Mason {
6268407aa46SMiao Xie 	return start_transaction(root, 0, TRANS_JOIN, 0);
627f9295749SChris Mason }
628f9295749SChris Mason 
6297a7eaa40SJosef Bacik struct btrfs_trans_handle *btrfs_join_transaction_nolock(struct btrfs_root *root)
6300af3d00bSJosef Bacik {
6318407aa46SMiao Xie 	return start_transaction(root, 0, TRANS_JOIN_NOLOCK, 0);
6320af3d00bSJosef Bacik }
6330af3d00bSJosef Bacik 
6347a7eaa40SJosef Bacik struct btrfs_trans_handle *btrfs_start_ioctl_transaction(struct btrfs_root *root)
6359ca9ee09SSage Weil {
6368407aa46SMiao Xie 	return start_transaction(root, 0, TRANS_USERSPACE, 0);
6379ca9ee09SSage Weil }
6389ca9ee09SSage Weil 
639d4edf39bSMiao Xie /*
640d4edf39bSMiao Xie  * btrfs_attach_transaction() - catch the running transaction
641d4edf39bSMiao Xie  *
642d4edf39bSMiao Xie  * It is used when we want to commit the current the transaction, but
643d4edf39bSMiao Xie  * don't want to start a new one.
644d4edf39bSMiao Xie  *
645d4edf39bSMiao Xie  * Note: If this function return -ENOENT, it just means there is no
646d4edf39bSMiao Xie  * running transaction. But it is possible that the inactive transaction
647d4edf39bSMiao Xie  * is still in the memory, not fully on disk. If you hope there is no
648d4edf39bSMiao Xie  * inactive transaction in the fs when -ENOENT is returned, you should
649d4edf39bSMiao Xie  * invoke
650d4edf39bSMiao Xie  *     btrfs_attach_transaction_barrier()
651d4edf39bSMiao Xie  */
652354aa0fbSMiao Xie struct btrfs_trans_handle *btrfs_attach_transaction(struct btrfs_root *root)
65360376ce4SJosef Bacik {
654354aa0fbSMiao Xie 	return start_transaction(root, 0, TRANS_ATTACH, 0);
65560376ce4SJosef Bacik }
65660376ce4SJosef Bacik 
657d4edf39bSMiao Xie /*
65890b6d283SWang Sheng-Hui  * btrfs_attach_transaction_barrier() - catch the running transaction
659d4edf39bSMiao Xie  *
660d4edf39bSMiao Xie  * It is similar to the above function, the differentia is this one
661d4edf39bSMiao Xie  * will wait for all the inactive transactions until they fully
662d4edf39bSMiao Xie  * complete.
663d4edf39bSMiao Xie  */
664d4edf39bSMiao Xie struct btrfs_trans_handle *
665d4edf39bSMiao Xie btrfs_attach_transaction_barrier(struct btrfs_root *root)
666d4edf39bSMiao Xie {
667d4edf39bSMiao Xie 	struct btrfs_trans_handle *trans;
668d4edf39bSMiao Xie 
669d4edf39bSMiao Xie 	trans = start_transaction(root, 0, TRANS_ATTACH, 0);
670d4edf39bSMiao Xie 	if (IS_ERR(trans) && PTR_ERR(trans) == -ENOENT)
671d4edf39bSMiao Xie 		btrfs_wait_for_commit(root, 0);
672d4edf39bSMiao Xie 
673d4edf39bSMiao Xie 	return trans;
674d4edf39bSMiao Xie }
675d4edf39bSMiao Xie 
676d352ac68SChris Mason /* wait for a transaction commit to be fully complete */
677b9c8300cSLi Zefan static noinline void wait_for_commit(struct btrfs_root *root,
67889ce8a63SChris Mason 				    struct btrfs_transaction *commit)
67989ce8a63SChris Mason {
6804a9d8bdeSMiao Xie 	wait_event(commit->commit_wait, commit->state == TRANS_STATE_COMPLETED);
68189ce8a63SChris Mason }
68289ce8a63SChris Mason 
68346204592SSage Weil int btrfs_wait_for_commit(struct btrfs_root *root, u64 transid)
68446204592SSage Weil {
68546204592SSage Weil 	struct btrfs_transaction *cur_trans = NULL, *t;
6868cd2807fSMiao Xie 	int ret = 0;
68746204592SSage Weil 
68846204592SSage Weil 	if (transid) {
68946204592SSage Weil 		if (transid <= root->fs_info->last_trans_committed)
690a4abeea4SJosef Bacik 			goto out;
69146204592SSage Weil 
69246204592SSage Weil 		/* find specified transaction */
693a4abeea4SJosef Bacik 		spin_lock(&root->fs_info->trans_lock);
69446204592SSage Weil 		list_for_each_entry(t, &root->fs_info->trans_list, list) {
69546204592SSage Weil 			if (t->transid == transid) {
69646204592SSage Weil 				cur_trans = t;
697a4abeea4SJosef Bacik 				atomic_inc(&cur_trans->use_count);
6988cd2807fSMiao Xie 				ret = 0;
69946204592SSage Weil 				break;
70046204592SSage Weil 			}
7018cd2807fSMiao Xie 			if (t->transid > transid) {
7028cd2807fSMiao Xie 				ret = 0;
70346204592SSage Weil 				break;
70446204592SSage Weil 			}
7058cd2807fSMiao Xie 		}
706a4abeea4SJosef Bacik 		spin_unlock(&root->fs_info->trans_lock);
70742383020SSage Weil 
70842383020SSage Weil 		/*
70942383020SSage Weil 		 * The specified transaction doesn't exist, or we
71042383020SSage Weil 		 * raced with btrfs_commit_transaction
71142383020SSage Weil 		 */
71242383020SSage Weil 		if (!cur_trans) {
71342383020SSage Weil 			if (transid > root->fs_info->last_trans_committed)
71442383020SSage Weil 				ret = -EINVAL;
7158cd2807fSMiao Xie 			goto out;
71642383020SSage Weil 		}
71746204592SSage Weil 	} else {
71846204592SSage Weil 		/* find newest transaction that is committing | committed */
719a4abeea4SJosef Bacik 		spin_lock(&root->fs_info->trans_lock);
72046204592SSage Weil 		list_for_each_entry_reverse(t, &root->fs_info->trans_list,
72146204592SSage Weil 					    list) {
7224a9d8bdeSMiao Xie 			if (t->state >= TRANS_STATE_COMMIT_START) {
7234a9d8bdeSMiao Xie 				if (t->state == TRANS_STATE_COMPLETED)
7243473f3c0SJosef Bacik 					break;
72546204592SSage Weil 				cur_trans = t;
726a4abeea4SJosef Bacik 				atomic_inc(&cur_trans->use_count);
72746204592SSage Weil 				break;
72846204592SSage Weil 			}
72946204592SSage Weil 		}
730a4abeea4SJosef Bacik 		spin_unlock(&root->fs_info->trans_lock);
73146204592SSage Weil 		if (!cur_trans)
732a4abeea4SJosef Bacik 			goto out;  /* nothing committing|committed */
73346204592SSage Weil 	}
73446204592SSage Weil 
73546204592SSage Weil 	wait_for_commit(root, cur_trans);
736724e2315SJosef Bacik 	btrfs_put_transaction(cur_trans);
737a4abeea4SJosef Bacik out:
73846204592SSage Weil 	return ret;
73946204592SSage Weil }
74046204592SSage Weil 
74137d1aeeeSChris Mason void btrfs_throttle(struct btrfs_root *root)
74237d1aeeeSChris Mason {
743a4abeea4SJosef Bacik 	if (!atomic_read(&root->fs_info->open_ioctl_trans))
74437d1aeeeSChris Mason 		wait_current_trans(root);
74537d1aeeeSChris Mason }
74637d1aeeeSChris Mason 
7478929ecfaSYan, Zheng static int should_end_transaction(struct btrfs_trans_handle *trans,
7488929ecfaSYan, Zheng 				  struct btrfs_root *root)
7498929ecfaSYan, Zheng {
7501be41b78SJosef Bacik 	if (root->fs_info->global_block_rsv.space_info->full &&
7510a2b2a84SJosef Bacik 	    btrfs_check_space_for_delayed_refs(trans, root))
7521be41b78SJosef Bacik 		return 1;
75336ba022aSJosef Bacik 
7541be41b78SJosef Bacik 	return !!btrfs_block_rsv_check(root, &root->fs_info->global_block_rsv, 5);
7558929ecfaSYan, Zheng }
7568929ecfaSYan, Zheng 
7578929ecfaSYan, Zheng int btrfs_should_end_transaction(struct btrfs_trans_handle *trans,
7588929ecfaSYan, Zheng 				 struct btrfs_root *root)
7598929ecfaSYan, Zheng {
7608929ecfaSYan, Zheng 	struct btrfs_transaction *cur_trans = trans->transaction;
7618929ecfaSYan, Zheng 	int updates;
76249b25e05SJeff Mahoney 	int err;
7638929ecfaSYan, Zheng 
764a4abeea4SJosef Bacik 	smp_mb();
7654a9d8bdeSMiao Xie 	if (cur_trans->state >= TRANS_STATE_BLOCKED ||
7664a9d8bdeSMiao Xie 	    cur_trans->delayed_refs.flushing)
7678929ecfaSYan, Zheng 		return 1;
7688929ecfaSYan, Zheng 
7698929ecfaSYan, Zheng 	updates = trans->delayed_ref_updates;
7708929ecfaSYan, Zheng 	trans->delayed_ref_updates = 0;
77149b25e05SJeff Mahoney 	if (updates) {
77228ed1345SChris Mason 		err = btrfs_run_delayed_refs(trans, root, updates * 2);
77349b25e05SJeff Mahoney 		if (err) /* Error code will also eval true */
77449b25e05SJeff Mahoney 			return err;
77549b25e05SJeff Mahoney 	}
7768929ecfaSYan, Zheng 
7778929ecfaSYan, Zheng 	return should_end_transaction(trans, root);
7788929ecfaSYan, Zheng }
7798929ecfaSYan, Zheng 
78089ce8a63SChris Mason static int __btrfs_end_transaction(struct btrfs_trans_handle *trans,
781a698d075SMiao Xie 			  struct btrfs_root *root, int throttle)
78279154b1bSChris Mason {
7838929ecfaSYan, Zheng 	struct btrfs_transaction *cur_trans = trans->transaction;
784ab78c84dSChris Mason 	struct btrfs_fs_info *info = root->fs_info;
7851be41b78SJosef Bacik 	unsigned long cur = trans->delayed_ref_updates;
786a698d075SMiao Xie 	int lock = (trans->type != TRANS_JOIN_NOLOCK);
7874edc2ca3SDave Jones 	int err = 0;
788a79b7d4bSChris Mason 	int must_run_delayed_refs = 0;
789d6e4a428SChris Mason 
7903bbb24b2SJosef Bacik 	if (trans->use_count > 1) {
7913bbb24b2SJosef Bacik 		trans->use_count--;
7922a1eb461SJosef Bacik 		trans->block_rsv = trans->orig_rsv;
7932a1eb461SJosef Bacik 		return 0;
7942a1eb461SJosef Bacik 	}
7952a1eb461SJosef Bacik 
796b24e03dbSJosef Bacik 	btrfs_trans_release_metadata(trans, root);
7974c13d758SJosef Bacik 	trans->block_rsv = NULL;
798c5567237SArne Jansen 
799ea658badSJosef Bacik 	if (!list_empty(&trans->new_bgs))
800ea658badSJosef Bacik 		btrfs_create_pending_block_groups(trans, root);
801ea658badSJosef Bacik 
80250d9aa99SJosef Bacik 	if (!list_empty(&trans->ordered)) {
80350d9aa99SJosef Bacik 		spin_lock(&info->trans_lock);
804d3efe084SFilipe Manana 		list_splice_init(&trans->ordered, &cur_trans->pending_ordered);
80550d9aa99SJosef Bacik 		spin_unlock(&info->trans_lock);
80650d9aa99SJosef Bacik 	}
80750d9aa99SJosef Bacik 
808c3e69d58SChris Mason 	trans->delayed_ref_updates = 0;
809a79b7d4bSChris Mason 	if (!trans->sync) {
810a79b7d4bSChris Mason 		must_run_delayed_refs =
811a79b7d4bSChris Mason 			btrfs_should_throttle_delayed_refs(trans, root);
8120a2b2a84SJosef Bacik 		cur = max_t(unsigned long, cur, 32);
813a79b7d4bSChris Mason 
814a79b7d4bSChris Mason 		/*
815a79b7d4bSChris Mason 		 * don't make the caller wait if they are from a NOLOCK
816a79b7d4bSChris Mason 		 * or ATTACH transaction, it will deadlock with commit
817a79b7d4bSChris Mason 		 */
818a79b7d4bSChris Mason 		if (must_run_delayed_refs == 1 &&
819a79b7d4bSChris Mason 		    (trans->type & (__TRANS_JOIN_NOLOCK | __TRANS_ATTACH)))
820a79b7d4bSChris Mason 			must_run_delayed_refs = 2;
82156bec294SChris Mason 	}
822bb721703SChris Mason 
823fcebe456SJosef Bacik 	if (trans->qgroup_reserved) {
824fcebe456SJosef Bacik 		/*
825fcebe456SJosef Bacik 		 * the same root has to be passed here between start_transaction
826fcebe456SJosef Bacik 		 * and end_transaction. Subvolume quota depends on this.
827fcebe456SJosef Bacik 		 */
828fcebe456SJosef Bacik 		btrfs_qgroup_free(trans->root, trans->qgroup_reserved);
829fcebe456SJosef Bacik 		trans->qgroup_reserved = 0;
830fcebe456SJosef Bacik 	}
831fcebe456SJosef Bacik 
8320e721106SJosef Bacik 	btrfs_trans_release_metadata(trans, root);
8330e721106SJosef Bacik 	trans->block_rsv = NULL;
83456bec294SChris Mason 
835ea658badSJosef Bacik 	if (!list_empty(&trans->new_bgs))
836ea658badSJosef Bacik 		btrfs_create_pending_block_groups(trans, root);
837ea658badSJosef Bacik 
8384fbcdf66SFilipe Manana 	btrfs_trans_release_chunk_metadata(trans);
8394fbcdf66SFilipe Manana 
840a4abeea4SJosef Bacik 	if (lock && !atomic_read(&root->fs_info->open_ioctl_trans) &&
8414a9d8bdeSMiao Xie 	    should_end_transaction(trans, root) &&
8424a9d8bdeSMiao Xie 	    ACCESS_ONCE(cur_trans->state) == TRANS_STATE_RUNNING) {
8434a9d8bdeSMiao Xie 		spin_lock(&info->trans_lock);
8444a9d8bdeSMiao Xie 		if (cur_trans->state == TRANS_STATE_RUNNING)
8454a9d8bdeSMiao Xie 			cur_trans->state = TRANS_STATE_BLOCKED;
8464a9d8bdeSMiao Xie 		spin_unlock(&info->trans_lock);
847a4abeea4SJosef Bacik 	}
8488929ecfaSYan, Zheng 
8494a9d8bdeSMiao Xie 	if (lock && ACCESS_ONCE(cur_trans->state) == TRANS_STATE_BLOCKED) {
8503bbb24b2SJosef Bacik 		if (throttle)
8518929ecfaSYan, Zheng 			return btrfs_commit_transaction(trans, root);
8523bbb24b2SJosef Bacik 		else
8538929ecfaSYan, Zheng 			wake_up_process(info->transaction_kthread);
8548929ecfaSYan, Zheng 	}
8558929ecfaSYan, Zheng 
8560860adfdSMiao Xie 	if (trans->type & __TRANS_FREEZABLE)
8576df7881aSJosef Bacik 		sb_end_intwrite(root->fs_info->sb);
8586df7881aSJosef Bacik 
8598929ecfaSYan, Zheng 	WARN_ON(cur_trans != info->running_transaction);
86013c5a93eSJosef Bacik 	WARN_ON(atomic_read(&cur_trans->num_writers) < 1);
86113c5a93eSJosef Bacik 	atomic_dec(&cur_trans->num_writers);
8620860adfdSMiao Xie 	extwriter_counter_dec(cur_trans, trans->type);
86389ce8a63SChris Mason 
864a83342aaSDavid Sterba 	/*
865a83342aaSDavid Sterba 	 * Make sure counter is updated before we wake up waiters.
866a83342aaSDavid Sterba 	 */
86799d16cbcSSage Weil 	smp_mb();
86879154b1bSChris Mason 	if (waitqueue_active(&cur_trans->writer_wait))
86979154b1bSChris Mason 		wake_up(&cur_trans->writer_wait);
870724e2315SJosef Bacik 	btrfs_put_transaction(cur_trans);
8719ed74f2dSJosef Bacik 
8729ed74f2dSJosef Bacik 	if (current->journal_info == trans)
8739ed74f2dSJosef Bacik 		current->journal_info = NULL;
874ab78c84dSChris Mason 
87524bbcf04SYan, Zheng 	if (throttle)
87624bbcf04SYan, Zheng 		btrfs_run_delayed_iputs(root);
87724bbcf04SYan, Zheng 
87849b25e05SJeff Mahoney 	if (trans->aborted ||
8794e121c06SJosef Bacik 	    test_bit(BTRFS_FS_STATE_ERROR, &root->fs_info->fs_state)) {
8804e121c06SJosef Bacik 		wake_up_process(info->transaction_kthread);
8814edc2ca3SDave Jones 		err = -EIO;
8824e121c06SJosef Bacik 	}
883edf39272SJan Schmidt 	assert_qgroups_uptodate(trans);
88449b25e05SJeff Mahoney 
8854edc2ca3SDave Jones 	kmem_cache_free(btrfs_trans_handle_cachep, trans);
886a79b7d4bSChris Mason 	if (must_run_delayed_refs) {
887a79b7d4bSChris Mason 		btrfs_async_run_delayed_refs(root, cur,
888a79b7d4bSChris Mason 					     must_run_delayed_refs == 1);
889a79b7d4bSChris Mason 	}
8904edc2ca3SDave Jones 	return err;
89179154b1bSChris Mason }
89279154b1bSChris Mason 
89389ce8a63SChris Mason int btrfs_end_transaction(struct btrfs_trans_handle *trans,
89489ce8a63SChris Mason 			  struct btrfs_root *root)
89589ce8a63SChris Mason {
89698ad43beSWang Shilong 	return __btrfs_end_transaction(trans, root, 0);
89789ce8a63SChris Mason }
89889ce8a63SChris Mason 
89989ce8a63SChris Mason int btrfs_end_transaction_throttle(struct btrfs_trans_handle *trans,
90089ce8a63SChris Mason 				   struct btrfs_root *root)
90189ce8a63SChris Mason {
90298ad43beSWang Shilong 	return __btrfs_end_transaction(trans, root, 1);
90316cdcec7SMiao Xie }
90416cdcec7SMiao Xie 
905d352ac68SChris Mason /*
906d352ac68SChris Mason  * when btree blocks are allocated, they have some corresponding bits set for
907d352ac68SChris Mason  * them in one of two extent_io trees.  This is used to make sure all of
908690587d1SChris Mason  * those extents are sent to disk but does not wait on them
909d352ac68SChris Mason  */
910690587d1SChris Mason int btrfs_write_marked_extents(struct btrfs_root *root,
9118cef4e16SYan, Zheng 			       struct extent_io_tree *dirty_pages, int mark)
91279154b1bSChris Mason {
913777e6bd7SChris Mason 	int err = 0;
9147c4452b9SChris Mason 	int werr = 0;
9151728366eSJosef Bacik 	struct address_space *mapping = root->fs_info->btree_inode->i_mapping;
916e6138876SJosef Bacik 	struct extent_state *cached_state = NULL;
917777e6bd7SChris Mason 	u64 start = 0;
9185f39d397SChris Mason 	u64 end;
9197c4452b9SChris Mason 
9201728366eSJosef Bacik 	while (!find_first_extent_bit(dirty_pages, start, &start, &end,
921e6138876SJosef Bacik 				      mark, &cached_state)) {
922663dfbb0SFilipe Manana 		bool wait_writeback = false;
923663dfbb0SFilipe Manana 
924663dfbb0SFilipe Manana 		err = convert_extent_bit(dirty_pages, start, end,
925663dfbb0SFilipe Manana 					 EXTENT_NEED_WAIT,
926e6138876SJosef Bacik 					 mark, &cached_state, GFP_NOFS);
927663dfbb0SFilipe Manana 		/*
928663dfbb0SFilipe Manana 		 * convert_extent_bit can return -ENOMEM, which is most of the
929663dfbb0SFilipe Manana 		 * time a temporary error. So when it happens, ignore the error
930663dfbb0SFilipe Manana 		 * and wait for writeback of this range to finish - because we
931663dfbb0SFilipe Manana 		 * failed to set the bit EXTENT_NEED_WAIT for the range, a call
932663dfbb0SFilipe Manana 		 * to btrfs_wait_marked_extents() would not know that writeback
933663dfbb0SFilipe Manana 		 * for this range started and therefore wouldn't wait for it to
934663dfbb0SFilipe Manana 		 * finish - we don't want to commit a superblock that points to
935663dfbb0SFilipe Manana 		 * btree nodes/leafs for which writeback hasn't finished yet
936663dfbb0SFilipe Manana 		 * (and without errors).
937663dfbb0SFilipe Manana 		 * We cleanup any entries left in the io tree when committing
938663dfbb0SFilipe Manana 		 * the transaction (through clear_btree_io_tree()).
939663dfbb0SFilipe Manana 		 */
940663dfbb0SFilipe Manana 		if (err == -ENOMEM) {
941663dfbb0SFilipe Manana 			err = 0;
942663dfbb0SFilipe Manana 			wait_writeback = true;
943663dfbb0SFilipe Manana 		}
944663dfbb0SFilipe Manana 		if (!err)
9451728366eSJosef Bacik 			err = filemap_fdatawrite_range(mapping, start, end);
9467c4452b9SChris Mason 		if (err)
9477c4452b9SChris Mason 			werr = err;
948663dfbb0SFilipe Manana 		else if (wait_writeback)
949663dfbb0SFilipe Manana 			werr = filemap_fdatawait_range(mapping, start, end);
950e38e2ed7SFilipe Manana 		free_extent_state(cached_state);
951663dfbb0SFilipe Manana 		cached_state = NULL;
9521728366eSJosef Bacik 		cond_resched();
9531728366eSJosef Bacik 		start = end + 1;
9547c4452b9SChris Mason 	}
955690587d1SChris Mason 	return werr;
956690587d1SChris Mason }
957690587d1SChris Mason 
958690587d1SChris Mason /*
959690587d1SChris Mason  * when btree blocks are allocated, they have some corresponding bits set for
960690587d1SChris Mason  * them in one of two extent_io trees.  This is used to make sure all of
961690587d1SChris Mason  * those extents are on disk for transaction or log commit.  We wait
962690587d1SChris Mason  * on all the pages and clear them from the dirty pages state tree
963690587d1SChris Mason  */
964690587d1SChris Mason int btrfs_wait_marked_extents(struct btrfs_root *root,
9658cef4e16SYan, Zheng 			      struct extent_io_tree *dirty_pages, int mark)
966690587d1SChris Mason {
967690587d1SChris Mason 	int err = 0;
968690587d1SChris Mason 	int werr = 0;
9691728366eSJosef Bacik 	struct address_space *mapping = root->fs_info->btree_inode->i_mapping;
970e6138876SJosef Bacik 	struct extent_state *cached_state = NULL;
971690587d1SChris Mason 	u64 start = 0;
972690587d1SChris Mason 	u64 end;
973656f30dbSFilipe Manana 	struct btrfs_inode *btree_ino = BTRFS_I(root->fs_info->btree_inode);
974656f30dbSFilipe Manana 	bool errors = false;
975690587d1SChris Mason 
9761728366eSJosef Bacik 	while (!find_first_extent_bit(dirty_pages, start, &start, &end,
977e6138876SJosef Bacik 				      EXTENT_NEED_WAIT, &cached_state)) {
978663dfbb0SFilipe Manana 		/*
979663dfbb0SFilipe Manana 		 * Ignore -ENOMEM errors returned by clear_extent_bit().
980663dfbb0SFilipe Manana 		 * When committing the transaction, we'll remove any entries
981663dfbb0SFilipe Manana 		 * left in the io tree. For a log commit, we don't remove them
982663dfbb0SFilipe Manana 		 * after committing the log because the tree can be accessed
983663dfbb0SFilipe Manana 		 * concurrently - we do it only at transaction commit time when
984663dfbb0SFilipe Manana 		 * it's safe to do it (through clear_btree_io_tree()).
985663dfbb0SFilipe Manana 		 */
986663dfbb0SFilipe Manana 		err = clear_extent_bit(dirty_pages, start, end,
987663dfbb0SFilipe Manana 				       EXTENT_NEED_WAIT,
988e6138876SJosef Bacik 				       0, 0, &cached_state, GFP_NOFS);
989663dfbb0SFilipe Manana 		if (err == -ENOMEM)
990663dfbb0SFilipe Manana 			err = 0;
991663dfbb0SFilipe Manana 		if (!err)
9921728366eSJosef Bacik 			err = filemap_fdatawait_range(mapping, start, end);
993777e6bd7SChris Mason 		if (err)
994777e6bd7SChris Mason 			werr = err;
995e38e2ed7SFilipe Manana 		free_extent_state(cached_state);
996e38e2ed7SFilipe Manana 		cached_state = NULL;
997777e6bd7SChris Mason 		cond_resched();
9981728366eSJosef Bacik 		start = end + 1;
999777e6bd7SChris Mason 	}
10007c4452b9SChris Mason 	if (err)
10017c4452b9SChris Mason 		werr = err;
1002656f30dbSFilipe Manana 
1003656f30dbSFilipe Manana 	if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) {
1004656f30dbSFilipe Manana 		if ((mark & EXTENT_DIRTY) &&
1005656f30dbSFilipe Manana 		    test_and_clear_bit(BTRFS_INODE_BTREE_LOG1_ERR,
1006656f30dbSFilipe Manana 				       &btree_ino->runtime_flags))
1007656f30dbSFilipe Manana 			errors = true;
1008656f30dbSFilipe Manana 
1009656f30dbSFilipe Manana 		if ((mark & EXTENT_NEW) &&
1010656f30dbSFilipe Manana 		    test_and_clear_bit(BTRFS_INODE_BTREE_LOG2_ERR,
1011656f30dbSFilipe Manana 				       &btree_ino->runtime_flags))
1012656f30dbSFilipe Manana 			errors = true;
1013656f30dbSFilipe Manana 	} else {
1014656f30dbSFilipe Manana 		if (test_and_clear_bit(BTRFS_INODE_BTREE_ERR,
1015656f30dbSFilipe Manana 				       &btree_ino->runtime_flags))
1016656f30dbSFilipe Manana 			errors = true;
1017656f30dbSFilipe Manana 	}
1018656f30dbSFilipe Manana 
1019656f30dbSFilipe Manana 	if (errors && !werr)
1020656f30dbSFilipe Manana 		werr = -EIO;
1021656f30dbSFilipe Manana 
10227c4452b9SChris Mason 	return werr;
102379154b1bSChris Mason }
102479154b1bSChris Mason 
1025690587d1SChris Mason /*
1026690587d1SChris Mason  * when btree blocks are allocated, they have some corresponding bits set for
1027690587d1SChris Mason  * them in one of two extent_io trees.  This is used to make sure all of
1028690587d1SChris Mason  * those extents are on disk for transaction or log commit
1029690587d1SChris Mason  */
1030171170c1SSergei Trofimovich static int btrfs_write_and_wait_marked_extents(struct btrfs_root *root,
10318cef4e16SYan, Zheng 				struct extent_io_tree *dirty_pages, int mark)
1032690587d1SChris Mason {
1033690587d1SChris Mason 	int ret;
1034690587d1SChris Mason 	int ret2;
1035c6adc9ccSMiao Xie 	struct blk_plug plug;
1036690587d1SChris Mason 
1037c6adc9ccSMiao Xie 	blk_start_plug(&plug);
10388cef4e16SYan, Zheng 	ret = btrfs_write_marked_extents(root, dirty_pages, mark);
1039c6adc9ccSMiao Xie 	blk_finish_plug(&plug);
10408cef4e16SYan, Zheng 	ret2 = btrfs_wait_marked_extents(root, dirty_pages, mark);
1041bf0da8c1SChris Mason 
1042bf0da8c1SChris Mason 	if (ret)
1043bf0da8c1SChris Mason 		return ret;
1044bf0da8c1SChris Mason 	if (ret2)
1045bf0da8c1SChris Mason 		return ret2;
1046bf0da8c1SChris Mason 	return 0;
1047690587d1SChris Mason }
1048690587d1SChris Mason 
1049663dfbb0SFilipe Manana static int btrfs_write_and_wait_transaction(struct btrfs_trans_handle *trans,
1050d0c803c4SChris Mason 				     struct btrfs_root *root)
1051d0c803c4SChris Mason {
1052663dfbb0SFilipe Manana 	int ret;
1053663dfbb0SFilipe Manana 
1054663dfbb0SFilipe Manana 	ret = btrfs_write_and_wait_marked_extents(root,
10558cef4e16SYan, Zheng 					   &trans->transaction->dirty_pages,
10568cef4e16SYan, Zheng 					   EXTENT_DIRTY);
1057663dfbb0SFilipe Manana 	clear_btree_io_tree(&trans->transaction->dirty_pages);
1058663dfbb0SFilipe Manana 
1059663dfbb0SFilipe Manana 	return ret;
1060d0c803c4SChris Mason }
1061d0c803c4SChris Mason 
1062d352ac68SChris Mason /*
1063d352ac68SChris Mason  * this is used to update the root pointer in the tree of tree roots.
1064d352ac68SChris Mason  *
1065d352ac68SChris Mason  * But, in the case of the extent allocation tree, updating the root
1066d352ac68SChris Mason  * pointer may allocate blocks which may change the root of the extent
1067d352ac68SChris Mason  * allocation tree.
1068d352ac68SChris Mason  *
1069d352ac68SChris Mason  * So, this loops and repeats and makes sure the cowonly root didn't
1070d352ac68SChris Mason  * change while the root pointer was being updated in the metadata.
1071d352ac68SChris Mason  */
10720b86a832SChris Mason static int update_cowonly_root(struct btrfs_trans_handle *trans,
107379154b1bSChris Mason 			       struct btrfs_root *root)
107479154b1bSChris Mason {
107579154b1bSChris Mason 	int ret;
10760b86a832SChris Mason 	u64 old_root_bytenr;
107786b9f2ecSYan, Zheng 	u64 old_root_used;
10780b86a832SChris Mason 	struct btrfs_root *tree_root = root->fs_info->tree_root;
107979154b1bSChris Mason 
108086b9f2ecSYan, Zheng 	old_root_used = btrfs_root_used(&root->root_item);
108156bec294SChris Mason 
108279154b1bSChris Mason 	while (1) {
10830b86a832SChris Mason 		old_root_bytenr = btrfs_root_bytenr(&root->root_item);
108486b9f2ecSYan, Zheng 		if (old_root_bytenr == root->node->start &&
1085ea526d18SJosef Bacik 		    old_root_used == btrfs_root_used(&root->root_item))
108679154b1bSChris Mason 			break;
108787ef2bb4SChris Mason 
10885d4f98a2SYan Zheng 		btrfs_set_root_node(&root->root_item, root->node);
108979154b1bSChris Mason 		ret = btrfs_update_root(trans, tree_root,
10900b86a832SChris Mason 					&root->root_key,
10910b86a832SChris Mason 					&root->root_item);
109249b25e05SJeff Mahoney 		if (ret)
109349b25e05SJeff Mahoney 			return ret;
109456bec294SChris Mason 
109586b9f2ecSYan, Zheng 		old_root_used = btrfs_root_used(&root->root_item);
1096e7070be1SJosef Bacik 	}
1097276e680dSYan Zheng 
10980b86a832SChris Mason 	return 0;
10990b86a832SChris Mason }
11000b86a832SChris Mason 
1101d352ac68SChris Mason /*
1102d352ac68SChris Mason  * update all the cowonly tree roots on disk
110349b25e05SJeff Mahoney  *
110449b25e05SJeff Mahoney  * The error handling in this function may not be obvious. Any of the
110549b25e05SJeff Mahoney  * failures will cause the file system to go offline. We still need
110649b25e05SJeff Mahoney  * to clean up the delayed refs.
1107d352ac68SChris Mason  */
11085d4f98a2SYan Zheng static noinline int commit_cowonly_roots(struct btrfs_trans_handle *trans,
11090b86a832SChris Mason 					 struct btrfs_root *root)
11100b86a832SChris Mason {
11110b86a832SChris Mason 	struct btrfs_fs_info *fs_info = root->fs_info;
1112ea526d18SJosef Bacik 	struct list_head *dirty_bgs = &trans->transaction->dirty_bgs;
11131bbc621eSChris Mason 	struct list_head *io_bgs = &trans->transaction->io_bgs;
11140b86a832SChris Mason 	struct list_head *next;
111584234f3aSYan Zheng 	struct extent_buffer *eb;
111656bec294SChris Mason 	int ret;
111784234f3aSYan Zheng 
111884234f3aSYan Zheng 	eb = btrfs_lock_root_node(fs_info->tree_root);
111949b25e05SJeff Mahoney 	ret = btrfs_cow_block(trans, fs_info->tree_root, eb, NULL,
112049b25e05SJeff Mahoney 			      0, &eb);
112184234f3aSYan Zheng 	btrfs_tree_unlock(eb);
112284234f3aSYan Zheng 	free_extent_buffer(eb);
11230b86a832SChris Mason 
112449b25e05SJeff Mahoney 	if (ret)
112549b25e05SJeff Mahoney 		return ret;
112649b25e05SJeff Mahoney 
112756bec294SChris Mason 	ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
112849b25e05SJeff Mahoney 	if (ret)
112949b25e05SJeff Mahoney 		return ret;
113087ef2bb4SChris Mason 
1131733f4fbbSStefan Behrens 	ret = btrfs_run_dev_stats(trans, root->fs_info);
1132c16ce190SJosef Bacik 	if (ret)
1133c16ce190SJosef Bacik 		return ret;
11348dabb742SStefan Behrens 	ret = btrfs_run_dev_replace(trans, root->fs_info);
1135c16ce190SJosef Bacik 	if (ret)
1136c16ce190SJosef Bacik 		return ret;
1137546adb0dSJan Schmidt 	ret = btrfs_run_qgroups(trans, root->fs_info);
1138c16ce190SJosef Bacik 	if (ret)
1139c16ce190SJosef Bacik 		return ret;
1140546adb0dSJan Schmidt 
1141dcdf7f6dSJosef Bacik 	ret = btrfs_setup_space_cache(trans, root);
1142dcdf7f6dSJosef Bacik 	if (ret)
1143dcdf7f6dSJosef Bacik 		return ret;
1144dcdf7f6dSJosef Bacik 
1145546adb0dSJan Schmidt 	/* run_qgroups might have added some more refs */
1146546adb0dSJan Schmidt 	ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
1147c16ce190SJosef Bacik 	if (ret)
1148c16ce190SJosef Bacik 		return ret;
1149ea526d18SJosef Bacik again:
11500b86a832SChris Mason 	while (!list_empty(&fs_info->dirty_cowonly_roots)) {
11510b86a832SChris Mason 		next = fs_info->dirty_cowonly_roots.next;
11520b86a832SChris Mason 		list_del_init(next);
11530b86a832SChris Mason 		root = list_entry(next, struct btrfs_root, dirty_list);
1154e7070be1SJosef Bacik 		clear_bit(BTRFS_ROOT_DIRTY, &root->state);
115587ef2bb4SChris Mason 
11569e351cc8SJosef Bacik 		if (root != fs_info->extent_root)
11579e351cc8SJosef Bacik 			list_add_tail(&root->dirty_list,
11589e351cc8SJosef Bacik 				      &trans->transaction->switch_commits);
115949b25e05SJeff Mahoney 		ret = update_cowonly_root(trans, root);
116049b25e05SJeff Mahoney 		if (ret)
116149b25e05SJeff Mahoney 			return ret;
1162ea526d18SJosef Bacik 		ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
1163ea526d18SJosef Bacik 		if (ret)
1164ea526d18SJosef Bacik 			return ret;
116579154b1bSChris Mason 	}
1166276e680dSYan Zheng 
11671bbc621eSChris Mason 	while (!list_empty(dirty_bgs) || !list_empty(io_bgs)) {
1168ea526d18SJosef Bacik 		ret = btrfs_write_dirty_block_groups(trans, root);
1169ea526d18SJosef Bacik 		if (ret)
1170ea526d18SJosef Bacik 			return ret;
1171ea526d18SJosef Bacik 		ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
1172ea526d18SJosef Bacik 		if (ret)
1173ea526d18SJosef Bacik 			return ret;
1174ea526d18SJosef Bacik 	}
1175ea526d18SJosef Bacik 
1176ea526d18SJosef Bacik 	if (!list_empty(&fs_info->dirty_cowonly_roots))
1177ea526d18SJosef Bacik 		goto again;
1178ea526d18SJosef Bacik 
11799e351cc8SJosef Bacik 	list_add_tail(&fs_info->extent_root->dirty_list,
11809e351cc8SJosef Bacik 		      &trans->transaction->switch_commits);
11818dabb742SStefan Behrens 	btrfs_after_dev_replace_commit(fs_info);
11828dabb742SStefan Behrens 
118379154b1bSChris Mason 	return 0;
118479154b1bSChris Mason }
118579154b1bSChris Mason 
1186d352ac68SChris Mason /*
1187d352ac68SChris Mason  * dead roots are old snapshots that need to be deleted.  This allocates
1188d352ac68SChris Mason  * a dirty root struct and adds it into the list of dead roots that need to
1189d352ac68SChris Mason  * be deleted
1190d352ac68SChris Mason  */
1191cfad392bSJosef Bacik void btrfs_add_dead_root(struct btrfs_root *root)
11925eda7b5eSChris Mason {
1193a4abeea4SJosef Bacik 	spin_lock(&root->fs_info->trans_lock);
1194cfad392bSJosef Bacik 	if (list_empty(&root->root_list))
11959d1a2a3aSDavid Sterba 		list_add_tail(&root->root_list, &root->fs_info->dead_roots);
1196a4abeea4SJosef Bacik 	spin_unlock(&root->fs_info->trans_lock);
11975eda7b5eSChris Mason }
11985eda7b5eSChris Mason 
1199d352ac68SChris Mason /*
12005d4f98a2SYan Zheng  * update all the cowonly tree roots on disk
1201d352ac68SChris Mason  */
12025d4f98a2SYan Zheng static noinline int commit_fs_roots(struct btrfs_trans_handle *trans,
12035d4f98a2SYan Zheng 				    struct btrfs_root *root)
12040f7d52f4SChris Mason {
12050f7d52f4SChris Mason 	struct btrfs_root *gang[8];
12065d4f98a2SYan Zheng 	struct btrfs_fs_info *fs_info = root->fs_info;
12070f7d52f4SChris Mason 	int i;
12080f7d52f4SChris Mason 	int ret;
120954aa1f4dSChris Mason 	int err = 0;
121054aa1f4dSChris Mason 
1211a4abeea4SJosef Bacik 	spin_lock(&fs_info->fs_roots_radix_lock);
12120f7d52f4SChris Mason 	while (1) {
12135d4f98a2SYan Zheng 		ret = radix_tree_gang_lookup_tag(&fs_info->fs_roots_radix,
12145d4f98a2SYan Zheng 						 (void **)gang, 0,
12150f7d52f4SChris Mason 						 ARRAY_SIZE(gang),
12160f7d52f4SChris Mason 						 BTRFS_ROOT_TRANS_TAG);
12170f7d52f4SChris Mason 		if (ret == 0)
12180f7d52f4SChris Mason 			break;
12190f7d52f4SChris Mason 		for (i = 0; i < ret; i++) {
12200f7d52f4SChris Mason 			root = gang[i];
12215d4f98a2SYan Zheng 			radix_tree_tag_clear(&fs_info->fs_roots_radix,
12222619ba1fSChris Mason 					(unsigned long)root->root_key.objectid,
12230f7d52f4SChris Mason 					BTRFS_ROOT_TRANS_TAG);
1224a4abeea4SJosef Bacik 			spin_unlock(&fs_info->fs_roots_radix_lock);
122531153d81SYan Zheng 
1226e02119d5SChris Mason 			btrfs_free_log(trans, root);
12275d4f98a2SYan Zheng 			btrfs_update_reloc_root(trans, root);
1228d68fc57bSYan, Zheng 			btrfs_orphan_commit_root(trans, root);
1229e02119d5SChris Mason 
123082d5902dSLi Zefan 			btrfs_save_ino_cache(root, trans);
123182d5902dSLi Zefan 
1232f1ebcc74SLiu Bo 			/* see comments in should_cow_block() */
123327cdeb70SMiao Xie 			clear_bit(BTRFS_ROOT_FORCE_COW, &root->state);
1234c7548af6SChris Mason 			smp_mb__after_atomic();
1235f1ebcc74SLiu Bo 
1236978d910dSYan Zheng 			if (root->commit_root != root->node) {
12379e351cc8SJosef Bacik 				list_add_tail(&root->dirty_list,
12389e351cc8SJosef Bacik 					&trans->transaction->switch_commits);
1239978d910dSYan Zheng 				btrfs_set_root_node(&root->root_item,
1240978d910dSYan Zheng 						    root->node);
1241978d910dSYan Zheng 			}
124231153d81SYan Zheng 
12435d4f98a2SYan Zheng 			err = btrfs_update_root(trans, fs_info->tree_root,
12440f7d52f4SChris Mason 						&root->root_key,
12450f7d52f4SChris Mason 						&root->root_item);
1246a4abeea4SJosef Bacik 			spin_lock(&fs_info->fs_roots_radix_lock);
124754aa1f4dSChris Mason 			if (err)
124854aa1f4dSChris Mason 				break;
12490f7d52f4SChris Mason 		}
12509f3a7427SChris Mason 	}
1251a4abeea4SJosef Bacik 	spin_unlock(&fs_info->fs_roots_radix_lock);
125254aa1f4dSChris Mason 	return err;
12530f7d52f4SChris Mason }
12540f7d52f4SChris Mason 
1255d352ac68SChris Mason /*
1256de78b51aSEric Sandeen  * defrag a given btree.
1257de78b51aSEric Sandeen  * Every leaf in the btree is read and defragged.
1258d352ac68SChris Mason  */
1259de78b51aSEric Sandeen int btrfs_defrag_root(struct btrfs_root *root)
1260e9d0b13bSChris Mason {
1261e9d0b13bSChris Mason 	struct btrfs_fs_info *info = root->fs_info;
1262e9d0b13bSChris Mason 	struct btrfs_trans_handle *trans;
12638929ecfaSYan, Zheng 	int ret;
1264e9d0b13bSChris Mason 
126527cdeb70SMiao Xie 	if (test_and_set_bit(BTRFS_ROOT_DEFRAG_RUNNING, &root->state))
1266e9d0b13bSChris Mason 		return 0;
12678929ecfaSYan, Zheng 
12686b80053dSChris Mason 	while (1) {
12698929ecfaSYan, Zheng 		trans = btrfs_start_transaction(root, 0);
12708929ecfaSYan, Zheng 		if (IS_ERR(trans))
12718929ecfaSYan, Zheng 			return PTR_ERR(trans);
12728929ecfaSYan, Zheng 
1273de78b51aSEric Sandeen 		ret = btrfs_defrag_leaves(trans, root);
12748929ecfaSYan, Zheng 
1275e9d0b13bSChris Mason 		btrfs_end_transaction(trans, root);
1276b53d3f5dSLiu Bo 		btrfs_btree_balance_dirty(info->tree_root);
1277e9d0b13bSChris Mason 		cond_resched();
1278e9d0b13bSChris Mason 
12797841cb28SDavid Sterba 		if (btrfs_fs_closing(root->fs_info) || ret != -EAGAIN)
1280e9d0b13bSChris Mason 			break;
1281210549ebSDavid Sterba 
1282210549ebSDavid Sterba 		if (btrfs_defrag_cancelled(root->fs_info)) {
1283efe120a0SFrank Holton 			pr_debug("BTRFS: defrag_root cancelled\n");
1284210549ebSDavid Sterba 			ret = -EAGAIN;
1285210549ebSDavid Sterba 			break;
1286210549ebSDavid Sterba 		}
1287e9d0b13bSChris Mason 	}
128827cdeb70SMiao Xie 	clear_bit(BTRFS_ROOT_DEFRAG_RUNNING, &root->state);
12898929ecfaSYan, Zheng 	return ret;
1290e9d0b13bSChris Mason }
1291e9d0b13bSChris Mason 
1292d352ac68SChris Mason /*
1293d352ac68SChris Mason  * new snapshots need to be created at a very specific time in the
1294aec8030aSMiao Xie  * transaction commit.  This does the actual creation.
1295aec8030aSMiao Xie  *
1296aec8030aSMiao Xie  * Note:
1297aec8030aSMiao Xie  * If the error which may affect the commitment of the current transaction
1298aec8030aSMiao Xie  * happens, we should return the error number. If the error which just affect
1299aec8030aSMiao Xie  * the creation of the pending snapshots, just return 0.
1300d352ac68SChris Mason  */
130180b6794dSChris Mason static noinline int create_pending_snapshot(struct btrfs_trans_handle *trans,
13023063d29fSChris Mason 				   struct btrfs_fs_info *fs_info,
13033063d29fSChris Mason 				   struct btrfs_pending_snapshot *pending)
13043063d29fSChris Mason {
13053063d29fSChris Mason 	struct btrfs_key key;
130680b6794dSChris Mason 	struct btrfs_root_item *new_root_item;
13073063d29fSChris Mason 	struct btrfs_root *tree_root = fs_info->tree_root;
13083063d29fSChris Mason 	struct btrfs_root *root = pending->root;
13096bdb72deSSage Weil 	struct btrfs_root *parent_root;
131098c9942aSLiu Bo 	struct btrfs_block_rsv *rsv;
13116bdb72deSSage Weil 	struct inode *parent_inode;
131242874b3dSMiao Xie 	struct btrfs_path *path;
131342874b3dSMiao Xie 	struct btrfs_dir_item *dir_item;
1314a22285a6SYan, Zheng 	struct dentry *dentry;
13153063d29fSChris Mason 	struct extent_buffer *tmp;
1316925baeddSChris Mason 	struct extent_buffer *old;
13178ea05e3aSAlexander Block 	struct timespec cur_time = CURRENT_TIME;
1318aec8030aSMiao Xie 	int ret = 0;
1319d68fc57bSYan, Zheng 	u64 to_reserve = 0;
13206bdb72deSSage Weil 	u64 index = 0;
1321a22285a6SYan, Zheng 	u64 objectid;
1322b83cc969SLi Zefan 	u64 root_flags;
13238ea05e3aSAlexander Block 	uuid_le new_uuid;
13243063d29fSChris Mason 
132542874b3dSMiao Xie 	path = btrfs_alloc_path();
132642874b3dSMiao Xie 	if (!path) {
1327aec8030aSMiao Xie 		pending->error = -ENOMEM;
1328aec8030aSMiao Xie 		return 0;
132942874b3dSMiao Xie 	}
133042874b3dSMiao Xie 
133180b6794dSChris Mason 	new_root_item = kmalloc(sizeof(*new_root_item), GFP_NOFS);
133280b6794dSChris Mason 	if (!new_root_item) {
1333aec8030aSMiao Xie 		pending->error = -ENOMEM;
13346fa9700eSMiao Xie 		goto root_item_alloc_fail;
133580b6794dSChris Mason 	}
1336a22285a6SYan, Zheng 
1337aec8030aSMiao Xie 	pending->error = btrfs_find_free_objectid(tree_root, &objectid);
1338aec8030aSMiao Xie 	if (pending->error)
13396fa9700eSMiao Xie 		goto no_free_objectid;
13403063d29fSChris Mason 
1341d6726335SQu Wenruo 	/*
1342d6726335SQu Wenruo 	 * Make qgroup to skip current new snapshot's qgroupid, as it is
1343d6726335SQu Wenruo 	 * accounted by later btrfs_qgroup_inherit().
1344d6726335SQu Wenruo 	 */
1345d6726335SQu Wenruo 	btrfs_set_skip_qgroup(trans, objectid);
1346d6726335SQu Wenruo 
1347147d256eSZhaolei 	btrfs_reloc_pre_snapshot(pending, &to_reserve);
1348d68fc57bSYan, Zheng 
1349d68fc57bSYan, Zheng 	if (to_reserve > 0) {
1350aec8030aSMiao Xie 		pending->error = btrfs_block_rsv_add(root,
1351aec8030aSMiao Xie 						     &pending->block_rsv,
135208e007d2SMiao Xie 						     to_reserve,
135308e007d2SMiao Xie 						     BTRFS_RESERVE_NO_FLUSH);
1354aec8030aSMiao Xie 		if (pending->error)
1355d6726335SQu Wenruo 			goto clear_skip_qgroup;
1356d68fc57bSYan, Zheng 	}
1357d68fc57bSYan, Zheng 
13583063d29fSChris Mason 	key.objectid = objectid;
1359a22285a6SYan, Zheng 	key.offset = (u64)-1;
1360a22285a6SYan, Zheng 	key.type = BTRFS_ROOT_ITEM_KEY;
13613063d29fSChris Mason 
13626fa9700eSMiao Xie 	rsv = trans->block_rsv;
1363a22285a6SYan, Zheng 	trans->block_rsv = &pending->block_rsv;
13642382c5ccSLiu Bo 	trans->bytes_reserved = trans->block_rsv->reserved;
13656bdb72deSSage Weil 
1366a22285a6SYan, Zheng 	dentry = pending->dentry;
1367e9662f70SMiao Xie 	parent_inode = pending->dir;
1368a22285a6SYan, Zheng 	parent_root = BTRFS_I(parent_inode)->root;
13697585717fSChris Mason 	record_root_in_trans(trans, parent_root);
1370a22285a6SYan, Zheng 
13716bdb72deSSage Weil 	/*
13726bdb72deSSage Weil 	 * insert the directory item
13736bdb72deSSage Weil 	 */
13746bdb72deSSage Weil 	ret = btrfs_set_inode_index(parent_inode, &index);
137549b25e05SJeff Mahoney 	BUG_ON(ret); /* -ENOMEM */
137642874b3dSMiao Xie 
137742874b3dSMiao Xie 	/* check if there is a file/dir which has the same name. */
137842874b3dSMiao Xie 	dir_item = btrfs_lookup_dir_item(NULL, parent_root, path,
137942874b3dSMiao Xie 					 btrfs_ino(parent_inode),
138042874b3dSMiao Xie 					 dentry->d_name.name,
138142874b3dSMiao Xie 					 dentry->d_name.len, 0);
138242874b3dSMiao Xie 	if (dir_item != NULL && !IS_ERR(dir_item)) {
1383fe66a05aSChris Mason 		pending->error = -EEXIST;
1384aec8030aSMiao Xie 		goto dir_item_existed;
138542874b3dSMiao Xie 	} else if (IS_ERR(dir_item)) {
138642874b3dSMiao Xie 		ret = PTR_ERR(dir_item);
13878732d44fSMiao Xie 		btrfs_abort_transaction(trans, root, ret);
13888732d44fSMiao Xie 		goto fail;
138979787eaaSJeff Mahoney 	}
139042874b3dSMiao Xie 	btrfs_release_path(path);
13916bdb72deSSage Weil 
1392e999376fSChris Mason 	/*
1393e999376fSChris Mason 	 * pull in the delayed directory update
1394e999376fSChris Mason 	 * and the delayed inode item
1395e999376fSChris Mason 	 * otherwise we corrupt the FS during
1396e999376fSChris Mason 	 * snapshot
1397e999376fSChris Mason 	 */
1398e999376fSChris Mason 	ret = btrfs_run_delayed_items(trans, root);
13998732d44fSMiao Xie 	if (ret) {	/* Transaction aborted */
14008732d44fSMiao Xie 		btrfs_abort_transaction(trans, root, ret);
14018732d44fSMiao Xie 		goto fail;
14028732d44fSMiao Xie 	}
1403e999376fSChris Mason 
14047585717fSChris Mason 	record_root_in_trans(trans, root);
14056bdb72deSSage Weil 	btrfs_set_root_last_snapshot(&root->root_item, trans->transid);
14066bdb72deSSage Weil 	memcpy(new_root_item, &root->root_item, sizeof(*new_root_item));
140708fe4db1SLi Zefan 	btrfs_check_and_init_root_item(new_root_item);
14086bdb72deSSage Weil 
1409b83cc969SLi Zefan 	root_flags = btrfs_root_flags(new_root_item);
1410b83cc969SLi Zefan 	if (pending->readonly)
1411b83cc969SLi Zefan 		root_flags |= BTRFS_ROOT_SUBVOL_RDONLY;
1412b83cc969SLi Zefan 	else
1413b83cc969SLi Zefan 		root_flags &= ~BTRFS_ROOT_SUBVOL_RDONLY;
1414b83cc969SLi Zefan 	btrfs_set_root_flags(new_root_item, root_flags);
1415b83cc969SLi Zefan 
14168ea05e3aSAlexander Block 	btrfs_set_root_generation_v2(new_root_item,
14178ea05e3aSAlexander Block 			trans->transid);
14188ea05e3aSAlexander Block 	uuid_le_gen(&new_uuid);
14198ea05e3aSAlexander Block 	memcpy(new_root_item->uuid, new_uuid.b, BTRFS_UUID_SIZE);
14208ea05e3aSAlexander Block 	memcpy(new_root_item->parent_uuid, root->root_item.uuid,
14218ea05e3aSAlexander Block 			BTRFS_UUID_SIZE);
142270023da2SStefan Behrens 	if (!(root_flags & BTRFS_ROOT_SUBVOL_RDONLY)) {
142370023da2SStefan Behrens 		memset(new_root_item->received_uuid, 0,
142470023da2SStefan Behrens 		       sizeof(new_root_item->received_uuid));
14258ea05e3aSAlexander Block 		memset(&new_root_item->stime, 0, sizeof(new_root_item->stime));
14268ea05e3aSAlexander Block 		memset(&new_root_item->rtime, 0, sizeof(new_root_item->rtime));
14278ea05e3aSAlexander Block 		btrfs_set_root_stransid(new_root_item, 0);
14288ea05e3aSAlexander Block 		btrfs_set_root_rtransid(new_root_item, 0);
142970023da2SStefan Behrens 	}
14303cae210fSQu Wenruo 	btrfs_set_stack_timespec_sec(&new_root_item->otime, cur_time.tv_sec);
14313cae210fSQu Wenruo 	btrfs_set_stack_timespec_nsec(&new_root_item->otime, cur_time.tv_nsec);
143270023da2SStefan Behrens 	btrfs_set_root_otransid(new_root_item, trans->transid);
14338ea05e3aSAlexander Block 
1434925baeddSChris Mason 	old = btrfs_lock_root_node(root);
143549b25e05SJeff Mahoney 	ret = btrfs_cow_block(trans, root, old, NULL, 0, &old);
143679787eaaSJeff Mahoney 	if (ret) {
143779787eaaSJeff Mahoney 		btrfs_tree_unlock(old);
143879787eaaSJeff Mahoney 		free_extent_buffer(old);
14398732d44fSMiao Xie 		btrfs_abort_transaction(trans, root, ret);
14408732d44fSMiao Xie 		goto fail;
144179787eaaSJeff Mahoney 	}
144249b25e05SJeff Mahoney 
14435d4f98a2SYan Zheng 	btrfs_set_lock_blocking(old);
14443063d29fSChris Mason 
144549b25e05SJeff Mahoney 	ret = btrfs_copy_root(trans, root, old, &tmp, objectid);
144679787eaaSJeff Mahoney 	/* clean up in any case */
1447925baeddSChris Mason 	btrfs_tree_unlock(old);
1448925baeddSChris Mason 	free_extent_buffer(old);
14498732d44fSMiao Xie 	if (ret) {
14508732d44fSMiao Xie 		btrfs_abort_transaction(trans, root, ret);
14518732d44fSMiao Xie 		goto fail;
14528732d44fSMiao Xie 	}
1453f1ebcc74SLiu Bo 	/* see comments in should_cow_block() */
145427cdeb70SMiao Xie 	set_bit(BTRFS_ROOT_FORCE_COW, &root->state);
1455f1ebcc74SLiu Bo 	smp_wmb();
1456f1ebcc74SLiu Bo 
14575d4f98a2SYan Zheng 	btrfs_set_root_node(new_root_item, tmp);
1458a22285a6SYan, Zheng 	/* record when the snapshot was created in key.offset */
1459a22285a6SYan, Zheng 	key.offset = trans->transid;
1460a22285a6SYan, Zheng 	ret = btrfs_insert_root(trans, tree_root, &key, new_root_item);
1461925baeddSChris Mason 	btrfs_tree_unlock(tmp);
14623063d29fSChris Mason 	free_extent_buffer(tmp);
14638732d44fSMiao Xie 	if (ret) {
14648732d44fSMiao Xie 		btrfs_abort_transaction(trans, root, ret);
14658732d44fSMiao Xie 		goto fail;
14668732d44fSMiao Xie 	}
14670660b5afSChris Mason 
1468a22285a6SYan, Zheng 	/*
1469a22285a6SYan, Zheng 	 * insert root back/forward references
1470a22285a6SYan, Zheng 	 */
1471a22285a6SYan, Zheng 	ret = btrfs_add_root_ref(trans, tree_root, objectid,
1472a22285a6SYan, Zheng 				 parent_root->root_key.objectid,
147333345d01SLi Zefan 				 btrfs_ino(parent_inode), index,
1474a22285a6SYan, Zheng 				 dentry->d_name.name, dentry->d_name.len);
14758732d44fSMiao Xie 	if (ret) {
14768732d44fSMiao Xie 		btrfs_abort_transaction(trans, root, ret);
14778732d44fSMiao Xie 		goto fail;
14788732d44fSMiao Xie 	}
1479a22285a6SYan, Zheng 
1480a22285a6SYan, Zheng 	key.offset = (u64)-1;
1481a22285a6SYan, Zheng 	pending->snap = btrfs_read_fs_root_no_name(root->fs_info, &key);
148279787eaaSJeff Mahoney 	if (IS_ERR(pending->snap)) {
148379787eaaSJeff Mahoney 		ret = PTR_ERR(pending->snap);
14848732d44fSMiao Xie 		btrfs_abort_transaction(trans, root, ret);
14858732d44fSMiao Xie 		goto fail;
148679787eaaSJeff Mahoney 	}
1487d68fc57bSYan, Zheng 
148849b25e05SJeff Mahoney 	ret = btrfs_reloc_post_snapshot(trans, pending);
14898732d44fSMiao Xie 	if (ret) {
14908732d44fSMiao Xie 		btrfs_abort_transaction(trans, root, ret);
14918732d44fSMiao Xie 		goto fail;
14928732d44fSMiao Xie 	}
1493361048f5SMiao Xie 
1494361048f5SMiao Xie 	ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
14958732d44fSMiao Xie 	if (ret) {
14968732d44fSMiao Xie 		btrfs_abort_transaction(trans, root, ret);
14978732d44fSMiao Xie 		goto fail;
14988732d44fSMiao Xie 	}
149942874b3dSMiao Xie 
150042874b3dSMiao Xie 	ret = btrfs_insert_dir_item(trans, parent_root,
150142874b3dSMiao Xie 				    dentry->d_name.name, dentry->d_name.len,
150242874b3dSMiao Xie 				    parent_inode, &key,
150342874b3dSMiao Xie 				    BTRFS_FT_DIR, index);
150442874b3dSMiao Xie 	/* We have check then name at the beginning, so it is impossible. */
15059c52057cSChris Mason 	BUG_ON(ret == -EEXIST || ret == -EOVERFLOW);
15068732d44fSMiao Xie 	if (ret) {
15078732d44fSMiao Xie 		btrfs_abort_transaction(trans, root, ret);
15088732d44fSMiao Xie 		goto fail;
15098732d44fSMiao Xie 	}
151042874b3dSMiao Xie 
151142874b3dSMiao Xie 	btrfs_i_size_write(parent_inode, parent_inode->i_size +
151242874b3dSMiao Xie 					 dentry->d_name.len * 2);
151342874b3dSMiao Xie 	parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
1514be6aef60SJosef Bacik 	ret = btrfs_update_inode_fallback(trans, parent_root, parent_inode);
1515dd5f9615SStefan Behrens 	if (ret) {
15168732d44fSMiao Xie 		btrfs_abort_transaction(trans, root, ret);
1517dd5f9615SStefan Behrens 		goto fail;
1518dd5f9615SStefan Behrens 	}
1519dd5f9615SStefan Behrens 	ret = btrfs_uuid_tree_add(trans, fs_info->uuid_root, new_uuid.b,
1520dd5f9615SStefan Behrens 				  BTRFS_UUID_KEY_SUBVOL, objectid);
1521dd5f9615SStefan Behrens 	if (ret) {
1522dd5f9615SStefan Behrens 		btrfs_abort_transaction(trans, root, ret);
1523dd5f9615SStefan Behrens 		goto fail;
1524dd5f9615SStefan Behrens 	}
1525dd5f9615SStefan Behrens 	if (!btrfs_is_empty_uuid(new_root_item->received_uuid)) {
1526dd5f9615SStefan Behrens 		ret = btrfs_uuid_tree_add(trans, fs_info->uuid_root,
1527dd5f9615SStefan Behrens 					  new_root_item->received_uuid,
1528dd5f9615SStefan Behrens 					  BTRFS_UUID_KEY_RECEIVED_SUBVOL,
1529dd5f9615SStefan Behrens 					  objectid);
1530dd5f9615SStefan Behrens 		if (ret && ret != -EEXIST) {
1531dd5f9615SStefan Behrens 			btrfs_abort_transaction(trans, root, ret);
1532dd5f9615SStefan Behrens 			goto fail;
1533dd5f9615SStefan Behrens 		}
1534dd5f9615SStefan Behrens 	}
1535d6726335SQu Wenruo 
1536d6726335SQu Wenruo 	ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
1537d6726335SQu Wenruo 	if (ret) {
1538d6726335SQu Wenruo 		btrfs_abort_transaction(trans, root, ret);
1539d6726335SQu Wenruo 		goto fail;
1540d6726335SQu Wenruo 	}
1541d6726335SQu Wenruo 
1542d6726335SQu Wenruo 	/*
1543d6726335SQu Wenruo 	 * account qgroup counters before qgroup_inherit()
1544d6726335SQu Wenruo 	 */
1545d6726335SQu Wenruo 	ret = btrfs_qgroup_prepare_account_extents(trans, fs_info);
1546d6726335SQu Wenruo 	if (ret)
1547d6726335SQu Wenruo 		goto fail;
1548d6726335SQu Wenruo 	ret = btrfs_qgroup_account_extents(trans, fs_info);
1549d6726335SQu Wenruo 	if (ret)
1550d6726335SQu Wenruo 		goto fail;
1551d6726335SQu Wenruo 	ret = btrfs_qgroup_inherit(trans, fs_info,
1552d6726335SQu Wenruo 				   root->root_key.objectid,
1553d6726335SQu Wenruo 				   objectid, pending->inherit);
1554d6726335SQu Wenruo 	if (ret) {
1555d6726335SQu Wenruo 		btrfs_abort_transaction(trans, root, ret);
1556d6726335SQu Wenruo 		goto fail;
1557d6726335SQu Wenruo 	}
1558d6726335SQu Wenruo 
15593063d29fSChris Mason fail:
1560aec8030aSMiao Xie 	pending->error = ret;
1561aec8030aSMiao Xie dir_item_existed:
156298c9942aSLiu Bo 	trans->block_rsv = rsv;
15632382c5ccSLiu Bo 	trans->bytes_reserved = 0;
1564d6726335SQu Wenruo clear_skip_qgroup:
1565d6726335SQu Wenruo 	btrfs_clear_skip_qgroup(trans);
15666fa9700eSMiao Xie no_free_objectid:
15676fa9700eSMiao Xie 	kfree(new_root_item);
15686fa9700eSMiao Xie root_item_alloc_fail:
156942874b3dSMiao Xie 	btrfs_free_path(path);
157049b25e05SJeff Mahoney 	return ret;
15713063d29fSChris Mason }
15723063d29fSChris Mason 
1573d352ac68SChris Mason /*
1574d352ac68SChris Mason  * create all the snapshots we've scheduled for creation
1575d352ac68SChris Mason  */
157680b6794dSChris Mason static noinline int create_pending_snapshots(struct btrfs_trans_handle *trans,
15773063d29fSChris Mason 					     struct btrfs_fs_info *fs_info)
15783063d29fSChris Mason {
1579aec8030aSMiao Xie 	struct btrfs_pending_snapshot *pending, *next;
15803063d29fSChris Mason 	struct list_head *head = &trans->transaction->pending_snapshots;
1581aec8030aSMiao Xie 	int ret = 0;
15823de4586cSChris Mason 
1583aec8030aSMiao Xie 	list_for_each_entry_safe(pending, next, head, list) {
1584aec8030aSMiao Xie 		list_del(&pending->list);
1585aec8030aSMiao Xie 		ret = create_pending_snapshot(trans, fs_info, pending);
1586aec8030aSMiao Xie 		if (ret)
1587aec8030aSMiao Xie 			break;
1588aec8030aSMiao Xie 	}
1589aec8030aSMiao Xie 	return ret;
15903de4586cSChris Mason }
15913de4586cSChris Mason 
15925d4f98a2SYan Zheng static void update_super_roots(struct btrfs_root *root)
15935d4f98a2SYan Zheng {
15945d4f98a2SYan Zheng 	struct btrfs_root_item *root_item;
15955d4f98a2SYan Zheng 	struct btrfs_super_block *super;
15965d4f98a2SYan Zheng 
15976c41761fSDavid Sterba 	super = root->fs_info->super_copy;
15985d4f98a2SYan Zheng 
15995d4f98a2SYan Zheng 	root_item = &root->fs_info->chunk_root->root_item;
16005d4f98a2SYan Zheng 	super->chunk_root = root_item->bytenr;
16015d4f98a2SYan Zheng 	super->chunk_root_generation = root_item->generation;
16025d4f98a2SYan Zheng 	super->chunk_root_level = root_item->level;
16035d4f98a2SYan Zheng 
16045d4f98a2SYan Zheng 	root_item = &root->fs_info->tree_root->root_item;
16055d4f98a2SYan Zheng 	super->root = root_item->bytenr;
16065d4f98a2SYan Zheng 	super->generation = root_item->generation;
16075d4f98a2SYan Zheng 	super->root_level = root_item->level;
160873bc1876SJosef Bacik 	if (btrfs_test_opt(root, SPACE_CACHE))
16090af3d00bSJosef Bacik 		super->cache_generation = root_item->generation;
161070f80175SStefan Behrens 	if (root->fs_info->update_uuid_tree_gen)
161126432799SStefan Behrens 		super->uuid_tree_generation = root_item->generation;
16125d4f98a2SYan Zheng }
16135d4f98a2SYan Zheng 
1614f36f3042SChris Mason int btrfs_transaction_in_commit(struct btrfs_fs_info *info)
1615f36f3042SChris Mason {
16164a9d8bdeSMiao Xie 	struct btrfs_transaction *trans;
1617f36f3042SChris Mason 	int ret = 0;
16184a9d8bdeSMiao Xie 
1619a4abeea4SJosef Bacik 	spin_lock(&info->trans_lock);
16204a9d8bdeSMiao Xie 	trans = info->running_transaction;
16214a9d8bdeSMiao Xie 	if (trans)
16224a9d8bdeSMiao Xie 		ret = (trans->state >= TRANS_STATE_COMMIT_START);
1623a4abeea4SJosef Bacik 	spin_unlock(&info->trans_lock);
1624f36f3042SChris Mason 	return ret;
1625f36f3042SChris Mason }
1626f36f3042SChris Mason 
16278929ecfaSYan, Zheng int btrfs_transaction_blocked(struct btrfs_fs_info *info)
16288929ecfaSYan, Zheng {
16294a9d8bdeSMiao Xie 	struct btrfs_transaction *trans;
16308929ecfaSYan, Zheng 	int ret = 0;
16314a9d8bdeSMiao Xie 
1632a4abeea4SJosef Bacik 	spin_lock(&info->trans_lock);
16334a9d8bdeSMiao Xie 	trans = info->running_transaction;
16344a9d8bdeSMiao Xie 	if (trans)
16354a9d8bdeSMiao Xie 		ret = is_transaction_blocked(trans);
1636a4abeea4SJosef Bacik 	spin_unlock(&info->trans_lock);
16378929ecfaSYan, Zheng 	return ret;
16388929ecfaSYan, Zheng }
16398929ecfaSYan, Zheng 
1640bb9c12c9SSage Weil /*
1641bb9c12c9SSage Weil  * wait for the current transaction commit to start and block subsequent
1642bb9c12c9SSage Weil  * transaction joins
1643bb9c12c9SSage Weil  */
1644bb9c12c9SSage Weil static void wait_current_trans_commit_start(struct btrfs_root *root,
1645bb9c12c9SSage Weil 					    struct btrfs_transaction *trans)
1646bb9c12c9SSage Weil {
16474a9d8bdeSMiao Xie 	wait_event(root->fs_info->transaction_blocked_wait,
1648501407aaSJosef Bacik 		   trans->state >= TRANS_STATE_COMMIT_START ||
1649501407aaSJosef Bacik 		   trans->aborted);
1650bb9c12c9SSage Weil }
1651bb9c12c9SSage Weil 
1652bb9c12c9SSage Weil /*
1653bb9c12c9SSage Weil  * wait for the current transaction to start and then become unblocked.
1654bb9c12c9SSage Weil  * caller holds ref.
1655bb9c12c9SSage Weil  */
1656bb9c12c9SSage Weil static void wait_current_trans_commit_start_and_unblock(struct btrfs_root *root,
1657bb9c12c9SSage Weil 					 struct btrfs_transaction *trans)
1658bb9c12c9SSage Weil {
165972d63ed6SLi Zefan 	wait_event(root->fs_info->transaction_wait,
1660501407aaSJosef Bacik 		   trans->state >= TRANS_STATE_UNBLOCKED ||
1661501407aaSJosef Bacik 		   trans->aborted);
1662bb9c12c9SSage Weil }
1663bb9c12c9SSage Weil 
1664bb9c12c9SSage Weil /*
1665bb9c12c9SSage Weil  * commit transactions asynchronously. once btrfs_commit_transaction_async
1666bb9c12c9SSage Weil  * returns, any subsequent transaction will not be allowed to join.
1667bb9c12c9SSage Weil  */
1668bb9c12c9SSage Weil struct btrfs_async_commit {
1669bb9c12c9SSage Weil 	struct btrfs_trans_handle *newtrans;
1670bb9c12c9SSage Weil 	struct btrfs_root *root;
16717892b5afSMiao Xie 	struct work_struct work;
1672bb9c12c9SSage Weil };
1673bb9c12c9SSage Weil 
1674bb9c12c9SSage Weil static void do_async_commit(struct work_struct *work)
1675bb9c12c9SSage Weil {
1676bb9c12c9SSage Weil 	struct btrfs_async_commit *ac =
16777892b5afSMiao Xie 		container_of(work, struct btrfs_async_commit, work);
1678bb9c12c9SSage Weil 
16796fc4e354SSage Weil 	/*
16806fc4e354SSage Weil 	 * We've got freeze protection passed with the transaction.
16816fc4e354SSage Weil 	 * Tell lockdep about it.
16826fc4e354SSage Weil 	 */
1683b1a06a4bSLiu Bo 	if (ac->newtrans->type & __TRANS_FREEZABLE)
1684bee9182dSOleg Nesterov 		__sb_writers_acquired(ac->root->fs_info->sb, SB_FREEZE_FS);
16856fc4e354SSage Weil 
1686e209db7aSSage Weil 	current->journal_info = ac->newtrans;
1687e209db7aSSage Weil 
1688bb9c12c9SSage Weil 	btrfs_commit_transaction(ac->newtrans, ac->root);
1689bb9c12c9SSage Weil 	kfree(ac);
1690bb9c12c9SSage Weil }
1691bb9c12c9SSage Weil 
1692bb9c12c9SSage Weil int btrfs_commit_transaction_async(struct btrfs_trans_handle *trans,
1693bb9c12c9SSage Weil 				   struct btrfs_root *root,
1694bb9c12c9SSage Weil 				   int wait_for_unblock)
1695bb9c12c9SSage Weil {
1696bb9c12c9SSage Weil 	struct btrfs_async_commit *ac;
1697bb9c12c9SSage Weil 	struct btrfs_transaction *cur_trans;
1698bb9c12c9SSage Weil 
1699bb9c12c9SSage Weil 	ac = kmalloc(sizeof(*ac), GFP_NOFS);
1700db5b493aSTsutomu Itoh 	if (!ac)
1701db5b493aSTsutomu Itoh 		return -ENOMEM;
1702bb9c12c9SSage Weil 
17037892b5afSMiao Xie 	INIT_WORK(&ac->work, do_async_commit);
1704bb9c12c9SSage Weil 	ac->root = root;
17057a7eaa40SJosef Bacik 	ac->newtrans = btrfs_join_transaction(root);
17063612b495STsutomu Itoh 	if (IS_ERR(ac->newtrans)) {
17073612b495STsutomu Itoh 		int err = PTR_ERR(ac->newtrans);
17083612b495STsutomu Itoh 		kfree(ac);
17093612b495STsutomu Itoh 		return err;
17103612b495STsutomu Itoh 	}
1711bb9c12c9SSage Weil 
1712bb9c12c9SSage Weil 	/* take transaction reference */
1713bb9c12c9SSage Weil 	cur_trans = trans->transaction;
171413c5a93eSJosef Bacik 	atomic_inc(&cur_trans->use_count);
1715bb9c12c9SSage Weil 
1716bb9c12c9SSage Weil 	btrfs_end_transaction(trans, root);
17176fc4e354SSage Weil 
17186fc4e354SSage Weil 	/*
17196fc4e354SSage Weil 	 * Tell lockdep we've released the freeze rwsem, since the
17206fc4e354SSage Weil 	 * async commit thread will be the one to unlock it.
17216fc4e354SSage Weil 	 */
1722b1a06a4bSLiu Bo 	if (ac->newtrans->type & __TRANS_FREEZABLE)
1723bee9182dSOleg Nesterov 		__sb_writers_release(root->fs_info->sb, SB_FREEZE_FS);
17246fc4e354SSage Weil 
17257892b5afSMiao Xie 	schedule_work(&ac->work);
1726bb9c12c9SSage Weil 
1727bb9c12c9SSage Weil 	/* wait for transaction to start and unblock */
1728bb9c12c9SSage Weil 	if (wait_for_unblock)
1729bb9c12c9SSage Weil 		wait_current_trans_commit_start_and_unblock(root, cur_trans);
1730bb9c12c9SSage Weil 	else
1731bb9c12c9SSage Weil 		wait_current_trans_commit_start(root, cur_trans);
1732bb9c12c9SSage Weil 
173338e88054SSage Weil 	if (current->journal_info == trans)
173438e88054SSage Weil 		current->journal_info = NULL;
173538e88054SSage Weil 
1736724e2315SJosef Bacik 	btrfs_put_transaction(cur_trans);
1737bb9c12c9SSage Weil 	return 0;
1738bb9c12c9SSage Weil }
1739bb9c12c9SSage Weil 
174049b25e05SJeff Mahoney 
174149b25e05SJeff Mahoney static void cleanup_transaction(struct btrfs_trans_handle *trans,
17427b8b92afSJosef Bacik 				struct btrfs_root *root, int err)
174349b25e05SJeff Mahoney {
174449b25e05SJeff Mahoney 	struct btrfs_transaction *cur_trans = trans->transaction;
1745f094ac32SLiu Bo 	DEFINE_WAIT(wait);
174649b25e05SJeff Mahoney 
174749b25e05SJeff Mahoney 	WARN_ON(trans->use_count > 1);
174849b25e05SJeff Mahoney 
17497b8b92afSJosef Bacik 	btrfs_abort_transaction(trans, root, err);
17507b8b92afSJosef Bacik 
175149b25e05SJeff Mahoney 	spin_lock(&root->fs_info->trans_lock);
175266b6135bSLiu Bo 
175325d8c284SMiao Xie 	/*
175425d8c284SMiao Xie 	 * If the transaction is removed from the list, it means this
175525d8c284SMiao Xie 	 * transaction has been committed successfully, so it is impossible
175625d8c284SMiao Xie 	 * to call the cleanup function.
175725d8c284SMiao Xie 	 */
175825d8c284SMiao Xie 	BUG_ON(list_empty(&cur_trans->list));
175966b6135bSLiu Bo 
176049b25e05SJeff Mahoney 	list_del_init(&cur_trans->list);
1761d7096fc3SJosef Bacik 	if (cur_trans == root->fs_info->running_transaction) {
17624a9d8bdeSMiao Xie 		cur_trans->state = TRANS_STATE_COMMIT_DOING;
1763f094ac32SLiu Bo 		spin_unlock(&root->fs_info->trans_lock);
1764f094ac32SLiu Bo 		wait_event(cur_trans->writer_wait,
1765f094ac32SLiu Bo 			   atomic_read(&cur_trans->num_writers) == 1);
1766f094ac32SLiu Bo 
1767f094ac32SLiu Bo 		spin_lock(&root->fs_info->trans_lock);
1768d7096fc3SJosef Bacik 	}
176949b25e05SJeff Mahoney 	spin_unlock(&root->fs_info->trans_lock);
177049b25e05SJeff Mahoney 
177149b25e05SJeff Mahoney 	btrfs_cleanup_one_transaction(trans->transaction, root);
177249b25e05SJeff Mahoney 
17734a9d8bdeSMiao Xie 	spin_lock(&root->fs_info->trans_lock);
17744a9d8bdeSMiao Xie 	if (cur_trans == root->fs_info->running_transaction)
17754a9d8bdeSMiao Xie 		root->fs_info->running_transaction = NULL;
17764a9d8bdeSMiao Xie 	spin_unlock(&root->fs_info->trans_lock);
17774a9d8bdeSMiao Xie 
1778e0228285SJosef Bacik 	if (trans->type & __TRANS_FREEZABLE)
1779e0228285SJosef Bacik 		sb_end_intwrite(root->fs_info->sb);
1780724e2315SJosef Bacik 	btrfs_put_transaction(cur_trans);
1781724e2315SJosef Bacik 	btrfs_put_transaction(cur_trans);
178249b25e05SJeff Mahoney 
178349b25e05SJeff Mahoney 	trace_btrfs_transaction_commit(root);
178449b25e05SJeff Mahoney 
178549b25e05SJeff Mahoney 	if (current->journal_info == trans)
178649b25e05SJeff Mahoney 		current->journal_info = NULL;
1787c0af8f0bSWang Shilong 	btrfs_scrub_cancel(root->fs_info);
178849b25e05SJeff Mahoney 
178949b25e05SJeff Mahoney 	kmem_cache_free(btrfs_trans_handle_cachep, trans);
179049b25e05SJeff Mahoney }
179149b25e05SJeff Mahoney 
179282436617SMiao Xie static inline int btrfs_start_delalloc_flush(struct btrfs_fs_info *fs_info)
179382436617SMiao Xie {
179482436617SMiao Xie 	if (btrfs_test_opt(fs_info->tree_root, FLUSHONCOMMIT))
17956c255e67SMiao Xie 		return btrfs_start_delalloc_roots(fs_info, 1, -1);
179682436617SMiao Xie 	return 0;
179782436617SMiao Xie }
179882436617SMiao Xie 
179982436617SMiao Xie static inline void btrfs_wait_delalloc_flush(struct btrfs_fs_info *fs_info)
180082436617SMiao Xie {
180182436617SMiao Xie 	if (btrfs_test_opt(fs_info->tree_root, FLUSHONCOMMIT))
1802b0244199SMiao Xie 		btrfs_wait_ordered_roots(fs_info, -1);
180382436617SMiao Xie }
180482436617SMiao Xie 
180550d9aa99SJosef Bacik static inline void
180650d9aa99SJosef Bacik btrfs_wait_pending_ordered(struct btrfs_transaction *cur_trans,
180750d9aa99SJosef Bacik 			   struct btrfs_fs_info *fs_info)
180850d9aa99SJosef Bacik {
180950d9aa99SJosef Bacik 	struct btrfs_ordered_extent *ordered;
181050d9aa99SJosef Bacik 
181150d9aa99SJosef Bacik 	spin_lock(&fs_info->trans_lock);
181250d9aa99SJosef Bacik 	while (!list_empty(&cur_trans->pending_ordered)) {
181350d9aa99SJosef Bacik 		ordered = list_first_entry(&cur_trans->pending_ordered,
181450d9aa99SJosef Bacik 					   struct btrfs_ordered_extent,
181550d9aa99SJosef Bacik 					   trans_list);
181650d9aa99SJosef Bacik 		list_del_init(&ordered->trans_list);
181750d9aa99SJosef Bacik 		spin_unlock(&fs_info->trans_lock);
181850d9aa99SJosef Bacik 
181950d9aa99SJosef Bacik 		wait_event(ordered->wait, test_bit(BTRFS_ORDERED_COMPLETE,
182050d9aa99SJosef Bacik 						   &ordered->flags));
182150d9aa99SJosef Bacik 		btrfs_put_ordered_extent(ordered);
182250d9aa99SJosef Bacik 		spin_lock(&fs_info->trans_lock);
182350d9aa99SJosef Bacik 	}
182450d9aa99SJosef Bacik 	spin_unlock(&fs_info->trans_lock);
182550d9aa99SJosef Bacik }
182650d9aa99SJosef Bacik 
182779154b1bSChris Mason int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
182879154b1bSChris Mason 			     struct btrfs_root *root)
182979154b1bSChris Mason {
183049b25e05SJeff Mahoney 	struct btrfs_transaction *cur_trans = trans->transaction;
18318fd17795SChris Mason 	struct btrfs_transaction *prev_trans = NULL;
1832656f30dbSFilipe Manana 	struct btrfs_inode *btree_ino = BTRFS_I(root->fs_info->btree_inode);
183325287e0aSMiao Xie 	int ret;
183479154b1bSChris Mason 
18358d25a086SMiao Xie 	/* Stop the commit early if ->aborted is set */
18368d25a086SMiao Xie 	if (unlikely(ACCESS_ONCE(cur_trans->aborted))) {
183725287e0aSMiao Xie 		ret = cur_trans->aborted;
1838e4a2bcacSJosef Bacik 		btrfs_end_transaction(trans, root);
1839e4a2bcacSJosef Bacik 		return ret;
184025287e0aSMiao Xie 	}
184149b25e05SJeff Mahoney 
184256bec294SChris Mason 	/* make a pass through all the delayed refs we have so far
184356bec294SChris Mason 	 * any runnings procs may add more while we are here
184456bec294SChris Mason 	 */
184556bec294SChris Mason 	ret = btrfs_run_delayed_refs(trans, root, 0);
1846e4a2bcacSJosef Bacik 	if (ret) {
1847e4a2bcacSJosef Bacik 		btrfs_end_transaction(trans, root);
1848e4a2bcacSJosef Bacik 		return ret;
1849e4a2bcacSJosef Bacik 	}
185056bec294SChris Mason 
18510e721106SJosef Bacik 	btrfs_trans_release_metadata(trans, root);
18520e721106SJosef Bacik 	trans->block_rsv = NULL;
1853272d26d0SMiao Xie 	if (trans->qgroup_reserved) {
1854272d26d0SMiao Xie 		btrfs_qgroup_free(root, trans->qgroup_reserved);
1855272d26d0SMiao Xie 		trans->qgroup_reserved = 0;
1856272d26d0SMiao Xie 	}
18570e721106SJosef Bacik 
1858b7ec40d7SChris Mason 	cur_trans = trans->transaction;
185949b25e05SJeff Mahoney 
186056bec294SChris Mason 	/*
186156bec294SChris Mason 	 * set the flushing flag so procs in this transaction have to
186256bec294SChris Mason 	 * start sending their work down.
186356bec294SChris Mason 	 */
1864b7ec40d7SChris Mason 	cur_trans->delayed_refs.flushing = 1;
18651be41b78SJosef Bacik 	smp_wmb();
186656bec294SChris Mason 
1867ea658badSJosef Bacik 	if (!list_empty(&trans->new_bgs))
1868ea658badSJosef Bacik 		btrfs_create_pending_block_groups(trans, root);
1869ea658badSJosef Bacik 
1870c3e69d58SChris Mason 	ret = btrfs_run_delayed_refs(trans, root, 0);
1871e4a2bcacSJosef Bacik 	if (ret) {
1872e4a2bcacSJosef Bacik 		btrfs_end_transaction(trans, root);
1873e4a2bcacSJosef Bacik 		return ret;
1874e4a2bcacSJosef Bacik 	}
187556bec294SChris Mason 
18761bbc621eSChris Mason 	if (!cur_trans->dirty_bg_run) {
18771bbc621eSChris Mason 		int run_it = 0;
18781bbc621eSChris Mason 
18791bbc621eSChris Mason 		/* this mutex is also taken before trying to set
18801bbc621eSChris Mason 		 * block groups readonly.  We need to make sure
18811bbc621eSChris Mason 		 * that nobody has set a block group readonly
18821bbc621eSChris Mason 		 * after a extents from that block group have been
18831bbc621eSChris Mason 		 * allocated for cache files.  btrfs_set_block_group_ro
18841bbc621eSChris Mason 		 * will wait for the transaction to commit if it
18851bbc621eSChris Mason 		 * finds dirty_bg_run = 1
18861bbc621eSChris Mason 		 *
18871bbc621eSChris Mason 		 * The dirty_bg_run flag is also used to make sure only
18881bbc621eSChris Mason 		 * one process starts all the block group IO.  It wouldn't
18891bbc621eSChris Mason 		 * hurt to have more than one go through, but there's no
18901bbc621eSChris Mason 		 * real advantage to it either.
18911bbc621eSChris Mason 		 */
18921bbc621eSChris Mason 		mutex_lock(&root->fs_info->ro_block_group_mutex);
18931bbc621eSChris Mason 		if (!cur_trans->dirty_bg_run) {
18941bbc621eSChris Mason 			run_it = 1;
18951bbc621eSChris Mason 			cur_trans->dirty_bg_run = 1;
18961bbc621eSChris Mason 		}
18971bbc621eSChris Mason 		mutex_unlock(&root->fs_info->ro_block_group_mutex);
18981bbc621eSChris Mason 
18991bbc621eSChris Mason 		if (run_it)
19001bbc621eSChris Mason 			ret = btrfs_start_dirty_block_groups(trans, root);
19011bbc621eSChris Mason 	}
19021bbc621eSChris Mason 	if (ret) {
19031bbc621eSChris Mason 		btrfs_end_transaction(trans, root);
19041bbc621eSChris Mason 		return ret;
19051bbc621eSChris Mason 	}
19061bbc621eSChris Mason 
19074a9d8bdeSMiao Xie 	spin_lock(&root->fs_info->trans_lock);
1908d3efe084SFilipe Manana 	list_splice_init(&trans->ordered, &cur_trans->pending_ordered);
19094a9d8bdeSMiao Xie 	if (cur_trans->state >= TRANS_STATE_COMMIT_START) {
19104a9d8bdeSMiao Xie 		spin_unlock(&root->fs_info->trans_lock);
191113c5a93eSJosef Bacik 		atomic_inc(&cur_trans->use_count);
191249b25e05SJeff Mahoney 		ret = btrfs_end_transaction(trans, root);
1913ccd467d6SChris Mason 
1914b9c8300cSLi Zefan 		wait_for_commit(root, cur_trans);
191515ee9bc7SJosef Bacik 
1916b4924a0fSLiu Bo 		if (unlikely(cur_trans->aborted))
1917b4924a0fSLiu Bo 			ret = cur_trans->aborted;
1918b4924a0fSLiu Bo 
1919724e2315SJosef Bacik 		btrfs_put_transaction(cur_trans);
192015ee9bc7SJosef Bacik 
192149b25e05SJeff Mahoney 		return ret;
192279154b1bSChris Mason 	}
19234313b399SChris Mason 
19244a9d8bdeSMiao Xie 	cur_trans->state = TRANS_STATE_COMMIT_START;
1925bb9c12c9SSage Weil 	wake_up(&root->fs_info->transaction_blocked_wait);
1926bb9c12c9SSage Weil 
1927ccd467d6SChris Mason 	if (cur_trans->list.prev != &root->fs_info->trans_list) {
1928ccd467d6SChris Mason 		prev_trans = list_entry(cur_trans->list.prev,
1929ccd467d6SChris Mason 					struct btrfs_transaction, list);
19304a9d8bdeSMiao Xie 		if (prev_trans->state != TRANS_STATE_COMPLETED) {
193113c5a93eSJosef Bacik 			atomic_inc(&prev_trans->use_count);
1932a4abeea4SJosef Bacik 			spin_unlock(&root->fs_info->trans_lock);
1933ccd467d6SChris Mason 
1934ccd467d6SChris Mason 			wait_for_commit(root, prev_trans);
19351f9b8c8fSFilipe Manana 			ret = prev_trans->aborted;
1936ccd467d6SChris Mason 
1937724e2315SJosef Bacik 			btrfs_put_transaction(prev_trans);
19381f9b8c8fSFilipe Manana 			if (ret)
19391f9b8c8fSFilipe Manana 				goto cleanup_transaction;
1940a4abeea4SJosef Bacik 		} else {
1941a4abeea4SJosef Bacik 			spin_unlock(&root->fs_info->trans_lock);
1942ccd467d6SChris Mason 		}
1943a4abeea4SJosef Bacik 	} else {
1944a4abeea4SJosef Bacik 		spin_unlock(&root->fs_info->trans_lock);
1945ccd467d6SChris Mason 	}
194615ee9bc7SJosef Bacik 
19470860adfdSMiao Xie 	extwriter_counter_dec(cur_trans, trans->type);
19480860adfdSMiao Xie 
194982436617SMiao Xie 	ret = btrfs_start_delalloc_flush(root->fs_info);
195082436617SMiao Xie 	if (ret)
195182436617SMiao Xie 		goto cleanup_transaction;
195282436617SMiao Xie 
19538d875f95SChris Mason 	ret = btrfs_run_delayed_items(trans, root);
195449b25e05SJeff Mahoney 	if (ret)
195549b25e05SJeff Mahoney 		goto cleanup_transaction;
195616cdcec7SMiao Xie 
1957581227d0SMiao Xie 	wait_event(cur_trans->writer_wait,
1958581227d0SMiao Xie 		   extwriter_counter_read(cur_trans) == 0);
1959ed3b3d31SChris Mason 
1960581227d0SMiao Xie 	/* some pending stuffs might be added after the previous flush. */
19618d875f95SChris Mason 	ret = btrfs_run_delayed_items(trans, root);
1962ca469637SMiao Xie 	if (ret)
1963ca469637SMiao Xie 		goto cleanup_transaction;
1964ca469637SMiao Xie 
196582436617SMiao Xie 	btrfs_wait_delalloc_flush(root->fs_info);
1966cb7ab021SWang Shilong 
196750d9aa99SJosef Bacik 	btrfs_wait_pending_ordered(cur_trans, root->fs_info);
196850d9aa99SJosef Bacik 
1969cb7ab021SWang Shilong 	btrfs_scrub_pause(root);
1970ed0ca140SJosef Bacik 	/*
1971ed0ca140SJosef Bacik 	 * Ok now we need to make sure to block out any other joins while we
1972ed0ca140SJosef Bacik 	 * commit the transaction.  We could have started a join before setting
19734a9d8bdeSMiao Xie 	 * COMMIT_DOING so make sure to wait for num_writers to == 1 again.
1974ed0ca140SJosef Bacik 	 */
1975a4abeea4SJosef Bacik 	spin_lock(&root->fs_info->trans_lock);
19764a9d8bdeSMiao Xie 	cur_trans->state = TRANS_STATE_COMMIT_DOING;
1977a4abeea4SJosef Bacik 	spin_unlock(&root->fs_info->trans_lock);
1978ed0ca140SJosef Bacik 	wait_event(cur_trans->writer_wait,
1979ed0ca140SJosef Bacik 		   atomic_read(&cur_trans->num_writers) == 1);
198015ee9bc7SJosef Bacik 
19812cba30f1SMiao Xie 	/* ->aborted might be set after the previous check, so check it */
19822cba30f1SMiao Xie 	if (unlikely(ACCESS_ONCE(cur_trans->aborted))) {
19832cba30f1SMiao Xie 		ret = cur_trans->aborted;
19846cf7f77eSWang Shilong 		goto scrub_continue;
19852cba30f1SMiao Xie 	}
19867585717fSChris Mason 	/*
19877585717fSChris Mason 	 * the reloc mutex makes sure that we stop
19887585717fSChris Mason 	 * the balancing code from coming in and moving
19897585717fSChris Mason 	 * extents around in the middle of the commit
19907585717fSChris Mason 	 */
19917585717fSChris Mason 	mutex_lock(&root->fs_info->reloc_mutex);
19927585717fSChris Mason 
199342874b3dSMiao Xie 	/*
199442874b3dSMiao Xie 	 * We needn't worry about the delayed items because we will
199542874b3dSMiao Xie 	 * deal with them in create_pending_snapshot(), which is the
199642874b3dSMiao Xie 	 * core function of the snapshot creation.
199742874b3dSMiao Xie 	 */
199842874b3dSMiao Xie 	ret = create_pending_snapshots(trans, root->fs_info);
199949b25e05SJeff Mahoney 	if (ret) {
200049b25e05SJeff Mahoney 		mutex_unlock(&root->fs_info->reloc_mutex);
20016cf7f77eSWang Shilong 		goto scrub_continue;
200249b25e05SJeff Mahoney 	}
20033063d29fSChris Mason 
200442874b3dSMiao Xie 	/*
200542874b3dSMiao Xie 	 * We insert the dir indexes of the snapshots and update the inode
200642874b3dSMiao Xie 	 * of the snapshots' parents after the snapshot creation, so there
200742874b3dSMiao Xie 	 * are some delayed items which are not dealt with. Now deal with
200842874b3dSMiao Xie 	 * them.
200942874b3dSMiao Xie 	 *
201042874b3dSMiao Xie 	 * We needn't worry that this operation will corrupt the snapshots,
201142874b3dSMiao Xie 	 * because all the tree which are snapshoted will be forced to COW
201242874b3dSMiao Xie 	 * the nodes and leaves.
201342874b3dSMiao Xie 	 */
201442874b3dSMiao Xie 	ret = btrfs_run_delayed_items(trans, root);
201549b25e05SJeff Mahoney 	if (ret) {
201649b25e05SJeff Mahoney 		mutex_unlock(&root->fs_info->reloc_mutex);
20176cf7f77eSWang Shilong 		goto scrub_continue;
201849b25e05SJeff Mahoney 	}
201916cdcec7SMiao Xie 
202056bec294SChris Mason 	ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
202149b25e05SJeff Mahoney 	if (ret) {
202249b25e05SJeff Mahoney 		mutex_unlock(&root->fs_info->reloc_mutex);
20236cf7f77eSWang Shilong 		goto scrub_continue;
202449b25e05SJeff Mahoney 	}
202556bec294SChris Mason 
20260ed4792aSQu Wenruo 	/* Reocrd old roots for later qgroup accounting */
20270ed4792aSQu Wenruo 	ret = btrfs_qgroup_prepare_account_extents(trans, root->fs_info);
20280ed4792aSQu Wenruo 	if (ret) {
20290ed4792aSQu Wenruo 		mutex_unlock(&root->fs_info->reloc_mutex);
20300ed4792aSQu Wenruo 		goto scrub_continue;
20310ed4792aSQu Wenruo 	}
20320ed4792aSQu Wenruo 
2033e999376fSChris Mason 	/*
2034e999376fSChris Mason 	 * make sure none of the code above managed to slip in a
2035e999376fSChris Mason 	 * delayed item
2036e999376fSChris Mason 	 */
2037e999376fSChris Mason 	btrfs_assert_delayed_root_empty(root);
2038e999376fSChris Mason 
20392c90e5d6SChris Mason 	WARN_ON(cur_trans != trans->transaction);
2040dc17ff8fSChris Mason 
2041e02119d5SChris Mason 	/* btrfs_commit_tree_roots is responsible for getting the
2042e02119d5SChris Mason 	 * various roots consistent with each other.  Every pointer
2043e02119d5SChris Mason 	 * in the tree of tree roots has to point to the most up to date
2044e02119d5SChris Mason 	 * root for every subvolume and other tree.  So, we have to keep
2045e02119d5SChris Mason 	 * the tree logging code from jumping in and changing any
2046e02119d5SChris Mason 	 * of the trees.
2047e02119d5SChris Mason 	 *
2048e02119d5SChris Mason 	 * At this point in the commit, there can't be any tree-log
2049e02119d5SChris Mason 	 * writers, but a little lower down we drop the trans mutex
2050e02119d5SChris Mason 	 * and let new people in.  By holding the tree_log_mutex
2051e02119d5SChris Mason 	 * from now until after the super is written, we avoid races
2052e02119d5SChris Mason 	 * with the tree-log code.
2053e02119d5SChris Mason 	 */
2054e02119d5SChris Mason 	mutex_lock(&root->fs_info->tree_log_mutex);
20551a40e23bSZheng Yan 
20565d4f98a2SYan Zheng 	ret = commit_fs_roots(trans, root);
205749b25e05SJeff Mahoney 	if (ret) {
205849b25e05SJeff Mahoney 		mutex_unlock(&root->fs_info->tree_log_mutex);
2059871383beSDavid Sterba 		mutex_unlock(&root->fs_info->reloc_mutex);
20606cf7f77eSWang Shilong 		goto scrub_continue;
206149b25e05SJeff Mahoney 	}
206254aa1f4dSChris Mason 
20633818aea2SQu Wenruo 	/*
20647e1876acSDavid Sterba 	 * Since the transaction is done, we can apply the pending changes
20657e1876acSDavid Sterba 	 * before the next transaction.
20663818aea2SQu Wenruo 	 */
2067572d9ab7SDavid Sterba 	btrfs_apply_pending_changes(root->fs_info);
20683818aea2SQu Wenruo 
20695d4f98a2SYan Zheng 	/* commit_fs_roots gets rid of all the tree log roots, it is now
2070e02119d5SChris Mason 	 * safe to free the root of tree log roots
2071e02119d5SChris Mason 	 */
2072e02119d5SChris Mason 	btrfs_free_log_root_tree(trans, root->fs_info);
2073e02119d5SChris Mason 
20740ed4792aSQu Wenruo 	/*
20750ed4792aSQu Wenruo 	 * Since fs roots are all committed, we can get a quite accurate
20760ed4792aSQu Wenruo 	 * new_roots. So let's do quota accounting.
20770ed4792aSQu Wenruo 	 */
20780ed4792aSQu Wenruo 	ret = btrfs_qgroup_account_extents(trans, root->fs_info);
20790ed4792aSQu Wenruo 	if (ret < 0) {
20800ed4792aSQu Wenruo 		mutex_unlock(&root->fs_info->tree_log_mutex);
20810ed4792aSQu Wenruo 		mutex_unlock(&root->fs_info->reloc_mutex);
20820ed4792aSQu Wenruo 		goto scrub_continue;
20830ed4792aSQu Wenruo 	}
20840ed4792aSQu Wenruo 
20855d4f98a2SYan Zheng 	ret = commit_cowonly_roots(trans, root);
208649b25e05SJeff Mahoney 	if (ret) {
208749b25e05SJeff Mahoney 		mutex_unlock(&root->fs_info->tree_log_mutex);
2088871383beSDavid Sterba 		mutex_unlock(&root->fs_info->reloc_mutex);
20896cf7f77eSWang Shilong 		goto scrub_continue;
209049b25e05SJeff Mahoney 	}
209154aa1f4dSChris Mason 
20922cba30f1SMiao Xie 	/*
20932cba30f1SMiao Xie 	 * The tasks which save the space cache and inode cache may also
20942cba30f1SMiao Xie 	 * update ->aborted, check it.
20952cba30f1SMiao Xie 	 */
20962cba30f1SMiao Xie 	if (unlikely(ACCESS_ONCE(cur_trans->aborted))) {
20972cba30f1SMiao Xie 		ret = cur_trans->aborted;
20982cba30f1SMiao Xie 		mutex_unlock(&root->fs_info->tree_log_mutex);
20992cba30f1SMiao Xie 		mutex_unlock(&root->fs_info->reloc_mutex);
21006cf7f77eSWang Shilong 		goto scrub_continue;
21012cba30f1SMiao Xie 	}
21022cba30f1SMiao Xie 
210311833d66SYan Zheng 	btrfs_prepare_extent_commit(trans, root);
210411833d66SYan Zheng 
210578fae27eSChris Mason 	cur_trans = root->fs_info->running_transaction;
21065f39d397SChris Mason 
21075d4f98a2SYan Zheng 	btrfs_set_root_node(&root->fs_info->tree_root->root_item,
21085d4f98a2SYan Zheng 			    root->fs_info->tree_root->node);
21099e351cc8SJosef Bacik 	list_add_tail(&root->fs_info->tree_root->dirty_list,
21109e351cc8SJosef Bacik 		      &cur_trans->switch_commits);
21115d4f98a2SYan Zheng 
21125d4f98a2SYan Zheng 	btrfs_set_root_node(&root->fs_info->chunk_root->root_item,
21135d4f98a2SYan Zheng 			    root->fs_info->chunk_root->node);
21149e351cc8SJosef Bacik 	list_add_tail(&root->fs_info->chunk_root->dirty_list,
21159e351cc8SJosef Bacik 		      &cur_trans->switch_commits);
21169e351cc8SJosef Bacik 
21179e351cc8SJosef Bacik 	switch_commit_roots(cur_trans, root->fs_info);
21185d4f98a2SYan Zheng 
2119edf39272SJan Schmidt 	assert_qgroups_uptodate(trans);
2120ce93ec54SJosef Bacik 	ASSERT(list_empty(&cur_trans->dirty_bgs));
21211bbc621eSChris Mason 	ASSERT(list_empty(&cur_trans->io_bgs));
21225d4f98a2SYan Zheng 	update_super_roots(root);
2123e02119d5SChris Mason 
21246c41761fSDavid Sterba 	btrfs_set_super_log_root(root->fs_info->super_copy, 0);
21256c41761fSDavid Sterba 	btrfs_set_super_log_root_level(root->fs_info->super_copy, 0);
21266c41761fSDavid Sterba 	memcpy(root->fs_info->super_for_commit, root->fs_info->super_copy,
21276c41761fSDavid Sterba 	       sizeof(*root->fs_info->super_copy));
2128ccd467d6SChris Mason 
2129935e5cc9SMiao Xie 	btrfs_update_commit_device_size(root->fs_info);
2130ce7213c7SMiao Xie 	btrfs_update_commit_device_bytes_used(root, cur_trans);
2131935e5cc9SMiao Xie 
2132656f30dbSFilipe Manana 	clear_bit(BTRFS_INODE_BTREE_LOG1_ERR, &btree_ino->runtime_flags);
2133656f30dbSFilipe Manana 	clear_bit(BTRFS_INODE_BTREE_LOG2_ERR, &btree_ino->runtime_flags);
2134656f30dbSFilipe Manana 
21354fbcdf66SFilipe Manana 	btrfs_trans_release_chunk_metadata(trans);
21364fbcdf66SFilipe Manana 
2137a4abeea4SJosef Bacik 	spin_lock(&root->fs_info->trans_lock);
21384a9d8bdeSMiao Xie 	cur_trans->state = TRANS_STATE_UNBLOCKED;
2139a4abeea4SJosef Bacik 	root->fs_info->running_transaction = NULL;
2140a4abeea4SJosef Bacik 	spin_unlock(&root->fs_info->trans_lock);
21417585717fSChris Mason 	mutex_unlock(&root->fs_info->reloc_mutex);
2142b7ec40d7SChris Mason 
2143f9295749SChris Mason 	wake_up(&root->fs_info->transaction_wait);
2144e6dcd2dcSChris Mason 
214579154b1bSChris Mason 	ret = btrfs_write_and_wait_transaction(trans, root);
214649b25e05SJeff Mahoney 	if (ret) {
214749b25e05SJeff Mahoney 		btrfs_error(root->fs_info, ret,
214808748810SDavid Sterba 			    "Error while writing out transaction");
214949b25e05SJeff Mahoney 		mutex_unlock(&root->fs_info->tree_log_mutex);
21506cf7f77eSWang Shilong 		goto scrub_continue;
215149b25e05SJeff Mahoney 	}
215249b25e05SJeff Mahoney 
215349b25e05SJeff Mahoney 	ret = write_ctree_super(trans, root, 0);
215449b25e05SJeff Mahoney 	if (ret) {
215549b25e05SJeff Mahoney 		mutex_unlock(&root->fs_info->tree_log_mutex);
21566cf7f77eSWang Shilong 		goto scrub_continue;
215749b25e05SJeff Mahoney 	}
21584313b399SChris Mason 
2159e02119d5SChris Mason 	/*
2160e02119d5SChris Mason 	 * the super is written, we can safely allow the tree-loggers
2161e02119d5SChris Mason 	 * to go about their business
2162e02119d5SChris Mason 	 */
2163e02119d5SChris Mason 	mutex_unlock(&root->fs_info->tree_log_mutex);
2164e02119d5SChris Mason 
216511833d66SYan Zheng 	btrfs_finish_extent_commit(trans, root);
21664313b399SChris Mason 
216713212b54SZhao Lei 	if (cur_trans->have_free_bgs)
216813212b54SZhao Lei 		btrfs_clear_space_info_full(root->fs_info);
216913212b54SZhao Lei 
217015ee9bc7SJosef Bacik 	root->fs_info->last_trans_committed = cur_trans->transid;
21714a9d8bdeSMiao Xie 	/*
21724a9d8bdeSMiao Xie 	 * We needn't acquire the lock here because there is no other task
21734a9d8bdeSMiao Xie 	 * which can change it.
21744a9d8bdeSMiao Xie 	 */
21754a9d8bdeSMiao Xie 	cur_trans->state = TRANS_STATE_COMPLETED;
21762c90e5d6SChris Mason 	wake_up(&cur_trans->commit_wait);
21773de4586cSChris Mason 
2178a4abeea4SJosef Bacik 	spin_lock(&root->fs_info->trans_lock);
217913c5a93eSJosef Bacik 	list_del_init(&cur_trans->list);
2180a4abeea4SJosef Bacik 	spin_unlock(&root->fs_info->trans_lock);
2181a4abeea4SJosef Bacik 
2182724e2315SJosef Bacik 	btrfs_put_transaction(cur_trans);
2183724e2315SJosef Bacik 	btrfs_put_transaction(cur_trans);
218458176a96SJosef Bacik 
21850860adfdSMiao Xie 	if (trans->type & __TRANS_FREEZABLE)
2186b2b5ef5cSJan Kara 		sb_end_intwrite(root->fs_info->sb);
2187b2b5ef5cSJan Kara 
21881abe9b8aSliubo 	trace_btrfs_transaction_commit(root);
21891abe9b8aSliubo 
2190a2de733cSArne Jansen 	btrfs_scrub_continue(root);
2191a2de733cSArne Jansen 
21929ed74f2dSJosef Bacik 	if (current->journal_info == trans)
21939ed74f2dSJosef Bacik 		current->journal_info = NULL;
21949ed74f2dSJosef Bacik 
21952c90e5d6SChris Mason 	kmem_cache_free(btrfs_trans_handle_cachep, trans);
219624bbcf04SYan, Zheng 
21978a733013SZhao Lei 	if (current != root->fs_info->transaction_kthread &&
21988a733013SZhao Lei 	    current != root->fs_info->cleaner_kthread)
219924bbcf04SYan, Zheng 		btrfs_run_delayed_iputs(root);
220024bbcf04SYan, Zheng 
220179154b1bSChris Mason 	return ret;
220249b25e05SJeff Mahoney 
22036cf7f77eSWang Shilong scrub_continue:
22046cf7f77eSWang Shilong 	btrfs_scrub_continue(root);
220549b25e05SJeff Mahoney cleanup_transaction:
22060e721106SJosef Bacik 	btrfs_trans_release_metadata(trans, root);
22074fbcdf66SFilipe Manana 	btrfs_trans_release_chunk_metadata(trans);
22080e721106SJosef Bacik 	trans->block_rsv = NULL;
2209272d26d0SMiao Xie 	if (trans->qgroup_reserved) {
2210272d26d0SMiao Xie 		btrfs_qgroup_free(root, trans->qgroup_reserved);
2211272d26d0SMiao Xie 		trans->qgroup_reserved = 0;
2212272d26d0SMiao Xie 	}
2213c2cf52ebSSimon Kirby 	btrfs_warn(root->fs_info, "Skipping commit of aborted transaction.");
221449b25e05SJeff Mahoney 	if (current->journal_info == trans)
221549b25e05SJeff Mahoney 		current->journal_info = NULL;
22167b8b92afSJosef Bacik 	cleanup_transaction(trans, root, ret);
221749b25e05SJeff Mahoney 
221849b25e05SJeff Mahoney 	return ret;
221979154b1bSChris Mason }
222079154b1bSChris Mason 
2221d352ac68SChris Mason /*
22229d1a2a3aSDavid Sterba  * return < 0 if error
22239d1a2a3aSDavid Sterba  * 0 if there are no more dead_roots at the time of call
22249d1a2a3aSDavid Sterba  * 1 there are more to be processed, call me again
22259d1a2a3aSDavid Sterba  *
22269d1a2a3aSDavid Sterba  * The return value indicates there are certainly more snapshots to delete, but
22279d1a2a3aSDavid Sterba  * if there comes a new one during processing, it may return 0. We don't mind,
22289d1a2a3aSDavid Sterba  * because btrfs_commit_super will poke cleaner thread and it will process it a
22299d1a2a3aSDavid Sterba  * few seconds later.
2230d352ac68SChris Mason  */
22319d1a2a3aSDavid Sterba int btrfs_clean_one_deleted_snapshot(struct btrfs_root *root)
2232e9d0b13bSChris Mason {
22339d1a2a3aSDavid Sterba 	int ret;
22345d4f98a2SYan Zheng 	struct btrfs_fs_info *fs_info = root->fs_info;
2235e9d0b13bSChris Mason 
2236a4abeea4SJosef Bacik 	spin_lock(&fs_info->trans_lock);
22379d1a2a3aSDavid Sterba 	if (list_empty(&fs_info->dead_roots)) {
22389d1a2a3aSDavid Sterba 		spin_unlock(&fs_info->trans_lock);
22399d1a2a3aSDavid Sterba 		return 0;
22409d1a2a3aSDavid Sterba 	}
22419d1a2a3aSDavid Sterba 	root = list_first_entry(&fs_info->dead_roots,
22429d1a2a3aSDavid Sterba 			struct btrfs_root, root_list);
2243cfad392bSJosef Bacik 	list_del_init(&root->root_list);
2244a4abeea4SJosef Bacik 	spin_unlock(&fs_info->trans_lock);
22455d4f98a2SYan Zheng 
2246efe120a0SFrank Holton 	pr_debug("BTRFS: cleaner removing %llu\n", root->objectid);
224776dda93cSYan, Zheng 
224816cdcec7SMiao Xie 	btrfs_kill_all_delayed_nodes(root);
224916cdcec7SMiao Xie 
225076dda93cSYan, Zheng 	if (btrfs_header_backref_rev(root->node) <
225176dda93cSYan, Zheng 			BTRFS_MIXED_BACKREF_REV)
22522c536799SJeff Mahoney 		ret = btrfs_drop_snapshot(root, NULL, 0, 0);
225376dda93cSYan, Zheng 	else
22542c536799SJeff Mahoney 		ret = btrfs_drop_snapshot(root, NULL, 1, 0);
225532471dc2SDavid Sterba 
22566596a928SJosef Bacik 	return (ret < 0) ? 0 : 1;
2257e9d0b13bSChris Mason }
2258572d9ab7SDavid Sterba 
2259572d9ab7SDavid Sterba void btrfs_apply_pending_changes(struct btrfs_fs_info *fs_info)
2260572d9ab7SDavid Sterba {
2261572d9ab7SDavid Sterba 	unsigned long prev;
2262572d9ab7SDavid Sterba 	unsigned long bit;
2263572d9ab7SDavid Sterba 
22646c9fe14fSQu Wenruo 	prev = xchg(&fs_info->pending_changes, 0);
2265572d9ab7SDavid Sterba 	if (!prev)
2266572d9ab7SDavid Sterba 		return;
2267572d9ab7SDavid Sterba 
22687e1876acSDavid Sterba 	bit = 1 << BTRFS_PENDING_SET_INODE_MAP_CACHE;
22697e1876acSDavid Sterba 	if (prev & bit)
22707e1876acSDavid Sterba 		btrfs_set_opt(fs_info->mount_opt, INODE_MAP_CACHE);
22717e1876acSDavid Sterba 	prev &= ~bit;
22727e1876acSDavid Sterba 
22737e1876acSDavid Sterba 	bit = 1 << BTRFS_PENDING_CLEAR_INODE_MAP_CACHE;
22747e1876acSDavid Sterba 	if (prev & bit)
22757e1876acSDavid Sterba 		btrfs_clear_opt(fs_info->mount_opt, INODE_MAP_CACHE);
22767e1876acSDavid Sterba 	prev &= ~bit;
22777e1876acSDavid Sterba 
2278d51033d0SDavid Sterba 	bit = 1 << BTRFS_PENDING_COMMIT;
2279d51033d0SDavid Sterba 	if (prev & bit)
2280d51033d0SDavid Sterba 		btrfs_debug(fs_info, "pending commit done");
2281d51033d0SDavid Sterba 	prev &= ~bit;
2282d51033d0SDavid Sterba 
2283572d9ab7SDavid Sterba 	if (prev)
2284572d9ab7SDavid Sterba 		btrfs_warn(fs_info,
2285572d9ab7SDavid Sterba 			"unknown pending changes left 0x%lx, ignoring", prev);
2286572d9ab7SDavid Sterba }
2287