xref: /openbmc/linux/fs/btrfs/transaction.c (revision ca469637)
16cbd5570SChris Mason /*
26cbd5570SChris Mason  * Copyright (C) 2007 Oracle.  All rights reserved.
36cbd5570SChris Mason  *
46cbd5570SChris Mason  * This program is free software; you can redistribute it and/or
56cbd5570SChris Mason  * modify it under the terms of the GNU General Public
66cbd5570SChris Mason  * License v2 as published by the Free Software Foundation.
76cbd5570SChris Mason  *
86cbd5570SChris Mason  * This program is distributed in the hope that it will be useful,
96cbd5570SChris Mason  * but WITHOUT ANY WARRANTY; without even the implied warranty of
106cbd5570SChris Mason  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
116cbd5570SChris Mason  * General Public License for more details.
126cbd5570SChris Mason  *
136cbd5570SChris Mason  * You should have received a copy of the GNU General Public
146cbd5570SChris Mason  * License along with this program; if not, write to the
156cbd5570SChris Mason  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
166cbd5570SChris Mason  * Boston, MA 021110-1307, USA.
176cbd5570SChris Mason  */
186cbd5570SChris Mason 
1979154b1bSChris Mason #include <linux/fs.h>
205a0e3ad6STejun Heo #include <linux/slab.h>
2134088780SChris Mason #include <linux/sched.h>
22d3c2fdcfSChris Mason #include <linux/writeback.h>
235f39d397SChris Mason #include <linux/pagemap.h>
245f2cc086SChris Mason #include <linux/blkdev.h>
258ea05e3aSAlexander Block #include <linux/uuid.h>
2679154b1bSChris Mason #include "ctree.h"
2779154b1bSChris Mason #include "disk-io.h"
2879154b1bSChris Mason #include "transaction.h"
29925baeddSChris Mason #include "locking.h"
30e02119d5SChris Mason #include "tree-log.h"
31581bb050SLi Zefan #include "inode-map.h"
32733f4fbbSStefan Behrens #include "volumes.h"
3379154b1bSChris Mason 
340f7d52f4SChris Mason #define BTRFS_ROOT_TRANS_TAG 0
350f7d52f4SChris Mason 
3649b25e05SJeff Mahoney void put_transaction(struct btrfs_transaction *transaction)
3779154b1bSChris Mason {
3813c5a93eSJosef Bacik 	WARN_ON(atomic_read(&transaction->use_count) == 0);
3913c5a93eSJosef Bacik 	if (atomic_dec_and_test(&transaction->use_count)) {
40a4abeea4SJosef Bacik 		BUG_ON(!list_empty(&transaction->list));
4100f04b88SArne Jansen 		WARN_ON(transaction->delayed_refs.root.rb_node);
422c90e5d6SChris Mason 		memset(transaction, 0, sizeof(*transaction));
432c90e5d6SChris Mason 		kmem_cache_free(btrfs_transaction_cachep, transaction);
4479154b1bSChris Mason 	}
4578fae27eSChris Mason }
4679154b1bSChris Mason 
47817d52f8SJosef Bacik static noinline void switch_commit_root(struct btrfs_root *root)
48817d52f8SJosef Bacik {
49817d52f8SJosef Bacik 	free_extent_buffer(root->commit_root);
50817d52f8SJosef Bacik 	root->commit_root = btrfs_root_node(root);
51817d52f8SJosef Bacik }
52817d52f8SJosef Bacik 
53d352ac68SChris Mason /*
54d352ac68SChris Mason  * either allocate a new transaction or hop into the existing one
55d352ac68SChris Mason  */
56354aa0fbSMiao Xie static noinline int join_transaction(struct btrfs_root *root, int type)
5779154b1bSChris Mason {
5879154b1bSChris Mason 	struct btrfs_transaction *cur_trans;
5919ae4e81SJan Schmidt 	struct btrfs_fs_info *fs_info = root->fs_info;
60a4abeea4SJosef Bacik 
6119ae4e81SJan Schmidt 	spin_lock(&fs_info->trans_lock);
62d43317dcSChris Mason loop:
6349b25e05SJeff Mahoney 	/* The file system has been taken offline. No new transactions. */
6419ae4e81SJan Schmidt 	if (fs_info->fs_state & BTRFS_SUPER_FLAG_ERROR) {
6519ae4e81SJan Schmidt 		spin_unlock(&fs_info->trans_lock);
6649b25e05SJeff Mahoney 		return -EROFS;
6749b25e05SJeff Mahoney 	}
6849b25e05SJeff Mahoney 
6919ae4e81SJan Schmidt 	if (fs_info->trans_no_join) {
70354aa0fbSMiao Xie 		/*
71354aa0fbSMiao Xie 		 * If we are JOIN_NOLOCK we're already committing a current
72354aa0fbSMiao Xie 		 * transaction, we just need a handle to deal with something
73354aa0fbSMiao Xie 		 * when committing the transaction, such as inode cache and
74354aa0fbSMiao Xie 		 * space cache. It is a special case.
75354aa0fbSMiao Xie 		 */
76354aa0fbSMiao Xie 		if (type != TRANS_JOIN_NOLOCK) {
7719ae4e81SJan Schmidt 			spin_unlock(&fs_info->trans_lock);
78a4abeea4SJosef Bacik 			return -EBUSY;
79a4abeea4SJosef Bacik 		}
80a4abeea4SJosef Bacik 	}
81a4abeea4SJosef Bacik 
8219ae4e81SJan Schmidt 	cur_trans = fs_info->running_transaction;
83a4abeea4SJosef Bacik 	if (cur_trans) {
84871383beSDavid Sterba 		if (cur_trans->aborted) {
8519ae4e81SJan Schmidt 			spin_unlock(&fs_info->trans_lock);
8649b25e05SJeff Mahoney 			return cur_trans->aborted;
87871383beSDavid Sterba 		}
88a4abeea4SJosef Bacik 		atomic_inc(&cur_trans->use_count);
89a4abeea4SJosef Bacik 		atomic_inc(&cur_trans->num_writers);
90a4abeea4SJosef Bacik 		cur_trans->num_joined++;
9119ae4e81SJan Schmidt 		spin_unlock(&fs_info->trans_lock);
92a4abeea4SJosef Bacik 		return 0;
93a4abeea4SJosef Bacik 	}
9419ae4e81SJan Schmidt 	spin_unlock(&fs_info->trans_lock);
95a4abeea4SJosef Bacik 
96354aa0fbSMiao Xie 	/*
97354aa0fbSMiao Xie 	 * If we are ATTACH, we just want to catch the current transaction,
98354aa0fbSMiao Xie 	 * and commit it. If there is no transaction, just return ENOENT.
99354aa0fbSMiao Xie 	 */
100354aa0fbSMiao Xie 	if (type == TRANS_ATTACH)
101354aa0fbSMiao Xie 		return -ENOENT;
102354aa0fbSMiao Xie 
103a4abeea4SJosef Bacik 	cur_trans = kmem_cache_alloc(btrfs_transaction_cachep, GFP_NOFS);
104db5b493aSTsutomu Itoh 	if (!cur_trans)
105db5b493aSTsutomu Itoh 		return -ENOMEM;
106d43317dcSChris Mason 
10719ae4e81SJan Schmidt 	spin_lock(&fs_info->trans_lock);
10819ae4e81SJan Schmidt 	if (fs_info->running_transaction) {
109d43317dcSChris Mason 		/*
110d43317dcSChris Mason 		 * someone started a transaction after we unlocked.  Make sure
111d43317dcSChris Mason 		 * to redo the trans_no_join checks above
112d43317dcSChris Mason 		 */
113a4abeea4SJosef Bacik 		kmem_cache_free(btrfs_transaction_cachep, cur_trans);
11419ae4e81SJan Schmidt 		cur_trans = fs_info->running_transaction;
115d43317dcSChris Mason 		goto loop;
116e4b50e14SDan Carpenter 	} else if (fs_info->fs_state & BTRFS_SUPER_FLAG_ERROR) {
117e4b50e14SDan Carpenter 		spin_unlock(&fs_info->trans_lock);
1187b8b92afSJosef Bacik 		kmem_cache_free(btrfs_transaction_cachep, cur_trans);
1197b8b92afSJosef Bacik 		return -EROFS;
120a4abeea4SJosef Bacik 	}
121d43317dcSChris Mason 
12213c5a93eSJosef Bacik 	atomic_set(&cur_trans->num_writers, 1);
12315ee9bc7SJosef Bacik 	cur_trans->num_joined = 0;
12479154b1bSChris Mason 	init_waitqueue_head(&cur_trans->writer_wait);
12579154b1bSChris Mason 	init_waitqueue_head(&cur_trans->commit_wait);
12679154b1bSChris Mason 	cur_trans->in_commit = 0;
127f9295749SChris Mason 	cur_trans->blocked = 0;
128a4abeea4SJosef Bacik 	/*
129a4abeea4SJosef Bacik 	 * One for this trans handle, one so it will live on until we
130a4abeea4SJosef Bacik 	 * commit the transaction.
131a4abeea4SJosef Bacik 	 */
132a4abeea4SJosef Bacik 	atomic_set(&cur_trans->use_count, 2);
13379154b1bSChris Mason 	cur_trans->commit_done = 0;
13408607c1bSChris Mason 	cur_trans->start_time = get_seconds();
13556bec294SChris Mason 
1366bef4d31SEric Paris 	cur_trans->delayed_refs.root = RB_ROOT;
13756bec294SChris Mason 	cur_trans->delayed_refs.num_entries = 0;
138c3e69d58SChris Mason 	cur_trans->delayed_refs.num_heads_ready = 0;
139c3e69d58SChris Mason 	cur_trans->delayed_refs.num_heads = 0;
14056bec294SChris Mason 	cur_trans->delayed_refs.flushing = 0;
141c3e69d58SChris Mason 	cur_trans->delayed_refs.run_delayed_start = 0;
14220b297d6SJan Schmidt 
14320b297d6SJan Schmidt 	/*
14420b297d6SJan Schmidt 	 * although the tree mod log is per file system and not per transaction,
14520b297d6SJan Schmidt 	 * the log must never go across transaction boundaries.
14620b297d6SJan Schmidt 	 */
14720b297d6SJan Schmidt 	smp_mb();
14820b297d6SJan Schmidt 	if (!list_empty(&fs_info->tree_mod_seq_list)) {
14920b297d6SJan Schmidt 		printk(KERN_ERR "btrfs: tree_mod_seq_list not empty when "
15020b297d6SJan Schmidt 			"creating a fresh transaction\n");
15120b297d6SJan Schmidt 		WARN_ON(1);
15220b297d6SJan Schmidt 	}
15320b297d6SJan Schmidt 	if (!RB_EMPTY_ROOT(&fs_info->tree_mod_log)) {
15420b297d6SJan Schmidt 		printk(KERN_ERR "btrfs: tree_mod_log rb tree not empty when "
15520b297d6SJan Schmidt 			"creating a fresh transaction\n");
15620b297d6SJan Schmidt 		WARN_ON(1);
15720b297d6SJan Schmidt 	}
15820b297d6SJan Schmidt 	atomic_set(&fs_info->tree_mod_seq, 0);
15920b297d6SJan Schmidt 
160a4abeea4SJosef Bacik 	spin_lock_init(&cur_trans->commit_lock);
16156bec294SChris Mason 	spin_lock_init(&cur_trans->delayed_refs.lock);
16256bec294SChris Mason 
1633063d29fSChris Mason 	INIT_LIST_HEAD(&cur_trans->pending_snapshots);
16419ae4e81SJan Schmidt 	list_add_tail(&cur_trans->list, &fs_info->trans_list);
165d1310b2eSChris Mason 	extent_io_tree_init(&cur_trans->dirty_pages,
16619ae4e81SJan Schmidt 			     fs_info->btree_inode->i_mapping);
16719ae4e81SJan Schmidt 	fs_info->generation++;
16819ae4e81SJan Schmidt 	cur_trans->transid = fs_info->generation;
16919ae4e81SJan Schmidt 	fs_info->running_transaction = cur_trans;
17049b25e05SJeff Mahoney 	cur_trans->aborted = 0;
17119ae4e81SJan Schmidt 	spin_unlock(&fs_info->trans_lock);
17215ee9bc7SJosef Bacik 
17379154b1bSChris Mason 	return 0;
17479154b1bSChris Mason }
17579154b1bSChris Mason 
176d352ac68SChris Mason /*
177d397712bSChris Mason  * this does all the record keeping required to make sure that a reference
178d397712bSChris Mason  * counted root is properly recorded in a given transaction.  This is required
179d397712bSChris Mason  * to make sure the old root from before we joined the transaction is deleted
180d397712bSChris Mason  * when the transaction commits
181d352ac68SChris Mason  */
1827585717fSChris Mason static int record_root_in_trans(struct btrfs_trans_handle *trans,
1835d4f98a2SYan Zheng 			       struct btrfs_root *root)
1846702ed49SChris Mason {
1855d4f98a2SYan Zheng 	if (root->ref_cows && root->last_trans < trans->transid) {
1866702ed49SChris Mason 		WARN_ON(root == root->fs_info->extent_root);
1875d4f98a2SYan Zheng 		WARN_ON(root->commit_root != root->node);
1885d4f98a2SYan Zheng 
1897585717fSChris Mason 		/*
1907585717fSChris Mason 		 * see below for in_trans_setup usage rules
1917585717fSChris Mason 		 * we have the reloc mutex held now, so there
1927585717fSChris Mason 		 * is only one writer in this function
1937585717fSChris Mason 		 */
1947585717fSChris Mason 		root->in_trans_setup = 1;
1957585717fSChris Mason 
1967585717fSChris Mason 		/* make sure readers find in_trans_setup before
1977585717fSChris Mason 		 * they find our root->last_trans update
1987585717fSChris Mason 		 */
1997585717fSChris Mason 		smp_wmb();
2007585717fSChris Mason 
201a4abeea4SJosef Bacik 		spin_lock(&root->fs_info->fs_roots_radix_lock);
202a4abeea4SJosef Bacik 		if (root->last_trans == trans->transid) {
203a4abeea4SJosef Bacik 			spin_unlock(&root->fs_info->fs_roots_radix_lock);
204a4abeea4SJosef Bacik 			return 0;
205a4abeea4SJosef Bacik 		}
2066702ed49SChris Mason 		radix_tree_tag_set(&root->fs_info->fs_roots_radix,
2076702ed49SChris Mason 			   (unsigned long)root->root_key.objectid,
2086702ed49SChris Mason 			   BTRFS_ROOT_TRANS_TAG);
209a4abeea4SJosef Bacik 		spin_unlock(&root->fs_info->fs_roots_radix_lock);
2107585717fSChris Mason 		root->last_trans = trans->transid;
2117585717fSChris Mason 
2127585717fSChris Mason 		/* this is pretty tricky.  We don't want to
2137585717fSChris Mason 		 * take the relocation lock in btrfs_record_root_in_trans
2147585717fSChris Mason 		 * unless we're really doing the first setup for this root in
2157585717fSChris Mason 		 * this transaction.
2167585717fSChris Mason 		 *
2177585717fSChris Mason 		 * Normally we'd use root->last_trans as a flag to decide
2187585717fSChris Mason 		 * if we want to take the expensive mutex.
2197585717fSChris Mason 		 *
2207585717fSChris Mason 		 * But, we have to set root->last_trans before we
2217585717fSChris Mason 		 * init the relocation root, otherwise, we trip over warnings
2227585717fSChris Mason 		 * in ctree.c.  The solution used here is to flag ourselves
2237585717fSChris Mason 		 * with root->in_trans_setup.  When this is 1, we're still
2247585717fSChris Mason 		 * fixing up the reloc trees and everyone must wait.
2257585717fSChris Mason 		 *
2267585717fSChris Mason 		 * When this is zero, they can trust root->last_trans and fly
2277585717fSChris Mason 		 * through btrfs_record_root_in_trans without having to take the
2287585717fSChris Mason 		 * lock.  smp_wmb() makes sure that all the writes above are
2297585717fSChris Mason 		 * done before we pop in the zero below
2307585717fSChris Mason 		 */
2315d4f98a2SYan Zheng 		btrfs_init_reloc_root(trans, root);
2327585717fSChris Mason 		smp_wmb();
2337585717fSChris Mason 		root->in_trans_setup = 0;
2346702ed49SChris Mason 	}
2355d4f98a2SYan Zheng 	return 0;
2366702ed49SChris Mason }
2375d4f98a2SYan Zheng 
2387585717fSChris Mason 
2397585717fSChris Mason int btrfs_record_root_in_trans(struct btrfs_trans_handle *trans,
2407585717fSChris Mason 			       struct btrfs_root *root)
2417585717fSChris Mason {
2427585717fSChris Mason 	if (!root->ref_cows)
2437585717fSChris Mason 		return 0;
2447585717fSChris Mason 
2457585717fSChris Mason 	/*
2467585717fSChris Mason 	 * see record_root_in_trans for comments about in_trans_setup usage
2477585717fSChris Mason 	 * and barriers
2487585717fSChris Mason 	 */
2497585717fSChris Mason 	smp_rmb();
2507585717fSChris Mason 	if (root->last_trans == trans->transid &&
2517585717fSChris Mason 	    !root->in_trans_setup)
2527585717fSChris Mason 		return 0;
2537585717fSChris Mason 
2547585717fSChris Mason 	mutex_lock(&root->fs_info->reloc_mutex);
2557585717fSChris Mason 	record_root_in_trans(trans, root);
2567585717fSChris Mason 	mutex_unlock(&root->fs_info->reloc_mutex);
2577585717fSChris Mason 
2587585717fSChris Mason 	return 0;
2597585717fSChris Mason }
2607585717fSChris Mason 
261d352ac68SChris Mason /* wait for commit against the current transaction to become unblocked
262d352ac68SChris Mason  * when this is done, it is safe to start a new transaction, but the current
263d352ac68SChris Mason  * transaction might not be fully on disk.
264d352ac68SChris Mason  */
26537d1aeeeSChris Mason static void wait_current_trans(struct btrfs_root *root)
26679154b1bSChris Mason {
267f9295749SChris Mason 	struct btrfs_transaction *cur_trans;
26879154b1bSChris Mason 
269a4abeea4SJosef Bacik 	spin_lock(&root->fs_info->trans_lock);
270f9295749SChris Mason 	cur_trans = root->fs_info->running_transaction;
27137d1aeeeSChris Mason 	if (cur_trans && cur_trans->blocked) {
27213c5a93eSJosef Bacik 		atomic_inc(&cur_trans->use_count);
273a4abeea4SJosef Bacik 		spin_unlock(&root->fs_info->trans_lock);
27472d63ed6SLi Zefan 
27572d63ed6SLi Zefan 		wait_event(root->fs_info->transaction_wait,
27672d63ed6SLi Zefan 			   !cur_trans->blocked);
277f9295749SChris Mason 		put_transaction(cur_trans);
278a4abeea4SJosef Bacik 	} else {
279a4abeea4SJosef Bacik 		spin_unlock(&root->fs_info->trans_lock);
280f9295749SChris Mason 	}
28137d1aeeeSChris Mason }
28237d1aeeeSChris Mason 
283a22285a6SYan, Zheng static int may_wait_transaction(struct btrfs_root *root, int type)
28437d1aeeeSChris Mason {
285a4abeea4SJosef Bacik 	if (root->fs_info->log_root_recovering)
286a4abeea4SJosef Bacik 		return 0;
287a4abeea4SJosef Bacik 
288a4abeea4SJosef Bacik 	if (type == TRANS_USERSPACE)
289a22285a6SYan, Zheng 		return 1;
290a4abeea4SJosef Bacik 
291a4abeea4SJosef Bacik 	if (type == TRANS_START &&
292a4abeea4SJosef Bacik 	    !atomic_read(&root->fs_info->open_ioctl_trans))
293a4abeea4SJosef Bacik 		return 1;
294a4abeea4SJosef Bacik 
295a22285a6SYan, Zheng 	return 0;
296a22285a6SYan, Zheng }
297a22285a6SYan, Zheng 
29808e007d2SMiao Xie static struct btrfs_trans_handle *
29908e007d2SMiao Xie start_transaction(struct btrfs_root *root, u64 num_items, int type,
30008e007d2SMiao Xie 		  enum btrfs_reserve_flush_enum flush)
301a22285a6SYan, Zheng {
302a22285a6SYan, Zheng 	struct btrfs_trans_handle *h;
303a22285a6SYan, Zheng 	struct btrfs_transaction *cur_trans;
304b5009945SJosef Bacik 	u64 num_bytes = 0;
305a22285a6SYan, Zheng 	int ret;
306c5567237SArne Jansen 	u64 qgroup_reserved = 0;
307acce952bSliubo 
308acce952bSliubo 	if (root->fs_info->fs_state & BTRFS_SUPER_FLAG_ERROR)
309acce952bSliubo 		return ERR_PTR(-EROFS);
3102a1eb461SJosef Bacik 
3112a1eb461SJosef Bacik 	if (current->journal_info) {
3122a1eb461SJosef Bacik 		WARN_ON(type != TRANS_JOIN && type != TRANS_JOIN_NOLOCK);
3132a1eb461SJosef Bacik 		h = current->journal_info;
3142a1eb461SJosef Bacik 		h->use_count++;
315b7d5b0a8SMiao Xie 		WARN_ON(h->use_count > 2);
3162a1eb461SJosef Bacik 		h->orig_rsv = h->block_rsv;
3172a1eb461SJosef Bacik 		h->block_rsv = NULL;
3182a1eb461SJosef Bacik 		goto got_it;
3192a1eb461SJosef Bacik 	}
320b5009945SJosef Bacik 
321b5009945SJosef Bacik 	/*
322b5009945SJosef Bacik 	 * Do the reservation before we join the transaction so we can do all
323b5009945SJosef Bacik 	 * the appropriate flushing if need be.
324b5009945SJosef Bacik 	 */
325b5009945SJosef Bacik 	if (num_items > 0 && root != root->fs_info->chunk_root) {
326c5567237SArne Jansen 		if (root->fs_info->quota_enabled &&
327c5567237SArne Jansen 		    is_fstree(root->root_key.objectid)) {
328c5567237SArne Jansen 			qgroup_reserved = num_items * root->leafsize;
329c5567237SArne Jansen 			ret = btrfs_qgroup_reserve(root, qgroup_reserved);
330c5567237SArne Jansen 			if (ret)
331c5567237SArne Jansen 				return ERR_PTR(ret);
332c5567237SArne Jansen 		}
333c5567237SArne Jansen 
334b5009945SJosef Bacik 		num_bytes = btrfs_calc_trans_metadata_size(root, num_items);
3354a92b1b8SJosef Bacik 		ret = btrfs_block_rsv_add(root,
336b5009945SJosef Bacik 					  &root->fs_info->trans_block_rsv,
33708e007d2SMiao Xie 					  num_bytes, flush);
338b5009945SJosef Bacik 		if (ret)
339b5009945SJosef Bacik 			return ERR_PTR(ret);
340b5009945SJosef Bacik 	}
341a22285a6SYan, Zheng again:
342a22285a6SYan, Zheng 	h = kmem_cache_alloc(btrfs_trans_handle_cachep, GFP_NOFS);
343a22285a6SYan, Zheng 	if (!h)
344a22285a6SYan, Zheng 		return ERR_PTR(-ENOMEM);
345a22285a6SYan, Zheng 
34698114659SJosef Bacik 	/*
34798114659SJosef Bacik 	 * If we are JOIN_NOLOCK we're already committing a transaction and
34898114659SJosef Bacik 	 * waiting on this guy, so we don't need to do the sb_start_intwrite
34998114659SJosef Bacik 	 * because we're already holding a ref.  We need this because we could
35098114659SJosef Bacik 	 * have raced in and did an fsync() on a file which can kick a commit
35198114659SJosef Bacik 	 * and then we deadlock with somebody doing a freeze.
352354aa0fbSMiao Xie 	 *
353354aa0fbSMiao Xie 	 * If we are ATTACH, it means we just want to catch the current
354354aa0fbSMiao Xie 	 * transaction and commit it, so we needn't do sb_start_intwrite().
35598114659SJosef Bacik 	 */
356354aa0fbSMiao Xie 	if (type < TRANS_JOIN_NOLOCK)
357b2b5ef5cSJan Kara 		sb_start_intwrite(root->fs_info->sb);
358b2b5ef5cSJan Kara 
359a22285a6SYan, Zheng 	if (may_wait_transaction(root, type))
36037d1aeeeSChris Mason 		wait_current_trans(root);
361a22285a6SYan, Zheng 
362a4abeea4SJosef Bacik 	do {
363354aa0fbSMiao Xie 		ret = join_transaction(root, type);
364a4abeea4SJosef Bacik 		if (ret == -EBUSY)
365a4abeea4SJosef Bacik 			wait_current_trans(root);
366a4abeea4SJosef Bacik 	} while (ret == -EBUSY);
367a4abeea4SJosef Bacik 
368db5b493aSTsutomu Itoh 	if (ret < 0) {
369354aa0fbSMiao Xie 		/* We must get the transaction if we are JOIN_NOLOCK. */
370354aa0fbSMiao Xie 		BUG_ON(type == TRANS_JOIN_NOLOCK);
371354aa0fbSMiao Xie 
372354aa0fbSMiao Xie 		if (type < TRANS_JOIN_NOLOCK)
373b2b5ef5cSJan Kara 			sb_end_intwrite(root->fs_info->sb);
3746e8df2aeSYoshinori Sano 		kmem_cache_free(btrfs_trans_handle_cachep, h);
375db5b493aSTsutomu Itoh 		return ERR_PTR(ret);
376db5b493aSTsutomu Itoh 	}
3770f7d52f4SChris Mason 
378a22285a6SYan, Zheng 	cur_trans = root->fs_info->running_transaction;
379a22285a6SYan, Zheng 
380a22285a6SYan, Zheng 	h->transid = cur_trans->transid;
381a22285a6SYan, Zheng 	h->transaction = cur_trans;
38279154b1bSChris Mason 	h->blocks_used = 0;
383a22285a6SYan, Zheng 	h->bytes_reserved = 0;
384d13603efSArne Jansen 	h->root = root;
38556bec294SChris Mason 	h->delayed_ref_updates = 0;
3862a1eb461SJosef Bacik 	h->use_count = 1;
3870e721106SJosef Bacik 	h->adding_csums = 0;
388f0486c68SYan, Zheng 	h->block_rsv = NULL;
3892a1eb461SJosef Bacik 	h->orig_rsv = NULL;
39049b25e05SJeff Mahoney 	h->aborted = 0;
391c5567237SArne Jansen 	h->qgroup_reserved = qgroup_reserved;
392bed92eaeSArne Jansen 	h->delayed_ref_elem.seq = 0;
393a698d075SMiao Xie 	h->type = type;
394bed92eaeSArne Jansen 	INIT_LIST_HEAD(&h->qgroup_ref_list);
395ea658badSJosef Bacik 	INIT_LIST_HEAD(&h->new_bgs);
396b7ec40d7SChris Mason 
397a22285a6SYan, Zheng 	smp_mb();
398a22285a6SYan, Zheng 	if (cur_trans->blocked && may_wait_transaction(root, type)) {
399a22285a6SYan, Zheng 		btrfs_commit_transaction(h, root);
400a22285a6SYan, Zheng 		goto again;
401a22285a6SYan, Zheng 	}
4029ed74f2dSJosef Bacik 
403b5009945SJosef Bacik 	if (num_bytes) {
4048c2a3ca2SJosef Bacik 		trace_btrfs_space_reservation(root->fs_info, "transaction",
4052bcc0328SLiu Bo 					      h->transid, num_bytes, 1);
406b5009945SJosef Bacik 		h->block_rsv = &root->fs_info->trans_block_rsv;
407b5009945SJosef Bacik 		h->bytes_reserved = num_bytes;
408a22285a6SYan, Zheng 	}
409a22285a6SYan, Zheng 
4102a1eb461SJosef Bacik got_it:
411a4abeea4SJosef Bacik 	btrfs_record_root_in_trans(h, root);
412a22285a6SYan, Zheng 
413a22285a6SYan, Zheng 	if (!current->journal_info && type != TRANS_USERSPACE)
414a22285a6SYan, Zheng 		current->journal_info = h;
41579154b1bSChris Mason 	return h;
41679154b1bSChris Mason }
41779154b1bSChris Mason 
418f9295749SChris Mason struct btrfs_trans_handle *btrfs_start_transaction(struct btrfs_root *root,
419a22285a6SYan, Zheng 						   int num_items)
420f9295749SChris Mason {
42108e007d2SMiao Xie 	return start_transaction(root, num_items, TRANS_START,
42208e007d2SMiao Xie 				 BTRFS_RESERVE_FLUSH_ALL);
423f9295749SChris Mason }
4248407aa46SMiao Xie 
42508e007d2SMiao Xie struct btrfs_trans_handle *btrfs_start_transaction_lflush(
4268407aa46SMiao Xie 					struct btrfs_root *root, int num_items)
4278407aa46SMiao Xie {
42808e007d2SMiao Xie 	return start_transaction(root, num_items, TRANS_START,
42908e007d2SMiao Xie 				 BTRFS_RESERVE_FLUSH_LIMIT);
4308407aa46SMiao Xie }
4318407aa46SMiao Xie 
4327a7eaa40SJosef Bacik struct btrfs_trans_handle *btrfs_join_transaction(struct btrfs_root *root)
433f9295749SChris Mason {
4348407aa46SMiao Xie 	return start_transaction(root, 0, TRANS_JOIN, 0);
435f9295749SChris Mason }
436f9295749SChris Mason 
4377a7eaa40SJosef Bacik struct btrfs_trans_handle *btrfs_join_transaction_nolock(struct btrfs_root *root)
4380af3d00bSJosef Bacik {
4398407aa46SMiao Xie 	return start_transaction(root, 0, TRANS_JOIN_NOLOCK, 0);
4400af3d00bSJosef Bacik }
4410af3d00bSJosef Bacik 
4427a7eaa40SJosef Bacik struct btrfs_trans_handle *btrfs_start_ioctl_transaction(struct btrfs_root *root)
4439ca9ee09SSage Weil {
4448407aa46SMiao Xie 	return start_transaction(root, 0, TRANS_USERSPACE, 0);
4459ca9ee09SSage Weil }
4469ca9ee09SSage Weil 
447354aa0fbSMiao Xie struct btrfs_trans_handle *btrfs_attach_transaction(struct btrfs_root *root)
44860376ce4SJosef Bacik {
449354aa0fbSMiao Xie 	return start_transaction(root, 0, TRANS_ATTACH, 0);
45060376ce4SJosef Bacik }
45160376ce4SJosef Bacik 
452d352ac68SChris Mason /* wait for a transaction commit to be fully complete */
453b9c8300cSLi Zefan static noinline void wait_for_commit(struct btrfs_root *root,
45489ce8a63SChris Mason 				    struct btrfs_transaction *commit)
45589ce8a63SChris Mason {
45672d63ed6SLi Zefan 	wait_event(commit->commit_wait, commit->commit_done);
45789ce8a63SChris Mason }
45889ce8a63SChris Mason 
45946204592SSage Weil int btrfs_wait_for_commit(struct btrfs_root *root, u64 transid)
46046204592SSage Weil {
46146204592SSage Weil 	struct btrfs_transaction *cur_trans = NULL, *t;
46246204592SSage Weil 	int ret;
46346204592SSage Weil 
46446204592SSage Weil 	ret = 0;
46546204592SSage Weil 	if (transid) {
46646204592SSage Weil 		if (transid <= root->fs_info->last_trans_committed)
467a4abeea4SJosef Bacik 			goto out;
46846204592SSage Weil 
46946204592SSage Weil 		/* find specified transaction */
470a4abeea4SJosef Bacik 		spin_lock(&root->fs_info->trans_lock);
47146204592SSage Weil 		list_for_each_entry(t, &root->fs_info->trans_list, list) {
47246204592SSage Weil 			if (t->transid == transid) {
47346204592SSage Weil 				cur_trans = t;
474a4abeea4SJosef Bacik 				atomic_inc(&cur_trans->use_count);
47546204592SSage Weil 				break;
47646204592SSage Weil 			}
47746204592SSage Weil 			if (t->transid > transid)
47846204592SSage Weil 				break;
47946204592SSage Weil 		}
480a4abeea4SJosef Bacik 		spin_unlock(&root->fs_info->trans_lock);
48146204592SSage Weil 		ret = -EINVAL;
48246204592SSage Weil 		if (!cur_trans)
483a4abeea4SJosef Bacik 			goto out;  /* bad transid */
48446204592SSage Weil 	} else {
48546204592SSage Weil 		/* find newest transaction that is committing | committed */
486a4abeea4SJosef Bacik 		spin_lock(&root->fs_info->trans_lock);
48746204592SSage Weil 		list_for_each_entry_reverse(t, &root->fs_info->trans_list,
48846204592SSage Weil 					    list) {
48946204592SSage Weil 			if (t->in_commit) {
49046204592SSage Weil 				if (t->commit_done)
4913473f3c0SJosef Bacik 					break;
49246204592SSage Weil 				cur_trans = t;
493a4abeea4SJosef Bacik 				atomic_inc(&cur_trans->use_count);
49446204592SSage Weil 				break;
49546204592SSage Weil 			}
49646204592SSage Weil 		}
497a4abeea4SJosef Bacik 		spin_unlock(&root->fs_info->trans_lock);
49846204592SSage Weil 		if (!cur_trans)
499a4abeea4SJosef Bacik 			goto out;  /* nothing committing|committed */
50046204592SSage Weil 	}
50146204592SSage Weil 
50246204592SSage Weil 	wait_for_commit(root, cur_trans);
50346204592SSage Weil 
50446204592SSage Weil 	put_transaction(cur_trans);
50546204592SSage Weil 	ret = 0;
506a4abeea4SJosef Bacik out:
50746204592SSage Weil 	return ret;
50846204592SSage Weil }
50946204592SSage Weil 
51037d1aeeeSChris Mason void btrfs_throttle(struct btrfs_root *root)
51137d1aeeeSChris Mason {
512a4abeea4SJosef Bacik 	if (!atomic_read(&root->fs_info->open_ioctl_trans))
51337d1aeeeSChris Mason 		wait_current_trans(root);
51437d1aeeeSChris Mason }
51537d1aeeeSChris Mason 
5168929ecfaSYan, Zheng static int should_end_transaction(struct btrfs_trans_handle *trans,
5178929ecfaSYan, Zheng 				  struct btrfs_root *root)
5188929ecfaSYan, Zheng {
5198929ecfaSYan, Zheng 	int ret;
52036ba022aSJosef Bacik 
52136ba022aSJosef Bacik 	ret = btrfs_block_rsv_check(root, &root->fs_info->global_block_rsv, 5);
5228929ecfaSYan, Zheng 	return ret ? 1 : 0;
5238929ecfaSYan, Zheng }
5248929ecfaSYan, Zheng 
5258929ecfaSYan, Zheng int btrfs_should_end_transaction(struct btrfs_trans_handle *trans,
5268929ecfaSYan, Zheng 				 struct btrfs_root *root)
5278929ecfaSYan, Zheng {
5288929ecfaSYan, Zheng 	struct btrfs_transaction *cur_trans = trans->transaction;
5298929ecfaSYan, Zheng 	int updates;
53049b25e05SJeff Mahoney 	int err;
5318929ecfaSYan, Zheng 
532a4abeea4SJosef Bacik 	smp_mb();
5338929ecfaSYan, Zheng 	if (cur_trans->blocked || cur_trans->delayed_refs.flushing)
5348929ecfaSYan, Zheng 		return 1;
5358929ecfaSYan, Zheng 
5368929ecfaSYan, Zheng 	updates = trans->delayed_ref_updates;
5378929ecfaSYan, Zheng 	trans->delayed_ref_updates = 0;
53849b25e05SJeff Mahoney 	if (updates) {
53949b25e05SJeff Mahoney 		err = btrfs_run_delayed_refs(trans, root, updates);
54049b25e05SJeff Mahoney 		if (err) /* Error code will also eval true */
54149b25e05SJeff Mahoney 			return err;
54249b25e05SJeff Mahoney 	}
5438929ecfaSYan, Zheng 
5448929ecfaSYan, Zheng 	return should_end_transaction(trans, root);
5458929ecfaSYan, Zheng }
5468929ecfaSYan, Zheng 
54789ce8a63SChris Mason static int __btrfs_end_transaction(struct btrfs_trans_handle *trans,
548a698d075SMiao Xie 			  struct btrfs_root *root, int throttle)
54979154b1bSChris Mason {
5508929ecfaSYan, Zheng 	struct btrfs_transaction *cur_trans = trans->transaction;
551ab78c84dSChris Mason 	struct btrfs_fs_info *info = root->fs_info;
552c3e69d58SChris Mason 	int count = 0;
553a698d075SMiao Xie 	int lock = (trans->type != TRANS_JOIN_NOLOCK);
5544edc2ca3SDave Jones 	int err = 0;
555d6e4a428SChris Mason 
5562a1eb461SJosef Bacik 	if (--trans->use_count) {
5572a1eb461SJosef Bacik 		trans->block_rsv = trans->orig_rsv;
5582a1eb461SJosef Bacik 		return 0;
5592a1eb461SJosef Bacik 	}
5602a1eb461SJosef Bacik 
561edf39272SJan Schmidt 	/*
562edf39272SJan Schmidt 	 * do the qgroup accounting as early as possible
563edf39272SJan Schmidt 	 */
564edf39272SJan Schmidt 	err = btrfs_delayed_refs_qgroup_accounting(trans, info);
565edf39272SJan Schmidt 
566b24e03dbSJosef Bacik 	btrfs_trans_release_metadata(trans, root);
5674c13d758SJosef Bacik 	trans->block_rsv = NULL;
568d13603efSArne Jansen 	/*
569d13603efSArne Jansen 	 * the same root has to be passed to start_transaction and
570d13603efSArne Jansen 	 * end_transaction. Subvolume quota depends on this.
571d13603efSArne Jansen 	 */
572d13603efSArne Jansen 	WARN_ON(trans->root != root);
573c5567237SArne Jansen 
574c5567237SArne Jansen 	if (trans->qgroup_reserved) {
575c5567237SArne Jansen 		btrfs_qgroup_free(root, trans->qgroup_reserved);
576c5567237SArne Jansen 		trans->qgroup_reserved = 0;
577c5567237SArne Jansen 	}
578c5567237SArne Jansen 
579ea658badSJosef Bacik 	if (!list_empty(&trans->new_bgs))
580ea658badSJosef Bacik 		btrfs_create_pending_block_groups(trans, root);
581ea658badSJosef Bacik 
582203bf287SChris Mason 	while (count < 2) {
583c3e69d58SChris Mason 		unsigned long cur = trans->delayed_ref_updates;
584c3e69d58SChris Mason 		trans->delayed_ref_updates = 0;
585c3e69d58SChris Mason 		if (cur &&
586c3e69d58SChris Mason 		    trans->transaction->delayed_refs.num_heads_ready > 64) {
587c3e69d58SChris Mason 			trans->delayed_ref_updates = 0;
588c3e69d58SChris Mason 			btrfs_run_delayed_refs(trans, root, cur);
589c3e69d58SChris Mason 		} else {
590c3e69d58SChris Mason 			break;
591c3e69d58SChris Mason 		}
592c3e69d58SChris Mason 		count++;
59356bec294SChris Mason 	}
5940e721106SJosef Bacik 	btrfs_trans_release_metadata(trans, root);
5950e721106SJosef Bacik 	trans->block_rsv = NULL;
59656bec294SChris Mason 
597ea658badSJosef Bacik 	if (!list_empty(&trans->new_bgs))
598ea658badSJosef Bacik 		btrfs_create_pending_block_groups(trans, root);
599ea658badSJosef Bacik 
600a4abeea4SJosef Bacik 	if (lock && !atomic_read(&root->fs_info->open_ioctl_trans) &&
601a4abeea4SJosef Bacik 	    should_end_transaction(trans, root)) {
6028929ecfaSYan, Zheng 		trans->transaction->blocked = 1;
603a4abeea4SJosef Bacik 		smp_wmb();
604a4abeea4SJosef Bacik 	}
6058929ecfaSYan, Zheng 
6060af3d00bSJosef Bacik 	if (lock && cur_trans->blocked && !cur_trans->in_commit) {
60781317fdeSJosef Bacik 		if (throttle) {
60881317fdeSJosef Bacik 			/*
60981317fdeSJosef Bacik 			 * We may race with somebody else here so end up having
61081317fdeSJosef Bacik 			 * to call end_transaction on ourselves again, so inc
61181317fdeSJosef Bacik 			 * our use_count.
61281317fdeSJosef Bacik 			 */
61381317fdeSJosef Bacik 			trans->use_count++;
6148929ecfaSYan, Zheng 			return btrfs_commit_transaction(trans, root);
61581317fdeSJosef Bacik 		} else {
6168929ecfaSYan, Zheng 			wake_up_process(info->transaction_kthread);
6178929ecfaSYan, Zheng 		}
61881317fdeSJosef Bacik 	}
6198929ecfaSYan, Zheng 
620354aa0fbSMiao Xie 	if (trans->type < TRANS_JOIN_NOLOCK)
6216df7881aSJosef Bacik 		sb_end_intwrite(root->fs_info->sb);
6226df7881aSJosef Bacik 
6238929ecfaSYan, Zheng 	WARN_ON(cur_trans != info->running_transaction);
62413c5a93eSJosef Bacik 	WARN_ON(atomic_read(&cur_trans->num_writers) < 1);
62513c5a93eSJosef Bacik 	atomic_dec(&cur_trans->num_writers);
62689ce8a63SChris Mason 
62799d16cbcSSage Weil 	smp_mb();
62879154b1bSChris Mason 	if (waitqueue_active(&cur_trans->writer_wait))
62979154b1bSChris Mason 		wake_up(&cur_trans->writer_wait);
63079154b1bSChris Mason 	put_transaction(cur_trans);
6319ed74f2dSJosef Bacik 
6329ed74f2dSJosef Bacik 	if (current->journal_info == trans)
6339ed74f2dSJosef Bacik 		current->journal_info = NULL;
634ab78c84dSChris Mason 
63524bbcf04SYan, Zheng 	if (throttle)
63624bbcf04SYan, Zheng 		btrfs_run_delayed_iputs(root);
63724bbcf04SYan, Zheng 
63849b25e05SJeff Mahoney 	if (trans->aborted ||
63949b25e05SJeff Mahoney 	    root->fs_info->fs_state & BTRFS_SUPER_FLAG_ERROR) {
6404edc2ca3SDave Jones 		err = -EIO;
64149b25e05SJeff Mahoney 	}
642edf39272SJan Schmidt 	assert_qgroups_uptodate(trans);
64349b25e05SJeff Mahoney 
6444edc2ca3SDave Jones 	memset(trans, 0, sizeof(*trans));
6454edc2ca3SDave Jones 	kmem_cache_free(btrfs_trans_handle_cachep, trans);
6464edc2ca3SDave Jones 	return err;
64779154b1bSChris Mason }
64879154b1bSChris Mason 
64989ce8a63SChris Mason int btrfs_end_transaction(struct btrfs_trans_handle *trans,
65089ce8a63SChris Mason 			  struct btrfs_root *root)
65189ce8a63SChris Mason {
65216cdcec7SMiao Xie 	int ret;
65316cdcec7SMiao Xie 
654a698d075SMiao Xie 	ret = __btrfs_end_transaction(trans, root, 0);
65516cdcec7SMiao Xie 	if (ret)
65616cdcec7SMiao Xie 		return ret;
65716cdcec7SMiao Xie 	return 0;
65889ce8a63SChris Mason }
65989ce8a63SChris Mason 
66089ce8a63SChris Mason int btrfs_end_transaction_throttle(struct btrfs_trans_handle *trans,
66189ce8a63SChris Mason 				   struct btrfs_root *root)
66289ce8a63SChris Mason {
66316cdcec7SMiao Xie 	int ret;
66416cdcec7SMiao Xie 
665a698d075SMiao Xie 	ret = __btrfs_end_transaction(trans, root, 1);
66616cdcec7SMiao Xie 	if (ret)
66716cdcec7SMiao Xie 		return ret;
66816cdcec7SMiao Xie 	return 0;
66916cdcec7SMiao Xie }
67016cdcec7SMiao Xie 
67116cdcec7SMiao Xie int btrfs_end_transaction_dmeta(struct btrfs_trans_handle *trans,
67216cdcec7SMiao Xie 				struct btrfs_root *root)
67316cdcec7SMiao Xie {
674a698d075SMiao Xie 	return __btrfs_end_transaction(trans, root, 1);
67589ce8a63SChris Mason }
67689ce8a63SChris Mason 
677d352ac68SChris Mason /*
678d352ac68SChris Mason  * when btree blocks are allocated, they have some corresponding bits set for
679d352ac68SChris Mason  * them in one of two extent_io trees.  This is used to make sure all of
680690587d1SChris Mason  * those extents are sent to disk but does not wait on them
681d352ac68SChris Mason  */
682690587d1SChris Mason int btrfs_write_marked_extents(struct btrfs_root *root,
6838cef4e16SYan, Zheng 			       struct extent_io_tree *dirty_pages, int mark)
68479154b1bSChris Mason {
685777e6bd7SChris Mason 	int err = 0;
6867c4452b9SChris Mason 	int werr = 0;
6871728366eSJosef Bacik 	struct address_space *mapping = root->fs_info->btree_inode->i_mapping;
688e6138876SJosef Bacik 	struct extent_state *cached_state = NULL;
689777e6bd7SChris Mason 	u64 start = 0;
6905f39d397SChris Mason 	u64 end;
6917c4452b9SChris Mason 
6921728366eSJosef Bacik 	while (!find_first_extent_bit(dirty_pages, start, &start, &end,
693e6138876SJosef Bacik 				      mark, &cached_state)) {
694e6138876SJosef Bacik 		convert_extent_bit(dirty_pages, start, end, EXTENT_NEED_WAIT,
695e6138876SJosef Bacik 				   mark, &cached_state, GFP_NOFS);
696e6138876SJosef Bacik 		cached_state = NULL;
6971728366eSJosef Bacik 		err = filemap_fdatawrite_range(mapping, start, end);
6987c4452b9SChris Mason 		if (err)
6997c4452b9SChris Mason 			werr = err;
7001728366eSJosef Bacik 		cond_resched();
7011728366eSJosef Bacik 		start = end + 1;
7027c4452b9SChris Mason 	}
703690587d1SChris Mason 	if (err)
704690587d1SChris Mason 		werr = err;
705690587d1SChris Mason 	return werr;
706690587d1SChris Mason }
707690587d1SChris Mason 
708690587d1SChris Mason /*
709690587d1SChris Mason  * when btree blocks are allocated, they have some corresponding bits set for
710690587d1SChris Mason  * them in one of two extent_io trees.  This is used to make sure all of
711690587d1SChris Mason  * those extents are on disk for transaction or log commit.  We wait
712690587d1SChris Mason  * on all the pages and clear them from the dirty pages state tree
713690587d1SChris Mason  */
714690587d1SChris Mason int btrfs_wait_marked_extents(struct btrfs_root *root,
7158cef4e16SYan, Zheng 			      struct extent_io_tree *dirty_pages, int mark)
716690587d1SChris Mason {
717690587d1SChris Mason 	int err = 0;
718690587d1SChris Mason 	int werr = 0;
7191728366eSJosef Bacik 	struct address_space *mapping = root->fs_info->btree_inode->i_mapping;
720e6138876SJosef Bacik 	struct extent_state *cached_state = NULL;
721690587d1SChris Mason 	u64 start = 0;
722690587d1SChris Mason 	u64 end;
723690587d1SChris Mason 
7241728366eSJosef Bacik 	while (!find_first_extent_bit(dirty_pages, start, &start, &end,
725e6138876SJosef Bacik 				      EXTENT_NEED_WAIT, &cached_state)) {
726e6138876SJosef Bacik 		clear_extent_bit(dirty_pages, start, end, EXTENT_NEED_WAIT,
727e6138876SJosef Bacik 				 0, 0, &cached_state, GFP_NOFS);
7281728366eSJosef Bacik 		err = filemap_fdatawait_range(mapping, start, end);
729777e6bd7SChris Mason 		if (err)
730777e6bd7SChris Mason 			werr = err;
731777e6bd7SChris Mason 		cond_resched();
7321728366eSJosef Bacik 		start = end + 1;
733777e6bd7SChris Mason 	}
7347c4452b9SChris Mason 	if (err)
7357c4452b9SChris Mason 		werr = err;
7367c4452b9SChris Mason 	return werr;
73779154b1bSChris Mason }
73879154b1bSChris Mason 
739690587d1SChris Mason /*
740690587d1SChris Mason  * when btree blocks are allocated, they have some corresponding bits set for
741690587d1SChris Mason  * them in one of two extent_io trees.  This is used to make sure all of
742690587d1SChris Mason  * those extents are on disk for transaction or log commit
743690587d1SChris Mason  */
744690587d1SChris Mason int btrfs_write_and_wait_marked_extents(struct btrfs_root *root,
7458cef4e16SYan, Zheng 				struct extent_io_tree *dirty_pages, int mark)
746690587d1SChris Mason {
747690587d1SChris Mason 	int ret;
748690587d1SChris Mason 	int ret2;
749690587d1SChris Mason 
7508cef4e16SYan, Zheng 	ret = btrfs_write_marked_extents(root, dirty_pages, mark);
7518cef4e16SYan, Zheng 	ret2 = btrfs_wait_marked_extents(root, dirty_pages, mark);
752bf0da8c1SChris Mason 
753bf0da8c1SChris Mason 	if (ret)
754bf0da8c1SChris Mason 		return ret;
755bf0da8c1SChris Mason 	if (ret2)
756bf0da8c1SChris Mason 		return ret2;
757bf0da8c1SChris Mason 	return 0;
758690587d1SChris Mason }
759690587d1SChris Mason 
760d0c803c4SChris Mason int btrfs_write_and_wait_transaction(struct btrfs_trans_handle *trans,
761d0c803c4SChris Mason 				     struct btrfs_root *root)
762d0c803c4SChris Mason {
763d0c803c4SChris Mason 	if (!trans || !trans->transaction) {
764d0c803c4SChris Mason 		struct inode *btree_inode;
765d0c803c4SChris Mason 		btree_inode = root->fs_info->btree_inode;
766d0c803c4SChris Mason 		return filemap_write_and_wait(btree_inode->i_mapping);
767d0c803c4SChris Mason 	}
768d0c803c4SChris Mason 	return btrfs_write_and_wait_marked_extents(root,
7698cef4e16SYan, Zheng 					   &trans->transaction->dirty_pages,
7708cef4e16SYan, Zheng 					   EXTENT_DIRTY);
771d0c803c4SChris Mason }
772d0c803c4SChris Mason 
773d352ac68SChris Mason /*
774d352ac68SChris Mason  * this is used to update the root pointer in the tree of tree roots.
775d352ac68SChris Mason  *
776d352ac68SChris Mason  * But, in the case of the extent allocation tree, updating the root
777d352ac68SChris Mason  * pointer may allocate blocks which may change the root of the extent
778d352ac68SChris Mason  * allocation tree.
779d352ac68SChris Mason  *
780d352ac68SChris Mason  * So, this loops and repeats and makes sure the cowonly root didn't
781d352ac68SChris Mason  * change while the root pointer was being updated in the metadata.
782d352ac68SChris Mason  */
7830b86a832SChris Mason static int update_cowonly_root(struct btrfs_trans_handle *trans,
78479154b1bSChris Mason 			       struct btrfs_root *root)
78579154b1bSChris Mason {
78679154b1bSChris Mason 	int ret;
7870b86a832SChris Mason 	u64 old_root_bytenr;
78886b9f2ecSYan, Zheng 	u64 old_root_used;
7890b86a832SChris Mason 	struct btrfs_root *tree_root = root->fs_info->tree_root;
79079154b1bSChris Mason 
79186b9f2ecSYan, Zheng 	old_root_used = btrfs_root_used(&root->root_item);
7920b86a832SChris Mason 	btrfs_write_dirty_block_groups(trans, root);
79356bec294SChris Mason 
79479154b1bSChris Mason 	while (1) {
7950b86a832SChris Mason 		old_root_bytenr = btrfs_root_bytenr(&root->root_item);
79686b9f2ecSYan, Zheng 		if (old_root_bytenr == root->node->start &&
79786b9f2ecSYan, Zheng 		    old_root_used == btrfs_root_used(&root->root_item))
79879154b1bSChris Mason 			break;
79987ef2bb4SChris Mason 
8005d4f98a2SYan Zheng 		btrfs_set_root_node(&root->root_item, root->node);
80179154b1bSChris Mason 		ret = btrfs_update_root(trans, tree_root,
8020b86a832SChris Mason 					&root->root_key,
8030b86a832SChris Mason 					&root->root_item);
80449b25e05SJeff Mahoney 		if (ret)
80549b25e05SJeff Mahoney 			return ret;
80656bec294SChris Mason 
80786b9f2ecSYan, Zheng 		old_root_used = btrfs_root_used(&root->root_item);
8084a8c9a62SYan Zheng 		ret = btrfs_write_dirty_block_groups(trans, root);
80949b25e05SJeff Mahoney 		if (ret)
81049b25e05SJeff Mahoney 			return ret;
8110b86a832SChris Mason 	}
812276e680dSYan Zheng 
813276e680dSYan Zheng 	if (root != root->fs_info->extent_root)
814817d52f8SJosef Bacik 		switch_commit_root(root);
815276e680dSYan Zheng 
8160b86a832SChris Mason 	return 0;
8170b86a832SChris Mason }
8180b86a832SChris Mason 
819d352ac68SChris Mason /*
820d352ac68SChris Mason  * update all the cowonly tree roots on disk
82149b25e05SJeff Mahoney  *
82249b25e05SJeff Mahoney  * The error handling in this function may not be obvious. Any of the
82349b25e05SJeff Mahoney  * failures will cause the file system to go offline. We still need
82449b25e05SJeff Mahoney  * to clean up the delayed refs.
825d352ac68SChris Mason  */
8265d4f98a2SYan Zheng static noinline int commit_cowonly_roots(struct btrfs_trans_handle *trans,
8270b86a832SChris Mason 					 struct btrfs_root *root)
8280b86a832SChris Mason {
8290b86a832SChris Mason 	struct btrfs_fs_info *fs_info = root->fs_info;
8300b86a832SChris Mason 	struct list_head *next;
83184234f3aSYan Zheng 	struct extent_buffer *eb;
83256bec294SChris Mason 	int ret;
83384234f3aSYan Zheng 
83456bec294SChris Mason 	ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
83549b25e05SJeff Mahoney 	if (ret)
83649b25e05SJeff Mahoney 		return ret;
83787ef2bb4SChris Mason 
83884234f3aSYan Zheng 	eb = btrfs_lock_root_node(fs_info->tree_root);
83949b25e05SJeff Mahoney 	ret = btrfs_cow_block(trans, fs_info->tree_root, eb, NULL,
84049b25e05SJeff Mahoney 			      0, &eb);
84184234f3aSYan Zheng 	btrfs_tree_unlock(eb);
84284234f3aSYan Zheng 	free_extent_buffer(eb);
8430b86a832SChris Mason 
84449b25e05SJeff Mahoney 	if (ret)
84549b25e05SJeff Mahoney 		return ret;
84649b25e05SJeff Mahoney 
84756bec294SChris Mason 	ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
84849b25e05SJeff Mahoney 	if (ret)
84949b25e05SJeff Mahoney 		return ret;
85087ef2bb4SChris Mason 
851733f4fbbSStefan Behrens 	ret = btrfs_run_dev_stats(trans, root->fs_info);
852733f4fbbSStefan Behrens 	BUG_ON(ret);
853733f4fbbSStefan Behrens 
854546adb0dSJan Schmidt 	ret = btrfs_run_qgroups(trans, root->fs_info);
855546adb0dSJan Schmidt 	BUG_ON(ret);
856546adb0dSJan Schmidt 
857546adb0dSJan Schmidt 	/* run_qgroups might have added some more refs */
858546adb0dSJan Schmidt 	ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
859546adb0dSJan Schmidt 	BUG_ON(ret);
860546adb0dSJan Schmidt 
8610b86a832SChris Mason 	while (!list_empty(&fs_info->dirty_cowonly_roots)) {
8620b86a832SChris Mason 		next = fs_info->dirty_cowonly_roots.next;
8630b86a832SChris Mason 		list_del_init(next);
8640b86a832SChris Mason 		root = list_entry(next, struct btrfs_root, dirty_list);
86587ef2bb4SChris Mason 
86649b25e05SJeff Mahoney 		ret = update_cowonly_root(trans, root);
86749b25e05SJeff Mahoney 		if (ret)
86849b25e05SJeff Mahoney 			return ret;
86979154b1bSChris Mason 	}
870276e680dSYan Zheng 
871276e680dSYan Zheng 	down_write(&fs_info->extent_commit_sem);
872276e680dSYan Zheng 	switch_commit_root(fs_info->extent_root);
873276e680dSYan Zheng 	up_write(&fs_info->extent_commit_sem);
874276e680dSYan Zheng 
87579154b1bSChris Mason 	return 0;
87679154b1bSChris Mason }
87779154b1bSChris Mason 
878d352ac68SChris Mason /*
879d352ac68SChris Mason  * dead roots are old snapshots that need to be deleted.  This allocates
880d352ac68SChris Mason  * a dirty root struct and adds it into the list of dead roots that need to
881d352ac68SChris Mason  * be deleted
882d352ac68SChris Mason  */
8835d4f98a2SYan Zheng int btrfs_add_dead_root(struct btrfs_root *root)
8845eda7b5eSChris Mason {
885a4abeea4SJosef Bacik 	spin_lock(&root->fs_info->trans_lock);
8865d4f98a2SYan Zheng 	list_add(&root->root_list, &root->fs_info->dead_roots);
887a4abeea4SJosef Bacik 	spin_unlock(&root->fs_info->trans_lock);
8885eda7b5eSChris Mason 	return 0;
8895eda7b5eSChris Mason }
8905eda7b5eSChris Mason 
891d352ac68SChris Mason /*
8925d4f98a2SYan Zheng  * update all the cowonly tree roots on disk
893d352ac68SChris Mason  */
8945d4f98a2SYan Zheng static noinline int commit_fs_roots(struct btrfs_trans_handle *trans,
8955d4f98a2SYan Zheng 				    struct btrfs_root *root)
8960f7d52f4SChris Mason {
8970f7d52f4SChris Mason 	struct btrfs_root *gang[8];
8985d4f98a2SYan Zheng 	struct btrfs_fs_info *fs_info = root->fs_info;
8990f7d52f4SChris Mason 	int i;
9000f7d52f4SChris Mason 	int ret;
90154aa1f4dSChris Mason 	int err = 0;
90254aa1f4dSChris Mason 
903a4abeea4SJosef Bacik 	spin_lock(&fs_info->fs_roots_radix_lock);
9040f7d52f4SChris Mason 	while (1) {
9055d4f98a2SYan Zheng 		ret = radix_tree_gang_lookup_tag(&fs_info->fs_roots_radix,
9065d4f98a2SYan Zheng 						 (void **)gang, 0,
9070f7d52f4SChris Mason 						 ARRAY_SIZE(gang),
9080f7d52f4SChris Mason 						 BTRFS_ROOT_TRANS_TAG);
9090f7d52f4SChris Mason 		if (ret == 0)
9100f7d52f4SChris Mason 			break;
9110f7d52f4SChris Mason 		for (i = 0; i < ret; i++) {
9120f7d52f4SChris Mason 			root = gang[i];
9135d4f98a2SYan Zheng 			radix_tree_tag_clear(&fs_info->fs_roots_radix,
9142619ba1fSChris Mason 					(unsigned long)root->root_key.objectid,
9150f7d52f4SChris Mason 					BTRFS_ROOT_TRANS_TAG);
916a4abeea4SJosef Bacik 			spin_unlock(&fs_info->fs_roots_radix_lock);
91731153d81SYan Zheng 
918e02119d5SChris Mason 			btrfs_free_log(trans, root);
9195d4f98a2SYan Zheng 			btrfs_update_reloc_root(trans, root);
920d68fc57bSYan, Zheng 			btrfs_orphan_commit_root(trans, root);
921e02119d5SChris Mason 
92282d5902dSLi Zefan 			btrfs_save_ino_cache(root, trans);
92382d5902dSLi Zefan 
924f1ebcc74SLiu Bo 			/* see comments in should_cow_block() */
925f1ebcc74SLiu Bo 			root->force_cow = 0;
926f1ebcc74SLiu Bo 			smp_wmb();
927f1ebcc74SLiu Bo 
928978d910dSYan Zheng 			if (root->commit_root != root->node) {
929581bb050SLi Zefan 				mutex_lock(&root->fs_commit_mutex);
930817d52f8SJosef Bacik 				switch_commit_root(root);
931581bb050SLi Zefan 				btrfs_unpin_free_ino(root);
932581bb050SLi Zefan 				mutex_unlock(&root->fs_commit_mutex);
933581bb050SLi Zefan 
934978d910dSYan Zheng 				btrfs_set_root_node(&root->root_item,
935978d910dSYan Zheng 						    root->node);
936978d910dSYan Zheng 			}
93731153d81SYan Zheng 
9385d4f98a2SYan Zheng 			err = btrfs_update_root(trans, fs_info->tree_root,
9390f7d52f4SChris Mason 						&root->root_key,
9400f7d52f4SChris Mason 						&root->root_item);
941a4abeea4SJosef Bacik 			spin_lock(&fs_info->fs_roots_radix_lock);
94254aa1f4dSChris Mason 			if (err)
94354aa1f4dSChris Mason 				break;
9440f7d52f4SChris Mason 		}
9459f3a7427SChris Mason 	}
946a4abeea4SJosef Bacik 	spin_unlock(&fs_info->fs_roots_radix_lock);
94754aa1f4dSChris Mason 	return err;
9480f7d52f4SChris Mason }
9490f7d52f4SChris Mason 
950d352ac68SChris Mason /*
951d352ac68SChris Mason  * defrag a given btree.  If cacheonly == 1, this won't read from the disk,
952d352ac68SChris Mason  * otherwise every leaf in the btree is read and defragged.
953d352ac68SChris Mason  */
954e9d0b13bSChris Mason int btrfs_defrag_root(struct btrfs_root *root, int cacheonly)
955e9d0b13bSChris Mason {
956e9d0b13bSChris Mason 	struct btrfs_fs_info *info = root->fs_info;
957e9d0b13bSChris Mason 	struct btrfs_trans_handle *trans;
9588929ecfaSYan, Zheng 	int ret;
959d3c2fdcfSChris Mason 	unsigned long nr;
960e9d0b13bSChris Mason 
9618929ecfaSYan, Zheng 	if (xchg(&root->defrag_running, 1))
962e9d0b13bSChris Mason 		return 0;
9638929ecfaSYan, Zheng 
9646b80053dSChris Mason 	while (1) {
9658929ecfaSYan, Zheng 		trans = btrfs_start_transaction(root, 0);
9668929ecfaSYan, Zheng 		if (IS_ERR(trans))
9678929ecfaSYan, Zheng 			return PTR_ERR(trans);
9688929ecfaSYan, Zheng 
969e9d0b13bSChris Mason 		ret = btrfs_defrag_leaves(trans, root, cacheonly);
9708929ecfaSYan, Zheng 
971d3c2fdcfSChris Mason 		nr = trans->blocks_used;
972e9d0b13bSChris Mason 		btrfs_end_transaction(trans, root);
973d3c2fdcfSChris Mason 		btrfs_btree_balance_dirty(info->tree_root, nr);
974e9d0b13bSChris Mason 		cond_resched();
975e9d0b13bSChris Mason 
9767841cb28SDavid Sterba 		if (btrfs_fs_closing(root->fs_info) || ret != -EAGAIN)
977e9d0b13bSChris Mason 			break;
978e9d0b13bSChris Mason 	}
979e9d0b13bSChris Mason 	root->defrag_running = 0;
9808929ecfaSYan, Zheng 	return ret;
981e9d0b13bSChris Mason }
982e9d0b13bSChris Mason 
983d352ac68SChris Mason /*
984d352ac68SChris Mason  * new snapshots need to be created at a very specific time in the
985d352ac68SChris Mason  * transaction commit.  This does the actual creation
986d352ac68SChris Mason  */
98780b6794dSChris Mason static noinline int create_pending_snapshot(struct btrfs_trans_handle *trans,
9883063d29fSChris Mason 				   struct btrfs_fs_info *fs_info,
9893063d29fSChris Mason 				   struct btrfs_pending_snapshot *pending)
9903063d29fSChris Mason {
9913063d29fSChris Mason 	struct btrfs_key key;
99280b6794dSChris Mason 	struct btrfs_root_item *new_root_item;
9933063d29fSChris Mason 	struct btrfs_root *tree_root = fs_info->tree_root;
9943063d29fSChris Mason 	struct btrfs_root *root = pending->root;
9956bdb72deSSage Weil 	struct btrfs_root *parent_root;
99698c9942aSLiu Bo 	struct btrfs_block_rsv *rsv;
9976bdb72deSSage Weil 	struct inode *parent_inode;
99842874b3dSMiao Xie 	struct btrfs_path *path;
99942874b3dSMiao Xie 	struct btrfs_dir_item *dir_item;
10006a912213SJosef Bacik 	struct dentry *parent;
1001a22285a6SYan, Zheng 	struct dentry *dentry;
10023063d29fSChris Mason 	struct extent_buffer *tmp;
1003925baeddSChris Mason 	struct extent_buffer *old;
10048ea05e3aSAlexander Block 	struct timespec cur_time = CURRENT_TIME;
10053063d29fSChris Mason 	int ret;
1006d68fc57bSYan, Zheng 	u64 to_reserve = 0;
10076bdb72deSSage Weil 	u64 index = 0;
1008a22285a6SYan, Zheng 	u64 objectid;
1009b83cc969SLi Zefan 	u64 root_flags;
10108ea05e3aSAlexander Block 	uuid_le new_uuid;
10113063d29fSChris Mason 
101242874b3dSMiao Xie 	path = btrfs_alloc_path();
101342874b3dSMiao Xie 	if (!path) {
101442874b3dSMiao Xie 		ret = pending->error = -ENOMEM;
101542874b3dSMiao Xie 		goto path_alloc_fail;
101642874b3dSMiao Xie 	}
101742874b3dSMiao Xie 
101880b6794dSChris Mason 	new_root_item = kmalloc(sizeof(*new_root_item), GFP_NOFS);
101980b6794dSChris Mason 	if (!new_root_item) {
102049b25e05SJeff Mahoney 		ret = pending->error = -ENOMEM;
10216fa9700eSMiao Xie 		goto root_item_alloc_fail;
102280b6794dSChris Mason 	}
1023a22285a6SYan, Zheng 
1024581bb050SLi Zefan 	ret = btrfs_find_free_objectid(tree_root, &objectid);
1025a22285a6SYan, Zheng 	if (ret) {
1026a22285a6SYan, Zheng 		pending->error = ret;
10276fa9700eSMiao Xie 		goto no_free_objectid;
1028a22285a6SYan, Zheng 	}
10293063d29fSChris Mason 
10303fd0a558SYan, Zheng 	btrfs_reloc_pre_snapshot(trans, pending, &to_reserve);
1031d68fc57bSYan, Zheng 
1032d68fc57bSYan, Zheng 	if (to_reserve > 0) {
103308e007d2SMiao Xie 		ret = btrfs_block_rsv_add(root, &pending->block_rsv,
103408e007d2SMiao Xie 					  to_reserve,
103508e007d2SMiao Xie 					  BTRFS_RESERVE_NO_FLUSH);
1036d68fc57bSYan, Zheng 		if (ret) {
1037d68fc57bSYan, Zheng 			pending->error = ret;
10386fa9700eSMiao Xie 			goto no_free_objectid;
1039d68fc57bSYan, Zheng 		}
1040d68fc57bSYan, Zheng 	}
1041d68fc57bSYan, Zheng 
10426f72c7e2SArne Jansen 	ret = btrfs_qgroup_inherit(trans, fs_info, root->root_key.objectid,
10436f72c7e2SArne Jansen 				   objectid, pending->inherit);
10446f72c7e2SArne Jansen 	if (ret) {
10456f72c7e2SArne Jansen 		pending->error = ret;
10466fa9700eSMiao Xie 		goto no_free_objectid;
10476f72c7e2SArne Jansen 	}
10486f72c7e2SArne Jansen 
10493063d29fSChris Mason 	key.objectid = objectid;
1050a22285a6SYan, Zheng 	key.offset = (u64)-1;
1051a22285a6SYan, Zheng 	key.type = BTRFS_ROOT_ITEM_KEY;
10523063d29fSChris Mason 
10536fa9700eSMiao Xie 	rsv = trans->block_rsv;
1054a22285a6SYan, Zheng 	trans->block_rsv = &pending->block_rsv;
10556bdb72deSSage Weil 
1056a22285a6SYan, Zheng 	dentry = pending->dentry;
10576a912213SJosef Bacik 	parent = dget_parent(dentry);
10586a912213SJosef Bacik 	parent_inode = parent->d_inode;
1059a22285a6SYan, Zheng 	parent_root = BTRFS_I(parent_inode)->root;
10607585717fSChris Mason 	record_root_in_trans(trans, parent_root);
1061a22285a6SYan, Zheng 
10626bdb72deSSage Weil 	/*
10636bdb72deSSage Weil 	 * insert the directory item
10646bdb72deSSage Weil 	 */
10656bdb72deSSage Weil 	ret = btrfs_set_inode_index(parent_inode, &index);
106649b25e05SJeff Mahoney 	BUG_ON(ret); /* -ENOMEM */
106742874b3dSMiao Xie 
106842874b3dSMiao Xie 	/* check if there is a file/dir which has the same name. */
106942874b3dSMiao Xie 	dir_item = btrfs_lookup_dir_item(NULL, parent_root, path,
107042874b3dSMiao Xie 					 btrfs_ino(parent_inode),
107142874b3dSMiao Xie 					 dentry->d_name.name,
107242874b3dSMiao Xie 					 dentry->d_name.len, 0);
107342874b3dSMiao Xie 	if (dir_item != NULL && !IS_ERR(dir_item)) {
1074fe66a05aSChris Mason 		pending->error = -EEXIST;
1075fe66a05aSChris Mason 		goto fail;
107642874b3dSMiao Xie 	} else if (IS_ERR(dir_item)) {
107742874b3dSMiao Xie 		ret = PTR_ERR(dir_item);
10788732d44fSMiao Xie 		btrfs_abort_transaction(trans, root, ret);
10798732d44fSMiao Xie 		goto fail;
108079787eaaSJeff Mahoney 	}
108142874b3dSMiao Xie 	btrfs_release_path(path);
10826bdb72deSSage Weil 
1083e999376fSChris Mason 	/*
1084e999376fSChris Mason 	 * pull in the delayed directory update
1085e999376fSChris Mason 	 * and the delayed inode item
1086e999376fSChris Mason 	 * otherwise we corrupt the FS during
1087e999376fSChris Mason 	 * snapshot
1088e999376fSChris Mason 	 */
1089e999376fSChris Mason 	ret = btrfs_run_delayed_items(trans, root);
10908732d44fSMiao Xie 	if (ret) {	/* Transaction aborted */
10918732d44fSMiao Xie 		btrfs_abort_transaction(trans, root, ret);
10928732d44fSMiao Xie 		goto fail;
10938732d44fSMiao Xie 	}
1094e999376fSChris Mason 
10957585717fSChris Mason 	record_root_in_trans(trans, root);
10966bdb72deSSage Weil 	btrfs_set_root_last_snapshot(&root->root_item, trans->transid);
10976bdb72deSSage Weil 	memcpy(new_root_item, &root->root_item, sizeof(*new_root_item));
109808fe4db1SLi Zefan 	btrfs_check_and_init_root_item(new_root_item);
10996bdb72deSSage Weil 
1100b83cc969SLi Zefan 	root_flags = btrfs_root_flags(new_root_item);
1101b83cc969SLi Zefan 	if (pending->readonly)
1102b83cc969SLi Zefan 		root_flags |= BTRFS_ROOT_SUBVOL_RDONLY;
1103b83cc969SLi Zefan 	else
1104b83cc969SLi Zefan 		root_flags &= ~BTRFS_ROOT_SUBVOL_RDONLY;
1105b83cc969SLi Zefan 	btrfs_set_root_flags(new_root_item, root_flags);
1106b83cc969SLi Zefan 
11078ea05e3aSAlexander Block 	btrfs_set_root_generation_v2(new_root_item,
11088ea05e3aSAlexander Block 			trans->transid);
11098ea05e3aSAlexander Block 	uuid_le_gen(&new_uuid);
11108ea05e3aSAlexander Block 	memcpy(new_root_item->uuid, new_uuid.b, BTRFS_UUID_SIZE);
11118ea05e3aSAlexander Block 	memcpy(new_root_item->parent_uuid, root->root_item.uuid,
11128ea05e3aSAlexander Block 			BTRFS_UUID_SIZE);
11138ea05e3aSAlexander Block 	new_root_item->otime.sec = cpu_to_le64(cur_time.tv_sec);
1114dadd1105SDan Carpenter 	new_root_item->otime.nsec = cpu_to_le32(cur_time.tv_nsec);
11158ea05e3aSAlexander Block 	btrfs_set_root_otransid(new_root_item, trans->transid);
11168ea05e3aSAlexander Block 	memset(&new_root_item->stime, 0, sizeof(new_root_item->stime));
11178ea05e3aSAlexander Block 	memset(&new_root_item->rtime, 0, sizeof(new_root_item->rtime));
11188ea05e3aSAlexander Block 	btrfs_set_root_stransid(new_root_item, 0);
11198ea05e3aSAlexander Block 	btrfs_set_root_rtransid(new_root_item, 0);
11208ea05e3aSAlexander Block 
1121925baeddSChris Mason 	old = btrfs_lock_root_node(root);
112249b25e05SJeff Mahoney 	ret = btrfs_cow_block(trans, root, old, NULL, 0, &old);
112379787eaaSJeff Mahoney 	if (ret) {
112479787eaaSJeff Mahoney 		btrfs_tree_unlock(old);
112579787eaaSJeff Mahoney 		free_extent_buffer(old);
11268732d44fSMiao Xie 		btrfs_abort_transaction(trans, root, ret);
11278732d44fSMiao Xie 		goto fail;
112879787eaaSJeff Mahoney 	}
112949b25e05SJeff Mahoney 
11305d4f98a2SYan Zheng 	btrfs_set_lock_blocking(old);
11313063d29fSChris Mason 
113249b25e05SJeff Mahoney 	ret = btrfs_copy_root(trans, root, old, &tmp, objectid);
113379787eaaSJeff Mahoney 	/* clean up in any case */
1134925baeddSChris Mason 	btrfs_tree_unlock(old);
1135925baeddSChris Mason 	free_extent_buffer(old);
11368732d44fSMiao Xie 	if (ret) {
11378732d44fSMiao Xie 		btrfs_abort_transaction(trans, root, ret);
11388732d44fSMiao Xie 		goto fail;
11398732d44fSMiao Xie 	}
11403063d29fSChris Mason 
1141f1ebcc74SLiu Bo 	/* see comments in should_cow_block() */
1142f1ebcc74SLiu Bo 	root->force_cow = 1;
1143f1ebcc74SLiu Bo 	smp_wmb();
1144f1ebcc74SLiu Bo 
11455d4f98a2SYan Zheng 	btrfs_set_root_node(new_root_item, tmp);
1146a22285a6SYan, Zheng 	/* record when the snapshot was created in key.offset */
1147a22285a6SYan, Zheng 	key.offset = trans->transid;
1148a22285a6SYan, Zheng 	ret = btrfs_insert_root(trans, tree_root, &key, new_root_item);
1149925baeddSChris Mason 	btrfs_tree_unlock(tmp);
11503063d29fSChris Mason 	free_extent_buffer(tmp);
11518732d44fSMiao Xie 	if (ret) {
11528732d44fSMiao Xie 		btrfs_abort_transaction(trans, root, ret);
11538732d44fSMiao Xie 		goto fail;
11548732d44fSMiao Xie 	}
11550660b5afSChris Mason 
1156a22285a6SYan, Zheng 	/*
1157a22285a6SYan, Zheng 	 * insert root back/forward references
1158a22285a6SYan, Zheng 	 */
1159a22285a6SYan, Zheng 	ret = btrfs_add_root_ref(trans, tree_root, objectid,
1160a22285a6SYan, Zheng 				 parent_root->root_key.objectid,
116133345d01SLi Zefan 				 btrfs_ino(parent_inode), index,
1162a22285a6SYan, Zheng 				 dentry->d_name.name, dentry->d_name.len);
11638732d44fSMiao Xie 	if (ret) {
11648732d44fSMiao Xie 		btrfs_abort_transaction(trans, root, ret);
11658732d44fSMiao Xie 		goto fail;
11668732d44fSMiao Xie 	}
1167a22285a6SYan, Zheng 
1168a22285a6SYan, Zheng 	key.offset = (u64)-1;
1169a22285a6SYan, Zheng 	pending->snap = btrfs_read_fs_root_no_name(root->fs_info, &key);
117079787eaaSJeff Mahoney 	if (IS_ERR(pending->snap)) {
117179787eaaSJeff Mahoney 		ret = PTR_ERR(pending->snap);
11728732d44fSMiao Xie 		btrfs_abort_transaction(trans, root, ret);
11738732d44fSMiao Xie 		goto fail;
117479787eaaSJeff Mahoney 	}
1175d68fc57bSYan, Zheng 
117649b25e05SJeff Mahoney 	ret = btrfs_reloc_post_snapshot(trans, pending);
11778732d44fSMiao Xie 	if (ret) {
11788732d44fSMiao Xie 		btrfs_abort_transaction(trans, root, ret);
11798732d44fSMiao Xie 		goto fail;
11808732d44fSMiao Xie 	}
1181361048f5SMiao Xie 
1182361048f5SMiao Xie 	ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
11838732d44fSMiao Xie 	if (ret) {
11848732d44fSMiao Xie 		btrfs_abort_transaction(trans, root, ret);
11858732d44fSMiao Xie 		goto fail;
11868732d44fSMiao Xie 	}
118742874b3dSMiao Xie 
118842874b3dSMiao Xie 	ret = btrfs_insert_dir_item(trans, parent_root,
118942874b3dSMiao Xie 				    dentry->d_name.name, dentry->d_name.len,
119042874b3dSMiao Xie 				    parent_inode, &key,
119142874b3dSMiao Xie 				    BTRFS_FT_DIR, index);
119242874b3dSMiao Xie 	/* We have check then name at the beginning, so it is impossible. */
119342874b3dSMiao Xie 	BUG_ON(ret == -EEXIST);
11948732d44fSMiao Xie 	if (ret) {
11958732d44fSMiao Xie 		btrfs_abort_transaction(trans, root, ret);
11968732d44fSMiao Xie 		goto fail;
11978732d44fSMiao Xie 	}
119842874b3dSMiao Xie 
119942874b3dSMiao Xie 	btrfs_i_size_write(parent_inode, parent_inode->i_size +
120042874b3dSMiao Xie 					 dentry->d_name.len * 2);
120142874b3dSMiao Xie 	parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
1202be6aef60SJosef Bacik 	ret = btrfs_update_inode_fallback(trans, parent_root, parent_inode);
120342874b3dSMiao Xie 	if (ret)
12048732d44fSMiao Xie 		btrfs_abort_transaction(trans, root, ret);
12053063d29fSChris Mason fail:
12066fa9700eSMiao Xie 	dput(parent);
120798c9942aSLiu Bo 	trans->block_rsv = rsv;
12086fa9700eSMiao Xie no_free_objectid:
12096fa9700eSMiao Xie 	kfree(new_root_item);
12106fa9700eSMiao Xie root_item_alloc_fail:
121142874b3dSMiao Xie 	btrfs_free_path(path);
121242874b3dSMiao Xie path_alloc_fail:
1213a22285a6SYan, Zheng 	btrfs_block_rsv_release(root, &pending->block_rsv, (u64)-1);
121449b25e05SJeff Mahoney 	return ret;
12153063d29fSChris Mason }
12163063d29fSChris Mason 
1217d352ac68SChris Mason /*
1218d352ac68SChris Mason  * create all the snapshots we've scheduled for creation
1219d352ac68SChris Mason  */
122080b6794dSChris Mason static noinline int create_pending_snapshots(struct btrfs_trans_handle *trans,
12213063d29fSChris Mason 					     struct btrfs_fs_info *fs_info)
12223063d29fSChris Mason {
12233063d29fSChris Mason 	struct btrfs_pending_snapshot *pending;
12243063d29fSChris Mason 	struct list_head *head = &trans->transaction->pending_snapshots;
12253de4586cSChris Mason 
1226fe66a05aSChris Mason 	list_for_each_entry(pending, head, list)
1227fe66a05aSChris Mason 		create_pending_snapshot(trans, fs_info, pending);
12283de4586cSChris Mason 	return 0;
12293de4586cSChris Mason }
12303de4586cSChris Mason 
12315d4f98a2SYan Zheng static void update_super_roots(struct btrfs_root *root)
12325d4f98a2SYan Zheng {
12335d4f98a2SYan Zheng 	struct btrfs_root_item *root_item;
12345d4f98a2SYan Zheng 	struct btrfs_super_block *super;
12355d4f98a2SYan Zheng 
12366c41761fSDavid Sterba 	super = root->fs_info->super_copy;
12375d4f98a2SYan Zheng 
12385d4f98a2SYan Zheng 	root_item = &root->fs_info->chunk_root->root_item;
12395d4f98a2SYan Zheng 	super->chunk_root = root_item->bytenr;
12405d4f98a2SYan Zheng 	super->chunk_root_generation = root_item->generation;
12415d4f98a2SYan Zheng 	super->chunk_root_level = root_item->level;
12425d4f98a2SYan Zheng 
12435d4f98a2SYan Zheng 	root_item = &root->fs_info->tree_root->root_item;
12445d4f98a2SYan Zheng 	super->root = root_item->bytenr;
12455d4f98a2SYan Zheng 	super->generation = root_item->generation;
12465d4f98a2SYan Zheng 	super->root_level = root_item->level;
124773bc1876SJosef Bacik 	if (btrfs_test_opt(root, SPACE_CACHE))
12480af3d00bSJosef Bacik 		super->cache_generation = root_item->generation;
12495d4f98a2SYan Zheng }
12505d4f98a2SYan Zheng 
1251f36f3042SChris Mason int btrfs_transaction_in_commit(struct btrfs_fs_info *info)
1252f36f3042SChris Mason {
1253f36f3042SChris Mason 	int ret = 0;
1254a4abeea4SJosef Bacik 	spin_lock(&info->trans_lock);
1255f36f3042SChris Mason 	if (info->running_transaction)
1256f36f3042SChris Mason 		ret = info->running_transaction->in_commit;
1257a4abeea4SJosef Bacik 	spin_unlock(&info->trans_lock);
1258f36f3042SChris Mason 	return ret;
1259f36f3042SChris Mason }
1260f36f3042SChris Mason 
12618929ecfaSYan, Zheng int btrfs_transaction_blocked(struct btrfs_fs_info *info)
12628929ecfaSYan, Zheng {
12638929ecfaSYan, Zheng 	int ret = 0;
1264a4abeea4SJosef Bacik 	spin_lock(&info->trans_lock);
12658929ecfaSYan, Zheng 	if (info->running_transaction)
12668929ecfaSYan, Zheng 		ret = info->running_transaction->blocked;
1267a4abeea4SJosef Bacik 	spin_unlock(&info->trans_lock);
12688929ecfaSYan, Zheng 	return ret;
12698929ecfaSYan, Zheng }
12708929ecfaSYan, Zheng 
1271bb9c12c9SSage Weil /*
1272bb9c12c9SSage Weil  * wait for the current transaction commit to start and block subsequent
1273bb9c12c9SSage Weil  * transaction joins
1274bb9c12c9SSage Weil  */
1275bb9c12c9SSage Weil static void wait_current_trans_commit_start(struct btrfs_root *root,
1276bb9c12c9SSage Weil 					    struct btrfs_transaction *trans)
1277bb9c12c9SSage Weil {
127872d63ed6SLi Zefan 	wait_event(root->fs_info->transaction_blocked_wait, trans->in_commit);
1279bb9c12c9SSage Weil }
1280bb9c12c9SSage Weil 
1281bb9c12c9SSage Weil /*
1282bb9c12c9SSage Weil  * wait for the current transaction to start and then become unblocked.
1283bb9c12c9SSage Weil  * caller holds ref.
1284bb9c12c9SSage Weil  */
1285bb9c12c9SSage Weil static void wait_current_trans_commit_start_and_unblock(struct btrfs_root *root,
1286bb9c12c9SSage Weil 					 struct btrfs_transaction *trans)
1287bb9c12c9SSage Weil {
128872d63ed6SLi Zefan 	wait_event(root->fs_info->transaction_wait,
128972d63ed6SLi Zefan 		   trans->commit_done || (trans->in_commit && !trans->blocked));
1290bb9c12c9SSage Weil }
1291bb9c12c9SSage Weil 
1292bb9c12c9SSage Weil /*
1293bb9c12c9SSage Weil  * commit transactions asynchronously. once btrfs_commit_transaction_async
1294bb9c12c9SSage Weil  * returns, any subsequent transaction will not be allowed to join.
1295bb9c12c9SSage Weil  */
1296bb9c12c9SSage Weil struct btrfs_async_commit {
1297bb9c12c9SSage Weil 	struct btrfs_trans_handle *newtrans;
1298bb9c12c9SSage Weil 	struct btrfs_root *root;
1299bb9c12c9SSage Weil 	struct delayed_work work;
1300bb9c12c9SSage Weil };
1301bb9c12c9SSage Weil 
1302bb9c12c9SSage Weil static void do_async_commit(struct work_struct *work)
1303bb9c12c9SSage Weil {
1304bb9c12c9SSage Weil 	struct btrfs_async_commit *ac =
1305bb9c12c9SSage Weil 		container_of(work, struct btrfs_async_commit, work.work);
1306bb9c12c9SSage Weil 
13076fc4e354SSage Weil 	/*
13086fc4e354SSage Weil 	 * We've got freeze protection passed with the transaction.
13096fc4e354SSage Weil 	 * Tell lockdep about it.
13106fc4e354SSage Weil 	 */
13116fc4e354SSage Weil 	rwsem_acquire_read(
13126fc4e354SSage Weil 		&ac->root->fs_info->sb->s_writers.lock_map[SB_FREEZE_FS-1],
13136fc4e354SSage Weil 		0, 1, _THIS_IP_);
13146fc4e354SSage Weil 
1315e209db7aSSage Weil 	current->journal_info = ac->newtrans;
1316e209db7aSSage Weil 
1317bb9c12c9SSage Weil 	btrfs_commit_transaction(ac->newtrans, ac->root);
1318bb9c12c9SSage Weil 	kfree(ac);
1319bb9c12c9SSage Weil }
1320bb9c12c9SSage Weil 
1321bb9c12c9SSage Weil int btrfs_commit_transaction_async(struct btrfs_trans_handle *trans,
1322bb9c12c9SSage Weil 				   struct btrfs_root *root,
1323bb9c12c9SSage Weil 				   int wait_for_unblock)
1324bb9c12c9SSage Weil {
1325bb9c12c9SSage Weil 	struct btrfs_async_commit *ac;
1326bb9c12c9SSage Weil 	struct btrfs_transaction *cur_trans;
1327bb9c12c9SSage Weil 
1328bb9c12c9SSage Weil 	ac = kmalloc(sizeof(*ac), GFP_NOFS);
1329db5b493aSTsutomu Itoh 	if (!ac)
1330db5b493aSTsutomu Itoh 		return -ENOMEM;
1331bb9c12c9SSage Weil 
1332bb9c12c9SSage Weil 	INIT_DELAYED_WORK(&ac->work, do_async_commit);
1333bb9c12c9SSage Weil 	ac->root = root;
13347a7eaa40SJosef Bacik 	ac->newtrans = btrfs_join_transaction(root);
13353612b495STsutomu Itoh 	if (IS_ERR(ac->newtrans)) {
13363612b495STsutomu Itoh 		int err = PTR_ERR(ac->newtrans);
13373612b495STsutomu Itoh 		kfree(ac);
13383612b495STsutomu Itoh 		return err;
13393612b495STsutomu Itoh 	}
1340bb9c12c9SSage Weil 
1341bb9c12c9SSage Weil 	/* take transaction reference */
1342bb9c12c9SSage Weil 	cur_trans = trans->transaction;
134313c5a93eSJosef Bacik 	atomic_inc(&cur_trans->use_count);
1344bb9c12c9SSage Weil 
1345bb9c12c9SSage Weil 	btrfs_end_transaction(trans, root);
13466fc4e354SSage Weil 
13476fc4e354SSage Weil 	/*
13486fc4e354SSage Weil 	 * Tell lockdep we've released the freeze rwsem, since the
13496fc4e354SSage Weil 	 * async commit thread will be the one to unlock it.
13506fc4e354SSage Weil 	 */
13516fc4e354SSage Weil 	rwsem_release(&root->fs_info->sb->s_writers.lock_map[SB_FREEZE_FS-1],
13526fc4e354SSage Weil 		      1, _THIS_IP_);
13536fc4e354SSage Weil 
1354bb9c12c9SSage Weil 	schedule_delayed_work(&ac->work, 0);
1355bb9c12c9SSage Weil 
1356bb9c12c9SSage Weil 	/* wait for transaction to start and unblock */
1357bb9c12c9SSage Weil 	if (wait_for_unblock)
1358bb9c12c9SSage Weil 		wait_current_trans_commit_start_and_unblock(root, cur_trans);
1359bb9c12c9SSage Weil 	else
1360bb9c12c9SSage Weil 		wait_current_trans_commit_start(root, cur_trans);
1361bb9c12c9SSage Weil 
136238e88054SSage Weil 	if (current->journal_info == trans)
136338e88054SSage Weil 		current->journal_info = NULL;
136438e88054SSage Weil 
136538e88054SSage Weil 	put_transaction(cur_trans);
1366bb9c12c9SSage Weil 	return 0;
1367bb9c12c9SSage Weil }
1368bb9c12c9SSage Weil 
136949b25e05SJeff Mahoney 
137049b25e05SJeff Mahoney static void cleanup_transaction(struct btrfs_trans_handle *trans,
13717b8b92afSJosef Bacik 				struct btrfs_root *root, int err)
137249b25e05SJeff Mahoney {
137349b25e05SJeff Mahoney 	struct btrfs_transaction *cur_trans = trans->transaction;
137449b25e05SJeff Mahoney 
137549b25e05SJeff Mahoney 	WARN_ON(trans->use_count > 1);
137649b25e05SJeff Mahoney 
13777b8b92afSJosef Bacik 	btrfs_abort_transaction(trans, root, err);
13787b8b92afSJosef Bacik 
137949b25e05SJeff Mahoney 	spin_lock(&root->fs_info->trans_lock);
138049b25e05SJeff Mahoney 	list_del_init(&cur_trans->list);
1381d7096fc3SJosef Bacik 	if (cur_trans == root->fs_info->running_transaction) {
1382d7096fc3SJosef Bacik 		root->fs_info->running_transaction = NULL;
1383d7096fc3SJosef Bacik 		root->fs_info->trans_no_join = 0;
1384d7096fc3SJosef Bacik 	}
138549b25e05SJeff Mahoney 	spin_unlock(&root->fs_info->trans_lock);
138649b25e05SJeff Mahoney 
138749b25e05SJeff Mahoney 	btrfs_cleanup_one_transaction(trans->transaction, root);
138849b25e05SJeff Mahoney 
138949b25e05SJeff Mahoney 	put_transaction(cur_trans);
139049b25e05SJeff Mahoney 	put_transaction(cur_trans);
139149b25e05SJeff Mahoney 
139249b25e05SJeff Mahoney 	trace_btrfs_transaction_commit(root);
139349b25e05SJeff Mahoney 
139449b25e05SJeff Mahoney 	btrfs_scrub_continue(root);
139549b25e05SJeff Mahoney 
139649b25e05SJeff Mahoney 	if (current->journal_info == trans)
139749b25e05SJeff Mahoney 		current->journal_info = NULL;
139849b25e05SJeff Mahoney 
139949b25e05SJeff Mahoney 	kmem_cache_free(btrfs_trans_handle_cachep, trans);
140049b25e05SJeff Mahoney }
140149b25e05SJeff Mahoney 
1402ca469637SMiao Xie static int btrfs_flush_all_pending_stuffs(struct btrfs_trans_handle *trans,
1403ca469637SMiao Xie 					  struct btrfs_root *root)
1404ca469637SMiao Xie {
1405ca469637SMiao Xie 	int flush_on_commit = btrfs_test_opt(root, FLUSHONCOMMIT);
1406ca469637SMiao Xie 	int snap_pending = 0;
1407ca469637SMiao Xie 	int ret;
1408ca469637SMiao Xie 
1409ca469637SMiao Xie 	if (!flush_on_commit) {
1410ca469637SMiao Xie 		spin_lock(&root->fs_info->trans_lock);
1411ca469637SMiao Xie 		if (!list_empty(&trans->transaction->pending_snapshots))
1412ca469637SMiao Xie 			snap_pending = 1;
1413ca469637SMiao Xie 		spin_unlock(&root->fs_info->trans_lock);
1414ca469637SMiao Xie 	}
1415ca469637SMiao Xie 
1416ca469637SMiao Xie 	if (flush_on_commit || snap_pending) {
1417ca469637SMiao Xie 		btrfs_start_delalloc_inodes(root, 1);
1418ca469637SMiao Xie 		btrfs_wait_ordered_extents(root, 1);
1419ca469637SMiao Xie 	}
1420ca469637SMiao Xie 
1421ca469637SMiao Xie 	ret = btrfs_run_delayed_items(trans, root);
1422ca469637SMiao Xie 	if (ret)
1423ca469637SMiao Xie 		return ret;
1424ca469637SMiao Xie 
1425ca469637SMiao Xie 	/*
1426ca469637SMiao Xie 	 * running the delayed items may have added new refs. account
1427ca469637SMiao Xie 	 * them now so that they hinder processing of more delayed refs
1428ca469637SMiao Xie 	 * as little as possible.
1429ca469637SMiao Xie 	 */
1430ca469637SMiao Xie 	btrfs_delayed_refs_qgroup_accounting(trans, root->fs_info);
1431ca469637SMiao Xie 
1432ca469637SMiao Xie 	/*
1433ca469637SMiao Xie 	 * rename don't use btrfs_join_transaction, so, once we
1434ca469637SMiao Xie 	 * set the transaction to blocked above, we aren't going
1435ca469637SMiao Xie 	 * to get any new ordered operations.  We can safely run
1436ca469637SMiao Xie 	 * it here and no for sure that nothing new will be added
1437ca469637SMiao Xie 	 * to the list
1438ca469637SMiao Xie 	 */
1439ca469637SMiao Xie 	btrfs_run_ordered_operations(root, 1);
1440ca469637SMiao Xie 
1441ca469637SMiao Xie 	return 0;
1442ca469637SMiao Xie }
1443ca469637SMiao Xie 
1444bb9c12c9SSage Weil /*
1445bb9c12c9SSage Weil  * btrfs_transaction state sequence:
1446bb9c12c9SSage Weil  *    in_commit = 0, blocked = 0  (initial)
1447bb9c12c9SSage Weil  *    in_commit = 1, blocked = 1
1448bb9c12c9SSage Weil  *    blocked = 0
1449bb9c12c9SSage Weil  *    commit_done = 1
1450bb9c12c9SSage Weil  */
145179154b1bSChris Mason int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
145279154b1bSChris Mason 			     struct btrfs_root *root)
145379154b1bSChris Mason {
145415ee9bc7SJosef Bacik 	unsigned long joined = 0;
145549b25e05SJeff Mahoney 	struct btrfs_transaction *cur_trans = trans->transaction;
14568fd17795SChris Mason 	struct btrfs_transaction *prev_trans = NULL;
145779154b1bSChris Mason 	DEFINE_WAIT(wait);
145825287e0aSMiao Xie 	int ret;
145989573b9cSChris Mason 	int should_grow = 0;
146089573b9cSChris Mason 	unsigned long now = get_seconds();
146179154b1bSChris Mason 
146225287e0aSMiao Xie 	ret = btrfs_run_ordered_operations(root, 0);
146325287e0aSMiao Xie 	if (ret) {
146425287e0aSMiao Xie 		btrfs_abort_transaction(trans, root, ret);
146549b25e05SJeff Mahoney 		goto cleanup_transaction;
146625287e0aSMiao Xie 	}
146725287e0aSMiao Xie 
146825287e0aSMiao Xie 	if (cur_trans->aborted) {
146925287e0aSMiao Xie 		ret = cur_trans->aborted;
147025287e0aSMiao Xie 		goto cleanup_transaction;
147125287e0aSMiao Xie 	}
147249b25e05SJeff Mahoney 
147356bec294SChris Mason 	/* make a pass through all the delayed refs we have so far
147456bec294SChris Mason 	 * any runnings procs may add more while we are here
147556bec294SChris Mason 	 */
147656bec294SChris Mason 	ret = btrfs_run_delayed_refs(trans, root, 0);
147749b25e05SJeff Mahoney 	if (ret)
147849b25e05SJeff Mahoney 		goto cleanup_transaction;
147956bec294SChris Mason 
14800e721106SJosef Bacik 	btrfs_trans_release_metadata(trans, root);
14810e721106SJosef Bacik 	trans->block_rsv = NULL;
14820e721106SJosef Bacik 
1483b7ec40d7SChris Mason 	cur_trans = trans->transaction;
148449b25e05SJeff Mahoney 
148556bec294SChris Mason 	/*
148656bec294SChris Mason 	 * set the flushing flag so procs in this transaction have to
148756bec294SChris Mason 	 * start sending their work down.
148856bec294SChris Mason 	 */
1489b7ec40d7SChris Mason 	cur_trans->delayed_refs.flushing = 1;
149056bec294SChris Mason 
1491ea658badSJosef Bacik 	if (!list_empty(&trans->new_bgs))
1492ea658badSJosef Bacik 		btrfs_create_pending_block_groups(trans, root);
1493ea658badSJosef Bacik 
1494c3e69d58SChris Mason 	ret = btrfs_run_delayed_refs(trans, root, 0);
149549b25e05SJeff Mahoney 	if (ret)
149649b25e05SJeff Mahoney 		goto cleanup_transaction;
149756bec294SChris Mason 
1498a4abeea4SJosef Bacik 	spin_lock(&cur_trans->commit_lock);
1499b7ec40d7SChris Mason 	if (cur_trans->in_commit) {
1500a4abeea4SJosef Bacik 		spin_unlock(&cur_trans->commit_lock);
150113c5a93eSJosef Bacik 		atomic_inc(&cur_trans->use_count);
150249b25e05SJeff Mahoney 		ret = btrfs_end_transaction(trans, root);
1503ccd467d6SChris Mason 
1504b9c8300cSLi Zefan 		wait_for_commit(root, cur_trans);
150515ee9bc7SJosef Bacik 
150679154b1bSChris Mason 		put_transaction(cur_trans);
150715ee9bc7SJosef Bacik 
150849b25e05SJeff Mahoney 		return ret;
150979154b1bSChris Mason 	}
15104313b399SChris Mason 
15112c90e5d6SChris Mason 	trans->transaction->in_commit = 1;
1512f9295749SChris Mason 	trans->transaction->blocked = 1;
1513a4abeea4SJosef Bacik 	spin_unlock(&cur_trans->commit_lock);
1514bb9c12c9SSage Weil 	wake_up(&root->fs_info->transaction_blocked_wait);
1515bb9c12c9SSage Weil 
1516a4abeea4SJosef Bacik 	spin_lock(&root->fs_info->trans_lock);
1517ccd467d6SChris Mason 	if (cur_trans->list.prev != &root->fs_info->trans_list) {
1518ccd467d6SChris Mason 		prev_trans = list_entry(cur_trans->list.prev,
1519ccd467d6SChris Mason 					struct btrfs_transaction, list);
1520ccd467d6SChris Mason 		if (!prev_trans->commit_done) {
152113c5a93eSJosef Bacik 			atomic_inc(&prev_trans->use_count);
1522a4abeea4SJosef Bacik 			spin_unlock(&root->fs_info->trans_lock);
1523ccd467d6SChris Mason 
1524ccd467d6SChris Mason 			wait_for_commit(root, prev_trans);
1525ccd467d6SChris Mason 
152615ee9bc7SJosef Bacik 			put_transaction(prev_trans);
1527a4abeea4SJosef Bacik 		} else {
1528a4abeea4SJosef Bacik 			spin_unlock(&root->fs_info->trans_lock);
1529ccd467d6SChris Mason 		}
1530a4abeea4SJosef Bacik 	} else {
1531a4abeea4SJosef Bacik 		spin_unlock(&root->fs_info->trans_lock);
1532ccd467d6SChris Mason 	}
153315ee9bc7SJosef Bacik 
1534e39e64acSChris Mason 	if (!btrfs_test_opt(root, SSD) &&
1535e39e64acSChris Mason 	    (now < cur_trans->start_time || now - cur_trans->start_time < 1))
153689573b9cSChris Mason 		should_grow = 1;
153789573b9cSChris Mason 
153815ee9bc7SJosef Bacik 	do {
153915ee9bc7SJosef Bacik 		joined = cur_trans->num_joined;
15407ea394f1SYan Zheng 
15412c90e5d6SChris Mason 		WARN_ON(cur_trans != trans->transaction);
154215ee9bc7SJosef Bacik 
1543ca469637SMiao Xie 		ret = btrfs_flush_all_pending_stuffs(trans, root);
154449b25e05SJeff Mahoney 		if (ret)
154549b25e05SJeff Mahoney 			goto cleanup_transaction;
154616cdcec7SMiao Xie 
1547ed3b3d31SChris Mason 		prepare_to_wait(&cur_trans->writer_wait, &wait,
1548ed3b3d31SChris Mason 				TASK_UNINTERRUPTIBLE);
1549ed3b3d31SChris Mason 
155013c5a93eSJosef Bacik 		if (atomic_read(&cur_trans->num_writers) > 1)
155199d16cbcSSage Weil 			schedule_timeout(MAX_SCHEDULE_TIMEOUT);
155299d16cbcSSage Weil 		else if (should_grow)
155399d16cbcSSage Weil 			schedule_timeout(1);
155415ee9bc7SJosef Bacik 
155515ee9bc7SJosef Bacik 		finish_wait(&cur_trans->writer_wait, &wait);
1556ed0ca140SJosef Bacik 	} while (atomic_read(&cur_trans->num_writers) > 1 ||
1557ed0ca140SJosef Bacik 		 (should_grow && cur_trans->num_joined != joined));
1558ed0ca140SJosef Bacik 
1559ca469637SMiao Xie 	ret = btrfs_flush_all_pending_stuffs(trans, root);
1560ca469637SMiao Xie 	if (ret)
1561ca469637SMiao Xie 		goto cleanup_transaction;
1562ca469637SMiao Xie 
1563ed0ca140SJosef Bacik 	/*
1564ed0ca140SJosef Bacik 	 * Ok now we need to make sure to block out any other joins while we
1565ed0ca140SJosef Bacik 	 * commit the transaction.  We could have started a join before setting
1566ed0ca140SJosef Bacik 	 * no_join so make sure to wait for num_writers to == 1 again.
1567ed0ca140SJosef Bacik 	 */
1568a4abeea4SJosef Bacik 	spin_lock(&root->fs_info->trans_lock);
1569a4abeea4SJosef Bacik 	root->fs_info->trans_no_join = 1;
1570a4abeea4SJosef Bacik 	spin_unlock(&root->fs_info->trans_lock);
1571ed0ca140SJosef Bacik 	wait_event(cur_trans->writer_wait,
1572ed0ca140SJosef Bacik 		   atomic_read(&cur_trans->num_writers) == 1);
157315ee9bc7SJosef Bacik 
15747585717fSChris Mason 	/*
15757585717fSChris Mason 	 * the reloc mutex makes sure that we stop
15767585717fSChris Mason 	 * the balancing code from coming in and moving
15777585717fSChris Mason 	 * extents around in the middle of the commit
15787585717fSChris Mason 	 */
15797585717fSChris Mason 	mutex_lock(&root->fs_info->reloc_mutex);
15807585717fSChris Mason 
158142874b3dSMiao Xie 	/*
158242874b3dSMiao Xie 	 * We needn't worry about the delayed items because we will
158342874b3dSMiao Xie 	 * deal with them in create_pending_snapshot(), which is the
158442874b3dSMiao Xie 	 * core function of the snapshot creation.
158542874b3dSMiao Xie 	 */
158642874b3dSMiao Xie 	ret = create_pending_snapshots(trans, root->fs_info);
158749b25e05SJeff Mahoney 	if (ret) {
158849b25e05SJeff Mahoney 		mutex_unlock(&root->fs_info->reloc_mutex);
158949b25e05SJeff Mahoney 		goto cleanup_transaction;
159049b25e05SJeff Mahoney 	}
15913063d29fSChris Mason 
159242874b3dSMiao Xie 	/*
159342874b3dSMiao Xie 	 * We insert the dir indexes of the snapshots and update the inode
159442874b3dSMiao Xie 	 * of the snapshots' parents after the snapshot creation, so there
159542874b3dSMiao Xie 	 * are some delayed items which are not dealt with. Now deal with
159642874b3dSMiao Xie 	 * them.
159742874b3dSMiao Xie 	 *
159842874b3dSMiao Xie 	 * We needn't worry that this operation will corrupt the snapshots,
159942874b3dSMiao Xie 	 * because all the tree which are snapshoted will be forced to COW
160042874b3dSMiao Xie 	 * the nodes and leaves.
160142874b3dSMiao Xie 	 */
160242874b3dSMiao Xie 	ret = btrfs_run_delayed_items(trans, root);
160349b25e05SJeff Mahoney 	if (ret) {
160449b25e05SJeff Mahoney 		mutex_unlock(&root->fs_info->reloc_mutex);
160549b25e05SJeff Mahoney 		goto cleanup_transaction;
160649b25e05SJeff Mahoney 	}
160716cdcec7SMiao Xie 
160856bec294SChris Mason 	ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
160949b25e05SJeff Mahoney 	if (ret) {
161049b25e05SJeff Mahoney 		mutex_unlock(&root->fs_info->reloc_mutex);
161149b25e05SJeff Mahoney 		goto cleanup_transaction;
161249b25e05SJeff Mahoney 	}
161356bec294SChris Mason 
1614e999376fSChris Mason 	/*
1615e999376fSChris Mason 	 * make sure none of the code above managed to slip in a
1616e999376fSChris Mason 	 * delayed item
1617e999376fSChris Mason 	 */
1618e999376fSChris Mason 	btrfs_assert_delayed_root_empty(root);
1619e999376fSChris Mason 
16202c90e5d6SChris Mason 	WARN_ON(cur_trans != trans->transaction);
1621dc17ff8fSChris Mason 
1622a2de733cSArne Jansen 	btrfs_scrub_pause(root);
1623e02119d5SChris Mason 	/* btrfs_commit_tree_roots is responsible for getting the
1624e02119d5SChris Mason 	 * various roots consistent with each other.  Every pointer
1625e02119d5SChris Mason 	 * in the tree of tree roots has to point to the most up to date
1626e02119d5SChris Mason 	 * root for every subvolume and other tree.  So, we have to keep
1627e02119d5SChris Mason 	 * the tree logging code from jumping in and changing any
1628e02119d5SChris Mason 	 * of the trees.
1629e02119d5SChris Mason 	 *
1630e02119d5SChris Mason 	 * At this point in the commit, there can't be any tree-log
1631e02119d5SChris Mason 	 * writers, but a little lower down we drop the trans mutex
1632e02119d5SChris Mason 	 * and let new people in.  By holding the tree_log_mutex
1633e02119d5SChris Mason 	 * from now until after the super is written, we avoid races
1634e02119d5SChris Mason 	 * with the tree-log code.
1635e02119d5SChris Mason 	 */
1636e02119d5SChris Mason 	mutex_lock(&root->fs_info->tree_log_mutex);
16371a40e23bSZheng Yan 
16385d4f98a2SYan Zheng 	ret = commit_fs_roots(trans, root);
163949b25e05SJeff Mahoney 	if (ret) {
164049b25e05SJeff Mahoney 		mutex_unlock(&root->fs_info->tree_log_mutex);
1641871383beSDavid Sterba 		mutex_unlock(&root->fs_info->reloc_mutex);
164249b25e05SJeff Mahoney 		goto cleanup_transaction;
164349b25e05SJeff Mahoney 	}
164454aa1f4dSChris Mason 
16455d4f98a2SYan Zheng 	/* commit_fs_roots gets rid of all the tree log roots, it is now
1646e02119d5SChris Mason 	 * safe to free the root of tree log roots
1647e02119d5SChris Mason 	 */
1648e02119d5SChris Mason 	btrfs_free_log_root_tree(trans, root->fs_info);
1649e02119d5SChris Mason 
16505d4f98a2SYan Zheng 	ret = commit_cowonly_roots(trans, root);
165149b25e05SJeff Mahoney 	if (ret) {
165249b25e05SJeff Mahoney 		mutex_unlock(&root->fs_info->tree_log_mutex);
1653871383beSDavid Sterba 		mutex_unlock(&root->fs_info->reloc_mutex);
165449b25e05SJeff Mahoney 		goto cleanup_transaction;
165549b25e05SJeff Mahoney 	}
165654aa1f4dSChris Mason 
165711833d66SYan Zheng 	btrfs_prepare_extent_commit(trans, root);
165811833d66SYan Zheng 
165978fae27eSChris Mason 	cur_trans = root->fs_info->running_transaction;
16605f39d397SChris Mason 
16615d4f98a2SYan Zheng 	btrfs_set_root_node(&root->fs_info->tree_root->root_item,
16625d4f98a2SYan Zheng 			    root->fs_info->tree_root->node);
1663817d52f8SJosef Bacik 	switch_commit_root(root->fs_info->tree_root);
16645d4f98a2SYan Zheng 
16655d4f98a2SYan Zheng 	btrfs_set_root_node(&root->fs_info->chunk_root->root_item,
16665d4f98a2SYan Zheng 			    root->fs_info->chunk_root->node);
1667817d52f8SJosef Bacik 	switch_commit_root(root->fs_info->chunk_root);
16685d4f98a2SYan Zheng 
1669edf39272SJan Schmidt 	assert_qgroups_uptodate(trans);
16705d4f98a2SYan Zheng 	update_super_roots(root);
1671e02119d5SChris Mason 
1672e02119d5SChris Mason 	if (!root->fs_info->log_root_recovering) {
16736c41761fSDavid Sterba 		btrfs_set_super_log_root(root->fs_info->super_copy, 0);
16746c41761fSDavid Sterba 		btrfs_set_super_log_root_level(root->fs_info->super_copy, 0);
1675e02119d5SChris Mason 	}
1676e02119d5SChris Mason 
16776c41761fSDavid Sterba 	memcpy(root->fs_info->super_for_commit, root->fs_info->super_copy,
16786c41761fSDavid Sterba 	       sizeof(*root->fs_info->super_copy));
1679ccd467d6SChris Mason 
1680f9295749SChris Mason 	trans->transaction->blocked = 0;
1681a4abeea4SJosef Bacik 	spin_lock(&root->fs_info->trans_lock);
1682a4abeea4SJosef Bacik 	root->fs_info->running_transaction = NULL;
1683a4abeea4SJosef Bacik 	root->fs_info->trans_no_join = 0;
1684a4abeea4SJosef Bacik 	spin_unlock(&root->fs_info->trans_lock);
16857585717fSChris Mason 	mutex_unlock(&root->fs_info->reloc_mutex);
1686b7ec40d7SChris Mason 
1687f9295749SChris Mason 	wake_up(&root->fs_info->transaction_wait);
1688e6dcd2dcSChris Mason 
168979154b1bSChris Mason 	ret = btrfs_write_and_wait_transaction(trans, root);
169049b25e05SJeff Mahoney 	if (ret) {
169149b25e05SJeff Mahoney 		btrfs_error(root->fs_info, ret,
169249b25e05SJeff Mahoney 			    "Error while writing out transaction.");
169349b25e05SJeff Mahoney 		mutex_unlock(&root->fs_info->tree_log_mutex);
169449b25e05SJeff Mahoney 		goto cleanup_transaction;
169549b25e05SJeff Mahoney 	}
169649b25e05SJeff Mahoney 
169749b25e05SJeff Mahoney 	ret = write_ctree_super(trans, root, 0);
169849b25e05SJeff Mahoney 	if (ret) {
169949b25e05SJeff Mahoney 		mutex_unlock(&root->fs_info->tree_log_mutex);
170049b25e05SJeff Mahoney 		goto cleanup_transaction;
170149b25e05SJeff Mahoney 	}
17024313b399SChris Mason 
1703e02119d5SChris Mason 	/*
1704e02119d5SChris Mason 	 * the super is written, we can safely allow the tree-loggers
1705e02119d5SChris Mason 	 * to go about their business
1706e02119d5SChris Mason 	 */
1707e02119d5SChris Mason 	mutex_unlock(&root->fs_info->tree_log_mutex);
1708e02119d5SChris Mason 
170911833d66SYan Zheng 	btrfs_finish_extent_commit(trans, root);
17104313b399SChris Mason 
17112c90e5d6SChris Mason 	cur_trans->commit_done = 1;
1712b7ec40d7SChris Mason 
171315ee9bc7SJosef Bacik 	root->fs_info->last_trans_committed = cur_trans->transid;
1714817d52f8SJosef Bacik 
17152c90e5d6SChris Mason 	wake_up(&cur_trans->commit_wait);
17163de4586cSChris Mason 
1717a4abeea4SJosef Bacik 	spin_lock(&root->fs_info->trans_lock);
171813c5a93eSJosef Bacik 	list_del_init(&cur_trans->list);
1719a4abeea4SJosef Bacik 	spin_unlock(&root->fs_info->trans_lock);
1720a4abeea4SJosef Bacik 
172179154b1bSChris Mason 	put_transaction(cur_trans);
172278fae27eSChris Mason 	put_transaction(cur_trans);
172358176a96SJosef Bacik 
1724354aa0fbSMiao Xie 	if (trans->type < TRANS_JOIN_NOLOCK)
1725b2b5ef5cSJan Kara 		sb_end_intwrite(root->fs_info->sb);
1726b2b5ef5cSJan Kara 
17271abe9b8aSliubo 	trace_btrfs_transaction_commit(root);
17281abe9b8aSliubo 
1729a2de733cSArne Jansen 	btrfs_scrub_continue(root);
1730a2de733cSArne Jansen 
17319ed74f2dSJosef Bacik 	if (current->journal_info == trans)
17329ed74f2dSJosef Bacik 		current->journal_info = NULL;
17339ed74f2dSJosef Bacik 
17342c90e5d6SChris Mason 	kmem_cache_free(btrfs_trans_handle_cachep, trans);
173524bbcf04SYan, Zheng 
173624bbcf04SYan, Zheng 	if (current != root->fs_info->transaction_kthread)
173724bbcf04SYan, Zheng 		btrfs_run_delayed_iputs(root);
173824bbcf04SYan, Zheng 
173979154b1bSChris Mason 	return ret;
174049b25e05SJeff Mahoney 
174149b25e05SJeff Mahoney cleanup_transaction:
17420e721106SJosef Bacik 	btrfs_trans_release_metadata(trans, root);
17430e721106SJosef Bacik 	trans->block_rsv = NULL;
174449b25e05SJeff Mahoney 	btrfs_printk(root->fs_info, "Skipping commit of aborted transaction.\n");
174549b25e05SJeff Mahoney //	WARN_ON(1);
174649b25e05SJeff Mahoney 	if (current->journal_info == trans)
174749b25e05SJeff Mahoney 		current->journal_info = NULL;
17487b8b92afSJosef Bacik 	cleanup_transaction(trans, root, ret);
174949b25e05SJeff Mahoney 
175049b25e05SJeff Mahoney 	return ret;
175179154b1bSChris Mason }
175279154b1bSChris Mason 
1753d352ac68SChris Mason /*
1754d352ac68SChris Mason  * interface function to delete all the snapshots we have scheduled for deletion
1755d352ac68SChris Mason  */
1756e9d0b13bSChris Mason int btrfs_clean_old_snapshots(struct btrfs_root *root)
1757e9d0b13bSChris Mason {
17585d4f98a2SYan Zheng 	LIST_HEAD(list);
17595d4f98a2SYan Zheng 	struct btrfs_fs_info *fs_info = root->fs_info;
1760e9d0b13bSChris Mason 
1761a4abeea4SJosef Bacik 	spin_lock(&fs_info->trans_lock);
17625d4f98a2SYan Zheng 	list_splice_init(&fs_info->dead_roots, &list);
1763a4abeea4SJosef Bacik 	spin_unlock(&fs_info->trans_lock);
17645d4f98a2SYan Zheng 
17655d4f98a2SYan Zheng 	while (!list_empty(&list)) {
17662c536799SJeff Mahoney 		int ret;
17672c536799SJeff Mahoney 
17685d4f98a2SYan Zheng 		root = list_entry(list.next, struct btrfs_root, root_list);
176976dda93cSYan, Zheng 		list_del(&root->root_list);
177076dda93cSYan, Zheng 
177116cdcec7SMiao Xie 		btrfs_kill_all_delayed_nodes(root);
177216cdcec7SMiao Xie 
177376dda93cSYan, Zheng 		if (btrfs_header_backref_rev(root->node) <
177476dda93cSYan, Zheng 		    BTRFS_MIXED_BACKREF_REV)
17752c536799SJeff Mahoney 			ret = btrfs_drop_snapshot(root, NULL, 0, 0);
177676dda93cSYan, Zheng 		else
17772c536799SJeff Mahoney 			ret =btrfs_drop_snapshot(root, NULL, 1, 0);
17782c536799SJeff Mahoney 		BUG_ON(ret < 0);
1779e9d0b13bSChris Mason 	}
1780e9d0b13bSChris Mason 	return 0;
1781e9d0b13bSChris Mason }
1782