xref: /openbmc/linux/fs/btrfs/transaction.c (revision 178260b2)
16cbd5570SChris Mason /*
26cbd5570SChris Mason  * Copyright (C) 2007 Oracle.  All rights reserved.
36cbd5570SChris Mason  *
46cbd5570SChris Mason  * This program is free software; you can redistribute it and/or
56cbd5570SChris Mason  * modify it under the terms of the GNU General Public
66cbd5570SChris Mason  * License v2 as published by the Free Software Foundation.
76cbd5570SChris Mason  *
86cbd5570SChris Mason  * This program is distributed in the hope that it will be useful,
96cbd5570SChris Mason  * but WITHOUT ANY WARRANTY; without even the implied warranty of
106cbd5570SChris Mason  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
116cbd5570SChris Mason  * General Public License for more details.
126cbd5570SChris Mason  *
136cbd5570SChris Mason  * You should have received a copy of the GNU General Public
146cbd5570SChris Mason  * License along with this program; if not, write to the
156cbd5570SChris Mason  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
166cbd5570SChris Mason  * Boston, MA 021110-1307, USA.
176cbd5570SChris Mason  */
186cbd5570SChris Mason 
1979154b1bSChris Mason #include <linux/fs.h>
205a0e3ad6STejun Heo #include <linux/slab.h>
2134088780SChris Mason #include <linux/sched.h>
22d3c2fdcfSChris Mason #include <linux/writeback.h>
235f39d397SChris Mason #include <linux/pagemap.h>
245f2cc086SChris Mason #include <linux/blkdev.h>
258ea05e3aSAlexander Block #include <linux/uuid.h>
2679154b1bSChris Mason #include "ctree.h"
2779154b1bSChris Mason #include "disk-io.h"
2879154b1bSChris Mason #include "transaction.h"
29925baeddSChris Mason #include "locking.h"
30e02119d5SChris Mason #include "tree-log.h"
31581bb050SLi Zefan #include "inode-map.h"
32733f4fbbSStefan Behrens #include "volumes.h"
338dabb742SStefan Behrens #include "dev-replace.h"
3479154b1bSChris Mason 
350f7d52f4SChris Mason #define BTRFS_ROOT_TRANS_TAG 0
360f7d52f4SChris Mason 
3749b25e05SJeff Mahoney void put_transaction(struct btrfs_transaction *transaction)
3879154b1bSChris Mason {
3913c5a93eSJosef Bacik 	WARN_ON(atomic_read(&transaction->use_count) == 0);
4013c5a93eSJosef Bacik 	if (atomic_dec_and_test(&transaction->use_count)) {
41a4abeea4SJosef Bacik 		BUG_ON(!list_empty(&transaction->list));
4200f04b88SArne Jansen 		WARN_ON(transaction->delayed_refs.root.rb_node);
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 
53178260b2SMiao Xie static inline int can_join_transaction(struct btrfs_transaction *trans,
54178260b2SMiao Xie 				       int type)
55178260b2SMiao Xie {
56178260b2SMiao Xie 	return !(trans->in_commit &&
57178260b2SMiao Xie 		 type != TRANS_JOIN &&
58178260b2SMiao Xie 		 type != TRANS_JOIN_NOLOCK);
59178260b2SMiao Xie }
60178260b2SMiao Xie 
61d352ac68SChris Mason /*
62d352ac68SChris Mason  * either allocate a new transaction or hop into the existing one
63d352ac68SChris Mason  */
64354aa0fbSMiao Xie static noinline int join_transaction(struct btrfs_root *root, int type)
6579154b1bSChris Mason {
6679154b1bSChris Mason 	struct btrfs_transaction *cur_trans;
6719ae4e81SJan Schmidt 	struct btrfs_fs_info *fs_info = root->fs_info;
68a4abeea4SJosef Bacik 
6919ae4e81SJan Schmidt 	spin_lock(&fs_info->trans_lock);
70d43317dcSChris Mason loop:
7149b25e05SJeff Mahoney 	/* The file system has been taken offline. No new transactions. */
7287533c47SMiao Xie 	if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) {
7319ae4e81SJan Schmidt 		spin_unlock(&fs_info->trans_lock);
7449b25e05SJeff Mahoney 		return -EROFS;
7549b25e05SJeff Mahoney 	}
7649b25e05SJeff Mahoney 
7719ae4e81SJan Schmidt 	if (fs_info->trans_no_join) {
78354aa0fbSMiao Xie 		/*
79354aa0fbSMiao Xie 		 * If we are JOIN_NOLOCK we're already committing a current
80354aa0fbSMiao Xie 		 * transaction, we just need a handle to deal with something
81354aa0fbSMiao Xie 		 * when committing the transaction, such as inode cache and
82354aa0fbSMiao Xie 		 * space cache. It is a special case.
83354aa0fbSMiao Xie 		 */
84354aa0fbSMiao Xie 		if (type != TRANS_JOIN_NOLOCK) {
8519ae4e81SJan Schmidt 			spin_unlock(&fs_info->trans_lock);
86a4abeea4SJosef Bacik 			return -EBUSY;
87a4abeea4SJosef Bacik 		}
88a4abeea4SJosef Bacik 	}
89a4abeea4SJosef Bacik 
9019ae4e81SJan Schmidt 	cur_trans = fs_info->running_transaction;
91a4abeea4SJosef Bacik 	if (cur_trans) {
92871383beSDavid Sterba 		if (cur_trans->aborted) {
9319ae4e81SJan Schmidt 			spin_unlock(&fs_info->trans_lock);
9449b25e05SJeff Mahoney 			return cur_trans->aborted;
95871383beSDavid Sterba 		}
96178260b2SMiao Xie 		if (!can_join_transaction(cur_trans, type)) {
97178260b2SMiao Xie 			spin_unlock(&fs_info->trans_lock);
98178260b2SMiao Xie 			return -EBUSY;
99178260b2SMiao Xie 		}
100a4abeea4SJosef Bacik 		atomic_inc(&cur_trans->use_count);
101a4abeea4SJosef Bacik 		atomic_inc(&cur_trans->num_writers);
102a4abeea4SJosef Bacik 		cur_trans->num_joined++;
10319ae4e81SJan Schmidt 		spin_unlock(&fs_info->trans_lock);
104a4abeea4SJosef Bacik 		return 0;
105a4abeea4SJosef Bacik 	}
10619ae4e81SJan Schmidt 	spin_unlock(&fs_info->trans_lock);
107a4abeea4SJosef Bacik 
108354aa0fbSMiao Xie 	/*
109354aa0fbSMiao Xie 	 * If we are ATTACH, we just want to catch the current transaction,
110354aa0fbSMiao Xie 	 * and commit it. If there is no transaction, just return ENOENT.
111354aa0fbSMiao Xie 	 */
112354aa0fbSMiao Xie 	if (type == TRANS_ATTACH)
113354aa0fbSMiao Xie 		return -ENOENT;
114354aa0fbSMiao Xie 
115a4abeea4SJosef Bacik 	cur_trans = kmem_cache_alloc(btrfs_transaction_cachep, GFP_NOFS);
116db5b493aSTsutomu Itoh 	if (!cur_trans)
117db5b493aSTsutomu Itoh 		return -ENOMEM;
118d43317dcSChris Mason 
11919ae4e81SJan Schmidt 	spin_lock(&fs_info->trans_lock);
12019ae4e81SJan Schmidt 	if (fs_info->running_transaction) {
121d43317dcSChris Mason 		/*
122d43317dcSChris Mason 		 * someone started a transaction after we unlocked.  Make sure
123d43317dcSChris Mason 		 * to redo the trans_no_join checks above
124d43317dcSChris Mason 		 */
125a4abeea4SJosef Bacik 		kmem_cache_free(btrfs_transaction_cachep, cur_trans);
12619ae4e81SJan Schmidt 		cur_trans = fs_info->running_transaction;
127d43317dcSChris Mason 		goto loop;
12887533c47SMiao Xie 	} else if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) {
129e4b50e14SDan Carpenter 		spin_unlock(&fs_info->trans_lock);
1307b8b92afSJosef Bacik 		kmem_cache_free(btrfs_transaction_cachep, cur_trans);
1317b8b92afSJosef Bacik 		return -EROFS;
132a4abeea4SJosef Bacik 	}
133d43317dcSChris Mason 
13413c5a93eSJosef Bacik 	atomic_set(&cur_trans->num_writers, 1);
13515ee9bc7SJosef Bacik 	cur_trans->num_joined = 0;
13679154b1bSChris Mason 	init_waitqueue_head(&cur_trans->writer_wait);
13779154b1bSChris Mason 	init_waitqueue_head(&cur_trans->commit_wait);
13879154b1bSChris Mason 	cur_trans->in_commit = 0;
139f9295749SChris Mason 	cur_trans->blocked = 0;
140a4abeea4SJosef Bacik 	/*
141a4abeea4SJosef Bacik 	 * One for this trans handle, one so it will live on until we
142a4abeea4SJosef Bacik 	 * commit the transaction.
143a4abeea4SJosef Bacik 	 */
144a4abeea4SJosef Bacik 	atomic_set(&cur_trans->use_count, 2);
14579154b1bSChris Mason 	cur_trans->commit_done = 0;
14608607c1bSChris Mason 	cur_trans->start_time = get_seconds();
14756bec294SChris Mason 
1486bef4d31SEric Paris 	cur_trans->delayed_refs.root = RB_ROOT;
14956bec294SChris Mason 	cur_trans->delayed_refs.num_entries = 0;
150c3e69d58SChris Mason 	cur_trans->delayed_refs.num_heads_ready = 0;
151c3e69d58SChris Mason 	cur_trans->delayed_refs.num_heads = 0;
15256bec294SChris Mason 	cur_trans->delayed_refs.flushing = 0;
153c3e69d58SChris Mason 	cur_trans->delayed_refs.run_delayed_start = 0;
15420b297d6SJan Schmidt 
15520b297d6SJan Schmidt 	/*
15620b297d6SJan Schmidt 	 * although the tree mod log is per file system and not per transaction,
15720b297d6SJan Schmidt 	 * the log must never go across transaction boundaries.
15820b297d6SJan Schmidt 	 */
15920b297d6SJan Schmidt 	smp_mb();
16031b1a2bdSJulia Lawall 	if (!list_empty(&fs_info->tree_mod_seq_list))
16131b1a2bdSJulia Lawall 		WARN(1, KERN_ERR "btrfs: tree_mod_seq_list not empty when "
16220b297d6SJan Schmidt 			"creating a fresh transaction\n");
16331b1a2bdSJulia Lawall 	if (!RB_EMPTY_ROOT(&fs_info->tree_mod_log))
16431b1a2bdSJulia Lawall 		WARN(1, KERN_ERR "btrfs: tree_mod_log rb tree not empty when "
16520b297d6SJan Schmidt 			"creating a fresh transaction\n");
16620b297d6SJan Schmidt 	atomic_set(&fs_info->tree_mod_seq, 0);
16720b297d6SJan Schmidt 
168a4abeea4SJosef Bacik 	spin_lock_init(&cur_trans->commit_lock);
16956bec294SChris Mason 	spin_lock_init(&cur_trans->delayed_refs.lock);
17056bec294SChris Mason 
1713063d29fSChris Mason 	INIT_LIST_HEAD(&cur_trans->pending_snapshots);
172569e0f35SJosef Bacik 	INIT_LIST_HEAD(&cur_trans->ordered_operations);
17319ae4e81SJan Schmidt 	list_add_tail(&cur_trans->list, &fs_info->trans_list);
174d1310b2eSChris Mason 	extent_io_tree_init(&cur_trans->dirty_pages,
17519ae4e81SJan Schmidt 			     fs_info->btree_inode->i_mapping);
17619ae4e81SJan Schmidt 	fs_info->generation++;
17719ae4e81SJan Schmidt 	cur_trans->transid = fs_info->generation;
17819ae4e81SJan Schmidt 	fs_info->running_transaction = cur_trans;
17949b25e05SJeff Mahoney 	cur_trans->aborted = 0;
18019ae4e81SJan Schmidt 	spin_unlock(&fs_info->trans_lock);
18115ee9bc7SJosef Bacik 
18279154b1bSChris Mason 	return 0;
18379154b1bSChris Mason }
18479154b1bSChris Mason 
185d352ac68SChris Mason /*
186d397712bSChris Mason  * this does all the record keeping required to make sure that a reference
187d397712bSChris Mason  * counted root is properly recorded in a given transaction.  This is required
188d397712bSChris Mason  * to make sure the old root from before we joined the transaction is deleted
189d397712bSChris Mason  * when the transaction commits
190d352ac68SChris Mason  */
1917585717fSChris Mason static int record_root_in_trans(struct btrfs_trans_handle *trans,
1925d4f98a2SYan Zheng 			       struct btrfs_root *root)
1936702ed49SChris Mason {
1945d4f98a2SYan Zheng 	if (root->ref_cows && root->last_trans < trans->transid) {
1956702ed49SChris Mason 		WARN_ON(root == root->fs_info->extent_root);
1965d4f98a2SYan Zheng 		WARN_ON(root->commit_root != root->node);
1975d4f98a2SYan Zheng 
1987585717fSChris Mason 		/*
1997585717fSChris Mason 		 * see below for in_trans_setup usage rules
2007585717fSChris Mason 		 * we have the reloc mutex held now, so there
2017585717fSChris Mason 		 * is only one writer in this function
2027585717fSChris Mason 		 */
2037585717fSChris Mason 		root->in_trans_setup = 1;
2047585717fSChris Mason 
2057585717fSChris Mason 		/* make sure readers find in_trans_setup before
2067585717fSChris Mason 		 * they find our root->last_trans update
2077585717fSChris Mason 		 */
2087585717fSChris Mason 		smp_wmb();
2097585717fSChris Mason 
210a4abeea4SJosef Bacik 		spin_lock(&root->fs_info->fs_roots_radix_lock);
211a4abeea4SJosef Bacik 		if (root->last_trans == trans->transid) {
212a4abeea4SJosef Bacik 			spin_unlock(&root->fs_info->fs_roots_radix_lock);
213a4abeea4SJosef Bacik 			return 0;
214a4abeea4SJosef Bacik 		}
2156702ed49SChris Mason 		radix_tree_tag_set(&root->fs_info->fs_roots_radix,
2166702ed49SChris Mason 			   (unsigned long)root->root_key.objectid,
2176702ed49SChris Mason 			   BTRFS_ROOT_TRANS_TAG);
218a4abeea4SJosef Bacik 		spin_unlock(&root->fs_info->fs_roots_radix_lock);
2197585717fSChris Mason 		root->last_trans = trans->transid;
2207585717fSChris Mason 
2217585717fSChris Mason 		/* this is pretty tricky.  We don't want to
2227585717fSChris Mason 		 * take the relocation lock in btrfs_record_root_in_trans
2237585717fSChris Mason 		 * unless we're really doing the first setup for this root in
2247585717fSChris Mason 		 * this transaction.
2257585717fSChris Mason 		 *
2267585717fSChris Mason 		 * Normally we'd use root->last_trans as a flag to decide
2277585717fSChris Mason 		 * if we want to take the expensive mutex.
2287585717fSChris Mason 		 *
2297585717fSChris Mason 		 * But, we have to set root->last_trans before we
2307585717fSChris Mason 		 * init the relocation root, otherwise, we trip over warnings
2317585717fSChris Mason 		 * in ctree.c.  The solution used here is to flag ourselves
2327585717fSChris Mason 		 * with root->in_trans_setup.  When this is 1, we're still
2337585717fSChris Mason 		 * fixing up the reloc trees and everyone must wait.
2347585717fSChris Mason 		 *
2357585717fSChris Mason 		 * When this is zero, they can trust root->last_trans and fly
2367585717fSChris Mason 		 * through btrfs_record_root_in_trans without having to take the
2377585717fSChris Mason 		 * lock.  smp_wmb() makes sure that all the writes above are
2387585717fSChris Mason 		 * done before we pop in the zero below
2397585717fSChris Mason 		 */
2405d4f98a2SYan Zheng 		btrfs_init_reloc_root(trans, root);
2417585717fSChris Mason 		smp_wmb();
2427585717fSChris Mason 		root->in_trans_setup = 0;
2436702ed49SChris Mason 	}
2445d4f98a2SYan Zheng 	return 0;
2456702ed49SChris Mason }
2465d4f98a2SYan Zheng 
2477585717fSChris Mason 
2487585717fSChris Mason int btrfs_record_root_in_trans(struct btrfs_trans_handle *trans,
2497585717fSChris Mason 			       struct btrfs_root *root)
2507585717fSChris Mason {
2517585717fSChris Mason 	if (!root->ref_cows)
2527585717fSChris Mason 		return 0;
2537585717fSChris Mason 
2547585717fSChris Mason 	/*
2557585717fSChris Mason 	 * see record_root_in_trans for comments about in_trans_setup usage
2567585717fSChris Mason 	 * and barriers
2577585717fSChris Mason 	 */
2587585717fSChris Mason 	smp_rmb();
2597585717fSChris Mason 	if (root->last_trans == trans->transid &&
2607585717fSChris Mason 	    !root->in_trans_setup)
2617585717fSChris Mason 		return 0;
2627585717fSChris Mason 
2637585717fSChris Mason 	mutex_lock(&root->fs_info->reloc_mutex);
2647585717fSChris Mason 	record_root_in_trans(trans, root);
2657585717fSChris Mason 	mutex_unlock(&root->fs_info->reloc_mutex);
2667585717fSChris Mason 
2677585717fSChris Mason 	return 0;
2687585717fSChris Mason }
2697585717fSChris Mason 
270d352ac68SChris Mason /* wait for commit against the current transaction to become unblocked
271d352ac68SChris Mason  * when this is done, it is safe to start a new transaction, but the current
272d352ac68SChris Mason  * transaction might not be fully on disk.
273d352ac68SChris Mason  */
27437d1aeeeSChris Mason static void wait_current_trans(struct btrfs_root *root)
27579154b1bSChris Mason {
276f9295749SChris Mason 	struct btrfs_transaction *cur_trans;
27779154b1bSChris Mason 
278a4abeea4SJosef Bacik 	spin_lock(&root->fs_info->trans_lock);
279f9295749SChris Mason 	cur_trans = root->fs_info->running_transaction;
28037d1aeeeSChris Mason 	if (cur_trans && cur_trans->blocked) {
28113c5a93eSJosef Bacik 		atomic_inc(&cur_trans->use_count);
282a4abeea4SJosef Bacik 		spin_unlock(&root->fs_info->trans_lock);
28372d63ed6SLi Zefan 
28472d63ed6SLi Zefan 		wait_event(root->fs_info->transaction_wait,
28572d63ed6SLi Zefan 			   !cur_trans->blocked);
286f9295749SChris Mason 		put_transaction(cur_trans);
287a4abeea4SJosef Bacik 	} else {
288a4abeea4SJosef Bacik 		spin_unlock(&root->fs_info->trans_lock);
289f9295749SChris Mason 	}
29037d1aeeeSChris Mason }
29137d1aeeeSChris Mason 
292a22285a6SYan, Zheng static int may_wait_transaction(struct btrfs_root *root, int type)
29337d1aeeeSChris Mason {
294a4abeea4SJosef Bacik 	if (root->fs_info->log_root_recovering)
295a4abeea4SJosef Bacik 		return 0;
296a4abeea4SJosef Bacik 
297a4abeea4SJosef Bacik 	if (type == TRANS_USERSPACE)
298a22285a6SYan, Zheng 		return 1;
299a4abeea4SJosef Bacik 
300a4abeea4SJosef Bacik 	if (type == TRANS_START &&
301a4abeea4SJosef Bacik 	    !atomic_read(&root->fs_info->open_ioctl_trans))
302a4abeea4SJosef Bacik 		return 1;
303a4abeea4SJosef Bacik 
304a22285a6SYan, Zheng 	return 0;
305a22285a6SYan, Zheng }
306a22285a6SYan, Zheng 
30708e007d2SMiao Xie static struct btrfs_trans_handle *
30808e007d2SMiao Xie start_transaction(struct btrfs_root *root, u64 num_items, int type,
30908e007d2SMiao Xie 		  enum btrfs_reserve_flush_enum flush)
310a22285a6SYan, Zheng {
311a22285a6SYan, Zheng 	struct btrfs_trans_handle *h;
312a22285a6SYan, Zheng 	struct btrfs_transaction *cur_trans;
313b5009945SJosef Bacik 	u64 num_bytes = 0;
314a22285a6SYan, Zheng 	int ret;
315c5567237SArne Jansen 	u64 qgroup_reserved = 0;
316acce952bSliubo 
31787533c47SMiao Xie 	if (test_bit(BTRFS_FS_STATE_ERROR, &root->fs_info->fs_state))
318acce952bSliubo 		return ERR_PTR(-EROFS);
3192a1eb461SJosef Bacik 
3202a1eb461SJosef Bacik 	if (current->journal_info) {
3212a1eb461SJosef Bacik 		WARN_ON(type != TRANS_JOIN && type != TRANS_JOIN_NOLOCK);
3222a1eb461SJosef Bacik 		h = current->journal_info;
3232a1eb461SJosef Bacik 		h->use_count++;
324b7d5b0a8SMiao Xie 		WARN_ON(h->use_count > 2);
3252a1eb461SJosef Bacik 		h->orig_rsv = h->block_rsv;
3262a1eb461SJosef Bacik 		h->block_rsv = NULL;
3272a1eb461SJosef Bacik 		goto got_it;
3282a1eb461SJosef Bacik 	}
329b5009945SJosef Bacik 
330b5009945SJosef Bacik 	/*
331b5009945SJosef Bacik 	 * Do the reservation before we join the transaction so we can do all
332b5009945SJosef Bacik 	 * the appropriate flushing if need be.
333b5009945SJosef Bacik 	 */
334b5009945SJosef Bacik 	if (num_items > 0 && root != root->fs_info->chunk_root) {
335c5567237SArne Jansen 		if (root->fs_info->quota_enabled &&
336c5567237SArne Jansen 		    is_fstree(root->root_key.objectid)) {
337c5567237SArne Jansen 			qgroup_reserved = num_items * root->leafsize;
338c5567237SArne Jansen 			ret = btrfs_qgroup_reserve(root, qgroup_reserved);
339c5567237SArne Jansen 			if (ret)
340c5567237SArne Jansen 				return ERR_PTR(ret);
341c5567237SArne Jansen 		}
342c5567237SArne Jansen 
343b5009945SJosef Bacik 		num_bytes = btrfs_calc_trans_metadata_size(root, num_items);
3444a92b1b8SJosef Bacik 		ret = btrfs_block_rsv_add(root,
345b5009945SJosef Bacik 					  &root->fs_info->trans_block_rsv,
34608e007d2SMiao Xie 					  num_bytes, flush);
347b5009945SJosef Bacik 		if (ret)
348843fcf35SMiao Xie 			goto reserve_fail;
349b5009945SJosef Bacik 	}
350a22285a6SYan, Zheng again:
351a22285a6SYan, Zheng 	h = kmem_cache_alloc(btrfs_trans_handle_cachep, GFP_NOFS);
352843fcf35SMiao Xie 	if (!h) {
353843fcf35SMiao Xie 		ret = -ENOMEM;
354843fcf35SMiao Xie 		goto alloc_fail;
355843fcf35SMiao Xie 	}
356a22285a6SYan, Zheng 
35798114659SJosef Bacik 	/*
35898114659SJosef Bacik 	 * If we are JOIN_NOLOCK we're already committing a transaction and
35998114659SJosef Bacik 	 * waiting on this guy, so we don't need to do the sb_start_intwrite
36098114659SJosef Bacik 	 * because we're already holding a ref.  We need this because we could
36198114659SJosef Bacik 	 * have raced in and did an fsync() on a file which can kick a commit
36298114659SJosef Bacik 	 * and then we deadlock with somebody doing a freeze.
363354aa0fbSMiao Xie 	 *
364354aa0fbSMiao Xie 	 * If we are ATTACH, it means we just want to catch the current
365354aa0fbSMiao Xie 	 * transaction and commit it, so we needn't do sb_start_intwrite().
36698114659SJosef Bacik 	 */
367354aa0fbSMiao Xie 	if (type < TRANS_JOIN_NOLOCK)
368b2b5ef5cSJan Kara 		sb_start_intwrite(root->fs_info->sb);
369b2b5ef5cSJan Kara 
370a22285a6SYan, Zheng 	if (may_wait_transaction(root, type))
37137d1aeeeSChris Mason 		wait_current_trans(root);
372a22285a6SYan, Zheng 
373a4abeea4SJosef Bacik 	do {
374354aa0fbSMiao Xie 		ret = join_transaction(root, type);
375178260b2SMiao Xie 		if (ret == -EBUSY) {
376a4abeea4SJosef Bacik 			wait_current_trans(root);
377178260b2SMiao Xie 			if (unlikely(type == TRANS_ATTACH))
378178260b2SMiao Xie 				ret = -ENOENT;
379178260b2SMiao Xie 		}
380a4abeea4SJosef Bacik 	} while (ret == -EBUSY);
381a4abeea4SJosef Bacik 
382db5b493aSTsutomu Itoh 	if (ret < 0) {
383354aa0fbSMiao Xie 		/* We must get the transaction if we are JOIN_NOLOCK. */
384354aa0fbSMiao Xie 		BUG_ON(type == TRANS_JOIN_NOLOCK);
385843fcf35SMiao Xie 		goto join_fail;
386db5b493aSTsutomu Itoh 	}
3870f7d52f4SChris Mason 
388a22285a6SYan, Zheng 	cur_trans = root->fs_info->running_transaction;
389a22285a6SYan, Zheng 
390a22285a6SYan, Zheng 	h->transid = cur_trans->transid;
391a22285a6SYan, Zheng 	h->transaction = cur_trans;
39279154b1bSChris Mason 	h->blocks_used = 0;
393a22285a6SYan, Zheng 	h->bytes_reserved = 0;
394d13603efSArne Jansen 	h->root = root;
39556bec294SChris Mason 	h->delayed_ref_updates = 0;
3962a1eb461SJosef Bacik 	h->use_count = 1;
3970e721106SJosef Bacik 	h->adding_csums = 0;
398f0486c68SYan, Zheng 	h->block_rsv = NULL;
3992a1eb461SJosef Bacik 	h->orig_rsv = NULL;
40049b25e05SJeff Mahoney 	h->aborted = 0;
4014b824906SMiao Xie 	h->qgroup_reserved = 0;
402bed92eaeSArne Jansen 	h->delayed_ref_elem.seq = 0;
403a698d075SMiao Xie 	h->type = type;
404c6b305a8SJosef Bacik 	h->allocating_chunk = false;
405bed92eaeSArne Jansen 	INIT_LIST_HEAD(&h->qgroup_ref_list);
406ea658badSJosef Bacik 	INIT_LIST_HEAD(&h->new_bgs);
407b7ec40d7SChris Mason 
408a22285a6SYan, Zheng 	smp_mb();
409a22285a6SYan, Zheng 	if (cur_trans->blocked && may_wait_transaction(root, type)) {
410a22285a6SYan, Zheng 		btrfs_commit_transaction(h, root);
411a22285a6SYan, Zheng 		goto again;
412a22285a6SYan, Zheng 	}
4139ed74f2dSJosef Bacik 
414b5009945SJosef Bacik 	if (num_bytes) {
4158c2a3ca2SJosef Bacik 		trace_btrfs_space_reservation(root->fs_info, "transaction",
4162bcc0328SLiu Bo 					      h->transid, num_bytes, 1);
417b5009945SJosef Bacik 		h->block_rsv = &root->fs_info->trans_block_rsv;
418b5009945SJosef Bacik 		h->bytes_reserved = num_bytes;
419a22285a6SYan, Zheng 	}
4204b824906SMiao Xie 	h->qgroup_reserved = qgroup_reserved;
421a22285a6SYan, Zheng 
4222a1eb461SJosef Bacik got_it:
423a4abeea4SJosef Bacik 	btrfs_record_root_in_trans(h, root);
424a22285a6SYan, Zheng 
425a22285a6SYan, Zheng 	if (!current->journal_info && type != TRANS_USERSPACE)
426a22285a6SYan, Zheng 		current->journal_info = h;
42779154b1bSChris Mason 	return h;
428843fcf35SMiao Xie 
429843fcf35SMiao Xie join_fail:
430843fcf35SMiao Xie 	if (type < TRANS_JOIN_NOLOCK)
431843fcf35SMiao Xie 		sb_end_intwrite(root->fs_info->sb);
432843fcf35SMiao Xie 	kmem_cache_free(btrfs_trans_handle_cachep, h);
433843fcf35SMiao Xie alloc_fail:
434843fcf35SMiao Xie 	if (num_bytes)
435843fcf35SMiao Xie 		btrfs_block_rsv_release(root, &root->fs_info->trans_block_rsv,
436843fcf35SMiao Xie 					num_bytes);
437843fcf35SMiao Xie reserve_fail:
438843fcf35SMiao Xie 	if (qgroup_reserved)
439843fcf35SMiao Xie 		btrfs_qgroup_free(root, qgroup_reserved);
440843fcf35SMiao Xie 	return ERR_PTR(ret);
44179154b1bSChris Mason }
44279154b1bSChris Mason 
443f9295749SChris Mason struct btrfs_trans_handle *btrfs_start_transaction(struct btrfs_root *root,
444a22285a6SYan, Zheng 						   int num_items)
445f9295749SChris Mason {
44608e007d2SMiao Xie 	return start_transaction(root, num_items, TRANS_START,
44708e007d2SMiao Xie 				 BTRFS_RESERVE_FLUSH_ALL);
448f9295749SChris Mason }
4498407aa46SMiao Xie 
45008e007d2SMiao Xie struct btrfs_trans_handle *btrfs_start_transaction_lflush(
4518407aa46SMiao Xie 					struct btrfs_root *root, int num_items)
4528407aa46SMiao Xie {
45308e007d2SMiao Xie 	return start_transaction(root, num_items, TRANS_START,
45408e007d2SMiao Xie 				 BTRFS_RESERVE_FLUSH_LIMIT);
4558407aa46SMiao Xie }
4568407aa46SMiao Xie 
4577a7eaa40SJosef Bacik struct btrfs_trans_handle *btrfs_join_transaction(struct btrfs_root *root)
458f9295749SChris Mason {
4598407aa46SMiao Xie 	return start_transaction(root, 0, TRANS_JOIN, 0);
460f9295749SChris Mason }
461f9295749SChris Mason 
4627a7eaa40SJosef Bacik struct btrfs_trans_handle *btrfs_join_transaction_nolock(struct btrfs_root *root)
4630af3d00bSJosef Bacik {
4648407aa46SMiao Xie 	return start_transaction(root, 0, TRANS_JOIN_NOLOCK, 0);
4650af3d00bSJosef Bacik }
4660af3d00bSJosef Bacik 
4677a7eaa40SJosef Bacik struct btrfs_trans_handle *btrfs_start_ioctl_transaction(struct btrfs_root *root)
4689ca9ee09SSage Weil {
4698407aa46SMiao Xie 	return start_transaction(root, 0, TRANS_USERSPACE, 0);
4709ca9ee09SSage Weil }
4719ca9ee09SSage Weil 
472354aa0fbSMiao Xie struct btrfs_trans_handle *btrfs_attach_transaction(struct btrfs_root *root)
47360376ce4SJosef Bacik {
474354aa0fbSMiao Xie 	return start_transaction(root, 0, TRANS_ATTACH, 0);
47560376ce4SJosef Bacik }
47660376ce4SJosef Bacik 
477d352ac68SChris Mason /* wait for a transaction commit to be fully complete */
478b9c8300cSLi Zefan static noinline void wait_for_commit(struct btrfs_root *root,
47989ce8a63SChris Mason 				    struct btrfs_transaction *commit)
48089ce8a63SChris Mason {
48172d63ed6SLi Zefan 	wait_event(commit->commit_wait, commit->commit_done);
48289ce8a63SChris Mason }
48389ce8a63SChris Mason 
48446204592SSage Weil int btrfs_wait_for_commit(struct btrfs_root *root, u64 transid)
48546204592SSage Weil {
48646204592SSage Weil 	struct btrfs_transaction *cur_trans = NULL, *t;
4878cd2807fSMiao Xie 	int ret = 0;
48846204592SSage Weil 
48946204592SSage Weil 	if (transid) {
49046204592SSage Weil 		if (transid <= root->fs_info->last_trans_committed)
491a4abeea4SJosef Bacik 			goto out;
49246204592SSage Weil 
4938cd2807fSMiao Xie 		ret = -EINVAL;
49446204592SSage Weil 		/* find specified transaction */
495a4abeea4SJosef Bacik 		spin_lock(&root->fs_info->trans_lock);
49646204592SSage Weil 		list_for_each_entry(t, &root->fs_info->trans_list, list) {
49746204592SSage Weil 			if (t->transid == transid) {
49846204592SSage Weil 				cur_trans = t;
499a4abeea4SJosef Bacik 				atomic_inc(&cur_trans->use_count);
5008cd2807fSMiao Xie 				ret = 0;
50146204592SSage Weil 				break;
50246204592SSage Weil 			}
5038cd2807fSMiao Xie 			if (t->transid > transid) {
5048cd2807fSMiao Xie 				ret = 0;
50546204592SSage Weil 				break;
50646204592SSage Weil 			}
5078cd2807fSMiao Xie 		}
508a4abeea4SJosef Bacik 		spin_unlock(&root->fs_info->trans_lock);
5098cd2807fSMiao Xie 		/* The specified transaction doesn't exist */
51046204592SSage Weil 		if (!cur_trans)
5118cd2807fSMiao Xie 			goto out;
51246204592SSage Weil 	} else {
51346204592SSage Weil 		/* find newest transaction that is committing | committed */
514a4abeea4SJosef Bacik 		spin_lock(&root->fs_info->trans_lock);
51546204592SSage Weil 		list_for_each_entry_reverse(t, &root->fs_info->trans_list,
51646204592SSage Weil 					    list) {
51746204592SSage Weil 			if (t->in_commit) {
51846204592SSage Weil 				if (t->commit_done)
5193473f3c0SJosef Bacik 					break;
52046204592SSage Weil 				cur_trans = t;
521a4abeea4SJosef Bacik 				atomic_inc(&cur_trans->use_count);
52246204592SSage Weil 				break;
52346204592SSage Weil 			}
52446204592SSage Weil 		}
525a4abeea4SJosef Bacik 		spin_unlock(&root->fs_info->trans_lock);
52646204592SSage Weil 		if (!cur_trans)
527a4abeea4SJosef Bacik 			goto out;  /* nothing committing|committed */
52846204592SSage Weil 	}
52946204592SSage Weil 
53046204592SSage Weil 	wait_for_commit(root, cur_trans);
53146204592SSage Weil 	put_transaction(cur_trans);
532a4abeea4SJosef Bacik out:
53346204592SSage Weil 	return ret;
53446204592SSage Weil }
53546204592SSage Weil 
53637d1aeeeSChris Mason void btrfs_throttle(struct btrfs_root *root)
53737d1aeeeSChris Mason {
538a4abeea4SJosef Bacik 	if (!atomic_read(&root->fs_info->open_ioctl_trans))
53937d1aeeeSChris Mason 		wait_current_trans(root);
54037d1aeeeSChris Mason }
54137d1aeeeSChris Mason 
5428929ecfaSYan, Zheng static int should_end_transaction(struct btrfs_trans_handle *trans,
5438929ecfaSYan, Zheng 				  struct btrfs_root *root)
5448929ecfaSYan, Zheng {
5458929ecfaSYan, Zheng 	int ret;
54636ba022aSJosef Bacik 
54736ba022aSJosef Bacik 	ret = btrfs_block_rsv_check(root, &root->fs_info->global_block_rsv, 5);
5488929ecfaSYan, Zheng 	return ret ? 1 : 0;
5498929ecfaSYan, Zheng }
5508929ecfaSYan, Zheng 
5518929ecfaSYan, Zheng int btrfs_should_end_transaction(struct btrfs_trans_handle *trans,
5528929ecfaSYan, Zheng 				 struct btrfs_root *root)
5538929ecfaSYan, Zheng {
5548929ecfaSYan, Zheng 	struct btrfs_transaction *cur_trans = trans->transaction;
5558929ecfaSYan, Zheng 	int updates;
55649b25e05SJeff Mahoney 	int err;
5578929ecfaSYan, Zheng 
558a4abeea4SJosef Bacik 	smp_mb();
5598929ecfaSYan, Zheng 	if (cur_trans->blocked || cur_trans->delayed_refs.flushing)
5608929ecfaSYan, Zheng 		return 1;
5618929ecfaSYan, Zheng 
5628929ecfaSYan, Zheng 	updates = trans->delayed_ref_updates;
5638929ecfaSYan, Zheng 	trans->delayed_ref_updates = 0;
56449b25e05SJeff Mahoney 	if (updates) {
56549b25e05SJeff Mahoney 		err = btrfs_run_delayed_refs(trans, root, updates);
56649b25e05SJeff Mahoney 		if (err) /* Error code will also eval true */
56749b25e05SJeff Mahoney 			return err;
56849b25e05SJeff Mahoney 	}
5698929ecfaSYan, Zheng 
5708929ecfaSYan, Zheng 	return should_end_transaction(trans, root);
5718929ecfaSYan, Zheng }
5728929ecfaSYan, Zheng 
57389ce8a63SChris Mason static int __btrfs_end_transaction(struct btrfs_trans_handle *trans,
574a698d075SMiao Xie 			  struct btrfs_root *root, int throttle)
57579154b1bSChris Mason {
5768929ecfaSYan, Zheng 	struct btrfs_transaction *cur_trans = trans->transaction;
577ab78c84dSChris Mason 	struct btrfs_fs_info *info = root->fs_info;
578c3e69d58SChris Mason 	int count = 0;
579a698d075SMiao Xie 	int lock = (trans->type != TRANS_JOIN_NOLOCK);
5804edc2ca3SDave Jones 	int err = 0;
581d6e4a428SChris Mason 
5822a1eb461SJosef Bacik 	if (--trans->use_count) {
5832a1eb461SJosef Bacik 		trans->block_rsv = trans->orig_rsv;
5842a1eb461SJosef Bacik 		return 0;
5852a1eb461SJosef Bacik 	}
5862a1eb461SJosef Bacik 
587edf39272SJan Schmidt 	/*
588edf39272SJan Schmidt 	 * do the qgroup accounting as early as possible
589edf39272SJan Schmidt 	 */
590edf39272SJan Schmidt 	err = btrfs_delayed_refs_qgroup_accounting(trans, info);
591edf39272SJan Schmidt 
592b24e03dbSJosef Bacik 	btrfs_trans_release_metadata(trans, root);
5934c13d758SJosef Bacik 	trans->block_rsv = NULL;
594d13603efSArne Jansen 	/*
595d13603efSArne Jansen 	 * the same root has to be passed to start_transaction and
596d13603efSArne Jansen 	 * end_transaction. Subvolume quota depends on this.
597d13603efSArne Jansen 	 */
598d13603efSArne Jansen 	WARN_ON(trans->root != root);
599c5567237SArne Jansen 
600c5567237SArne Jansen 	if (trans->qgroup_reserved) {
601c5567237SArne Jansen 		btrfs_qgroup_free(root, trans->qgroup_reserved);
602c5567237SArne Jansen 		trans->qgroup_reserved = 0;
603c5567237SArne Jansen 	}
604c5567237SArne Jansen 
605ea658badSJosef Bacik 	if (!list_empty(&trans->new_bgs))
606ea658badSJosef Bacik 		btrfs_create_pending_block_groups(trans, root);
607ea658badSJosef Bacik 
608203bf287SChris Mason 	while (count < 2) {
609c3e69d58SChris Mason 		unsigned long cur = trans->delayed_ref_updates;
610c3e69d58SChris Mason 		trans->delayed_ref_updates = 0;
611c3e69d58SChris Mason 		if (cur &&
612c3e69d58SChris Mason 		    trans->transaction->delayed_refs.num_heads_ready > 64) {
613c3e69d58SChris Mason 			trans->delayed_ref_updates = 0;
614c3e69d58SChris Mason 			btrfs_run_delayed_refs(trans, root, cur);
615c3e69d58SChris Mason 		} else {
616c3e69d58SChris Mason 			break;
617c3e69d58SChris Mason 		}
618c3e69d58SChris Mason 		count++;
61956bec294SChris Mason 	}
6200e721106SJosef Bacik 	btrfs_trans_release_metadata(trans, root);
6210e721106SJosef Bacik 	trans->block_rsv = NULL;
62256bec294SChris Mason 
623ea658badSJosef Bacik 	if (!list_empty(&trans->new_bgs))
624ea658badSJosef Bacik 		btrfs_create_pending_block_groups(trans, root);
625ea658badSJosef Bacik 
626a4abeea4SJosef Bacik 	if (lock && !atomic_read(&root->fs_info->open_ioctl_trans) &&
627a4abeea4SJosef Bacik 	    should_end_transaction(trans, root)) {
6288929ecfaSYan, Zheng 		trans->transaction->blocked = 1;
629a4abeea4SJosef Bacik 		smp_wmb();
630a4abeea4SJosef Bacik 	}
6318929ecfaSYan, Zheng 
6320af3d00bSJosef Bacik 	if (lock && cur_trans->blocked && !cur_trans->in_commit) {
63381317fdeSJosef Bacik 		if (throttle) {
63481317fdeSJosef Bacik 			/*
63581317fdeSJosef Bacik 			 * We may race with somebody else here so end up having
63681317fdeSJosef Bacik 			 * to call end_transaction on ourselves again, so inc
63781317fdeSJosef Bacik 			 * our use_count.
63881317fdeSJosef Bacik 			 */
63981317fdeSJosef Bacik 			trans->use_count++;
6408929ecfaSYan, Zheng 			return btrfs_commit_transaction(trans, root);
64181317fdeSJosef Bacik 		} else {
6428929ecfaSYan, Zheng 			wake_up_process(info->transaction_kthread);
6438929ecfaSYan, Zheng 		}
64481317fdeSJosef Bacik 	}
6458929ecfaSYan, Zheng 
646354aa0fbSMiao Xie 	if (trans->type < TRANS_JOIN_NOLOCK)
6476df7881aSJosef Bacik 		sb_end_intwrite(root->fs_info->sb);
6486df7881aSJosef Bacik 
6498929ecfaSYan, Zheng 	WARN_ON(cur_trans != info->running_transaction);
65013c5a93eSJosef Bacik 	WARN_ON(atomic_read(&cur_trans->num_writers) < 1);
65113c5a93eSJosef Bacik 	atomic_dec(&cur_trans->num_writers);
65289ce8a63SChris Mason 
65399d16cbcSSage Weil 	smp_mb();
65479154b1bSChris Mason 	if (waitqueue_active(&cur_trans->writer_wait))
65579154b1bSChris Mason 		wake_up(&cur_trans->writer_wait);
65679154b1bSChris Mason 	put_transaction(cur_trans);
6579ed74f2dSJosef Bacik 
6589ed74f2dSJosef Bacik 	if (current->journal_info == trans)
6599ed74f2dSJosef Bacik 		current->journal_info = NULL;
660ab78c84dSChris Mason 
66124bbcf04SYan, Zheng 	if (throttle)
66224bbcf04SYan, Zheng 		btrfs_run_delayed_iputs(root);
66324bbcf04SYan, Zheng 
66449b25e05SJeff Mahoney 	if (trans->aborted ||
66587533c47SMiao Xie 	    test_bit(BTRFS_FS_STATE_ERROR, &root->fs_info->fs_state))
6664edc2ca3SDave Jones 		err = -EIO;
667edf39272SJan Schmidt 	assert_qgroups_uptodate(trans);
66849b25e05SJeff Mahoney 
6694edc2ca3SDave Jones 	kmem_cache_free(btrfs_trans_handle_cachep, trans);
6704edc2ca3SDave Jones 	return err;
67179154b1bSChris Mason }
67279154b1bSChris Mason 
67389ce8a63SChris Mason int btrfs_end_transaction(struct btrfs_trans_handle *trans,
67489ce8a63SChris Mason 			  struct btrfs_root *root)
67589ce8a63SChris Mason {
67616cdcec7SMiao Xie 	int ret;
67716cdcec7SMiao Xie 
678a698d075SMiao Xie 	ret = __btrfs_end_transaction(trans, root, 0);
67916cdcec7SMiao Xie 	if (ret)
68016cdcec7SMiao Xie 		return ret;
68116cdcec7SMiao Xie 	return 0;
68289ce8a63SChris Mason }
68389ce8a63SChris Mason 
68489ce8a63SChris Mason int btrfs_end_transaction_throttle(struct btrfs_trans_handle *trans,
68589ce8a63SChris Mason 				   struct btrfs_root *root)
68689ce8a63SChris Mason {
68716cdcec7SMiao Xie 	int ret;
68816cdcec7SMiao Xie 
689a698d075SMiao Xie 	ret = __btrfs_end_transaction(trans, root, 1);
69016cdcec7SMiao Xie 	if (ret)
69116cdcec7SMiao Xie 		return ret;
69216cdcec7SMiao Xie 	return 0;
69316cdcec7SMiao Xie }
69416cdcec7SMiao Xie 
69516cdcec7SMiao Xie int btrfs_end_transaction_dmeta(struct btrfs_trans_handle *trans,
69616cdcec7SMiao Xie 				struct btrfs_root *root)
69716cdcec7SMiao Xie {
698a698d075SMiao Xie 	return __btrfs_end_transaction(trans, root, 1);
69989ce8a63SChris Mason }
70089ce8a63SChris Mason 
701d352ac68SChris Mason /*
702d352ac68SChris Mason  * when btree blocks are allocated, they have some corresponding bits set for
703d352ac68SChris Mason  * them in one of two extent_io trees.  This is used to make sure all of
704690587d1SChris Mason  * those extents are sent to disk but does not wait on them
705d352ac68SChris Mason  */
706690587d1SChris Mason int btrfs_write_marked_extents(struct btrfs_root *root,
7078cef4e16SYan, Zheng 			       struct extent_io_tree *dirty_pages, int mark)
70879154b1bSChris Mason {
709777e6bd7SChris Mason 	int err = 0;
7107c4452b9SChris Mason 	int werr = 0;
7111728366eSJosef Bacik 	struct address_space *mapping = root->fs_info->btree_inode->i_mapping;
712e6138876SJosef Bacik 	struct extent_state *cached_state = NULL;
713777e6bd7SChris Mason 	u64 start = 0;
7145f39d397SChris Mason 	u64 end;
7157c4452b9SChris Mason 
7161728366eSJosef Bacik 	while (!find_first_extent_bit(dirty_pages, start, &start, &end,
717e6138876SJosef Bacik 				      mark, &cached_state)) {
718e6138876SJosef Bacik 		convert_extent_bit(dirty_pages, start, end, EXTENT_NEED_WAIT,
719e6138876SJosef Bacik 				   mark, &cached_state, GFP_NOFS);
720e6138876SJosef Bacik 		cached_state = NULL;
7211728366eSJosef Bacik 		err = filemap_fdatawrite_range(mapping, start, end);
7227c4452b9SChris Mason 		if (err)
7237c4452b9SChris Mason 			werr = err;
7241728366eSJosef Bacik 		cond_resched();
7251728366eSJosef Bacik 		start = end + 1;
7267c4452b9SChris Mason 	}
727690587d1SChris Mason 	if (err)
728690587d1SChris Mason 		werr = err;
729690587d1SChris Mason 	return werr;
730690587d1SChris Mason }
731690587d1SChris Mason 
732690587d1SChris Mason /*
733690587d1SChris Mason  * when btree blocks are allocated, they have some corresponding bits set for
734690587d1SChris Mason  * them in one of two extent_io trees.  This is used to make sure all of
735690587d1SChris Mason  * those extents are on disk for transaction or log commit.  We wait
736690587d1SChris Mason  * on all the pages and clear them from the dirty pages state tree
737690587d1SChris Mason  */
738690587d1SChris Mason int btrfs_wait_marked_extents(struct btrfs_root *root,
7398cef4e16SYan, Zheng 			      struct extent_io_tree *dirty_pages, int mark)
740690587d1SChris Mason {
741690587d1SChris Mason 	int err = 0;
742690587d1SChris Mason 	int werr = 0;
7431728366eSJosef Bacik 	struct address_space *mapping = root->fs_info->btree_inode->i_mapping;
744e6138876SJosef Bacik 	struct extent_state *cached_state = NULL;
745690587d1SChris Mason 	u64 start = 0;
746690587d1SChris Mason 	u64 end;
747690587d1SChris Mason 
7481728366eSJosef Bacik 	while (!find_first_extent_bit(dirty_pages, start, &start, &end,
749e6138876SJosef Bacik 				      EXTENT_NEED_WAIT, &cached_state)) {
750e6138876SJosef Bacik 		clear_extent_bit(dirty_pages, start, end, EXTENT_NEED_WAIT,
751e6138876SJosef Bacik 				 0, 0, &cached_state, GFP_NOFS);
7521728366eSJosef Bacik 		err = filemap_fdatawait_range(mapping, start, end);
753777e6bd7SChris Mason 		if (err)
754777e6bd7SChris Mason 			werr = err;
755777e6bd7SChris Mason 		cond_resched();
7561728366eSJosef Bacik 		start = end + 1;
757777e6bd7SChris Mason 	}
7587c4452b9SChris Mason 	if (err)
7597c4452b9SChris Mason 		werr = err;
7607c4452b9SChris Mason 	return werr;
76179154b1bSChris Mason }
76279154b1bSChris Mason 
763690587d1SChris Mason /*
764690587d1SChris Mason  * when btree blocks are allocated, they have some corresponding bits set for
765690587d1SChris Mason  * them in one of two extent_io trees.  This is used to make sure all of
766690587d1SChris Mason  * those extents are on disk for transaction or log commit
767690587d1SChris Mason  */
768690587d1SChris Mason int btrfs_write_and_wait_marked_extents(struct btrfs_root *root,
7698cef4e16SYan, Zheng 				struct extent_io_tree *dirty_pages, int mark)
770690587d1SChris Mason {
771690587d1SChris Mason 	int ret;
772690587d1SChris Mason 	int ret2;
773690587d1SChris Mason 
7748cef4e16SYan, Zheng 	ret = btrfs_write_marked_extents(root, dirty_pages, mark);
7758cef4e16SYan, Zheng 	ret2 = btrfs_wait_marked_extents(root, dirty_pages, mark);
776bf0da8c1SChris Mason 
777bf0da8c1SChris Mason 	if (ret)
778bf0da8c1SChris Mason 		return ret;
779bf0da8c1SChris Mason 	if (ret2)
780bf0da8c1SChris Mason 		return ret2;
781bf0da8c1SChris Mason 	return 0;
782690587d1SChris Mason }
783690587d1SChris Mason 
784d0c803c4SChris Mason int btrfs_write_and_wait_transaction(struct btrfs_trans_handle *trans,
785d0c803c4SChris Mason 				     struct btrfs_root *root)
786d0c803c4SChris Mason {
787d0c803c4SChris Mason 	if (!trans || !trans->transaction) {
788d0c803c4SChris Mason 		struct inode *btree_inode;
789d0c803c4SChris Mason 		btree_inode = root->fs_info->btree_inode;
790d0c803c4SChris Mason 		return filemap_write_and_wait(btree_inode->i_mapping);
791d0c803c4SChris Mason 	}
792d0c803c4SChris Mason 	return btrfs_write_and_wait_marked_extents(root,
7938cef4e16SYan, Zheng 					   &trans->transaction->dirty_pages,
7948cef4e16SYan, Zheng 					   EXTENT_DIRTY);
795d0c803c4SChris Mason }
796d0c803c4SChris Mason 
797d352ac68SChris Mason /*
798d352ac68SChris Mason  * this is used to update the root pointer in the tree of tree roots.
799d352ac68SChris Mason  *
800d352ac68SChris Mason  * But, in the case of the extent allocation tree, updating the root
801d352ac68SChris Mason  * pointer may allocate blocks which may change the root of the extent
802d352ac68SChris Mason  * allocation tree.
803d352ac68SChris Mason  *
804d352ac68SChris Mason  * So, this loops and repeats and makes sure the cowonly root didn't
805d352ac68SChris Mason  * change while the root pointer was being updated in the metadata.
806d352ac68SChris Mason  */
8070b86a832SChris Mason static int update_cowonly_root(struct btrfs_trans_handle *trans,
80879154b1bSChris Mason 			       struct btrfs_root *root)
80979154b1bSChris Mason {
81079154b1bSChris Mason 	int ret;
8110b86a832SChris Mason 	u64 old_root_bytenr;
81286b9f2ecSYan, Zheng 	u64 old_root_used;
8130b86a832SChris Mason 	struct btrfs_root *tree_root = root->fs_info->tree_root;
81479154b1bSChris Mason 
81586b9f2ecSYan, Zheng 	old_root_used = btrfs_root_used(&root->root_item);
8160b86a832SChris Mason 	btrfs_write_dirty_block_groups(trans, root);
81756bec294SChris Mason 
81879154b1bSChris Mason 	while (1) {
8190b86a832SChris Mason 		old_root_bytenr = btrfs_root_bytenr(&root->root_item);
82086b9f2ecSYan, Zheng 		if (old_root_bytenr == root->node->start &&
82186b9f2ecSYan, Zheng 		    old_root_used == btrfs_root_used(&root->root_item))
82279154b1bSChris Mason 			break;
82387ef2bb4SChris Mason 
8245d4f98a2SYan Zheng 		btrfs_set_root_node(&root->root_item, root->node);
82579154b1bSChris Mason 		ret = btrfs_update_root(trans, tree_root,
8260b86a832SChris Mason 					&root->root_key,
8270b86a832SChris Mason 					&root->root_item);
82849b25e05SJeff Mahoney 		if (ret)
82949b25e05SJeff Mahoney 			return ret;
83056bec294SChris Mason 
83186b9f2ecSYan, Zheng 		old_root_used = btrfs_root_used(&root->root_item);
8324a8c9a62SYan Zheng 		ret = btrfs_write_dirty_block_groups(trans, root);
83349b25e05SJeff Mahoney 		if (ret)
83449b25e05SJeff Mahoney 			return ret;
8350b86a832SChris Mason 	}
836276e680dSYan Zheng 
837276e680dSYan Zheng 	if (root != root->fs_info->extent_root)
838817d52f8SJosef Bacik 		switch_commit_root(root);
839276e680dSYan Zheng 
8400b86a832SChris Mason 	return 0;
8410b86a832SChris Mason }
8420b86a832SChris Mason 
843d352ac68SChris Mason /*
844d352ac68SChris Mason  * update all the cowonly tree roots on disk
84549b25e05SJeff Mahoney  *
84649b25e05SJeff Mahoney  * The error handling in this function may not be obvious. Any of the
84749b25e05SJeff Mahoney  * failures will cause the file system to go offline. We still need
84849b25e05SJeff Mahoney  * to clean up the delayed refs.
849d352ac68SChris Mason  */
8505d4f98a2SYan Zheng static noinline int commit_cowonly_roots(struct btrfs_trans_handle *trans,
8510b86a832SChris Mason 					 struct btrfs_root *root)
8520b86a832SChris Mason {
8530b86a832SChris Mason 	struct btrfs_fs_info *fs_info = root->fs_info;
8540b86a832SChris Mason 	struct list_head *next;
85584234f3aSYan Zheng 	struct extent_buffer *eb;
85656bec294SChris Mason 	int ret;
85784234f3aSYan Zheng 
85856bec294SChris Mason 	ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
85949b25e05SJeff Mahoney 	if (ret)
86049b25e05SJeff Mahoney 		return ret;
86187ef2bb4SChris Mason 
86284234f3aSYan Zheng 	eb = btrfs_lock_root_node(fs_info->tree_root);
86349b25e05SJeff Mahoney 	ret = btrfs_cow_block(trans, fs_info->tree_root, eb, NULL,
86449b25e05SJeff Mahoney 			      0, &eb);
86584234f3aSYan Zheng 	btrfs_tree_unlock(eb);
86684234f3aSYan Zheng 	free_extent_buffer(eb);
8670b86a832SChris Mason 
86849b25e05SJeff Mahoney 	if (ret)
86949b25e05SJeff Mahoney 		return ret;
87049b25e05SJeff Mahoney 
87156bec294SChris Mason 	ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
87249b25e05SJeff Mahoney 	if (ret)
87349b25e05SJeff Mahoney 		return ret;
87487ef2bb4SChris Mason 
875733f4fbbSStefan Behrens 	ret = btrfs_run_dev_stats(trans, root->fs_info);
8768dabb742SStefan Behrens 	WARN_ON(ret);
8778dabb742SStefan Behrens 	ret = btrfs_run_dev_replace(trans, root->fs_info);
8788dabb742SStefan Behrens 	WARN_ON(ret);
879733f4fbbSStefan Behrens 
880546adb0dSJan Schmidt 	ret = btrfs_run_qgroups(trans, root->fs_info);
881546adb0dSJan Schmidt 	BUG_ON(ret);
882546adb0dSJan Schmidt 
883546adb0dSJan Schmidt 	/* run_qgroups might have added some more refs */
884546adb0dSJan Schmidt 	ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
885546adb0dSJan Schmidt 	BUG_ON(ret);
886546adb0dSJan Schmidt 
8870b86a832SChris Mason 	while (!list_empty(&fs_info->dirty_cowonly_roots)) {
8880b86a832SChris Mason 		next = fs_info->dirty_cowonly_roots.next;
8890b86a832SChris Mason 		list_del_init(next);
8900b86a832SChris Mason 		root = list_entry(next, struct btrfs_root, dirty_list);
89187ef2bb4SChris Mason 
89249b25e05SJeff Mahoney 		ret = update_cowonly_root(trans, root);
89349b25e05SJeff Mahoney 		if (ret)
89449b25e05SJeff Mahoney 			return ret;
89579154b1bSChris Mason 	}
896276e680dSYan Zheng 
897276e680dSYan Zheng 	down_write(&fs_info->extent_commit_sem);
898276e680dSYan Zheng 	switch_commit_root(fs_info->extent_root);
899276e680dSYan Zheng 	up_write(&fs_info->extent_commit_sem);
900276e680dSYan Zheng 
9018dabb742SStefan Behrens 	btrfs_after_dev_replace_commit(fs_info);
9028dabb742SStefan Behrens 
90379154b1bSChris Mason 	return 0;
90479154b1bSChris Mason }
90579154b1bSChris Mason 
906d352ac68SChris Mason /*
907d352ac68SChris Mason  * dead roots are old snapshots that need to be deleted.  This allocates
908d352ac68SChris Mason  * a dirty root struct and adds it into the list of dead roots that need to
909d352ac68SChris Mason  * be deleted
910d352ac68SChris Mason  */
9115d4f98a2SYan Zheng int btrfs_add_dead_root(struct btrfs_root *root)
9125eda7b5eSChris Mason {
913a4abeea4SJosef Bacik 	spin_lock(&root->fs_info->trans_lock);
9145d4f98a2SYan Zheng 	list_add(&root->root_list, &root->fs_info->dead_roots);
915a4abeea4SJosef Bacik 	spin_unlock(&root->fs_info->trans_lock);
9165eda7b5eSChris Mason 	return 0;
9175eda7b5eSChris Mason }
9185eda7b5eSChris Mason 
919d352ac68SChris Mason /*
9205d4f98a2SYan Zheng  * update all the cowonly tree roots on disk
921d352ac68SChris Mason  */
9225d4f98a2SYan Zheng static noinline int commit_fs_roots(struct btrfs_trans_handle *trans,
9235d4f98a2SYan Zheng 				    struct btrfs_root *root)
9240f7d52f4SChris Mason {
9250f7d52f4SChris Mason 	struct btrfs_root *gang[8];
9265d4f98a2SYan Zheng 	struct btrfs_fs_info *fs_info = root->fs_info;
9270f7d52f4SChris Mason 	int i;
9280f7d52f4SChris Mason 	int ret;
92954aa1f4dSChris Mason 	int err = 0;
93054aa1f4dSChris Mason 
931a4abeea4SJosef Bacik 	spin_lock(&fs_info->fs_roots_radix_lock);
9320f7d52f4SChris Mason 	while (1) {
9335d4f98a2SYan Zheng 		ret = radix_tree_gang_lookup_tag(&fs_info->fs_roots_radix,
9345d4f98a2SYan Zheng 						 (void **)gang, 0,
9350f7d52f4SChris Mason 						 ARRAY_SIZE(gang),
9360f7d52f4SChris Mason 						 BTRFS_ROOT_TRANS_TAG);
9370f7d52f4SChris Mason 		if (ret == 0)
9380f7d52f4SChris Mason 			break;
9390f7d52f4SChris Mason 		for (i = 0; i < ret; i++) {
9400f7d52f4SChris Mason 			root = gang[i];
9415d4f98a2SYan Zheng 			radix_tree_tag_clear(&fs_info->fs_roots_radix,
9422619ba1fSChris Mason 					(unsigned long)root->root_key.objectid,
9430f7d52f4SChris Mason 					BTRFS_ROOT_TRANS_TAG);
944a4abeea4SJosef Bacik 			spin_unlock(&fs_info->fs_roots_radix_lock);
94531153d81SYan Zheng 
946e02119d5SChris Mason 			btrfs_free_log(trans, root);
9475d4f98a2SYan Zheng 			btrfs_update_reloc_root(trans, root);
948d68fc57bSYan, Zheng 			btrfs_orphan_commit_root(trans, root);
949e02119d5SChris Mason 
95082d5902dSLi Zefan 			btrfs_save_ino_cache(root, trans);
95182d5902dSLi Zefan 
952f1ebcc74SLiu Bo 			/* see comments in should_cow_block() */
953f1ebcc74SLiu Bo 			root->force_cow = 0;
954f1ebcc74SLiu Bo 			smp_wmb();
955f1ebcc74SLiu Bo 
956978d910dSYan Zheng 			if (root->commit_root != root->node) {
957581bb050SLi Zefan 				mutex_lock(&root->fs_commit_mutex);
958817d52f8SJosef Bacik 				switch_commit_root(root);
959581bb050SLi Zefan 				btrfs_unpin_free_ino(root);
960581bb050SLi Zefan 				mutex_unlock(&root->fs_commit_mutex);
961581bb050SLi Zefan 
962978d910dSYan Zheng 				btrfs_set_root_node(&root->root_item,
963978d910dSYan Zheng 						    root->node);
964978d910dSYan Zheng 			}
96531153d81SYan Zheng 
9665d4f98a2SYan Zheng 			err = btrfs_update_root(trans, fs_info->tree_root,
9670f7d52f4SChris Mason 						&root->root_key,
9680f7d52f4SChris Mason 						&root->root_item);
969a4abeea4SJosef Bacik 			spin_lock(&fs_info->fs_roots_radix_lock);
97054aa1f4dSChris Mason 			if (err)
97154aa1f4dSChris Mason 				break;
9720f7d52f4SChris Mason 		}
9739f3a7427SChris Mason 	}
974a4abeea4SJosef Bacik 	spin_unlock(&fs_info->fs_roots_radix_lock);
97554aa1f4dSChris Mason 	return err;
9760f7d52f4SChris Mason }
9770f7d52f4SChris Mason 
978d352ac68SChris Mason /*
979de78b51aSEric Sandeen  * defrag a given btree.
980de78b51aSEric Sandeen  * Every leaf in the btree is read and defragged.
981d352ac68SChris Mason  */
982de78b51aSEric Sandeen int btrfs_defrag_root(struct btrfs_root *root)
983e9d0b13bSChris Mason {
984e9d0b13bSChris Mason 	struct btrfs_fs_info *info = root->fs_info;
985e9d0b13bSChris Mason 	struct btrfs_trans_handle *trans;
9868929ecfaSYan, Zheng 	int ret;
987e9d0b13bSChris Mason 
9888929ecfaSYan, Zheng 	if (xchg(&root->defrag_running, 1))
989e9d0b13bSChris Mason 		return 0;
9908929ecfaSYan, Zheng 
9916b80053dSChris Mason 	while (1) {
9928929ecfaSYan, Zheng 		trans = btrfs_start_transaction(root, 0);
9938929ecfaSYan, Zheng 		if (IS_ERR(trans))
9948929ecfaSYan, Zheng 			return PTR_ERR(trans);
9958929ecfaSYan, Zheng 
996de78b51aSEric Sandeen 		ret = btrfs_defrag_leaves(trans, root);
9978929ecfaSYan, Zheng 
998e9d0b13bSChris Mason 		btrfs_end_transaction(trans, root);
999b53d3f5dSLiu Bo 		btrfs_btree_balance_dirty(info->tree_root);
1000e9d0b13bSChris Mason 		cond_resched();
1001e9d0b13bSChris Mason 
10027841cb28SDavid Sterba 		if (btrfs_fs_closing(root->fs_info) || ret != -EAGAIN)
1003e9d0b13bSChris Mason 			break;
1004210549ebSDavid Sterba 
1005210549ebSDavid Sterba 		if (btrfs_defrag_cancelled(root->fs_info)) {
1006210549ebSDavid Sterba 			printk(KERN_DEBUG "btrfs: defrag_root cancelled\n");
1007210549ebSDavid Sterba 			ret = -EAGAIN;
1008210549ebSDavid Sterba 			break;
1009210549ebSDavid Sterba 		}
1010e9d0b13bSChris Mason 	}
1011e9d0b13bSChris Mason 	root->defrag_running = 0;
10128929ecfaSYan, Zheng 	return ret;
1013e9d0b13bSChris Mason }
1014e9d0b13bSChris Mason 
1015d352ac68SChris Mason /*
1016d352ac68SChris Mason  * new snapshots need to be created at a very specific time in the
1017d352ac68SChris Mason  * transaction commit.  This does the actual creation
1018d352ac68SChris Mason  */
101980b6794dSChris Mason static noinline int create_pending_snapshot(struct btrfs_trans_handle *trans,
10203063d29fSChris Mason 				   struct btrfs_fs_info *fs_info,
10213063d29fSChris Mason 				   struct btrfs_pending_snapshot *pending)
10223063d29fSChris Mason {
10233063d29fSChris Mason 	struct btrfs_key key;
102480b6794dSChris Mason 	struct btrfs_root_item *new_root_item;
10253063d29fSChris Mason 	struct btrfs_root *tree_root = fs_info->tree_root;
10263063d29fSChris Mason 	struct btrfs_root *root = pending->root;
10276bdb72deSSage Weil 	struct btrfs_root *parent_root;
102898c9942aSLiu Bo 	struct btrfs_block_rsv *rsv;
10296bdb72deSSage Weil 	struct inode *parent_inode;
103042874b3dSMiao Xie 	struct btrfs_path *path;
103142874b3dSMiao Xie 	struct btrfs_dir_item *dir_item;
10326a912213SJosef Bacik 	struct dentry *parent;
1033a22285a6SYan, Zheng 	struct dentry *dentry;
10343063d29fSChris Mason 	struct extent_buffer *tmp;
1035925baeddSChris Mason 	struct extent_buffer *old;
10368ea05e3aSAlexander Block 	struct timespec cur_time = CURRENT_TIME;
10373063d29fSChris Mason 	int ret;
1038d68fc57bSYan, Zheng 	u64 to_reserve = 0;
10396bdb72deSSage Weil 	u64 index = 0;
1040a22285a6SYan, Zheng 	u64 objectid;
1041b83cc969SLi Zefan 	u64 root_flags;
10428ea05e3aSAlexander Block 	uuid_le new_uuid;
10433063d29fSChris Mason 
104442874b3dSMiao Xie 	path = btrfs_alloc_path();
104542874b3dSMiao Xie 	if (!path) {
104642874b3dSMiao Xie 		ret = pending->error = -ENOMEM;
104742874b3dSMiao Xie 		goto path_alloc_fail;
104842874b3dSMiao Xie 	}
104942874b3dSMiao Xie 
105080b6794dSChris Mason 	new_root_item = kmalloc(sizeof(*new_root_item), GFP_NOFS);
105180b6794dSChris Mason 	if (!new_root_item) {
105249b25e05SJeff Mahoney 		ret = pending->error = -ENOMEM;
10536fa9700eSMiao Xie 		goto root_item_alloc_fail;
105480b6794dSChris Mason 	}
1055a22285a6SYan, Zheng 
1056581bb050SLi Zefan 	ret = btrfs_find_free_objectid(tree_root, &objectid);
1057a22285a6SYan, Zheng 	if (ret) {
1058a22285a6SYan, Zheng 		pending->error = ret;
10596fa9700eSMiao Xie 		goto no_free_objectid;
1060a22285a6SYan, Zheng 	}
10613063d29fSChris Mason 
10623fd0a558SYan, Zheng 	btrfs_reloc_pre_snapshot(trans, pending, &to_reserve);
1063d68fc57bSYan, Zheng 
1064d68fc57bSYan, Zheng 	if (to_reserve > 0) {
106508e007d2SMiao Xie 		ret = btrfs_block_rsv_add(root, &pending->block_rsv,
106608e007d2SMiao Xie 					  to_reserve,
106708e007d2SMiao Xie 					  BTRFS_RESERVE_NO_FLUSH);
1068d68fc57bSYan, Zheng 		if (ret) {
1069d68fc57bSYan, Zheng 			pending->error = ret;
10706fa9700eSMiao Xie 			goto no_free_objectid;
1071d68fc57bSYan, Zheng 		}
1072d68fc57bSYan, Zheng 	}
1073d68fc57bSYan, Zheng 
10746f72c7e2SArne Jansen 	ret = btrfs_qgroup_inherit(trans, fs_info, root->root_key.objectid,
10756f72c7e2SArne Jansen 				   objectid, pending->inherit);
10766f72c7e2SArne Jansen 	if (ret) {
10776f72c7e2SArne Jansen 		pending->error = ret;
10786fa9700eSMiao Xie 		goto no_free_objectid;
10796f72c7e2SArne Jansen 	}
10806f72c7e2SArne Jansen 
10813063d29fSChris Mason 	key.objectid = objectid;
1082a22285a6SYan, Zheng 	key.offset = (u64)-1;
1083a22285a6SYan, Zheng 	key.type = BTRFS_ROOT_ITEM_KEY;
10843063d29fSChris Mason 
10856fa9700eSMiao Xie 	rsv = trans->block_rsv;
1086a22285a6SYan, Zheng 	trans->block_rsv = &pending->block_rsv;
10876bdb72deSSage Weil 
1088a22285a6SYan, Zheng 	dentry = pending->dentry;
10896a912213SJosef Bacik 	parent = dget_parent(dentry);
10906a912213SJosef Bacik 	parent_inode = parent->d_inode;
1091a22285a6SYan, Zheng 	parent_root = BTRFS_I(parent_inode)->root;
10927585717fSChris Mason 	record_root_in_trans(trans, parent_root);
1093a22285a6SYan, Zheng 
10946bdb72deSSage Weil 	/*
10956bdb72deSSage Weil 	 * insert the directory item
10966bdb72deSSage Weil 	 */
10976bdb72deSSage Weil 	ret = btrfs_set_inode_index(parent_inode, &index);
109849b25e05SJeff Mahoney 	BUG_ON(ret); /* -ENOMEM */
109942874b3dSMiao Xie 
110042874b3dSMiao Xie 	/* check if there is a file/dir which has the same name. */
110142874b3dSMiao Xie 	dir_item = btrfs_lookup_dir_item(NULL, parent_root, path,
110242874b3dSMiao Xie 					 btrfs_ino(parent_inode),
110342874b3dSMiao Xie 					 dentry->d_name.name,
110442874b3dSMiao Xie 					 dentry->d_name.len, 0);
110542874b3dSMiao Xie 	if (dir_item != NULL && !IS_ERR(dir_item)) {
1106fe66a05aSChris Mason 		pending->error = -EEXIST;
1107fe66a05aSChris Mason 		goto fail;
110842874b3dSMiao Xie 	} else if (IS_ERR(dir_item)) {
110942874b3dSMiao Xie 		ret = PTR_ERR(dir_item);
11108732d44fSMiao Xie 		btrfs_abort_transaction(trans, root, ret);
11118732d44fSMiao Xie 		goto fail;
111279787eaaSJeff Mahoney 	}
111342874b3dSMiao Xie 	btrfs_release_path(path);
11146bdb72deSSage Weil 
1115e999376fSChris Mason 	/*
1116e999376fSChris Mason 	 * pull in the delayed directory update
1117e999376fSChris Mason 	 * and the delayed inode item
1118e999376fSChris Mason 	 * otherwise we corrupt the FS during
1119e999376fSChris Mason 	 * snapshot
1120e999376fSChris Mason 	 */
1121e999376fSChris Mason 	ret = btrfs_run_delayed_items(trans, root);
11228732d44fSMiao Xie 	if (ret) {	/* Transaction aborted */
11238732d44fSMiao Xie 		btrfs_abort_transaction(trans, root, ret);
11248732d44fSMiao Xie 		goto fail;
11258732d44fSMiao Xie 	}
1126e999376fSChris Mason 
11277585717fSChris Mason 	record_root_in_trans(trans, root);
11286bdb72deSSage Weil 	btrfs_set_root_last_snapshot(&root->root_item, trans->transid);
11296bdb72deSSage Weil 	memcpy(new_root_item, &root->root_item, sizeof(*new_root_item));
113008fe4db1SLi Zefan 	btrfs_check_and_init_root_item(new_root_item);
11316bdb72deSSage Weil 
1132b83cc969SLi Zefan 	root_flags = btrfs_root_flags(new_root_item);
1133b83cc969SLi Zefan 	if (pending->readonly)
1134b83cc969SLi Zefan 		root_flags |= BTRFS_ROOT_SUBVOL_RDONLY;
1135b83cc969SLi Zefan 	else
1136b83cc969SLi Zefan 		root_flags &= ~BTRFS_ROOT_SUBVOL_RDONLY;
1137b83cc969SLi Zefan 	btrfs_set_root_flags(new_root_item, root_flags);
1138b83cc969SLi Zefan 
11398ea05e3aSAlexander Block 	btrfs_set_root_generation_v2(new_root_item,
11408ea05e3aSAlexander Block 			trans->transid);
11418ea05e3aSAlexander Block 	uuid_le_gen(&new_uuid);
11428ea05e3aSAlexander Block 	memcpy(new_root_item->uuid, new_uuid.b, BTRFS_UUID_SIZE);
11438ea05e3aSAlexander Block 	memcpy(new_root_item->parent_uuid, root->root_item.uuid,
11448ea05e3aSAlexander Block 			BTRFS_UUID_SIZE);
11458ea05e3aSAlexander Block 	new_root_item->otime.sec = cpu_to_le64(cur_time.tv_sec);
1146dadd1105SDan Carpenter 	new_root_item->otime.nsec = cpu_to_le32(cur_time.tv_nsec);
11478ea05e3aSAlexander Block 	btrfs_set_root_otransid(new_root_item, trans->transid);
11488ea05e3aSAlexander Block 	memset(&new_root_item->stime, 0, sizeof(new_root_item->stime));
11498ea05e3aSAlexander Block 	memset(&new_root_item->rtime, 0, sizeof(new_root_item->rtime));
11508ea05e3aSAlexander Block 	btrfs_set_root_stransid(new_root_item, 0);
11518ea05e3aSAlexander Block 	btrfs_set_root_rtransid(new_root_item, 0);
11528ea05e3aSAlexander Block 
1153925baeddSChris Mason 	old = btrfs_lock_root_node(root);
115449b25e05SJeff Mahoney 	ret = btrfs_cow_block(trans, root, old, NULL, 0, &old);
115579787eaaSJeff Mahoney 	if (ret) {
115679787eaaSJeff Mahoney 		btrfs_tree_unlock(old);
115779787eaaSJeff Mahoney 		free_extent_buffer(old);
11588732d44fSMiao Xie 		btrfs_abort_transaction(trans, root, ret);
11598732d44fSMiao Xie 		goto fail;
116079787eaaSJeff Mahoney 	}
116149b25e05SJeff Mahoney 
11625d4f98a2SYan Zheng 	btrfs_set_lock_blocking(old);
11633063d29fSChris Mason 
116449b25e05SJeff Mahoney 	ret = btrfs_copy_root(trans, root, old, &tmp, objectid);
116579787eaaSJeff Mahoney 	/* clean up in any case */
1166925baeddSChris Mason 	btrfs_tree_unlock(old);
1167925baeddSChris Mason 	free_extent_buffer(old);
11688732d44fSMiao Xie 	if (ret) {
11698732d44fSMiao Xie 		btrfs_abort_transaction(trans, root, ret);
11708732d44fSMiao Xie 		goto fail;
11718732d44fSMiao Xie 	}
11723063d29fSChris Mason 
1173f1ebcc74SLiu Bo 	/* see comments in should_cow_block() */
1174f1ebcc74SLiu Bo 	root->force_cow = 1;
1175f1ebcc74SLiu Bo 	smp_wmb();
1176f1ebcc74SLiu Bo 
11775d4f98a2SYan Zheng 	btrfs_set_root_node(new_root_item, tmp);
1178a22285a6SYan, Zheng 	/* record when the snapshot was created in key.offset */
1179a22285a6SYan, Zheng 	key.offset = trans->transid;
1180a22285a6SYan, Zheng 	ret = btrfs_insert_root(trans, tree_root, &key, new_root_item);
1181925baeddSChris Mason 	btrfs_tree_unlock(tmp);
11823063d29fSChris Mason 	free_extent_buffer(tmp);
11838732d44fSMiao Xie 	if (ret) {
11848732d44fSMiao Xie 		btrfs_abort_transaction(trans, root, ret);
11858732d44fSMiao Xie 		goto fail;
11868732d44fSMiao Xie 	}
11870660b5afSChris Mason 
1188a22285a6SYan, Zheng 	/*
1189a22285a6SYan, Zheng 	 * insert root back/forward references
1190a22285a6SYan, Zheng 	 */
1191a22285a6SYan, Zheng 	ret = btrfs_add_root_ref(trans, tree_root, objectid,
1192a22285a6SYan, Zheng 				 parent_root->root_key.objectid,
119333345d01SLi Zefan 				 btrfs_ino(parent_inode), index,
1194a22285a6SYan, Zheng 				 dentry->d_name.name, dentry->d_name.len);
11958732d44fSMiao Xie 	if (ret) {
11968732d44fSMiao Xie 		btrfs_abort_transaction(trans, root, ret);
11978732d44fSMiao Xie 		goto fail;
11988732d44fSMiao Xie 	}
1199a22285a6SYan, Zheng 
1200a22285a6SYan, Zheng 	key.offset = (u64)-1;
1201a22285a6SYan, Zheng 	pending->snap = btrfs_read_fs_root_no_name(root->fs_info, &key);
120279787eaaSJeff Mahoney 	if (IS_ERR(pending->snap)) {
120379787eaaSJeff Mahoney 		ret = PTR_ERR(pending->snap);
12048732d44fSMiao Xie 		btrfs_abort_transaction(trans, root, ret);
12058732d44fSMiao Xie 		goto fail;
120679787eaaSJeff Mahoney 	}
1207d68fc57bSYan, Zheng 
120849b25e05SJeff Mahoney 	ret = btrfs_reloc_post_snapshot(trans, pending);
12098732d44fSMiao Xie 	if (ret) {
12108732d44fSMiao Xie 		btrfs_abort_transaction(trans, root, ret);
12118732d44fSMiao Xie 		goto fail;
12128732d44fSMiao Xie 	}
1213361048f5SMiao Xie 
1214361048f5SMiao Xie 	ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
12158732d44fSMiao Xie 	if (ret) {
12168732d44fSMiao Xie 		btrfs_abort_transaction(trans, root, ret);
12178732d44fSMiao Xie 		goto fail;
12188732d44fSMiao Xie 	}
121942874b3dSMiao Xie 
122042874b3dSMiao Xie 	ret = btrfs_insert_dir_item(trans, parent_root,
122142874b3dSMiao Xie 				    dentry->d_name.name, dentry->d_name.len,
122242874b3dSMiao Xie 				    parent_inode, &key,
122342874b3dSMiao Xie 				    BTRFS_FT_DIR, index);
122442874b3dSMiao Xie 	/* We have check then name at the beginning, so it is impossible. */
12259c52057cSChris Mason 	BUG_ON(ret == -EEXIST || ret == -EOVERFLOW);
12268732d44fSMiao Xie 	if (ret) {
12278732d44fSMiao Xie 		btrfs_abort_transaction(trans, root, ret);
12288732d44fSMiao Xie 		goto fail;
12298732d44fSMiao Xie 	}
123042874b3dSMiao Xie 
123142874b3dSMiao Xie 	btrfs_i_size_write(parent_inode, parent_inode->i_size +
123242874b3dSMiao Xie 					 dentry->d_name.len * 2);
123342874b3dSMiao Xie 	parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
1234be6aef60SJosef Bacik 	ret = btrfs_update_inode_fallback(trans, parent_root, parent_inode);
123542874b3dSMiao Xie 	if (ret)
12368732d44fSMiao Xie 		btrfs_abort_transaction(trans, root, ret);
12373063d29fSChris Mason fail:
12386fa9700eSMiao Xie 	dput(parent);
123998c9942aSLiu Bo 	trans->block_rsv = rsv;
12406fa9700eSMiao Xie no_free_objectid:
12416fa9700eSMiao Xie 	kfree(new_root_item);
12426fa9700eSMiao Xie root_item_alloc_fail:
124342874b3dSMiao Xie 	btrfs_free_path(path);
124442874b3dSMiao Xie path_alloc_fail:
1245a22285a6SYan, Zheng 	btrfs_block_rsv_release(root, &pending->block_rsv, (u64)-1);
124649b25e05SJeff Mahoney 	return ret;
12473063d29fSChris Mason }
12483063d29fSChris Mason 
1249d352ac68SChris Mason /*
1250d352ac68SChris Mason  * create all the snapshots we've scheduled for creation
1251d352ac68SChris Mason  */
125280b6794dSChris Mason static noinline int create_pending_snapshots(struct btrfs_trans_handle *trans,
12533063d29fSChris Mason 					     struct btrfs_fs_info *fs_info)
12543063d29fSChris Mason {
12553063d29fSChris Mason 	struct btrfs_pending_snapshot *pending;
12563063d29fSChris Mason 	struct list_head *head = &trans->transaction->pending_snapshots;
12573de4586cSChris Mason 
1258fe66a05aSChris Mason 	list_for_each_entry(pending, head, list)
1259fe66a05aSChris Mason 		create_pending_snapshot(trans, fs_info, pending);
12603de4586cSChris Mason 	return 0;
12613de4586cSChris Mason }
12623de4586cSChris Mason 
12635d4f98a2SYan Zheng static void update_super_roots(struct btrfs_root *root)
12645d4f98a2SYan Zheng {
12655d4f98a2SYan Zheng 	struct btrfs_root_item *root_item;
12665d4f98a2SYan Zheng 	struct btrfs_super_block *super;
12675d4f98a2SYan Zheng 
12686c41761fSDavid Sterba 	super = root->fs_info->super_copy;
12695d4f98a2SYan Zheng 
12705d4f98a2SYan Zheng 	root_item = &root->fs_info->chunk_root->root_item;
12715d4f98a2SYan Zheng 	super->chunk_root = root_item->bytenr;
12725d4f98a2SYan Zheng 	super->chunk_root_generation = root_item->generation;
12735d4f98a2SYan Zheng 	super->chunk_root_level = root_item->level;
12745d4f98a2SYan Zheng 
12755d4f98a2SYan Zheng 	root_item = &root->fs_info->tree_root->root_item;
12765d4f98a2SYan Zheng 	super->root = root_item->bytenr;
12775d4f98a2SYan Zheng 	super->generation = root_item->generation;
12785d4f98a2SYan Zheng 	super->root_level = root_item->level;
127973bc1876SJosef Bacik 	if (btrfs_test_opt(root, SPACE_CACHE))
12800af3d00bSJosef Bacik 		super->cache_generation = root_item->generation;
12815d4f98a2SYan Zheng }
12825d4f98a2SYan Zheng 
1283f36f3042SChris Mason int btrfs_transaction_in_commit(struct btrfs_fs_info *info)
1284f36f3042SChris Mason {
1285f36f3042SChris Mason 	int ret = 0;
1286a4abeea4SJosef Bacik 	spin_lock(&info->trans_lock);
1287f36f3042SChris Mason 	if (info->running_transaction)
1288f36f3042SChris Mason 		ret = info->running_transaction->in_commit;
1289a4abeea4SJosef Bacik 	spin_unlock(&info->trans_lock);
1290f36f3042SChris Mason 	return ret;
1291f36f3042SChris Mason }
1292f36f3042SChris Mason 
12938929ecfaSYan, Zheng int btrfs_transaction_blocked(struct btrfs_fs_info *info)
12948929ecfaSYan, Zheng {
12958929ecfaSYan, Zheng 	int ret = 0;
1296a4abeea4SJosef Bacik 	spin_lock(&info->trans_lock);
12978929ecfaSYan, Zheng 	if (info->running_transaction)
12988929ecfaSYan, Zheng 		ret = info->running_transaction->blocked;
1299a4abeea4SJosef Bacik 	spin_unlock(&info->trans_lock);
13008929ecfaSYan, Zheng 	return ret;
13018929ecfaSYan, Zheng }
13028929ecfaSYan, Zheng 
1303bb9c12c9SSage Weil /*
1304bb9c12c9SSage Weil  * wait for the current transaction commit to start and block subsequent
1305bb9c12c9SSage Weil  * transaction joins
1306bb9c12c9SSage Weil  */
1307bb9c12c9SSage Weil static void wait_current_trans_commit_start(struct btrfs_root *root,
1308bb9c12c9SSage Weil 					    struct btrfs_transaction *trans)
1309bb9c12c9SSage Weil {
131072d63ed6SLi Zefan 	wait_event(root->fs_info->transaction_blocked_wait, trans->in_commit);
1311bb9c12c9SSage Weil }
1312bb9c12c9SSage Weil 
1313bb9c12c9SSage Weil /*
1314bb9c12c9SSage Weil  * wait for the current transaction to start and then become unblocked.
1315bb9c12c9SSage Weil  * caller holds ref.
1316bb9c12c9SSage Weil  */
1317bb9c12c9SSage Weil static void wait_current_trans_commit_start_and_unblock(struct btrfs_root *root,
1318bb9c12c9SSage Weil 					 struct btrfs_transaction *trans)
1319bb9c12c9SSage Weil {
132072d63ed6SLi Zefan 	wait_event(root->fs_info->transaction_wait,
132172d63ed6SLi Zefan 		   trans->commit_done || (trans->in_commit && !trans->blocked));
1322bb9c12c9SSage Weil }
1323bb9c12c9SSage Weil 
1324bb9c12c9SSage Weil /*
1325bb9c12c9SSage Weil  * commit transactions asynchronously. once btrfs_commit_transaction_async
1326bb9c12c9SSage Weil  * returns, any subsequent transaction will not be allowed to join.
1327bb9c12c9SSage Weil  */
1328bb9c12c9SSage Weil struct btrfs_async_commit {
1329bb9c12c9SSage Weil 	struct btrfs_trans_handle *newtrans;
1330bb9c12c9SSage Weil 	struct btrfs_root *root;
13317892b5afSMiao Xie 	struct work_struct work;
1332bb9c12c9SSage Weil };
1333bb9c12c9SSage Weil 
1334bb9c12c9SSage Weil static void do_async_commit(struct work_struct *work)
1335bb9c12c9SSage Weil {
1336bb9c12c9SSage Weil 	struct btrfs_async_commit *ac =
13377892b5afSMiao Xie 		container_of(work, struct btrfs_async_commit, work);
1338bb9c12c9SSage Weil 
13396fc4e354SSage Weil 	/*
13406fc4e354SSage Weil 	 * We've got freeze protection passed with the transaction.
13416fc4e354SSage Weil 	 * Tell lockdep about it.
13426fc4e354SSage Weil 	 */
1343ff7c1d33SMiao Xie 	if (ac->newtrans->type < TRANS_JOIN_NOLOCK)
13446fc4e354SSage Weil 		rwsem_acquire_read(
13456fc4e354SSage Weil 		     &ac->root->fs_info->sb->s_writers.lock_map[SB_FREEZE_FS-1],
13466fc4e354SSage Weil 		     0, 1, _THIS_IP_);
13476fc4e354SSage Weil 
1348e209db7aSSage Weil 	current->journal_info = ac->newtrans;
1349e209db7aSSage Weil 
1350bb9c12c9SSage Weil 	btrfs_commit_transaction(ac->newtrans, ac->root);
1351bb9c12c9SSage Weil 	kfree(ac);
1352bb9c12c9SSage Weil }
1353bb9c12c9SSage Weil 
1354bb9c12c9SSage Weil int btrfs_commit_transaction_async(struct btrfs_trans_handle *trans,
1355bb9c12c9SSage Weil 				   struct btrfs_root *root,
1356bb9c12c9SSage Weil 				   int wait_for_unblock)
1357bb9c12c9SSage Weil {
1358bb9c12c9SSage Weil 	struct btrfs_async_commit *ac;
1359bb9c12c9SSage Weil 	struct btrfs_transaction *cur_trans;
1360bb9c12c9SSage Weil 
1361bb9c12c9SSage Weil 	ac = kmalloc(sizeof(*ac), GFP_NOFS);
1362db5b493aSTsutomu Itoh 	if (!ac)
1363db5b493aSTsutomu Itoh 		return -ENOMEM;
1364bb9c12c9SSage Weil 
13657892b5afSMiao Xie 	INIT_WORK(&ac->work, do_async_commit);
1366bb9c12c9SSage Weil 	ac->root = root;
13677a7eaa40SJosef Bacik 	ac->newtrans = btrfs_join_transaction(root);
13683612b495STsutomu Itoh 	if (IS_ERR(ac->newtrans)) {
13693612b495STsutomu Itoh 		int err = PTR_ERR(ac->newtrans);
13703612b495STsutomu Itoh 		kfree(ac);
13713612b495STsutomu Itoh 		return err;
13723612b495STsutomu Itoh 	}
1373bb9c12c9SSage Weil 
1374bb9c12c9SSage Weil 	/* take transaction reference */
1375bb9c12c9SSage Weil 	cur_trans = trans->transaction;
137613c5a93eSJosef Bacik 	atomic_inc(&cur_trans->use_count);
1377bb9c12c9SSage Weil 
1378bb9c12c9SSage Weil 	btrfs_end_transaction(trans, root);
13796fc4e354SSage Weil 
13806fc4e354SSage Weil 	/*
13816fc4e354SSage Weil 	 * Tell lockdep we've released the freeze rwsem, since the
13826fc4e354SSage Weil 	 * async commit thread will be the one to unlock it.
13836fc4e354SSage Weil 	 */
1384ff7c1d33SMiao Xie 	if (trans->type < TRANS_JOIN_NOLOCK)
1385ff7c1d33SMiao Xie 		rwsem_release(
1386ff7c1d33SMiao Xie 			&root->fs_info->sb->s_writers.lock_map[SB_FREEZE_FS-1],
13876fc4e354SSage Weil 			1, _THIS_IP_);
13886fc4e354SSage Weil 
13897892b5afSMiao Xie 	schedule_work(&ac->work);
1390bb9c12c9SSage Weil 
1391bb9c12c9SSage Weil 	/* wait for transaction to start and unblock */
1392bb9c12c9SSage Weil 	if (wait_for_unblock)
1393bb9c12c9SSage Weil 		wait_current_trans_commit_start_and_unblock(root, cur_trans);
1394bb9c12c9SSage Weil 	else
1395bb9c12c9SSage Weil 		wait_current_trans_commit_start(root, cur_trans);
1396bb9c12c9SSage Weil 
139738e88054SSage Weil 	if (current->journal_info == trans)
139838e88054SSage Weil 		current->journal_info = NULL;
139938e88054SSage Weil 
140038e88054SSage Weil 	put_transaction(cur_trans);
1401bb9c12c9SSage Weil 	return 0;
1402bb9c12c9SSage Weil }
1403bb9c12c9SSage Weil 
140449b25e05SJeff Mahoney 
140549b25e05SJeff Mahoney static void cleanup_transaction(struct btrfs_trans_handle *trans,
14067b8b92afSJosef Bacik 				struct btrfs_root *root, int err)
140749b25e05SJeff Mahoney {
140849b25e05SJeff Mahoney 	struct btrfs_transaction *cur_trans = trans->transaction;
140949b25e05SJeff Mahoney 
141049b25e05SJeff Mahoney 	WARN_ON(trans->use_count > 1);
141149b25e05SJeff Mahoney 
14127b8b92afSJosef Bacik 	btrfs_abort_transaction(trans, root, err);
14137b8b92afSJosef Bacik 
141449b25e05SJeff Mahoney 	spin_lock(&root->fs_info->trans_lock);
141549b25e05SJeff Mahoney 	list_del_init(&cur_trans->list);
1416d7096fc3SJosef Bacik 	if (cur_trans == root->fs_info->running_transaction) {
1417d7096fc3SJosef Bacik 		root->fs_info->running_transaction = NULL;
1418d7096fc3SJosef Bacik 		root->fs_info->trans_no_join = 0;
1419d7096fc3SJosef Bacik 	}
142049b25e05SJeff Mahoney 	spin_unlock(&root->fs_info->trans_lock);
142149b25e05SJeff Mahoney 
142249b25e05SJeff Mahoney 	btrfs_cleanup_one_transaction(trans->transaction, root);
142349b25e05SJeff Mahoney 
142449b25e05SJeff Mahoney 	put_transaction(cur_trans);
142549b25e05SJeff Mahoney 	put_transaction(cur_trans);
142649b25e05SJeff Mahoney 
142749b25e05SJeff Mahoney 	trace_btrfs_transaction_commit(root);
142849b25e05SJeff Mahoney 
142949b25e05SJeff Mahoney 	btrfs_scrub_continue(root);
143049b25e05SJeff Mahoney 
143149b25e05SJeff Mahoney 	if (current->journal_info == trans)
143249b25e05SJeff Mahoney 		current->journal_info = NULL;
143349b25e05SJeff Mahoney 
143449b25e05SJeff Mahoney 	kmem_cache_free(btrfs_trans_handle_cachep, trans);
143549b25e05SJeff Mahoney }
143649b25e05SJeff Mahoney 
1437ca469637SMiao Xie static int btrfs_flush_all_pending_stuffs(struct btrfs_trans_handle *trans,
1438ca469637SMiao Xie 					  struct btrfs_root *root)
1439ca469637SMiao Xie {
1440ca469637SMiao Xie 	int flush_on_commit = btrfs_test_opt(root, FLUSHONCOMMIT);
1441ca469637SMiao Xie 	int snap_pending = 0;
1442ca469637SMiao Xie 	int ret;
1443ca469637SMiao Xie 
1444ca469637SMiao Xie 	if (!flush_on_commit) {
1445ca469637SMiao Xie 		spin_lock(&root->fs_info->trans_lock);
1446ca469637SMiao Xie 		if (!list_empty(&trans->transaction->pending_snapshots))
1447ca469637SMiao Xie 			snap_pending = 1;
1448ca469637SMiao Xie 		spin_unlock(&root->fs_info->trans_lock);
1449ca469637SMiao Xie 	}
1450ca469637SMiao Xie 
1451ca469637SMiao Xie 	if (flush_on_commit || snap_pending) {
14523edb2a68SMiao Xie 		ret = btrfs_start_delalloc_inodes(root, 1);
14533edb2a68SMiao Xie 		if (ret)
14543edb2a68SMiao Xie 			return ret;
1455ca469637SMiao Xie 		btrfs_wait_ordered_extents(root, 1);
1456ca469637SMiao Xie 	}
1457ca469637SMiao Xie 
1458ca469637SMiao Xie 	ret = btrfs_run_delayed_items(trans, root);
1459ca469637SMiao Xie 	if (ret)
1460ca469637SMiao Xie 		return ret;
1461ca469637SMiao Xie 
1462ca469637SMiao Xie 	/*
1463ca469637SMiao Xie 	 * running the delayed items may have added new refs. account
1464ca469637SMiao Xie 	 * them now so that they hinder processing of more delayed refs
1465ca469637SMiao Xie 	 * as little as possible.
1466ca469637SMiao Xie 	 */
1467ca469637SMiao Xie 	btrfs_delayed_refs_qgroup_accounting(trans, root->fs_info);
1468ca469637SMiao Xie 
1469ca469637SMiao Xie 	/*
1470ca469637SMiao Xie 	 * rename don't use btrfs_join_transaction, so, once we
1471ca469637SMiao Xie 	 * set the transaction to blocked above, we aren't going
1472ca469637SMiao Xie 	 * to get any new ordered operations.  We can safely run
1473ca469637SMiao Xie 	 * it here and no for sure that nothing new will be added
1474ca469637SMiao Xie 	 * to the list
1475ca469637SMiao Xie 	 */
1476569e0f35SJosef Bacik 	ret = btrfs_run_ordered_operations(trans, root, 1);
1477ca469637SMiao Xie 
1478eebc6084SMiao Xie 	return ret;
1479ca469637SMiao Xie }
1480ca469637SMiao Xie 
1481bb9c12c9SSage Weil /*
1482bb9c12c9SSage Weil  * btrfs_transaction state sequence:
1483bb9c12c9SSage Weil  *    in_commit = 0, blocked = 0  (initial)
1484bb9c12c9SSage Weil  *    in_commit = 1, blocked = 1
1485bb9c12c9SSage Weil  *    blocked = 0
1486bb9c12c9SSage Weil  *    commit_done = 1
1487bb9c12c9SSage Weil  */
148879154b1bSChris Mason int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
148979154b1bSChris Mason 			     struct btrfs_root *root)
149079154b1bSChris Mason {
149115ee9bc7SJosef Bacik 	unsigned long joined = 0;
149249b25e05SJeff Mahoney 	struct btrfs_transaction *cur_trans = trans->transaction;
14938fd17795SChris Mason 	struct btrfs_transaction *prev_trans = NULL;
149479154b1bSChris Mason 	DEFINE_WAIT(wait);
149525287e0aSMiao Xie 	int ret;
149689573b9cSChris Mason 	int should_grow = 0;
149789573b9cSChris Mason 	unsigned long now = get_seconds();
149879154b1bSChris Mason 
1499569e0f35SJosef Bacik 	ret = btrfs_run_ordered_operations(trans, root, 0);
150025287e0aSMiao Xie 	if (ret) {
150125287e0aSMiao Xie 		btrfs_abort_transaction(trans, root, ret);
1502e4a2bcacSJosef Bacik 		btrfs_end_transaction(trans, root);
1503e4a2bcacSJosef Bacik 		return ret;
150425287e0aSMiao Xie 	}
150525287e0aSMiao Xie 
15068d25a086SMiao Xie 	/* Stop the commit early if ->aborted is set */
15078d25a086SMiao Xie 	if (unlikely(ACCESS_ONCE(cur_trans->aborted))) {
150825287e0aSMiao Xie 		ret = cur_trans->aborted;
1509e4a2bcacSJosef Bacik 		btrfs_end_transaction(trans, root);
1510e4a2bcacSJosef Bacik 		return ret;
151125287e0aSMiao Xie 	}
151249b25e05SJeff Mahoney 
151356bec294SChris Mason 	/* make a pass through all the delayed refs we have so far
151456bec294SChris Mason 	 * any runnings procs may add more while we are here
151556bec294SChris Mason 	 */
151656bec294SChris Mason 	ret = btrfs_run_delayed_refs(trans, root, 0);
1517e4a2bcacSJosef Bacik 	if (ret) {
1518e4a2bcacSJosef Bacik 		btrfs_end_transaction(trans, root);
1519e4a2bcacSJosef Bacik 		return ret;
1520e4a2bcacSJosef Bacik 	}
152156bec294SChris Mason 
15220e721106SJosef Bacik 	btrfs_trans_release_metadata(trans, root);
15230e721106SJosef Bacik 	trans->block_rsv = NULL;
15240e721106SJosef Bacik 
1525b7ec40d7SChris Mason 	cur_trans = trans->transaction;
152649b25e05SJeff Mahoney 
152756bec294SChris Mason 	/*
152856bec294SChris Mason 	 * set the flushing flag so procs in this transaction have to
152956bec294SChris Mason 	 * start sending their work down.
153056bec294SChris Mason 	 */
1531b7ec40d7SChris Mason 	cur_trans->delayed_refs.flushing = 1;
153256bec294SChris Mason 
1533ea658badSJosef Bacik 	if (!list_empty(&trans->new_bgs))
1534ea658badSJosef Bacik 		btrfs_create_pending_block_groups(trans, root);
1535ea658badSJosef Bacik 
1536c3e69d58SChris Mason 	ret = btrfs_run_delayed_refs(trans, root, 0);
1537e4a2bcacSJosef Bacik 	if (ret) {
1538e4a2bcacSJosef Bacik 		btrfs_end_transaction(trans, root);
1539e4a2bcacSJosef Bacik 		return ret;
1540e4a2bcacSJosef Bacik 	}
154156bec294SChris Mason 
1542a4abeea4SJosef Bacik 	spin_lock(&cur_trans->commit_lock);
1543b7ec40d7SChris Mason 	if (cur_trans->in_commit) {
1544a4abeea4SJosef Bacik 		spin_unlock(&cur_trans->commit_lock);
154513c5a93eSJosef Bacik 		atomic_inc(&cur_trans->use_count);
154649b25e05SJeff Mahoney 		ret = btrfs_end_transaction(trans, root);
1547ccd467d6SChris Mason 
1548b9c8300cSLi Zefan 		wait_for_commit(root, cur_trans);
154915ee9bc7SJosef Bacik 
155079154b1bSChris Mason 		put_transaction(cur_trans);
155115ee9bc7SJosef Bacik 
155249b25e05SJeff Mahoney 		return ret;
155379154b1bSChris Mason 	}
15544313b399SChris Mason 
15552c90e5d6SChris Mason 	trans->transaction->in_commit = 1;
1556f9295749SChris Mason 	trans->transaction->blocked = 1;
1557a4abeea4SJosef Bacik 	spin_unlock(&cur_trans->commit_lock);
1558bb9c12c9SSage Weil 	wake_up(&root->fs_info->transaction_blocked_wait);
1559bb9c12c9SSage Weil 
1560a4abeea4SJosef Bacik 	spin_lock(&root->fs_info->trans_lock);
1561ccd467d6SChris Mason 	if (cur_trans->list.prev != &root->fs_info->trans_list) {
1562ccd467d6SChris Mason 		prev_trans = list_entry(cur_trans->list.prev,
1563ccd467d6SChris Mason 					struct btrfs_transaction, list);
1564ccd467d6SChris Mason 		if (!prev_trans->commit_done) {
156513c5a93eSJosef Bacik 			atomic_inc(&prev_trans->use_count);
1566a4abeea4SJosef Bacik 			spin_unlock(&root->fs_info->trans_lock);
1567ccd467d6SChris Mason 
1568ccd467d6SChris Mason 			wait_for_commit(root, prev_trans);
1569ccd467d6SChris Mason 
157015ee9bc7SJosef Bacik 			put_transaction(prev_trans);
1571a4abeea4SJosef Bacik 		} else {
1572a4abeea4SJosef Bacik 			spin_unlock(&root->fs_info->trans_lock);
1573ccd467d6SChris Mason 		}
1574a4abeea4SJosef Bacik 	} else {
1575a4abeea4SJosef Bacik 		spin_unlock(&root->fs_info->trans_lock);
1576ccd467d6SChris Mason 	}
157715ee9bc7SJosef Bacik 
1578e39e64acSChris Mason 	if (!btrfs_test_opt(root, SSD) &&
1579e39e64acSChris Mason 	    (now < cur_trans->start_time || now - cur_trans->start_time < 1))
158089573b9cSChris Mason 		should_grow = 1;
158189573b9cSChris Mason 
158215ee9bc7SJosef Bacik 	do {
158315ee9bc7SJosef Bacik 		joined = cur_trans->num_joined;
15847ea394f1SYan Zheng 
15852c90e5d6SChris Mason 		WARN_ON(cur_trans != trans->transaction);
158615ee9bc7SJosef Bacik 
1587ca469637SMiao Xie 		ret = btrfs_flush_all_pending_stuffs(trans, root);
158849b25e05SJeff Mahoney 		if (ret)
158949b25e05SJeff Mahoney 			goto cleanup_transaction;
159016cdcec7SMiao Xie 
1591ed3b3d31SChris Mason 		prepare_to_wait(&cur_trans->writer_wait, &wait,
1592ed3b3d31SChris Mason 				TASK_UNINTERRUPTIBLE);
1593ed3b3d31SChris Mason 
159413c5a93eSJosef Bacik 		if (atomic_read(&cur_trans->num_writers) > 1)
159599d16cbcSSage Weil 			schedule_timeout(MAX_SCHEDULE_TIMEOUT);
159699d16cbcSSage Weil 		else if (should_grow)
159799d16cbcSSage Weil 			schedule_timeout(1);
159815ee9bc7SJosef Bacik 
159915ee9bc7SJosef Bacik 		finish_wait(&cur_trans->writer_wait, &wait);
1600ed0ca140SJosef Bacik 	} while (atomic_read(&cur_trans->num_writers) > 1 ||
1601ed0ca140SJosef Bacik 		 (should_grow && cur_trans->num_joined != joined));
1602ed0ca140SJosef Bacik 
1603ca469637SMiao Xie 	ret = btrfs_flush_all_pending_stuffs(trans, root);
1604ca469637SMiao Xie 	if (ret)
1605ca469637SMiao Xie 		goto cleanup_transaction;
1606ca469637SMiao Xie 
1607ed0ca140SJosef Bacik 	/*
1608ed0ca140SJosef Bacik 	 * Ok now we need to make sure to block out any other joins while we
1609ed0ca140SJosef Bacik 	 * commit the transaction.  We could have started a join before setting
1610ed0ca140SJosef Bacik 	 * no_join so make sure to wait for num_writers to == 1 again.
1611ed0ca140SJosef Bacik 	 */
1612a4abeea4SJosef Bacik 	spin_lock(&root->fs_info->trans_lock);
1613a4abeea4SJosef Bacik 	root->fs_info->trans_no_join = 1;
1614a4abeea4SJosef Bacik 	spin_unlock(&root->fs_info->trans_lock);
1615ed0ca140SJosef Bacik 	wait_event(cur_trans->writer_wait,
1616ed0ca140SJosef Bacik 		   atomic_read(&cur_trans->num_writers) == 1);
161715ee9bc7SJosef Bacik 
16182cba30f1SMiao Xie 	/* ->aborted might be set after the previous check, so check it */
16192cba30f1SMiao Xie 	if (unlikely(ACCESS_ONCE(cur_trans->aborted))) {
16202cba30f1SMiao Xie 		ret = cur_trans->aborted;
16212cba30f1SMiao Xie 		goto cleanup_transaction;
16222cba30f1SMiao Xie 	}
16237585717fSChris Mason 	/*
16247585717fSChris Mason 	 * the reloc mutex makes sure that we stop
16257585717fSChris Mason 	 * the balancing code from coming in and moving
16267585717fSChris Mason 	 * extents around in the middle of the commit
16277585717fSChris Mason 	 */
16287585717fSChris Mason 	mutex_lock(&root->fs_info->reloc_mutex);
16297585717fSChris Mason 
163042874b3dSMiao Xie 	/*
163142874b3dSMiao Xie 	 * We needn't worry about the delayed items because we will
163242874b3dSMiao Xie 	 * deal with them in create_pending_snapshot(), which is the
163342874b3dSMiao Xie 	 * core function of the snapshot creation.
163442874b3dSMiao Xie 	 */
163542874b3dSMiao Xie 	ret = create_pending_snapshots(trans, root->fs_info);
163649b25e05SJeff Mahoney 	if (ret) {
163749b25e05SJeff Mahoney 		mutex_unlock(&root->fs_info->reloc_mutex);
163849b25e05SJeff Mahoney 		goto cleanup_transaction;
163949b25e05SJeff Mahoney 	}
16403063d29fSChris Mason 
164142874b3dSMiao Xie 	/*
164242874b3dSMiao Xie 	 * We insert the dir indexes of the snapshots and update the inode
164342874b3dSMiao Xie 	 * of the snapshots' parents after the snapshot creation, so there
164442874b3dSMiao Xie 	 * are some delayed items which are not dealt with. Now deal with
164542874b3dSMiao Xie 	 * them.
164642874b3dSMiao Xie 	 *
164742874b3dSMiao Xie 	 * We needn't worry that this operation will corrupt the snapshots,
164842874b3dSMiao Xie 	 * because all the tree which are snapshoted will be forced to COW
164942874b3dSMiao Xie 	 * the nodes and leaves.
165042874b3dSMiao Xie 	 */
165142874b3dSMiao Xie 	ret = btrfs_run_delayed_items(trans, root);
165249b25e05SJeff Mahoney 	if (ret) {
165349b25e05SJeff Mahoney 		mutex_unlock(&root->fs_info->reloc_mutex);
165449b25e05SJeff Mahoney 		goto cleanup_transaction;
165549b25e05SJeff Mahoney 	}
165616cdcec7SMiao Xie 
165756bec294SChris Mason 	ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
165849b25e05SJeff Mahoney 	if (ret) {
165949b25e05SJeff Mahoney 		mutex_unlock(&root->fs_info->reloc_mutex);
166049b25e05SJeff Mahoney 		goto cleanup_transaction;
166149b25e05SJeff Mahoney 	}
166256bec294SChris Mason 
1663e999376fSChris Mason 	/*
1664e999376fSChris Mason 	 * make sure none of the code above managed to slip in a
1665e999376fSChris Mason 	 * delayed item
1666e999376fSChris Mason 	 */
1667e999376fSChris Mason 	btrfs_assert_delayed_root_empty(root);
1668e999376fSChris Mason 
16692c90e5d6SChris Mason 	WARN_ON(cur_trans != trans->transaction);
1670dc17ff8fSChris Mason 
1671a2de733cSArne Jansen 	btrfs_scrub_pause(root);
1672e02119d5SChris Mason 	/* btrfs_commit_tree_roots is responsible for getting the
1673e02119d5SChris Mason 	 * various roots consistent with each other.  Every pointer
1674e02119d5SChris Mason 	 * in the tree of tree roots has to point to the most up to date
1675e02119d5SChris Mason 	 * root for every subvolume and other tree.  So, we have to keep
1676e02119d5SChris Mason 	 * the tree logging code from jumping in and changing any
1677e02119d5SChris Mason 	 * of the trees.
1678e02119d5SChris Mason 	 *
1679e02119d5SChris Mason 	 * At this point in the commit, there can't be any tree-log
1680e02119d5SChris Mason 	 * writers, but a little lower down we drop the trans mutex
1681e02119d5SChris Mason 	 * and let new people in.  By holding the tree_log_mutex
1682e02119d5SChris Mason 	 * from now until after the super is written, we avoid races
1683e02119d5SChris Mason 	 * with the tree-log code.
1684e02119d5SChris Mason 	 */
1685e02119d5SChris Mason 	mutex_lock(&root->fs_info->tree_log_mutex);
16861a40e23bSZheng Yan 
16875d4f98a2SYan Zheng 	ret = commit_fs_roots(trans, root);
168849b25e05SJeff Mahoney 	if (ret) {
168949b25e05SJeff Mahoney 		mutex_unlock(&root->fs_info->tree_log_mutex);
1690871383beSDavid Sterba 		mutex_unlock(&root->fs_info->reloc_mutex);
169149b25e05SJeff Mahoney 		goto cleanup_transaction;
169249b25e05SJeff Mahoney 	}
169354aa1f4dSChris Mason 
16945d4f98a2SYan Zheng 	/* commit_fs_roots gets rid of all the tree log roots, it is now
1695e02119d5SChris Mason 	 * safe to free the root of tree log roots
1696e02119d5SChris Mason 	 */
1697e02119d5SChris Mason 	btrfs_free_log_root_tree(trans, root->fs_info);
1698e02119d5SChris Mason 
16995d4f98a2SYan Zheng 	ret = commit_cowonly_roots(trans, root);
170049b25e05SJeff Mahoney 	if (ret) {
170149b25e05SJeff Mahoney 		mutex_unlock(&root->fs_info->tree_log_mutex);
1702871383beSDavid Sterba 		mutex_unlock(&root->fs_info->reloc_mutex);
170349b25e05SJeff Mahoney 		goto cleanup_transaction;
170449b25e05SJeff Mahoney 	}
170554aa1f4dSChris Mason 
17062cba30f1SMiao Xie 	/*
17072cba30f1SMiao Xie 	 * The tasks which save the space cache and inode cache may also
17082cba30f1SMiao Xie 	 * update ->aborted, check it.
17092cba30f1SMiao Xie 	 */
17102cba30f1SMiao Xie 	if (unlikely(ACCESS_ONCE(cur_trans->aborted))) {
17112cba30f1SMiao Xie 		ret = cur_trans->aborted;
17122cba30f1SMiao Xie 		mutex_unlock(&root->fs_info->tree_log_mutex);
17132cba30f1SMiao Xie 		mutex_unlock(&root->fs_info->reloc_mutex);
17142cba30f1SMiao Xie 		goto cleanup_transaction;
17152cba30f1SMiao Xie 	}
17162cba30f1SMiao Xie 
171711833d66SYan Zheng 	btrfs_prepare_extent_commit(trans, root);
171811833d66SYan Zheng 
171978fae27eSChris Mason 	cur_trans = root->fs_info->running_transaction;
17205f39d397SChris Mason 
17215d4f98a2SYan Zheng 	btrfs_set_root_node(&root->fs_info->tree_root->root_item,
17225d4f98a2SYan Zheng 			    root->fs_info->tree_root->node);
1723817d52f8SJosef Bacik 	switch_commit_root(root->fs_info->tree_root);
17245d4f98a2SYan Zheng 
17255d4f98a2SYan Zheng 	btrfs_set_root_node(&root->fs_info->chunk_root->root_item,
17265d4f98a2SYan Zheng 			    root->fs_info->chunk_root->node);
1727817d52f8SJosef Bacik 	switch_commit_root(root->fs_info->chunk_root);
17285d4f98a2SYan Zheng 
1729edf39272SJan Schmidt 	assert_qgroups_uptodate(trans);
17305d4f98a2SYan Zheng 	update_super_roots(root);
1731e02119d5SChris Mason 
1732e02119d5SChris Mason 	if (!root->fs_info->log_root_recovering) {
17336c41761fSDavid Sterba 		btrfs_set_super_log_root(root->fs_info->super_copy, 0);
17346c41761fSDavid Sterba 		btrfs_set_super_log_root_level(root->fs_info->super_copy, 0);
1735e02119d5SChris Mason 	}
1736e02119d5SChris Mason 
17376c41761fSDavid Sterba 	memcpy(root->fs_info->super_for_commit, root->fs_info->super_copy,
17386c41761fSDavid Sterba 	       sizeof(*root->fs_info->super_copy));
1739ccd467d6SChris Mason 
1740f9295749SChris Mason 	trans->transaction->blocked = 0;
1741a4abeea4SJosef Bacik 	spin_lock(&root->fs_info->trans_lock);
1742a4abeea4SJosef Bacik 	root->fs_info->running_transaction = NULL;
1743a4abeea4SJosef Bacik 	root->fs_info->trans_no_join = 0;
1744a4abeea4SJosef Bacik 	spin_unlock(&root->fs_info->trans_lock);
17457585717fSChris Mason 	mutex_unlock(&root->fs_info->reloc_mutex);
1746b7ec40d7SChris Mason 
1747f9295749SChris Mason 	wake_up(&root->fs_info->transaction_wait);
1748e6dcd2dcSChris Mason 
174979154b1bSChris Mason 	ret = btrfs_write_and_wait_transaction(trans, root);
175049b25e05SJeff Mahoney 	if (ret) {
175149b25e05SJeff Mahoney 		btrfs_error(root->fs_info, ret,
175249b25e05SJeff Mahoney 			    "Error while writing out transaction.");
175349b25e05SJeff Mahoney 		mutex_unlock(&root->fs_info->tree_log_mutex);
175449b25e05SJeff Mahoney 		goto cleanup_transaction;
175549b25e05SJeff Mahoney 	}
175649b25e05SJeff Mahoney 
175749b25e05SJeff Mahoney 	ret = write_ctree_super(trans, root, 0);
175849b25e05SJeff Mahoney 	if (ret) {
175949b25e05SJeff Mahoney 		mutex_unlock(&root->fs_info->tree_log_mutex);
176049b25e05SJeff Mahoney 		goto cleanup_transaction;
176149b25e05SJeff Mahoney 	}
17624313b399SChris Mason 
1763e02119d5SChris Mason 	/*
1764e02119d5SChris Mason 	 * the super is written, we can safely allow the tree-loggers
1765e02119d5SChris Mason 	 * to go about their business
1766e02119d5SChris Mason 	 */
1767e02119d5SChris Mason 	mutex_unlock(&root->fs_info->tree_log_mutex);
1768e02119d5SChris Mason 
176911833d66SYan Zheng 	btrfs_finish_extent_commit(trans, root);
17704313b399SChris Mason 
17712c90e5d6SChris Mason 	cur_trans->commit_done = 1;
1772b7ec40d7SChris Mason 
177315ee9bc7SJosef Bacik 	root->fs_info->last_trans_committed = cur_trans->transid;
1774817d52f8SJosef Bacik 
17752c90e5d6SChris Mason 	wake_up(&cur_trans->commit_wait);
17763de4586cSChris Mason 
1777a4abeea4SJosef Bacik 	spin_lock(&root->fs_info->trans_lock);
177813c5a93eSJosef Bacik 	list_del_init(&cur_trans->list);
1779a4abeea4SJosef Bacik 	spin_unlock(&root->fs_info->trans_lock);
1780a4abeea4SJosef Bacik 
178179154b1bSChris Mason 	put_transaction(cur_trans);
178278fae27eSChris Mason 	put_transaction(cur_trans);
178358176a96SJosef Bacik 
1784354aa0fbSMiao Xie 	if (trans->type < TRANS_JOIN_NOLOCK)
1785b2b5ef5cSJan Kara 		sb_end_intwrite(root->fs_info->sb);
1786b2b5ef5cSJan Kara 
17871abe9b8aSliubo 	trace_btrfs_transaction_commit(root);
17881abe9b8aSliubo 
1789a2de733cSArne Jansen 	btrfs_scrub_continue(root);
1790a2de733cSArne Jansen 
17919ed74f2dSJosef Bacik 	if (current->journal_info == trans)
17929ed74f2dSJosef Bacik 		current->journal_info = NULL;
17939ed74f2dSJosef Bacik 
17942c90e5d6SChris Mason 	kmem_cache_free(btrfs_trans_handle_cachep, trans);
179524bbcf04SYan, Zheng 
179624bbcf04SYan, Zheng 	if (current != root->fs_info->transaction_kthread)
179724bbcf04SYan, Zheng 		btrfs_run_delayed_iputs(root);
179824bbcf04SYan, Zheng 
179979154b1bSChris Mason 	return ret;
180049b25e05SJeff Mahoney 
180149b25e05SJeff Mahoney cleanup_transaction:
18020e721106SJosef Bacik 	btrfs_trans_release_metadata(trans, root);
18030e721106SJosef Bacik 	trans->block_rsv = NULL;
180449b25e05SJeff Mahoney 	btrfs_printk(root->fs_info, "Skipping commit of aborted transaction.\n");
180549b25e05SJeff Mahoney //	WARN_ON(1);
180649b25e05SJeff Mahoney 	if (current->journal_info == trans)
180749b25e05SJeff Mahoney 		current->journal_info = NULL;
18087b8b92afSJosef Bacik 	cleanup_transaction(trans, root, ret);
180949b25e05SJeff Mahoney 
181049b25e05SJeff Mahoney 	return ret;
181179154b1bSChris Mason }
181279154b1bSChris Mason 
1813d352ac68SChris Mason /*
1814d352ac68SChris Mason  * interface function to delete all the snapshots we have scheduled for deletion
1815d352ac68SChris Mason  */
1816e9d0b13bSChris Mason int btrfs_clean_old_snapshots(struct btrfs_root *root)
1817e9d0b13bSChris Mason {
18185d4f98a2SYan Zheng 	LIST_HEAD(list);
18195d4f98a2SYan Zheng 	struct btrfs_fs_info *fs_info = root->fs_info;
1820e9d0b13bSChris Mason 
1821a4abeea4SJosef Bacik 	spin_lock(&fs_info->trans_lock);
18225d4f98a2SYan Zheng 	list_splice_init(&fs_info->dead_roots, &list);
1823a4abeea4SJosef Bacik 	spin_unlock(&fs_info->trans_lock);
18245d4f98a2SYan Zheng 
18255d4f98a2SYan Zheng 	while (!list_empty(&list)) {
18262c536799SJeff Mahoney 		int ret;
18272c536799SJeff Mahoney 
18285d4f98a2SYan Zheng 		root = list_entry(list.next, struct btrfs_root, root_list);
182976dda93cSYan, Zheng 		list_del(&root->root_list);
183076dda93cSYan, Zheng 
183116cdcec7SMiao Xie 		btrfs_kill_all_delayed_nodes(root);
183216cdcec7SMiao Xie 
183376dda93cSYan, Zheng 		if (btrfs_header_backref_rev(root->node) <
183476dda93cSYan, Zheng 		    BTRFS_MIXED_BACKREF_REV)
18352c536799SJeff Mahoney 			ret = btrfs_drop_snapshot(root, NULL, 0, 0);
183676dda93cSYan, Zheng 		else
18372c536799SJeff Mahoney 			ret =btrfs_drop_snapshot(root, NULL, 1, 0);
18382c536799SJeff Mahoney 		BUG_ON(ret < 0);
1839e9d0b13bSChris Mason 	}
1840e9d0b13bSChris Mason 	return 0;
1841e9d0b13bSChris Mason }
1842