xref: /openbmc/linux/fs/btrfs/transaction.c (revision 36ba022a)
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));
392c90e5d6SChris Mason 		memset(transaction, 0, sizeof(*transaction));
402c90e5d6SChris Mason 		kmem_cache_free(btrfs_transaction_cachep, transaction);
4179154b1bSChris Mason 	}
4278fae27eSChris Mason }
4379154b1bSChris Mason 
44817d52f8SJosef Bacik static noinline void switch_commit_root(struct btrfs_root *root)
45817d52f8SJosef Bacik {
46817d52f8SJosef Bacik 	free_extent_buffer(root->commit_root);
47817d52f8SJosef Bacik 	root->commit_root = btrfs_root_node(root);
48817d52f8SJosef Bacik }
49817d52f8SJosef Bacik 
50d352ac68SChris Mason /*
51d352ac68SChris Mason  * either allocate a new transaction or hop into the existing one
52d352ac68SChris Mason  */
53a4abeea4SJosef Bacik static noinline int join_transaction(struct btrfs_root *root, int nofail)
5479154b1bSChris Mason {
5579154b1bSChris Mason 	struct btrfs_transaction *cur_trans;
56a4abeea4SJosef Bacik 
57a4abeea4SJosef Bacik 	spin_lock(&root->fs_info->trans_lock);
58a4abeea4SJosef Bacik 	if (root->fs_info->trans_no_join) {
59a4abeea4SJosef Bacik 		if (!nofail) {
60a4abeea4SJosef Bacik 			spin_unlock(&root->fs_info->trans_lock);
61a4abeea4SJosef Bacik 			return -EBUSY;
62a4abeea4SJosef Bacik 		}
63a4abeea4SJosef Bacik 	}
64a4abeea4SJosef Bacik 
6579154b1bSChris Mason 	cur_trans = root->fs_info->running_transaction;
66a4abeea4SJosef Bacik 	if (cur_trans) {
67a4abeea4SJosef Bacik 		atomic_inc(&cur_trans->use_count);
68a4abeea4SJosef Bacik 		atomic_inc(&cur_trans->num_writers);
69a4abeea4SJosef Bacik 		cur_trans->num_joined++;
70a4abeea4SJosef Bacik 		spin_unlock(&root->fs_info->trans_lock);
71a4abeea4SJosef Bacik 		return 0;
72a4abeea4SJosef Bacik 	}
73a4abeea4SJosef Bacik 	spin_unlock(&root->fs_info->trans_lock);
74a4abeea4SJosef Bacik 
75a4abeea4SJosef Bacik 	cur_trans = kmem_cache_alloc(btrfs_transaction_cachep, GFP_NOFS);
76db5b493aSTsutomu Itoh 	if (!cur_trans)
77db5b493aSTsutomu Itoh 		return -ENOMEM;
78a4abeea4SJosef Bacik 	spin_lock(&root->fs_info->trans_lock);
79a4abeea4SJosef Bacik 	if (root->fs_info->running_transaction) {
80a4abeea4SJosef Bacik 		kmem_cache_free(btrfs_transaction_cachep, cur_trans);
81a4abeea4SJosef Bacik 		cur_trans = root->fs_info->running_transaction;
82a4abeea4SJosef Bacik 		atomic_inc(&cur_trans->use_count);
83a4abeea4SJosef Bacik 		atomic_inc(&cur_trans->num_writers);
84a4abeea4SJosef Bacik 		cur_trans->num_joined++;
85a4abeea4SJosef Bacik 		spin_unlock(&root->fs_info->trans_lock);
86a4abeea4SJosef Bacik 		return 0;
87a4abeea4SJosef Bacik 	}
8813c5a93eSJosef Bacik 	atomic_set(&cur_trans->num_writers, 1);
8915ee9bc7SJosef Bacik 	cur_trans->num_joined = 0;
9079154b1bSChris Mason 	init_waitqueue_head(&cur_trans->writer_wait);
9179154b1bSChris Mason 	init_waitqueue_head(&cur_trans->commit_wait);
9279154b1bSChris Mason 	cur_trans->in_commit = 0;
93f9295749SChris Mason 	cur_trans->blocked = 0;
94a4abeea4SJosef Bacik 	/*
95a4abeea4SJosef Bacik 	 * One for this trans handle, one so it will live on until we
96a4abeea4SJosef Bacik 	 * commit the transaction.
97a4abeea4SJosef Bacik 	 */
98a4abeea4SJosef Bacik 	atomic_set(&cur_trans->use_count, 2);
9979154b1bSChris Mason 	cur_trans->commit_done = 0;
10008607c1bSChris Mason 	cur_trans->start_time = get_seconds();
10156bec294SChris Mason 
1026bef4d31SEric Paris 	cur_trans->delayed_refs.root = RB_ROOT;
10356bec294SChris Mason 	cur_trans->delayed_refs.num_entries = 0;
104c3e69d58SChris Mason 	cur_trans->delayed_refs.num_heads_ready = 0;
105c3e69d58SChris Mason 	cur_trans->delayed_refs.num_heads = 0;
10656bec294SChris Mason 	cur_trans->delayed_refs.flushing = 0;
107c3e69d58SChris Mason 	cur_trans->delayed_refs.run_delayed_start = 0;
108a4abeea4SJosef Bacik 	spin_lock_init(&cur_trans->commit_lock);
10956bec294SChris Mason 	spin_lock_init(&cur_trans->delayed_refs.lock);
11056bec294SChris Mason 
1113063d29fSChris Mason 	INIT_LIST_HEAD(&cur_trans->pending_snapshots);
1128fd17795SChris Mason 	list_add_tail(&cur_trans->list, &root->fs_info->trans_list);
113d1310b2eSChris Mason 	extent_io_tree_init(&cur_trans->dirty_pages,
114f993c883SDavid Sterba 			     root->fs_info->btree_inode->i_mapping);
115a4abeea4SJosef Bacik 	root->fs_info->generation++;
116a4abeea4SJosef Bacik 	cur_trans->transid = root->fs_info->generation;
11748ec2cf8SChris Mason 	root->fs_info->running_transaction = cur_trans;
118a4abeea4SJosef Bacik 	spin_unlock(&root->fs_info->trans_lock);
11915ee9bc7SJosef Bacik 
12079154b1bSChris Mason 	return 0;
12179154b1bSChris Mason }
12279154b1bSChris Mason 
123d352ac68SChris Mason /*
124d397712bSChris Mason  * this does all the record keeping required to make sure that a reference
125d397712bSChris Mason  * counted root is properly recorded in a given transaction.  This is required
126d397712bSChris Mason  * to make sure the old root from before we joined the transaction is deleted
127d397712bSChris Mason  * when the transaction commits
128d352ac68SChris Mason  */
1297585717fSChris Mason static int record_root_in_trans(struct btrfs_trans_handle *trans,
1305d4f98a2SYan Zheng 			       struct btrfs_root *root)
1316702ed49SChris Mason {
1325d4f98a2SYan Zheng 	if (root->ref_cows && root->last_trans < trans->transid) {
1336702ed49SChris Mason 		WARN_ON(root == root->fs_info->extent_root);
1345d4f98a2SYan Zheng 		WARN_ON(root->commit_root != root->node);
1355d4f98a2SYan Zheng 
1367585717fSChris Mason 		/*
1377585717fSChris Mason 		 * see below for in_trans_setup usage rules
1387585717fSChris Mason 		 * we have the reloc mutex held now, so there
1397585717fSChris Mason 		 * is only one writer in this function
1407585717fSChris Mason 		 */
1417585717fSChris Mason 		root->in_trans_setup = 1;
1427585717fSChris Mason 
1437585717fSChris Mason 		/* make sure readers find in_trans_setup before
1447585717fSChris Mason 		 * they find our root->last_trans update
1457585717fSChris Mason 		 */
1467585717fSChris Mason 		smp_wmb();
1477585717fSChris Mason 
148a4abeea4SJosef Bacik 		spin_lock(&root->fs_info->fs_roots_radix_lock);
149a4abeea4SJosef Bacik 		if (root->last_trans == trans->transid) {
150a4abeea4SJosef Bacik 			spin_unlock(&root->fs_info->fs_roots_radix_lock);
151a4abeea4SJosef Bacik 			return 0;
152a4abeea4SJosef Bacik 		}
1536702ed49SChris Mason 		radix_tree_tag_set(&root->fs_info->fs_roots_radix,
1546702ed49SChris Mason 			   (unsigned long)root->root_key.objectid,
1556702ed49SChris Mason 			   BTRFS_ROOT_TRANS_TAG);
156a4abeea4SJosef Bacik 		spin_unlock(&root->fs_info->fs_roots_radix_lock);
1577585717fSChris Mason 		root->last_trans = trans->transid;
1587585717fSChris Mason 
1597585717fSChris Mason 		/* this is pretty tricky.  We don't want to
1607585717fSChris Mason 		 * take the relocation lock in btrfs_record_root_in_trans
1617585717fSChris Mason 		 * unless we're really doing the first setup for this root in
1627585717fSChris Mason 		 * this transaction.
1637585717fSChris Mason 		 *
1647585717fSChris Mason 		 * Normally we'd use root->last_trans as a flag to decide
1657585717fSChris Mason 		 * if we want to take the expensive mutex.
1667585717fSChris Mason 		 *
1677585717fSChris Mason 		 * But, we have to set root->last_trans before we
1687585717fSChris Mason 		 * init the relocation root, otherwise, we trip over warnings
1697585717fSChris Mason 		 * in ctree.c.  The solution used here is to flag ourselves
1707585717fSChris Mason 		 * with root->in_trans_setup.  When this is 1, we're still
1717585717fSChris Mason 		 * fixing up the reloc trees and everyone must wait.
1727585717fSChris Mason 		 *
1737585717fSChris Mason 		 * When this is zero, they can trust root->last_trans and fly
1747585717fSChris Mason 		 * through btrfs_record_root_in_trans without having to take the
1757585717fSChris Mason 		 * lock.  smp_wmb() makes sure that all the writes above are
1767585717fSChris Mason 		 * done before we pop in the zero below
1777585717fSChris Mason 		 */
1785d4f98a2SYan Zheng 		btrfs_init_reloc_root(trans, root);
1797585717fSChris Mason 		smp_wmb();
1807585717fSChris Mason 		root->in_trans_setup = 0;
1816702ed49SChris Mason 	}
1825d4f98a2SYan Zheng 	return 0;
1836702ed49SChris Mason }
1845d4f98a2SYan Zheng 
1857585717fSChris Mason 
1867585717fSChris Mason int btrfs_record_root_in_trans(struct btrfs_trans_handle *trans,
1877585717fSChris Mason 			       struct btrfs_root *root)
1887585717fSChris Mason {
1897585717fSChris Mason 	if (!root->ref_cows)
1907585717fSChris Mason 		return 0;
1917585717fSChris Mason 
1927585717fSChris Mason 	/*
1937585717fSChris Mason 	 * see record_root_in_trans for comments about in_trans_setup usage
1947585717fSChris Mason 	 * and barriers
1957585717fSChris Mason 	 */
1967585717fSChris Mason 	smp_rmb();
1977585717fSChris Mason 	if (root->last_trans == trans->transid &&
1987585717fSChris Mason 	    !root->in_trans_setup)
1997585717fSChris Mason 		return 0;
2007585717fSChris Mason 
2017585717fSChris Mason 	mutex_lock(&root->fs_info->reloc_mutex);
2027585717fSChris Mason 	record_root_in_trans(trans, root);
2037585717fSChris Mason 	mutex_unlock(&root->fs_info->reloc_mutex);
2047585717fSChris Mason 
2057585717fSChris Mason 	return 0;
2067585717fSChris Mason }
2077585717fSChris Mason 
208d352ac68SChris Mason /* wait for commit against the current transaction to become unblocked
209d352ac68SChris Mason  * when this is done, it is safe to start a new transaction, but the current
210d352ac68SChris Mason  * transaction might not be fully on disk.
211d352ac68SChris Mason  */
21237d1aeeeSChris Mason static void wait_current_trans(struct btrfs_root *root)
21379154b1bSChris Mason {
214f9295749SChris Mason 	struct btrfs_transaction *cur_trans;
21579154b1bSChris Mason 
216a4abeea4SJosef Bacik 	spin_lock(&root->fs_info->trans_lock);
217f9295749SChris Mason 	cur_trans = root->fs_info->running_transaction;
21837d1aeeeSChris Mason 	if (cur_trans && cur_trans->blocked) {
21913c5a93eSJosef Bacik 		atomic_inc(&cur_trans->use_count);
220a4abeea4SJosef Bacik 		spin_unlock(&root->fs_info->trans_lock);
22172d63ed6SLi Zefan 
22272d63ed6SLi Zefan 		wait_event(root->fs_info->transaction_wait,
22372d63ed6SLi Zefan 			   !cur_trans->blocked);
224f9295749SChris Mason 		put_transaction(cur_trans);
225a4abeea4SJosef Bacik 	} else {
226a4abeea4SJosef Bacik 		spin_unlock(&root->fs_info->trans_lock);
227f9295749SChris Mason 	}
22837d1aeeeSChris Mason }
22937d1aeeeSChris Mason 
230249ac1e5SJosef Bacik enum btrfs_trans_type {
231249ac1e5SJosef Bacik 	TRANS_START,
232249ac1e5SJosef Bacik 	TRANS_JOIN,
233249ac1e5SJosef Bacik 	TRANS_USERSPACE,
2340af3d00bSJosef Bacik 	TRANS_JOIN_NOLOCK,
235249ac1e5SJosef Bacik };
236249ac1e5SJosef Bacik 
237a22285a6SYan, Zheng static int may_wait_transaction(struct btrfs_root *root, int type)
23837d1aeeeSChris Mason {
239a4abeea4SJosef Bacik 	if (root->fs_info->log_root_recovering)
240a4abeea4SJosef Bacik 		return 0;
241a4abeea4SJosef Bacik 
242a4abeea4SJosef Bacik 	if (type == TRANS_USERSPACE)
243a22285a6SYan, Zheng 		return 1;
244a4abeea4SJosef Bacik 
245a4abeea4SJosef Bacik 	if (type == TRANS_START &&
246a4abeea4SJosef Bacik 	    !atomic_read(&root->fs_info->open_ioctl_trans))
247a4abeea4SJosef Bacik 		return 1;
248a4abeea4SJosef Bacik 
249a22285a6SYan, Zheng 	return 0;
250a22285a6SYan, Zheng }
251a22285a6SYan, Zheng 
252a22285a6SYan, Zheng static struct btrfs_trans_handle *start_transaction(struct btrfs_root *root,
253a22285a6SYan, Zheng 						    u64 num_items, int type)
254a22285a6SYan, Zheng {
255a22285a6SYan, Zheng 	struct btrfs_trans_handle *h;
256a22285a6SYan, Zheng 	struct btrfs_transaction *cur_trans;
257b5009945SJosef Bacik 	u64 num_bytes = 0;
258a22285a6SYan, Zheng 	int ret;
259acce952bSliubo 
260acce952bSliubo 	if (root->fs_info->fs_state & BTRFS_SUPER_FLAG_ERROR)
261acce952bSliubo 		return ERR_PTR(-EROFS);
2622a1eb461SJosef Bacik 
2632a1eb461SJosef Bacik 	if (current->journal_info) {
2642a1eb461SJosef Bacik 		WARN_ON(type != TRANS_JOIN && type != TRANS_JOIN_NOLOCK);
2652a1eb461SJosef Bacik 		h = current->journal_info;
2662a1eb461SJosef Bacik 		h->use_count++;
2672a1eb461SJosef Bacik 		h->orig_rsv = h->block_rsv;
2682a1eb461SJosef Bacik 		h->block_rsv = NULL;
2692a1eb461SJosef Bacik 		goto got_it;
2702a1eb461SJosef Bacik 	}
271b5009945SJosef Bacik 
272b5009945SJosef Bacik 	/*
273b5009945SJosef Bacik 	 * Do the reservation before we join the transaction so we can do all
274b5009945SJosef Bacik 	 * the appropriate flushing if need be.
275b5009945SJosef Bacik 	 */
276b5009945SJosef Bacik 	if (num_items > 0 && root != root->fs_info->chunk_root) {
277b5009945SJosef Bacik 		num_bytes = btrfs_calc_trans_metadata_size(root, num_items);
2784a92b1b8SJosef Bacik 		ret = btrfs_block_rsv_add(root,
279b5009945SJosef Bacik 					  &root->fs_info->trans_block_rsv,
280b5009945SJosef Bacik 					  num_bytes);
281b5009945SJosef Bacik 		if (ret)
282b5009945SJosef Bacik 			return ERR_PTR(ret);
283b5009945SJosef Bacik 	}
284a22285a6SYan, Zheng again:
285a22285a6SYan, Zheng 	h = kmem_cache_alloc(btrfs_trans_handle_cachep, GFP_NOFS);
286a22285a6SYan, Zheng 	if (!h)
287a22285a6SYan, Zheng 		return ERR_PTR(-ENOMEM);
288a22285a6SYan, Zheng 
289a22285a6SYan, Zheng 	if (may_wait_transaction(root, type))
29037d1aeeeSChris Mason 		wait_current_trans(root);
291a22285a6SYan, Zheng 
292a4abeea4SJosef Bacik 	do {
293a4abeea4SJosef Bacik 		ret = join_transaction(root, type == TRANS_JOIN_NOLOCK);
294a4abeea4SJosef Bacik 		if (ret == -EBUSY)
295a4abeea4SJosef Bacik 			wait_current_trans(root);
296a4abeea4SJosef Bacik 	} while (ret == -EBUSY);
297a4abeea4SJosef Bacik 
298db5b493aSTsutomu Itoh 	if (ret < 0) {
2996e8df2aeSYoshinori Sano 		kmem_cache_free(btrfs_trans_handle_cachep, h);
300db5b493aSTsutomu Itoh 		return ERR_PTR(ret);
301db5b493aSTsutomu Itoh 	}
3020f7d52f4SChris Mason 
303a22285a6SYan, Zheng 	cur_trans = root->fs_info->running_transaction;
304a22285a6SYan, Zheng 
305a22285a6SYan, Zheng 	h->transid = cur_trans->transid;
306a22285a6SYan, Zheng 	h->transaction = cur_trans;
30779154b1bSChris Mason 	h->blocks_used = 0;
308a22285a6SYan, Zheng 	h->bytes_reserved = 0;
30956bec294SChris Mason 	h->delayed_ref_updates = 0;
3102a1eb461SJosef Bacik 	h->use_count = 1;
311f0486c68SYan, Zheng 	h->block_rsv = NULL;
3122a1eb461SJosef Bacik 	h->orig_rsv = NULL;
313b7ec40d7SChris Mason 
314a22285a6SYan, Zheng 	smp_mb();
315a22285a6SYan, Zheng 	if (cur_trans->blocked && may_wait_transaction(root, type)) {
316a22285a6SYan, Zheng 		btrfs_commit_transaction(h, root);
317a22285a6SYan, Zheng 		goto again;
318a22285a6SYan, Zheng 	}
3199ed74f2dSJosef Bacik 
320b5009945SJosef Bacik 	if (num_bytes) {
321b5009945SJosef Bacik 		h->block_rsv = &root->fs_info->trans_block_rsv;
322b5009945SJosef Bacik 		h->bytes_reserved = num_bytes;
323a22285a6SYan, Zheng 	}
324a22285a6SYan, Zheng 
3252a1eb461SJosef Bacik got_it:
326a4abeea4SJosef Bacik 	btrfs_record_root_in_trans(h, root);
327a22285a6SYan, Zheng 
328a22285a6SYan, Zheng 	if (!current->journal_info && type != TRANS_USERSPACE)
329a22285a6SYan, Zheng 		current->journal_info = h;
33079154b1bSChris Mason 	return h;
33179154b1bSChris Mason }
33279154b1bSChris Mason 
333f9295749SChris Mason struct btrfs_trans_handle *btrfs_start_transaction(struct btrfs_root *root,
334a22285a6SYan, Zheng 						   int num_items)
335f9295749SChris Mason {
336a22285a6SYan, Zheng 	return start_transaction(root, num_items, TRANS_START);
337f9295749SChris Mason }
3387a7eaa40SJosef Bacik struct btrfs_trans_handle *btrfs_join_transaction(struct btrfs_root *root)
339f9295749SChris Mason {
340a22285a6SYan, Zheng 	return start_transaction(root, 0, TRANS_JOIN);
341f9295749SChris Mason }
342f9295749SChris Mason 
3437a7eaa40SJosef Bacik struct btrfs_trans_handle *btrfs_join_transaction_nolock(struct btrfs_root *root)
3440af3d00bSJosef Bacik {
3450af3d00bSJosef Bacik 	return start_transaction(root, 0, TRANS_JOIN_NOLOCK);
3460af3d00bSJosef Bacik }
3470af3d00bSJosef Bacik 
3487a7eaa40SJosef Bacik struct btrfs_trans_handle *btrfs_start_ioctl_transaction(struct btrfs_root *root)
3499ca9ee09SSage Weil {
3507a7eaa40SJosef Bacik 	return start_transaction(root, 0, TRANS_USERSPACE);
3519ca9ee09SSage Weil }
3529ca9ee09SSage Weil 
353d352ac68SChris Mason /* wait for a transaction commit to be fully complete */
354b9c8300cSLi Zefan static noinline void wait_for_commit(struct btrfs_root *root,
35589ce8a63SChris Mason 				    struct btrfs_transaction *commit)
35689ce8a63SChris Mason {
35772d63ed6SLi Zefan 	wait_event(commit->commit_wait, commit->commit_done);
35889ce8a63SChris Mason }
35989ce8a63SChris Mason 
36046204592SSage Weil int btrfs_wait_for_commit(struct btrfs_root *root, u64 transid)
36146204592SSage Weil {
36246204592SSage Weil 	struct btrfs_transaction *cur_trans = NULL, *t;
36346204592SSage Weil 	int ret;
36446204592SSage Weil 
36546204592SSage Weil 	ret = 0;
36646204592SSage Weil 	if (transid) {
36746204592SSage Weil 		if (transid <= root->fs_info->last_trans_committed)
368a4abeea4SJosef Bacik 			goto out;
36946204592SSage Weil 
37046204592SSage Weil 		/* find specified transaction */
371a4abeea4SJosef Bacik 		spin_lock(&root->fs_info->trans_lock);
37246204592SSage Weil 		list_for_each_entry(t, &root->fs_info->trans_list, list) {
37346204592SSage Weil 			if (t->transid == transid) {
37446204592SSage Weil 				cur_trans = t;
375a4abeea4SJosef Bacik 				atomic_inc(&cur_trans->use_count);
37646204592SSage Weil 				break;
37746204592SSage Weil 			}
37846204592SSage Weil 			if (t->transid > transid)
37946204592SSage Weil 				break;
38046204592SSage Weil 		}
381a4abeea4SJosef Bacik 		spin_unlock(&root->fs_info->trans_lock);
38246204592SSage Weil 		ret = -EINVAL;
38346204592SSage Weil 		if (!cur_trans)
384a4abeea4SJosef Bacik 			goto out;  /* bad transid */
38546204592SSage Weil 	} else {
38646204592SSage Weil 		/* find newest transaction that is committing | committed */
387a4abeea4SJosef Bacik 		spin_lock(&root->fs_info->trans_lock);
38846204592SSage Weil 		list_for_each_entry_reverse(t, &root->fs_info->trans_list,
38946204592SSage Weil 					    list) {
39046204592SSage Weil 			if (t->in_commit) {
39146204592SSage Weil 				if (t->commit_done)
3923473f3c0SJosef Bacik 					break;
39346204592SSage Weil 				cur_trans = t;
394a4abeea4SJosef Bacik 				atomic_inc(&cur_trans->use_count);
39546204592SSage Weil 				break;
39646204592SSage Weil 			}
39746204592SSage Weil 		}
398a4abeea4SJosef Bacik 		spin_unlock(&root->fs_info->trans_lock);
39946204592SSage Weil 		if (!cur_trans)
400a4abeea4SJosef Bacik 			goto out;  /* nothing committing|committed */
40146204592SSage Weil 	}
40246204592SSage Weil 
40346204592SSage Weil 	wait_for_commit(root, cur_trans);
40446204592SSage Weil 
40546204592SSage Weil 	put_transaction(cur_trans);
40646204592SSage Weil 	ret = 0;
407a4abeea4SJosef Bacik out:
40846204592SSage Weil 	return ret;
40946204592SSage Weil }
41046204592SSage Weil 
41137d1aeeeSChris Mason void btrfs_throttle(struct btrfs_root *root)
41237d1aeeeSChris Mason {
413a4abeea4SJosef Bacik 	if (!atomic_read(&root->fs_info->open_ioctl_trans))
41437d1aeeeSChris Mason 		wait_current_trans(root);
41537d1aeeeSChris Mason }
41637d1aeeeSChris Mason 
4178929ecfaSYan, Zheng static int should_end_transaction(struct btrfs_trans_handle *trans,
4188929ecfaSYan, Zheng 				  struct btrfs_root *root)
4198929ecfaSYan, Zheng {
4208929ecfaSYan, Zheng 	int ret;
42136ba022aSJosef Bacik 
42236ba022aSJosef Bacik 	ret = btrfs_block_rsv_check(root, &root->fs_info->global_block_rsv, 5);
4238929ecfaSYan, Zheng 	return ret ? 1 : 0;
4248929ecfaSYan, Zheng }
4258929ecfaSYan, Zheng 
4268929ecfaSYan, Zheng int btrfs_should_end_transaction(struct btrfs_trans_handle *trans,
4278929ecfaSYan, Zheng 				 struct btrfs_root *root)
4288929ecfaSYan, Zheng {
4298929ecfaSYan, Zheng 	struct btrfs_transaction *cur_trans = trans->transaction;
4309c8d86dbSJosef Bacik 	struct btrfs_block_rsv *rsv = trans->block_rsv;
4318929ecfaSYan, Zheng 	int updates;
4328929ecfaSYan, Zheng 
433a4abeea4SJosef Bacik 	smp_mb();
4348929ecfaSYan, Zheng 	if (cur_trans->blocked || cur_trans->delayed_refs.flushing)
4358929ecfaSYan, Zheng 		return 1;
4368929ecfaSYan, Zheng 
4379c8d86dbSJosef Bacik 	/*
4389c8d86dbSJosef Bacik 	 * We need to do this in case we're deleting csums so the global block
4399c8d86dbSJosef Bacik 	 * rsv get's used instead of the csum block rsv.
4409c8d86dbSJosef Bacik 	 */
4419c8d86dbSJosef Bacik 	trans->block_rsv = NULL;
4429c8d86dbSJosef Bacik 
4438929ecfaSYan, Zheng 	updates = trans->delayed_ref_updates;
4448929ecfaSYan, Zheng 	trans->delayed_ref_updates = 0;
4458929ecfaSYan, Zheng 	if (updates)
4468929ecfaSYan, Zheng 		btrfs_run_delayed_refs(trans, root, updates);
4478929ecfaSYan, Zheng 
4489c8d86dbSJosef Bacik 	trans->block_rsv = rsv;
4499c8d86dbSJosef Bacik 
4508929ecfaSYan, Zheng 	return should_end_transaction(trans, root);
4518929ecfaSYan, Zheng }
4528929ecfaSYan, Zheng 
45389ce8a63SChris Mason static int __btrfs_end_transaction(struct btrfs_trans_handle *trans,
4540af3d00bSJosef Bacik 			  struct btrfs_root *root, int throttle, int lock)
45579154b1bSChris Mason {
4568929ecfaSYan, Zheng 	struct btrfs_transaction *cur_trans = trans->transaction;
457ab78c84dSChris Mason 	struct btrfs_fs_info *info = root->fs_info;
458c3e69d58SChris Mason 	int count = 0;
459d6e4a428SChris Mason 
4602a1eb461SJosef Bacik 	if (--trans->use_count) {
4612a1eb461SJosef Bacik 		trans->block_rsv = trans->orig_rsv;
4622a1eb461SJosef Bacik 		return 0;
4632a1eb461SJosef Bacik 	}
4642a1eb461SJosef Bacik 
465b24e03dbSJosef Bacik 	btrfs_trans_release_metadata(trans, root);
4664c13d758SJosef Bacik 	trans->block_rsv = NULL;
467c3e69d58SChris Mason 	while (count < 4) {
468c3e69d58SChris Mason 		unsigned long cur = trans->delayed_ref_updates;
469c3e69d58SChris Mason 		trans->delayed_ref_updates = 0;
470c3e69d58SChris Mason 		if (cur &&
471c3e69d58SChris Mason 		    trans->transaction->delayed_refs.num_heads_ready > 64) {
472c3e69d58SChris Mason 			trans->delayed_ref_updates = 0;
473b7ec40d7SChris Mason 
474b7ec40d7SChris Mason 			/*
475b7ec40d7SChris Mason 			 * do a full flush if the transaction is trying
476b7ec40d7SChris Mason 			 * to close
477b7ec40d7SChris Mason 			 */
478b7ec40d7SChris Mason 			if (trans->transaction->delayed_refs.flushing)
479b7ec40d7SChris Mason 				cur = 0;
480c3e69d58SChris Mason 			btrfs_run_delayed_refs(trans, root, cur);
481c3e69d58SChris Mason 		} else {
482c3e69d58SChris Mason 			break;
483c3e69d58SChris Mason 		}
484c3e69d58SChris Mason 		count++;
48556bec294SChris Mason 	}
48656bec294SChris Mason 
487a4abeea4SJosef Bacik 	if (lock && !atomic_read(&root->fs_info->open_ioctl_trans) &&
488a4abeea4SJosef Bacik 	    should_end_transaction(trans, root)) {
4898929ecfaSYan, Zheng 		trans->transaction->blocked = 1;
490a4abeea4SJosef Bacik 		smp_wmb();
491a4abeea4SJosef Bacik 	}
4928929ecfaSYan, Zheng 
4930af3d00bSJosef Bacik 	if (lock && cur_trans->blocked && !cur_trans->in_commit) {
49481317fdeSJosef Bacik 		if (throttle) {
49581317fdeSJosef Bacik 			/*
49681317fdeSJosef Bacik 			 * We may race with somebody else here so end up having
49781317fdeSJosef Bacik 			 * to call end_transaction on ourselves again, so inc
49881317fdeSJosef Bacik 			 * our use_count.
49981317fdeSJosef Bacik 			 */
50081317fdeSJosef Bacik 			trans->use_count++;
5018929ecfaSYan, Zheng 			return btrfs_commit_transaction(trans, root);
50281317fdeSJosef Bacik 		} else {
5038929ecfaSYan, Zheng 			wake_up_process(info->transaction_kthread);
5048929ecfaSYan, Zheng 		}
50581317fdeSJosef Bacik 	}
5068929ecfaSYan, Zheng 
5078929ecfaSYan, Zheng 	WARN_ON(cur_trans != info->running_transaction);
50813c5a93eSJosef Bacik 	WARN_ON(atomic_read(&cur_trans->num_writers) < 1);
50913c5a93eSJosef Bacik 	atomic_dec(&cur_trans->num_writers);
51089ce8a63SChris Mason 
51199d16cbcSSage Weil 	smp_mb();
51279154b1bSChris Mason 	if (waitqueue_active(&cur_trans->writer_wait))
51379154b1bSChris Mason 		wake_up(&cur_trans->writer_wait);
51479154b1bSChris Mason 	put_transaction(cur_trans);
5159ed74f2dSJosef Bacik 
5169ed74f2dSJosef Bacik 	if (current->journal_info == trans)
5179ed74f2dSJosef Bacik 		current->journal_info = NULL;
518d6025579SChris Mason 	memset(trans, 0, sizeof(*trans));
5192c90e5d6SChris Mason 	kmem_cache_free(btrfs_trans_handle_cachep, trans);
520ab78c84dSChris Mason 
52124bbcf04SYan, Zheng 	if (throttle)
52224bbcf04SYan, Zheng 		btrfs_run_delayed_iputs(root);
52324bbcf04SYan, Zheng 
52479154b1bSChris Mason 	return 0;
52579154b1bSChris Mason }
52679154b1bSChris Mason 
52789ce8a63SChris Mason int btrfs_end_transaction(struct btrfs_trans_handle *trans,
52889ce8a63SChris Mason 			  struct btrfs_root *root)
52989ce8a63SChris Mason {
53016cdcec7SMiao Xie 	int ret;
53116cdcec7SMiao Xie 
53216cdcec7SMiao Xie 	ret = __btrfs_end_transaction(trans, root, 0, 1);
53316cdcec7SMiao Xie 	if (ret)
53416cdcec7SMiao Xie 		return ret;
53516cdcec7SMiao Xie 	return 0;
53689ce8a63SChris Mason }
53789ce8a63SChris Mason 
53889ce8a63SChris Mason int btrfs_end_transaction_throttle(struct btrfs_trans_handle *trans,
53989ce8a63SChris Mason 				   struct btrfs_root *root)
54089ce8a63SChris Mason {
54116cdcec7SMiao Xie 	int ret;
54216cdcec7SMiao Xie 
54316cdcec7SMiao Xie 	ret = __btrfs_end_transaction(trans, root, 1, 1);
54416cdcec7SMiao Xie 	if (ret)
54516cdcec7SMiao Xie 		return ret;
54616cdcec7SMiao Xie 	return 0;
5470af3d00bSJosef Bacik }
5480af3d00bSJosef Bacik 
5490af3d00bSJosef Bacik int btrfs_end_transaction_nolock(struct btrfs_trans_handle *trans,
5500af3d00bSJosef Bacik 				 struct btrfs_root *root)
5510af3d00bSJosef Bacik {
55216cdcec7SMiao Xie 	int ret;
55316cdcec7SMiao Xie 
55416cdcec7SMiao Xie 	ret = __btrfs_end_transaction(trans, root, 0, 0);
55516cdcec7SMiao Xie 	if (ret)
55616cdcec7SMiao Xie 		return ret;
55716cdcec7SMiao Xie 	return 0;
55816cdcec7SMiao Xie }
55916cdcec7SMiao Xie 
56016cdcec7SMiao Xie int btrfs_end_transaction_dmeta(struct btrfs_trans_handle *trans,
56116cdcec7SMiao Xie 				struct btrfs_root *root)
56216cdcec7SMiao Xie {
56316cdcec7SMiao Xie 	return __btrfs_end_transaction(trans, root, 1, 1);
56489ce8a63SChris Mason }
56589ce8a63SChris Mason 
566d352ac68SChris Mason /*
567d352ac68SChris Mason  * when btree blocks are allocated, they have some corresponding bits set for
568d352ac68SChris Mason  * them in one of two extent_io trees.  This is used to make sure all of
569690587d1SChris Mason  * those extents are sent to disk but does not wait on them
570d352ac68SChris Mason  */
571690587d1SChris Mason int btrfs_write_marked_extents(struct btrfs_root *root,
5728cef4e16SYan, Zheng 			       struct extent_io_tree *dirty_pages, int mark)
57379154b1bSChris Mason {
574777e6bd7SChris Mason 	int err = 0;
5757c4452b9SChris Mason 	int werr = 0;
5761728366eSJosef Bacik 	struct address_space *mapping = root->fs_info->btree_inode->i_mapping;
577777e6bd7SChris Mason 	u64 start = 0;
5785f39d397SChris Mason 	u64 end;
5797c4452b9SChris Mason 
5801728366eSJosef Bacik 	while (!find_first_extent_bit(dirty_pages, start, &start, &end,
5811728366eSJosef Bacik 				      mark)) {
5821728366eSJosef Bacik 		convert_extent_bit(dirty_pages, start, end, EXTENT_NEED_WAIT, mark,
5831728366eSJosef Bacik 				   GFP_NOFS);
5841728366eSJosef Bacik 		err = filemap_fdatawrite_range(mapping, start, end);
5857c4452b9SChris Mason 		if (err)
5867c4452b9SChris Mason 			werr = err;
5871728366eSJosef Bacik 		cond_resched();
5881728366eSJosef Bacik 		start = end + 1;
5897c4452b9SChris Mason 	}
590690587d1SChris Mason 	if (err)
591690587d1SChris Mason 		werr = err;
592690587d1SChris Mason 	return werr;
593690587d1SChris Mason }
594690587d1SChris Mason 
595690587d1SChris Mason /*
596690587d1SChris Mason  * when btree blocks are allocated, they have some corresponding bits set for
597690587d1SChris Mason  * them in one of two extent_io trees.  This is used to make sure all of
598690587d1SChris Mason  * those extents are on disk for transaction or log commit.  We wait
599690587d1SChris Mason  * on all the pages and clear them from the dirty pages state tree
600690587d1SChris Mason  */
601690587d1SChris Mason int btrfs_wait_marked_extents(struct btrfs_root *root,
6028cef4e16SYan, Zheng 			      struct extent_io_tree *dirty_pages, int mark)
603690587d1SChris Mason {
604690587d1SChris Mason 	int err = 0;
605690587d1SChris Mason 	int werr = 0;
6061728366eSJosef Bacik 	struct address_space *mapping = root->fs_info->btree_inode->i_mapping;
607690587d1SChris Mason 	u64 start = 0;
608690587d1SChris Mason 	u64 end;
609690587d1SChris Mason 
6101728366eSJosef Bacik 	while (!find_first_extent_bit(dirty_pages, start, &start, &end,
6111728366eSJosef Bacik 				      EXTENT_NEED_WAIT)) {
6121728366eSJosef Bacik 		clear_extent_bits(dirty_pages, start, end, EXTENT_NEED_WAIT, GFP_NOFS);
6131728366eSJosef Bacik 		err = filemap_fdatawait_range(mapping, start, end);
614777e6bd7SChris Mason 		if (err)
615777e6bd7SChris Mason 			werr = err;
616777e6bd7SChris Mason 		cond_resched();
6171728366eSJosef Bacik 		start = end + 1;
618777e6bd7SChris Mason 	}
6197c4452b9SChris Mason 	if (err)
6207c4452b9SChris Mason 		werr = err;
6217c4452b9SChris Mason 	return werr;
62279154b1bSChris Mason }
62379154b1bSChris Mason 
624690587d1SChris Mason /*
625690587d1SChris Mason  * when btree blocks are allocated, they have some corresponding bits set for
626690587d1SChris Mason  * them in one of two extent_io trees.  This is used to make sure all of
627690587d1SChris Mason  * those extents are on disk for transaction or log commit
628690587d1SChris Mason  */
629690587d1SChris Mason int btrfs_write_and_wait_marked_extents(struct btrfs_root *root,
6308cef4e16SYan, Zheng 				struct extent_io_tree *dirty_pages, int mark)
631690587d1SChris Mason {
632690587d1SChris Mason 	int ret;
633690587d1SChris Mason 	int ret2;
634690587d1SChris Mason 
6358cef4e16SYan, Zheng 	ret = btrfs_write_marked_extents(root, dirty_pages, mark);
6368cef4e16SYan, Zheng 	ret2 = btrfs_wait_marked_extents(root, dirty_pages, mark);
637690587d1SChris Mason 	return ret || ret2;
638690587d1SChris Mason }
639690587d1SChris Mason 
640d0c803c4SChris Mason int btrfs_write_and_wait_transaction(struct btrfs_trans_handle *trans,
641d0c803c4SChris Mason 				     struct btrfs_root *root)
642d0c803c4SChris Mason {
643d0c803c4SChris Mason 	if (!trans || !trans->transaction) {
644d0c803c4SChris Mason 		struct inode *btree_inode;
645d0c803c4SChris Mason 		btree_inode = root->fs_info->btree_inode;
646d0c803c4SChris Mason 		return filemap_write_and_wait(btree_inode->i_mapping);
647d0c803c4SChris Mason 	}
648d0c803c4SChris Mason 	return btrfs_write_and_wait_marked_extents(root,
6498cef4e16SYan, Zheng 					   &trans->transaction->dirty_pages,
6508cef4e16SYan, Zheng 					   EXTENT_DIRTY);
651d0c803c4SChris Mason }
652d0c803c4SChris Mason 
653d352ac68SChris Mason /*
654d352ac68SChris Mason  * this is used to update the root pointer in the tree of tree roots.
655d352ac68SChris Mason  *
656d352ac68SChris Mason  * But, in the case of the extent allocation tree, updating the root
657d352ac68SChris Mason  * pointer may allocate blocks which may change the root of the extent
658d352ac68SChris Mason  * allocation tree.
659d352ac68SChris Mason  *
660d352ac68SChris Mason  * So, this loops and repeats and makes sure the cowonly root didn't
661d352ac68SChris Mason  * change while the root pointer was being updated in the metadata.
662d352ac68SChris Mason  */
6630b86a832SChris Mason static int update_cowonly_root(struct btrfs_trans_handle *trans,
66479154b1bSChris Mason 			       struct btrfs_root *root)
66579154b1bSChris Mason {
66679154b1bSChris Mason 	int ret;
6670b86a832SChris Mason 	u64 old_root_bytenr;
66886b9f2ecSYan, Zheng 	u64 old_root_used;
6690b86a832SChris Mason 	struct btrfs_root *tree_root = root->fs_info->tree_root;
67079154b1bSChris Mason 
67186b9f2ecSYan, Zheng 	old_root_used = btrfs_root_used(&root->root_item);
6720b86a832SChris Mason 	btrfs_write_dirty_block_groups(trans, root);
67356bec294SChris Mason 
67479154b1bSChris Mason 	while (1) {
6750b86a832SChris Mason 		old_root_bytenr = btrfs_root_bytenr(&root->root_item);
67686b9f2ecSYan, Zheng 		if (old_root_bytenr == root->node->start &&
67786b9f2ecSYan, Zheng 		    old_root_used == btrfs_root_used(&root->root_item))
67879154b1bSChris Mason 			break;
67987ef2bb4SChris Mason 
6805d4f98a2SYan Zheng 		btrfs_set_root_node(&root->root_item, root->node);
68179154b1bSChris Mason 		ret = btrfs_update_root(trans, tree_root,
6820b86a832SChris Mason 					&root->root_key,
6830b86a832SChris Mason 					&root->root_item);
68479154b1bSChris Mason 		BUG_ON(ret);
68556bec294SChris Mason 
68686b9f2ecSYan, Zheng 		old_root_used = btrfs_root_used(&root->root_item);
6874a8c9a62SYan Zheng 		ret = btrfs_write_dirty_block_groups(trans, root);
68856bec294SChris Mason 		BUG_ON(ret);
6890b86a832SChris Mason 	}
690276e680dSYan Zheng 
691276e680dSYan Zheng 	if (root != root->fs_info->extent_root)
692817d52f8SJosef Bacik 		switch_commit_root(root);
693276e680dSYan Zheng 
6940b86a832SChris Mason 	return 0;
6950b86a832SChris Mason }
6960b86a832SChris Mason 
697d352ac68SChris Mason /*
698d352ac68SChris Mason  * update all the cowonly tree roots on disk
699d352ac68SChris Mason  */
7005d4f98a2SYan Zheng static noinline int commit_cowonly_roots(struct btrfs_trans_handle *trans,
7010b86a832SChris Mason 					 struct btrfs_root *root)
7020b86a832SChris Mason {
7030b86a832SChris Mason 	struct btrfs_fs_info *fs_info = root->fs_info;
7040b86a832SChris Mason 	struct list_head *next;
70584234f3aSYan Zheng 	struct extent_buffer *eb;
70656bec294SChris Mason 	int ret;
70784234f3aSYan Zheng 
70856bec294SChris Mason 	ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
70956bec294SChris Mason 	BUG_ON(ret);
71087ef2bb4SChris Mason 
71184234f3aSYan Zheng 	eb = btrfs_lock_root_node(fs_info->tree_root);
7129fa8cfe7SChris Mason 	btrfs_cow_block(trans, fs_info->tree_root, eb, NULL, 0, &eb);
71384234f3aSYan Zheng 	btrfs_tree_unlock(eb);
71484234f3aSYan Zheng 	free_extent_buffer(eb);
7150b86a832SChris Mason 
71656bec294SChris Mason 	ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
71756bec294SChris Mason 	BUG_ON(ret);
71887ef2bb4SChris Mason 
7190b86a832SChris Mason 	while (!list_empty(&fs_info->dirty_cowonly_roots)) {
7200b86a832SChris Mason 		next = fs_info->dirty_cowonly_roots.next;
7210b86a832SChris Mason 		list_del_init(next);
7220b86a832SChris Mason 		root = list_entry(next, struct btrfs_root, dirty_list);
72387ef2bb4SChris Mason 
7240b86a832SChris Mason 		update_cowonly_root(trans, root);
72579154b1bSChris Mason 	}
726276e680dSYan Zheng 
727276e680dSYan Zheng 	down_write(&fs_info->extent_commit_sem);
728276e680dSYan Zheng 	switch_commit_root(fs_info->extent_root);
729276e680dSYan Zheng 	up_write(&fs_info->extent_commit_sem);
730276e680dSYan Zheng 
73179154b1bSChris Mason 	return 0;
73279154b1bSChris Mason }
73379154b1bSChris Mason 
734d352ac68SChris Mason /*
735d352ac68SChris Mason  * dead roots are old snapshots that need to be deleted.  This allocates
736d352ac68SChris Mason  * a dirty root struct and adds it into the list of dead roots that need to
737d352ac68SChris Mason  * be deleted
738d352ac68SChris Mason  */
7395d4f98a2SYan Zheng int btrfs_add_dead_root(struct btrfs_root *root)
7405eda7b5eSChris Mason {
741a4abeea4SJosef Bacik 	spin_lock(&root->fs_info->trans_lock);
7425d4f98a2SYan Zheng 	list_add(&root->root_list, &root->fs_info->dead_roots);
743a4abeea4SJosef Bacik 	spin_unlock(&root->fs_info->trans_lock);
7445eda7b5eSChris Mason 	return 0;
7455eda7b5eSChris Mason }
7465eda7b5eSChris Mason 
747d352ac68SChris Mason /*
7485d4f98a2SYan Zheng  * update all the cowonly tree roots on disk
749d352ac68SChris Mason  */
7505d4f98a2SYan Zheng static noinline int commit_fs_roots(struct btrfs_trans_handle *trans,
7515d4f98a2SYan Zheng 				    struct btrfs_root *root)
7520f7d52f4SChris Mason {
7530f7d52f4SChris Mason 	struct btrfs_root *gang[8];
7545d4f98a2SYan Zheng 	struct btrfs_fs_info *fs_info = root->fs_info;
7550f7d52f4SChris Mason 	int i;
7560f7d52f4SChris Mason 	int ret;
75754aa1f4dSChris Mason 	int err = 0;
75854aa1f4dSChris Mason 
759a4abeea4SJosef Bacik 	spin_lock(&fs_info->fs_roots_radix_lock);
7600f7d52f4SChris Mason 	while (1) {
7615d4f98a2SYan Zheng 		ret = radix_tree_gang_lookup_tag(&fs_info->fs_roots_radix,
7625d4f98a2SYan Zheng 						 (void **)gang, 0,
7630f7d52f4SChris Mason 						 ARRAY_SIZE(gang),
7640f7d52f4SChris Mason 						 BTRFS_ROOT_TRANS_TAG);
7650f7d52f4SChris Mason 		if (ret == 0)
7660f7d52f4SChris Mason 			break;
7670f7d52f4SChris Mason 		for (i = 0; i < ret; i++) {
7680f7d52f4SChris Mason 			root = gang[i];
7695d4f98a2SYan Zheng 			radix_tree_tag_clear(&fs_info->fs_roots_radix,
7702619ba1fSChris Mason 					(unsigned long)root->root_key.objectid,
7710f7d52f4SChris Mason 					BTRFS_ROOT_TRANS_TAG);
772a4abeea4SJosef Bacik 			spin_unlock(&fs_info->fs_roots_radix_lock);
77331153d81SYan Zheng 
774e02119d5SChris Mason 			btrfs_free_log(trans, root);
7755d4f98a2SYan Zheng 			btrfs_update_reloc_root(trans, root);
776d68fc57bSYan, Zheng 			btrfs_orphan_commit_root(trans, root);
777e02119d5SChris Mason 
77882d5902dSLi Zefan 			btrfs_save_ino_cache(root, trans);
77982d5902dSLi Zefan 
780978d910dSYan Zheng 			if (root->commit_root != root->node) {
781581bb050SLi Zefan 				mutex_lock(&root->fs_commit_mutex);
782817d52f8SJosef Bacik 				switch_commit_root(root);
783581bb050SLi Zefan 				btrfs_unpin_free_ino(root);
784581bb050SLi Zefan 				mutex_unlock(&root->fs_commit_mutex);
785581bb050SLi Zefan 
786978d910dSYan Zheng 				btrfs_set_root_node(&root->root_item,
787978d910dSYan Zheng 						    root->node);
788978d910dSYan Zheng 			}
78931153d81SYan Zheng 
7905d4f98a2SYan Zheng 			err = btrfs_update_root(trans, fs_info->tree_root,
7910f7d52f4SChris Mason 						&root->root_key,
7920f7d52f4SChris Mason 						&root->root_item);
793a4abeea4SJosef Bacik 			spin_lock(&fs_info->fs_roots_radix_lock);
79454aa1f4dSChris Mason 			if (err)
79554aa1f4dSChris Mason 				break;
7960f7d52f4SChris Mason 		}
7979f3a7427SChris Mason 	}
798a4abeea4SJosef Bacik 	spin_unlock(&fs_info->fs_roots_radix_lock);
79954aa1f4dSChris Mason 	return err;
8000f7d52f4SChris Mason }
8010f7d52f4SChris Mason 
802d352ac68SChris Mason /*
803d352ac68SChris Mason  * defrag a given btree.  If cacheonly == 1, this won't read from the disk,
804d352ac68SChris Mason  * otherwise every leaf in the btree is read and defragged.
805d352ac68SChris Mason  */
806e9d0b13bSChris Mason int btrfs_defrag_root(struct btrfs_root *root, int cacheonly)
807e9d0b13bSChris Mason {
808e9d0b13bSChris Mason 	struct btrfs_fs_info *info = root->fs_info;
809e9d0b13bSChris Mason 	struct btrfs_trans_handle *trans;
8108929ecfaSYan, Zheng 	int ret;
811d3c2fdcfSChris Mason 	unsigned long nr;
812e9d0b13bSChris Mason 
8138929ecfaSYan, Zheng 	if (xchg(&root->defrag_running, 1))
814e9d0b13bSChris Mason 		return 0;
8158929ecfaSYan, Zheng 
8166b80053dSChris Mason 	while (1) {
8178929ecfaSYan, Zheng 		trans = btrfs_start_transaction(root, 0);
8188929ecfaSYan, Zheng 		if (IS_ERR(trans))
8198929ecfaSYan, Zheng 			return PTR_ERR(trans);
8208929ecfaSYan, Zheng 
821e9d0b13bSChris Mason 		ret = btrfs_defrag_leaves(trans, root, cacheonly);
8228929ecfaSYan, Zheng 
823d3c2fdcfSChris Mason 		nr = trans->blocks_used;
824e9d0b13bSChris Mason 		btrfs_end_transaction(trans, root);
825d3c2fdcfSChris Mason 		btrfs_btree_balance_dirty(info->tree_root, nr);
826e9d0b13bSChris Mason 		cond_resched();
827e9d0b13bSChris Mason 
8287841cb28SDavid Sterba 		if (btrfs_fs_closing(root->fs_info) || ret != -EAGAIN)
829e9d0b13bSChris Mason 			break;
830e9d0b13bSChris Mason 	}
831e9d0b13bSChris Mason 	root->defrag_running = 0;
8328929ecfaSYan, Zheng 	return ret;
833e9d0b13bSChris Mason }
834e9d0b13bSChris Mason 
835d352ac68SChris Mason /*
836d352ac68SChris Mason  * new snapshots need to be created at a very specific time in the
837d352ac68SChris Mason  * transaction commit.  This does the actual creation
838d352ac68SChris Mason  */
83980b6794dSChris Mason static noinline int create_pending_snapshot(struct btrfs_trans_handle *trans,
8403063d29fSChris Mason 				   struct btrfs_fs_info *fs_info,
8413063d29fSChris Mason 				   struct btrfs_pending_snapshot *pending)
8423063d29fSChris Mason {
8433063d29fSChris Mason 	struct btrfs_key key;
84480b6794dSChris Mason 	struct btrfs_root_item *new_root_item;
8453063d29fSChris Mason 	struct btrfs_root *tree_root = fs_info->tree_root;
8463063d29fSChris Mason 	struct btrfs_root *root = pending->root;
8476bdb72deSSage Weil 	struct btrfs_root *parent_root;
84898c9942aSLiu Bo 	struct btrfs_block_rsv *rsv;
8496bdb72deSSage Weil 	struct inode *parent_inode;
8506a912213SJosef Bacik 	struct dentry *parent;
851a22285a6SYan, Zheng 	struct dentry *dentry;
8523063d29fSChris Mason 	struct extent_buffer *tmp;
853925baeddSChris Mason 	struct extent_buffer *old;
8543063d29fSChris Mason 	int ret;
855d68fc57bSYan, Zheng 	u64 to_reserve = 0;
8566bdb72deSSage Weil 	u64 index = 0;
857a22285a6SYan, Zheng 	u64 objectid;
858b83cc969SLi Zefan 	u64 root_flags;
8593063d29fSChris Mason 
86098c9942aSLiu Bo 	rsv = trans->block_rsv;
86198c9942aSLiu Bo 
86280b6794dSChris Mason 	new_root_item = kmalloc(sizeof(*new_root_item), GFP_NOFS);
86380b6794dSChris Mason 	if (!new_root_item) {
864a22285a6SYan, Zheng 		pending->error = -ENOMEM;
86580b6794dSChris Mason 		goto fail;
86680b6794dSChris Mason 	}
867a22285a6SYan, Zheng 
868581bb050SLi Zefan 	ret = btrfs_find_free_objectid(tree_root, &objectid);
869a22285a6SYan, Zheng 	if (ret) {
870a22285a6SYan, Zheng 		pending->error = ret;
8713063d29fSChris Mason 		goto fail;
872a22285a6SYan, Zheng 	}
8733063d29fSChris Mason 
8743fd0a558SYan, Zheng 	btrfs_reloc_pre_snapshot(trans, pending, &to_reserve);
875d68fc57bSYan, Zheng 
876d68fc57bSYan, Zheng 	if (to_reserve > 0) {
8774a92b1b8SJosef Bacik 		ret = btrfs_block_rsv_add(root, &pending->block_rsv,
8788bb8ab2eSJosef Bacik 					  to_reserve);
879d68fc57bSYan, Zheng 		if (ret) {
880d68fc57bSYan, Zheng 			pending->error = ret;
881d68fc57bSYan, Zheng 			goto fail;
882d68fc57bSYan, Zheng 		}
883d68fc57bSYan, Zheng 	}
884d68fc57bSYan, Zheng 
8853063d29fSChris Mason 	key.objectid = objectid;
886a22285a6SYan, Zheng 	key.offset = (u64)-1;
887a22285a6SYan, Zheng 	key.type = BTRFS_ROOT_ITEM_KEY;
8883063d29fSChris Mason 
889a22285a6SYan, Zheng 	trans->block_rsv = &pending->block_rsv;
8906bdb72deSSage Weil 
891a22285a6SYan, Zheng 	dentry = pending->dentry;
8926a912213SJosef Bacik 	parent = dget_parent(dentry);
8936a912213SJosef Bacik 	parent_inode = parent->d_inode;
894a22285a6SYan, Zheng 	parent_root = BTRFS_I(parent_inode)->root;
8957585717fSChris Mason 	record_root_in_trans(trans, parent_root);
896a22285a6SYan, Zheng 
8976bdb72deSSage Weil 	/*
8986bdb72deSSage Weil 	 * insert the directory item
8996bdb72deSSage Weil 	 */
9006bdb72deSSage Weil 	ret = btrfs_set_inode_index(parent_inode, &index);
9016bdb72deSSage Weil 	BUG_ON(ret);
9026bdb72deSSage Weil 	ret = btrfs_insert_dir_item(trans, parent_root,
903a22285a6SYan, Zheng 				dentry->d_name.name, dentry->d_name.len,
90416cdcec7SMiao Xie 				parent_inode, &key,
905a22285a6SYan, Zheng 				BTRFS_FT_DIR, index);
9066bdb72deSSage Weil 	BUG_ON(ret);
9076bdb72deSSage Weil 
908a22285a6SYan, Zheng 	btrfs_i_size_write(parent_inode, parent_inode->i_size +
909a22285a6SYan, Zheng 					 dentry->d_name.len * 2);
9106bdb72deSSage Weil 	ret = btrfs_update_inode(trans, parent_root, parent_inode);
9116bdb72deSSage Weil 	BUG_ON(ret);
9126bdb72deSSage Weil 
913e999376fSChris Mason 	/*
914e999376fSChris Mason 	 * pull in the delayed directory update
915e999376fSChris Mason 	 * and the delayed inode item
916e999376fSChris Mason 	 * otherwise we corrupt the FS during
917e999376fSChris Mason 	 * snapshot
918e999376fSChris Mason 	 */
919e999376fSChris Mason 	ret = btrfs_run_delayed_items(trans, root);
920e999376fSChris Mason 	BUG_ON(ret);
921e999376fSChris Mason 
9227585717fSChris Mason 	record_root_in_trans(trans, root);
9236bdb72deSSage Weil 	btrfs_set_root_last_snapshot(&root->root_item, trans->transid);
9246bdb72deSSage Weil 	memcpy(new_root_item, &root->root_item, sizeof(*new_root_item));
92508fe4db1SLi Zefan 	btrfs_check_and_init_root_item(new_root_item);
9266bdb72deSSage Weil 
927b83cc969SLi Zefan 	root_flags = btrfs_root_flags(new_root_item);
928b83cc969SLi Zefan 	if (pending->readonly)
929b83cc969SLi Zefan 		root_flags |= BTRFS_ROOT_SUBVOL_RDONLY;
930b83cc969SLi Zefan 	else
931b83cc969SLi Zefan 		root_flags &= ~BTRFS_ROOT_SUBVOL_RDONLY;
932b83cc969SLi Zefan 	btrfs_set_root_flags(new_root_item, root_flags);
933b83cc969SLi Zefan 
934925baeddSChris Mason 	old = btrfs_lock_root_node(root);
9359fa8cfe7SChris Mason 	btrfs_cow_block(trans, root, old, NULL, 0, &old);
9365d4f98a2SYan Zheng 	btrfs_set_lock_blocking(old);
9373063d29fSChris Mason 
938925baeddSChris Mason 	btrfs_copy_root(trans, root, old, &tmp, objectid);
939925baeddSChris Mason 	btrfs_tree_unlock(old);
940925baeddSChris Mason 	free_extent_buffer(old);
9413063d29fSChris Mason 
9425d4f98a2SYan Zheng 	btrfs_set_root_node(new_root_item, tmp);
943a22285a6SYan, Zheng 	/* record when the snapshot was created in key.offset */
944a22285a6SYan, Zheng 	key.offset = trans->transid;
945a22285a6SYan, Zheng 	ret = btrfs_insert_root(trans, tree_root, &key, new_root_item);
946925baeddSChris Mason 	btrfs_tree_unlock(tmp);
9473063d29fSChris Mason 	free_extent_buffer(tmp);
9480660b5afSChris Mason 	BUG_ON(ret);
9490660b5afSChris Mason 
950a22285a6SYan, Zheng 	/*
951a22285a6SYan, Zheng 	 * insert root back/forward references
952a22285a6SYan, Zheng 	 */
953a22285a6SYan, Zheng 	ret = btrfs_add_root_ref(trans, tree_root, objectid,
954a22285a6SYan, Zheng 				 parent_root->root_key.objectid,
95533345d01SLi Zefan 				 btrfs_ino(parent_inode), index,
956a22285a6SYan, Zheng 				 dentry->d_name.name, dentry->d_name.len);
957a22285a6SYan, Zheng 	BUG_ON(ret);
9586a912213SJosef Bacik 	dput(parent);
959a22285a6SYan, Zheng 
960a22285a6SYan, Zheng 	key.offset = (u64)-1;
961a22285a6SYan, Zheng 	pending->snap = btrfs_read_fs_root_no_name(root->fs_info, &key);
962a22285a6SYan, Zheng 	BUG_ON(IS_ERR(pending->snap));
963d68fc57bSYan, Zheng 
9643fd0a558SYan, Zheng 	btrfs_reloc_post_snapshot(trans, pending);
9653063d29fSChris Mason fail:
9666bdb72deSSage Weil 	kfree(new_root_item);
96798c9942aSLiu Bo 	trans->block_rsv = rsv;
968a22285a6SYan, Zheng 	btrfs_block_rsv_release(root, &pending->block_rsv, (u64)-1);
969a22285a6SYan, Zheng 	return 0;
9703063d29fSChris Mason }
9713063d29fSChris Mason 
972d352ac68SChris Mason /*
973d352ac68SChris Mason  * create all the snapshots we've scheduled for creation
974d352ac68SChris Mason  */
97580b6794dSChris Mason static noinline int create_pending_snapshots(struct btrfs_trans_handle *trans,
9763063d29fSChris Mason 					     struct btrfs_fs_info *fs_info)
9773063d29fSChris Mason {
9783063d29fSChris Mason 	struct btrfs_pending_snapshot *pending;
9793063d29fSChris Mason 	struct list_head *head = &trans->transaction->pending_snapshots;
9803de4586cSChris Mason 	int ret;
9813de4586cSChris Mason 
982c6e30871SQinghuang Feng 	list_for_each_entry(pending, head, list) {
9833de4586cSChris Mason 		ret = create_pending_snapshot(trans, fs_info, pending);
9843de4586cSChris Mason 		BUG_ON(ret);
9853de4586cSChris Mason 	}
9863de4586cSChris Mason 	return 0;
9873de4586cSChris Mason }
9883de4586cSChris Mason 
9895d4f98a2SYan Zheng static void update_super_roots(struct btrfs_root *root)
9905d4f98a2SYan Zheng {
9915d4f98a2SYan Zheng 	struct btrfs_root_item *root_item;
9925d4f98a2SYan Zheng 	struct btrfs_super_block *super;
9935d4f98a2SYan Zheng 
9945d4f98a2SYan Zheng 	super = &root->fs_info->super_copy;
9955d4f98a2SYan Zheng 
9965d4f98a2SYan Zheng 	root_item = &root->fs_info->chunk_root->root_item;
9975d4f98a2SYan Zheng 	super->chunk_root = root_item->bytenr;
9985d4f98a2SYan Zheng 	super->chunk_root_generation = root_item->generation;
9995d4f98a2SYan Zheng 	super->chunk_root_level = root_item->level;
10005d4f98a2SYan Zheng 
10015d4f98a2SYan Zheng 	root_item = &root->fs_info->tree_root->root_item;
10025d4f98a2SYan Zheng 	super->root = root_item->bytenr;
10035d4f98a2SYan Zheng 	super->generation = root_item->generation;
10045d4f98a2SYan Zheng 	super->root_level = root_item->level;
100573bc1876SJosef Bacik 	if (btrfs_test_opt(root, SPACE_CACHE))
10060af3d00bSJosef Bacik 		super->cache_generation = root_item->generation;
10075d4f98a2SYan Zheng }
10085d4f98a2SYan Zheng 
1009f36f3042SChris Mason int btrfs_transaction_in_commit(struct btrfs_fs_info *info)
1010f36f3042SChris Mason {
1011f36f3042SChris Mason 	int ret = 0;
1012a4abeea4SJosef Bacik 	spin_lock(&info->trans_lock);
1013f36f3042SChris Mason 	if (info->running_transaction)
1014f36f3042SChris Mason 		ret = info->running_transaction->in_commit;
1015a4abeea4SJosef Bacik 	spin_unlock(&info->trans_lock);
1016f36f3042SChris Mason 	return ret;
1017f36f3042SChris Mason }
1018f36f3042SChris Mason 
10198929ecfaSYan, Zheng int btrfs_transaction_blocked(struct btrfs_fs_info *info)
10208929ecfaSYan, Zheng {
10218929ecfaSYan, Zheng 	int ret = 0;
1022a4abeea4SJosef Bacik 	spin_lock(&info->trans_lock);
10238929ecfaSYan, Zheng 	if (info->running_transaction)
10248929ecfaSYan, Zheng 		ret = info->running_transaction->blocked;
1025a4abeea4SJosef Bacik 	spin_unlock(&info->trans_lock);
10268929ecfaSYan, Zheng 	return ret;
10278929ecfaSYan, Zheng }
10288929ecfaSYan, Zheng 
1029bb9c12c9SSage Weil /*
1030bb9c12c9SSage Weil  * wait for the current transaction commit to start and block subsequent
1031bb9c12c9SSage Weil  * transaction joins
1032bb9c12c9SSage Weil  */
1033bb9c12c9SSage Weil static void wait_current_trans_commit_start(struct btrfs_root *root,
1034bb9c12c9SSage Weil 					    struct btrfs_transaction *trans)
1035bb9c12c9SSage Weil {
103672d63ed6SLi Zefan 	wait_event(root->fs_info->transaction_blocked_wait, trans->in_commit);
1037bb9c12c9SSage Weil }
1038bb9c12c9SSage Weil 
1039bb9c12c9SSage Weil /*
1040bb9c12c9SSage Weil  * wait for the current transaction to start and then become unblocked.
1041bb9c12c9SSage Weil  * caller holds ref.
1042bb9c12c9SSage Weil  */
1043bb9c12c9SSage Weil static void wait_current_trans_commit_start_and_unblock(struct btrfs_root *root,
1044bb9c12c9SSage Weil 					 struct btrfs_transaction *trans)
1045bb9c12c9SSage Weil {
104672d63ed6SLi Zefan 	wait_event(root->fs_info->transaction_wait,
104772d63ed6SLi Zefan 		   trans->commit_done || (trans->in_commit && !trans->blocked));
1048bb9c12c9SSage Weil }
1049bb9c12c9SSage Weil 
1050bb9c12c9SSage Weil /*
1051bb9c12c9SSage Weil  * commit transactions asynchronously. once btrfs_commit_transaction_async
1052bb9c12c9SSage Weil  * returns, any subsequent transaction will not be allowed to join.
1053bb9c12c9SSage Weil  */
1054bb9c12c9SSage Weil struct btrfs_async_commit {
1055bb9c12c9SSage Weil 	struct btrfs_trans_handle *newtrans;
1056bb9c12c9SSage Weil 	struct btrfs_root *root;
1057bb9c12c9SSage Weil 	struct delayed_work work;
1058bb9c12c9SSage Weil };
1059bb9c12c9SSage Weil 
1060bb9c12c9SSage Weil static void do_async_commit(struct work_struct *work)
1061bb9c12c9SSage Weil {
1062bb9c12c9SSage Weil 	struct btrfs_async_commit *ac =
1063bb9c12c9SSage Weil 		container_of(work, struct btrfs_async_commit, work.work);
1064bb9c12c9SSage Weil 
1065bb9c12c9SSage Weil 	btrfs_commit_transaction(ac->newtrans, ac->root);
1066bb9c12c9SSage Weil 	kfree(ac);
1067bb9c12c9SSage Weil }
1068bb9c12c9SSage Weil 
1069bb9c12c9SSage Weil int btrfs_commit_transaction_async(struct btrfs_trans_handle *trans,
1070bb9c12c9SSage Weil 				   struct btrfs_root *root,
1071bb9c12c9SSage Weil 				   int wait_for_unblock)
1072bb9c12c9SSage Weil {
1073bb9c12c9SSage Weil 	struct btrfs_async_commit *ac;
1074bb9c12c9SSage Weil 	struct btrfs_transaction *cur_trans;
1075bb9c12c9SSage Weil 
1076bb9c12c9SSage Weil 	ac = kmalloc(sizeof(*ac), GFP_NOFS);
1077db5b493aSTsutomu Itoh 	if (!ac)
1078db5b493aSTsutomu Itoh 		return -ENOMEM;
1079bb9c12c9SSage Weil 
1080bb9c12c9SSage Weil 	INIT_DELAYED_WORK(&ac->work, do_async_commit);
1081bb9c12c9SSage Weil 	ac->root = root;
10827a7eaa40SJosef Bacik 	ac->newtrans = btrfs_join_transaction(root);
10833612b495STsutomu Itoh 	if (IS_ERR(ac->newtrans)) {
10843612b495STsutomu Itoh 		int err = PTR_ERR(ac->newtrans);
10853612b495STsutomu Itoh 		kfree(ac);
10863612b495STsutomu Itoh 		return err;
10873612b495STsutomu Itoh 	}
1088bb9c12c9SSage Weil 
1089bb9c12c9SSage Weil 	/* take transaction reference */
1090bb9c12c9SSage Weil 	cur_trans = trans->transaction;
109113c5a93eSJosef Bacik 	atomic_inc(&cur_trans->use_count);
1092bb9c12c9SSage Weil 
1093bb9c12c9SSage Weil 	btrfs_end_transaction(trans, root);
1094bb9c12c9SSage Weil 	schedule_delayed_work(&ac->work, 0);
1095bb9c12c9SSage Weil 
1096bb9c12c9SSage Weil 	/* wait for transaction to start and unblock */
1097bb9c12c9SSage Weil 	if (wait_for_unblock)
1098bb9c12c9SSage Weil 		wait_current_trans_commit_start_and_unblock(root, cur_trans);
1099bb9c12c9SSage Weil 	else
1100bb9c12c9SSage Weil 		wait_current_trans_commit_start(root, cur_trans);
1101bb9c12c9SSage Weil 
110238e88054SSage Weil 	if (current->journal_info == trans)
110338e88054SSage Weil 		current->journal_info = NULL;
110438e88054SSage Weil 
110538e88054SSage Weil 	put_transaction(cur_trans);
1106bb9c12c9SSage Weil 	return 0;
1107bb9c12c9SSage Weil }
1108bb9c12c9SSage Weil 
1109bb9c12c9SSage Weil /*
1110bb9c12c9SSage Weil  * btrfs_transaction state sequence:
1111bb9c12c9SSage Weil  *    in_commit = 0, blocked = 0  (initial)
1112bb9c12c9SSage Weil  *    in_commit = 1, blocked = 1
1113bb9c12c9SSage Weil  *    blocked = 0
1114bb9c12c9SSage Weil  *    commit_done = 1
1115bb9c12c9SSage Weil  */
111679154b1bSChris Mason int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
111779154b1bSChris Mason 			     struct btrfs_root *root)
111879154b1bSChris Mason {
111915ee9bc7SJosef Bacik 	unsigned long joined = 0;
112079154b1bSChris Mason 	struct btrfs_transaction *cur_trans;
11218fd17795SChris Mason 	struct btrfs_transaction *prev_trans = NULL;
112279154b1bSChris Mason 	DEFINE_WAIT(wait);
112315ee9bc7SJosef Bacik 	int ret;
112489573b9cSChris Mason 	int should_grow = 0;
112589573b9cSChris Mason 	unsigned long now = get_seconds();
1126dccae999SSage Weil 	int flush_on_commit = btrfs_test_opt(root, FLUSHONCOMMIT);
112779154b1bSChris Mason 
11285a3f23d5SChris Mason 	btrfs_run_ordered_operations(root, 0);
11295a3f23d5SChris Mason 
1130b24e03dbSJosef Bacik 	btrfs_trans_release_metadata(trans, root);
11319c8d86dbSJosef Bacik 	trans->block_rsv = NULL;
11329c8d86dbSJosef Bacik 
113356bec294SChris Mason 	/* make a pass through all the delayed refs we have so far
113456bec294SChris Mason 	 * any runnings procs may add more while we are here
113556bec294SChris Mason 	 */
113656bec294SChris Mason 	ret = btrfs_run_delayed_refs(trans, root, 0);
113756bec294SChris Mason 	BUG_ON(ret);
113856bec294SChris Mason 
1139b7ec40d7SChris Mason 	cur_trans = trans->transaction;
114056bec294SChris Mason 	/*
114156bec294SChris Mason 	 * set the flushing flag so procs in this transaction have to
114256bec294SChris Mason 	 * start sending their work down.
114356bec294SChris Mason 	 */
1144b7ec40d7SChris Mason 	cur_trans->delayed_refs.flushing = 1;
114556bec294SChris Mason 
1146c3e69d58SChris Mason 	ret = btrfs_run_delayed_refs(trans, root, 0);
114756bec294SChris Mason 	BUG_ON(ret);
114856bec294SChris Mason 
1149a4abeea4SJosef Bacik 	spin_lock(&cur_trans->commit_lock);
1150b7ec40d7SChris Mason 	if (cur_trans->in_commit) {
1151a4abeea4SJosef Bacik 		spin_unlock(&cur_trans->commit_lock);
115213c5a93eSJosef Bacik 		atomic_inc(&cur_trans->use_count);
115379154b1bSChris Mason 		btrfs_end_transaction(trans, root);
1154ccd467d6SChris Mason 
1155b9c8300cSLi Zefan 		wait_for_commit(root, cur_trans);
115615ee9bc7SJosef Bacik 
115779154b1bSChris Mason 		put_transaction(cur_trans);
115815ee9bc7SJosef Bacik 
115979154b1bSChris Mason 		return 0;
116079154b1bSChris Mason 	}
11614313b399SChris Mason 
11622c90e5d6SChris Mason 	trans->transaction->in_commit = 1;
1163f9295749SChris Mason 	trans->transaction->blocked = 1;
1164a4abeea4SJosef Bacik 	spin_unlock(&cur_trans->commit_lock);
1165bb9c12c9SSage Weil 	wake_up(&root->fs_info->transaction_blocked_wait);
1166bb9c12c9SSage Weil 
1167a4abeea4SJosef Bacik 	spin_lock(&root->fs_info->trans_lock);
1168ccd467d6SChris Mason 	if (cur_trans->list.prev != &root->fs_info->trans_list) {
1169ccd467d6SChris Mason 		prev_trans = list_entry(cur_trans->list.prev,
1170ccd467d6SChris Mason 					struct btrfs_transaction, list);
1171ccd467d6SChris Mason 		if (!prev_trans->commit_done) {
117213c5a93eSJosef Bacik 			atomic_inc(&prev_trans->use_count);
1173a4abeea4SJosef Bacik 			spin_unlock(&root->fs_info->trans_lock);
1174ccd467d6SChris Mason 
1175ccd467d6SChris Mason 			wait_for_commit(root, prev_trans);
1176ccd467d6SChris Mason 
117715ee9bc7SJosef Bacik 			put_transaction(prev_trans);
1178a4abeea4SJosef Bacik 		} else {
1179a4abeea4SJosef Bacik 			spin_unlock(&root->fs_info->trans_lock);
1180ccd467d6SChris Mason 		}
1181a4abeea4SJosef Bacik 	} else {
1182a4abeea4SJosef Bacik 		spin_unlock(&root->fs_info->trans_lock);
1183ccd467d6SChris Mason 	}
118415ee9bc7SJosef Bacik 
118589573b9cSChris Mason 	if (now < cur_trans->start_time || now - cur_trans->start_time < 1)
118689573b9cSChris Mason 		should_grow = 1;
118789573b9cSChris Mason 
118815ee9bc7SJosef Bacik 	do {
11897ea394f1SYan Zheng 		int snap_pending = 0;
1190a4abeea4SJosef Bacik 
119115ee9bc7SJosef Bacik 		joined = cur_trans->num_joined;
11927ea394f1SYan Zheng 		if (!list_empty(&trans->transaction->pending_snapshots))
11937ea394f1SYan Zheng 			snap_pending = 1;
11947ea394f1SYan Zheng 
11952c90e5d6SChris Mason 		WARN_ON(cur_trans != trans->transaction);
119615ee9bc7SJosef Bacik 
11970bdb1db2SSage Weil 		if (flush_on_commit || snap_pending) {
119824bbcf04SYan, Zheng 			btrfs_start_delalloc_inodes(root, 1);
119924bbcf04SYan, Zheng 			ret = btrfs_wait_ordered_extents(root, 0, 1);
1200ebecd3d9SSage Weil 			BUG_ON(ret);
12017ea394f1SYan Zheng 		}
12027ea394f1SYan Zheng 
120316cdcec7SMiao Xie 		ret = btrfs_run_delayed_items(trans, root);
120416cdcec7SMiao Xie 		BUG_ON(ret);
120516cdcec7SMiao Xie 
12065a3f23d5SChris Mason 		/*
12075a3f23d5SChris Mason 		 * rename don't use btrfs_join_transaction, so, once we
12085a3f23d5SChris Mason 		 * set the transaction to blocked above, we aren't going
12095a3f23d5SChris Mason 		 * to get any new ordered operations.  We can safely run
12105a3f23d5SChris Mason 		 * it here and no for sure that nothing new will be added
12115a3f23d5SChris Mason 		 * to the list
12125a3f23d5SChris Mason 		 */
12135a3f23d5SChris Mason 		btrfs_run_ordered_operations(root, 1);
12145a3f23d5SChris Mason 
1215ed3b3d31SChris Mason 		prepare_to_wait(&cur_trans->writer_wait, &wait,
1216ed3b3d31SChris Mason 				TASK_UNINTERRUPTIBLE);
1217ed3b3d31SChris Mason 
121813c5a93eSJosef Bacik 		if (atomic_read(&cur_trans->num_writers) > 1)
121999d16cbcSSage Weil 			schedule_timeout(MAX_SCHEDULE_TIMEOUT);
122099d16cbcSSage Weil 		else if (should_grow)
122199d16cbcSSage Weil 			schedule_timeout(1);
122215ee9bc7SJosef Bacik 
122315ee9bc7SJosef Bacik 		finish_wait(&cur_trans->writer_wait, &wait);
1224ed0ca140SJosef Bacik 	} while (atomic_read(&cur_trans->num_writers) > 1 ||
1225ed0ca140SJosef Bacik 		 (should_grow && cur_trans->num_joined != joined));
1226ed0ca140SJosef Bacik 
1227ed0ca140SJosef Bacik 	/*
1228ed0ca140SJosef Bacik 	 * Ok now we need to make sure to block out any other joins while we
1229ed0ca140SJosef Bacik 	 * commit the transaction.  We could have started a join before setting
1230ed0ca140SJosef Bacik 	 * no_join so make sure to wait for num_writers to == 1 again.
1231ed0ca140SJosef Bacik 	 */
1232a4abeea4SJosef Bacik 	spin_lock(&root->fs_info->trans_lock);
1233a4abeea4SJosef Bacik 	root->fs_info->trans_no_join = 1;
1234a4abeea4SJosef Bacik 	spin_unlock(&root->fs_info->trans_lock);
1235ed0ca140SJosef Bacik 	wait_event(cur_trans->writer_wait,
1236ed0ca140SJosef Bacik 		   atomic_read(&cur_trans->num_writers) == 1);
123715ee9bc7SJosef Bacik 
12387585717fSChris Mason 	/*
12397585717fSChris Mason 	 * the reloc mutex makes sure that we stop
12407585717fSChris Mason 	 * the balancing code from coming in and moving
12417585717fSChris Mason 	 * extents around in the middle of the commit
12427585717fSChris Mason 	 */
12437585717fSChris Mason 	mutex_lock(&root->fs_info->reloc_mutex);
12447585717fSChris Mason 
1245e999376fSChris Mason 	ret = btrfs_run_delayed_items(trans, root);
12463063d29fSChris Mason 	BUG_ON(ret);
12473063d29fSChris Mason 
1248e999376fSChris Mason 	ret = create_pending_snapshots(trans, root->fs_info);
124916cdcec7SMiao Xie 	BUG_ON(ret);
125016cdcec7SMiao Xie 
125156bec294SChris Mason 	ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
125256bec294SChris Mason 	BUG_ON(ret);
125356bec294SChris Mason 
1254e999376fSChris Mason 	/*
1255e999376fSChris Mason 	 * make sure none of the code above managed to slip in a
1256e999376fSChris Mason 	 * delayed item
1257e999376fSChris Mason 	 */
1258e999376fSChris Mason 	btrfs_assert_delayed_root_empty(root);
1259e999376fSChris Mason 
12602c90e5d6SChris Mason 	WARN_ON(cur_trans != trans->transaction);
1261dc17ff8fSChris Mason 
1262a2de733cSArne Jansen 	btrfs_scrub_pause(root);
1263e02119d5SChris Mason 	/* btrfs_commit_tree_roots is responsible for getting the
1264e02119d5SChris Mason 	 * various roots consistent with each other.  Every pointer
1265e02119d5SChris Mason 	 * in the tree of tree roots has to point to the most up to date
1266e02119d5SChris Mason 	 * root for every subvolume and other tree.  So, we have to keep
1267e02119d5SChris Mason 	 * the tree logging code from jumping in and changing any
1268e02119d5SChris Mason 	 * of the trees.
1269e02119d5SChris Mason 	 *
1270e02119d5SChris Mason 	 * At this point in the commit, there can't be any tree-log
1271e02119d5SChris Mason 	 * writers, but a little lower down we drop the trans mutex
1272e02119d5SChris Mason 	 * and let new people in.  By holding the tree_log_mutex
1273e02119d5SChris Mason 	 * from now until after the super is written, we avoid races
1274e02119d5SChris Mason 	 * with the tree-log code.
1275e02119d5SChris Mason 	 */
1276e02119d5SChris Mason 	mutex_lock(&root->fs_info->tree_log_mutex);
12771a40e23bSZheng Yan 
12785d4f98a2SYan Zheng 	ret = commit_fs_roots(trans, root);
127954aa1f4dSChris Mason 	BUG_ON(ret);
128054aa1f4dSChris Mason 
12815d4f98a2SYan Zheng 	/* commit_fs_roots gets rid of all the tree log roots, it is now
1282e02119d5SChris Mason 	 * safe to free the root of tree log roots
1283e02119d5SChris Mason 	 */
1284e02119d5SChris Mason 	btrfs_free_log_root_tree(trans, root->fs_info);
1285e02119d5SChris Mason 
12865d4f98a2SYan Zheng 	ret = commit_cowonly_roots(trans, root);
128779154b1bSChris Mason 	BUG_ON(ret);
128854aa1f4dSChris Mason 
128911833d66SYan Zheng 	btrfs_prepare_extent_commit(trans, root);
129011833d66SYan Zheng 
129178fae27eSChris Mason 	cur_trans = root->fs_info->running_transaction;
12925f39d397SChris Mason 
12935d4f98a2SYan Zheng 	btrfs_set_root_node(&root->fs_info->tree_root->root_item,
12945d4f98a2SYan Zheng 			    root->fs_info->tree_root->node);
1295817d52f8SJosef Bacik 	switch_commit_root(root->fs_info->tree_root);
12965d4f98a2SYan Zheng 
12975d4f98a2SYan Zheng 	btrfs_set_root_node(&root->fs_info->chunk_root->root_item,
12985d4f98a2SYan Zheng 			    root->fs_info->chunk_root->node);
1299817d52f8SJosef Bacik 	switch_commit_root(root->fs_info->chunk_root);
13005d4f98a2SYan Zheng 
13015d4f98a2SYan Zheng 	update_super_roots(root);
1302e02119d5SChris Mason 
1303e02119d5SChris Mason 	if (!root->fs_info->log_root_recovering) {
1304e02119d5SChris Mason 		btrfs_set_super_log_root(&root->fs_info->super_copy, 0);
1305e02119d5SChris Mason 		btrfs_set_super_log_root_level(&root->fs_info->super_copy, 0);
1306e02119d5SChris Mason 	}
1307e02119d5SChris Mason 
1308a061fc8dSChris Mason 	memcpy(&root->fs_info->super_for_commit, &root->fs_info->super_copy,
13094b52dff6SChris Mason 	       sizeof(root->fs_info->super_copy));
1310ccd467d6SChris Mason 
1311f9295749SChris Mason 	trans->transaction->blocked = 0;
1312a4abeea4SJosef Bacik 	spin_lock(&root->fs_info->trans_lock);
1313a4abeea4SJosef Bacik 	root->fs_info->running_transaction = NULL;
1314a4abeea4SJosef Bacik 	root->fs_info->trans_no_join = 0;
1315a4abeea4SJosef Bacik 	spin_unlock(&root->fs_info->trans_lock);
13167585717fSChris Mason 	mutex_unlock(&root->fs_info->reloc_mutex);
1317b7ec40d7SChris Mason 
1318f9295749SChris Mason 	wake_up(&root->fs_info->transaction_wait);
1319e6dcd2dcSChris Mason 
132079154b1bSChris Mason 	ret = btrfs_write_and_wait_transaction(trans, root);
132179154b1bSChris Mason 	BUG_ON(ret);
1322a512bbf8SYan Zheng 	write_ctree_super(trans, root, 0);
13234313b399SChris Mason 
1324e02119d5SChris Mason 	/*
1325e02119d5SChris Mason 	 * the super is written, we can safely allow the tree-loggers
1326e02119d5SChris Mason 	 * to go about their business
1327e02119d5SChris Mason 	 */
1328e02119d5SChris Mason 	mutex_unlock(&root->fs_info->tree_log_mutex);
1329e02119d5SChris Mason 
133011833d66SYan Zheng 	btrfs_finish_extent_commit(trans, root);
13314313b399SChris Mason 
13322c90e5d6SChris Mason 	cur_trans->commit_done = 1;
1333b7ec40d7SChris Mason 
133415ee9bc7SJosef Bacik 	root->fs_info->last_trans_committed = cur_trans->transid;
1335817d52f8SJosef Bacik 
13362c90e5d6SChris Mason 	wake_up(&cur_trans->commit_wait);
13373de4586cSChris Mason 
1338a4abeea4SJosef Bacik 	spin_lock(&root->fs_info->trans_lock);
133913c5a93eSJosef Bacik 	list_del_init(&cur_trans->list);
1340a4abeea4SJosef Bacik 	spin_unlock(&root->fs_info->trans_lock);
1341a4abeea4SJosef Bacik 
134279154b1bSChris Mason 	put_transaction(cur_trans);
134378fae27eSChris Mason 	put_transaction(cur_trans);
134458176a96SJosef Bacik 
13451abe9b8aSliubo 	trace_btrfs_transaction_commit(root);
13461abe9b8aSliubo 
1347a2de733cSArne Jansen 	btrfs_scrub_continue(root);
1348a2de733cSArne Jansen 
13499ed74f2dSJosef Bacik 	if (current->journal_info == trans)
13509ed74f2dSJosef Bacik 		current->journal_info = NULL;
13519ed74f2dSJosef Bacik 
13522c90e5d6SChris Mason 	kmem_cache_free(btrfs_trans_handle_cachep, trans);
135324bbcf04SYan, Zheng 
135424bbcf04SYan, Zheng 	if (current != root->fs_info->transaction_kthread)
135524bbcf04SYan, Zheng 		btrfs_run_delayed_iputs(root);
135624bbcf04SYan, Zheng 
135779154b1bSChris Mason 	return ret;
135879154b1bSChris Mason }
135979154b1bSChris Mason 
1360d352ac68SChris Mason /*
1361d352ac68SChris Mason  * interface function to delete all the snapshots we have scheduled for deletion
1362d352ac68SChris Mason  */
1363e9d0b13bSChris Mason int btrfs_clean_old_snapshots(struct btrfs_root *root)
1364e9d0b13bSChris Mason {
13655d4f98a2SYan Zheng 	LIST_HEAD(list);
13665d4f98a2SYan Zheng 	struct btrfs_fs_info *fs_info = root->fs_info;
1367e9d0b13bSChris Mason 
1368a4abeea4SJosef Bacik 	spin_lock(&fs_info->trans_lock);
13695d4f98a2SYan Zheng 	list_splice_init(&fs_info->dead_roots, &list);
1370a4abeea4SJosef Bacik 	spin_unlock(&fs_info->trans_lock);
13715d4f98a2SYan Zheng 
13725d4f98a2SYan Zheng 	while (!list_empty(&list)) {
13735d4f98a2SYan Zheng 		root = list_entry(list.next, struct btrfs_root, root_list);
137476dda93cSYan, Zheng 		list_del(&root->root_list);
137576dda93cSYan, Zheng 
137616cdcec7SMiao Xie 		btrfs_kill_all_delayed_nodes(root);
137716cdcec7SMiao Xie 
137876dda93cSYan, Zheng 		if (btrfs_header_backref_rev(root->node) <
137976dda93cSYan, Zheng 		    BTRFS_MIXED_BACKREF_REV)
13803fd0a558SYan, Zheng 			btrfs_drop_snapshot(root, NULL, 0);
138176dda93cSYan, Zheng 		else
13823fd0a558SYan, Zheng 			btrfs_drop_snapshot(root, NULL, 1);
1383e9d0b13bSChris Mason 	}
1384e9d0b13bSChris Mason 	return 0;
1385e9d0b13bSChris Mason }
1386