xref: /openbmc/linux/fs/btrfs/transaction.c (revision 00f04b88)
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>
2579154b1bSChris Mason #include "ctree.h"
2679154b1bSChris Mason #include "disk-io.h"
2779154b1bSChris Mason #include "transaction.h"
28925baeddSChris Mason #include "locking.h"
29e02119d5SChris Mason #include "tree-log.h"
30581bb050SLi Zefan #include "inode-map.h"
3179154b1bSChris Mason 
320f7d52f4SChris Mason #define BTRFS_ROOT_TRANS_TAG 0
330f7d52f4SChris Mason 
3480b6794dSChris Mason static noinline void put_transaction(struct btrfs_transaction *transaction)
3579154b1bSChris Mason {
3613c5a93eSJosef Bacik 	WARN_ON(atomic_read(&transaction->use_count) == 0);
3713c5a93eSJosef Bacik 	if (atomic_dec_and_test(&transaction->use_count)) {
38a4abeea4SJosef Bacik 		BUG_ON(!list_empty(&transaction->list));
3900f04b88SArne Jansen 		WARN_ON(transaction->delayed_refs.root.rb_node);
4000f04b88SArne Jansen 		WARN_ON(!list_empty(&transaction->delayed_refs.seq_head));
412c90e5d6SChris Mason 		memset(transaction, 0, sizeof(*transaction));
422c90e5d6SChris Mason 		kmem_cache_free(btrfs_transaction_cachep, transaction);
4379154b1bSChris Mason 	}
4478fae27eSChris Mason }
4579154b1bSChris Mason 
46817d52f8SJosef Bacik static noinline void switch_commit_root(struct btrfs_root *root)
47817d52f8SJosef Bacik {
48817d52f8SJosef Bacik 	free_extent_buffer(root->commit_root);
49817d52f8SJosef Bacik 	root->commit_root = btrfs_root_node(root);
50817d52f8SJosef Bacik }
51817d52f8SJosef Bacik 
52d352ac68SChris Mason /*
53d352ac68SChris Mason  * either allocate a new transaction or hop into the existing one
54d352ac68SChris Mason  */
55a4abeea4SJosef Bacik static noinline int join_transaction(struct btrfs_root *root, int nofail)
5679154b1bSChris Mason {
5779154b1bSChris Mason 	struct btrfs_transaction *cur_trans;
58a4abeea4SJosef Bacik 
59a4abeea4SJosef Bacik 	spin_lock(&root->fs_info->trans_lock);
60d43317dcSChris Mason loop:
61a4abeea4SJosef Bacik 	if (root->fs_info->trans_no_join) {
62a4abeea4SJosef Bacik 		if (!nofail) {
63a4abeea4SJosef Bacik 			spin_unlock(&root->fs_info->trans_lock);
64a4abeea4SJosef Bacik 			return -EBUSY;
65a4abeea4SJosef Bacik 		}
66a4abeea4SJosef Bacik 	}
67a4abeea4SJosef Bacik 
6879154b1bSChris Mason 	cur_trans = root->fs_info->running_transaction;
69a4abeea4SJosef Bacik 	if (cur_trans) {
70a4abeea4SJosef Bacik 		atomic_inc(&cur_trans->use_count);
71a4abeea4SJosef Bacik 		atomic_inc(&cur_trans->num_writers);
72a4abeea4SJosef Bacik 		cur_trans->num_joined++;
73a4abeea4SJosef Bacik 		spin_unlock(&root->fs_info->trans_lock);
74a4abeea4SJosef Bacik 		return 0;
75a4abeea4SJosef Bacik 	}
76a4abeea4SJosef Bacik 	spin_unlock(&root->fs_info->trans_lock);
77a4abeea4SJosef Bacik 
78a4abeea4SJosef Bacik 	cur_trans = kmem_cache_alloc(btrfs_transaction_cachep, GFP_NOFS);
79db5b493aSTsutomu Itoh 	if (!cur_trans)
80db5b493aSTsutomu Itoh 		return -ENOMEM;
81d43317dcSChris Mason 
82a4abeea4SJosef Bacik 	spin_lock(&root->fs_info->trans_lock);
83a4abeea4SJosef Bacik 	if (root->fs_info->running_transaction) {
84d43317dcSChris Mason 		/*
85d43317dcSChris Mason 		 * someone started a transaction after we unlocked.  Make sure
86d43317dcSChris Mason 		 * to redo the trans_no_join checks above
87d43317dcSChris Mason 		 */
88a4abeea4SJosef Bacik 		kmem_cache_free(btrfs_transaction_cachep, cur_trans);
89a4abeea4SJosef Bacik 		cur_trans = root->fs_info->running_transaction;
90d43317dcSChris Mason 		goto loop;
91a4abeea4SJosef Bacik 	}
92d43317dcSChris Mason 
9313c5a93eSJosef Bacik 	atomic_set(&cur_trans->num_writers, 1);
9415ee9bc7SJosef Bacik 	cur_trans->num_joined = 0;
9579154b1bSChris Mason 	init_waitqueue_head(&cur_trans->writer_wait);
9679154b1bSChris Mason 	init_waitqueue_head(&cur_trans->commit_wait);
9779154b1bSChris Mason 	cur_trans->in_commit = 0;
98f9295749SChris Mason 	cur_trans->blocked = 0;
99a4abeea4SJosef Bacik 	/*
100a4abeea4SJosef Bacik 	 * One for this trans handle, one so it will live on until we
101a4abeea4SJosef Bacik 	 * commit the transaction.
102a4abeea4SJosef Bacik 	 */
103a4abeea4SJosef Bacik 	atomic_set(&cur_trans->use_count, 2);
10479154b1bSChris Mason 	cur_trans->commit_done = 0;
10508607c1bSChris Mason 	cur_trans->start_time = get_seconds();
10656bec294SChris Mason 
1076bef4d31SEric Paris 	cur_trans->delayed_refs.root = RB_ROOT;
10856bec294SChris Mason 	cur_trans->delayed_refs.num_entries = 0;
109c3e69d58SChris Mason 	cur_trans->delayed_refs.num_heads_ready = 0;
110c3e69d58SChris Mason 	cur_trans->delayed_refs.num_heads = 0;
11156bec294SChris Mason 	cur_trans->delayed_refs.flushing = 0;
112c3e69d58SChris Mason 	cur_trans->delayed_refs.run_delayed_start = 0;
11300f04b88SArne Jansen 	cur_trans->delayed_refs.seq = 1;
114a4abeea4SJosef Bacik 	spin_lock_init(&cur_trans->commit_lock);
11556bec294SChris Mason 	spin_lock_init(&cur_trans->delayed_refs.lock);
11600f04b88SArne Jansen 	INIT_LIST_HEAD(&cur_trans->delayed_refs.seq_head);
11756bec294SChris Mason 
1183063d29fSChris Mason 	INIT_LIST_HEAD(&cur_trans->pending_snapshots);
1198fd17795SChris Mason 	list_add_tail(&cur_trans->list, &root->fs_info->trans_list);
120d1310b2eSChris Mason 	extent_io_tree_init(&cur_trans->dirty_pages,
121f993c883SDavid Sterba 			     root->fs_info->btree_inode->i_mapping);
122a4abeea4SJosef Bacik 	root->fs_info->generation++;
123a4abeea4SJosef Bacik 	cur_trans->transid = root->fs_info->generation;
12448ec2cf8SChris Mason 	root->fs_info->running_transaction = cur_trans;
125a4abeea4SJosef Bacik 	spin_unlock(&root->fs_info->trans_lock);
12615ee9bc7SJosef Bacik 
12779154b1bSChris Mason 	return 0;
12879154b1bSChris Mason }
12979154b1bSChris Mason 
130d352ac68SChris Mason /*
131d397712bSChris Mason  * this does all the record keeping required to make sure that a reference
132d397712bSChris Mason  * counted root is properly recorded in a given transaction.  This is required
133d397712bSChris Mason  * to make sure the old root from before we joined the transaction is deleted
134d397712bSChris Mason  * when the transaction commits
135d352ac68SChris Mason  */
1367585717fSChris Mason static int record_root_in_trans(struct btrfs_trans_handle *trans,
1375d4f98a2SYan Zheng 			       struct btrfs_root *root)
1386702ed49SChris Mason {
1395d4f98a2SYan Zheng 	if (root->ref_cows && root->last_trans < trans->transid) {
1406702ed49SChris Mason 		WARN_ON(root == root->fs_info->extent_root);
1415d4f98a2SYan Zheng 		WARN_ON(root->commit_root != root->node);
1425d4f98a2SYan Zheng 
1437585717fSChris Mason 		/*
1447585717fSChris Mason 		 * see below for in_trans_setup usage rules
1457585717fSChris Mason 		 * we have the reloc mutex held now, so there
1467585717fSChris Mason 		 * is only one writer in this function
1477585717fSChris Mason 		 */
1487585717fSChris Mason 		root->in_trans_setup = 1;
1497585717fSChris Mason 
1507585717fSChris Mason 		/* make sure readers find in_trans_setup before
1517585717fSChris Mason 		 * they find our root->last_trans update
1527585717fSChris Mason 		 */
1537585717fSChris Mason 		smp_wmb();
1547585717fSChris Mason 
155a4abeea4SJosef Bacik 		spin_lock(&root->fs_info->fs_roots_radix_lock);
156a4abeea4SJosef Bacik 		if (root->last_trans == trans->transid) {
157a4abeea4SJosef Bacik 			spin_unlock(&root->fs_info->fs_roots_radix_lock);
158a4abeea4SJosef Bacik 			return 0;
159a4abeea4SJosef Bacik 		}
1606702ed49SChris Mason 		radix_tree_tag_set(&root->fs_info->fs_roots_radix,
1616702ed49SChris Mason 			   (unsigned long)root->root_key.objectid,
1626702ed49SChris Mason 			   BTRFS_ROOT_TRANS_TAG);
163a4abeea4SJosef Bacik 		spin_unlock(&root->fs_info->fs_roots_radix_lock);
1647585717fSChris Mason 		root->last_trans = trans->transid;
1657585717fSChris Mason 
1667585717fSChris Mason 		/* this is pretty tricky.  We don't want to
1677585717fSChris Mason 		 * take the relocation lock in btrfs_record_root_in_trans
1687585717fSChris Mason 		 * unless we're really doing the first setup for this root in
1697585717fSChris Mason 		 * this transaction.
1707585717fSChris Mason 		 *
1717585717fSChris Mason 		 * Normally we'd use root->last_trans as a flag to decide
1727585717fSChris Mason 		 * if we want to take the expensive mutex.
1737585717fSChris Mason 		 *
1747585717fSChris Mason 		 * But, we have to set root->last_trans before we
1757585717fSChris Mason 		 * init the relocation root, otherwise, we trip over warnings
1767585717fSChris Mason 		 * in ctree.c.  The solution used here is to flag ourselves
1777585717fSChris Mason 		 * with root->in_trans_setup.  When this is 1, we're still
1787585717fSChris Mason 		 * fixing up the reloc trees and everyone must wait.
1797585717fSChris Mason 		 *
1807585717fSChris Mason 		 * When this is zero, they can trust root->last_trans and fly
1817585717fSChris Mason 		 * through btrfs_record_root_in_trans without having to take the
1827585717fSChris Mason 		 * lock.  smp_wmb() makes sure that all the writes above are
1837585717fSChris Mason 		 * done before we pop in the zero below
1847585717fSChris Mason 		 */
1855d4f98a2SYan Zheng 		btrfs_init_reloc_root(trans, root);
1867585717fSChris Mason 		smp_wmb();
1877585717fSChris Mason 		root->in_trans_setup = 0;
1886702ed49SChris Mason 	}
1895d4f98a2SYan Zheng 	return 0;
1906702ed49SChris Mason }
1915d4f98a2SYan Zheng 
1927585717fSChris Mason 
1937585717fSChris Mason int btrfs_record_root_in_trans(struct btrfs_trans_handle *trans,
1947585717fSChris Mason 			       struct btrfs_root *root)
1957585717fSChris Mason {
1967585717fSChris Mason 	if (!root->ref_cows)
1977585717fSChris Mason 		return 0;
1987585717fSChris Mason 
1997585717fSChris Mason 	/*
2007585717fSChris Mason 	 * see record_root_in_trans for comments about in_trans_setup usage
2017585717fSChris Mason 	 * and barriers
2027585717fSChris Mason 	 */
2037585717fSChris Mason 	smp_rmb();
2047585717fSChris Mason 	if (root->last_trans == trans->transid &&
2057585717fSChris Mason 	    !root->in_trans_setup)
2067585717fSChris Mason 		return 0;
2077585717fSChris Mason 
2087585717fSChris Mason 	mutex_lock(&root->fs_info->reloc_mutex);
2097585717fSChris Mason 	record_root_in_trans(trans, root);
2107585717fSChris Mason 	mutex_unlock(&root->fs_info->reloc_mutex);
2117585717fSChris Mason 
2127585717fSChris Mason 	return 0;
2137585717fSChris Mason }
2147585717fSChris Mason 
215d352ac68SChris Mason /* wait for commit against the current transaction to become unblocked
216d352ac68SChris Mason  * when this is done, it is safe to start a new transaction, but the current
217d352ac68SChris Mason  * transaction might not be fully on disk.
218d352ac68SChris Mason  */
21937d1aeeeSChris Mason static void wait_current_trans(struct btrfs_root *root)
22079154b1bSChris Mason {
221f9295749SChris Mason 	struct btrfs_transaction *cur_trans;
22279154b1bSChris Mason 
223a4abeea4SJosef Bacik 	spin_lock(&root->fs_info->trans_lock);
224f9295749SChris Mason 	cur_trans = root->fs_info->running_transaction;
22537d1aeeeSChris Mason 	if (cur_trans && cur_trans->blocked) {
22613c5a93eSJosef Bacik 		atomic_inc(&cur_trans->use_count);
227a4abeea4SJosef Bacik 		spin_unlock(&root->fs_info->trans_lock);
22872d63ed6SLi Zefan 
22972d63ed6SLi Zefan 		wait_event(root->fs_info->transaction_wait,
23072d63ed6SLi Zefan 			   !cur_trans->blocked);
231f9295749SChris Mason 		put_transaction(cur_trans);
232a4abeea4SJosef Bacik 	} else {
233a4abeea4SJosef Bacik 		spin_unlock(&root->fs_info->trans_lock);
234f9295749SChris Mason 	}
23537d1aeeeSChris Mason }
23637d1aeeeSChris Mason 
237249ac1e5SJosef Bacik enum btrfs_trans_type {
238249ac1e5SJosef Bacik 	TRANS_START,
239249ac1e5SJosef Bacik 	TRANS_JOIN,
240249ac1e5SJosef Bacik 	TRANS_USERSPACE,
2410af3d00bSJosef Bacik 	TRANS_JOIN_NOLOCK,
242249ac1e5SJosef Bacik };
243249ac1e5SJosef Bacik 
244a22285a6SYan, Zheng static int may_wait_transaction(struct btrfs_root *root, int type)
24537d1aeeeSChris Mason {
246a4abeea4SJosef Bacik 	if (root->fs_info->log_root_recovering)
247a4abeea4SJosef Bacik 		return 0;
248a4abeea4SJosef Bacik 
249a4abeea4SJosef Bacik 	if (type == TRANS_USERSPACE)
250a22285a6SYan, Zheng 		return 1;
251a4abeea4SJosef Bacik 
252a4abeea4SJosef Bacik 	if (type == TRANS_START &&
253a4abeea4SJosef Bacik 	    !atomic_read(&root->fs_info->open_ioctl_trans))
254a4abeea4SJosef Bacik 		return 1;
255a4abeea4SJosef Bacik 
256a22285a6SYan, Zheng 	return 0;
257a22285a6SYan, Zheng }
258a22285a6SYan, Zheng 
259a22285a6SYan, Zheng static struct btrfs_trans_handle *start_transaction(struct btrfs_root *root,
260a22285a6SYan, Zheng 						    u64 num_items, int type)
261a22285a6SYan, Zheng {
262a22285a6SYan, Zheng 	struct btrfs_trans_handle *h;
263a22285a6SYan, Zheng 	struct btrfs_transaction *cur_trans;
264b5009945SJosef Bacik 	u64 num_bytes = 0;
265a22285a6SYan, Zheng 	int ret;
266acce952bSliubo 
267acce952bSliubo 	if (root->fs_info->fs_state & BTRFS_SUPER_FLAG_ERROR)
268acce952bSliubo 		return ERR_PTR(-EROFS);
2692a1eb461SJosef Bacik 
2702a1eb461SJosef Bacik 	if (current->journal_info) {
2712a1eb461SJosef Bacik 		WARN_ON(type != TRANS_JOIN && type != TRANS_JOIN_NOLOCK);
2722a1eb461SJosef Bacik 		h = current->journal_info;
2732a1eb461SJosef Bacik 		h->use_count++;
2742a1eb461SJosef Bacik 		h->orig_rsv = h->block_rsv;
2752a1eb461SJosef Bacik 		h->block_rsv = NULL;
2762a1eb461SJosef Bacik 		goto got_it;
2772a1eb461SJosef Bacik 	}
278b5009945SJosef Bacik 
279b5009945SJosef Bacik 	/*
280b5009945SJosef Bacik 	 * Do the reservation before we join the transaction so we can do all
281b5009945SJosef Bacik 	 * the appropriate flushing if need be.
282b5009945SJosef Bacik 	 */
283b5009945SJosef Bacik 	if (num_items > 0 && root != root->fs_info->chunk_root) {
284b5009945SJosef Bacik 		num_bytes = btrfs_calc_trans_metadata_size(root, num_items);
2854a92b1b8SJosef Bacik 		ret = btrfs_block_rsv_add(root,
286b5009945SJosef Bacik 					  &root->fs_info->trans_block_rsv,
287b5009945SJosef Bacik 					  num_bytes);
288b5009945SJosef Bacik 		if (ret)
289b5009945SJosef Bacik 			return ERR_PTR(ret);
290b5009945SJosef Bacik 	}
291a22285a6SYan, Zheng again:
292a22285a6SYan, Zheng 	h = kmem_cache_alloc(btrfs_trans_handle_cachep, GFP_NOFS);
293a22285a6SYan, Zheng 	if (!h)
294a22285a6SYan, Zheng 		return ERR_PTR(-ENOMEM);
295a22285a6SYan, Zheng 
296a22285a6SYan, Zheng 	if (may_wait_transaction(root, type))
29737d1aeeeSChris Mason 		wait_current_trans(root);
298a22285a6SYan, Zheng 
299a4abeea4SJosef Bacik 	do {
300a4abeea4SJosef Bacik 		ret = join_transaction(root, type == TRANS_JOIN_NOLOCK);
301a4abeea4SJosef Bacik 		if (ret == -EBUSY)
302a4abeea4SJosef Bacik 			wait_current_trans(root);
303a4abeea4SJosef Bacik 	} while (ret == -EBUSY);
304a4abeea4SJosef Bacik 
305db5b493aSTsutomu Itoh 	if (ret < 0) {
3066e8df2aeSYoshinori Sano 		kmem_cache_free(btrfs_trans_handle_cachep, h);
307db5b493aSTsutomu Itoh 		return ERR_PTR(ret);
308db5b493aSTsutomu Itoh 	}
3090f7d52f4SChris Mason 
310a22285a6SYan, Zheng 	cur_trans = root->fs_info->running_transaction;
311a22285a6SYan, Zheng 
312a22285a6SYan, Zheng 	h->transid = cur_trans->transid;
313a22285a6SYan, Zheng 	h->transaction = cur_trans;
31479154b1bSChris Mason 	h->blocks_used = 0;
315a22285a6SYan, Zheng 	h->bytes_reserved = 0;
31656bec294SChris Mason 	h->delayed_ref_updates = 0;
3172a1eb461SJosef Bacik 	h->use_count = 1;
318f0486c68SYan, Zheng 	h->block_rsv = NULL;
3192a1eb461SJosef Bacik 	h->orig_rsv = NULL;
320b7ec40d7SChris Mason 
321a22285a6SYan, Zheng 	smp_mb();
322a22285a6SYan, Zheng 	if (cur_trans->blocked && may_wait_transaction(root, type)) {
323a22285a6SYan, Zheng 		btrfs_commit_transaction(h, root);
324a22285a6SYan, Zheng 		goto again;
325a22285a6SYan, Zheng 	}
3269ed74f2dSJosef Bacik 
327b5009945SJosef Bacik 	if (num_bytes) {
328b5009945SJosef Bacik 		h->block_rsv = &root->fs_info->trans_block_rsv;
329b5009945SJosef Bacik 		h->bytes_reserved = num_bytes;
330a22285a6SYan, Zheng 	}
331a22285a6SYan, Zheng 
3322a1eb461SJosef Bacik got_it:
333a4abeea4SJosef Bacik 	btrfs_record_root_in_trans(h, root);
334a22285a6SYan, Zheng 
335a22285a6SYan, Zheng 	if (!current->journal_info && type != TRANS_USERSPACE)
336a22285a6SYan, Zheng 		current->journal_info = h;
33779154b1bSChris Mason 	return h;
33879154b1bSChris Mason }
33979154b1bSChris Mason 
340f9295749SChris Mason struct btrfs_trans_handle *btrfs_start_transaction(struct btrfs_root *root,
341a22285a6SYan, Zheng 						   int num_items)
342f9295749SChris Mason {
343a22285a6SYan, Zheng 	return start_transaction(root, num_items, TRANS_START);
344f9295749SChris Mason }
3457a7eaa40SJosef Bacik struct btrfs_trans_handle *btrfs_join_transaction(struct btrfs_root *root)
346f9295749SChris Mason {
347a22285a6SYan, Zheng 	return start_transaction(root, 0, TRANS_JOIN);
348f9295749SChris Mason }
349f9295749SChris Mason 
3507a7eaa40SJosef Bacik struct btrfs_trans_handle *btrfs_join_transaction_nolock(struct btrfs_root *root)
3510af3d00bSJosef Bacik {
3520af3d00bSJosef Bacik 	return start_transaction(root, 0, TRANS_JOIN_NOLOCK);
3530af3d00bSJosef Bacik }
3540af3d00bSJosef Bacik 
3557a7eaa40SJosef Bacik struct btrfs_trans_handle *btrfs_start_ioctl_transaction(struct btrfs_root *root)
3569ca9ee09SSage Weil {
3577a7eaa40SJosef Bacik 	return start_transaction(root, 0, TRANS_USERSPACE);
3589ca9ee09SSage Weil }
3599ca9ee09SSage Weil 
360d352ac68SChris Mason /* wait for a transaction commit to be fully complete */
361b9c8300cSLi Zefan static noinline void wait_for_commit(struct btrfs_root *root,
36289ce8a63SChris Mason 				    struct btrfs_transaction *commit)
36389ce8a63SChris Mason {
36472d63ed6SLi Zefan 	wait_event(commit->commit_wait, commit->commit_done);
36589ce8a63SChris Mason }
36689ce8a63SChris Mason 
36746204592SSage Weil int btrfs_wait_for_commit(struct btrfs_root *root, u64 transid)
36846204592SSage Weil {
36946204592SSage Weil 	struct btrfs_transaction *cur_trans = NULL, *t;
37046204592SSage Weil 	int ret;
37146204592SSage Weil 
37246204592SSage Weil 	ret = 0;
37346204592SSage Weil 	if (transid) {
37446204592SSage Weil 		if (transid <= root->fs_info->last_trans_committed)
375a4abeea4SJosef Bacik 			goto out;
37646204592SSage Weil 
37746204592SSage Weil 		/* find specified transaction */
378a4abeea4SJosef Bacik 		spin_lock(&root->fs_info->trans_lock);
37946204592SSage Weil 		list_for_each_entry(t, &root->fs_info->trans_list, list) {
38046204592SSage Weil 			if (t->transid == transid) {
38146204592SSage Weil 				cur_trans = t;
382a4abeea4SJosef Bacik 				atomic_inc(&cur_trans->use_count);
38346204592SSage Weil 				break;
38446204592SSage Weil 			}
38546204592SSage Weil 			if (t->transid > transid)
38646204592SSage Weil 				break;
38746204592SSage Weil 		}
388a4abeea4SJosef Bacik 		spin_unlock(&root->fs_info->trans_lock);
38946204592SSage Weil 		ret = -EINVAL;
39046204592SSage Weil 		if (!cur_trans)
391a4abeea4SJosef Bacik 			goto out;  /* bad transid */
39246204592SSage Weil 	} else {
39346204592SSage Weil 		/* find newest transaction that is committing | committed */
394a4abeea4SJosef Bacik 		spin_lock(&root->fs_info->trans_lock);
39546204592SSage Weil 		list_for_each_entry_reverse(t, &root->fs_info->trans_list,
39646204592SSage Weil 					    list) {
39746204592SSage Weil 			if (t->in_commit) {
39846204592SSage Weil 				if (t->commit_done)
3993473f3c0SJosef Bacik 					break;
40046204592SSage Weil 				cur_trans = t;
401a4abeea4SJosef Bacik 				atomic_inc(&cur_trans->use_count);
40246204592SSage Weil 				break;
40346204592SSage Weil 			}
40446204592SSage Weil 		}
405a4abeea4SJosef Bacik 		spin_unlock(&root->fs_info->trans_lock);
40646204592SSage Weil 		if (!cur_trans)
407a4abeea4SJosef Bacik 			goto out;  /* nothing committing|committed */
40846204592SSage Weil 	}
40946204592SSage Weil 
41046204592SSage Weil 	wait_for_commit(root, cur_trans);
41146204592SSage Weil 
41246204592SSage Weil 	put_transaction(cur_trans);
41346204592SSage Weil 	ret = 0;
414a4abeea4SJosef Bacik out:
41546204592SSage Weil 	return ret;
41646204592SSage Weil }
41746204592SSage Weil 
41837d1aeeeSChris Mason void btrfs_throttle(struct btrfs_root *root)
41937d1aeeeSChris Mason {
420a4abeea4SJosef Bacik 	if (!atomic_read(&root->fs_info->open_ioctl_trans))
42137d1aeeeSChris Mason 		wait_current_trans(root);
42237d1aeeeSChris Mason }
42337d1aeeeSChris Mason 
4248929ecfaSYan, Zheng static int should_end_transaction(struct btrfs_trans_handle *trans,
4258929ecfaSYan, Zheng 				  struct btrfs_root *root)
4268929ecfaSYan, Zheng {
4278929ecfaSYan, Zheng 	int ret;
42836ba022aSJosef Bacik 
42936ba022aSJosef Bacik 	ret = btrfs_block_rsv_check(root, &root->fs_info->global_block_rsv, 5);
4308929ecfaSYan, Zheng 	return ret ? 1 : 0;
4318929ecfaSYan, Zheng }
4328929ecfaSYan, Zheng 
4338929ecfaSYan, Zheng int btrfs_should_end_transaction(struct btrfs_trans_handle *trans,
4348929ecfaSYan, Zheng 				 struct btrfs_root *root)
4358929ecfaSYan, Zheng {
4368929ecfaSYan, Zheng 	struct btrfs_transaction *cur_trans = trans->transaction;
4379c8d86dbSJosef Bacik 	struct btrfs_block_rsv *rsv = trans->block_rsv;
4388929ecfaSYan, Zheng 	int updates;
4398929ecfaSYan, Zheng 
440a4abeea4SJosef Bacik 	smp_mb();
4418929ecfaSYan, Zheng 	if (cur_trans->blocked || cur_trans->delayed_refs.flushing)
4428929ecfaSYan, Zheng 		return 1;
4438929ecfaSYan, Zheng 
4449c8d86dbSJosef Bacik 	/*
4459c8d86dbSJosef Bacik 	 * We need to do this in case we're deleting csums so the global block
4469c8d86dbSJosef Bacik 	 * rsv get's used instead of the csum block rsv.
4479c8d86dbSJosef Bacik 	 */
4489c8d86dbSJosef Bacik 	trans->block_rsv = NULL;
4499c8d86dbSJosef Bacik 
4508929ecfaSYan, Zheng 	updates = trans->delayed_ref_updates;
4518929ecfaSYan, Zheng 	trans->delayed_ref_updates = 0;
4528929ecfaSYan, Zheng 	if (updates)
4538929ecfaSYan, Zheng 		btrfs_run_delayed_refs(trans, root, updates);
4548929ecfaSYan, Zheng 
4559c8d86dbSJosef Bacik 	trans->block_rsv = rsv;
4569c8d86dbSJosef Bacik 
4578929ecfaSYan, Zheng 	return should_end_transaction(trans, root);
4588929ecfaSYan, Zheng }
4598929ecfaSYan, Zheng 
46089ce8a63SChris Mason static int __btrfs_end_transaction(struct btrfs_trans_handle *trans,
4610af3d00bSJosef Bacik 			  struct btrfs_root *root, int throttle, int lock)
46279154b1bSChris Mason {
4638929ecfaSYan, Zheng 	struct btrfs_transaction *cur_trans = trans->transaction;
464ab78c84dSChris Mason 	struct btrfs_fs_info *info = root->fs_info;
465c3e69d58SChris Mason 	int count = 0;
466d6e4a428SChris Mason 
4672a1eb461SJosef Bacik 	if (--trans->use_count) {
4682a1eb461SJosef Bacik 		trans->block_rsv = trans->orig_rsv;
4692a1eb461SJosef Bacik 		return 0;
4702a1eb461SJosef Bacik 	}
4712a1eb461SJosef Bacik 
472b24e03dbSJosef Bacik 	btrfs_trans_release_metadata(trans, root);
4734c13d758SJosef Bacik 	trans->block_rsv = NULL;
474c3e69d58SChris Mason 	while (count < 4) {
475c3e69d58SChris Mason 		unsigned long cur = trans->delayed_ref_updates;
476c3e69d58SChris Mason 		trans->delayed_ref_updates = 0;
477c3e69d58SChris Mason 		if (cur &&
478c3e69d58SChris Mason 		    trans->transaction->delayed_refs.num_heads_ready > 64) {
479c3e69d58SChris Mason 			trans->delayed_ref_updates = 0;
480b7ec40d7SChris Mason 
481b7ec40d7SChris Mason 			/*
482b7ec40d7SChris Mason 			 * do a full flush if the transaction is trying
483b7ec40d7SChris Mason 			 * to close
484b7ec40d7SChris Mason 			 */
485b7ec40d7SChris Mason 			if (trans->transaction->delayed_refs.flushing)
486b7ec40d7SChris Mason 				cur = 0;
487c3e69d58SChris Mason 			btrfs_run_delayed_refs(trans, root, cur);
488c3e69d58SChris Mason 		} else {
489c3e69d58SChris Mason 			break;
490c3e69d58SChris Mason 		}
491c3e69d58SChris Mason 		count++;
49256bec294SChris Mason 	}
49356bec294SChris Mason 
494a4abeea4SJosef Bacik 	if (lock && !atomic_read(&root->fs_info->open_ioctl_trans) &&
495a4abeea4SJosef Bacik 	    should_end_transaction(trans, root)) {
4968929ecfaSYan, Zheng 		trans->transaction->blocked = 1;
497a4abeea4SJosef Bacik 		smp_wmb();
498a4abeea4SJosef Bacik 	}
4998929ecfaSYan, Zheng 
5000af3d00bSJosef Bacik 	if (lock && cur_trans->blocked && !cur_trans->in_commit) {
50181317fdeSJosef Bacik 		if (throttle) {
50281317fdeSJosef Bacik 			/*
50381317fdeSJosef Bacik 			 * We may race with somebody else here so end up having
50481317fdeSJosef Bacik 			 * to call end_transaction on ourselves again, so inc
50581317fdeSJosef Bacik 			 * our use_count.
50681317fdeSJosef Bacik 			 */
50781317fdeSJosef Bacik 			trans->use_count++;
5088929ecfaSYan, Zheng 			return btrfs_commit_transaction(trans, root);
50981317fdeSJosef Bacik 		} else {
5108929ecfaSYan, Zheng 			wake_up_process(info->transaction_kthread);
5118929ecfaSYan, Zheng 		}
51281317fdeSJosef Bacik 	}
5138929ecfaSYan, Zheng 
5148929ecfaSYan, Zheng 	WARN_ON(cur_trans != info->running_transaction);
51513c5a93eSJosef Bacik 	WARN_ON(atomic_read(&cur_trans->num_writers) < 1);
51613c5a93eSJosef Bacik 	atomic_dec(&cur_trans->num_writers);
51789ce8a63SChris Mason 
51899d16cbcSSage Weil 	smp_mb();
51979154b1bSChris Mason 	if (waitqueue_active(&cur_trans->writer_wait))
52079154b1bSChris Mason 		wake_up(&cur_trans->writer_wait);
52179154b1bSChris Mason 	put_transaction(cur_trans);
5229ed74f2dSJosef Bacik 
5239ed74f2dSJosef Bacik 	if (current->journal_info == trans)
5249ed74f2dSJosef Bacik 		current->journal_info = NULL;
525d6025579SChris Mason 	memset(trans, 0, sizeof(*trans));
5262c90e5d6SChris Mason 	kmem_cache_free(btrfs_trans_handle_cachep, trans);
527ab78c84dSChris Mason 
52824bbcf04SYan, Zheng 	if (throttle)
52924bbcf04SYan, Zheng 		btrfs_run_delayed_iputs(root);
53024bbcf04SYan, Zheng 
53179154b1bSChris Mason 	return 0;
53279154b1bSChris Mason }
53379154b1bSChris Mason 
53489ce8a63SChris Mason int btrfs_end_transaction(struct btrfs_trans_handle *trans,
53589ce8a63SChris Mason 			  struct btrfs_root *root)
53689ce8a63SChris Mason {
53716cdcec7SMiao Xie 	int ret;
53816cdcec7SMiao Xie 
53916cdcec7SMiao Xie 	ret = __btrfs_end_transaction(trans, root, 0, 1);
54016cdcec7SMiao Xie 	if (ret)
54116cdcec7SMiao Xie 		return ret;
54216cdcec7SMiao Xie 	return 0;
54389ce8a63SChris Mason }
54489ce8a63SChris Mason 
54589ce8a63SChris Mason int btrfs_end_transaction_throttle(struct btrfs_trans_handle *trans,
54689ce8a63SChris Mason 				   struct btrfs_root *root)
54789ce8a63SChris Mason {
54816cdcec7SMiao Xie 	int ret;
54916cdcec7SMiao Xie 
55016cdcec7SMiao Xie 	ret = __btrfs_end_transaction(trans, root, 1, 1);
55116cdcec7SMiao Xie 	if (ret)
55216cdcec7SMiao Xie 		return ret;
55316cdcec7SMiao Xie 	return 0;
5540af3d00bSJosef Bacik }
5550af3d00bSJosef Bacik 
5560af3d00bSJosef Bacik int btrfs_end_transaction_nolock(struct btrfs_trans_handle *trans,
5570af3d00bSJosef Bacik 				 struct btrfs_root *root)
5580af3d00bSJosef Bacik {
55916cdcec7SMiao Xie 	int ret;
56016cdcec7SMiao Xie 
56116cdcec7SMiao Xie 	ret = __btrfs_end_transaction(trans, root, 0, 0);
56216cdcec7SMiao Xie 	if (ret)
56316cdcec7SMiao Xie 		return ret;
56416cdcec7SMiao Xie 	return 0;
56516cdcec7SMiao Xie }
56616cdcec7SMiao Xie 
56716cdcec7SMiao Xie int btrfs_end_transaction_dmeta(struct btrfs_trans_handle *trans,
56816cdcec7SMiao Xie 				struct btrfs_root *root)
56916cdcec7SMiao Xie {
57016cdcec7SMiao Xie 	return __btrfs_end_transaction(trans, root, 1, 1);
57189ce8a63SChris Mason }
57289ce8a63SChris Mason 
573d352ac68SChris Mason /*
574d352ac68SChris Mason  * when btree blocks are allocated, they have some corresponding bits set for
575d352ac68SChris Mason  * them in one of two extent_io trees.  This is used to make sure all of
576690587d1SChris Mason  * those extents are sent to disk but does not wait on them
577d352ac68SChris Mason  */
578690587d1SChris Mason int btrfs_write_marked_extents(struct btrfs_root *root,
5798cef4e16SYan, Zheng 			       struct extent_io_tree *dirty_pages, int mark)
58079154b1bSChris Mason {
581777e6bd7SChris Mason 	int err = 0;
5827c4452b9SChris Mason 	int werr = 0;
5831728366eSJosef Bacik 	struct address_space *mapping = root->fs_info->btree_inode->i_mapping;
584777e6bd7SChris Mason 	u64 start = 0;
5855f39d397SChris Mason 	u64 end;
5867c4452b9SChris Mason 
5871728366eSJosef Bacik 	while (!find_first_extent_bit(dirty_pages, start, &start, &end,
5881728366eSJosef Bacik 				      mark)) {
5891728366eSJosef Bacik 		convert_extent_bit(dirty_pages, start, end, EXTENT_NEED_WAIT, mark,
5901728366eSJosef Bacik 				   GFP_NOFS);
5911728366eSJosef Bacik 		err = filemap_fdatawrite_range(mapping, start, end);
5927c4452b9SChris Mason 		if (err)
5937c4452b9SChris Mason 			werr = err;
5941728366eSJosef Bacik 		cond_resched();
5951728366eSJosef Bacik 		start = end + 1;
5967c4452b9SChris Mason 	}
597690587d1SChris Mason 	if (err)
598690587d1SChris Mason 		werr = err;
599690587d1SChris Mason 	return werr;
600690587d1SChris Mason }
601690587d1SChris Mason 
602690587d1SChris Mason /*
603690587d1SChris Mason  * when btree blocks are allocated, they have some corresponding bits set for
604690587d1SChris Mason  * them in one of two extent_io trees.  This is used to make sure all of
605690587d1SChris Mason  * those extents are on disk for transaction or log commit.  We wait
606690587d1SChris Mason  * on all the pages and clear them from the dirty pages state tree
607690587d1SChris Mason  */
608690587d1SChris Mason int btrfs_wait_marked_extents(struct btrfs_root *root,
6098cef4e16SYan, Zheng 			      struct extent_io_tree *dirty_pages, int mark)
610690587d1SChris Mason {
611690587d1SChris Mason 	int err = 0;
612690587d1SChris Mason 	int werr = 0;
6131728366eSJosef Bacik 	struct address_space *mapping = root->fs_info->btree_inode->i_mapping;
614690587d1SChris Mason 	u64 start = 0;
615690587d1SChris Mason 	u64 end;
616690587d1SChris Mason 
6171728366eSJosef Bacik 	while (!find_first_extent_bit(dirty_pages, start, &start, &end,
6181728366eSJosef Bacik 				      EXTENT_NEED_WAIT)) {
6191728366eSJosef Bacik 		clear_extent_bits(dirty_pages, start, end, EXTENT_NEED_WAIT, GFP_NOFS);
6201728366eSJosef Bacik 		err = filemap_fdatawait_range(mapping, start, end);
621777e6bd7SChris Mason 		if (err)
622777e6bd7SChris Mason 			werr = err;
623777e6bd7SChris Mason 		cond_resched();
6241728366eSJosef Bacik 		start = end + 1;
625777e6bd7SChris Mason 	}
6267c4452b9SChris Mason 	if (err)
6277c4452b9SChris Mason 		werr = err;
6287c4452b9SChris Mason 	return werr;
62979154b1bSChris Mason }
63079154b1bSChris Mason 
631690587d1SChris Mason /*
632690587d1SChris Mason  * when btree blocks are allocated, they have some corresponding bits set for
633690587d1SChris Mason  * them in one of two extent_io trees.  This is used to make sure all of
634690587d1SChris Mason  * those extents are on disk for transaction or log commit
635690587d1SChris Mason  */
636690587d1SChris Mason int btrfs_write_and_wait_marked_extents(struct btrfs_root *root,
6378cef4e16SYan, Zheng 				struct extent_io_tree *dirty_pages, int mark)
638690587d1SChris Mason {
639690587d1SChris Mason 	int ret;
640690587d1SChris Mason 	int ret2;
641690587d1SChris Mason 
6428cef4e16SYan, Zheng 	ret = btrfs_write_marked_extents(root, dirty_pages, mark);
6438cef4e16SYan, Zheng 	ret2 = btrfs_wait_marked_extents(root, dirty_pages, mark);
644bf0da8c1SChris Mason 
645bf0da8c1SChris Mason 	if (ret)
646bf0da8c1SChris Mason 		return ret;
647bf0da8c1SChris Mason 	if (ret2)
648bf0da8c1SChris Mason 		return ret2;
649bf0da8c1SChris Mason 	return 0;
650690587d1SChris Mason }
651690587d1SChris Mason 
652d0c803c4SChris Mason int btrfs_write_and_wait_transaction(struct btrfs_trans_handle *trans,
653d0c803c4SChris Mason 				     struct btrfs_root *root)
654d0c803c4SChris Mason {
655d0c803c4SChris Mason 	if (!trans || !trans->transaction) {
656d0c803c4SChris Mason 		struct inode *btree_inode;
657d0c803c4SChris Mason 		btree_inode = root->fs_info->btree_inode;
658d0c803c4SChris Mason 		return filemap_write_and_wait(btree_inode->i_mapping);
659d0c803c4SChris Mason 	}
660d0c803c4SChris Mason 	return btrfs_write_and_wait_marked_extents(root,
6618cef4e16SYan, Zheng 					   &trans->transaction->dirty_pages,
6628cef4e16SYan, Zheng 					   EXTENT_DIRTY);
663d0c803c4SChris Mason }
664d0c803c4SChris Mason 
665d352ac68SChris Mason /*
666d352ac68SChris Mason  * this is used to update the root pointer in the tree of tree roots.
667d352ac68SChris Mason  *
668d352ac68SChris Mason  * But, in the case of the extent allocation tree, updating the root
669d352ac68SChris Mason  * pointer may allocate blocks which may change the root of the extent
670d352ac68SChris Mason  * allocation tree.
671d352ac68SChris Mason  *
672d352ac68SChris Mason  * So, this loops and repeats and makes sure the cowonly root didn't
673d352ac68SChris Mason  * change while the root pointer was being updated in the metadata.
674d352ac68SChris Mason  */
6750b86a832SChris Mason static int update_cowonly_root(struct btrfs_trans_handle *trans,
67679154b1bSChris Mason 			       struct btrfs_root *root)
67779154b1bSChris Mason {
67879154b1bSChris Mason 	int ret;
6790b86a832SChris Mason 	u64 old_root_bytenr;
68086b9f2ecSYan, Zheng 	u64 old_root_used;
6810b86a832SChris Mason 	struct btrfs_root *tree_root = root->fs_info->tree_root;
68279154b1bSChris Mason 
68386b9f2ecSYan, Zheng 	old_root_used = btrfs_root_used(&root->root_item);
6840b86a832SChris Mason 	btrfs_write_dirty_block_groups(trans, root);
68556bec294SChris Mason 
68679154b1bSChris Mason 	while (1) {
6870b86a832SChris Mason 		old_root_bytenr = btrfs_root_bytenr(&root->root_item);
68886b9f2ecSYan, Zheng 		if (old_root_bytenr == root->node->start &&
68986b9f2ecSYan, Zheng 		    old_root_used == btrfs_root_used(&root->root_item))
69079154b1bSChris Mason 			break;
69187ef2bb4SChris Mason 
6925d4f98a2SYan Zheng 		btrfs_set_root_node(&root->root_item, root->node);
69379154b1bSChris Mason 		ret = btrfs_update_root(trans, tree_root,
6940b86a832SChris Mason 					&root->root_key,
6950b86a832SChris Mason 					&root->root_item);
69679154b1bSChris Mason 		BUG_ON(ret);
69756bec294SChris Mason 
69886b9f2ecSYan, Zheng 		old_root_used = btrfs_root_used(&root->root_item);
6994a8c9a62SYan Zheng 		ret = btrfs_write_dirty_block_groups(trans, root);
70056bec294SChris Mason 		BUG_ON(ret);
7010b86a832SChris Mason 	}
702276e680dSYan Zheng 
703276e680dSYan Zheng 	if (root != root->fs_info->extent_root)
704817d52f8SJosef Bacik 		switch_commit_root(root);
705276e680dSYan Zheng 
7060b86a832SChris Mason 	return 0;
7070b86a832SChris Mason }
7080b86a832SChris Mason 
709d352ac68SChris Mason /*
710d352ac68SChris Mason  * update all the cowonly tree roots on disk
711d352ac68SChris Mason  */
7125d4f98a2SYan Zheng static noinline int commit_cowonly_roots(struct btrfs_trans_handle *trans,
7130b86a832SChris Mason 					 struct btrfs_root *root)
7140b86a832SChris Mason {
7150b86a832SChris Mason 	struct btrfs_fs_info *fs_info = root->fs_info;
7160b86a832SChris Mason 	struct list_head *next;
71784234f3aSYan Zheng 	struct extent_buffer *eb;
71856bec294SChris Mason 	int ret;
71984234f3aSYan Zheng 
72056bec294SChris Mason 	ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
72156bec294SChris Mason 	BUG_ON(ret);
72287ef2bb4SChris Mason 
72384234f3aSYan Zheng 	eb = btrfs_lock_root_node(fs_info->tree_root);
7249fa8cfe7SChris Mason 	btrfs_cow_block(trans, fs_info->tree_root, eb, NULL, 0, &eb);
72584234f3aSYan Zheng 	btrfs_tree_unlock(eb);
72684234f3aSYan Zheng 	free_extent_buffer(eb);
7270b86a832SChris Mason 
72856bec294SChris Mason 	ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
72956bec294SChris Mason 	BUG_ON(ret);
73087ef2bb4SChris Mason 
7310b86a832SChris Mason 	while (!list_empty(&fs_info->dirty_cowonly_roots)) {
7320b86a832SChris Mason 		next = fs_info->dirty_cowonly_roots.next;
7330b86a832SChris Mason 		list_del_init(next);
7340b86a832SChris Mason 		root = list_entry(next, struct btrfs_root, dirty_list);
73587ef2bb4SChris Mason 
7360b86a832SChris Mason 		update_cowonly_root(trans, root);
73779154b1bSChris Mason 	}
738276e680dSYan Zheng 
739276e680dSYan Zheng 	down_write(&fs_info->extent_commit_sem);
740276e680dSYan Zheng 	switch_commit_root(fs_info->extent_root);
741276e680dSYan Zheng 	up_write(&fs_info->extent_commit_sem);
742276e680dSYan Zheng 
74379154b1bSChris Mason 	return 0;
74479154b1bSChris Mason }
74579154b1bSChris Mason 
746d352ac68SChris Mason /*
747d352ac68SChris Mason  * dead roots are old snapshots that need to be deleted.  This allocates
748d352ac68SChris Mason  * a dirty root struct and adds it into the list of dead roots that need to
749d352ac68SChris Mason  * be deleted
750d352ac68SChris Mason  */
7515d4f98a2SYan Zheng int btrfs_add_dead_root(struct btrfs_root *root)
7525eda7b5eSChris Mason {
753a4abeea4SJosef Bacik 	spin_lock(&root->fs_info->trans_lock);
7545d4f98a2SYan Zheng 	list_add(&root->root_list, &root->fs_info->dead_roots);
755a4abeea4SJosef Bacik 	spin_unlock(&root->fs_info->trans_lock);
7565eda7b5eSChris Mason 	return 0;
7575eda7b5eSChris Mason }
7585eda7b5eSChris Mason 
759d352ac68SChris Mason /*
7605d4f98a2SYan Zheng  * update all the cowonly tree roots on disk
761d352ac68SChris Mason  */
7625d4f98a2SYan Zheng static noinline int commit_fs_roots(struct btrfs_trans_handle *trans,
7635d4f98a2SYan Zheng 				    struct btrfs_root *root)
7640f7d52f4SChris Mason {
7650f7d52f4SChris Mason 	struct btrfs_root *gang[8];
7665d4f98a2SYan Zheng 	struct btrfs_fs_info *fs_info = root->fs_info;
7670f7d52f4SChris Mason 	int i;
7680f7d52f4SChris Mason 	int ret;
76954aa1f4dSChris Mason 	int err = 0;
77054aa1f4dSChris Mason 
771a4abeea4SJosef Bacik 	spin_lock(&fs_info->fs_roots_radix_lock);
7720f7d52f4SChris Mason 	while (1) {
7735d4f98a2SYan Zheng 		ret = radix_tree_gang_lookup_tag(&fs_info->fs_roots_radix,
7745d4f98a2SYan Zheng 						 (void **)gang, 0,
7750f7d52f4SChris Mason 						 ARRAY_SIZE(gang),
7760f7d52f4SChris Mason 						 BTRFS_ROOT_TRANS_TAG);
7770f7d52f4SChris Mason 		if (ret == 0)
7780f7d52f4SChris Mason 			break;
7790f7d52f4SChris Mason 		for (i = 0; i < ret; i++) {
7800f7d52f4SChris Mason 			root = gang[i];
7815d4f98a2SYan Zheng 			radix_tree_tag_clear(&fs_info->fs_roots_radix,
7822619ba1fSChris Mason 					(unsigned long)root->root_key.objectid,
7830f7d52f4SChris Mason 					BTRFS_ROOT_TRANS_TAG);
784a4abeea4SJosef Bacik 			spin_unlock(&fs_info->fs_roots_radix_lock);
78531153d81SYan Zheng 
786e02119d5SChris Mason 			btrfs_free_log(trans, root);
7875d4f98a2SYan Zheng 			btrfs_update_reloc_root(trans, root);
788d68fc57bSYan, Zheng 			btrfs_orphan_commit_root(trans, root);
789e02119d5SChris Mason 
79082d5902dSLi Zefan 			btrfs_save_ino_cache(root, trans);
79182d5902dSLi Zefan 
792f1ebcc74SLiu Bo 			/* see comments in should_cow_block() */
793f1ebcc74SLiu Bo 			root->force_cow = 0;
794f1ebcc74SLiu Bo 			smp_wmb();
795f1ebcc74SLiu Bo 
796978d910dSYan Zheng 			if (root->commit_root != root->node) {
797581bb050SLi Zefan 				mutex_lock(&root->fs_commit_mutex);
798817d52f8SJosef Bacik 				switch_commit_root(root);
799581bb050SLi Zefan 				btrfs_unpin_free_ino(root);
800581bb050SLi Zefan 				mutex_unlock(&root->fs_commit_mutex);
801581bb050SLi Zefan 
802978d910dSYan Zheng 				btrfs_set_root_node(&root->root_item,
803978d910dSYan Zheng 						    root->node);
804978d910dSYan Zheng 			}
80531153d81SYan Zheng 
8065d4f98a2SYan Zheng 			err = btrfs_update_root(trans, fs_info->tree_root,
8070f7d52f4SChris Mason 						&root->root_key,
8080f7d52f4SChris Mason 						&root->root_item);
809a4abeea4SJosef Bacik 			spin_lock(&fs_info->fs_roots_radix_lock);
81054aa1f4dSChris Mason 			if (err)
81154aa1f4dSChris Mason 				break;
8120f7d52f4SChris Mason 		}
8139f3a7427SChris Mason 	}
814a4abeea4SJosef Bacik 	spin_unlock(&fs_info->fs_roots_radix_lock);
81554aa1f4dSChris Mason 	return err;
8160f7d52f4SChris Mason }
8170f7d52f4SChris Mason 
818d352ac68SChris Mason /*
819d352ac68SChris Mason  * defrag a given btree.  If cacheonly == 1, this won't read from the disk,
820d352ac68SChris Mason  * otherwise every leaf in the btree is read and defragged.
821d352ac68SChris Mason  */
822e9d0b13bSChris Mason int btrfs_defrag_root(struct btrfs_root *root, int cacheonly)
823e9d0b13bSChris Mason {
824e9d0b13bSChris Mason 	struct btrfs_fs_info *info = root->fs_info;
825e9d0b13bSChris Mason 	struct btrfs_trans_handle *trans;
8268929ecfaSYan, Zheng 	int ret;
827d3c2fdcfSChris Mason 	unsigned long nr;
828e9d0b13bSChris Mason 
8298929ecfaSYan, Zheng 	if (xchg(&root->defrag_running, 1))
830e9d0b13bSChris Mason 		return 0;
8318929ecfaSYan, Zheng 
8326b80053dSChris Mason 	while (1) {
8338929ecfaSYan, Zheng 		trans = btrfs_start_transaction(root, 0);
8348929ecfaSYan, Zheng 		if (IS_ERR(trans))
8358929ecfaSYan, Zheng 			return PTR_ERR(trans);
8368929ecfaSYan, Zheng 
837e9d0b13bSChris Mason 		ret = btrfs_defrag_leaves(trans, root, cacheonly);
8388929ecfaSYan, Zheng 
839d3c2fdcfSChris Mason 		nr = trans->blocks_used;
840e9d0b13bSChris Mason 		btrfs_end_transaction(trans, root);
841d3c2fdcfSChris Mason 		btrfs_btree_balance_dirty(info->tree_root, nr);
842e9d0b13bSChris Mason 		cond_resched();
843e9d0b13bSChris Mason 
8447841cb28SDavid Sterba 		if (btrfs_fs_closing(root->fs_info) || ret != -EAGAIN)
845e9d0b13bSChris Mason 			break;
846e9d0b13bSChris Mason 	}
847e9d0b13bSChris Mason 	root->defrag_running = 0;
8488929ecfaSYan, Zheng 	return ret;
849e9d0b13bSChris Mason }
850e9d0b13bSChris Mason 
851d352ac68SChris Mason /*
852d352ac68SChris Mason  * new snapshots need to be created at a very specific time in the
853d352ac68SChris Mason  * transaction commit.  This does the actual creation
854d352ac68SChris Mason  */
85580b6794dSChris Mason static noinline int create_pending_snapshot(struct btrfs_trans_handle *trans,
8563063d29fSChris Mason 				   struct btrfs_fs_info *fs_info,
8573063d29fSChris Mason 				   struct btrfs_pending_snapshot *pending)
8583063d29fSChris Mason {
8593063d29fSChris Mason 	struct btrfs_key key;
86080b6794dSChris Mason 	struct btrfs_root_item *new_root_item;
8613063d29fSChris Mason 	struct btrfs_root *tree_root = fs_info->tree_root;
8623063d29fSChris Mason 	struct btrfs_root *root = pending->root;
8636bdb72deSSage Weil 	struct btrfs_root *parent_root;
86498c9942aSLiu Bo 	struct btrfs_block_rsv *rsv;
8656bdb72deSSage Weil 	struct inode *parent_inode;
8666a912213SJosef Bacik 	struct dentry *parent;
867a22285a6SYan, Zheng 	struct dentry *dentry;
8683063d29fSChris Mason 	struct extent_buffer *tmp;
869925baeddSChris Mason 	struct extent_buffer *old;
8703063d29fSChris Mason 	int ret;
871d68fc57bSYan, Zheng 	u64 to_reserve = 0;
8726bdb72deSSage Weil 	u64 index = 0;
873a22285a6SYan, Zheng 	u64 objectid;
874b83cc969SLi Zefan 	u64 root_flags;
8753063d29fSChris Mason 
87698c9942aSLiu Bo 	rsv = trans->block_rsv;
87798c9942aSLiu Bo 
87880b6794dSChris Mason 	new_root_item = kmalloc(sizeof(*new_root_item), GFP_NOFS);
87980b6794dSChris Mason 	if (!new_root_item) {
880a22285a6SYan, Zheng 		pending->error = -ENOMEM;
88180b6794dSChris Mason 		goto fail;
88280b6794dSChris Mason 	}
883a22285a6SYan, Zheng 
884581bb050SLi Zefan 	ret = btrfs_find_free_objectid(tree_root, &objectid);
885a22285a6SYan, Zheng 	if (ret) {
886a22285a6SYan, Zheng 		pending->error = ret;
8873063d29fSChris Mason 		goto fail;
888a22285a6SYan, Zheng 	}
8893063d29fSChris Mason 
8903fd0a558SYan, Zheng 	btrfs_reloc_pre_snapshot(trans, pending, &to_reserve);
891d68fc57bSYan, Zheng 
892d68fc57bSYan, Zheng 	if (to_reserve > 0) {
89362f30c54SMiao Xie 		ret = btrfs_block_rsv_add_noflush(root, &pending->block_rsv,
8948bb8ab2eSJosef Bacik 						  to_reserve);
895d68fc57bSYan, Zheng 		if (ret) {
896d68fc57bSYan, Zheng 			pending->error = ret;
897d68fc57bSYan, Zheng 			goto fail;
898d68fc57bSYan, Zheng 		}
899d68fc57bSYan, Zheng 	}
900d68fc57bSYan, Zheng 
9013063d29fSChris Mason 	key.objectid = objectid;
902a22285a6SYan, Zheng 	key.offset = (u64)-1;
903a22285a6SYan, Zheng 	key.type = BTRFS_ROOT_ITEM_KEY;
9043063d29fSChris Mason 
905a22285a6SYan, Zheng 	trans->block_rsv = &pending->block_rsv;
9066bdb72deSSage Weil 
907a22285a6SYan, Zheng 	dentry = pending->dentry;
9086a912213SJosef Bacik 	parent = dget_parent(dentry);
9096a912213SJosef Bacik 	parent_inode = parent->d_inode;
910a22285a6SYan, Zheng 	parent_root = BTRFS_I(parent_inode)->root;
9117585717fSChris Mason 	record_root_in_trans(trans, parent_root);
912a22285a6SYan, Zheng 
9136bdb72deSSage Weil 	/*
9146bdb72deSSage Weil 	 * insert the directory item
9156bdb72deSSage Weil 	 */
9166bdb72deSSage Weil 	ret = btrfs_set_inode_index(parent_inode, &index);
9176bdb72deSSage Weil 	BUG_ON(ret);
9186bdb72deSSage Weil 	ret = btrfs_insert_dir_item(trans, parent_root,
919a22285a6SYan, Zheng 				dentry->d_name.name, dentry->d_name.len,
92016cdcec7SMiao Xie 				parent_inode, &key,
921a22285a6SYan, Zheng 				BTRFS_FT_DIR, index);
9226bdb72deSSage Weil 	BUG_ON(ret);
9236bdb72deSSage Weil 
924a22285a6SYan, Zheng 	btrfs_i_size_write(parent_inode, parent_inode->i_size +
925a22285a6SYan, Zheng 					 dentry->d_name.len * 2);
9266bdb72deSSage Weil 	ret = btrfs_update_inode(trans, parent_root, parent_inode);
9276bdb72deSSage Weil 	BUG_ON(ret);
9286bdb72deSSage Weil 
929e999376fSChris Mason 	/*
930e999376fSChris Mason 	 * pull in the delayed directory update
931e999376fSChris Mason 	 * and the delayed inode item
932e999376fSChris Mason 	 * otherwise we corrupt the FS during
933e999376fSChris Mason 	 * snapshot
934e999376fSChris Mason 	 */
935e999376fSChris Mason 	ret = btrfs_run_delayed_items(trans, root);
936e999376fSChris Mason 	BUG_ON(ret);
937e999376fSChris Mason 
9387585717fSChris Mason 	record_root_in_trans(trans, root);
9396bdb72deSSage Weil 	btrfs_set_root_last_snapshot(&root->root_item, trans->transid);
9406bdb72deSSage Weil 	memcpy(new_root_item, &root->root_item, sizeof(*new_root_item));
94108fe4db1SLi Zefan 	btrfs_check_and_init_root_item(new_root_item);
9426bdb72deSSage Weil 
943b83cc969SLi Zefan 	root_flags = btrfs_root_flags(new_root_item);
944b83cc969SLi Zefan 	if (pending->readonly)
945b83cc969SLi Zefan 		root_flags |= BTRFS_ROOT_SUBVOL_RDONLY;
946b83cc969SLi Zefan 	else
947b83cc969SLi Zefan 		root_flags &= ~BTRFS_ROOT_SUBVOL_RDONLY;
948b83cc969SLi Zefan 	btrfs_set_root_flags(new_root_item, root_flags);
949b83cc969SLi Zefan 
950925baeddSChris Mason 	old = btrfs_lock_root_node(root);
9519fa8cfe7SChris Mason 	btrfs_cow_block(trans, root, old, NULL, 0, &old);
9525d4f98a2SYan Zheng 	btrfs_set_lock_blocking(old);
9533063d29fSChris Mason 
954925baeddSChris Mason 	btrfs_copy_root(trans, root, old, &tmp, objectid);
955925baeddSChris Mason 	btrfs_tree_unlock(old);
956925baeddSChris Mason 	free_extent_buffer(old);
9573063d29fSChris Mason 
958f1ebcc74SLiu Bo 	/* see comments in should_cow_block() */
959f1ebcc74SLiu Bo 	root->force_cow = 1;
960f1ebcc74SLiu Bo 	smp_wmb();
961f1ebcc74SLiu Bo 
9625d4f98a2SYan Zheng 	btrfs_set_root_node(new_root_item, tmp);
963a22285a6SYan, Zheng 	/* record when the snapshot was created in key.offset */
964a22285a6SYan, Zheng 	key.offset = trans->transid;
965a22285a6SYan, Zheng 	ret = btrfs_insert_root(trans, tree_root, &key, new_root_item);
966925baeddSChris Mason 	btrfs_tree_unlock(tmp);
9673063d29fSChris Mason 	free_extent_buffer(tmp);
9680660b5afSChris Mason 	BUG_ON(ret);
9690660b5afSChris Mason 
970a22285a6SYan, Zheng 	/*
971a22285a6SYan, Zheng 	 * insert root back/forward references
972a22285a6SYan, Zheng 	 */
973a22285a6SYan, Zheng 	ret = btrfs_add_root_ref(trans, tree_root, objectid,
974a22285a6SYan, Zheng 				 parent_root->root_key.objectid,
97533345d01SLi Zefan 				 btrfs_ino(parent_inode), index,
976a22285a6SYan, Zheng 				 dentry->d_name.name, dentry->d_name.len);
977a22285a6SYan, Zheng 	BUG_ON(ret);
9786a912213SJosef Bacik 	dput(parent);
979a22285a6SYan, Zheng 
980a22285a6SYan, Zheng 	key.offset = (u64)-1;
981a22285a6SYan, Zheng 	pending->snap = btrfs_read_fs_root_no_name(root->fs_info, &key);
982a22285a6SYan, Zheng 	BUG_ON(IS_ERR(pending->snap));
983d68fc57bSYan, Zheng 
9843fd0a558SYan, Zheng 	btrfs_reloc_post_snapshot(trans, pending);
9853063d29fSChris Mason fail:
9866bdb72deSSage Weil 	kfree(new_root_item);
98798c9942aSLiu Bo 	trans->block_rsv = rsv;
988a22285a6SYan, Zheng 	btrfs_block_rsv_release(root, &pending->block_rsv, (u64)-1);
989a22285a6SYan, Zheng 	return 0;
9903063d29fSChris Mason }
9913063d29fSChris Mason 
992d352ac68SChris Mason /*
993d352ac68SChris Mason  * create all the snapshots we've scheduled for creation
994d352ac68SChris Mason  */
99580b6794dSChris Mason static noinline int create_pending_snapshots(struct btrfs_trans_handle *trans,
9963063d29fSChris Mason 					     struct btrfs_fs_info *fs_info)
9973063d29fSChris Mason {
9983063d29fSChris Mason 	struct btrfs_pending_snapshot *pending;
9993063d29fSChris Mason 	struct list_head *head = &trans->transaction->pending_snapshots;
10003de4586cSChris Mason 	int ret;
10013de4586cSChris Mason 
1002c6e30871SQinghuang Feng 	list_for_each_entry(pending, head, list) {
10033de4586cSChris Mason 		ret = create_pending_snapshot(trans, fs_info, pending);
10043de4586cSChris Mason 		BUG_ON(ret);
10053de4586cSChris Mason 	}
10063de4586cSChris Mason 	return 0;
10073de4586cSChris Mason }
10083de4586cSChris Mason 
10095d4f98a2SYan Zheng static void update_super_roots(struct btrfs_root *root)
10105d4f98a2SYan Zheng {
10115d4f98a2SYan Zheng 	struct btrfs_root_item *root_item;
10125d4f98a2SYan Zheng 	struct btrfs_super_block *super;
10135d4f98a2SYan Zheng 
10146c41761fSDavid Sterba 	super = root->fs_info->super_copy;
10155d4f98a2SYan Zheng 
10165d4f98a2SYan Zheng 	root_item = &root->fs_info->chunk_root->root_item;
10175d4f98a2SYan Zheng 	super->chunk_root = root_item->bytenr;
10185d4f98a2SYan Zheng 	super->chunk_root_generation = root_item->generation;
10195d4f98a2SYan Zheng 	super->chunk_root_level = root_item->level;
10205d4f98a2SYan Zheng 
10215d4f98a2SYan Zheng 	root_item = &root->fs_info->tree_root->root_item;
10225d4f98a2SYan Zheng 	super->root = root_item->bytenr;
10235d4f98a2SYan Zheng 	super->generation = root_item->generation;
10245d4f98a2SYan Zheng 	super->root_level = root_item->level;
102573bc1876SJosef Bacik 	if (btrfs_test_opt(root, SPACE_CACHE))
10260af3d00bSJosef Bacik 		super->cache_generation = root_item->generation;
10275d4f98a2SYan Zheng }
10285d4f98a2SYan Zheng 
1029f36f3042SChris Mason int btrfs_transaction_in_commit(struct btrfs_fs_info *info)
1030f36f3042SChris Mason {
1031f36f3042SChris Mason 	int ret = 0;
1032a4abeea4SJosef Bacik 	spin_lock(&info->trans_lock);
1033f36f3042SChris Mason 	if (info->running_transaction)
1034f36f3042SChris Mason 		ret = info->running_transaction->in_commit;
1035a4abeea4SJosef Bacik 	spin_unlock(&info->trans_lock);
1036f36f3042SChris Mason 	return ret;
1037f36f3042SChris Mason }
1038f36f3042SChris Mason 
10398929ecfaSYan, Zheng int btrfs_transaction_blocked(struct btrfs_fs_info *info)
10408929ecfaSYan, Zheng {
10418929ecfaSYan, Zheng 	int ret = 0;
1042a4abeea4SJosef Bacik 	spin_lock(&info->trans_lock);
10438929ecfaSYan, Zheng 	if (info->running_transaction)
10448929ecfaSYan, Zheng 		ret = info->running_transaction->blocked;
1045a4abeea4SJosef Bacik 	spin_unlock(&info->trans_lock);
10468929ecfaSYan, Zheng 	return ret;
10478929ecfaSYan, Zheng }
10488929ecfaSYan, Zheng 
1049bb9c12c9SSage Weil /*
1050bb9c12c9SSage Weil  * wait for the current transaction commit to start and block subsequent
1051bb9c12c9SSage Weil  * transaction joins
1052bb9c12c9SSage Weil  */
1053bb9c12c9SSage Weil static void wait_current_trans_commit_start(struct btrfs_root *root,
1054bb9c12c9SSage Weil 					    struct btrfs_transaction *trans)
1055bb9c12c9SSage Weil {
105672d63ed6SLi Zefan 	wait_event(root->fs_info->transaction_blocked_wait, trans->in_commit);
1057bb9c12c9SSage Weil }
1058bb9c12c9SSage Weil 
1059bb9c12c9SSage Weil /*
1060bb9c12c9SSage Weil  * wait for the current transaction to start and then become unblocked.
1061bb9c12c9SSage Weil  * caller holds ref.
1062bb9c12c9SSage Weil  */
1063bb9c12c9SSage Weil static void wait_current_trans_commit_start_and_unblock(struct btrfs_root *root,
1064bb9c12c9SSage Weil 					 struct btrfs_transaction *trans)
1065bb9c12c9SSage Weil {
106672d63ed6SLi Zefan 	wait_event(root->fs_info->transaction_wait,
106772d63ed6SLi Zefan 		   trans->commit_done || (trans->in_commit && !trans->blocked));
1068bb9c12c9SSage Weil }
1069bb9c12c9SSage Weil 
1070bb9c12c9SSage Weil /*
1071bb9c12c9SSage Weil  * commit transactions asynchronously. once btrfs_commit_transaction_async
1072bb9c12c9SSage Weil  * returns, any subsequent transaction will not be allowed to join.
1073bb9c12c9SSage Weil  */
1074bb9c12c9SSage Weil struct btrfs_async_commit {
1075bb9c12c9SSage Weil 	struct btrfs_trans_handle *newtrans;
1076bb9c12c9SSage Weil 	struct btrfs_root *root;
1077bb9c12c9SSage Weil 	struct delayed_work work;
1078bb9c12c9SSage Weil };
1079bb9c12c9SSage Weil 
1080bb9c12c9SSage Weil static void do_async_commit(struct work_struct *work)
1081bb9c12c9SSage Weil {
1082bb9c12c9SSage Weil 	struct btrfs_async_commit *ac =
1083bb9c12c9SSage Weil 		container_of(work, struct btrfs_async_commit, work.work);
1084bb9c12c9SSage Weil 
1085bb9c12c9SSage Weil 	btrfs_commit_transaction(ac->newtrans, ac->root);
1086bb9c12c9SSage Weil 	kfree(ac);
1087bb9c12c9SSage Weil }
1088bb9c12c9SSage Weil 
1089bb9c12c9SSage Weil int btrfs_commit_transaction_async(struct btrfs_trans_handle *trans,
1090bb9c12c9SSage Weil 				   struct btrfs_root *root,
1091bb9c12c9SSage Weil 				   int wait_for_unblock)
1092bb9c12c9SSage Weil {
1093bb9c12c9SSage Weil 	struct btrfs_async_commit *ac;
1094bb9c12c9SSage Weil 	struct btrfs_transaction *cur_trans;
1095bb9c12c9SSage Weil 
1096bb9c12c9SSage Weil 	ac = kmalloc(sizeof(*ac), GFP_NOFS);
1097db5b493aSTsutomu Itoh 	if (!ac)
1098db5b493aSTsutomu Itoh 		return -ENOMEM;
1099bb9c12c9SSage Weil 
1100bb9c12c9SSage Weil 	INIT_DELAYED_WORK(&ac->work, do_async_commit);
1101bb9c12c9SSage Weil 	ac->root = root;
11027a7eaa40SJosef Bacik 	ac->newtrans = btrfs_join_transaction(root);
11033612b495STsutomu Itoh 	if (IS_ERR(ac->newtrans)) {
11043612b495STsutomu Itoh 		int err = PTR_ERR(ac->newtrans);
11053612b495STsutomu Itoh 		kfree(ac);
11063612b495STsutomu Itoh 		return err;
11073612b495STsutomu Itoh 	}
1108bb9c12c9SSage Weil 
1109bb9c12c9SSage Weil 	/* take transaction reference */
1110bb9c12c9SSage Weil 	cur_trans = trans->transaction;
111113c5a93eSJosef Bacik 	atomic_inc(&cur_trans->use_count);
1112bb9c12c9SSage Weil 
1113bb9c12c9SSage Weil 	btrfs_end_transaction(trans, root);
1114bb9c12c9SSage Weil 	schedule_delayed_work(&ac->work, 0);
1115bb9c12c9SSage Weil 
1116bb9c12c9SSage Weil 	/* wait for transaction to start and unblock */
1117bb9c12c9SSage Weil 	if (wait_for_unblock)
1118bb9c12c9SSage Weil 		wait_current_trans_commit_start_and_unblock(root, cur_trans);
1119bb9c12c9SSage Weil 	else
1120bb9c12c9SSage Weil 		wait_current_trans_commit_start(root, cur_trans);
1121bb9c12c9SSage Weil 
112238e88054SSage Weil 	if (current->journal_info == trans)
112338e88054SSage Weil 		current->journal_info = NULL;
112438e88054SSage Weil 
112538e88054SSage Weil 	put_transaction(cur_trans);
1126bb9c12c9SSage Weil 	return 0;
1127bb9c12c9SSage Weil }
1128bb9c12c9SSage Weil 
1129bb9c12c9SSage Weil /*
1130bb9c12c9SSage Weil  * btrfs_transaction state sequence:
1131bb9c12c9SSage Weil  *    in_commit = 0, blocked = 0  (initial)
1132bb9c12c9SSage Weil  *    in_commit = 1, blocked = 1
1133bb9c12c9SSage Weil  *    blocked = 0
1134bb9c12c9SSage Weil  *    commit_done = 1
1135bb9c12c9SSage Weil  */
113679154b1bSChris Mason int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
113779154b1bSChris Mason 			     struct btrfs_root *root)
113879154b1bSChris Mason {
113915ee9bc7SJosef Bacik 	unsigned long joined = 0;
114079154b1bSChris Mason 	struct btrfs_transaction *cur_trans;
11418fd17795SChris Mason 	struct btrfs_transaction *prev_trans = NULL;
114279154b1bSChris Mason 	DEFINE_WAIT(wait);
114315ee9bc7SJosef Bacik 	int ret;
114489573b9cSChris Mason 	int should_grow = 0;
114589573b9cSChris Mason 	unsigned long now = get_seconds();
1146dccae999SSage Weil 	int flush_on_commit = btrfs_test_opt(root, FLUSHONCOMMIT);
114779154b1bSChris Mason 
11485a3f23d5SChris Mason 	btrfs_run_ordered_operations(root, 0);
11495a3f23d5SChris Mason 
1150b24e03dbSJosef Bacik 	btrfs_trans_release_metadata(trans, root);
11519c8d86dbSJosef Bacik 	trans->block_rsv = NULL;
11529c8d86dbSJosef Bacik 
115356bec294SChris Mason 	/* make a pass through all the delayed refs we have so far
115456bec294SChris Mason 	 * any runnings procs may add more while we are here
115556bec294SChris Mason 	 */
115656bec294SChris Mason 	ret = btrfs_run_delayed_refs(trans, root, 0);
115756bec294SChris Mason 	BUG_ON(ret);
115856bec294SChris Mason 
1159b7ec40d7SChris Mason 	cur_trans = trans->transaction;
116056bec294SChris Mason 	/*
116156bec294SChris Mason 	 * set the flushing flag so procs in this transaction have to
116256bec294SChris Mason 	 * start sending their work down.
116356bec294SChris Mason 	 */
1164b7ec40d7SChris Mason 	cur_trans->delayed_refs.flushing = 1;
116556bec294SChris Mason 
1166c3e69d58SChris Mason 	ret = btrfs_run_delayed_refs(trans, root, 0);
116756bec294SChris Mason 	BUG_ON(ret);
116856bec294SChris Mason 
1169a4abeea4SJosef Bacik 	spin_lock(&cur_trans->commit_lock);
1170b7ec40d7SChris Mason 	if (cur_trans->in_commit) {
1171a4abeea4SJosef Bacik 		spin_unlock(&cur_trans->commit_lock);
117213c5a93eSJosef Bacik 		atomic_inc(&cur_trans->use_count);
117379154b1bSChris Mason 		btrfs_end_transaction(trans, root);
1174ccd467d6SChris Mason 
1175b9c8300cSLi Zefan 		wait_for_commit(root, cur_trans);
117615ee9bc7SJosef Bacik 
117779154b1bSChris Mason 		put_transaction(cur_trans);
117815ee9bc7SJosef Bacik 
117979154b1bSChris Mason 		return 0;
118079154b1bSChris Mason 	}
11814313b399SChris Mason 
11822c90e5d6SChris Mason 	trans->transaction->in_commit = 1;
1183f9295749SChris Mason 	trans->transaction->blocked = 1;
1184a4abeea4SJosef Bacik 	spin_unlock(&cur_trans->commit_lock);
1185bb9c12c9SSage Weil 	wake_up(&root->fs_info->transaction_blocked_wait);
1186bb9c12c9SSage Weil 
1187a4abeea4SJosef Bacik 	spin_lock(&root->fs_info->trans_lock);
1188ccd467d6SChris Mason 	if (cur_trans->list.prev != &root->fs_info->trans_list) {
1189ccd467d6SChris Mason 		prev_trans = list_entry(cur_trans->list.prev,
1190ccd467d6SChris Mason 					struct btrfs_transaction, list);
1191ccd467d6SChris Mason 		if (!prev_trans->commit_done) {
119213c5a93eSJosef Bacik 			atomic_inc(&prev_trans->use_count);
1193a4abeea4SJosef Bacik 			spin_unlock(&root->fs_info->trans_lock);
1194ccd467d6SChris Mason 
1195ccd467d6SChris Mason 			wait_for_commit(root, prev_trans);
1196ccd467d6SChris Mason 
119715ee9bc7SJosef Bacik 			put_transaction(prev_trans);
1198a4abeea4SJosef Bacik 		} else {
1199a4abeea4SJosef Bacik 			spin_unlock(&root->fs_info->trans_lock);
1200ccd467d6SChris Mason 		}
1201a4abeea4SJosef Bacik 	} else {
1202a4abeea4SJosef Bacik 		spin_unlock(&root->fs_info->trans_lock);
1203ccd467d6SChris Mason 	}
120415ee9bc7SJosef Bacik 
120589573b9cSChris Mason 	if (now < cur_trans->start_time || now - cur_trans->start_time < 1)
120689573b9cSChris Mason 		should_grow = 1;
120789573b9cSChris Mason 
120815ee9bc7SJosef Bacik 	do {
12097ea394f1SYan Zheng 		int snap_pending = 0;
1210a4abeea4SJosef Bacik 
121115ee9bc7SJosef Bacik 		joined = cur_trans->num_joined;
12127ea394f1SYan Zheng 		if (!list_empty(&trans->transaction->pending_snapshots))
12137ea394f1SYan Zheng 			snap_pending = 1;
12147ea394f1SYan Zheng 
12152c90e5d6SChris Mason 		WARN_ON(cur_trans != trans->transaction);
121615ee9bc7SJosef Bacik 
12170bdb1db2SSage Weil 		if (flush_on_commit || snap_pending) {
121824bbcf04SYan, Zheng 			btrfs_start_delalloc_inodes(root, 1);
121924bbcf04SYan, Zheng 			ret = btrfs_wait_ordered_extents(root, 0, 1);
1220ebecd3d9SSage Weil 			BUG_ON(ret);
12217ea394f1SYan Zheng 		}
12227ea394f1SYan Zheng 
122316cdcec7SMiao Xie 		ret = btrfs_run_delayed_items(trans, root);
122416cdcec7SMiao Xie 		BUG_ON(ret);
122516cdcec7SMiao Xie 
12265a3f23d5SChris Mason 		/*
12275a3f23d5SChris Mason 		 * rename don't use btrfs_join_transaction, so, once we
12285a3f23d5SChris Mason 		 * set the transaction to blocked above, we aren't going
12295a3f23d5SChris Mason 		 * to get any new ordered operations.  We can safely run
12305a3f23d5SChris Mason 		 * it here and no for sure that nothing new will be added
12315a3f23d5SChris Mason 		 * to the list
12325a3f23d5SChris Mason 		 */
12335a3f23d5SChris Mason 		btrfs_run_ordered_operations(root, 1);
12345a3f23d5SChris Mason 
1235ed3b3d31SChris Mason 		prepare_to_wait(&cur_trans->writer_wait, &wait,
1236ed3b3d31SChris Mason 				TASK_UNINTERRUPTIBLE);
1237ed3b3d31SChris Mason 
123813c5a93eSJosef Bacik 		if (atomic_read(&cur_trans->num_writers) > 1)
123999d16cbcSSage Weil 			schedule_timeout(MAX_SCHEDULE_TIMEOUT);
124099d16cbcSSage Weil 		else if (should_grow)
124199d16cbcSSage Weil 			schedule_timeout(1);
124215ee9bc7SJosef Bacik 
124315ee9bc7SJosef Bacik 		finish_wait(&cur_trans->writer_wait, &wait);
1244ed0ca140SJosef Bacik 	} while (atomic_read(&cur_trans->num_writers) > 1 ||
1245ed0ca140SJosef Bacik 		 (should_grow && cur_trans->num_joined != joined));
1246ed0ca140SJosef Bacik 
1247ed0ca140SJosef Bacik 	/*
1248ed0ca140SJosef Bacik 	 * Ok now we need to make sure to block out any other joins while we
1249ed0ca140SJosef Bacik 	 * commit the transaction.  We could have started a join before setting
1250ed0ca140SJosef Bacik 	 * no_join so make sure to wait for num_writers to == 1 again.
1251ed0ca140SJosef Bacik 	 */
1252a4abeea4SJosef Bacik 	spin_lock(&root->fs_info->trans_lock);
1253a4abeea4SJosef Bacik 	root->fs_info->trans_no_join = 1;
1254a4abeea4SJosef Bacik 	spin_unlock(&root->fs_info->trans_lock);
1255ed0ca140SJosef Bacik 	wait_event(cur_trans->writer_wait,
1256ed0ca140SJosef Bacik 		   atomic_read(&cur_trans->num_writers) == 1);
125715ee9bc7SJosef Bacik 
12587585717fSChris Mason 	/*
12597585717fSChris Mason 	 * the reloc mutex makes sure that we stop
12607585717fSChris Mason 	 * the balancing code from coming in and moving
12617585717fSChris Mason 	 * extents around in the middle of the commit
12627585717fSChris Mason 	 */
12637585717fSChris Mason 	mutex_lock(&root->fs_info->reloc_mutex);
12647585717fSChris Mason 
1265e999376fSChris Mason 	ret = btrfs_run_delayed_items(trans, root);
12663063d29fSChris Mason 	BUG_ON(ret);
12673063d29fSChris Mason 
1268e999376fSChris Mason 	ret = create_pending_snapshots(trans, root->fs_info);
126916cdcec7SMiao Xie 	BUG_ON(ret);
127016cdcec7SMiao Xie 
127156bec294SChris Mason 	ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
127256bec294SChris Mason 	BUG_ON(ret);
127356bec294SChris Mason 
1274e999376fSChris Mason 	/*
1275e999376fSChris Mason 	 * make sure none of the code above managed to slip in a
1276e999376fSChris Mason 	 * delayed item
1277e999376fSChris Mason 	 */
1278e999376fSChris Mason 	btrfs_assert_delayed_root_empty(root);
1279e999376fSChris Mason 
12802c90e5d6SChris Mason 	WARN_ON(cur_trans != trans->transaction);
1281dc17ff8fSChris Mason 
1282a2de733cSArne Jansen 	btrfs_scrub_pause(root);
1283e02119d5SChris Mason 	/* btrfs_commit_tree_roots is responsible for getting the
1284e02119d5SChris Mason 	 * various roots consistent with each other.  Every pointer
1285e02119d5SChris Mason 	 * in the tree of tree roots has to point to the most up to date
1286e02119d5SChris Mason 	 * root for every subvolume and other tree.  So, we have to keep
1287e02119d5SChris Mason 	 * the tree logging code from jumping in and changing any
1288e02119d5SChris Mason 	 * of the trees.
1289e02119d5SChris Mason 	 *
1290e02119d5SChris Mason 	 * At this point in the commit, there can't be any tree-log
1291e02119d5SChris Mason 	 * writers, but a little lower down we drop the trans mutex
1292e02119d5SChris Mason 	 * and let new people in.  By holding the tree_log_mutex
1293e02119d5SChris Mason 	 * from now until after the super is written, we avoid races
1294e02119d5SChris Mason 	 * with the tree-log code.
1295e02119d5SChris Mason 	 */
1296e02119d5SChris Mason 	mutex_lock(&root->fs_info->tree_log_mutex);
12971a40e23bSZheng Yan 
12985d4f98a2SYan Zheng 	ret = commit_fs_roots(trans, root);
129954aa1f4dSChris Mason 	BUG_ON(ret);
130054aa1f4dSChris Mason 
13015d4f98a2SYan Zheng 	/* commit_fs_roots gets rid of all the tree log roots, it is now
1302e02119d5SChris Mason 	 * safe to free the root of tree log roots
1303e02119d5SChris Mason 	 */
1304e02119d5SChris Mason 	btrfs_free_log_root_tree(trans, root->fs_info);
1305e02119d5SChris Mason 
13065d4f98a2SYan Zheng 	ret = commit_cowonly_roots(trans, root);
130779154b1bSChris Mason 	BUG_ON(ret);
130854aa1f4dSChris Mason 
130911833d66SYan Zheng 	btrfs_prepare_extent_commit(trans, root);
131011833d66SYan Zheng 
131178fae27eSChris Mason 	cur_trans = root->fs_info->running_transaction;
13125f39d397SChris Mason 
13135d4f98a2SYan Zheng 	btrfs_set_root_node(&root->fs_info->tree_root->root_item,
13145d4f98a2SYan Zheng 			    root->fs_info->tree_root->node);
1315817d52f8SJosef Bacik 	switch_commit_root(root->fs_info->tree_root);
13165d4f98a2SYan Zheng 
13175d4f98a2SYan Zheng 	btrfs_set_root_node(&root->fs_info->chunk_root->root_item,
13185d4f98a2SYan Zheng 			    root->fs_info->chunk_root->node);
1319817d52f8SJosef Bacik 	switch_commit_root(root->fs_info->chunk_root);
13205d4f98a2SYan Zheng 
13215d4f98a2SYan Zheng 	update_super_roots(root);
1322e02119d5SChris Mason 
1323e02119d5SChris Mason 	if (!root->fs_info->log_root_recovering) {
13246c41761fSDavid Sterba 		btrfs_set_super_log_root(root->fs_info->super_copy, 0);
13256c41761fSDavid Sterba 		btrfs_set_super_log_root_level(root->fs_info->super_copy, 0);
1326e02119d5SChris Mason 	}
1327e02119d5SChris Mason 
13286c41761fSDavid Sterba 	memcpy(root->fs_info->super_for_commit, root->fs_info->super_copy,
13296c41761fSDavid Sterba 	       sizeof(*root->fs_info->super_copy));
1330ccd467d6SChris Mason 
1331f9295749SChris Mason 	trans->transaction->blocked = 0;
1332a4abeea4SJosef Bacik 	spin_lock(&root->fs_info->trans_lock);
1333a4abeea4SJosef Bacik 	root->fs_info->running_transaction = NULL;
1334a4abeea4SJosef Bacik 	root->fs_info->trans_no_join = 0;
1335a4abeea4SJosef Bacik 	spin_unlock(&root->fs_info->trans_lock);
13367585717fSChris Mason 	mutex_unlock(&root->fs_info->reloc_mutex);
1337b7ec40d7SChris Mason 
1338f9295749SChris Mason 	wake_up(&root->fs_info->transaction_wait);
1339e6dcd2dcSChris Mason 
134079154b1bSChris Mason 	ret = btrfs_write_and_wait_transaction(trans, root);
134179154b1bSChris Mason 	BUG_ON(ret);
1342a512bbf8SYan Zheng 	write_ctree_super(trans, root, 0);
13434313b399SChris Mason 
1344e02119d5SChris Mason 	/*
1345e02119d5SChris Mason 	 * the super is written, we can safely allow the tree-loggers
1346e02119d5SChris Mason 	 * to go about their business
1347e02119d5SChris Mason 	 */
1348e02119d5SChris Mason 	mutex_unlock(&root->fs_info->tree_log_mutex);
1349e02119d5SChris Mason 
135011833d66SYan Zheng 	btrfs_finish_extent_commit(trans, root);
13514313b399SChris Mason 
13522c90e5d6SChris Mason 	cur_trans->commit_done = 1;
1353b7ec40d7SChris Mason 
135415ee9bc7SJosef Bacik 	root->fs_info->last_trans_committed = cur_trans->transid;
1355817d52f8SJosef Bacik 
13562c90e5d6SChris Mason 	wake_up(&cur_trans->commit_wait);
13573de4586cSChris Mason 
1358a4abeea4SJosef Bacik 	spin_lock(&root->fs_info->trans_lock);
135913c5a93eSJosef Bacik 	list_del_init(&cur_trans->list);
1360a4abeea4SJosef Bacik 	spin_unlock(&root->fs_info->trans_lock);
1361a4abeea4SJosef Bacik 
136279154b1bSChris Mason 	put_transaction(cur_trans);
136378fae27eSChris Mason 	put_transaction(cur_trans);
136458176a96SJosef Bacik 
13651abe9b8aSliubo 	trace_btrfs_transaction_commit(root);
13661abe9b8aSliubo 
1367a2de733cSArne Jansen 	btrfs_scrub_continue(root);
1368a2de733cSArne Jansen 
13699ed74f2dSJosef Bacik 	if (current->journal_info == trans)
13709ed74f2dSJosef Bacik 		current->journal_info = NULL;
13719ed74f2dSJosef Bacik 
13722c90e5d6SChris Mason 	kmem_cache_free(btrfs_trans_handle_cachep, trans);
137324bbcf04SYan, Zheng 
137424bbcf04SYan, Zheng 	if (current != root->fs_info->transaction_kthread)
137524bbcf04SYan, Zheng 		btrfs_run_delayed_iputs(root);
137624bbcf04SYan, Zheng 
137779154b1bSChris Mason 	return ret;
137879154b1bSChris Mason }
137979154b1bSChris Mason 
1380d352ac68SChris Mason /*
1381d352ac68SChris Mason  * interface function to delete all the snapshots we have scheduled for deletion
1382d352ac68SChris Mason  */
1383e9d0b13bSChris Mason int btrfs_clean_old_snapshots(struct btrfs_root *root)
1384e9d0b13bSChris Mason {
13855d4f98a2SYan Zheng 	LIST_HEAD(list);
13865d4f98a2SYan Zheng 	struct btrfs_fs_info *fs_info = root->fs_info;
1387e9d0b13bSChris Mason 
1388a4abeea4SJosef Bacik 	spin_lock(&fs_info->trans_lock);
13895d4f98a2SYan Zheng 	list_splice_init(&fs_info->dead_roots, &list);
1390a4abeea4SJosef Bacik 	spin_unlock(&fs_info->trans_lock);
13915d4f98a2SYan Zheng 
13925d4f98a2SYan Zheng 	while (!list_empty(&list)) {
13935d4f98a2SYan Zheng 		root = list_entry(list.next, struct btrfs_root, root_list);
139476dda93cSYan, Zheng 		list_del(&root->root_list);
139576dda93cSYan, Zheng 
139616cdcec7SMiao Xie 		btrfs_kill_all_delayed_nodes(root);
139716cdcec7SMiao Xie 
139876dda93cSYan, Zheng 		if (btrfs_header_backref_rev(root->node) <
139976dda93cSYan, Zheng 		    BTRFS_MIXED_BACKREF_REV)
140066d7e7f0SArne Jansen 			btrfs_drop_snapshot(root, NULL, 0, 0);
140176dda93cSYan, Zheng 		else
140266d7e7f0SArne Jansen 			btrfs_drop_snapshot(root, NULL, 1, 0);
1403e9d0b13bSChris Mason 	}
1404e9d0b13bSChris Mason 	return 0;
1405e9d0b13bSChris Mason }
1406