xref: /openbmc/linux/fs/btrfs/transaction.c (revision 2ff7e61e)
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)
68ab8d0fc4SJeff Mahoney 			btrfs_err(transaction->fs_info,
69ab8d0fc4SJeff Mahoney 				  "pending csums is %llu",
701262133bSJosef Bacik 				  transaction->delayed_refs.pending_csums);
716df9a95eSJosef Bacik 		while (!list_empty(&transaction->pending_chunks)) {
726df9a95eSJosef Bacik 			struct extent_map *em;
736df9a95eSJosef Bacik 
746df9a95eSJosef Bacik 			em = list_first_entry(&transaction->pending_chunks,
756df9a95eSJosef Bacik 					      struct extent_map, list);
766df9a95eSJosef Bacik 			list_del_init(&em->list);
776df9a95eSJosef Bacik 			free_extent_map(em);
786df9a95eSJosef Bacik 		}
797785a663SFilipe Manana 		/*
807785a663SFilipe Manana 		 * If any block groups are found in ->deleted_bgs then it's
817785a663SFilipe Manana 		 * because the transaction was aborted and a commit did not
827785a663SFilipe Manana 		 * happen (things failed before writing the new superblock
837785a663SFilipe Manana 		 * and calling btrfs_finish_extent_commit()), so we can not
847785a663SFilipe Manana 		 * discard the physical locations of the block groups.
857785a663SFilipe Manana 		 */
867785a663SFilipe Manana 		while (!list_empty(&transaction->deleted_bgs)) {
877785a663SFilipe Manana 			struct btrfs_block_group_cache *cache;
887785a663SFilipe Manana 
897785a663SFilipe Manana 			cache = list_first_entry(&transaction->deleted_bgs,
907785a663SFilipe Manana 						 struct btrfs_block_group_cache,
917785a663SFilipe Manana 						 bg_list);
927785a663SFilipe Manana 			list_del_init(&cache->bg_list);
937785a663SFilipe Manana 			btrfs_put_block_group_trimming(cache);
947785a663SFilipe Manana 			btrfs_put_block_group(cache);
957785a663SFilipe Manana 		}
962c90e5d6SChris Mason 		kmem_cache_free(btrfs_transaction_cachep, transaction);
9779154b1bSChris Mason 	}
9878fae27eSChris Mason }
9979154b1bSChris Mason 
100663dfbb0SFilipe Manana static void clear_btree_io_tree(struct extent_io_tree *tree)
101663dfbb0SFilipe Manana {
102663dfbb0SFilipe Manana 	spin_lock(&tree->lock);
103b666a9cdSDavid Sterba 	/*
104b666a9cdSDavid Sterba 	 * Do a single barrier for the waitqueue_active check here, the state
105b666a9cdSDavid Sterba 	 * of the waitqueue should not change once clear_btree_io_tree is
106b666a9cdSDavid Sterba 	 * called.
107b666a9cdSDavid Sterba 	 */
108b666a9cdSDavid Sterba 	smp_mb();
109663dfbb0SFilipe Manana 	while (!RB_EMPTY_ROOT(&tree->state)) {
110663dfbb0SFilipe Manana 		struct rb_node *node;
111663dfbb0SFilipe Manana 		struct extent_state *state;
112663dfbb0SFilipe Manana 
113663dfbb0SFilipe Manana 		node = rb_first(&tree->state);
114663dfbb0SFilipe Manana 		state = rb_entry(node, struct extent_state, rb_node);
115663dfbb0SFilipe Manana 		rb_erase(&state->rb_node, &tree->state);
116663dfbb0SFilipe Manana 		RB_CLEAR_NODE(&state->rb_node);
117663dfbb0SFilipe Manana 		/*
118663dfbb0SFilipe Manana 		 * btree io trees aren't supposed to have tasks waiting for
119663dfbb0SFilipe Manana 		 * changes in the flags of extent states ever.
120663dfbb0SFilipe Manana 		 */
121663dfbb0SFilipe Manana 		ASSERT(!waitqueue_active(&state->wq));
122663dfbb0SFilipe Manana 		free_extent_state(state);
123351810c1SDavid Sterba 
124351810c1SDavid Sterba 		cond_resched_lock(&tree->lock);
125663dfbb0SFilipe Manana 	}
126663dfbb0SFilipe Manana 	spin_unlock(&tree->lock);
127663dfbb0SFilipe Manana }
128663dfbb0SFilipe Manana 
1299e351cc8SJosef Bacik static noinline void switch_commit_roots(struct btrfs_transaction *trans,
1309e351cc8SJosef Bacik 					 struct btrfs_fs_info *fs_info)
131817d52f8SJosef Bacik {
1329e351cc8SJosef Bacik 	struct btrfs_root *root, *tmp;
1339e351cc8SJosef Bacik 
1349e351cc8SJosef Bacik 	down_write(&fs_info->commit_root_sem);
1359e351cc8SJosef Bacik 	list_for_each_entry_safe(root, tmp, &trans->switch_commits,
1369e351cc8SJosef Bacik 				 dirty_list) {
1379e351cc8SJosef Bacik 		list_del_init(&root->dirty_list);
138817d52f8SJosef Bacik 		free_extent_buffer(root->commit_root);
139817d52f8SJosef Bacik 		root->commit_root = btrfs_root_node(root);
1409e351cc8SJosef Bacik 		if (is_fstree(root->objectid))
1419e351cc8SJosef Bacik 			btrfs_unpin_free_ino(root);
142663dfbb0SFilipe Manana 		clear_btree_io_tree(&root->dirty_log_pages);
1439e351cc8SJosef Bacik 	}
1442b9dbef2SJosef Bacik 
1452b9dbef2SJosef Bacik 	/* We can free old roots now. */
1462b9dbef2SJosef Bacik 	spin_lock(&trans->dropped_roots_lock);
1472b9dbef2SJosef Bacik 	while (!list_empty(&trans->dropped_roots)) {
1482b9dbef2SJosef Bacik 		root = list_first_entry(&trans->dropped_roots,
1492b9dbef2SJosef Bacik 					struct btrfs_root, root_list);
1502b9dbef2SJosef Bacik 		list_del_init(&root->root_list);
1512b9dbef2SJosef Bacik 		spin_unlock(&trans->dropped_roots_lock);
1522b9dbef2SJosef Bacik 		btrfs_drop_and_free_fs_root(fs_info, root);
1532b9dbef2SJosef Bacik 		spin_lock(&trans->dropped_roots_lock);
1542b9dbef2SJosef Bacik 	}
1552b9dbef2SJosef Bacik 	spin_unlock(&trans->dropped_roots_lock);
1569e351cc8SJosef Bacik 	up_write(&fs_info->commit_root_sem);
157817d52f8SJosef Bacik }
158817d52f8SJosef Bacik 
1590860adfdSMiao Xie static inline void extwriter_counter_inc(struct btrfs_transaction *trans,
1600860adfdSMiao Xie 					 unsigned int type)
1610860adfdSMiao Xie {
1620860adfdSMiao Xie 	if (type & TRANS_EXTWRITERS)
1630860adfdSMiao Xie 		atomic_inc(&trans->num_extwriters);
1640860adfdSMiao Xie }
1650860adfdSMiao Xie 
1660860adfdSMiao Xie static inline void extwriter_counter_dec(struct btrfs_transaction *trans,
1670860adfdSMiao Xie 					 unsigned int type)
1680860adfdSMiao Xie {
1690860adfdSMiao Xie 	if (type & TRANS_EXTWRITERS)
1700860adfdSMiao Xie 		atomic_dec(&trans->num_extwriters);
1710860adfdSMiao Xie }
1720860adfdSMiao Xie 
1730860adfdSMiao Xie static inline void extwriter_counter_init(struct btrfs_transaction *trans,
1740860adfdSMiao Xie 					  unsigned int type)
1750860adfdSMiao Xie {
1760860adfdSMiao Xie 	atomic_set(&trans->num_extwriters, ((type & TRANS_EXTWRITERS) ? 1 : 0));
1770860adfdSMiao Xie }
1780860adfdSMiao Xie 
1790860adfdSMiao Xie static inline int extwriter_counter_read(struct btrfs_transaction *trans)
1800860adfdSMiao Xie {
1810860adfdSMiao Xie 	return atomic_read(&trans->num_extwriters);
182178260b2SMiao Xie }
183178260b2SMiao Xie 
184d352ac68SChris Mason /*
185d352ac68SChris Mason  * either allocate a new transaction or hop into the existing one
186d352ac68SChris Mason  */
1872ff7e61eSJeff Mahoney static noinline int join_transaction(struct btrfs_fs_info *fs_info,
1882ff7e61eSJeff Mahoney 				     unsigned int type)
18979154b1bSChris Mason {
19079154b1bSChris Mason 	struct btrfs_transaction *cur_trans;
191a4abeea4SJosef Bacik 
19219ae4e81SJan Schmidt 	spin_lock(&fs_info->trans_lock);
193d43317dcSChris Mason loop:
19449b25e05SJeff Mahoney 	/* The file system has been taken offline. No new transactions. */
19587533c47SMiao Xie 	if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) {
19619ae4e81SJan Schmidt 		spin_unlock(&fs_info->trans_lock);
19749b25e05SJeff Mahoney 		return -EROFS;
19849b25e05SJeff Mahoney 	}
19949b25e05SJeff Mahoney 
20019ae4e81SJan Schmidt 	cur_trans = fs_info->running_transaction;
201a4abeea4SJosef Bacik 	if (cur_trans) {
202871383beSDavid Sterba 		if (cur_trans->aborted) {
20319ae4e81SJan Schmidt 			spin_unlock(&fs_info->trans_lock);
20449b25e05SJeff Mahoney 			return cur_trans->aborted;
205871383beSDavid Sterba 		}
2064a9d8bdeSMiao Xie 		if (btrfs_blocked_trans_types[cur_trans->state] & type) {
207178260b2SMiao Xie 			spin_unlock(&fs_info->trans_lock);
208178260b2SMiao Xie 			return -EBUSY;
209178260b2SMiao Xie 		}
210a4abeea4SJosef Bacik 		atomic_inc(&cur_trans->use_count);
211a4abeea4SJosef Bacik 		atomic_inc(&cur_trans->num_writers);
2120860adfdSMiao Xie 		extwriter_counter_inc(cur_trans, type);
21319ae4e81SJan Schmidt 		spin_unlock(&fs_info->trans_lock);
214a4abeea4SJosef Bacik 		return 0;
215a4abeea4SJosef Bacik 	}
21619ae4e81SJan Schmidt 	spin_unlock(&fs_info->trans_lock);
217a4abeea4SJosef Bacik 
218354aa0fbSMiao Xie 	/*
219354aa0fbSMiao Xie 	 * If we are ATTACH, we just want to catch the current transaction,
220354aa0fbSMiao Xie 	 * and commit it. If there is no transaction, just return ENOENT.
221354aa0fbSMiao Xie 	 */
222354aa0fbSMiao Xie 	if (type == TRANS_ATTACH)
223354aa0fbSMiao Xie 		return -ENOENT;
224354aa0fbSMiao Xie 
2254a9d8bdeSMiao Xie 	/*
2264a9d8bdeSMiao Xie 	 * JOIN_NOLOCK only happens during the transaction commit, so
2274a9d8bdeSMiao Xie 	 * it is impossible that ->running_transaction is NULL
2284a9d8bdeSMiao Xie 	 */
2294a9d8bdeSMiao Xie 	BUG_ON(type == TRANS_JOIN_NOLOCK);
2304a9d8bdeSMiao Xie 
231a4abeea4SJosef Bacik 	cur_trans = kmem_cache_alloc(btrfs_transaction_cachep, GFP_NOFS);
232db5b493aSTsutomu Itoh 	if (!cur_trans)
233db5b493aSTsutomu Itoh 		return -ENOMEM;
234d43317dcSChris Mason 
23519ae4e81SJan Schmidt 	spin_lock(&fs_info->trans_lock);
23619ae4e81SJan Schmidt 	if (fs_info->running_transaction) {
237d43317dcSChris Mason 		/*
238d43317dcSChris Mason 		 * someone started a transaction after we unlocked.  Make sure
2394a9d8bdeSMiao Xie 		 * to redo the checks above
240d43317dcSChris Mason 		 */
241a4abeea4SJosef Bacik 		kmem_cache_free(btrfs_transaction_cachep, cur_trans);
242d43317dcSChris Mason 		goto loop;
24387533c47SMiao Xie 	} else if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) {
244e4b50e14SDan Carpenter 		spin_unlock(&fs_info->trans_lock);
2457b8b92afSJosef Bacik 		kmem_cache_free(btrfs_transaction_cachep, cur_trans);
2467b8b92afSJosef Bacik 		return -EROFS;
247a4abeea4SJosef Bacik 	}
248d43317dcSChris Mason 
249ab8d0fc4SJeff Mahoney 	cur_trans->fs_info = fs_info;
25013c5a93eSJosef Bacik 	atomic_set(&cur_trans->num_writers, 1);
2510860adfdSMiao Xie 	extwriter_counter_init(cur_trans, type);
25279154b1bSChris Mason 	init_waitqueue_head(&cur_trans->writer_wait);
25379154b1bSChris Mason 	init_waitqueue_head(&cur_trans->commit_wait);
254161c3549SJosef Bacik 	init_waitqueue_head(&cur_trans->pending_wait);
2554a9d8bdeSMiao Xie 	cur_trans->state = TRANS_STATE_RUNNING;
256a4abeea4SJosef Bacik 	/*
257a4abeea4SJosef Bacik 	 * One for this trans handle, one so it will live on until we
258a4abeea4SJosef Bacik 	 * commit the transaction.
259a4abeea4SJosef Bacik 	 */
260a4abeea4SJosef Bacik 	atomic_set(&cur_trans->use_count, 2);
261161c3549SJosef Bacik 	atomic_set(&cur_trans->pending_ordered, 0);
2623204d33cSJosef Bacik 	cur_trans->flags = 0;
26308607c1bSChris Mason 	cur_trans->start_time = get_seconds();
26456bec294SChris Mason 
265a099d0fdSAlexandru Moise 	memset(&cur_trans->delayed_refs, 0, sizeof(cur_trans->delayed_refs));
266a099d0fdSAlexandru Moise 
267c46effa6SLiu Bo 	cur_trans->delayed_refs.href_root = RB_ROOT;
2683368d001SQu Wenruo 	cur_trans->delayed_refs.dirty_extent_root = RB_ROOT;
269d7df2c79SJosef Bacik 	atomic_set(&cur_trans->delayed_refs.num_entries, 0);
27020b297d6SJan Schmidt 
27120b297d6SJan Schmidt 	/*
27220b297d6SJan Schmidt 	 * although the tree mod log is per file system and not per transaction,
27320b297d6SJan Schmidt 	 * the log must never go across transaction boundaries.
27420b297d6SJan Schmidt 	 */
27520b297d6SJan Schmidt 	smp_mb();
27631b1a2bdSJulia Lawall 	if (!list_empty(&fs_info->tree_mod_seq_list))
2775d163e0eSJeff Mahoney 		WARN(1, KERN_ERR "BTRFS: tree_mod_seq_list not empty when creating a fresh transaction\n");
27831b1a2bdSJulia Lawall 	if (!RB_EMPTY_ROOT(&fs_info->tree_mod_log))
2795d163e0eSJeff Mahoney 		WARN(1, KERN_ERR "BTRFS: tree_mod_log rb tree not empty when creating a fresh transaction\n");
280fc36ed7eSJan Schmidt 	atomic64_set(&fs_info->tree_mod_seq, 0);
28120b297d6SJan Schmidt 
28256bec294SChris Mason 	spin_lock_init(&cur_trans->delayed_refs.lock);
28356bec294SChris Mason 
2843063d29fSChris Mason 	INIT_LIST_HEAD(&cur_trans->pending_snapshots);
2856df9a95eSJosef Bacik 	INIT_LIST_HEAD(&cur_trans->pending_chunks);
2869e351cc8SJosef Bacik 	INIT_LIST_HEAD(&cur_trans->switch_commits);
287ce93ec54SJosef Bacik 	INIT_LIST_HEAD(&cur_trans->dirty_bgs);
2881bbc621eSChris Mason 	INIT_LIST_HEAD(&cur_trans->io_bgs);
2892b9dbef2SJosef Bacik 	INIT_LIST_HEAD(&cur_trans->dropped_roots);
2901bbc621eSChris Mason 	mutex_init(&cur_trans->cache_write_mutex);
291cb723e49SJosef Bacik 	cur_trans->num_dirty_bgs = 0;
292ce93ec54SJosef Bacik 	spin_lock_init(&cur_trans->dirty_bgs_lock);
293e33e17eeSJeff Mahoney 	INIT_LIST_HEAD(&cur_trans->deleted_bgs);
2942b9dbef2SJosef Bacik 	spin_lock_init(&cur_trans->dropped_roots_lock);
29519ae4e81SJan Schmidt 	list_add_tail(&cur_trans->list, &fs_info->trans_list);
296d1310b2eSChris Mason 	extent_io_tree_init(&cur_trans->dirty_pages,
29719ae4e81SJan Schmidt 			     fs_info->btree_inode->i_mapping);
29819ae4e81SJan Schmidt 	fs_info->generation++;
29919ae4e81SJan Schmidt 	cur_trans->transid = fs_info->generation;
30019ae4e81SJan Schmidt 	fs_info->running_transaction = cur_trans;
30149b25e05SJeff Mahoney 	cur_trans->aborted = 0;
30219ae4e81SJan Schmidt 	spin_unlock(&fs_info->trans_lock);
30315ee9bc7SJosef Bacik 
30479154b1bSChris Mason 	return 0;
30579154b1bSChris Mason }
30679154b1bSChris Mason 
307d352ac68SChris Mason /*
308d397712bSChris Mason  * this does all the record keeping required to make sure that a reference
309d397712bSChris Mason  * counted root is properly recorded in a given transaction.  This is required
310d397712bSChris Mason  * to make sure the old root from before we joined the transaction is deleted
311d397712bSChris Mason  * when the transaction commits
312d352ac68SChris Mason  */
3137585717fSChris Mason static int record_root_in_trans(struct btrfs_trans_handle *trans,
3146426c7adSQu Wenruo 			       struct btrfs_root *root,
3156426c7adSQu Wenruo 			       int force)
3166702ed49SChris Mason {
3170b246afaSJeff Mahoney 	struct btrfs_fs_info *fs_info = root->fs_info;
3180b246afaSJeff Mahoney 
3196426c7adSQu Wenruo 	if ((test_bit(BTRFS_ROOT_REF_COWS, &root->state) &&
3206426c7adSQu Wenruo 	    root->last_trans < trans->transid) || force) {
3210b246afaSJeff Mahoney 		WARN_ON(root == fs_info->extent_root);
3225d4f98a2SYan Zheng 		WARN_ON(root->commit_root != root->node);
3235d4f98a2SYan Zheng 
3247585717fSChris Mason 		/*
32527cdeb70SMiao Xie 		 * see below for IN_TRANS_SETUP usage rules
3267585717fSChris Mason 		 * we have the reloc mutex held now, so there
3277585717fSChris Mason 		 * is only one writer in this function
3287585717fSChris Mason 		 */
32927cdeb70SMiao Xie 		set_bit(BTRFS_ROOT_IN_TRANS_SETUP, &root->state);
3307585717fSChris Mason 
33127cdeb70SMiao Xie 		/* make sure readers find IN_TRANS_SETUP before
3327585717fSChris Mason 		 * they find our root->last_trans update
3337585717fSChris Mason 		 */
3347585717fSChris Mason 		smp_wmb();
3357585717fSChris Mason 
3360b246afaSJeff Mahoney 		spin_lock(&fs_info->fs_roots_radix_lock);
3376426c7adSQu Wenruo 		if (root->last_trans == trans->transid && !force) {
3380b246afaSJeff Mahoney 			spin_unlock(&fs_info->fs_roots_radix_lock);
339a4abeea4SJosef Bacik 			return 0;
340a4abeea4SJosef Bacik 		}
3410b246afaSJeff Mahoney 		radix_tree_tag_set(&fs_info->fs_roots_radix,
3426702ed49SChris Mason 				   (unsigned long)root->root_key.objectid,
3436702ed49SChris Mason 				   BTRFS_ROOT_TRANS_TAG);
3440b246afaSJeff Mahoney 		spin_unlock(&fs_info->fs_roots_radix_lock);
3457585717fSChris Mason 		root->last_trans = trans->transid;
3467585717fSChris Mason 
3477585717fSChris Mason 		/* this is pretty tricky.  We don't want to
3487585717fSChris Mason 		 * take the relocation lock in btrfs_record_root_in_trans
3497585717fSChris Mason 		 * unless we're really doing the first setup for this root in
3507585717fSChris Mason 		 * this transaction.
3517585717fSChris Mason 		 *
3527585717fSChris Mason 		 * Normally we'd use root->last_trans as a flag to decide
3537585717fSChris Mason 		 * if we want to take the expensive mutex.
3547585717fSChris Mason 		 *
3557585717fSChris Mason 		 * But, we have to set root->last_trans before we
3567585717fSChris Mason 		 * init the relocation root, otherwise, we trip over warnings
3577585717fSChris Mason 		 * in ctree.c.  The solution used here is to flag ourselves
35827cdeb70SMiao Xie 		 * with root IN_TRANS_SETUP.  When this is 1, we're still
3597585717fSChris Mason 		 * fixing up the reloc trees and everyone must wait.
3607585717fSChris Mason 		 *
3617585717fSChris Mason 		 * When this is zero, they can trust root->last_trans and fly
3627585717fSChris Mason 		 * through btrfs_record_root_in_trans without having to take the
3637585717fSChris Mason 		 * lock.  smp_wmb() makes sure that all the writes above are
3647585717fSChris Mason 		 * done before we pop in the zero below
3657585717fSChris Mason 		 */
3665d4f98a2SYan Zheng 		btrfs_init_reloc_root(trans, root);
367c7548af6SChris Mason 		smp_mb__before_atomic();
36827cdeb70SMiao Xie 		clear_bit(BTRFS_ROOT_IN_TRANS_SETUP, &root->state);
3696702ed49SChris Mason 	}
3705d4f98a2SYan Zheng 	return 0;
3716702ed49SChris Mason }
3725d4f98a2SYan Zheng 
3737585717fSChris Mason 
3742b9dbef2SJosef Bacik void btrfs_add_dropped_root(struct btrfs_trans_handle *trans,
3752b9dbef2SJosef Bacik 			    struct btrfs_root *root)
3762b9dbef2SJosef Bacik {
3770b246afaSJeff Mahoney 	struct btrfs_fs_info *fs_info = root->fs_info;
3782b9dbef2SJosef Bacik 	struct btrfs_transaction *cur_trans = trans->transaction;
3792b9dbef2SJosef Bacik 
3802b9dbef2SJosef Bacik 	/* Add ourselves to the transaction dropped list */
3812b9dbef2SJosef Bacik 	spin_lock(&cur_trans->dropped_roots_lock);
3822b9dbef2SJosef Bacik 	list_add_tail(&root->root_list, &cur_trans->dropped_roots);
3832b9dbef2SJosef Bacik 	spin_unlock(&cur_trans->dropped_roots_lock);
3842b9dbef2SJosef Bacik 
3852b9dbef2SJosef Bacik 	/* Make sure we don't try to update the root at commit time */
3860b246afaSJeff Mahoney 	spin_lock(&fs_info->fs_roots_radix_lock);
3870b246afaSJeff Mahoney 	radix_tree_tag_clear(&fs_info->fs_roots_radix,
3882b9dbef2SJosef Bacik 			     (unsigned long)root->root_key.objectid,
3892b9dbef2SJosef Bacik 			     BTRFS_ROOT_TRANS_TAG);
3900b246afaSJeff Mahoney 	spin_unlock(&fs_info->fs_roots_radix_lock);
3912b9dbef2SJosef Bacik }
3922b9dbef2SJosef Bacik 
3937585717fSChris Mason int btrfs_record_root_in_trans(struct btrfs_trans_handle *trans,
3947585717fSChris Mason 			       struct btrfs_root *root)
3957585717fSChris Mason {
3960b246afaSJeff Mahoney 	struct btrfs_fs_info *fs_info = root->fs_info;
3970b246afaSJeff Mahoney 
39827cdeb70SMiao Xie 	if (!test_bit(BTRFS_ROOT_REF_COWS, &root->state))
3997585717fSChris Mason 		return 0;
4007585717fSChris Mason 
4017585717fSChris Mason 	/*
40227cdeb70SMiao Xie 	 * see record_root_in_trans for comments about IN_TRANS_SETUP usage
4037585717fSChris Mason 	 * and barriers
4047585717fSChris Mason 	 */
4057585717fSChris Mason 	smp_rmb();
4067585717fSChris Mason 	if (root->last_trans == trans->transid &&
40727cdeb70SMiao Xie 	    !test_bit(BTRFS_ROOT_IN_TRANS_SETUP, &root->state))
4087585717fSChris Mason 		return 0;
4097585717fSChris Mason 
4100b246afaSJeff Mahoney 	mutex_lock(&fs_info->reloc_mutex);
4116426c7adSQu Wenruo 	record_root_in_trans(trans, root, 0);
4120b246afaSJeff Mahoney 	mutex_unlock(&fs_info->reloc_mutex);
4137585717fSChris Mason 
4147585717fSChris Mason 	return 0;
4157585717fSChris Mason }
4167585717fSChris Mason 
4174a9d8bdeSMiao Xie static inline int is_transaction_blocked(struct btrfs_transaction *trans)
4184a9d8bdeSMiao Xie {
4194a9d8bdeSMiao Xie 	return (trans->state >= TRANS_STATE_BLOCKED &&
420501407aaSJosef Bacik 		trans->state < TRANS_STATE_UNBLOCKED &&
421501407aaSJosef Bacik 		!trans->aborted);
4224a9d8bdeSMiao Xie }
4234a9d8bdeSMiao Xie 
424d352ac68SChris Mason /* wait for commit against the current transaction to become unblocked
425d352ac68SChris Mason  * when this is done, it is safe to start a new transaction, but the current
426d352ac68SChris Mason  * transaction might not be fully on disk.
427d352ac68SChris Mason  */
4282ff7e61eSJeff Mahoney static void wait_current_trans(struct btrfs_fs_info *fs_info)
42979154b1bSChris Mason {
430f9295749SChris Mason 	struct btrfs_transaction *cur_trans;
43179154b1bSChris Mason 
4320b246afaSJeff Mahoney 	spin_lock(&fs_info->trans_lock);
4330b246afaSJeff Mahoney 	cur_trans = fs_info->running_transaction;
4344a9d8bdeSMiao Xie 	if (cur_trans && is_transaction_blocked(cur_trans)) {
43513c5a93eSJosef Bacik 		atomic_inc(&cur_trans->use_count);
4360b246afaSJeff Mahoney 		spin_unlock(&fs_info->trans_lock);
43772d63ed6SLi Zefan 
4380b246afaSJeff Mahoney 		wait_event(fs_info->transaction_wait,
439501407aaSJosef Bacik 			   cur_trans->state >= TRANS_STATE_UNBLOCKED ||
440501407aaSJosef Bacik 			   cur_trans->aborted);
441724e2315SJosef Bacik 		btrfs_put_transaction(cur_trans);
442a4abeea4SJosef Bacik 	} else {
4430b246afaSJeff Mahoney 		spin_unlock(&fs_info->trans_lock);
444f9295749SChris Mason 	}
44537d1aeeeSChris Mason }
44637d1aeeeSChris Mason 
4472ff7e61eSJeff Mahoney static int may_wait_transaction(struct btrfs_fs_info *fs_info, int type)
44837d1aeeeSChris Mason {
4490b246afaSJeff Mahoney 	if (test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags))
450a4abeea4SJosef Bacik 		return 0;
451a4abeea4SJosef Bacik 
452a4abeea4SJosef Bacik 	if (type == TRANS_USERSPACE)
453a22285a6SYan, Zheng 		return 1;
454a4abeea4SJosef Bacik 
455a4abeea4SJosef Bacik 	if (type == TRANS_START &&
4560b246afaSJeff Mahoney 	    !atomic_read(&fs_info->open_ioctl_trans))
457a4abeea4SJosef Bacik 		return 1;
458a4abeea4SJosef Bacik 
459a22285a6SYan, Zheng 	return 0;
460a22285a6SYan, Zheng }
461a22285a6SYan, Zheng 
46220dd2cbfSMiao Xie static inline bool need_reserve_reloc_root(struct btrfs_root *root)
46320dd2cbfSMiao Xie {
4640b246afaSJeff Mahoney 	struct btrfs_fs_info *fs_info = root->fs_info;
4650b246afaSJeff Mahoney 
4660b246afaSJeff Mahoney 	if (!fs_info->reloc_ctl ||
46727cdeb70SMiao Xie 	    !test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
46820dd2cbfSMiao Xie 	    root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID ||
46920dd2cbfSMiao Xie 	    root->reloc_root)
47020dd2cbfSMiao Xie 		return false;
47120dd2cbfSMiao Xie 
47220dd2cbfSMiao Xie 	return true;
47320dd2cbfSMiao Xie }
47420dd2cbfSMiao Xie 
47508e007d2SMiao Xie static struct btrfs_trans_handle *
4765aed1dd8SAlexandru Moise start_transaction(struct btrfs_root *root, unsigned int num_items,
4775aed1dd8SAlexandru Moise 		  unsigned int type, enum btrfs_reserve_flush_enum flush)
478a22285a6SYan, Zheng {
4790b246afaSJeff Mahoney 	struct btrfs_fs_info *fs_info = root->fs_info;
4800b246afaSJeff Mahoney 
481a22285a6SYan, Zheng 	struct btrfs_trans_handle *h;
482a22285a6SYan, Zheng 	struct btrfs_transaction *cur_trans;
483b5009945SJosef Bacik 	u64 num_bytes = 0;
484c5567237SArne Jansen 	u64 qgroup_reserved = 0;
48520dd2cbfSMiao Xie 	bool reloc_reserved = false;
48620dd2cbfSMiao Xie 	int ret;
487acce952bSliubo 
48846c4e71eSFilipe Manana 	/* Send isn't supposed to start transactions. */
4892755a0deSDavid Sterba 	ASSERT(current->journal_info != BTRFS_SEND_TRANS_STUB);
49046c4e71eSFilipe Manana 
4910b246afaSJeff Mahoney 	if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
492acce952bSliubo 		return ERR_PTR(-EROFS);
4932a1eb461SJosef Bacik 
49446c4e71eSFilipe Manana 	if (current->journal_info) {
4950860adfdSMiao Xie 		WARN_ON(type & TRANS_EXTWRITERS);
4962a1eb461SJosef Bacik 		h = current->journal_info;
4972a1eb461SJosef Bacik 		h->use_count++;
498b7d5b0a8SMiao Xie 		WARN_ON(h->use_count > 2);
4992a1eb461SJosef Bacik 		h->orig_rsv = h->block_rsv;
5002a1eb461SJosef Bacik 		h->block_rsv = NULL;
5012a1eb461SJosef Bacik 		goto got_it;
5022a1eb461SJosef Bacik 	}
503b5009945SJosef Bacik 
504b5009945SJosef Bacik 	/*
505b5009945SJosef Bacik 	 * Do the reservation before we join the transaction so we can do all
506b5009945SJosef Bacik 	 * the appropriate flushing if need be.
507b5009945SJosef Bacik 	 */
5080b246afaSJeff Mahoney 	if (num_items > 0 && root != fs_info->chunk_root) {
5090b246afaSJeff Mahoney 		qgroup_reserved = num_items * fs_info->nodesize;
5107174109cSQu Wenruo 		ret = btrfs_qgroup_reserve_meta(root, qgroup_reserved);
511c5567237SArne Jansen 		if (ret)
512c5567237SArne Jansen 			return ERR_PTR(ret);
513c5567237SArne Jansen 
5140b246afaSJeff Mahoney 		num_bytes = btrfs_calc_trans_metadata_size(fs_info, num_items);
51520dd2cbfSMiao Xie 		/*
51620dd2cbfSMiao Xie 		 * Do the reservation for the relocation root creation
51720dd2cbfSMiao Xie 		 */
518ee39b432SDavid Sterba 		if (need_reserve_reloc_root(root)) {
5190b246afaSJeff Mahoney 			num_bytes += fs_info->nodesize;
52020dd2cbfSMiao Xie 			reloc_reserved = true;
52120dd2cbfSMiao Xie 		}
52220dd2cbfSMiao Xie 
5230b246afaSJeff Mahoney 		ret = btrfs_block_rsv_add(root, &fs_info->trans_block_rsv,
52408e007d2SMiao Xie 					  num_bytes, flush);
525b5009945SJosef Bacik 		if (ret)
526843fcf35SMiao Xie 			goto reserve_fail;
527b5009945SJosef Bacik 	}
528a22285a6SYan, Zheng again:
529f2f767e7SAlexandru Moise 	h = kmem_cache_zalloc(btrfs_trans_handle_cachep, GFP_NOFS);
530843fcf35SMiao Xie 	if (!h) {
531843fcf35SMiao Xie 		ret = -ENOMEM;
532843fcf35SMiao Xie 		goto alloc_fail;
533843fcf35SMiao Xie 	}
534a22285a6SYan, Zheng 
53598114659SJosef Bacik 	/*
53698114659SJosef Bacik 	 * If we are JOIN_NOLOCK we're already committing a transaction and
53798114659SJosef Bacik 	 * waiting on this guy, so we don't need to do the sb_start_intwrite
53898114659SJosef Bacik 	 * because we're already holding a ref.  We need this because we could
53998114659SJosef Bacik 	 * have raced in and did an fsync() on a file which can kick a commit
54098114659SJosef Bacik 	 * and then we deadlock with somebody doing a freeze.
541354aa0fbSMiao Xie 	 *
542354aa0fbSMiao Xie 	 * If we are ATTACH, it means we just want to catch the current
543354aa0fbSMiao Xie 	 * transaction and commit it, so we needn't do sb_start_intwrite().
54498114659SJosef Bacik 	 */
5450860adfdSMiao Xie 	if (type & __TRANS_FREEZABLE)
5460b246afaSJeff Mahoney 		sb_start_intwrite(fs_info->sb);
547b2b5ef5cSJan Kara 
5482ff7e61eSJeff Mahoney 	if (may_wait_transaction(fs_info, type))
5492ff7e61eSJeff Mahoney 		wait_current_trans(fs_info);
550a22285a6SYan, Zheng 
551a4abeea4SJosef Bacik 	do {
5522ff7e61eSJeff Mahoney 		ret = join_transaction(fs_info, type);
553178260b2SMiao Xie 		if (ret == -EBUSY) {
5542ff7e61eSJeff Mahoney 			wait_current_trans(fs_info);
555178260b2SMiao Xie 			if (unlikely(type == TRANS_ATTACH))
556178260b2SMiao Xie 				ret = -ENOENT;
557178260b2SMiao Xie 		}
558a4abeea4SJosef Bacik 	} while (ret == -EBUSY);
559a4abeea4SJosef Bacik 
560a43f7f82SLiu Bo 	if (ret < 0)
561843fcf35SMiao Xie 		goto join_fail;
5620f7d52f4SChris Mason 
5630b246afaSJeff Mahoney 	cur_trans = fs_info->running_transaction;
564a22285a6SYan, Zheng 
565a22285a6SYan, Zheng 	h->transid = cur_trans->transid;
566a22285a6SYan, Zheng 	h->transaction = cur_trans;
567d13603efSArne Jansen 	h->root = root;
5682a1eb461SJosef Bacik 	h->use_count = 1;
56964b63580SJeff Mahoney 	h->fs_info = root->fs_info;
5707174109cSQu Wenruo 
571a698d075SMiao Xie 	h->type = type;
572d9a0540aSFilipe Manana 	h->can_flush_pending_bgs = true;
573bed92eaeSArne Jansen 	INIT_LIST_HEAD(&h->qgroup_ref_list);
574ea658badSJosef Bacik 	INIT_LIST_HEAD(&h->new_bgs);
575b7ec40d7SChris Mason 
576a22285a6SYan, Zheng 	smp_mb();
5774a9d8bdeSMiao Xie 	if (cur_trans->state >= TRANS_STATE_BLOCKED &&
5782ff7e61eSJeff Mahoney 	    may_wait_transaction(fs_info, type)) {
579abdd2e80SFilipe Manana 		current->journal_info = h;
580a22285a6SYan, Zheng 		btrfs_commit_transaction(h, root);
581a22285a6SYan, Zheng 		goto again;
582a22285a6SYan, Zheng 	}
5839ed74f2dSJosef Bacik 
584b5009945SJosef Bacik 	if (num_bytes) {
5850b246afaSJeff Mahoney 		trace_btrfs_space_reservation(fs_info, "transaction",
5862bcc0328SLiu Bo 					      h->transid, num_bytes, 1);
5870b246afaSJeff Mahoney 		h->block_rsv = &fs_info->trans_block_rsv;
588b5009945SJosef Bacik 		h->bytes_reserved = num_bytes;
58920dd2cbfSMiao Xie 		h->reloc_reserved = reloc_reserved;
590a22285a6SYan, Zheng 	}
591a22285a6SYan, Zheng 
5922a1eb461SJosef Bacik got_it:
593a4abeea4SJosef Bacik 	btrfs_record_root_in_trans(h, root);
594a22285a6SYan, Zheng 
595a22285a6SYan, Zheng 	if (!current->journal_info && type != TRANS_USERSPACE)
596a22285a6SYan, Zheng 		current->journal_info = h;
59779154b1bSChris Mason 	return h;
598843fcf35SMiao Xie 
599843fcf35SMiao Xie join_fail:
6000860adfdSMiao Xie 	if (type & __TRANS_FREEZABLE)
6010b246afaSJeff Mahoney 		sb_end_intwrite(fs_info->sb);
602843fcf35SMiao Xie 	kmem_cache_free(btrfs_trans_handle_cachep, h);
603843fcf35SMiao Xie alloc_fail:
604843fcf35SMiao Xie 	if (num_bytes)
6052ff7e61eSJeff Mahoney 		btrfs_block_rsv_release(fs_info, &fs_info->trans_block_rsv,
606843fcf35SMiao Xie 					num_bytes);
607843fcf35SMiao Xie reserve_fail:
6087174109cSQu Wenruo 	btrfs_qgroup_free_meta(root, qgroup_reserved);
609843fcf35SMiao Xie 	return ERR_PTR(ret);
61079154b1bSChris Mason }
61179154b1bSChris Mason 
612f9295749SChris Mason struct btrfs_trans_handle *btrfs_start_transaction(struct btrfs_root *root,
6135aed1dd8SAlexandru Moise 						   unsigned int num_items)
614f9295749SChris Mason {
61508e007d2SMiao Xie 	return start_transaction(root, num_items, TRANS_START,
61608e007d2SMiao Xie 				 BTRFS_RESERVE_FLUSH_ALL);
617f9295749SChris Mason }
6188eab77ffSFilipe Manana struct btrfs_trans_handle *btrfs_start_transaction_fallback_global_rsv(
6198eab77ffSFilipe Manana 					struct btrfs_root *root,
6208eab77ffSFilipe Manana 					unsigned int num_items,
6218eab77ffSFilipe Manana 					int min_factor)
6228eab77ffSFilipe Manana {
6230b246afaSJeff Mahoney 	struct btrfs_fs_info *fs_info = root->fs_info;
6248eab77ffSFilipe Manana 	struct btrfs_trans_handle *trans;
6258eab77ffSFilipe Manana 	u64 num_bytes;
6268eab77ffSFilipe Manana 	int ret;
6278eab77ffSFilipe Manana 
6288eab77ffSFilipe Manana 	trans = btrfs_start_transaction(root, num_items);
6298eab77ffSFilipe Manana 	if (!IS_ERR(trans) || PTR_ERR(trans) != -ENOSPC)
6308eab77ffSFilipe Manana 		return trans;
6318eab77ffSFilipe Manana 
6328eab77ffSFilipe Manana 	trans = btrfs_start_transaction(root, 0);
6338eab77ffSFilipe Manana 	if (IS_ERR(trans))
6348eab77ffSFilipe Manana 		return trans;
6358eab77ffSFilipe Manana 
6360b246afaSJeff Mahoney 	num_bytes = btrfs_calc_trans_metadata_size(fs_info, num_items);
6370b246afaSJeff Mahoney 	ret = btrfs_cond_migrate_bytes(fs_info, &fs_info->trans_block_rsv,
6380b246afaSJeff Mahoney 				       num_bytes, min_factor);
6398eab77ffSFilipe Manana 	if (ret) {
6408eab77ffSFilipe Manana 		btrfs_end_transaction(trans, root);
6418eab77ffSFilipe Manana 		return ERR_PTR(ret);
6428eab77ffSFilipe Manana 	}
6438eab77ffSFilipe Manana 
6440b246afaSJeff Mahoney 	trans->block_rsv = &fs_info->trans_block_rsv;
6458eab77ffSFilipe Manana 	trans->bytes_reserved = num_bytes;
6460b246afaSJeff Mahoney 	trace_btrfs_space_reservation(fs_info, "transaction",
64788d3a5aaSJosef Bacik 				      trans->transid, num_bytes, 1);
6488eab77ffSFilipe Manana 
6498eab77ffSFilipe Manana 	return trans;
6508eab77ffSFilipe Manana }
6518407aa46SMiao Xie 
65208e007d2SMiao Xie struct btrfs_trans_handle *btrfs_start_transaction_lflush(
6535aed1dd8SAlexandru Moise 					struct btrfs_root *root,
6545aed1dd8SAlexandru Moise 					unsigned int num_items)
6558407aa46SMiao Xie {
65608e007d2SMiao Xie 	return start_transaction(root, num_items, TRANS_START,
65708e007d2SMiao Xie 				 BTRFS_RESERVE_FLUSH_LIMIT);
6588407aa46SMiao Xie }
6598407aa46SMiao Xie 
6607a7eaa40SJosef Bacik struct btrfs_trans_handle *btrfs_join_transaction(struct btrfs_root *root)
661f9295749SChris Mason {
662575a75d6SAlexandru Moise 	return start_transaction(root, 0, TRANS_JOIN,
663575a75d6SAlexandru Moise 				 BTRFS_RESERVE_NO_FLUSH);
664f9295749SChris Mason }
665f9295749SChris Mason 
6667a7eaa40SJosef Bacik struct btrfs_trans_handle *btrfs_join_transaction_nolock(struct btrfs_root *root)
6670af3d00bSJosef Bacik {
668575a75d6SAlexandru Moise 	return start_transaction(root, 0, TRANS_JOIN_NOLOCK,
669575a75d6SAlexandru Moise 				 BTRFS_RESERVE_NO_FLUSH);
6700af3d00bSJosef Bacik }
6710af3d00bSJosef Bacik 
6727a7eaa40SJosef Bacik struct btrfs_trans_handle *btrfs_start_ioctl_transaction(struct btrfs_root *root)
6739ca9ee09SSage Weil {
674575a75d6SAlexandru Moise 	return start_transaction(root, 0, TRANS_USERSPACE,
675575a75d6SAlexandru Moise 				 BTRFS_RESERVE_NO_FLUSH);
6769ca9ee09SSage Weil }
6779ca9ee09SSage Weil 
678d4edf39bSMiao Xie /*
679d4edf39bSMiao Xie  * btrfs_attach_transaction() - catch the running transaction
680d4edf39bSMiao Xie  *
681d4edf39bSMiao Xie  * It is used when we want to commit the current the transaction, but
682d4edf39bSMiao Xie  * don't want to start a new one.
683d4edf39bSMiao Xie  *
684d4edf39bSMiao Xie  * Note: If this function return -ENOENT, it just means there is no
685d4edf39bSMiao Xie  * running transaction. But it is possible that the inactive transaction
686d4edf39bSMiao Xie  * is still in the memory, not fully on disk. If you hope there is no
687d4edf39bSMiao Xie  * inactive transaction in the fs when -ENOENT is returned, you should
688d4edf39bSMiao Xie  * invoke
689d4edf39bSMiao Xie  *     btrfs_attach_transaction_barrier()
690d4edf39bSMiao Xie  */
691354aa0fbSMiao Xie struct btrfs_trans_handle *btrfs_attach_transaction(struct btrfs_root *root)
69260376ce4SJosef Bacik {
693575a75d6SAlexandru Moise 	return start_transaction(root, 0, TRANS_ATTACH,
694575a75d6SAlexandru Moise 				 BTRFS_RESERVE_NO_FLUSH);
69560376ce4SJosef Bacik }
69660376ce4SJosef Bacik 
697d4edf39bSMiao Xie /*
69890b6d283SWang Sheng-Hui  * btrfs_attach_transaction_barrier() - catch the running transaction
699d4edf39bSMiao Xie  *
700d4edf39bSMiao Xie  * It is similar to the above function, the differentia is this one
701d4edf39bSMiao Xie  * will wait for all the inactive transactions until they fully
702d4edf39bSMiao Xie  * complete.
703d4edf39bSMiao Xie  */
704d4edf39bSMiao Xie struct btrfs_trans_handle *
705d4edf39bSMiao Xie btrfs_attach_transaction_barrier(struct btrfs_root *root)
706d4edf39bSMiao Xie {
707d4edf39bSMiao Xie 	struct btrfs_trans_handle *trans;
708d4edf39bSMiao Xie 
709575a75d6SAlexandru Moise 	trans = start_transaction(root, 0, TRANS_ATTACH,
710575a75d6SAlexandru Moise 				  BTRFS_RESERVE_NO_FLUSH);
711d4edf39bSMiao Xie 	if (IS_ERR(trans) && PTR_ERR(trans) == -ENOENT)
7122ff7e61eSJeff Mahoney 		btrfs_wait_for_commit(root->fs_info, 0);
713d4edf39bSMiao Xie 
714d4edf39bSMiao Xie 	return trans;
715d4edf39bSMiao Xie }
716d4edf39bSMiao Xie 
717d352ac68SChris Mason /* wait for a transaction commit to be fully complete */
7182ff7e61eSJeff Mahoney static noinline void wait_for_commit(struct btrfs_transaction *commit)
71989ce8a63SChris Mason {
7204a9d8bdeSMiao Xie 	wait_event(commit->commit_wait, commit->state == TRANS_STATE_COMPLETED);
72189ce8a63SChris Mason }
72289ce8a63SChris Mason 
7232ff7e61eSJeff Mahoney int btrfs_wait_for_commit(struct btrfs_fs_info *fs_info, u64 transid)
72446204592SSage Weil {
72546204592SSage Weil 	struct btrfs_transaction *cur_trans = NULL, *t;
7268cd2807fSMiao Xie 	int ret = 0;
72746204592SSage Weil 
72846204592SSage Weil 	if (transid) {
7290b246afaSJeff Mahoney 		if (transid <= fs_info->last_trans_committed)
730a4abeea4SJosef Bacik 			goto out;
73146204592SSage Weil 
73246204592SSage Weil 		/* find specified transaction */
7330b246afaSJeff Mahoney 		spin_lock(&fs_info->trans_lock);
7340b246afaSJeff Mahoney 		list_for_each_entry(t, &fs_info->trans_list, list) {
73546204592SSage Weil 			if (t->transid == transid) {
73646204592SSage Weil 				cur_trans = t;
737a4abeea4SJosef Bacik 				atomic_inc(&cur_trans->use_count);
7388cd2807fSMiao Xie 				ret = 0;
73946204592SSage Weil 				break;
74046204592SSage Weil 			}
7418cd2807fSMiao Xie 			if (t->transid > transid) {
7428cd2807fSMiao Xie 				ret = 0;
74346204592SSage Weil 				break;
74446204592SSage Weil 			}
7458cd2807fSMiao Xie 		}
7460b246afaSJeff Mahoney 		spin_unlock(&fs_info->trans_lock);
74742383020SSage Weil 
74842383020SSage Weil 		/*
74942383020SSage Weil 		 * The specified transaction doesn't exist, or we
75042383020SSage Weil 		 * raced with btrfs_commit_transaction
75142383020SSage Weil 		 */
75242383020SSage Weil 		if (!cur_trans) {
7530b246afaSJeff Mahoney 			if (transid > fs_info->last_trans_committed)
75442383020SSage Weil 				ret = -EINVAL;
7558cd2807fSMiao Xie 			goto out;
75642383020SSage Weil 		}
75746204592SSage Weil 	} else {
75846204592SSage Weil 		/* find newest transaction that is committing | committed */
7590b246afaSJeff Mahoney 		spin_lock(&fs_info->trans_lock);
7600b246afaSJeff Mahoney 		list_for_each_entry_reverse(t, &fs_info->trans_list,
76146204592SSage Weil 					    list) {
7624a9d8bdeSMiao Xie 			if (t->state >= TRANS_STATE_COMMIT_START) {
7634a9d8bdeSMiao Xie 				if (t->state == TRANS_STATE_COMPLETED)
7643473f3c0SJosef Bacik 					break;
76546204592SSage Weil 				cur_trans = t;
766a4abeea4SJosef Bacik 				atomic_inc(&cur_trans->use_count);
76746204592SSage Weil 				break;
76846204592SSage Weil 			}
76946204592SSage Weil 		}
7700b246afaSJeff Mahoney 		spin_unlock(&fs_info->trans_lock);
77146204592SSage Weil 		if (!cur_trans)
772a4abeea4SJosef Bacik 			goto out;  /* nothing committing|committed */
77346204592SSage Weil 	}
77446204592SSage Weil 
7752ff7e61eSJeff Mahoney 	wait_for_commit(cur_trans);
776724e2315SJosef Bacik 	btrfs_put_transaction(cur_trans);
777a4abeea4SJosef Bacik out:
77846204592SSage Weil 	return ret;
77946204592SSage Weil }
78046204592SSage Weil 
7812ff7e61eSJeff Mahoney void btrfs_throttle(struct btrfs_fs_info *fs_info)
78237d1aeeeSChris Mason {
7830b246afaSJeff Mahoney 	if (!atomic_read(&fs_info->open_ioctl_trans))
7842ff7e61eSJeff Mahoney 		wait_current_trans(fs_info);
78537d1aeeeSChris Mason }
78637d1aeeeSChris Mason 
7872ff7e61eSJeff Mahoney static int should_end_transaction(struct btrfs_trans_handle *trans)
7888929ecfaSYan, Zheng {
7892ff7e61eSJeff Mahoney 	struct btrfs_fs_info *fs_info = trans->fs_info;
7900b246afaSJeff Mahoney 
7910b246afaSJeff Mahoney 	if (fs_info->global_block_rsv.space_info->full &&
7922ff7e61eSJeff Mahoney 	    btrfs_check_space_for_delayed_refs(trans, fs_info))
7931be41b78SJosef Bacik 		return 1;
79436ba022aSJosef Bacik 
7952ff7e61eSJeff Mahoney 	return !!btrfs_block_rsv_check(&fs_info->global_block_rsv, 5);
7968929ecfaSYan, Zheng }
7978929ecfaSYan, Zheng 
7988929ecfaSYan, Zheng int btrfs_should_end_transaction(struct btrfs_trans_handle *trans,
7998929ecfaSYan, Zheng 				 struct btrfs_root *root)
8008929ecfaSYan, Zheng {
8012ff7e61eSJeff Mahoney 	struct btrfs_fs_info *fs_info = root->fs_info;
8028929ecfaSYan, Zheng 	struct btrfs_transaction *cur_trans = trans->transaction;
8038929ecfaSYan, Zheng 	int updates;
80449b25e05SJeff Mahoney 	int err;
8058929ecfaSYan, Zheng 
806a4abeea4SJosef Bacik 	smp_mb();
8074a9d8bdeSMiao Xie 	if (cur_trans->state >= TRANS_STATE_BLOCKED ||
8084a9d8bdeSMiao Xie 	    cur_trans->delayed_refs.flushing)
8098929ecfaSYan, Zheng 		return 1;
8108929ecfaSYan, Zheng 
8118929ecfaSYan, Zheng 	updates = trans->delayed_ref_updates;
8128929ecfaSYan, Zheng 	trans->delayed_ref_updates = 0;
81349b25e05SJeff Mahoney 	if (updates) {
8142ff7e61eSJeff Mahoney 		err = btrfs_run_delayed_refs(trans, fs_info, updates * 2);
81549b25e05SJeff Mahoney 		if (err) /* Error code will also eval true */
81649b25e05SJeff Mahoney 			return err;
81749b25e05SJeff Mahoney 	}
8188929ecfaSYan, Zheng 
8192ff7e61eSJeff Mahoney 	return should_end_transaction(trans);
8208929ecfaSYan, Zheng }
8218929ecfaSYan, Zheng 
82289ce8a63SChris Mason static int __btrfs_end_transaction(struct btrfs_trans_handle *trans,
823a698d075SMiao Xie 			  struct btrfs_root *root, int throttle)
82479154b1bSChris Mason {
8258929ecfaSYan, Zheng 	struct btrfs_transaction *cur_trans = trans->transaction;
826ab78c84dSChris Mason 	struct btrfs_fs_info *info = root->fs_info;
82731b9655fSJosef Bacik 	u64 transid = trans->transid;
8281be41b78SJosef Bacik 	unsigned long cur = trans->delayed_ref_updates;
829a698d075SMiao Xie 	int lock = (trans->type != TRANS_JOIN_NOLOCK);
8304edc2ca3SDave Jones 	int err = 0;
831a79b7d4bSChris Mason 	int must_run_delayed_refs = 0;
832d6e4a428SChris Mason 
8333bbb24b2SJosef Bacik 	if (trans->use_count > 1) {
8343bbb24b2SJosef Bacik 		trans->use_count--;
8352a1eb461SJosef Bacik 		trans->block_rsv = trans->orig_rsv;
8362a1eb461SJosef Bacik 		return 0;
8372a1eb461SJosef Bacik 	}
8382a1eb461SJosef Bacik 
8392ff7e61eSJeff Mahoney 	btrfs_trans_release_metadata(trans, info);
8404c13d758SJosef Bacik 	trans->block_rsv = NULL;
841c5567237SArne Jansen 
842ea658badSJosef Bacik 	if (!list_empty(&trans->new_bgs))
8432ff7e61eSJeff Mahoney 		btrfs_create_pending_block_groups(trans, info);
844ea658badSJosef Bacik 
845c3e69d58SChris Mason 	trans->delayed_ref_updates = 0;
846a79b7d4bSChris Mason 	if (!trans->sync) {
847a79b7d4bSChris Mason 		must_run_delayed_refs =
8482ff7e61eSJeff Mahoney 			btrfs_should_throttle_delayed_refs(trans, info);
8490a2b2a84SJosef Bacik 		cur = max_t(unsigned long, cur, 32);
850a79b7d4bSChris Mason 
851a79b7d4bSChris Mason 		/*
852a79b7d4bSChris Mason 		 * don't make the caller wait if they are from a NOLOCK
853a79b7d4bSChris Mason 		 * or ATTACH transaction, it will deadlock with commit
854a79b7d4bSChris Mason 		 */
855a79b7d4bSChris Mason 		if (must_run_delayed_refs == 1 &&
856a79b7d4bSChris Mason 		    (trans->type & (__TRANS_JOIN_NOLOCK | __TRANS_ATTACH)))
857a79b7d4bSChris Mason 			must_run_delayed_refs = 2;
85856bec294SChris Mason 	}
859bb721703SChris Mason 
8602ff7e61eSJeff Mahoney 	btrfs_trans_release_metadata(trans, info);
8610e721106SJosef Bacik 	trans->block_rsv = NULL;
86256bec294SChris Mason 
863ea658badSJosef Bacik 	if (!list_empty(&trans->new_bgs))
8642ff7e61eSJeff Mahoney 		btrfs_create_pending_block_groups(trans, info);
865ea658badSJosef Bacik 
8664fbcdf66SFilipe Manana 	btrfs_trans_release_chunk_metadata(trans);
8674fbcdf66SFilipe Manana 
8680b246afaSJeff Mahoney 	if (lock && !atomic_read(&info->open_ioctl_trans) &&
8692ff7e61eSJeff Mahoney 	    should_end_transaction(trans) &&
8704a9d8bdeSMiao Xie 	    ACCESS_ONCE(cur_trans->state) == TRANS_STATE_RUNNING) {
8714a9d8bdeSMiao Xie 		spin_lock(&info->trans_lock);
8724a9d8bdeSMiao Xie 		if (cur_trans->state == TRANS_STATE_RUNNING)
8734a9d8bdeSMiao Xie 			cur_trans->state = TRANS_STATE_BLOCKED;
8744a9d8bdeSMiao Xie 		spin_unlock(&info->trans_lock);
875a4abeea4SJosef Bacik 	}
8768929ecfaSYan, Zheng 
8774a9d8bdeSMiao Xie 	if (lock && ACCESS_ONCE(cur_trans->state) == TRANS_STATE_BLOCKED) {
8783bbb24b2SJosef Bacik 		if (throttle)
8798929ecfaSYan, Zheng 			return btrfs_commit_transaction(trans, root);
8803bbb24b2SJosef Bacik 		else
8818929ecfaSYan, Zheng 			wake_up_process(info->transaction_kthread);
8828929ecfaSYan, Zheng 	}
8838929ecfaSYan, Zheng 
8840860adfdSMiao Xie 	if (trans->type & __TRANS_FREEZABLE)
8850b246afaSJeff Mahoney 		sb_end_intwrite(info->sb);
8866df7881aSJosef Bacik 
8878929ecfaSYan, Zheng 	WARN_ON(cur_trans != info->running_transaction);
88813c5a93eSJosef Bacik 	WARN_ON(atomic_read(&cur_trans->num_writers) < 1);
88913c5a93eSJosef Bacik 	atomic_dec(&cur_trans->num_writers);
8900860adfdSMiao Xie 	extwriter_counter_dec(cur_trans, trans->type);
89189ce8a63SChris Mason 
892a83342aaSDavid Sterba 	/*
893a83342aaSDavid Sterba 	 * Make sure counter is updated before we wake up waiters.
894a83342aaSDavid Sterba 	 */
89599d16cbcSSage Weil 	smp_mb();
89679154b1bSChris Mason 	if (waitqueue_active(&cur_trans->writer_wait))
89779154b1bSChris Mason 		wake_up(&cur_trans->writer_wait);
898724e2315SJosef Bacik 	btrfs_put_transaction(cur_trans);
8999ed74f2dSJosef Bacik 
9009ed74f2dSJosef Bacik 	if (current->journal_info == trans)
9019ed74f2dSJosef Bacik 		current->journal_info = NULL;
902ab78c84dSChris Mason 
90324bbcf04SYan, Zheng 	if (throttle)
9042ff7e61eSJeff Mahoney 		btrfs_run_delayed_iputs(info);
90524bbcf04SYan, Zheng 
90649b25e05SJeff Mahoney 	if (trans->aborted ||
9070b246afaSJeff Mahoney 	    test_bit(BTRFS_FS_STATE_ERROR, &info->fs_state)) {
9084e121c06SJosef Bacik 		wake_up_process(info->transaction_kthread);
9094edc2ca3SDave Jones 		err = -EIO;
9104e121c06SJosef Bacik 	}
911edf39272SJan Schmidt 	assert_qgroups_uptodate(trans);
91249b25e05SJeff Mahoney 
9134edc2ca3SDave Jones 	kmem_cache_free(btrfs_trans_handle_cachep, trans);
914a79b7d4bSChris Mason 	if (must_run_delayed_refs) {
9152ff7e61eSJeff Mahoney 		btrfs_async_run_delayed_refs(info, cur, transid,
916a79b7d4bSChris Mason 					     must_run_delayed_refs == 1);
917a79b7d4bSChris Mason 	}
9184edc2ca3SDave Jones 	return err;
91979154b1bSChris Mason }
92079154b1bSChris Mason 
92189ce8a63SChris Mason int btrfs_end_transaction(struct btrfs_trans_handle *trans,
92289ce8a63SChris Mason 			  struct btrfs_root *root)
92389ce8a63SChris Mason {
92498ad43beSWang Shilong 	return __btrfs_end_transaction(trans, root, 0);
92589ce8a63SChris Mason }
92689ce8a63SChris Mason 
92789ce8a63SChris Mason int btrfs_end_transaction_throttle(struct btrfs_trans_handle *trans,
92889ce8a63SChris Mason 				   struct btrfs_root *root)
92989ce8a63SChris Mason {
93098ad43beSWang Shilong 	return __btrfs_end_transaction(trans, root, 1);
93116cdcec7SMiao Xie }
93216cdcec7SMiao Xie 
933d352ac68SChris Mason /*
934d352ac68SChris Mason  * when btree blocks are allocated, they have some corresponding bits set for
935d352ac68SChris Mason  * them in one of two extent_io trees.  This is used to make sure all of
936690587d1SChris Mason  * those extents are sent to disk but does not wait on them
937d352ac68SChris Mason  */
9382ff7e61eSJeff Mahoney int btrfs_write_marked_extents(struct btrfs_fs_info *fs_info,
9398cef4e16SYan, Zheng 			       struct extent_io_tree *dirty_pages, int mark)
94079154b1bSChris Mason {
941777e6bd7SChris Mason 	int err = 0;
9427c4452b9SChris Mason 	int werr = 0;
9430b246afaSJeff Mahoney 	struct address_space *mapping = fs_info->btree_inode->i_mapping;
944e6138876SJosef Bacik 	struct extent_state *cached_state = NULL;
945777e6bd7SChris Mason 	u64 start = 0;
9465f39d397SChris Mason 	u64 end;
9477c4452b9SChris Mason 
9481728366eSJosef Bacik 	while (!find_first_extent_bit(dirty_pages, start, &start, &end,
949e6138876SJosef Bacik 				      mark, &cached_state)) {
950663dfbb0SFilipe Manana 		bool wait_writeback = false;
951663dfbb0SFilipe Manana 
952663dfbb0SFilipe Manana 		err = convert_extent_bit(dirty_pages, start, end,
953663dfbb0SFilipe Manana 					 EXTENT_NEED_WAIT,
954210aa277SDavid Sterba 					 mark, &cached_state);
955663dfbb0SFilipe Manana 		/*
956663dfbb0SFilipe Manana 		 * convert_extent_bit can return -ENOMEM, which is most of the
957663dfbb0SFilipe Manana 		 * time a temporary error. So when it happens, ignore the error
958663dfbb0SFilipe Manana 		 * and wait for writeback of this range to finish - because we
959663dfbb0SFilipe Manana 		 * failed to set the bit EXTENT_NEED_WAIT for the range, a call
960663dfbb0SFilipe Manana 		 * to btrfs_wait_marked_extents() would not know that writeback
961663dfbb0SFilipe Manana 		 * for this range started and therefore wouldn't wait for it to
962663dfbb0SFilipe Manana 		 * finish - we don't want to commit a superblock that points to
963663dfbb0SFilipe Manana 		 * btree nodes/leafs for which writeback hasn't finished yet
964663dfbb0SFilipe Manana 		 * (and without errors).
965663dfbb0SFilipe Manana 		 * We cleanup any entries left in the io tree when committing
966663dfbb0SFilipe Manana 		 * the transaction (through clear_btree_io_tree()).
967663dfbb0SFilipe Manana 		 */
968663dfbb0SFilipe Manana 		if (err == -ENOMEM) {
969663dfbb0SFilipe Manana 			err = 0;
970663dfbb0SFilipe Manana 			wait_writeback = true;
971663dfbb0SFilipe Manana 		}
972663dfbb0SFilipe Manana 		if (!err)
9731728366eSJosef Bacik 			err = filemap_fdatawrite_range(mapping, start, end);
9747c4452b9SChris Mason 		if (err)
9757c4452b9SChris Mason 			werr = err;
976663dfbb0SFilipe Manana 		else if (wait_writeback)
977663dfbb0SFilipe Manana 			werr = filemap_fdatawait_range(mapping, start, end);
978e38e2ed7SFilipe Manana 		free_extent_state(cached_state);
979663dfbb0SFilipe Manana 		cached_state = NULL;
9801728366eSJosef Bacik 		cond_resched();
9811728366eSJosef Bacik 		start = end + 1;
9827c4452b9SChris Mason 	}
983690587d1SChris Mason 	return werr;
984690587d1SChris Mason }
985690587d1SChris Mason 
986690587d1SChris Mason /*
987690587d1SChris Mason  * when btree blocks are allocated, they have some corresponding bits set for
988690587d1SChris Mason  * them in one of two extent_io trees.  This is used to make sure all of
989690587d1SChris Mason  * those extents are on disk for transaction or log commit.  We wait
990690587d1SChris Mason  * on all the pages and clear them from the dirty pages state tree
991690587d1SChris Mason  */
992690587d1SChris Mason int btrfs_wait_marked_extents(struct btrfs_root *root,
9938cef4e16SYan, Zheng 			      struct extent_io_tree *dirty_pages, int mark)
994690587d1SChris Mason {
995690587d1SChris Mason 	int err = 0;
996690587d1SChris Mason 	int werr = 0;
9970b246afaSJeff Mahoney 	struct btrfs_fs_info *fs_info = root->fs_info;
9980b246afaSJeff Mahoney 	struct address_space *mapping = fs_info->btree_inode->i_mapping;
999e6138876SJosef Bacik 	struct extent_state *cached_state = NULL;
1000690587d1SChris Mason 	u64 start = 0;
1001690587d1SChris Mason 	u64 end;
1002656f30dbSFilipe Manana 	bool errors = false;
1003690587d1SChris Mason 
10041728366eSJosef Bacik 	while (!find_first_extent_bit(dirty_pages, start, &start, &end,
1005e6138876SJosef Bacik 				      EXTENT_NEED_WAIT, &cached_state)) {
1006663dfbb0SFilipe Manana 		/*
1007663dfbb0SFilipe Manana 		 * Ignore -ENOMEM errors returned by clear_extent_bit().
1008663dfbb0SFilipe Manana 		 * When committing the transaction, we'll remove any entries
1009663dfbb0SFilipe Manana 		 * left in the io tree. For a log commit, we don't remove them
1010663dfbb0SFilipe Manana 		 * after committing the log because the tree can be accessed
1011663dfbb0SFilipe Manana 		 * concurrently - we do it only at transaction commit time when
1012663dfbb0SFilipe Manana 		 * it's safe to do it (through clear_btree_io_tree()).
1013663dfbb0SFilipe Manana 		 */
1014663dfbb0SFilipe Manana 		err = clear_extent_bit(dirty_pages, start, end,
1015663dfbb0SFilipe Manana 				       EXTENT_NEED_WAIT,
1016e6138876SJosef Bacik 				       0, 0, &cached_state, GFP_NOFS);
1017663dfbb0SFilipe Manana 		if (err == -ENOMEM)
1018663dfbb0SFilipe Manana 			err = 0;
1019663dfbb0SFilipe Manana 		if (!err)
10201728366eSJosef Bacik 			err = filemap_fdatawait_range(mapping, start, end);
1021777e6bd7SChris Mason 		if (err)
1022777e6bd7SChris Mason 			werr = err;
1023e38e2ed7SFilipe Manana 		free_extent_state(cached_state);
1024e38e2ed7SFilipe Manana 		cached_state = NULL;
1025777e6bd7SChris Mason 		cond_resched();
10261728366eSJosef Bacik 		start = end + 1;
1027777e6bd7SChris Mason 	}
10287c4452b9SChris Mason 	if (err)
10297c4452b9SChris Mason 		werr = err;
1030656f30dbSFilipe Manana 
1031656f30dbSFilipe Manana 	if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) {
1032656f30dbSFilipe Manana 		if ((mark & EXTENT_DIRTY) &&
10330b246afaSJeff Mahoney 		    test_and_clear_bit(BTRFS_FS_LOG1_ERR, &fs_info->flags))
1034656f30dbSFilipe Manana 			errors = true;
1035656f30dbSFilipe Manana 
1036656f30dbSFilipe Manana 		if ((mark & EXTENT_NEW) &&
10370b246afaSJeff Mahoney 		    test_and_clear_bit(BTRFS_FS_LOG2_ERR, &fs_info->flags))
1038656f30dbSFilipe Manana 			errors = true;
1039656f30dbSFilipe Manana 	} else {
10400b246afaSJeff Mahoney 		if (test_and_clear_bit(BTRFS_FS_BTREE_ERR, &fs_info->flags))
1041656f30dbSFilipe Manana 			errors = true;
1042656f30dbSFilipe Manana 	}
1043656f30dbSFilipe Manana 
1044656f30dbSFilipe Manana 	if (errors && !werr)
1045656f30dbSFilipe Manana 		werr = -EIO;
1046656f30dbSFilipe Manana 
10477c4452b9SChris Mason 	return werr;
104879154b1bSChris Mason }
104979154b1bSChris Mason 
1050690587d1SChris Mason /*
1051690587d1SChris Mason  * when btree blocks are allocated, they have some corresponding bits set for
1052690587d1SChris Mason  * them in one of two extent_io trees.  This is used to make sure all of
1053690587d1SChris Mason  * those extents are on disk for transaction or log commit
1054690587d1SChris Mason  */
1055171170c1SSergei Trofimovich static int btrfs_write_and_wait_marked_extents(struct btrfs_root *root,
10568cef4e16SYan, Zheng 				struct extent_io_tree *dirty_pages, int mark)
1057690587d1SChris Mason {
1058690587d1SChris Mason 	int ret;
1059690587d1SChris Mason 	int ret2;
1060c6adc9ccSMiao Xie 	struct blk_plug plug;
1061690587d1SChris Mason 
1062c6adc9ccSMiao Xie 	blk_start_plug(&plug);
10632ff7e61eSJeff Mahoney 	ret = btrfs_write_marked_extents(root->fs_info, dirty_pages, mark);
1064c6adc9ccSMiao Xie 	blk_finish_plug(&plug);
10658cef4e16SYan, Zheng 	ret2 = btrfs_wait_marked_extents(root, dirty_pages, mark);
1066bf0da8c1SChris Mason 
1067bf0da8c1SChris Mason 	if (ret)
1068bf0da8c1SChris Mason 		return ret;
1069bf0da8c1SChris Mason 	if (ret2)
1070bf0da8c1SChris Mason 		return ret2;
1071bf0da8c1SChris Mason 	return 0;
1072690587d1SChris Mason }
1073690587d1SChris Mason 
1074663dfbb0SFilipe Manana static int btrfs_write_and_wait_transaction(struct btrfs_trans_handle *trans,
1075d0c803c4SChris Mason 					    struct btrfs_root *root)
1076d0c803c4SChris Mason {
1077663dfbb0SFilipe Manana 	int ret;
1078663dfbb0SFilipe Manana 
1079663dfbb0SFilipe Manana 	ret = btrfs_write_and_wait_marked_extents(root,
10808cef4e16SYan, Zheng 					   &trans->transaction->dirty_pages,
10818cef4e16SYan, Zheng 					   EXTENT_DIRTY);
1082663dfbb0SFilipe Manana 	clear_btree_io_tree(&trans->transaction->dirty_pages);
1083663dfbb0SFilipe Manana 
1084663dfbb0SFilipe Manana 	return ret;
1085d0c803c4SChris Mason }
1086d0c803c4SChris Mason 
1087d352ac68SChris Mason /*
1088d352ac68SChris Mason  * this is used to update the root pointer in the tree of tree roots.
1089d352ac68SChris Mason  *
1090d352ac68SChris Mason  * But, in the case of the extent allocation tree, updating the root
1091d352ac68SChris Mason  * pointer may allocate blocks which may change the root of the extent
1092d352ac68SChris Mason  * allocation tree.
1093d352ac68SChris Mason  *
1094d352ac68SChris Mason  * So, this loops and repeats and makes sure the cowonly root didn't
1095d352ac68SChris Mason  * change while the root pointer was being updated in the metadata.
1096d352ac68SChris Mason  */
10970b86a832SChris Mason static int update_cowonly_root(struct btrfs_trans_handle *trans,
109879154b1bSChris Mason 			       struct btrfs_root *root)
109979154b1bSChris Mason {
110079154b1bSChris Mason 	int ret;
11010b86a832SChris Mason 	u64 old_root_bytenr;
110286b9f2ecSYan, Zheng 	u64 old_root_used;
11030b246afaSJeff Mahoney 	struct btrfs_fs_info *fs_info = root->fs_info;
11040b246afaSJeff Mahoney 	struct btrfs_root *tree_root = fs_info->tree_root;
110579154b1bSChris Mason 
110686b9f2ecSYan, Zheng 	old_root_used = btrfs_root_used(&root->root_item);
110756bec294SChris Mason 
110879154b1bSChris Mason 	while (1) {
11090b86a832SChris Mason 		old_root_bytenr = btrfs_root_bytenr(&root->root_item);
111086b9f2ecSYan, Zheng 		if (old_root_bytenr == root->node->start &&
1111ea526d18SJosef Bacik 		    old_root_used == btrfs_root_used(&root->root_item))
111279154b1bSChris Mason 			break;
111387ef2bb4SChris Mason 
11145d4f98a2SYan Zheng 		btrfs_set_root_node(&root->root_item, root->node);
111579154b1bSChris Mason 		ret = btrfs_update_root(trans, tree_root,
11160b86a832SChris Mason 					&root->root_key,
11170b86a832SChris Mason 					&root->root_item);
111849b25e05SJeff Mahoney 		if (ret)
111949b25e05SJeff Mahoney 			return ret;
112056bec294SChris Mason 
112186b9f2ecSYan, Zheng 		old_root_used = btrfs_root_used(&root->root_item);
1122e7070be1SJosef Bacik 	}
1123276e680dSYan Zheng 
11240b86a832SChris Mason 	return 0;
11250b86a832SChris Mason }
11260b86a832SChris Mason 
1127d352ac68SChris Mason /*
1128d352ac68SChris Mason  * update all the cowonly tree roots on disk
112949b25e05SJeff Mahoney  *
113049b25e05SJeff Mahoney  * The error handling in this function may not be obvious. Any of the
113149b25e05SJeff Mahoney  * failures will cause the file system to go offline. We still need
113249b25e05SJeff Mahoney  * to clean up the delayed refs.
1133d352ac68SChris Mason  */
11345d4f98a2SYan Zheng static noinline int commit_cowonly_roots(struct btrfs_trans_handle *trans,
11352ff7e61eSJeff Mahoney 					 struct btrfs_fs_info *fs_info)
11360b86a832SChris Mason {
1137ea526d18SJosef Bacik 	struct list_head *dirty_bgs = &trans->transaction->dirty_bgs;
11381bbc621eSChris Mason 	struct list_head *io_bgs = &trans->transaction->io_bgs;
11390b86a832SChris Mason 	struct list_head *next;
114084234f3aSYan Zheng 	struct extent_buffer *eb;
114156bec294SChris Mason 	int ret;
114284234f3aSYan Zheng 
114384234f3aSYan Zheng 	eb = btrfs_lock_root_node(fs_info->tree_root);
114449b25e05SJeff Mahoney 	ret = btrfs_cow_block(trans, fs_info->tree_root, eb, NULL,
114549b25e05SJeff Mahoney 			      0, &eb);
114684234f3aSYan Zheng 	btrfs_tree_unlock(eb);
114784234f3aSYan Zheng 	free_extent_buffer(eb);
11480b86a832SChris Mason 
114949b25e05SJeff Mahoney 	if (ret)
115049b25e05SJeff Mahoney 		return ret;
115149b25e05SJeff Mahoney 
11522ff7e61eSJeff Mahoney 	ret = btrfs_run_delayed_refs(trans, fs_info, (unsigned long)-1);
115349b25e05SJeff Mahoney 	if (ret)
115449b25e05SJeff Mahoney 		return ret;
115587ef2bb4SChris Mason 
11560b246afaSJeff Mahoney 	ret = btrfs_run_dev_stats(trans, fs_info);
1157c16ce190SJosef Bacik 	if (ret)
1158c16ce190SJosef Bacik 		return ret;
11590b246afaSJeff Mahoney 	ret = btrfs_run_dev_replace(trans, fs_info);
1160c16ce190SJosef Bacik 	if (ret)
1161c16ce190SJosef Bacik 		return ret;
11620b246afaSJeff Mahoney 	ret = btrfs_run_qgroups(trans, fs_info);
1163c16ce190SJosef Bacik 	if (ret)
1164c16ce190SJosef Bacik 		return ret;
1165546adb0dSJan Schmidt 
11662ff7e61eSJeff Mahoney 	ret = btrfs_setup_space_cache(trans, fs_info);
1167dcdf7f6dSJosef Bacik 	if (ret)
1168dcdf7f6dSJosef Bacik 		return ret;
1169dcdf7f6dSJosef Bacik 
1170546adb0dSJan Schmidt 	/* run_qgroups might have added some more refs */
11712ff7e61eSJeff Mahoney 	ret = btrfs_run_delayed_refs(trans, fs_info, (unsigned long)-1);
1172c16ce190SJosef Bacik 	if (ret)
1173c16ce190SJosef Bacik 		return ret;
1174ea526d18SJosef Bacik again:
11750b86a832SChris Mason 	while (!list_empty(&fs_info->dirty_cowonly_roots)) {
11762ff7e61eSJeff Mahoney 		struct btrfs_root *root;
11770b86a832SChris Mason 		next = fs_info->dirty_cowonly_roots.next;
11780b86a832SChris Mason 		list_del_init(next);
11790b86a832SChris Mason 		root = list_entry(next, struct btrfs_root, dirty_list);
1180e7070be1SJosef Bacik 		clear_bit(BTRFS_ROOT_DIRTY, &root->state);
118187ef2bb4SChris Mason 
11829e351cc8SJosef Bacik 		if (root != fs_info->extent_root)
11839e351cc8SJosef Bacik 			list_add_tail(&root->dirty_list,
11849e351cc8SJosef Bacik 				      &trans->transaction->switch_commits);
118549b25e05SJeff Mahoney 		ret = update_cowonly_root(trans, root);
118649b25e05SJeff Mahoney 		if (ret)
118749b25e05SJeff Mahoney 			return ret;
11882ff7e61eSJeff Mahoney 		ret = btrfs_run_delayed_refs(trans, fs_info, (unsigned long)-1);
1189ea526d18SJosef Bacik 		if (ret)
1190ea526d18SJosef Bacik 			return ret;
119179154b1bSChris Mason 	}
1192276e680dSYan Zheng 
11931bbc621eSChris Mason 	while (!list_empty(dirty_bgs) || !list_empty(io_bgs)) {
11942ff7e61eSJeff Mahoney 		ret = btrfs_write_dirty_block_groups(trans, fs_info);
1195ea526d18SJosef Bacik 		if (ret)
1196ea526d18SJosef Bacik 			return ret;
11972ff7e61eSJeff Mahoney 		ret = btrfs_run_delayed_refs(trans, fs_info, (unsigned long)-1);
1198ea526d18SJosef Bacik 		if (ret)
1199ea526d18SJosef Bacik 			return ret;
1200ea526d18SJosef Bacik 	}
1201ea526d18SJosef Bacik 
1202ea526d18SJosef Bacik 	if (!list_empty(&fs_info->dirty_cowonly_roots))
1203ea526d18SJosef Bacik 		goto again;
1204ea526d18SJosef Bacik 
12059e351cc8SJosef Bacik 	list_add_tail(&fs_info->extent_root->dirty_list,
12069e351cc8SJosef Bacik 		      &trans->transaction->switch_commits);
12078dabb742SStefan Behrens 	btrfs_after_dev_replace_commit(fs_info);
12088dabb742SStefan Behrens 
120979154b1bSChris Mason 	return 0;
121079154b1bSChris Mason }
121179154b1bSChris Mason 
1212d352ac68SChris Mason /*
1213d352ac68SChris Mason  * dead roots are old snapshots that need to be deleted.  This allocates
1214d352ac68SChris Mason  * a dirty root struct and adds it into the list of dead roots that need to
1215d352ac68SChris Mason  * be deleted
1216d352ac68SChris Mason  */
1217cfad392bSJosef Bacik void btrfs_add_dead_root(struct btrfs_root *root)
12185eda7b5eSChris Mason {
12190b246afaSJeff Mahoney 	struct btrfs_fs_info *fs_info = root->fs_info;
12200b246afaSJeff Mahoney 
12210b246afaSJeff Mahoney 	spin_lock(&fs_info->trans_lock);
1222cfad392bSJosef Bacik 	if (list_empty(&root->root_list))
12230b246afaSJeff Mahoney 		list_add_tail(&root->root_list, &fs_info->dead_roots);
12240b246afaSJeff Mahoney 	spin_unlock(&fs_info->trans_lock);
12255eda7b5eSChris Mason }
12265eda7b5eSChris Mason 
1227d352ac68SChris Mason /*
12285d4f98a2SYan Zheng  * update all the cowonly tree roots on disk
1229d352ac68SChris Mason  */
12305d4f98a2SYan Zheng static noinline int commit_fs_roots(struct btrfs_trans_handle *trans,
12315b4aacefSJeff Mahoney 				    struct btrfs_fs_info *fs_info)
12320f7d52f4SChris Mason {
12330f7d52f4SChris Mason 	struct btrfs_root *gang[8];
12340f7d52f4SChris Mason 	int i;
12350f7d52f4SChris Mason 	int ret;
123654aa1f4dSChris Mason 	int err = 0;
123754aa1f4dSChris Mason 
1238a4abeea4SJosef Bacik 	spin_lock(&fs_info->fs_roots_radix_lock);
12390f7d52f4SChris Mason 	while (1) {
12405d4f98a2SYan Zheng 		ret = radix_tree_gang_lookup_tag(&fs_info->fs_roots_radix,
12415d4f98a2SYan Zheng 						 (void **)gang, 0,
12420f7d52f4SChris Mason 						 ARRAY_SIZE(gang),
12430f7d52f4SChris Mason 						 BTRFS_ROOT_TRANS_TAG);
12440f7d52f4SChris Mason 		if (ret == 0)
12450f7d52f4SChris Mason 			break;
12460f7d52f4SChris Mason 		for (i = 0; i < ret; i++) {
12475b4aacefSJeff Mahoney 			struct btrfs_root *root = gang[i];
12485d4f98a2SYan Zheng 			radix_tree_tag_clear(&fs_info->fs_roots_radix,
12492619ba1fSChris Mason 					(unsigned long)root->root_key.objectid,
12500f7d52f4SChris Mason 					BTRFS_ROOT_TRANS_TAG);
1251a4abeea4SJosef Bacik 			spin_unlock(&fs_info->fs_roots_radix_lock);
125231153d81SYan Zheng 
1253e02119d5SChris Mason 			btrfs_free_log(trans, root);
12545d4f98a2SYan Zheng 			btrfs_update_reloc_root(trans, root);
1255d68fc57bSYan, Zheng 			btrfs_orphan_commit_root(trans, root);
1256e02119d5SChris Mason 
125782d5902dSLi Zefan 			btrfs_save_ino_cache(root, trans);
125882d5902dSLi Zefan 
1259f1ebcc74SLiu Bo 			/* see comments in should_cow_block() */
126027cdeb70SMiao Xie 			clear_bit(BTRFS_ROOT_FORCE_COW, &root->state);
1261c7548af6SChris Mason 			smp_mb__after_atomic();
1262f1ebcc74SLiu Bo 
1263978d910dSYan Zheng 			if (root->commit_root != root->node) {
12649e351cc8SJosef Bacik 				list_add_tail(&root->dirty_list,
12659e351cc8SJosef Bacik 					&trans->transaction->switch_commits);
1266978d910dSYan Zheng 				btrfs_set_root_node(&root->root_item,
1267978d910dSYan Zheng 						    root->node);
1268978d910dSYan Zheng 			}
126931153d81SYan Zheng 
12705d4f98a2SYan Zheng 			err = btrfs_update_root(trans, fs_info->tree_root,
12710f7d52f4SChris Mason 						&root->root_key,
12720f7d52f4SChris Mason 						&root->root_item);
1273a4abeea4SJosef Bacik 			spin_lock(&fs_info->fs_roots_radix_lock);
127454aa1f4dSChris Mason 			if (err)
127554aa1f4dSChris Mason 				break;
12767174109cSQu Wenruo 			btrfs_qgroup_free_meta_all(root);
12770f7d52f4SChris Mason 		}
12789f3a7427SChris Mason 	}
1279a4abeea4SJosef Bacik 	spin_unlock(&fs_info->fs_roots_radix_lock);
128054aa1f4dSChris Mason 	return err;
12810f7d52f4SChris Mason }
12820f7d52f4SChris Mason 
1283d352ac68SChris Mason /*
1284de78b51aSEric Sandeen  * defrag a given btree.
1285de78b51aSEric Sandeen  * Every leaf in the btree is read and defragged.
1286d352ac68SChris Mason  */
1287de78b51aSEric Sandeen int btrfs_defrag_root(struct btrfs_root *root)
1288e9d0b13bSChris Mason {
1289e9d0b13bSChris Mason 	struct btrfs_fs_info *info = root->fs_info;
1290e9d0b13bSChris Mason 	struct btrfs_trans_handle *trans;
12918929ecfaSYan, Zheng 	int ret;
1292e9d0b13bSChris Mason 
129327cdeb70SMiao Xie 	if (test_and_set_bit(BTRFS_ROOT_DEFRAG_RUNNING, &root->state))
1294e9d0b13bSChris Mason 		return 0;
12958929ecfaSYan, Zheng 
12966b80053dSChris Mason 	while (1) {
12978929ecfaSYan, Zheng 		trans = btrfs_start_transaction(root, 0);
12988929ecfaSYan, Zheng 		if (IS_ERR(trans))
12998929ecfaSYan, Zheng 			return PTR_ERR(trans);
13008929ecfaSYan, Zheng 
1301de78b51aSEric Sandeen 		ret = btrfs_defrag_leaves(trans, root);
13028929ecfaSYan, Zheng 
1303e9d0b13bSChris Mason 		btrfs_end_transaction(trans, root);
13042ff7e61eSJeff Mahoney 		btrfs_btree_balance_dirty(info);
1305e9d0b13bSChris Mason 		cond_resched();
1306e9d0b13bSChris Mason 
1307ab8d0fc4SJeff Mahoney 		if (btrfs_fs_closing(info) || ret != -EAGAIN)
1308e9d0b13bSChris Mason 			break;
1309210549ebSDavid Sterba 
1310ab8d0fc4SJeff Mahoney 		if (btrfs_defrag_cancelled(info)) {
1311ab8d0fc4SJeff Mahoney 			btrfs_debug(info, "defrag_root cancelled");
1312210549ebSDavid Sterba 			ret = -EAGAIN;
1313210549ebSDavid Sterba 			break;
1314210549ebSDavid Sterba 		}
1315e9d0b13bSChris Mason 	}
131627cdeb70SMiao Xie 	clear_bit(BTRFS_ROOT_DEFRAG_RUNNING, &root->state);
13178929ecfaSYan, Zheng 	return ret;
1318e9d0b13bSChris Mason }
1319e9d0b13bSChris Mason 
1320d352ac68SChris Mason /*
13216426c7adSQu Wenruo  * Do all special snapshot related qgroup dirty hack.
13226426c7adSQu Wenruo  *
13236426c7adSQu Wenruo  * Will do all needed qgroup inherit and dirty hack like switch commit
13246426c7adSQu Wenruo  * roots inside one transaction and write all btree into disk, to make
13256426c7adSQu Wenruo  * qgroup works.
13266426c7adSQu Wenruo  */
13276426c7adSQu Wenruo static int qgroup_account_snapshot(struct btrfs_trans_handle *trans,
13286426c7adSQu Wenruo 				   struct btrfs_root *src,
13296426c7adSQu Wenruo 				   struct btrfs_root *parent,
13306426c7adSQu Wenruo 				   struct btrfs_qgroup_inherit *inherit,
13316426c7adSQu Wenruo 				   u64 dst_objectid)
13326426c7adSQu Wenruo {
13336426c7adSQu Wenruo 	struct btrfs_fs_info *fs_info = src->fs_info;
13346426c7adSQu Wenruo 	int ret;
13356426c7adSQu Wenruo 
13366426c7adSQu Wenruo 	/*
13376426c7adSQu Wenruo 	 * Save some performance in the case that qgroups are not
13386426c7adSQu Wenruo 	 * enabled. If this check races with the ioctl, rescan will
13396426c7adSQu Wenruo 	 * kick in anyway.
13406426c7adSQu Wenruo 	 */
13416426c7adSQu Wenruo 	mutex_lock(&fs_info->qgroup_ioctl_lock);
1342afcdd129SJosef Bacik 	if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags)) {
13436426c7adSQu Wenruo 		mutex_unlock(&fs_info->qgroup_ioctl_lock);
13446426c7adSQu Wenruo 		return 0;
13456426c7adSQu Wenruo 	}
13466426c7adSQu Wenruo 	mutex_unlock(&fs_info->qgroup_ioctl_lock);
13476426c7adSQu Wenruo 
13486426c7adSQu Wenruo 	/*
13496426c7adSQu Wenruo 	 * We are going to commit transaction, see btrfs_commit_transaction()
13506426c7adSQu Wenruo 	 * comment for reason locking tree_log_mutex
13516426c7adSQu Wenruo 	 */
13526426c7adSQu Wenruo 	mutex_lock(&fs_info->tree_log_mutex);
13536426c7adSQu Wenruo 
13545b4aacefSJeff Mahoney 	ret = commit_fs_roots(trans, fs_info);
13556426c7adSQu Wenruo 	if (ret)
13566426c7adSQu Wenruo 		goto out;
13576426c7adSQu Wenruo 	ret = btrfs_qgroup_prepare_account_extents(trans, fs_info);
13586426c7adSQu Wenruo 	if (ret < 0)
13596426c7adSQu Wenruo 		goto out;
13606426c7adSQu Wenruo 	ret = btrfs_qgroup_account_extents(trans, fs_info);
13616426c7adSQu Wenruo 	if (ret < 0)
13626426c7adSQu Wenruo 		goto out;
13636426c7adSQu Wenruo 
13646426c7adSQu Wenruo 	/* Now qgroup are all updated, we can inherit it to new qgroups */
13656426c7adSQu Wenruo 	ret = btrfs_qgroup_inherit(trans, fs_info,
13666426c7adSQu Wenruo 				   src->root_key.objectid, dst_objectid,
13676426c7adSQu Wenruo 				   inherit);
13686426c7adSQu Wenruo 	if (ret < 0)
13696426c7adSQu Wenruo 		goto out;
13706426c7adSQu Wenruo 
13716426c7adSQu Wenruo 	/*
13726426c7adSQu Wenruo 	 * Now we do a simplified commit transaction, which will:
13736426c7adSQu Wenruo 	 * 1) commit all subvolume and extent tree
13746426c7adSQu Wenruo 	 *    To ensure all subvolume and extent tree have a valid
13756426c7adSQu Wenruo 	 *    commit_root to accounting later insert_dir_item()
13766426c7adSQu Wenruo 	 * 2) write all btree blocks onto disk
13776426c7adSQu Wenruo 	 *    This is to make sure later btree modification will be cowed
13786426c7adSQu Wenruo 	 *    Or commit_root can be populated and cause wrong qgroup numbers
13796426c7adSQu Wenruo 	 * In this simplified commit, we don't really care about other trees
13806426c7adSQu Wenruo 	 * like chunk and root tree, as they won't affect qgroup.
13816426c7adSQu Wenruo 	 * And we don't write super to avoid half committed status.
13826426c7adSQu Wenruo 	 */
13832ff7e61eSJeff Mahoney 	ret = commit_cowonly_roots(trans, fs_info);
13846426c7adSQu Wenruo 	if (ret)
13856426c7adSQu Wenruo 		goto out;
13866426c7adSQu Wenruo 	switch_commit_roots(trans->transaction, fs_info);
13876426c7adSQu Wenruo 	ret = btrfs_write_and_wait_transaction(trans, src);
13886426c7adSQu Wenruo 	if (ret)
1389f7af3934SDavid Sterba 		btrfs_handle_fs_error(fs_info, ret,
13906426c7adSQu Wenruo 			"Error while writing out transaction for qgroup");
13916426c7adSQu Wenruo 
13926426c7adSQu Wenruo out:
13936426c7adSQu Wenruo 	mutex_unlock(&fs_info->tree_log_mutex);
13946426c7adSQu Wenruo 
13956426c7adSQu Wenruo 	/*
13966426c7adSQu Wenruo 	 * Force parent root to be updated, as we recorded it before so its
13976426c7adSQu Wenruo 	 * last_trans == cur_transid.
13986426c7adSQu Wenruo 	 * Or it won't be committed again onto disk after later
13996426c7adSQu Wenruo 	 * insert_dir_item()
14006426c7adSQu Wenruo 	 */
14016426c7adSQu Wenruo 	if (!ret)
14026426c7adSQu Wenruo 		record_root_in_trans(trans, parent, 1);
14036426c7adSQu Wenruo 	return ret;
14046426c7adSQu Wenruo }
14056426c7adSQu Wenruo 
14066426c7adSQu Wenruo /*
1407d352ac68SChris Mason  * new snapshots need to be created at a very specific time in the
1408aec8030aSMiao Xie  * transaction commit.  This does the actual creation.
1409aec8030aSMiao Xie  *
1410aec8030aSMiao Xie  * Note:
1411aec8030aSMiao Xie  * If the error which may affect the commitment of the current transaction
1412aec8030aSMiao Xie  * happens, we should return the error number. If the error which just affect
1413aec8030aSMiao Xie  * the creation of the pending snapshots, just return 0.
1414d352ac68SChris Mason  */
141580b6794dSChris Mason static noinline int create_pending_snapshot(struct btrfs_trans_handle *trans,
14163063d29fSChris Mason 				   struct btrfs_fs_info *fs_info,
14173063d29fSChris Mason 				   struct btrfs_pending_snapshot *pending)
14183063d29fSChris Mason {
14193063d29fSChris Mason 	struct btrfs_key key;
142080b6794dSChris Mason 	struct btrfs_root_item *new_root_item;
14213063d29fSChris Mason 	struct btrfs_root *tree_root = fs_info->tree_root;
14223063d29fSChris Mason 	struct btrfs_root *root = pending->root;
14236bdb72deSSage Weil 	struct btrfs_root *parent_root;
142498c9942aSLiu Bo 	struct btrfs_block_rsv *rsv;
14256bdb72deSSage Weil 	struct inode *parent_inode;
142642874b3dSMiao Xie 	struct btrfs_path *path;
142742874b3dSMiao Xie 	struct btrfs_dir_item *dir_item;
1428a22285a6SYan, Zheng 	struct dentry *dentry;
14293063d29fSChris Mason 	struct extent_buffer *tmp;
1430925baeddSChris Mason 	struct extent_buffer *old;
143104b285f3SDeepa Dinamani 	struct timespec cur_time;
1432aec8030aSMiao Xie 	int ret = 0;
1433d68fc57bSYan, Zheng 	u64 to_reserve = 0;
14346bdb72deSSage Weil 	u64 index = 0;
1435a22285a6SYan, Zheng 	u64 objectid;
1436b83cc969SLi Zefan 	u64 root_flags;
14378ea05e3aSAlexander Block 	uuid_le new_uuid;
14383063d29fSChris Mason 
14398546b570SDavid Sterba 	ASSERT(pending->path);
14408546b570SDavid Sterba 	path = pending->path;
144142874b3dSMiao Xie 
1442b0c0ea63SDavid Sterba 	ASSERT(pending->root_item);
1443b0c0ea63SDavid Sterba 	new_root_item = pending->root_item;
1444a22285a6SYan, Zheng 
1445aec8030aSMiao Xie 	pending->error = btrfs_find_free_objectid(tree_root, &objectid);
1446aec8030aSMiao Xie 	if (pending->error)
14476fa9700eSMiao Xie 		goto no_free_objectid;
14483063d29fSChris Mason 
1449d6726335SQu Wenruo 	/*
1450d6726335SQu Wenruo 	 * Make qgroup to skip current new snapshot's qgroupid, as it is
1451d6726335SQu Wenruo 	 * accounted by later btrfs_qgroup_inherit().
1452d6726335SQu Wenruo 	 */
1453d6726335SQu Wenruo 	btrfs_set_skip_qgroup(trans, objectid);
1454d6726335SQu Wenruo 
1455147d256eSZhaolei 	btrfs_reloc_pre_snapshot(pending, &to_reserve);
1456d68fc57bSYan, Zheng 
1457d68fc57bSYan, Zheng 	if (to_reserve > 0) {
1458aec8030aSMiao Xie 		pending->error = btrfs_block_rsv_add(root,
1459aec8030aSMiao Xie 						     &pending->block_rsv,
146008e007d2SMiao Xie 						     to_reserve,
146108e007d2SMiao Xie 						     BTRFS_RESERVE_NO_FLUSH);
1462aec8030aSMiao Xie 		if (pending->error)
1463d6726335SQu Wenruo 			goto clear_skip_qgroup;
1464d68fc57bSYan, Zheng 	}
1465d68fc57bSYan, Zheng 
14663063d29fSChris Mason 	key.objectid = objectid;
1467a22285a6SYan, Zheng 	key.offset = (u64)-1;
1468a22285a6SYan, Zheng 	key.type = BTRFS_ROOT_ITEM_KEY;
14693063d29fSChris Mason 
14706fa9700eSMiao Xie 	rsv = trans->block_rsv;
1471a22285a6SYan, Zheng 	trans->block_rsv = &pending->block_rsv;
14722382c5ccSLiu Bo 	trans->bytes_reserved = trans->block_rsv->reserved;
14730b246afaSJeff Mahoney 	trace_btrfs_space_reservation(fs_info, "transaction",
147488d3a5aaSJosef Bacik 				      trans->transid,
147588d3a5aaSJosef Bacik 				      trans->bytes_reserved, 1);
1476a22285a6SYan, Zheng 	dentry = pending->dentry;
1477e9662f70SMiao Xie 	parent_inode = pending->dir;
1478a22285a6SYan, Zheng 	parent_root = BTRFS_I(parent_inode)->root;
14796426c7adSQu Wenruo 	record_root_in_trans(trans, parent_root, 0);
1480a22285a6SYan, Zheng 
1481c2050a45SDeepa Dinamani 	cur_time = current_time(parent_inode);
148204b285f3SDeepa Dinamani 
14836bdb72deSSage Weil 	/*
14846bdb72deSSage Weil 	 * insert the directory item
14856bdb72deSSage Weil 	 */
14866bdb72deSSage Weil 	ret = btrfs_set_inode_index(parent_inode, &index);
148749b25e05SJeff Mahoney 	BUG_ON(ret); /* -ENOMEM */
148842874b3dSMiao Xie 
148942874b3dSMiao Xie 	/* check if there is a file/dir which has the same name. */
149042874b3dSMiao Xie 	dir_item = btrfs_lookup_dir_item(NULL, parent_root, path,
149142874b3dSMiao Xie 					 btrfs_ino(parent_inode),
149242874b3dSMiao Xie 					 dentry->d_name.name,
149342874b3dSMiao Xie 					 dentry->d_name.len, 0);
149442874b3dSMiao Xie 	if (dir_item != NULL && !IS_ERR(dir_item)) {
1495fe66a05aSChris Mason 		pending->error = -EEXIST;
1496aec8030aSMiao Xie 		goto dir_item_existed;
149742874b3dSMiao Xie 	} else if (IS_ERR(dir_item)) {
149842874b3dSMiao Xie 		ret = PTR_ERR(dir_item);
149966642832SJeff Mahoney 		btrfs_abort_transaction(trans, ret);
15008732d44fSMiao Xie 		goto fail;
150179787eaaSJeff Mahoney 	}
150242874b3dSMiao Xie 	btrfs_release_path(path);
15036bdb72deSSage Weil 
1504e999376fSChris Mason 	/*
1505e999376fSChris Mason 	 * pull in the delayed directory update
1506e999376fSChris Mason 	 * and the delayed inode item
1507e999376fSChris Mason 	 * otherwise we corrupt the FS during
1508e999376fSChris Mason 	 * snapshot
1509e999376fSChris Mason 	 */
15102ff7e61eSJeff Mahoney 	ret = btrfs_run_delayed_items(trans, fs_info);
15118732d44fSMiao Xie 	if (ret) {	/* Transaction aborted */
151266642832SJeff Mahoney 		btrfs_abort_transaction(trans, ret);
15138732d44fSMiao Xie 		goto fail;
15148732d44fSMiao Xie 	}
1515e999376fSChris Mason 
15166426c7adSQu Wenruo 	record_root_in_trans(trans, root, 0);
15176bdb72deSSage Weil 	btrfs_set_root_last_snapshot(&root->root_item, trans->transid);
15186bdb72deSSage Weil 	memcpy(new_root_item, &root->root_item, sizeof(*new_root_item));
151908fe4db1SLi Zefan 	btrfs_check_and_init_root_item(new_root_item);
15206bdb72deSSage Weil 
1521b83cc969SLi Zefan 	root_flags = btrfs_root_flags(new_root_item);
1522b83cc969SLi Zefan 	if (pending->readonly)
1523b83cc969SLi Zefan 		root_flags |= BTRFS_ROOT_SUBVOL_RDONLY;
1524b83cc969SLi Zefan 	else
1525b83cc969SLi Zefan 		root_flags &= ~BTRFS_ROOT_SUBVOL_RDONLY;
1526b83cc969SLi Zefan 	btrfs_set_root_flags(new_root_item, root_flags);
1527b83cc969SLi Zefan 
15288ea05e3aSAlexander Block 	btrfs_set_root_generation_v2(new_root_item,
15298ea05e3aSAlexander Block 			trans->transid);
15308ea05e3aSAlexander Block 	uuid_le_gen(&new_uuid);
15318ea05e3aSAlexander Block 	memcpy(new_root_item->uuid, new_uuid.b, BTRFS_UUID_SIZE);
15328ea05e3aSAlexander Block 	memcpy(new_root_item->parent_uuid, root->root_item.uuid,
15338ea05e3aSAlexander Block 			BTRFS_UUID_SIZE);
153470023da2SStefan Behrens 	if (!(root_flags & BTRFS_ROOT_SUBVOL_RDONLY)) {
153570023da2SStefan Behrens 		memset(new_root_item->received_uuid, 0,
153670023da2SStefan Behrens 		       sizeof(new_root_item->received_uuid));
15378ea05e3aSAlexander Block 		memset(&new_root_item->stime, 0, sizeof(new_root_item->stime));
15388ea05e3aSAlexander Block 		memset(&new_root_item->rtime, 0, sizeof(new_root_item->rtime));
15398ea05e3aSAlexander Block 		btrfs_set_root_stransid(new_root_item, 0);
15408ea05e3aSAlexander Block 		btrfs_set_root_rtransid(new_root_item, 0);
154170023da2SStefan Behrens 	}
15423cae210fSQu Wenruo 	btrfs_set_stack_timespec_sec(&new_root_item->otime, cur_time.tv_sec);
15433cae210fSQu Wenruo 	btrfs_set_stack_timespec_nsec(&new_root_item->otime, cur_time.tv_nsec);
154470023da2SStefan Behrens 	btrfs_set_root_otransid(new_root_item, trans->transid);
15458ea05e3aSAlexander Block 
1546925baeddSChris Mason 	old = btrfs_lock_root_node(root);
154749b25e05SJeff Mahoney 	ret = btrfs_cow_block(trans, root, old, NULL, 0, &old);
154879787eaaSJeff Mahoney 	if (ret) {
154979787eaaSJeff Mahoney 		btrfs_tree_unlock(old);
155079787eaaSJeff Mahoney 		free_extent_buffer(old);
155166642832SJeff Mahoney 		btrfs_abort_transaction(trans, ret);
15528732d44fSMiao Xie 		goto fail;
155379787eaaSJeff Mahoney 	}
155449b25e05SJeff Mahoney 
15555d4f98a2SYan Zheng 	btrfs_set_lock_blocking(old);
15563063d29fSChris Mason 
155749b25e05SJeff Mahoney 	ret = btrfs_copy_root(trans, root, old, &tmp, objectid);
155879787eaaSJeff Mahoney 	/* clean up in any case */
1559925baeddSChris Mason 	btrfs_tree_unlock(old);
1560925baeddSChris Mason 	free_extent_buffer(old);
15618732d44fSMiao Xie 	if (ret) {
156266642832SJeff Mahoney 		btrfs_abort_transaction(trans, ret);
15638732d44fSMiao Xie 		goto fail;
15648732d44fSMiao Xie 	}
1565f1ebcc74SLiu Bo 	/* see comments in should_cow_block() */
156627cdeb70SMiao Xie 	set_bit(BTRFS_ROOT_FORCE_COW, &root->state);
1567f1ebcc74SLiu Bo 	smp_wmb();
1568f1ebcc74SLiu Bo 
15695d4f98a2SYan Zheng 	btrfs_set_root_node(new_root_item, tmp);
1570a22285a6SYan, Zheng 	/* record when the snapshot was created in key.offset */
1571a22285a6SYan, Zheng 	key.offset = trans->transid;
1572a22285a6SYan, Zheng 	ret = btrfs_insert_root(trans, tree_root, &key, new_root_item);
1573925baeddSChris Mason 	btrfs_tree_unlock(tmp);
15743063d29fSChris Mason 	free_extent_buffer(tmp);
15758732d44fSMiao Xie 	if (ret) {
157666642832SJeff Mahoney 		btrfs_abort_transaction(trans, ret);
15778732d44fSMiao Xie 		goto fail;
15788732d44fSMiao Xie 	}
15790660b5afSChris Mason 
1580a22285a6SYan, Zheng 	/*
1581a22285a6SYan, Zheng 	 * insert root back/forward references
1582a22285a6SYan, Zheng 	 */
15836bccf3abSJeff Mahoney 	ret = btrfs_add_root_ref(trans, fs_info, objectid,
1584a22285a6SYan, Zheng 				 parent_root->root_key.objectid,
158533345d01SLi Zefan 				 btrfs_ino(parent_inode), index,
1586a22285a6SYan, Zheng 				 dentry->d_name.name, dentry->d_name.len);
15878732d44fSMiao Xie 	if (ret) {
158866642832SJeff Mahoney 		btrfs_abort_transaction(trans, ret);
15898732d44fSMiao Xie 		goto fail;
15908732d44fSMiao Xie 	}
1591a22285a6SYan, Zheng 
1592a22285a6SYan, Zheng 	key.offset = (u64)-1;
15930b246afaSJeff Mahoney 	pending->snap = btrfs_read_fs_root_no_name(fs_info, &key);
159479787eaaSJeff Mahoney 	if (IS_ERR(pending->snap)) {
159579787eaaSJeff Mahoney 		ret = PTR_ERR(pending->snap);
159666642832SJeff Mahoney 		btrfs_abort_transaction(trans, ret);
15978732d44fSMiao Xie 		goto fail;
159879787eaaSJeff Mahoney 	}
1599d68fc57bSYan, Zheng 
160049b25e05SJeff Mahoney 	ret = btrfs_reloc_post_snapshot(trans, pending);
16018732d44fSMiao Xie 	if (ret) {
160266642832SJeff Mahoney 		btrfs_abort_transaction(trans, ret);
16038732d44fSMiao Xie 		goto fail;
16048732d44fSMiao Xie 	}
1605361048f5SMiao Xie 
16062ff7e61eSJeff Mahoney 	ret = btrfs_run_delayed_refs(trans, fs_info, (unsigned long)-1);
16078732d44fSMiao Xie 	if (ret) {
160866642832SJeff Mahoney 		btrfs_abort_transaction(trans, ret);
16098732d44fSMiao Xie 		goto fail;
16108732d44fSMiao Xie 	}
161142874b3dSMiao Xie 
16126426c7adSQu Wenruo 	/*
16136426c7adSQu Wenruo 	 * Do special qgroup accounting for snapshot, as we do some qgroup
16146426c7adSQu Wenruo 	 * snapshot hack to do fast snapshot.
16156426c7adSQu Wenruo 	 * To co-operate with that hack, we do hack again.
16166426c7adSQu Wenruo 	 * Or snapshot will be greatly slowed down by a subtree qgroup rescan
16176426c7adSQu Wenruo 	 */
16186426c7adSQu Wenruo 	ret = qgroup_account_snapshot(trans, root, parent_root,
16196426c7adSQu Wenruo 				      pending->inherit, objectid);
16206426c7adSQu Wenruo 	if (ret < 0)
16216426c7adSQu Wenruo 		goto fail;
16226426c7adSQu Wenruo 
162342874b3dSMiao Xie 	ret = btrfs_insert_dir_item(trans, parent_root,
162442874b3dSMiao Xie 				    dentry->d_name.name, dentry->d_name.len,
162542874b3dSMiao Xie 				    parent_inode, &key,
162642874b3dSMiao Xie 				    BTRFS_FT_DIR, index);
162742874b3dSMiao Xie 	/* We have check then name at the beginning, so it is impossible. */
16289c52057cSChris Mason 	BUG_ON(ret == -EEXIST || ret == -EOVERFLOW);
16298732d44fSMiao Xie 	if (ret) {
163066642832SJeff Mahoney 		btrfs_abort_transaction(trans, ret);
16318732d44fSMiao Xie 		goto fail;
16328732d44fSMiao Xie 	}
163342874b3dSMiao Xie 
163442874b3dSMiao Xie 	btrfs_i_size_write(parent_inode, parent_inode->i_size +
163542874b3dSMiao Xie 					 dentry->d_name.len * 2);
163604b285f3SDeepa Dinamani 	parent_inode->i_mtime = parent_inode->i_ctime =
1637c2050a45SDeepa Dinamani 		current_time(parent_inode);
1638be6aef60SJosef Bacik 	ret = btrfs_update_inode_fallback(trans, parent_root, parent_inode);
1639dd5f9615SStefan Behrens 	if (ret) {
164066642832SJeff Mahoney 		btrfs_abort_transaction(trans, ret);
1641dd5f9615SStefan Behrens 		goto fail;
1642dd5f9615SStefan Behrens 	}
16436bccf3abSJeff Mahoney 	ret = btrfs_uuid_tree_add(trans, fs_info, new_uuid.b,
1644dd5f9615SStefan Behrens 				  BTRFS_UUID_KEY_SUBVOL, objectid);
1645dd5f9615SStefan Behrens 	if (ret) {
164666642832SJeff Mahoney 		btrfs_abort_transaction(trans, ret);
1647dd5f9615SStefan Behrens 		goto fail;
1648dd5f9615SStefan Behrens 	}
1649dd5f9615SStefan Behrens 	if (!btrfs_is_empty_uuid(new_root_item->received_uuid)) {
16506bccf3abSJeff Mahoney 		ret = btrfs_uuid_tree_add(trans, fs_info,
1651dd5f9615SStefan Behrens 					  new_root_item->received_uuid,
1652dd5f9615SStefan Behrens 					  BTRFS_UUID_KEY_RECEIVED_SUBVOL,
1653dd5f9615SStefan Behrens 					  objectid);
1654dd5f9615SStefan Behrens 		if (ret && ret != -EEXIST) {
165566642832SJeff Mahoney 			btrfs_abort_transaction(trans, ret);
1656dd5f9615SStefan Behrens 			goto fail;
1657dd5f9615SStefan Behrens 		}
1658dd5f9615SStefan Behrens 	}
1659d6726335SQu Wenruo 
16602ff7e61eSJeff Mahoney 	ret = btrfs_run_delayed_refs(trans, fs_info, (unsigned long)-1);
1661d6726335SQu Wenruo 	if (ret) {
166266642832SJeff Mahoney 		btrfs_abort_transaction(trans, ret);
1663d6726335SQu Wenruo 		goto fail;
1664d6726335SQu Wenruo 	}
1665d6726335SQu Wenruo 
16663063d29fSChris Mason fail:
1667aec8030aSMiao Xie 	pending->error = ret;
1668aec8030aSMiao Xie dir_item_existed:
166998c9942aSLiu Bo 	trans->block_rsv = rsv;
16702382c5ccSLiu Bo 	trans->bytes_reserved = 0;
1671d6726335SQu Wenruo clear_skip_qgroup:
1672d6726335SQu Wenruo 	btrfs_clear_skip_qgroup(trans);
16736fa9700eSMiao Xie no_free_objectid:
16746fa9700eSMiao Xie 	kfree(new_root_item);
1675b0c0ea63SDavid Sterba 	pending->root_item = NULL;
167642874b3dSMiao Xie 	btrfs_free_path(path);
16778546b570SDavid Sterba 	pending->path = NULL;
16788546b570SDavid Sterba 
167949b25e05SJeff Mahoney 	return ret;
16803063d29fSChris Mason }
16813063d29fSChris Mason 
1682d352ac68SChris Mason /*
1683d352ac68SChris Mason  * create all the snapshots we've scheduled for creation
1684d352ac68SChris Mason  */
168580b6794dSChris Mason static noinline int create_pending_snapshots(struct btrfs_trans_handle *trans,
16863063d29fSChris Mason 					     struct btrfs_fs_info *fs_info)
16873063d29fSChris Mason {
1688aec8030aSMiao Xie 	struct btrfs_pending_snapshot *pending, *next;
16893063d29fSChris Mason 	struct list_head *head = &trans->transaction->pending_snapshots;
1690aec8030aSMiao Xie 	int ret = 0;
16913de4586cSChris Mason 
1692aec8030aSMiao Xie 	list_for_each_entry_safe(pending, next, head, list) {
1693aec8030aSMiao Xie 		list_del(&pending->list);
1694aec8030aSMiao Xie 		ret = create_pending_snapshot(trans, fs_info, pending);
1695aec8030aSMiao Xie 		if (ret)
1696aec8030aSMiao Xie 			break;
1697aec8030aSMiao Xie 	}
1698aec8030aSMiao Xie 	return ret;
16993de4586cSChris Mason }
17003de4586cSChris Mason 
17012ff7e61eSJeff Mahoney static void update_super_roots(struct btrfs_fs_info *fs_info)
17025d4f98a2SYan Zheng {
17035d4f98a2SYan Zheng 	struct btrfs_root_item *root_item;
17045d4f98a2SYan Zheng 	struct btrfs_super_block *super;
17055d4f98a2SYan Zheng 
17060b246afaSJeff Mahoney 	super = fs_info->super_copy;
17075d4f98a2SYan Zheng 
17080b246afaSJeff Mahoney 	root_item = &fs_info->chunk_root->root_item;
17095d4f98a2SYan Zheng 	super->chunk_root = root_item->bytenr;
17105d4f98a2SYan Zheng 	super->chunk_root_generation = root_item->generation;
17115d4f98a2SYan Zheng 	super->chunk_root_level = root_item->level;
17125d4f98a2SYan Zheng 
17130b246afaSJeff Mahoney 	root_item = &fs_info->tree_root->root_item;
17145d4f98a2SYan Zheng 	super->root = root_item->bytenr;
17155d4f98a2SYan Zheng 	super->generation = root_item->generation;
17165d4f98a2SYan Zheng 	super->root_level = root_item->level;
17170b246afaSJeff Mahoney 	if (btrfs_test_opt(fs_info, SPACE_CACHE))
17180af3d00bSJosef Bacik 		super->cache_generation = root_item->generation;
17190b246afaSJeff Mahoney 	if (test_bit(BTRFS_FS_UPDATE_UUID_TREE_GEN, &fs_info->flags))
172026432799SStefan Behrens 		super->uuid_tree_generation = root_item->generation;
17215d4f98a2SYan Zheng }
17225d4f98a2SYan Zheng 
1723f36f3042SChris Mason int btrfs_transaction_in_commit(struct btrfs_fs_info *info)
1724f36f3042SChris Mason {
17254a9d8bdeSMiao Xie 	struct btrfs_transaction *trans;
1726f36f3042SChris Mason 	int ret = 0;
17274a9d8bdeSMiao Xie 
1728a4abeea4SJosef Bacik 	spin_lock(&info->trans_lock);
17294a9d8bdeSMiao Xie 	trans = info->running_transaction;
17304a9d8bdeSMiao Xie 	if (trans)
17314a9d8bdeSMiao Xie 		ret = (trans->state >= TRANS_STATE_COMMIT_START);
1732a4abeea4SJosef Bacik 	spin_unlock(&info->trans_lock);
1733f36f3042SChris Mason 	return ret;
1734f36f3042SChris Mason }
1735f36f3042SChris Mason 
17368929ecfaSYan, Zheng int btrfs_transaction_blocked(struct btrfs_fs_info *info)
17378929ecfaSYan, Zheng {
17384a9d8bdeSMiao Xie 	struct btrfs_transaction *trans;
17398929ecfaSYan, Zheng 	int ret = 0;
17404a9d8bdeSMiao Xie 
1741a4abeea4SJosef Bacik 	spin_lock(&info->trans_lock);
17424a9d8bdeSMiao Xie 	trans = info->running_transaction;
17434a9d8bdeSMiao Xie 	if (trans)
17444a9d8bdeSMiao Xie 		ret = is_transaction_blocked(trans);
1745a4abeea4SJosef Bacik 	spin_unlock(&info->trans_lock);
17468929ecfaSYan, Zheng 	return ret;
17478929ecfaSYan, Zheng }
17488929ecfaSYan, Zheng 
1749bb9c12c9SSage Weil /*
1750bb9c12c9SSage Weil  * wait for the current transaction commit to start and block subsequent
1751bb9c12c9SSage Weil  * transaction joins
1752bb9c12c9SSage Weil  */
17532ff7e61eSJeff Mahoney static void wait_current_trans_commit_start(struct btrfs_fs_info *fs_info,
1754bb9c12c9SSage Weil 					    struct btrfs_transaction *trans)
1755bb9c12c9SSage Weil {
17562ff7e61eSJeff Mahoney 	wait_event(fs_info->transaction_blocked_wait,
17572ff7e61eSJeff Mahoney 		   trans->state >= TRANS_STATE_COMMIT_START || trans->aborted);
1758bb9c12c9SSage Weil }
1759bb9c12c9SSage Weil 
1760bb9c12c9SSage Weil /*
1761bb9c12c9SSage Weil  * wait for the current transaction to start and then become unblocked.
1762bb9c12c9SSage Weil  * caller holds ref.
1763bb9c12c9SSage Weil  */
17642ff7e61eSJeff Mahoney static void wait_current_trans_commit_start_and_unblock(
17652ff7e61eSJeff Mahoney 					struct btrfs_fs_info *fs_info,
1766bb9c12c9SSage Weil 					struct btrfs_transaction *trans)
1767bb9c12c9SSage Weil {
17682ff7e61eSJeff Mahoney 	wait_event(fs_info->transaction_wait,
17692ff7e61eSJeff Mahoney 		   trans->state >= TRANS_STATE_UNBLOCKED || trans->aborted);
1770bb9c12c9SSage Weil }
1771bb9c12c9SSage Weil 
1772bb9c12c9SSage Weil /*
1773bb9c12c9SSage Weil  * commit transactions asynchronously. once btrfs_commit_transaction_async
1774bb9c12c9SSage Weil  * returns, any subsequent transaction will not be allowed to join.
1775bb9c12c9SSage Weil  */
1776bb9c12c9SSage Weil struct btrfs_async_commit {
1777bb9c12c9SSage Weil 	struct btrfs_trans_handle *newtrans;
1778bb9c12c9SSage Weil 	struct btrfs_root *root;
17797892b5afSMiao Xie 	struct work_struct work;
1780bb9c12c9SSage Weil };
1781bb9c12c9SSage Weil 
1782bb9c12c9SSage Weil static void do_async_commit(struct work_struct *work)
1783bb9c12c9SSage Weil {
1784bb9c12c9SSage Weil 	struct btrfs_async_commit *ac =
17857892b5afSMiao Xie 		container_of(work, struct btrfs_async_commit, work);
1786bb9c12c9SSage Weil 
17876fc4e354SSage Weil 	/*
17886fc4e354SSage Weil 	 * We've got freeze protection passed with the transaction.
17896fc4e354SSage Weil 	 * Tell lockdep about it.
17906fc4e354SSage Weil 	 */
1791b1a06a4bSLiu Bo 	if (ac->newtrans->type & __TRANS_FREEZABLE)
1792bee9182dSOleg Nesterov 		__sb_writers_acquired(ac->root->fs_info->sb, SB_FREEZE_FS);
17936fc4e354SSage Weil 
1794e209db7aSSage Weil 	current->journal_info = ac->newtrans;
1795e209db7aSSage Weil 
1796bb9c12c9SSage Weil 	btrfs_commit_transaction(ac->newtrans, ac->root);
1797bb9c12c9SSage Weil 	kfree(ac);
1798bb9c12c9SSage Weil }
1799bb9c12c9SSage Weil 
1800bb9c12c9SSage Weil int btrfs_commit_transaction_async(struct btrfs_trans_handle *trans,
1801bb9c12c9SSage Weil 				   struct btrfs_root *root,
1802bb9c12c9SSage Weil 				   int wait_for_unblock)
1803bb9c12c9SSage Weil {
18040b246afaSJeff Mahoney 	struct btrfs_fs_info *fs_info = root->fs_info;
1805bb9c12c9SSage Weil 	struct btrfs_async_commit *ac;
1806bb9c12c9SSage Weil 	struct btrfs_transaction *cur_trans;
1807bb9c12c9SSage Weil 
1808bb9c12c9SSage Weil 	ac = kmalloc(sizeof(*ac), GFP_NOFS);
1809db5b493aSTsutomu Itoh 	if (!ac)
1810db5b493aSTsutomu Itoh 		return -ENOMEM;
1811bb9c12c9SSage Weil 
18127892b5afSMiao Xie 	INIT_WORK(&ac->work, do_async_commit);
1813bb9c12c9SSage Weil 	ac->root = root;
18147a7eaa40SJosef Bacik 	ac->newtrans = btrfs_join_transaction(root);
18153612b495STsutomu Itoh 	if (IS_ERR(ac->newtrans)) {
18163612b495STsutomu Itoh 		int err = PTR_ERR(ac->newtrans);
18173612b495STsutomu Itoh 		kfree(ac);
18183612b495STsutomu Itoh 		return err;
18193612b495STsutomu Itoh 	}
1820bb9c12c9SSage Weil 
1821bb9c12c9SSage Weil 	/* take transaction reference */
1822bb9c12c9SSage Weil 	cur_trans = trans->transaction;
182313c5a93eSJosef Bacik 	atomic_inc(&cur_trans->use_count);
1824bb9c12c9SSage Weil 
1825bb9c12c9SSage Weil 	btrfs_end_transaction(trans, root);
18266fc4e354SSage Weil 
18276fc4e354SSage Weil 	/*
18286fc4e354SSage Weil 	 * Tell lockdep we've released the freeze rwsem, since the
18296fc4e354SSage Weil 	 * async commit thread will be the one to unlock it.
18306fc4e354SSage Weil 	 */
1831b1a06a4bSLiu Bo 	if (ac->newtrans->type & __TRANS_FREEZABLE)
18320b246afaSJeff Mahoney 		__sb_writers_release(fs_info->sb, SB_FREEZE_FS);
18336fc4e354SSage Weil 
18347892b5afSMiao Xie 	schedule_work(&ac->work);
1835bb9c12c9SSage Weil 
1836bb9c12c9SSage Weil 	/* wait for transaction to start and unblock */
1837bb9c12c9SSage Weil 	if (wait_for_unblock)
18382ff7e61eSJeff Mahoney 		wait_current_trans_commit_start_and_unblock(fs_info, cur_trans);
1839bb9c12c9SSage Weil 	else
18402ff7e61eSJeff Mahoney 		wait_current_trans_commit_start(fs_info, cur_trans);
1841bb9c12c9SSage Weil 
184238e88054SSage Weil 	if (current->journal_info == trans)
184338e88054SSage Weil 		current->journal_info = NULL;
184438e88054SSage Weil 
1845724e2315SJosef Bacik 	btrfs_put_transaction(cur_trans);
1846bb9c12c9SSage Weil 	return 0;
1847bb9c12c9SSage Weil }
1848bb9c12c9SSage Weil 
184949b25e05SJeff Mahoney 
185049b25e05SJeff Mahoney static void cleanup_transaction(struct btrfs_trans_handle *trans,
18517b8b92afSJosef Bacik 				struct btrfs_root *root, int err)
185249b25e05SJeff Mahoney {
18530b246afaSJeff Mahoney 	struct btrfs_fs_info *fs_info = root->fs_info;
185449b25e05SJeff Mahoney 	struct btrfs_transaction *cur_trans = trans->transaction;
1855f094ac32SLiu Bo 	DEFINE_WAIT(wait);
185649b25e05SJeff Mahoney 
185749b25e05SJeff Mahoney 	WARN_ON(trans->use_count > 1);
185849b25e05SJeff Mahoney 
185966642832SJeff Mahoney 	btrfs_abort_transaction(trans, err);
18607b8b92afSJosef Bacik 
18610b246afaSJeff Mahoney 	spin_lock(&fs_info->trans_lock);
186266b6135bSLiu Bo 
186325d8c284SMiao Xie 	/*
186425d8c284SMiao Xie 	 * If the transaction is removed from the list, it means this
186525d8c284SMiao Xie 	 * transaction has been committed successfully, so it is impossible
186625d8c284SMiao Xie 	 * to call the cleanup function.
186725d8c284SMiao Xie 	 */
186825d8c284SMiao Xie 	BUG_ON(list_empty(&cur_trans->list));
186966b6135bSLiu Bo 
187049b25e05SJeff Mahoney 	list_del_init(&cur_trans->list);
18710b246afaSJeff Mahoney 	if (cur_trans == fs_info->running_transaction) {
18724a9d8bdeSMiao Xie 		cur_trans->state = TRANS_STATE_COMMIT_DOING;
18730b246afaSJeff Mahoney 		spin_unlock(&fs_info->trans_lock);
1874f094ac32SLiu Bo 		wait_event(cur_trans->writer_wait,
1875f094ac32SLiu Bo 			   atomic_read(&cur_trans->num_writers) == 1);
1876f094ac32SLiu Bo 
18770b246afaSJeff Mahoney 		spin_lock(&fs_info->trans_lock);
1878d7096fc3SJosef Bacik 	}
18790b246afaSJeff Mahoney 	spin_unlock(&fs_info->trans_lock);
188049b25e05SJeff Mahoney 
18812ff7e61eSJeff Mahoney 	btrfs_cleanup_one_transaction(trans->transaction, fs_info);
188249b25e05SJeff Mahoney 
18830b246afaSJeff Mahoney 	spin_lock(&fs_info->trans_lock);
18840b246afaSJeff Mahoney 	if (cur_trans == fs_info->running_transaction)
18850b246afaSJeff Mahoney 		fs_info->running_transaction = NULL;
18860b246afaSJeff Mahoney 	spin_unlock(&fs_info->trans_lock);
18874a9d8bdeSMiao Xie 
1888e0228285SJosef Bacik 	if (trans->type & __TRANS_FREEZABLE)
18890b246afaSJeff Mahoney 		sb_end_intwrite(fs_info->sb);
1890724e2315SJosef Bacik 	btrfs_put_transaction(cur_trans);
1891724e2315SJosef Bacik 	btrfs_put_transaction(cur_trans);
189249b25e05SJeff Mahoney 
189349b25e05SJeff Mahoney 	trace_btrfs_transaction_commit(root);
189449b25e05SJeff Mahoney 
189549b25e05SJeff Mahoney 	if (current->journal_info == trans)
189649b25e05SJeff Mahoney 		current->journal_info = NULL;
18970b246afaSJeff Mahoney 	btrfs_scrub_cancel(fs_info);
189849b25e05SJeff Mahoney 
189949b25e05SJeff Mahoney 	kmem_cache_free(btrfs_trans_handle_cachep, trans);
190049b25e05SJeff Mahoney }
190149b25e05SJeff Mahoney 
190282436617SMiao Xie static inline int btrfs_start_delalloc_flush(struct btrfs_fs_info *fs_info)
190382436617SMiao Xie {
19043cdde224SJeff Mahoney 	if (btrfs_test_opt(fs_info, FLUSHONCOMMIT))
19056c255e67SMiao Xie 		return btrfs_start_delalloc_roots(fs_info, 1, -1);
190682436617SMiao Xie 	return 0;
190782436617SMiao Xie }
190882436617SMiao Xie 
190982436617SMiao Xie static inline void btrfs_wait_delalloc_flush(struct btrfs_fs_info *fs_info)
191082436617SMiao Xie {
19113cdde224SJeff Mahoney 	if (btrfs_test_opt(fs_info, FLUSHONCOMMIT))
1912578def7cSFilipe Manana 		btrfs_wait_ordered_roots(fs_info, -1, 0, (u64)-1);
191382436617SMiao Xie }
191482436617SMiao Xie 
191550d9aa99SJosef Bacik static inline void
1916161c3549SJosef Bacik btrfs_wait_pending_ordered(struct btrfs_transaction *cur_trans)
191750d9aa99SJosef Bacik {
1918161c3549SJosef Bacik 	wait_event(cur_trans->pending_wait,
1919161c3549SJosef Bacik 		   atomic_read(&cur_trans->pending_ordered) == 0);
192050d9aa99SJosef Bacik }
192150d9aa99SJosef Bacik 
192279154b1bSChris Mason int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
192379154b1bSChris Mason 			     struct btrfs_root *root)
192479154b1bSChris Mason {
19250b246afaSJeff Mahoney 	struct btrfs_fs_info *fs_info = root->fs_info;
192649b25e05SJeff Mahoney 	struct btrfs_transaction *cur_trans = trans->transaction;
19278fd17795SChris Mason 	struct btrfs_transaction *prev_trans = NULL;
192825287e0aSMiao Xie 	int ret;
192979154b1bSChris Mason 
19308d25a086SMiao Xie 	/* Stop the commit early if ->aborted is set */
19318d25a086SMiao Xie 	if (unlikely(ACCESS_ONCE(cur_trans->aborted))) {
193225287e0aSMiao Xie 		ret = cur_trans->aborted;
1933e4a2bcacSJosef Bacik 		btrfs_end_transaction(trans, root);
1934e4a2bcacSJosef Bacik 		return ret;
193525287e0aSMiao Xie 	}
193649b25e05SJeff Mahoney 
193756bec294SChris Mason 	/* make a pass through all the delayed refs we have so far
193856bec294SChris Mason 	 * any runnings procs may add more while we are here
193956bec294SChris Mason 	 */
19402ff7e61eSJeff Mahoney 	ret = btrfs_run_delayed_refs(trans, fs_info, 0);
1941e4a2bcacSJosef Bacik 	if (ret) {
1942e4a2bcacSJosef Bacik 		btrfs_end_transaction(trans, root);
1943e4a2bcacSJosef Bacik 		return ret;
1944e4a2bcacSJosef Bacik 	}
194556bec294SChris Mason 
19462ff7e61eSJeff Mahoney 	btrfs_trans_release_metadata(trans, fs_info);
19470e721106SJosef Bacik 	trans->block_rsv = NULL;
19480e721106SJosef Bacik 
1949b7ec40d7SChris Mason 	cur_trans = trans->transaction;
195049b25e05SJeff Mahoney 
195156bec294SChris Mason 	/*
195256bec294SChris Mason 	 * set the flushing flag so procs in this transaction have to
195356bec294SChris Mason 	 * start sending their work down.
195456bec294SChris Mason 	 */
1955b7ec40d7SChris Mason 	cur_trans->delayed_refs.flushing = 1;
19561be41b78SJosef Bacik 	smp_wmb();
195756bec294SChris Mason 
1958ea658badSJosef Bacik 	if (!list_empty(&trans->new_bgs))
19592ff7e61eSJeff Mahoney 		btrfs_create_pending_block_groups(trans, fs_info);
1960ea658badSJosef Bacik 
19612ff7e61eSJeff Mahoney 	ret = btrfs_run_delayed_refs(trans, fs_info, 0);
1962e4a2bcacSJosef Bacik 	if (ret) {
1963e4a2bcacSJosef Bacik 		btrfs_end_transaction(trans, root);
1964e4a2bcacSJosef Bacik 		return ret;
1965e4a2bcacSJosef Bacik 	}
196656bec294SChris Mason 
19673204d33cSJosef Bacik 	if (!test_bit(BTRFS_TRANS_DIRTY_BG_RUN, &cur_trans->flags)) {
19681bbc621eSChris Mason 		int run_it = 0;
19691bbc621eSChris Mason 
19701bbc621eSChris Mason 		/* this mutex is also taken before trying to set
19711bbc621eSChris Mason 		 * block groups readonly.  We need to make sure
19721bbc621eSChris Mason 		 * that nobody has set a block group readonly
19731bbc621eSChris Mason 		 * after a extents from that block group have been
19741bbc621eSChris Mason 		 * allocated for cache files.  btrfs_set_block_group_ro
19751bbc621eSChris Mason 		 * will wait for the transaction to commit if it
19763204d33cSJosef Bacik 		 * finds BTRFS_TRANS_DIRTY_BG_RUN set.
19771bbc621eSChris Mason 		 *
19783204d33cSJosef Bacik 		 * The BTRFS_TRANS_DIRTY_BG_RUN flag is also used to make sure
19793204d33cSJosef Bacik 		 * only one process starts all the block group IO.  It wouldn't
19801bbc621eSChris Mason 		 * hurt to have more than one go through, but there's no
19811bbc621eSChris Mason 		 * real advantage to it either.
19821bbc621eSChris Mason 		 */
19830b246afaSJeff Mahoney 		mutex_lock(&fs_info->ro_block_group_mutex);
19843204d33cSJosef Bacik 		if (!test_and_set_bit(BTRFS_TRANS_DIRTY_BG_RUN,
19853204d33cSJosef Bacik 				      &cur_trans->flags))
19861bbc621eSChris Mason 			run_it = 1;
19870b246afaSJeff Mahoney 		mutex_unlock(&fs_info->ro_block_group_mutex);
19881bbc621eSChris Mason 
19891bbc621eSChris Mason 		if (run_it)
19902ff7e61eSJeff Mahoney 			ret = btrfs_start_dirty_block_groups(trans, fs_info);
19911bbc621eSChris Mason 	}
19921bbc621eSChris Mason 	if (ret) {
19931bbc621eSChris Mason 		btrfs_end_transaction(trans, root);
19941bbc621eSChris Mason 		return ret;
19951bbc621eSChris Mason 	}
19961bbc621eSChris Mason 
19970b246afaSJeff Mahoney 	spin_lock(&fs_info->trans_lock);
19984a9d8bdeSMiao Xie 	if (cur_trans->state >= TRANS_STATE_COMMIT_START) {
19990b246afaSJeff Mahoney 		spin_unlock(&fs_info->trans_lock);
200013c5a93eSJosef Bacik 		atomic_inc(&cur_trans->use_count);
200149b25e05SJeff Mahoney 		ret = btrfs_end_transaction(trans, root);
2002ccd467d6SChris Mason 
20032ff7e61eSJeff Mahoney 		wait_for_commit(cur_trans);
200415ee9bc7SJosef Bacik 
2005b4924a0fSLiu Bo 		if (unlikely(cur_trans->aborted))
2006b4924a0fSLiu Bo 			ret = cur_trans->aborted;
2007b4924a0fSLiu Bo 
2008724e2315SJosef Bacik 		btrfs_put_transaction(cur_trans);
200915ee9bc7SJosef Bacik 
201049b25e05SJeff Mahoney 		return ret;
201179154b1bSChris Mason 	}
20124313b399SChris Mason 
20134a9d8bdeSMiao Xie 	cur_trans->state = TRANS_STATE_COMMIT_START;
20140b246afaSJeff Mahoney 	wake_up(&fs_info->transaction_blocked_wait);
2015bb9c12c9SSage Weil 
20160b246afaSJeff Mahoney 	if (cur_trans->list.prev != &fs_info->trans_list) {
2017ccd467d6SChris Mason 		prev_trans = list_entry(cur_trans->list.prev,
2018ccd467d6SChris Mason 					struct btrfs_transaction, list);
20194a9d8bdeSMiao Xie 		if (prev_trans->state != TRANS_STATE_COMPLETED) {
202013c5a93eSJosef Bacik 			atomic_inc(&prev_trans->use_count);
20210b246afaSJeff Mahoney 			spin_unlock(&fs_info->trans_lock);
2022ccd467d6SChris Mason 
20232ff7e61eSJeff Mahoney 			wait_for_commit(prev_trans);
20241f9b8c8fSFilipe Manana 			ret = prev_trans->aborted;
2025ccd467d6SChris Mason 
2026724e2315SJosef Bacik 			btrfs_put_transaction(prev_trans);
20271f9b8c8fSFilipe Manana 			if (ret)
20281f9b8c8fSFilipe Manana 				goto cleanup_transaction;
2029a4abeea4SJosef Bacik 		} else {
20300b246afaSJeff Mahoney 			spin_unlock(&fs_info->trans_lock);
2031ccd467d6SChris Mason 		}
2032a4abeea4SJosef Bacik 	} else {
20330b246afaSJeff Mahoney 		spin_unlock(&fs_info->trans_lock);
2034ccd467d6SChris Mason 	}
203515ee9bc7SJosef Bacik 
20360860adfdSMiao Xie 	extwriter_counter_dec(cur_trans, trans->type);
20370860adfdSMiao Xie 
20380b246afaSJeff Mahoney 	ret = btrfs_start_delalloc_flush(fs_info);
203982436617SMiao Xie 	if (ret)
204082436617SMiao Xie 		goto cleanup_transaction;
204182436617SMiao Xie 
20422ff7e61eSJeff Mahoney 	ret = btrfs_run_delayed_items(trans, fs_info);
204349b25e05SJeff Mahoney 	if (ret)
204449b25e05SJeff Mahoney 		goto cleanup_transaction;
204516cdcec7SMiao Xie 
2046581227d0SMiao Xie 	wait_event(cur_trans->writer_wait,
2047581227d0SMiao Xie 		   extwriter_counter_read(cur_trans) == 0);
2048ed3b3d31SChris Mason 
2049581227d0SMiao Xie 	/* some pending stuffs might be added after the previous flush. */
20502ff7e61eSJeff Mahoney 	ret = btrfs_run_delayed_items(trans, fs_info);
2051ca469637SMiao Xie 	if (ret)
2052ca469637SMiao Xie 		goto cleanup_transaction;
2053ca469637SMiao Xie 
20540b246afaSJeff Mahoney 	btrfs_wait_delalloc_flush(fs_info);
2055cb7ab021SWang Shilong 
2056161c3549SJosef Bacik 	btrfs_wait_pending_ordered(cur_trans);
205750d9aa99SJosef Bacik 
20582ff7e61eSJeff Mahoney 	btrfs_scrub_pause(fs_info);
2059ed0ca140SJosef Bacik 	/*
2060ed0ca140SJosef Bacik 	 * Ok now we need to make sure to block out any other joins while we
2061ed0ca140SJosef Bacik 	 * commit the transaction.  We could have started a join before setting
20624a9d8bdeSMiao Xie 	 * COMMIT_DOING so make sure to wait for num_writers to == 1 again.
2063ed0ca140SJosef Bacik 	 */
20640b246afaSJeff Mahoney 	spin_lock(&fs_info->trans_lock);
20654a9d8bdeSMiao Xie 	cur_trans->state = TRANS_STATE_COMMIT_DOING;
20660b246afaSJeff Mahoney 	spin_unlock(&fs_info->trans_lock);
2067ed0ca140SJosef Bacik 	wait_event(cur_trans->writer_wait,
2068ed0ca140SJosef Bacik 		   atomic_read(&cur_trans->num_writers) == 1);
206915ee9bc7SJosef Bacik 
20702cba30f1SMiao Xie 	/* ->aborted might be set after the previous check, so check it */
20712cba30f1SMiao Xie 	if (unlikely(ACCESS_ONCE(cur_trans->aborted))) {
20722cba30f1SMiao Xie 		ret = cur_trans->aborted;
20736cf7f77eSWang Shilong 		goto scrub_continue;
20742cba30f1SMiao Xie 	}
20757585717fSChris Mason 	/*
20767585717fSChris Mason 	 * the reloc mutex makes sure that we stop
20777585717fSChris Mason 	 * the balancing code from coming in and moving
20787585717fSChris Mason 	 * extents around in the middle of the commit
20797585717fSChris Mason 	 */
20800b246afaSJeff Mahoney 	mutex_lock(&fs_info->reloc_mutex);
20817585717fSChris Mason 
208242874b3dSMiao Xie 	/*
208342874b3dSMiao Xie 	 * We needn't worry about the delayed items because we will
208442874b3dSMiao Xie 	 * deal with them in create_pending_snapshot(), which is the
208542874b3dSMiao Xie 	 * core function of the snapshot creation.
208642874b3dSMiao Xie 	 */
20870b246afaSJeff Mahoney 	ret = create_pending_snapshots(trans, fs_info);
208849b25e05SJeff Mahoney 	if (ret) {
20890b246afaSJeff Mahoney 		mutex_unlock(&fs_info->reloc_mutex);
20906cf7f77eSWang Shilong 		goto scrub_continue;
209149b25e05SJeff Mahoney 	}
20923063d29fSChris Mason 
209342874b3dSMiao Xie 	/*
209442874b3dSMiao Xie 	 * We insert the dir indexes of the snapshots and update the inode
209542874b3dSMiao Xie 	 * of the snapshots' parents after the snapshot creation, so there
209642874b3dSMiao Xie 	 * are some delayed items which are not dealt with. Now deal with
209742874b3dSMiao Xie 	 * them.
209842874b3dSMiao Xie 	 *
209942874b3dSMiao Xie 	 * We needn't worry that this operation will corrupt the snapshots,
210042874b3dSMiao Xie 	 * because all the tree which are snapshoted will be forced to COW
210142874b3dSMiao Xie 	 * the nodes and leaves.
210242874b3dSMiao Xie 	 */
21032ff7e61eSJeff Mahoney 	ret = btrfs_run_delayed_items(trans, fs_info);
210449b25e05SJeff Mahoney 	if (ret) {
21050b246afaSJeff Mahoney 		mutex_unlock(&fs_info->reloc_mutex);
21066cf7f77eSWang Shilong 		goto scrub_continue;
210749b25e05SJeff Mahoney 	}
210816cdcec7SMiao Xie 
21092ff7e61eSJeff Mahoney 	ret = btrfs_run_delayed_refs(trans, fs_info, (unsigned long)-1);
211049b25e05SJeff Mahoney 	if (ret) {
21110b246afaSJeff Mahoney 		mutex_unlock(&fs_info->reloc_mutex);
21126cf7f77eSWang Shilong 		goto scrub_continue;
211349b25e05SJeff Mahoney 	}
211456bec294SChris Mason 
21150ed4792aSQu Wenruo 	/* Reocrd old roots for later qgroup accounting */
21160b246afaSJeff Mahoney 	ret = btrfs_qgroup_prepare_account_extents(trans, fs_info);
21170ed4792aSQu Wenruo 	if (ret) {
21180b246afaSJeff Mahoney 		mutex_unlock(&fs_info->reloc_mutex);
21190ed4792aSQu Wenruo 		goto scrub_continue;
21200ed4792aSQu Wenruo 	}
21210ed4792aSQu Wenruo 
2122e999376fSChris Mason 	/*
2123e999376fSChris Mason 	 * make sure none of the code above managed to slip in a
2124e999376fSChris Mason 	 * delayed item
2125e999376fSChris Mason 	 */
2126ccdf9b30SJeff Mahoney 	btrfs_assert_delayed_root_empty(fs_info);
2127e999376fSChris Mason 
21282c90e5d6SChris Mason 	WARN_ON(cur_trans != trans->transaction);
2129dc17ff8fSChris Mason 
2130e02119d5SChris Mason 	/* btrfs_commit_tree_roots is responsible for getting the
2131e02119d5SChris Mason 	 * various roots consistent with each other.  Every pointer
2132e02119d5SChris Mason 	 * in the tree of tree roots has to point to the most up to date
2133e02119d5SChris Mason 	 * root for every subvolume and other tree.  So, we have to keep
2134e02119d5SChris Mason 	 * the tree logging code from jumping in and changing any
2135e02119d5SChris Mason 	 * of the trees.
2136e02119d5SChris Mason 	 *
2137e02119d5SChris Mason 	 * At this point in the commit, there can't be any tree-log
2138e02119d5SChris Mason 	 * writers, but a little lower down we drop the trans mutex
2139e02119d5SChris Mason 	 * and let new people in.  By holding the tree_log_mutex
2140e02119d5SChris Mason 	 * from now until after the super is written, we avoid races
2141e02119d5SChris Mason 	 * with the tree-log code.
2142e02119d5SChris Mason 	 */
21430b246afaSJeff Mahoney 	mutex_lock(&fs_info->tree_log_mutex);
21441a40e23bSZheng Yan 
21450b246afaSJeff Mahoney 	ret = commit_fs_roots(trans, fs_info);
214649b25e05SJeff Mahoney 	if (ret) {
21470b246afaSJeff Mahoney 		mutex_unlock(&fs_info->tree_log_mutex);
21480b246afaSJeff Mahoney 		mutex_unlock(&fs_info->reloc_mutex);
21496cf7f77eSWang Shilong 		goto scrub_continue;
215049b25e05SJeff Mahoney 	}
215154aa1f4dSChris Mason 
21523818aea2SQu Wenruo 	/*
21537e1876acSDavid Sterba 	 * Since the transaction is done, we can apply the pending changes
21547e1876acSDavid Sterba 	 * before the next transaction.
21553818aea2SQu Wenruo 	 */
21560b246afaSJeff Mahoney 	btrfs_apply_pending_changes(fs_info);
21573818aea2SQu Wenruo 
21585d4f98a2SYan Zheng 	/* commit_fs_roots gets rid of all the tree log roots, it is now
2159e02119d5SChris Mason 	 * safe to free the root of tree log roots
2160e02119d5SChris Mason 	 */
21610b246afaSJeff Mahoney 	btrfs_free_log_root_tree(trans, fs_info);
2162e02119d5SChris Mason 
21630ed4792aSQu Wenruo 	/*
21640ed4792aSQu Wenruo 	 * Since fs roots are all committed, we can get a quite accurate
21650ed4792aSQu Wenruo 	 * new_roots. So let's do quota accounting.
21660ed4792aSQu Wenruo 	 */
21670b246afaSJeff Mahoney 	ret = btrfs_qgroup_account_extents(trans, fs_info);
21680ed4792aSQu Wenruo 	if (ret < 0) {
21690b246afaSJeff Mahoney 		mutex_unlock(&fs_info->tree_log_mutex);
21700b246afaSJeff Mahoney 		mutex_unlock(&fs_info->reloc_mutex);
21710ed4792aSQu Wenruo 		goto scrub_continue;
21720ed4792aSQu Wenruo 	}
21730ed4792aSQu Wenruo 
21742ff7e61eSJeff Mahoney 	ret = commit_cowonly_roots(trans, fs_info);
217549b25e05SJeff Mahoney 	if (ret) {
21760b246afaSJeff Mahoney 		mutex_unlock(&fs_info->tree_log_mutex);
21770b246afaSJeff Mahoney 		mutex_unlock(&fs_info->reloc_mutex);
21786cf7f77eSWang Shilong 		goto scrub_continue;
217949b25e05SJeff Mahoney 	}
218054aa1f4dSChris Mason 
21812cba30f1SMiao Xie 	/*
21822cba30f1SMiao Xie 	 * The tasks which save the space cache and inode cache may also
21832cba30f1SMiao Xie 	 * update ->aborted, check it.
21842cba30f1SMiao Xie 	 */
21852cba30f1SMiao Xie 	if (unlikely(ACCESS_ONCE(cur_trans->aborted))) {
21862cba30f1SMiao Xie 		ret = cur_trans->aborted;
21870b246afaSJeff Mahoney 		mutex_unlock(&fs_info->tree_log_mutex);
21880b246afaSJeff Mahoney 		mutex_unlock(&fs_info->reloc_mutex);
21896cf7f77eSWang Shilong 		goto scrub_continue;
21902cba30f1SMiao Xie 	}
21912cba30f1SMiao Xie 
21922ff7e61eSJeff Mahoney 	btrfs_prepare_extent_commit(trans, fs_info);
219311833d66SYan Zheng 
21940b246afaSJeff Mahoney 	cur_trans = fs_info->running_transaction;
21955f39d397SChris Mason 
21960b246afaSJeff Mahoney 	btrfs_set_root_node(&fs_info->tree_root->root_item,
21970b246afaSJeff Mahoney 			    fs_info->tree_root->node);
21980b246afaSJeff Mahoney 	list_add_tail(&fs_info->tree_root->dirty_list,
21999e351cc8SJosef Bacik 		      &cur_trans->switch_commits);
22005d4f98a2SYan Zheng 
22010b246afaSJeff Mahoney 	btrfs_set_root_node(&fs_info->chunk_root->root_item,
22020b246afaSJeff Mahoney 			    fs_info->chunk_root->node);
22030b246afaSJeff Mahoney 	list_add_tail(&fs_info->chunk_root->dirty_list,
22049e351cc8SJosef Bacik 		      &cur_trans->switch_commits);
22059e351cc8SJosef Bacik 
22060b246afaSJeff Mahoney 	switch_commit_roots(cur_trans, fs_info);
22075d4f98a2SYan Zheng 
2208edf39272SJan Schmidt 	assert_qgroups_uptodate(trans);
2209ce93ec54SJosef Bacik 	ASSERT(list_empty(&cur_trans->dirty_bgs));
22101bbc621eSChris Mason 	ASSERT(list_empty(&cur_trans->io_bgs));
22112ff7e61eSJeff Mahoney 	update_super_roots(fs_info);
2212e02119d5SChris Mason 
22130b246afaSJeff Mahoney 	btrfs_set_super_log_root(fs_info->super_copy, 0);
22140b246afaSJeff Mahoney 	btrfs_set_super_log_root_level(fs_info->super_copy, 0);
22150b246afaSJeff Mahoney 	memcpy(fs_info->super_for_commit, fs_info->super_copy,
22160b246afaSJeff Mahoney 	       sizeof(*fs_info->super_copy));
2217ccd467d6SChris Mason 
22180b246afaSJeff Mahoney 	btrfs_update_commit_device_size(fs_info);
22192ff7e61eSJeff Mahoney 	btrfs_update_commit_device_bytes_used(fs_info, cur_trans);
2220935e5cc9SMiao Xie 
22210b246afaSJeff Mahoney 	clear_bit(BTRFS_FS_LOG1_ERR, &fs_info->flags);
22220b246afaSJeff Mahoney 	clear_bit(BTRFS_FS_LOG2_ERR, &fs_info->flags);
2223656f30dbSFilipe Manana 
22244fbcdf66SFilipe Manana 	btrfs_trans_release_chunk_metadata(trans);
22254fbcdf66SFilipe Manana 
22260b246afaSJeff Mahoney 	spin_lock(&fs_info->trans_lock);
22274a9d8bdeSMiao Xie 	cur_trans->state = TRANS_STATE_UNBLOCKED;
22280b246afaSJeff Mahoney 	fs_info->running_transaction = NULL;
22290b246afaSJeff Mahoney 	spin_unlock(&fs_info->trans_lock);
22300b246afaSJeff Mahoney 	mutex_unlock(&fs_info->reloc_mutex);
2231b7ec40d7SChris Mason 
22320b246afaSJeff Mahoney 	wake_up(&fs_info->transaction_wait);
2233e6dcd2dcSChris Mason 
223479154b1bSChris Mason 	ret = btrfs_write_and_wait_transaction(trans, root);
223549b25e05SJeff Mahoney 	if (ret) {
22360b246afaSJeff Mahoney 		btrfs_handle_fs_error(fs_info, ret,
223708748810SDavid Sterba 				      "Error while writing out transaction");
22380b246afaSJeff Mahoney 		mutex_unlock(&fs_info->tree_log_mutex);
22396cf7f77eSWang Shilong 		goto scrub_continue;
224049b25e05SJeff Mahoney 	}
224149b25e05SJeff Mahoney 
22422ff7e61eSJeff Mahoney 	ret = write_ctree_super(trans, fs_info, 0);
224349b25e05SJeff Mahoney 	if (ret) {
22440b246afaSJeff Mahoney 		mutex_unlock(&fs_info->tree_log_mutex);
22456cf7f77eSWang Shilong 		goto scrub_continue;
224649b25e05SJeff Mahoney 	}
22474313b399SChris Mason 
2248e02119d5SChris Mason 	/*
2249e02119d5SChris Mason 	 * the super is written, we can safely allow the tree-loggers
2250e02119d5SChris Mason 	 * to go about their business
2251e02119d5SChris Mason 	 */
22520b246afaSJeff Mahoney 	mutex_unlock(&fs_info->tree_log_mutex);
2253e02119d5SChris Mason 
22542ff7e61eSJeff Mahoney 	btrfs_finish_extent_commit(trans, fs_info);
22554313b399SChris Mason 
22563204d33cSJosef Bacik 	if (test_bit(BTRFS_TRANS_HAVE_FREE_BGS, &cur_trans->flags))
22570b246afaSJeff Mahoney 		btrfs_clear_space_info_full(fs_info);
225813212b54SZhao Lei 
22590b246afaSJeff Mahoney 	fs_info->last_trans_committed = cur_trans->transid;
22604a9d8bdeSMiao Xie 	/*
22614a9d8bdeSMiao Xie 	 * We needn't acquire the lock here because there is no other task
22624a9d8bdeSMiao Xie 	 * which can change it.
22634a9d8bdeSMiao Xie 	 */
22644a9d8bdeSMiao Xie 	cur_trans->state = TRANS_STATE_COMPLETED;
22652c90e5d6SChris Mason 	wake_up(&cur_trans->commit_wait);
22663de4586cSChris Mason 
22670b246afaSJeff Mahoney 	spin_lock(&fs_info->trans_lock);
226813c5a93eSJosef Bacik 	list_del_init(&cur_trans->list);
22690b246afaSJeff Mahoney 	spin_unlock(&fs_info->trans_lock);
2270a4abeea4SJosef Bacik 
2271724e2315SJosef Bacik 	btrfs_put_transaction(cur_trans);
2272724e2315SJosef Bacik 	btrfs_put_transaction(cur_trans);
227358176a96SJosef Bacik 
22740860adfdSMiao Xie 	if (trans->type & __TRANS_FREEZABLE)
22750b246afaSJeff Mahoney 		sb_end_intwrite(fs_info->sb);
2276b2b5ef5cSJan Kara 
22771abe9b8aSliubo 	trace_btrfs_transaction_commit(root);
22781abe9b8aSliubo 
22792ff7e61eSJeff Mahoney 	btrfs_scrub_continue(fs_info);
2280a2de733cSArne Jansen 
22819ed74f2dSJosef Bacik 	if (current->journal_info == trans)
22829ed74f2dSJosef Bacik 		current->journal_info = NULL;
22839ed74f2dSJosef Bacik 
22842c90e5d6SChris Mason 	kmem_cache_free(btrfs_trans_handle_cachep, trans);
228524bbcf04SYan, Zheng 
22869e7cc91aSWang Xiaoguang 	/*
22879e7cc91aSWang Xiaoguang 	 * If fs has been frozen, we can not handle delayed iputs, otherwise
22889e7cc91aSWang Xiaoguang 	 * it'll result in deadlock about SB_FREEZE_FS.
22899e7cc91aSWang Xiaoguang 	 */
22900b246afaSJeff Mahoney 	if (current != fs_info->transaction_kthread &&
22910b246afaSJeff Mahoney 	    current != fs_info->cleaner_kthread && !fs_info->fs_frozen)
22922ff7e61eSJeff Mahoney 		btrfs_run_delayed_iputs(fs_info);
229324bbcf04SYan, Zheng 
229479154b1bSChris Mason 	return ret;
229549b25e05SJeff Mahoney 
22966cf7f77eSWang Shilong scrub_continue:
22972ff7e61eSJeff Mahoney 	btrfs_scrub_continue(fs_info);
229849b25e05SJeff Mahoney cleanup_transaction:
22992ff7e61eSJeff Mahoney 	btrfs_trans_release_metadata(trans, fs_info);
23004fbcdf66SFilipe Manana 	btrfs_trans_release_chunk_metadata(trans);
23010e721106SJosef Bacik 	trans->block_rsv = NULL;
23020b246afaSJeff Mahoney 	btrfs_warn(fs_info, "Skipping commit of aborted transaction.");
230349b25e05SJeff Mahoney 	if (current->journal_info == trans)
230449b25e05SJeff Mahoney 		current->journal_info = NULL;
23057b8b92afSJosef Bacik 	cleanup_transaction(trans, root, ret);
230649b25e05SJeff Mahoney 
230749b25e05SJeff Mahoney 	return ret;
230879154b1bSChris Mason }
230979154b1bSChris Mason 
2310d352ac68SChris Mason /*
23119d1a2a3aSDavid Sterba  * return < 0 if error
23129d1a2a3aSDavid Sterba  * 0 if there are no more dead_roots at the time of call
23139d1a2a3aSDavid Sterba  * 1 there are more to be processed, call me again
23149d1a2a3aSDavid Sterba  *
23159d1a2a3aSDavid Sterba  * The return value indicates there are certainly more snapshots to delete, but
23169d1a2a3aSDavid Sterba  * if there comes a new one during processing, it may return 0. We don't mind,
23179d1a2a3aSDavid Sterba  * because btrfs_commit_super will poke cleaner thread and it will process it a
23189d1a2a3aSDavid Sterba  * few seconds later.
2319d352ac68SChris Mason  */
23209d1a2a3aSDavid Sterba int btrfs_clean_one_deleted_snapshot(struct btrfs_root *root)
2321e9d0b13bSChris Mason {
23229d1a2a3aSDavid Sterba 	int ret;
23235d4f98a2SYan Zheng 	struct btrfs_fs_info *fs_info = root->fs_info;
2324e9d0b13bSChris Mason 
2325a4abeea4SJosef Bacik 	spin_lock(&fs_info->trans_lock);
23269d1a2a3aSDavid Sterba 	if (list_empty(&fs_info->dead_roots)) {
23279d1a2a3aSDavid Sterba 		spin_unlock(&fs_info->trans_lock);
23289d1a2a3aSDavid Sterba 		return 0;
23299d1a2a3aSDavid Sterba 	}
23309d1a2a3aSDavid Sterba 	root = list_first_entry(&fs_info->dead_roots,
23319d1a2a3aSDavid Sterba 			struct btrfs_root, root_list);
2332cfad392bSJosef Bacik 	list_del_init(&root->root_list);
2333a4abeea4SJosef Bacik 	spin_unlock(&fs_info->trans_lock);
23345d4f98a2SYan Zheng 
2335ab8d0fc4SJeff Mahoney 	btrfs_debug(fs_info, "cleaner removing %llu", root->objectid);
233676dda93cSYan, Zheng 
233716cdcec7SMiao Xie 	btrfs_kill_all_delayed_nodes(root);
233816cdcec7SMiao Xie 
233976dda93cSYan, Zheng 	if (btrfs_header_backref_rev(root->node) <
234076dda93cSYan, Zheng 			BTRFS_MIXED_BACKREF_REV)
23412c536799SJeff Mahoney 		ret = btrfs_drop_snapshot(root, NULL, 0, 0);
234276dda93cSYan, Zheng 	else
23432c536799SJeff Mahoney 		ret = btrfs_drop_snapshot(root, NULL, 1, 0);
234432471dc2SDavid Sterba 
23456596a928SJosef Bacik 	return (ret < 0) ? 0 : 1;
2346e9d0b13bSChris Mason }
2347572d9ab7SDavid Sterba 
2348572d9ab7SDavid Sterba void btrfs_apply_pending_changes(struct btrfs_fs_info *fs_info)
2349572d9ab7SDavid Sterba {
2350572d9ab7SDavid Sterba 	unsigned long prev;
2351572d9ab7SDavid Sterba 	unsigned long bit;
2352572d9ab7SDavid Sterba 
23536c9fe14fSQu Wenruo 	prev = xchg(&fs_info->pending_changes, 0);
2354572d9ab7SDavid Sterba 	if (!prev)
2355572d9ab7SDavid Sterba 		return;
2356572d9ab7SDavid Sterba 
23577e1876acSDavid Sterba 	bit = 1 << BTRFS_PENDING_SET_INODE_MAP_CACHE;
23587e1876acSDavid Sterba 	if (prev & bit)
23597e1876acSDavid Sterba 		btrfs_set_opt(fs_info->mount_opt, INODE_MAP_CACHE);
23607e1876acSDavid Sterba 	prev &= ~bit;
23617e1876acSDavid Sterba 
23627e1876acSDavid Sterba 	bit = 1 << BTRFS_PENDING_CLEAR_INODE_MAP_CACHE;
23637e1876acSDavid Sterba 	if (prev & bit)
23647e1876acSDavid Sterba 		btrfs_clear_opt(fs_info->mount_opt, INODE_MAP_CACHE);
23657e1876acSDavid Sterba 	prev &= ~bit;
23667e1876acSDavid Sterba 
2367d51033d0SDavid Sterba 	bit = 1 << BTRFS_PENDING_COMMIT;
2368d51033d0SDavid Sterba 	if (prev & bit)
2369d51033d0SDavid Sterba 		btrfs_debug(fs_info, "pending commit done");
2370d51033d0SDavid Sterba 	prev &= ~bit;
2371d51033d0SDavid Sterba 
2372572d9ab7SDavid Sterba 	if (prev)
2373572d9ab7SDavid Sterba 		btrfs_warn(fs_info,
2374572d9ab7SDavid Sterba 			"unknown pending changes left 0x%lx, ignoring", prev);
2375572d9ab7SDavid Sterba }
2376