xref: /openbmc/linux/fs/btrfs/transaction.c (revision 53b381b3)
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 		memset(transaction, 0, sizeof(*transaction));
442c90e5d6SChris Mason 		kmem_cache_free(btrfs_transaction_cachep, transaction);
4579154b1bSChris Mason 	}
4678fae27eSChris Mason }
4779154b1bSChris Mason 
48817d52f8SJosef Bacik static noinline void switch_commit_root(struct btrfs_root *root)
49817d52f8SJosef Bacik {
50817d52f8SJosef Bacik 	free_extent_buffer(root->commit_root);
51817d52f8SJosef Bacik 	root->commit_root = btrfs_root_node(root);
52817d52f8SJosef Bacik }
53817d52f8SJosef Bacik 
54d352ac68SChris Mason /*
55d352ac68SChris Mason  * either allocate a new transaction or hop into the existing one
56d352ac68SChris Mason  */
57354aa0fbSMiao Xie static noinline int join_transaction(struct btrfs_root *root, int type)
5879154b1bSChris Mason {
5979154b1bSChris Mason 	struct btrfs_transaction *cur_trans;
6019ae4e81SJan Schmidt 	struct btrfs_fs_info *fs_info = root->fs_info;
61a4abeea4SJosef Bacik 
6219ae4e81SJan Schmidt 	spin_lock(&fs_info->trans_lock);
63d43317dcSChris Mason loop:
6449b25e05SJeff Mahoney 	/* The file system has been taken offline. No new transactions. */
6519ae4e81SJan Schmidt 	if (fs_info->fs_state & BTRFS_SUPER_FLAG_ERROR) {
6619ae4e81SJan Schmidt 		spin_unlock(&fs_info->trans_lock);
6749b25e05SJeff Mahoney 		return -EROFS;
6849b25e05SJeff Mahoney 	}
6949b25e05SJeff Mahoney 
7019ae4e81SJan Schmidt 	if (fs_info->trans_no_join) {
71354aa0fbSMiao Xie 		/*
72354aa0fbSMiao Xie 		 * If we are JOIN_NOLOCK we're already committing a current
73354aa0fbSMiao Xie 		 * transaction, we just need a handle to deal with something
74354aa0fbSMiao Xie 		 * when committing the transaction, such as inode cache and
75354aa0fbSMiao Xie 		 * space cache. It is a special case.
76354aa0fbSMiao Xie 		 */
77354aa0fbSMiao Xie 		if (type != TRANS_JOIN_NOLOCK) {
7819ae4e81SJan Schmidt 			spin_unlock(&fs_info->trans_lock);
79a4abeea4SJosef Bacik 			return -EBUSY;
80a4abeea4SJosef Bacik 		}
81a4abeea4SJosef Bacik 	}
82a4abeea4SJosef Bacik 
8319ae4e81SJan Schmidt 	cur_trans = fs_info->running_transaction;
84a4abeea4SJosef Bacik 	if (cur_trans) {
85871383beSDavid Sterba 		if (cur_trans->aborted) {
8619ae4e81SJan Schmidt 			spin_unlock(&fs_info->trans_lock);
8749b25e05SJeff Mahoney 			return cur_trans->aborted;
88871383beSDavid Sterba 		}
89a4abeea4SJosef Bacik 		atomic_inc(&cur_trans->use_count);
90a4abeea4SJosef Bacik 		atomic_inc(&cur_trans->num_writers);
91a4abeea4SJosef Bacik 		cur_trans->num_joined++;
9219ae4e81SJan Schmidt 		spin_unlock(&fs_info->trans_lock);
93a4abeea4SJosef Bacik 		return 0;
94a4abeea4SJosef Bacik 	}
9519ae4e81SJan Schmidt 	spin_unlock(&fs_info->trans_lock);
96a4abeea4SJosef Bacik 
97354aa0fbSMiao Xie 	/*
98354aa0fbSMiao Xie 	 * If we are ATTACH, we just want to catch the current transaction,
99354aa0fbSMiao Xie 	 * and commit it. If there is no transaction, just return ENOENT.
100354aa0fbSMiao Xie 	 */
101354aa0fbSMiao Xie 	if (type == TRANS_ATTACH)
102354aa0fbSMiao Xie 		return -ENOENT;
103354aa0fbSMiao Xie 
104a4abeea4SJosef Bacik 	cur_trans = kmem_cache_alloc(btrfs_transaction_cachep, GFP_NOFS);
105db5b493aSTsutomu Itoh 	if (!cur_trans)
106db5b493aSTsutomu Itoh 		return -ENOMEM;
107d43317dcSChris Mason 
10819ae4e81SJan Schmidt 	spin_lock(&fs_info->trans_lock);
10919ae4e81SJan Schmidt 	if (fs_info->running_transaction) {
110d43317dcSChris Mason 		/*
111d43317dcSChris Mason 		 * someone started a transaction after we unlocked.  Make sure
112d43317dcSChris Mason 		 * to redo the trans_no_join checks above
113d43317dcSChris Mason 		 */
114a4abeea4SJosef Bacik 		kmem_cache_free(btrfs_transaction_cachep, cur_trans);
11519ae4e81SJan Schmidt 		cur_trans = fs_info->running_transaction;
116d43317dcSChris Mason 		goto loop;
117e4b50e14SDan Carpenter 	} else if (fs_info->fs_state & BTRFS_SUPER_FLAG_ERROR) {
118e4b50e14SDan Carpenter 		spin_unlock(&fs_info->trans_lock);
1197b8b92afSJosef Bacik 		kmem_cache_free(btrfs_transaction_cachep, cur_trans);
1207b8b92afSJosef Bacik 		return -EROFS;
121a4abeea4SJosef Bacik 	}
122d43317dcSChris Mason 
12313c5a93eSJosef Bacik 	atomic_set(&cur_trans->num_writers, 1);
12415ee9bc7SJosef Bacik 	cur_trans->num_joined = 0;
12579154b1bSChris Mason 	init_waitqueue_head(&cur_trans->writer_wait);
12679154b1bSChris Mason 	init_waitqueue_head(&cur_trans->commit_wait);
12779154b1bSChris Mason 	cur_trans->in_commit = 0;
128f9295749SChris Mason 	cur_trans->blocked = 0;
129a4abeea4SJosef Bacik 	/*
130a4abeea4SJosef Bacik 	 * One for this trans handle, one so it will live on until we
131a4abeea4SJosef Bacik 	 * commit the transaction.
132a4abeea4SJosef Bacik 	 */
133a4abeea4SJosef Bacik 	atomic_set(&cur_trans->use_count, 2);
13479154b1bSChris Mason 	cur_trans->commit_done = 0;
13508607c1bSChris Mason 	cur_trans->start_time = get_seconds();
13656bec294SChris Mason 
1376bef4d31SEric Paris 	cur_trans->delayed_refs.root = RB_ROOT;
13856bec294SChris Mason 	cur_trans->delayed_refs.num_entries = 0;
139c3e69d58SChris Mason 	cur_trans->delayed_refs.num_heads_ready = 0;
140c3e69d58SChris Mason 	cur_trans->delayed_refs.num_heads = 0;
14156bec294SChris Mason 	cur_trans->delayed_refs.flushing = 0;
142c3e69d58SChris Mason 	cur_trans->delayed_refs.run_delayed_start = 0;
14320b297d6SJan Schmidt 
14420b297d6SJan Schmidt 	/*
14520b297d6SJan Schmidt 	 * although the tree mod log is per file system and not per transaction,
14620b297d6SJan Schmidt 	 * the log must never go across transaction boundaries.
14720b297d6SJan Schmidt 	 */
14820b297d6SJan Schmidt 	smp_mb();
14931b1a2bdSJulia Lawall 	if (!list_empty(&fs_info->tree_mod_seq_list))
15031b1a2bdSJulia Lawall 		WARN(1, KERN_ERR "btrfs: tree_mod_seq_list not empty when "
15120b297d6SJan Schmidt 			"creating a fresh transaction\n");
15231b1a2bdSJulia Lawall 	if (!RB_EMPTY_ROOT(&fs_info->tree_mod_log))
15331b1a2bdSJulia Lawall 		WARN(1, KERN_ERR "btrfs: tree_mod_log rb tree not empty when "
15420b297d6SJan Schmidt 			"creating a fresh transaction\n");
15520b297d6SJan Schmidt 	atomic_set(&fs_info->tree_mod_seq, 0);
15620b297d6SJan Schmidt 
157a4abeea4SJosef Bacik 	spin_lock_init(&cur_trans->commit_lock);
15856bec294SChris Mason 	spin_lock_init(&cur_trans->delayed_refs.lock);
15956bec294SChris Mason 
1603063d29fSChris Mason 	INIT_LIST_HEAD(&cur_trans->pending_snapshots);
16119ae4e81SJan Schmidt 	list_add_tail(&cur_trans->list, &fs_info->trans_list);
162d1310b2eSChris Mason 	extent_io_tree_init(&cur_trans->dirty_pages,
16319ae4e81SJan Schmidt 			     fs_info->btree_inode->i_mapping);
16419ae4e81SJan Schmidt 	fs_info->generation++;
16519ae4e81SJan Schmidt 	cur_trans->transid = fs_info->generation;
16619ae4e81SJan Schmidt 	fs_info->running_transaction = cur_trans;
16749b25e05SJeff Mahoney 	cur_trans->aborted = 0;
16819ae4e81SJan Schmidt 	spin_unlock(&fs_info->trans_lock);
16915ee9bc7SJosef Bacik 
17079154b1bSChris Mason 	return 0;
17179154b1bSChris Mason }
17279154b1bSChris Mason 
173d352ac68SChris Mason /*
174d397712bSChris Mason  * this does all the record keeping required to make sure that a reference
175d397712bSChris Mason  * counted root is properly recorded in a given transaction.  This is required
176d397712bSChris Mason  * to make sure the old root from before we joined the transaction is deleted
177d397712bSChris Mason  * when the transaction commits
178d352ac68SChris Mason  */
1797585717fSChris Mason static int record_root_in_trans(struct btrfs_trans_handle *trans,
1805d4f98a2SYan Zheng 			       struct btrfs_root *root)
1816702ed49SChris Mason {
1825d4f98a2SYan Zheng 	if (root->ref_cows && root->last_trans < trans->transid) {
1836702ed49SChris Mason 		WARN_ON(root == root->fs_info->extent_root);
1845d4f98a2SYan Zheng 		WARN_ON(root->commit_root != root->node);
1855d4f98a2SYan Zheng 
1867585717fSChris Mason 		/*
1877585717fSChris Mason 		 * see below for in_trans_setup usage rules
1887585717fSChris Mason 		 * we have the reloc mutex held now, so there
1897585717fSChris Mason 		 * is only one writer in this function
1907585717fSChris Mason 		 */
1917585717fSChris Mason 		root->in_trans_setup = 1;
1927585717fSChris Mason 
1937585717fSChris Mason 		/* make sure readers find in_trans_setup before
1947585717fSChris Mason 		 * they find our root->last_trans update
1957585717fSChris Mason 		 */
1967585717fSChris Mason 		smp_wmb();
1977585717fSChris Mason 
198a4abeea4SJosef Bacik 		spin_lock(&root->fs_info->fs_roots_radix_lock);
199a4abeea4SJosef Bacik 		if (root->last_trans == trans->transid) {
200a4abeea4SJosef Bacik 			spin_unlock(&root->fs_info->fs_roots_radix_lock);
201a4abeea4SJosef Bacik 			return 0;
202a4abeea4SJosef Bacik 		}
2036702ed49SChris Mason 		radix_tree_tag_set(&root->fs_info->fs_roots_radix,
2046702ed49SChris Mason 			   (unsigned long)root->root_key.objectid,
2056702ed49SChris Mason 			   BTRFS_ROOT_TRANS_TAG);
206a4abeea4SJosef Bacik 		spin_unlock(&root->fs_info->fs_roots_radix_lock);
2077585717fSChris Mason 		root->last_trans = trans->transid;
2087585717fSChris Mason 
2097585717fSChris Mason 		/* this is pretty tricky.  We don't want to
2107585717fSChris Mason 		 * take the relocation lock in btrfs_record_root_in_trans
2117585717fSChris Mason 		 * unless we're really doing the first setup for this root in
2127585717fSChris Mason 		 * this transaction.
2137585717fSChris Mason 		 *
2147585717fSChris Mason 		 * Normally we'd use root->last_trans as a flag to decide
2157585717fSChris Mason 		 * if we want to take the expensive mutex.
2167585717fSChris Mason 		 *
2177585717fSChris Mason 		 * But, we have to set root->last_trans before we
2187585717fSChris Mason 		 * init the relocation root, otherwise, we trip over warnings
2197585717fSChris Mason 		 * in ctree.c.  The solution used here is to flag ourselves
2207585717fSChris Mason 		 * with root->in_trans_setup.  When this is 1, we're still
2217585717fSChris Mason 		 * fixing up the reloc trees and everyone must wait.
2227585717fSChris Mason 		 *
2237585717fSChris Mason 		 * When this is zero, they can trust root->last_trans and fly
2247585717fSChris Mason 		 * through btrfs_record_root_in_trans without having to take the
2257585717fSChris Mason 		 * lock.  smp_wmb() makes sure that all the writes above are
2267585717fSChris Mason 		 * done before we pop in the zero below
2277585717fSChris Mason 		 */
2285d4f98a2SYan Zheng 		btrfs_init_reloc_root(trans, root);
2297585717fSChris Mason 		smp_wmb();
2307585717fSChris Mason 		root->in_trans_setup = 0;
2316702ed49SChris Mason 	}
2325d4f98a2SYan Zheng 	return 0;
2336702ed49SChris Mason }
2345d4f98a2SYan Zheng 
2357585717fSChris Mason 
2367585717fSChris Mason int btrfs_record_root_in_trans(struct btrfs_trans_handle *trans,
2377585717fSChris Mason 			       struct btrfs_root *root)
2387585717fSChris Mason {
2397585717fSChris Mason 	if (!root->ref_cows)
2407585717fSChris Mason 		return 0;
2417585717fSChris Mason 
2427585717fSChris Mason 	/*
2437585717fSChris Mason 	 * see record_root_in_trans for comments about in_trans_setup usage
2447585717fSChris Mason 	 * and barriers
2457585717fSChris Mason 	 */
2467585717fSChris Mason 	smp_rmb();
2477585717fSChris Mason 	if (root->last_trans == trans->transid &&
2487585717fSChris Mason 	    !root->in_trans_setup)
2497585717fSChris Mason 		return 0;
2507585717fSChris Mason 
2517585717fSChris Mason 	mutex_lock(&root->fs_info->reloc_mutex);
2527585717fSChris Mason 	record_root_in_trans(trans, root);
2537585717fSChris Mason 	mutex_unlock(&root->fs_info->reloc_mutex);
2547585717fSChris Mason 
2557585717fSChris Mason 	return 0;
2567585717fSChris Mason }
2577585717fSChris Mason 
258d352ac68SChris Mason /* wait for commit against the current transaction to become unblocked
259d352ac68SChris Mason  * when this is done, it is safe to start a new transaction, but the current
260d352ac68SChris Mason  * transaction might not be fully on disk.
261d352ac68SChris Mason  */
26237d1aeeeSChris Mason static void wait_current_trans(struct btrfs_root *root)
26379154b1bSChris Mason {
264f9295749SChris Mason 	struct btrfs_transaction *cur_trans;
26579154b1bSChris Mason 
266a4abeea4SJosef Bacik 	spin_lock(&root->fs_info->trans_lock);
267f9295749SChris Mason 	cur_trans = root->fs_info->running_transaction;
26837d1aeeeSChris Mason 	if (cur_trans && cur_trans->blocked) {
26913c5a93eSJosef Bacik 		atomic_inc(&cur_trans->use_count);
270a4abeea4SJosef Bacik 		spin_unlock(&root->fs_info->trans_lock);
27172d63ed6SLi Zefan 
27272d63ed6SLi Zefan 		wait_event(root->fs_info->transaction_wait,
27372d63ed6SLi Zefan 			   !cur_trans->blocked);
274f9295749SChris Mason 		put_transaction(cur_trans);
275a4abeea4SJosef Bacik 	} else {
276a4abeea4SJosef Bacik 		spin_unlock(&root->fs_info->trans_lock);
277f9295749SChris Mason 	}
27837d1aeeeSChris Mason }
27937d1aeeeSChris Mason 
280a22285a6SYan, Zheng static int may_wait_transaction(struct btrfs_root *root, int type)
28137d1aeeeSChris Mason {
282a4abeea4SJosef Bacik 	if (root->fs_info->log_root_recovering)
283a4abeea4SJosef Bacik 		return 0;
284a4abeea4SJosef Bacik 
285a4abeea4SJosef Bacik 	if (type == TRANS_USERSPACE)
286a22285a6SYan, Zheng 		return 1;
287a4abeea4SJosef Bacik 
288a4abeea4SJosef Bacik 	if (type == TRANS_START &&
289a4abeea4SJosef Bacik 	    !atomic_read(&root->fs_info->open_ioctl_trans))
290a4abeea4SJosef Bacik 		return 1;
291a4abeea4SJosef Bacik 
292a22285a6SYan, Zheng 	return 0;
293a22285a6SYan, Zheng }
294a22285a6SYan, Zheng 
29508e007d2SMiao Xie static struct btrfs_trans_handle *
29608e007d2SMiao Xie start_transaction(struct btrfs_root *root, u64 num_items, int type,
29708e007d2SMiao Xie 		  enum btrfs_reserve_flush_enum flush)
298a22285a6SYan, Zheng {
299a22285a6SYan, Zheng 	struct btrfs_trans_handle *h;
300a22285a6SYan, Zheng 	struct btrfs_transaction *cur_trans;
301b5009945SJosef Bacik 	u64 num_bytes = 0;
302a22285a6SYan, Zheng 	int ret;
303c5567237SArne Jansen 	u64 qgroup_reserved = 0;
304acce952bSliubo 
305acce952bSliubo 	if (root->fs_info->fs_state & BTRFS_SUPER_FLAG_ERROR)
306acce952bSliubo 		return ERR_PTR(-EROFS);
3072a1eb461SJosef Bacik 
3082a1eb461SJosef Bacik 	if (current->journal_info) {
3092a1eb461SJosef Bacik 		WARN_ON(type != TRANS_JOIN && type != TRANS_JOIN_NOLOCK);
3102a1eb461SJosef Bacik 		h = current->journal_info;
3112a1eb461SJosef Bacik 		h->use_count++;
312b7d5b0a8SMiao Xie 		WARN_ON(h->use_count > 2);
3132a1eb461SJosef Bacik 		h->orig_rsv = h->block_rsv;
3142a1eb461SJosef Bacik 		h->block_rsv = NULL;
3152a1eb461SJosef Bacik 		goto got_it;
3162a1eb461SJosef Bacik 	}
317b5009945SJosef Bacik 
318b5009945SJosef Bacik 	/*
319b5009945SJosef Bacik 	 * Do the reservation before we join the transaction so we can do all
320b5009945SJosef Bacik 	 * the appropriate flushing if need be.
321b5009945SJosef Bacik 	 */
322b5009945SJosef Bacik 	if (num_items > 0 && root != root->fs_info->chunk_root) {
323c5567237SArne Jansen 		if (root->fs_info->quota_enabled &&
324c5567237SArne Jansen 		    is_fstree(root->root_key.objectid)) {
325c5567237SArne Jansen 			qgroup_reserved = num_items * root->leafsize;
326c5567237SArne Jansen 			ret = btrfs_qgroup_reserve(root, qgroup_reserved);
327c5567237SArne Jansen 			if (ret)
328c5567237SArne Jansen 				return ERR_PTR(ret);
329c5567237SArne Jansen 		}
330c5567237SArne Jansen 
331b5009945SJosef Bacik 		num_bytes = btrfs_calc_trans_metadata_size(root, num_items);
3324a92b1b8SJosef Bacik 		ret = btrfs_block_rsv_add(root,
333b5009945SJosef Bacik 					  &root->fs_info->trans_block_rsv,
33408e007d2SMiao Xie 					  num_bytes, flush);
335b5009945SJosef Bacik 		if (ret)
336b5009945SJosef Bacik 			return ERR_PTR(ret);
337b5009945SJosef Bacik 	}
338a22285a6SYan, Zheng again:
339a22285a6SYan, Zheng 	h = kmem_cache_alloc(btrfs_trans_handle_cachep, GFP_NOFS);
340a22285a6SYan, Zheng 	if (!h)
341a22285a6SYan, Zheng 		return ERR_PTR(-ENOMEM);
342a22285a6SYan, Zheng 
34398114659SJosef Bacik 	/*
34498114659SJosef Bacik 	 * If we are JOIN_NOLOCK we're already committing a transaction and
34598114659SJosef Bacik 	 * waiting on this guy, so we don't need to do the sb_start_intwrite
34698114659SJosef Bacik 	 * because we're already holding a ref.  We need this because we could
34798114659SJosef Bacik 	 * have raced in and did an fsync() on a file which can kick a commit
34898114659SJosef Bacik 	 * and then we deadlock with somebody doing a freeze.
349354aa0fbSMiao Xie 	 *
350354aa0fbSMiao Xie 	 * If we are ATTACH, it means we just want to catch the current
351354aa0fbSMiao Xie 	 * transaction and commit it, so we needn't do sb_start_intwrite().
35298114659SJosef Bacik 	 */
353354aa0fbSMiao Xie 	if (type < TRANS_JOIN_NOLOCK)
354b2b5ef5cSJan Kara 		sb_start_intwrite(root->fs_info->sb);
355b2b5ef5cSJan Kara 
356a22285a6SYan, Zheng 	if (may_wait_transaction(root, type))
35737d1aeeeSChris Mason 		wait_current_trans(root);
358a22285a6SYan, Zheng 
359a4abeea4SJosef Bacik 	do {
360354aa0fbSMiao Xie 		ret = join_transaction(root, type);
361a4abeea4SJosef Bacik 		if (ret == -EBUSY)
362a4abeea4SJosef Bacik 			wait_current_trans(root);
363a4abeea4SJosef Bacik 	} while (ret == -EBUSY);
364a4abeea4SJosef Bacik 
365db5b493aSTsutomu Itoh 	if (ret < 0) {
366354aa0fbSMiao Xie 		/* We must get the transaction if we are JOIN_NOLOCK. */
367354aa0fbSMiao Xie 		BUG_ON(type == TRANS_JOIN_NOLOCK);
368354aa0fbSMiao Xie 
369354aa0fbSMiao Xie 		if (type < TRANS_JOIN_NOLOCK)
370b2b5ef5cSJan Kara 			sb_end_intwrite(root->fs_info->sb);
3716e8df2aeSYoshinori Sano 		kmem_cache_free(btrfs_trans_handle_cachep, h);
372db5b493aSTsutomu Itoh 		return ERR_PTR(ret);
373db5b493aSTsutomu Itoh 	}
3740f7d52f4SChris Mason 
375a22285a6SYan, Zheng 	cur_trans = root->fs_info->running_transaction;
376a22285a6SYan, Zheng 
377a22285a6SYan, Zheng 	h->transid = cur_trans->transid;
378a22285a6SYan, Zheng 	h->transaction = cur_trans;
37979154b1bSChris Mason 	h->blocks_used = 0;
380a22285a6SYan, Zheng 	h->bytes_reserved = 0;
381d13603efSArne Jansen 	h->root = root;
38256bec294SChris Mason 	h->delayed_ref_updates = 0;
3832a1eb461SJosef Bacik 	h->use_count = 1;
3840e721106SJosef Bacik 	h->adding_csums = 0;
385f0486c68SYan, Zheng 	h->block_rsv = NULL;
3862a1eb461SJosef Bacik 	h->orig_rsv = NULL;
38749b25e05SJeff Mahoney 	h->aborted = 0;
388c5567237SArne Jansen 	h->qgroup_reserved = qgroup_reserved;
389bed92eaeSArne Jansen 	h->delayed_ref_elem.seq = 0;
390a698d075SMiao Xie 	h->type = type;
391bed92eaeSArne Jansen 	INIT_LIST_HEAD(&h->qgroup_ref_list);
392ea658badSJosef Bacik 	INIT_LIST_HEAD(&h->new_bgs);
393b7ec40d7SChris Mason 
394a22285a6SYan, Zheng 	smp_mb();
395a22285a6SYan, Zheng 	if (cur_trans->blocked && may_wait_transaction(root, type)) {
396a22285a6SYan, Zheng 		btrfs_commit_transaction(h, root);
397a22285a6SYan, Zheng 		goto again;
398a22285a6SYan, Zheng 	}
3999ed74f2dSJosef Bacik 
400b5009945SJosef Bacik 	if (num_bytes) {
4018c2a3ca2SJosef Bacik 		trace_btrfs_space_reservation(root->fs_info, "transaction",
4022bcc0328SLiu Bo 					      h->transid, num_bytes, 1);
403b5009945SJosef Bacik 		h->block_rsv = &root->fs_info->trans_block_rsv;
404b5009945SJosef Bacik 		h->bytes_reserved = num_bytes;
405a22285a6SYan, Zheng 	}
406a22285a6SYan, Zheng 
4072a1eb461SJosef Bacik got_it:
408a4abeea4SJosef Bacik 	btrfs_record_root_in_trans(h, root);
409a22285a6SYan, Zheng 
410a22285a6SYan, Zheng 	if (!current->journal_info && type != TRANS_USERSPACE)
411a22285a6SYan, Zheng 		current->journal_info = h;
41279154b1bSChris Mason 	return h;
41379154b1bSChris Mason }
41479154b1bSChris Mason 
415f9295749SChris Mason struct btrfs_trans_handle *btrfs_start_transaction(struct btrfs_root *root,
416a22285a6SYan, Zheng 						   int num_items)
417f9295749SChris Mason {
41808e007d2SMiao Xie 	return start_transaction(root, num_items, TRANS_START,
41908e007d2SMiao Xie 				 BTRFS_RESERVE_FLUSH_ALL);
420f9295749SChris Mason }
4218407aa46SMiao Xie 
42208e007d2SMiao Xie struct btrfs_trans_handle *btrfs_start_transaction_lflush(
4238407aa46SMiao Xie 					struct btrfs_root *root, int num_items)
4248407aa46SMiao Xie {
42508e007d2SMiao Xie 	return start_transaction(root, num_items, TRANS_START,
42608e007d2SMiao Xie 				 BTRFS_RESERVE_FLUSH_LIMIT);
4278407aa46SMiao Xie }
4288407aa46SMiao Xie 
4297a7eaa40SJosef Bacik struct btrfs_trans_handle *btrfs_join_transaction(struct btrfs_root *root)
430f9295749SChris Mason {
4318407aa46SMiao Xie 	return start_transaction(root, 0, TRANS_JOIN, 0);
432f9295749SChris Mason }
433f9295749SChris Mason 
4347a7eaa40SJosef Bacik struct btrfs_trans_handle *btrfs_join_transaction_nolock(struct btrfs_root *root)
4350af3d00bSJosef Bacik {
4368407aa46SMiao Xie 	return start_transaction(root, 0, TRANS_JOIN_NOLOCK, 0);
4370af3d00bSJosef Bacik }
4380af3d00bSJosef Bacik 
4397a7eaa40SJosef Bacik struct btrfs_trans_handle *btrfs_start_ioctl_transaction(struct btrfs_root *root)
4409ca9ee09SSage Weil {
4418407aa46SMiao Xie 	return start_transaction(root, 0, TRANS_USERSPACE, 0);
4429ca9ee09SSage Weil }
4439ca9ee09SSage Weil 
444354aa0fbSMiao Xie struct btrfs_trans_handle *btrfs_attach_transaction(struct btrfs_root *root)
44560376ce4SJosef Bacik {
446354aa0fbSMiao Xie 	return start_transaction(root, 0, TRANS_ATTACH, 0);
44760376ce4SJosef Bacik }
44860376ce4SJosef Bacik 
449d352ac68SChris Mason /* wait for a transaction commit to be fully complete */
450b9c8300cSLi Zefan static noinline void wait_for_commit(struct btrfs_root *root,
45189ce8a63SChris Mason 				    struct btrfs_transaction *commit)
45289ce8a63SChris Mason {
45372d63ed6SLi Zefan 	wait_event(commit->commit_wait, commit->commit_done);
45489ce8a63SChris Mason }
45589ce8a63SChris Mason 
45646204592SSage Weil int btrfs_wait_for_commit(struct btrfs_root *root, u64 transid)
45746204592SSage Weil {
45846204592SSage Weil 	struct btrfs_transaction *cur_trans = NULL, *t;
4598cd2807fSMiao Xie 	int ret = 0;
46046204592SSage Weil 
46146204592SSage Weil 	if (transid) {
46246204592SSage Weil 		if (transid <= root->fs_info->last_trans_committed)
463a4abeea4SJosef Bacik 			goto out;
46446204592SSage Weil 
4658cd2807fSMiao Xie 		ret = -EINVAL;
46646204592SSage Weil 		/* find specified transaction */
467a4abeea4SJosef Bacik 		spin_lock(&root->fs_info->trans_lock);
46846204592SSage Weil 		list_for_each_entry(t, &root->fs_info->trans_list, list) {
46946204592SSage Weil 			if (t->transid == transid) {
47046204592SSage Weil 				cur_trans = t;
471a4abeea4SJosef Bacik 				atomic_inc(&cur_trans->use_count);
4728cd2807fSMiao Xie 				ret = 0;
47346204592SSage Weil 				break;
47446204592SSage Weil 			}
4758cd2807fSMiao Xie 			if (t->transid > transid) {
4768cd2807fSMiao Xie 				ret = 0;
47746204592SSage Weil 				break;
47846204592SSage Weil 			}
4798cd2807fSMiao Xie 		}
480a4abeea4SJosef Bacik 		spin_unlock(&root->fs_info->trans_lock);
4818cd2807fSMiao Xie 		/* The specified transaction doesn't exist */
48246204592SSage Weil 		if (!cur_trans)
4838cd2807fSMiao Xie 			goto out;
48446204592SSage Weil 	} else {
48546204592SSage Weil 		/* find newest transaction that is committing | committed */
486a4abeea4SJosef Bacik 		spin_lock(&root->fs_info->trans_lock);
48746204592SSage Weil 		list_for_each_entry_reverse(t, &root->fs_info->trans_list,
48846204592SSage Weil 					    list) {
48946204592SSage Weil 			if (t->in_commit) {
49046204592SSage Weil 				if (t->commit_done)
4913473f3c0SJosef Bacik 					break;
49246204592SSage Weil 				cur_trans = t;
493a4abeea4SJosef Bacik 				atomic_inc(&cur_trans->use_count);
49446204592SSage Weil 				break;
49546204592SSage Weil 			}
49646204592SSage Weil 		}
497a4abeea4SJosef Bacik 		spin_unlock(&root->fs_info->trans_lock);
49846204592SSage Weil 		if (!cur_trans)
499a4abeea4SJosef Bacik 			goto out;  /* nothing committing|committed */
50046204592SSage Weil 	}
50146204592SSage Weil 
50246204592SSage Weil 	wait_for_commit(root, cur_trans);
50346204592SSage Weil 	put_transaction(cur_trans);
504a4abeea4SJosef Bacik out:
50546204592SSage Weil 	return ret;
50646204592SSage Weil }
50746204592SSage Weil 
50837d1aeeeSChris Mason void btrfs_throttle(struct btrfs_root *root)
50937d1aeeeSChris Mason {
510a4abeea4SJosef Bacik 	if (!atomic_read(&root->fs_info->open_ioctl_trans))
51137d1aeeeSChris Mason 		wait_current_trans(root);
51237d1aeeeSChris Mason }
51337d1aeeeSChris Mason 
5148929ecfaSYan, Zheng static int should_end_transaction(struct btrfs_trans_handle *trans,
5158929ecfaSYan, Zheng 				  struct btrfs_root *root)
5168929ecfaSYan, Zheng {
5178929ecfaSYan, Zheng 	int ret;
51836ba022aSJosef Bacik 
51936ba022aSJosef Bacik 	ret = btrfs_block_rsv_check(root, &root->fs_info->global_block_rsv, 5);
5208929ecfaSYan, Zheng 	return ret ? 1 : 0;
5218929ecfaSYan, Zheng }
5228929ecfaSYan, Zheng 
5238929ecfaSYan, Zheng int btrfs_should_end_transaction(struct btrfs_trans_handle *trans,
5248929ecfaSYan, Zheng 				 struct btrfs_root *root)
5258929ecfaSYan, Zheng {
5268929ecfaSYan, Zheng 	struct btrfs_transaction *cur_trans = trans->transaction;
5278929ecfaSYan, Zheng 	int updates;
52849b25e05SJeff Mahoney 	int err;
5298929ecfaSYan, Zheng 
530a4abeea4SJosef Bacik 	smp_mb();
5318929ecfaSYan, Zheng 	if (cur_trans->blocked || cur_trans->delayed_refs.flushing)
5328929ecfaSYan, Zheng 		return 1;
5338929ecfaSYan, Zheng 
5348929ecfaSYan, Zheng 	updates = trans->delayed_ref_updates;
5358929ecfaSYan, Zheng 	trans->delayed_ref_updates = 0;
53649b25e05SJeff Mahoney 	if (updates) {
53749b25e05SJeff Mahoney 		err = btrfs_run_delayed_refs(trans, root, updates);
53849b25e05SJeff Mahoney 		if (err) /* Error code will also eval true */
53949b25e05SJeff Mahoney 			return err;
54049b25e05SJeff Mahoney 	}
5418929ecfaSYan, Zheng 
5428929ecfaSYan, Zheng 	return should_end_transaction(trans, root);
5438929ecfaSYan, Zheng }
5448929ecfaSYan, Zheng 
54589ce8a63SChris Mason static int __btrfs_end_transaction(struct btrfs_trans_handle *trans,
546a698d075SMiao Xie 			  struct btrfs_root *root, int throttle)
54779154b1bSChris Mason {
5488929ecfaSYan, Zheng 	struct btrfs_transaction *cur_trans = trans->transaction;
549ab78c84dSChris Mason 	struct btrfs_fs_info *info = root->fs_info;
550c3e69d58SChris Mason 	int count = 0;
551a698d075SMiao Xie 	int lock = (trans->type != TRANS_JOIN_NOLOCK);
5524edc2ca3SDave Jones 	int err = 0;
553d6e4a428SChris Mason 
5542a1eb461SJosef Bacik 	if (--trans->use_count) {
5552a1eb461SJosef Bacik 		trans->block_rsv = trans->orig_rsv;
5562a1eb461SJosef Bacik 		return 0;
5572a1eb461SJosef Bacik 	}
5582a1eb461SJosef Bacik 
559edf39272SJan Schmidt 	/*
560edf39272SJan Schmidt 	 * do the qgroup accounting as early as possible
561edf39272SJan Schmidt 	 */
562edf39272SJan Schmidt 	err = btrfs_delayed_refs_qgroup_accounting(trans, info);
563edf39272SJan Schmidt 
564b24e03dbSJosef Bacik 	btrfs_trans_release_metadata(trans, root);
5654c13d758SJosef Bacik 	trans->block_rsv = NULL;
566d13603efSArne Jansen 	/*
567d13603efSArne Jansen 	 * the same root has to be passed to start_transaction and
568d13603efSArne Jansen 	 * end_transaction. Subvolume quota depends on this.
569d13603efSArne Jansen 	 */
570d13603efSArne Jansen 	WARN_ON(trans->root != root);
571c5567237SArne Jansen 
572c5567237SArne Jansen 	if (trans->qgroup_reserved) {
573c5567237SArne Jansen 		btrfs_qgroup_free(root, trans->qgroup_reserved);
574c5567237SArne Jansen 		trans->qgroup_reserved = 0;
575c5567237SArne Jansen 	}
576c5567237SArne Jansen 
577ea658badSJosef Bacik 	if (!list_empty(&trans->new_bgs))
578ea658badSJosef Bacik 		btrfs_create_pending_block_groups(trans, root);
579ea658badSJosef Bacik 
580203bf287SChris Mason 	while (count < 2) {
581c3e69d58SChris Mason 		unsigned long cur = trans->delayed_ref_updates;
582c3e69d58SChris Mason 		trans->delayed_ref_updates = 0;
583c3e69d58SChris Mason 		if (cur &&
584c3e69d58SChris Mason 		    trans->transaction->delayed_refs.num_heads_ready > 64) {
585c3e69d58SChris Mason 			trans->delayed_ref_updates = 0;
586c3e69d58SChris Mason 			btrfs_run_delayed_refs(trans, root, cur);
587c3e69d58SChris Mason 		} else {
588c3e69d58SChris Mason 			break;
589c3e69d58SChris Mason 		}
590c3e69d58SChris Mason 		count++;
59156bec294SChris Mason 	}
5920e721106SJosef Bacik 	btrfs_trans_release_metadata(trans, root);
5930e721106SJosef Bacik 	trans->block_rsv = NULL;
59456bec294SChris Mason 
595ea658badSJosef Bacik 	if (!list_empty(&trans->new_bgs))
596ea658badSJosef Bacik 		btrfs_create_pending_block_groups(trans, root);
597ea658badSJosef Bacik 
598a4abeea4SJosef Bacik 	if (lock && !atomic_read(&root->fs_info->open_ioctl_trans) &&
599a4abeea4SJosef Bacik 	    should_end_transaction(trans, root)) {
6008929ecfaSYan, Zheng 		trans->transaction->blocked = 1;
601a4abeea4SJosef Bacik 		smp_wmb();
602a4abeea4SJosef Bacik 	}
6038929ecfaSYan, Zheng 
6040af3d00bSJosef Bacik 	if (lock && cur_trans->blocked && !cur_trans->in_commit) {
60581317fdeSJosef Bacik 		if (throttle) {
60681317fdeSJosef Bacik 			/*
60781317fdeSJosef Bacik 			 * We may race with somebody else here so end up having
60881317fdeSJosef Bacik 			 * to call end_transaction on ourselves again, so inc
60981317fdeSJosef Bacik 			 * our use_count.
61081317fdeSJosef Bacik 			 */
61181317fdeSJosef Bacik 			trans->use_count++;
6128929ecfaSYan, Zheng 			return btrfs_commit_transaction(trans, root);
61381317fdeSJosef Bacik 		} else {
6148929ecfaSYan, Zheng 			wake_up_process(info->transaction_kthread);
6158929ecfaSYan, Zheng 		}
61681317fdeSJosef Bacik 	}
6178929ecfaSYan, Zheng 
618354aa0fbSMiao Xie 	if (trans->type < TRANS_JOIN_NOLOCK)
6196df7881aSJosef Bacik 		sb_end_intwrite(root->fs_info->sb);
6206df7881aSJosef Bacik 
6218929ecfaSYan, Zheng 	WARN_ON(cur_trans != info->running_transaction);
62213c5a93eSJosef Bacik 	WARN_ON(atomic_read(&cur_trans->num_writers) < 1);
62313c5a93eSJosef Bacik 	atomic_dec(&cur_trans->num_writers);
62489ce8a63SChris Mason 
62599d16cbcSSage Weil 	smp_mb();
62679154b1bSChris Mason 	if (waitqueue_active(&cur_trans->writer_wait))
62779154b1bSChris Mason 		wake_up(&cur_trans->writer_wait);
62879154b1bSChris Mason 	put_transaction(cur_trans);
6299ed74f2dSJosef Bacik 
6309ed74f2dSJosef Bacik 	if (current->journal_info == trans)
6319ed74f2dSJosef Bacik 		current->journal_info = NULL;
632ab78c84dSChris Mason 
63324bbcf04SYan, Zheng 	if (throttle)
63424bbcf04SYan, Zheng 		btrfs_run_delayed_iputs(root);
63524bbcf04SYan, Zheng 
63649b25e05SJeff Mahoney 	if (trans->aborted ||
63749b25e05SJeff Mahoney 	    root->fs_info->fs_state & BTRFS_SUPER_FLAG_ERROR) {
6384edc2ca3SDave Jones 		err = -EIO;
63949b25e05SJeff Mahoney 	}
640edf39272SJan Schmidt 	assert_qgroups_uptodate(trans);
64149b25e05SJeff Mahoney 
6424edc2ca3SDave Jones 	memset(trans, 0, sizeof(*trans));
6434edc2ca3SDave Jones 	kmem_cache_free(btrfs_trans_handle_cachep, trans);
6444edc2ca3SDave Jones 	return err;
64579154b1bSChris Mason }
64679154b1bSChris Mason 
64789ce8a63SChris Mason int btrfs_end_transaction(struct btrfs_trans_handle *trans,
64889ce8a63SChris Mason 			  struct btrfs_root *root)
64989ce8a63SChris Mason {
65016cdcec7SMiao Xie 	int ret;
65116cdcec7SMiao Xie 
652a698d075SMiao Xie 	ret = __btrfs_end_transaction(trans, root, 0);
65316cdcec7SMiao Xie 	if (ret)
65416cdcec7SMiao Xie 		return ret;
65516cdcec7SMiao Xie 	return 0;
65689ce8a63SChris Mason }
65789ce8a63SChris Mason 
65889ce8a63SChris Mason int btrfs_end_transaction_throttle(struct btrfs_trans_handle *trans,
65989ce8a63SChris Mason 				   struct btrfs_root *root)
66089ce8a63SChris Mason {
66116cdcec7SMiao Xie 	int ret;
66216cdcec7SMiao Xie 
663a698d075SMiao Xie 	ret = __btrfs_end_transaction(trans, root, 1);
66416cdcec7SMiao Xie 	if (ret)
66516cdcec7SMiao Xie 		return ret;
66616cdcec7SMiao Xie 	return 0;
66716cdcec7SMiao Xie }
66816cdcec7SMiao Xie 
66916cdcec7SMiao Xie int btrfs_end_transaction_dmeta(struct btrfs_trans_handle *trans,
67016cdcec7SMiao Xie 				struct btrfs_root *root)
67116cdcec7SMiao Xie {
672a698d075SMiao Xie 	return __btrfs_end_transaction(trans, root, 1);
67389ce8a63SChris Mason }
67489ce8a63SChris Mason 
675d352ac68SChris Mason /*
676d352ac68SChris Mason  * when btree blocks are allocated, they have some corresponding bits set for
677d352ac68SChris Mason  * them in one of two extent_io trees.  This is used to make sure all of
678690587d1SChris Mason  * those extents are sent to disk but does not wait on them
679d352ac68SChris Mason  */
680690587d1SChris Mason int btrfs_write_marked_extents(struct btrfs_root *root,
6818cef4e16SYan, Zheng 			       struct extent_io_tree *dirty_pages, int mark)
68279154b1bSChris Mason {
683777e6bd7SChris Mason 	int err = 0;
6847c4452b9SChris Mason 	int werr = 0;
6851728366eSJosef Bacik 	struct address_space *mapping = root->fs_info->btree_inode->i_mapping;
686e6138876SJosef Bacik 	struct extent_state *cached_state = NULL;
687777e6bd7SChris Mason 	u64 start = 0;
6885f39d397SChris Mason 	u64 end;
68953b381b3SDavid Woodhouse 	struct blk_plug plug;
6907c4452b9SChris Mason 
69153b381b3SDavid Woodhouse 	blk_start_plug(&plug);
6921728366eSJosef Bacik 	while (!find_first_extent_bit(dirty_pages, start, &start, &end,
693e6138876SJosef Bacik 				      mark, &cached_state)) {
694e6138876SJosef Bacik 		convert_extent_bit(dirty_pages, start, end, EXTENT_NEED_WAIT,
695e6138876SJosef Bacik 				   mark, &cached_state, GFP_NOFS);
696e6138876SJosef Bacik 		cached_state = NULL;
6971728366eSJosef Bacik 		err = filemap_fdatawrite_range(mapping, start, end);
6987c4452b9SChris Mason 		if (err)
6997c4452b9SChris Mason 			werr = err;
7001728366eSJosef Bacik 		cond_resched();
7011728366eSJosef Bacik 		start = end + 1;
7027c4452b9SChris Mason 	}
703690587d1SChris Mason 	if (err)
704690587d1SChris Mason 		werr = err;
70553b381b3SDavid Woodhouse 	blk_finish_plug(&plug);
706690587d1SChris Mason 	return werr;
707690587d1SChris Mason }
708690587d1SChris Mason 
709690587d1SChris Mason /*
710690587d1SChris Mason  * when btree blocks are allocated, they have some corresponding bits set for
711690587d1SChris Mason  * them in one of two extent_io trees.  This is used to make sure all of
712690587d1SChris Mason  * those extents are on disk for transaction or log commit.  We wait
713690587d1SChris Mason  * on all the pages and clear them from the dirty pages state tree
714690587d1SChris Mason  */
715690587d1SChris Mason int btrfs_wait_marked_extents(struct btrfs_root *root,
7168cef4e16SYan, Zheng 			      struct extent_io_tree *dirty_pages, int mark)
717690587d1SChris Mason {
718690587d1SChris Mason 	int err = 0;
719690587d1SChris Mason 	int werr = 0;
7201728366eSJosef Bacik 	struct address_space *mapping = root->fs_info->btree_inode->i_mapping;
721e6138876SJosef Bacik 	struct extent_state *cached_state = NULL;
722690587d1SChris Mason 	u64 start = 0;
723690587d1SChris Mason 	u64 end;
724690587d1SChris Mason 
7251728366eSJosef Bacik 	while (!find_first_extent_bit(dirty_pages, start, &start, &end,
726e6138876SJosef Bacik 				      EXTENT_NEED_WAIT, &cached_state)) {
727e6138876SJosef Bacik 		clear_extent_bit(dirty_pages, start, end, EXTENT_NEED_WAIT,
728e6138876SJosef Bacik 				 0, 0, &cached_state, GFP_NOFS);
7291728366eSJosef Bacik 		err = filemap_fdatawait_range(mapping, start, end);
730777e6bd7SChris Mason 		if (err)
731777e6bd7SChris Mason 			werr = err;
732777e6bd7SChris Mason 		cond_resched();
7331728366eSJosef Bacik 		start = end + 1;
734777e6bd7SChris Mason 	}
7357c4452b9SChris Mason 	if (err)
7367c4452b9SChris Mason 		werr = err;
7377c4452b9SChris Mason 	return werr;
73879154b1bSChris Mason }
73979154b1bSChris Mason 
740690587d1SChris Mason /*
741690587d1SChris Mason  * when btree blocks are allocated, they have some corresponding bits set for
742690587d1SChris Mason  * them in one of two extent_io trees.  This is used to make sure all of
743690587d1SChris Mason  * those extents are on disk for transaction or log commit
744690587d1SChris Mason  */
745690587d1SChris Mason int btrfs_write_and_wait_marked_extents(struct btrfs_root *root,
7468cef4e16SYan, Zheng 				struct extent_io_tree *dirty_pages, int mark)
747690587d1SChris Mason {
748690587d1SChris Mason 	int ret;
749690587d1SChris Mason 	int ret2;
750690587d1SChris Mason 
7518cef4e16SYan, Zheng 	ret = btrfs_write_marked_extents(root, dirty_pages, mark);
7528cef4e16SYan, Zheng 	ret2 = btrfs_wait_marked_extents(root, dirty_pages, mark);
753bf0da8c1SChris Mason 
754bf0da8c1SChris Mason 	if (ret)
755bf0da8c1SChris Mason 		return ret;
756bf0da8c1SChris Mason 	if (ret2)
757bf0da8c1SChris Mason 		return ret2;
758bf0da8c1SChris Mason 	return 0;
759690587d1SChris Mason }
760690587d1SChris Mason 
761d0c803c4SChris Mason int btrfs_write_and_wait_transaction(struct btrfs_trans_handle *trans,
762d0c803c4SChris Mason 				     struct btrfs_root *root)
763d0c803c4SChris Mason {
764d0c803c4SChris Mason 	if (!trans || !trans->transaction) {
765d0c803c4SChris Mason 		struct inode *btree_inode;
766d0c803c4SChris Mason 		btree_inode = root->fs_info->btree_inode;
767d0c803c4SChris Mason 		return filemap_write_and_wait(btree_inode->i_mapping);
768d0c803c4SChris Mason 	}
769d0c803c4SChris Mason 	return btrfs_write_and_wait_marked_extents(root,
7708cef4e16SYan, Zheng 					   &trans->transaction->dirty_pages,
7718cef4e16SYan, Zheng 					   EXTENT_DIRTY);
772d0c803c4SChris Mason }
773d0c803c4SChris Mason 
774d352ac68SChris Mason /*
775d352ac68SChris Mason  * this is used to update the root pointer in the tree of tree roots.
776d352ac68SChris Mason  *
777d352ac68SChris Mason  * But, in the case of the extent allocation tree, updating the root
778d352ac68SChris Mason  * pointer may allocate blocks which may change the root of the extent
779d352ac68SChris Mason  * allocation tree.
780d352ac68SChris Mason  *
781d352ac68SChris Mason  * So, this loops and repeats and makes sure the cowonly root didn't
782d352ac68SChris Mason  * change while the root pointer was being updated in the metadata.
783d352ac68SChris Mason  */
7840b86a832SChris Mason static int update_cowonly_root(struct btrfs_trans_handle *trans,
78579154b1bSChris Mason 			       struct btrfs_root *root)
78679154b1bSChris Mason {
78779154b1bSChris Mason 	int ret;
7880b86a832SChris Mason 	u64 old_root_bytenr;
78986b9f2ecSYan, Zheng 	u64 old_root_used;
7900b86a832SChris Mason 	struct btrfs_root *tree_root = root->fs_info->tree_root;
79179154b1bSChris Mason 
79286b9f2ecSYan, Zheng 	old_root_used = btrfs_root_used(&root->root_item);
7930b86a832SChris Mason 	btrfs_write_dirty_block_groups(trans, root);
79456bec294SChris Mason 
79579154b1bSChris Mason 	while (1) {
7960b86a832SChris Mason 		old_root_bytenr = btrfs_root_bytenr(&root->root_item);
79786b9f2ecSYan, Zheng 		if (old_root_bytenr == root->node->start &&
79886b9f2ecSYan, Zheng 		    old_root_used == btrfs_root_used(&root->root_item))
79979154b1bSChris Mason 			break;
80087ef2bb4SChris Mason 
8015d4f98a2SYan Zheng 		btrfs_set_root_node(&root->root_item, root->node);
80279154b1bSChris Mason 		ret = btrfs_update_root(trans, tree_root,
8030b86a832SChris Mason 					&root->root_key,
8040b86a832SChris Mason 					&root->root_item);
80549b25e05SJeff Mahoney 		if (ret)
80649b25e05SJeff Mahoney 			return ret;
80756bec294SChris Mason 
80886b9f2ecSYan, Zheng 		old_root_used = btrfs_root_used(&root->root_item);
8094a8c9a62SYan Zheng 		ret = btrfs_write_dirty_block_groups(trans, root);
81049b25e05SJeff Mahoney 		if (ret)
81149b25e05SJeff Mahoney 			return ret;
8120b86a832SChris Mason 	}
813276e680dSYan Zheng 
814276e680dSYan Zheng 	if (root != root->fs_info->extent_root)
815817d52f8SJosef Bacik 		switch_commit_root(root);
816276e680dSYan Zheng 
8170b86a832SChris Mason 	return 0;
8180b86a832SChris Mason }
8190b86a832SChris Mason 
820d352ac68SChris Mason /*
821d352ac68SChris Mason  * update all the cowonly tree roots on disk
82249b25e05SJeff Mahoney  *
82349b25e05SJeff Mahoney  * The error handling in this function may not be obvious. Any of the
82449b25e05SJeff Mahoney  * failures will cause the file system to go offline. We still need
82549b25e05SJeff Mahoney  * to clean up the delayed refs.
826d352ac68SChris Mason  */
8275d4f98a2SYan Zheng static noinline int commit_cowonly_roots(struct btrfs_trans_handle *trans,
8280b86a832SChris Mason 					 struct btrfs_root *root)
8290b86a832SChris Mason {
8300b86a832SChris Mason 	struct btrfs_fs_info *fs_info = root->fs_info;
8310b86a832SChris Mason 	struct list_head *next;
83284234f3aSYan Zheng 	struct extent_buffer *eb;
83356bec294SChris Mason 	int ret;
83484234f3aSYan Zheng 
83556bec294SChris Mason 	ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
83649b25e05SJeff Mahoney 	if (ret)
83749b25e05SJeff Mahoney 		return ret;
83887ef2bb4SChris Mason 
83984234f3aSYan Zheng 	eb = btrfs_lock_root_node(fs_info->tree_root);
84049b25e05SJeff Mahoney 	ret = btrfs_cow_block(trans, fs_info->tree_root, eb, NULL,
84149b25e05SJeff Mahoney 			      0, &eb);
84284234f3aSYan Zheng 	btrfs_tree_unlock(eb);
84384234f3aSYan Zheng 	free_extent_buffer(eb);
8440b86a832SChris Mason 
84549b25e05SJeff Mahoney 	if (ret)
84649b25e05SJeff Mahoney 		return ret;
84749b25e05SJeff Mahoney 
84856bec294SChris Mason 	ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
84949b25e05SJeff Mahoney 	if (ret)
85049b25e05SJeff Mahoney 		return ret;
85187ef2bb4SChris Mason 
852733f4fbbSStefan Behrens 	ret = btrfs_run_dev_stats(trans, root->fs_info);
8538dabb742SStefan Behrens 	WARN_ON(ret);
8548dabb742SStefan Behrens 	ret = btrfs_run_dev_replace(trans, root->fs_info);
8558dabb742SStefan Behrens 	WARN_ON(ret);
856733f4fbbSStefan Behrens 
857546adb0dSJan Schmidt 	ret = btrfs_run_qgroups(trans, root->fs_info);
858546adb0dSJan Schmidt 	BUG_ON(ret);
859546adb0dSJan Schmidt 
860546adb0dSJan Schmidt 	/* run_qgroups might have added some more refs */
861546adb0dSJan Schmidt 	ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
862546adb0dSJan Schmidt 	BUG_ON(ret);
863546adb0dSJan Schmidt 
8640b86a832SChris Mason 	while (!list_empty(&fs_info->dirty_cowonly_roots)) {
8650b86a832SChris Mason 		next = fs_info->dirty_cowonly_roots.next;
8660b86a832SChris Mason 		list_del_init(next);
8670b86a832SChris Mason 		root = list_entry(next, struct btrfs_root, dirty_list);
86887ef2bb4SChris Mason 
86949b25e05SJeff Mahoney 		ret = update_cowonly_root(trans, root);
87049b25e05SJeff Mahoney 		if (ret)
87149b25e05SJeff Mahoney 			return ret;
87279154b1bSChris Mason 	}
873276e680dSYan Zheng 
874276e680dSYan Zheng 	down_write(&fs_info->extent_commit_sem);
875276e680dSYan Zheng 	switch_commit_root(fs_info->extent_root);
876276e680dSYan Zheng 	up_write(&fs_info->extent_commit_sem);
877276e680dSYan Zheng 
8788dabb742SStefan Behrens 	btrfs_after_dev_replace_commit(fs_info);
8798dabb742SStefan Behrens 
88079154b1bSChris Mason 	return 0;
88179154b1bSChris Mason }
88279154b1bSChris Mason 
883d352ac68SChris Mason /*
884d352ac68SChris Mason  * dead roots are old snapshots that need to be deleted.  This allocates
885d352ac68SChris Mason  * a dirty root struct and adds it into the list of dead roots that need to
886d352ac68SChris Mason  * be deleted
887d352ac68SChris Mason  */
8885d4f98a2SYan Zheng int btrfs_add_dead_root(struct btrfs_root *root)
8895eda7b5eSChris Mason {
890a4abeea4SJosef Bacik 	spin_lock(&root->fs_info->trans_lock);
8915d4f98a2SYan Zheng 	list_add(&root->root_list, &root->fs_info->dead_roots);
892a4abeea4SJosef Bacik 	spin_unlock(&root->fs_info->trans_lock);
8935eda7b5eSChris Mason 	return 0;
8945eda7b5eSChris Mason }
8955eda7b5eSChris Mason 
896d352ac68SChris Mason /*
8975d4f98a2SYan Zheng  * update all the cowonly tree roots on disk
898d352ac68SChris Mason  */
8995d4f98a2SYan Zheng static noinline int commit_fs_roots(struct btrfs_trans_handle *trans,
9005d4f98a2SYan Zheng 				    struct btrfs_root *root)
9010f7d52f4SChris Mason {
9020f7d52f4SChris Mason 	struct btrfs_root *gang[8];
9035d4f98a2SYan Zheng 	struct btrfs_fs_info *fs_info = root->fs_info;
9040f7d52f4SChris Mason 	int i;
9050f7d52f4SChris Mason 	int ret;
90654aa1f4dSChris Mason 	int err = 0;
90754aa1f4dSChris Mason 
908a4abeea4SJosef Bacik 	spin_lock(&fs_info->fs_roots_radix_lock);
9090f7d52f4SChris Mason 	while (1) {
9105d4f98a2SYan Zheng 		ret = radix_tree_gang_lookup_tag(&fs_info->fs_roots_radix,
9115d4f98a2SYan Zheng 						 (void **)gang, 0,
9120f7d52f4SChris Mason 						 ARRAY_SIZE(gang),
9130f7d52f4SChris Mason 						 BTRFS_ROOT_TRANS_TAG);
9140f7d52f4SChris Mason 		if (ret == 0)
9150f7d52f4SChris Mason 			break;
9160f7d52f4SChris Mason 		for (i = 0; i < ret; i++) {
9170f7d52f4SChris Mason 			root = gang[i];
9185d4f98a2SYan Zheng 			radix_tree_tag_clear(&fs_info->fs_roots_radix,
9192619ba1fSChris Mason 					(unsigned long)root->root_key.objectid,
9200f7d52f4SChris Mason 					BTRFS_ROOT_TRANS_TAG);
921a4abeea4SJosef Bacik 			spin_unlock(&fs_info->fs_roots_radix_lock);
92231153d81SYan Zheng 
923e02119d5SChris Mason 			btrfs_free_log(trans, root);
9245d4f98a2SYan Zheng 			btrfs_update_reloc_root(trans, root);
925d68fc57bSYan, Zheng 			btrfs_orphan_commit_root(trans, root);
926e02119d5SChris Mason 
92782d5902dSLi Zefan 			btrfs_save_ino_cache(root, trans);
92882d5902dSLi Zefan 
929f1ebcc74SLiu Bo 			/* see comments in should_cow_block() */
930f1ebcc74SLiu Bo 			root->force_cow = 0;
931f1ebcc74SLiu Bo 			smp_wmb();
932f1ebcc74SLiu Bo 
933978d910dSYan Zheng 			if (root->commit_root != root->node) {
934581bb050SLi Zefan 				mutex_lock(&root->fs_commit_mutex);
935817d52f8SJosef Bacik 				switch_commit_root(root);
936581bb050SLi Zefan 				btrfs_unpin_free_ino(root);
937581bb050SLi Zefan 				mutex_unlock(&root->fs_commit_mutex);
938581bb050SLi Zefan 
939978d910dSYan Zheng 				btrfs_set_root_node(&root->root_item,
940978d910dSYan Zheng 						    root->node);
941978d910dSYan Zheng 			}
94231153d81SYan Zheng 
9435d4f98a2SYan Zheng 			err = btrfs_update_root(trans, fs_info->tree_root,
9440f7d52f4SChris Mason 						&root->root_key,
9450f7d52f4SChris Mason 						&root->root_item);
946a4abeea4SJosef Bacik 			spin_lock(&fs_info->fs_roots_radix_lock);
94754aa1f4dSChris Mason 			if (err)
94854aa1f4dSChris Mason 				break;
9490f7d52f4SChris Mason 		}
9509f3a7427SChris Mason 	}
951a4abeea4SJosef Bacik 	spin_unlock(&fs_info->fs_roots_radix_lock);
95254aa1f4dSChris Mason 	return err;
9530f7d52f4SChris Mason }
9540f7d52f4SChris Mason 
955d352ac68SChris Mason /*
956d352ac68SChris Mason  * defrag a given btree.  If cacheonly == 1, this won't read from the disk,
957d352ac68SChris Mason  * otherwise every leaf in the btree is read and defragged.
958d352ac68SChris Mason  */
959e9d0b13bSChris Mason int btrfs_defrag_root(struct btrfs_root *root, int cacheonly)
960e9d0b13bSChris Mason {
961e9d0b13bSChris Mason 	struct btrfs_fs_info *info = root->fs_info;
962e9d0b13bSChris Mason 	struct btrfs_trans_handle *trans;
9638929ecfaSYan, Zheng 	int ret;
964e9d0b13bSChris Mason 
9658929ecfaSYan, Zheng 	if (xchg(&root->defrag_running, 1))
966e9d0b13bSChris Mason 		return 0;
9678929ecfaSYan, Zheng 
9686b80053dSChris Mason 	while (1) {
9698929ecfaSYan, Zheng 		trans = btrfs_start_transaction(root, 0);
9708929ecfaSYan, Zheng 		if (IS_ERR(trans))
9718929ecfaSYan, Zheng 			return PTR_ERR(trans);
9728929ecfaSYan, Zheng 
973e9d0b13bSChris Mason 		ret = btrfs_defrag_leaves(trans, root, cacheonly);
9748929ecfaSYan, Zheng 
975e9d0b13bSChris Mason 		btrfs_end_transaction(trans, root);
976b53d3f5dSLiu Bo 		btrfs_btree_balance_dirty(info->tree_root);
977e9d0b13bSChris Mason 		cond_resched();
978e9d0b13bSChris Mason 
9797841cb28SDavid Sterba 		if (btrfs_fs_closing(root->fs_info) || ret != -EAGAIN)
980e9d0b13bSChris Mason 			break;
981e9d0b13bSChris Mason 	}
982e9d0b13bSChris Mason 	root->defrag_running = 0;
9838929ecfaSYan, Zheng 	return ret;
984e9d0b13bSChris Mason }
985e9d0b13bSChris Mason 
986d352ac68SChris Mason /*
987d352ac68SChris Mason  * new snapshots need to be created at a very specific time in the
988d352ac68SChris Mason  * transaction commit.  This does the actual creation
989d352ac68SChris Mason  */
99080b6794dSChris Mason static noinline int create_pending_snapshot(struct btrfs_trans_handle *trans,
9913063d29fSChris Mason 				   struct btrfs_fs_info *fs_info,
9923063d29fSChris Mason 				   struct btrfs_pending_snapshot *pending)
9933063d29fSChris Mason {
9943063d29fSChris Mason 	struct btrfs_key key;
99580b6794dSChris Mason 	struct btrfs_root_item *new_root_item;
9963063d29fSChris Mason 	struct btrfs_root *tree_root = fs_info->tree_root;
9973063d29fSChris Mason 	struct btrfs_root *root = pending->root;
9986bdb72deSSage Weil 	struct btrfs_root *parent_root;
99998c9942aSLiu Bo 	struct btrfs_block_rsv *rsv;
10006bdb72deSSage Weil 	struct inode *parent_inode;
100142874b3dSMiao Xie 	struct btrfs_path *path;
100242874b3dSMiao Xie 	struct btrfs_dir_item *dir_item;
10036a912213SJosef Bacik 	struct dentry *parent;
1004a22285a6SYan, Zheng 	struct dentry *dentry;
10053063d29fSChris Mason 	struct extent_buffer *tmp;
1006925baeddSChris Mason 	struct extent_buffer *old;
10078ea05e3aSAlexander Block 	struct timespec cur_time = CURRENT_TIME;
10083063d29fSChris Mason 	int ret;
1009d68fc57bSYan, Zheng 	u64 to_reserve = 0;
10106bdb72deSSage Weil 	u64 index = 0;
1011a22285a6SYan, Zheng 	u64 objectid;
1012b83cc969SLi Zefan 	u64 root_flags;
10138ea05e3aSAlexander Block 	uuid_le new_uuid;
10143063d29fSChris Mason 
101542874b3dSMiao Xie 	path = btrfs_alloc_path();
101642874b3dSMiao Xie 	if (!path) {
101742874b3dSMiao Xie 		ret = pending->error = -ENOMEM;
101842874b3dSMiao Xie 		goto path_alloc_fail;
101942874b3dSMiao Xie 	}
102042874b3dSMiao Xie 
102180b6794dSChris Mason 	new_root_item = kmalloc(sizeof(*new_root_item), GFP_NOFS);
102280b6794dSChris Mason 	if (!new_root_item) {
102349b25e05SJeff Mahoney 		ret = pending->error = -ENOMEM;
10246fa9700eSMiao Xie 		goto root_item_alloc_fail;
102580b6794dSChris Mason 	}
1026a22285a6SYan, Zheng 
1027581bb050SLi Zefan 	ret = btrfs_find_free_objectid(tree_root, &objectid);
1028a22285a6SYan, Zheng 	if (ret) {
1029a22285a6SYan, Zheng 		pending->error = ret;
10306fa9700eSMiao Xie 		goto no_free_objectid;
1031a22285a6SYan, Zheng 	}
10323063d29fSChris Mason 
10333fd0a558SYan, Zheng 	btrfs_reloc_pre_snapshot(trans, pending, &to_reserve);
1034d68fc57bSYan, Zheng 
1035d68fc57bSYan, Zheng 	if (to_reserve > 0) {
103608e007d2SMiao Xie 		ret = btrfs_block_rsv_add(root, &pending->block_rsv,
103708e007d2SMiao Xie 					  to_reserve,
103808e007d2SMiao Xie 					  BTRFS_RESERVE_NO_FLUSH);
1039d68fc57bSYan, Zheng 		if (ret) {
1040d68fc57bSYan, Zheng 			pending->error = ret;
10416fa9700eSMiao Xie 			goto no_free_objectid;
1042d68fc57bSYan, Zheng 		}
1043d68fc57bSYan, Zheng 	}
1044d68fc57bSYan, Zheng 
10456f72c7e2SArne Jansen 	ret = btrfs_qgroup_inherit(trans, fs_info, root->root_key.objectid,
10466f72c7e2SArne Jansen 				   objectid, pending->inherit);
10476f72c7e2SArne Jansen 	if (ret) {
10486f72c7e2SArne Jansen 		pending->error = ret;
10496fa9700eSMiao Xie 		goto no_free_objectid;
10506f72c7e2SArne Jansen 	}
10516f72c7e2SArne Jansen 
10523063d29fSChris Mason 	key.objectid = objectid;
1053a22285a6SYan, Zheng 	key.offset = (u64)-1;
1054a22285a6SYan, Zheng 	key.type = BTRFS_ROOT_ITEM_KEY;
10553063d29fSChris Mason 
10566fa9700eSMiao Xie 	rsv = trans->block_rsv;
1057a22285a6SYan, Zheng 	trans->block_rsv = &pending->block_rsv;
10586bdb72deSSage Weil 
1059a22285a6SYan, Zheng 	dentry = pending->dentry;
10606a912213SJosef Bacik 	parent = dget_parent(dentry);
10616a912213SJosef Bacik 	parent_inode = parent->d_inode;
1062a22285a6SYan, Zheng 	parent_root = BTRFS_I(parent_inode)->root;
10637585717fSChris Mason 	record_root_in_trans(trans, parent_root);
1064a22285a6SYan, Zheng 
10656bdb72deSSage Weil 	/*
10666bdb72deSSage Weil 	 * insert the directory item
10676bdb72deSSage Weil 	 */
10686bdb72deSSage Weil 	ret = btrfs_set_inode_index(parent_inode, &index);
106949b25e05SJeff Mahoney 	BUG_ON(ret); /* -ENOMEM */
107042874b3dSMiao Xie 
107142874b3dSMiao Xie 	/* check if there is a file/dir which has the same name. */
107242874b3dSMiao Xie 	dir_item = btrfs_lookup_dir_item(NULL, parent_root, path,
107342874b3dSMiao Xie 					 btrfs_ino(parent_inode),
107442874b3dSMiao Xie 					 dentry->d_name.name,
107542874b3dSMiao Xie 					 dentry->d_name.len, 0);
107642874b3dSMiao Xie 	if (dir_item != NULL && !IS_ERR(dir_item)) {
1077fe66a05aSChris Mason 		pending->error = -EEXIST;
1078fe66a05aSChris Mason 		goto fail;
107942874b3dSMiao Xie 	} else if (IS_ERR(dir_item)) {
108042874b3dSMiao Xie 		ret = PTR_ERR(dir_item);
10818732d44fSMiao Xie 		btrfs_abort_transaction(trans, root, ret);
10828732d44fSMiao Xie 		goto fail;
108379787eaaSJeff Mahoney 	}
108442874b3dSMiao Xie 	btrfs_release_path(path);
10856bdb72deSSage Weil 
1086e999376fSChris Mason 	/*
1087e999376fSChris Mason 	 * pull in the delayed directory update
1088e999376fSChris Mason 	 * and the delayed inode item
1089e999376fSChris Mason 	 * otherwise we corrupt the FS during
1090e999376fSChris Mason 	 * snapshot
1091e999376fSChris Mason 	 */
1092e999376fSChris Mason 	ret = btrfs_run_delayed_items(trans, root);
10938732d44fSMiao Xie 	if (ret) {	/* Transaction aborted */
10948732d44fSMiao Xie 		btrfs_abort_transaction(trans, root, ret);
10958732d44fSMiao Xie 		goto fail;
10968732d44fSMiao Xie 	}
1097e999376fSChris Mason 
10987585717fSChris Mason 	record_root_in_trans(trans, root);
10996bdb72deSSage Weil 	btrfs_set_root_last_snapshot(&root->root_item, trans->transid);
11006bdb72deSSage Weil 	memcpy(new_root_item, &root->root_item, sizeof(*new_root_item));
110108fe4db1SLi Zefan 	btrfs_check_and_init_root_item(new_root_item);
11026bdb72deSSage Weil 
1103b83cc969SLi Zefan 	root_flags = btrfs_root_flags(new_root_item);
1104b83cc969SLi Zefan 	if (pending->readonly)
1105b83cc969SLi Zefan 		root_flags |= BTRFS_ROOT_SUBVOL_RDONLY;
1106b83cc969SLi Zefan 	else
1107b83cc969SLi Zefan 		root_flags &= ~BTRFS_ROOT_SUBVOL_RDONLY;
1108b83cc969SLi Zefan 	btrfs_set_root_flags(new_root_item, root_flags);
1109b83cc969SLi Zefan 
11108ea05e3aSAlexander Block 	btrfs_set_root_generation_v2(new_root_item,
11118ea05e3aSAlexander Block 			trans->transid);
11128ea05e3aSAlexander Block 	uuid_le_gen(&new_uuid);
11138ea05e3aSAlexander Block 	memcpy(new_root_item->uuid, new_uuid.b, BTRFS_UUID_SIZE);
11148ea05e3aSAlexander Block 	memcpy(new_root_item->parent_uuid, root->root_item.uuid,
11158ea05e3aSAlexander Block 			BTRFS_UUID_SIZE);
11168ea05e3aSAlexander Block 	new_root_item->otime.sec = cpu_to_le64(cur_time.tv_sec);
1117dadd1105SDan Carpenter 	new_root_item->otime.nsec = cpu_to_le32(cur_time.tv_nsec);
11188ea05e3aSAlexander Block 	btrfs_set_root_otransid(new_root_item, trans->transid);
11198ea05e3aSAlexander Block 	memset(&new_root_item->stime, 0, sizeof(new_root_item->stime));
11208ea05e3aSAlexander Block 	memset(&new_root_item->rtime, 0, sizeof(new_root_item->rtime));
11218ea05e3aSAlexander Block 	btrfs_set_root_stransid(new_root_item, 0);
11228ea05e3aSAlexander Block 	btrfs_set_root_rtransid(new_root_item, 0);
11238ea05e3aSAlexander Block 
1124925baeddSChris Mason 	old = btrfs_lock_root_node(root);
112549b25e05SJeff Mahoney 	ret = btrfs_cow_block(trans, root, old, NULL, 0, &old);
112679787eaaSJeff Mahoney 	if (ret) {
112779787eaaSJeff Mahoney 		btrfs_tree_unlock(old);
112879787eaaSJeff Mahoney 		free_extent_buffer(old);
11298732d44fSMiao Xie 		btrfs_abort_transaction(trans, root, ret);
11308732d44fSMiao Xie 		goto fail;
113179787eaaSJeff Mahoney 	}
113249b25e05SJeff Mahoney 
11335d4f98a2SYan Zheng 	btrfs_set_lock_blocking(old);
11343063d29fSChris Mason 
113549b25e05SJeff Mahoney 	ret = btrfs_copy_root(trans, root, old, &tmp, objectid);
113679787eaaSJeff Mahoney 	/* clean up in any case */
1137925baeddSChris Mason 	btrfs_tree_unlock(old);
1138925baeddSChris Mason 	free_extent_buffer(old);
11398732d44fSMiao Xie 	if (ret) {
11408732d44fSMiao Xie 		btrfs_abort_transaction(trans, root, ret);
11418732d44fSMiao Xie 		goto fail;
11428732d44fSMiao Xie 	}
11433063d29fSChris Mason 
1144f1ebcc74SLiu Bo 	/* see comments in should_cow_block() */
1145f1ebcc74SLiu Bo 	root->force_cow = 1;
1146f1ebcc74SLiu Bo 	smp_wmb();
1147f1ebcc74SLiu Bo 
11485d4f98a2SYan Zheng 	btrfs_set_root_node(new_root_item, tmp);
1149a22285a6SYan, Zheng 	/* record when the snapshot was created in key.offset */
1150a22285a6SYan, Zheng 	key.offset = trans->transid;
1151a22285a6SYan, Zheng 	ret = btrfs_insert_root(trans, tree_root, &key, new_root_item);
1152925baeddSChris Mason 	btrfs_tree_unlock(tmp);
11533063d29fSChris Mason 	free_extent_buffer(tmp);
11548732d44fSMiao Xie 	if (ret) {
11558732d44fSMiao Xie 		btrfs_abort_transaction(trans, root, ret);
11568732d44fSMiao Xie 		goto fail;
11578732d44fSMiao Xie 	}
11580660b5afSChris Mason 
1159a22285a6SYan, Zheng 	/*
1160a22285a6SYan, Zheng 	 * insert root back/forward references
1161a22285a6SYan, Zheng 	 */
1162a22285a6SYan, Zheng 	ret = btrfs_add_root_ref(trans, tree_root, objectid,
1163a22285a6SYan, Zheng 				 parent_root->root_key.objectid,
116433345d01SLi Zefan 				 btrfs_ino(parent_inode), index,
1165a22285a6SYan, Zheng 				 dentry->d_name.name, dentry->d_name.len);
11668732d44fSMiao Xie 	if (ret) {
11678732d44fSMiao Xie 		btrfs_abort_transaction(trans, root, ret);
11688732d44fSMiao Xie 		goto fail;
11698732d44fSMiao Xie 	}
1170a22285a6SYan, Zheng 
1171a22285a6SYan, Zheng 	key.offset = (u64)-1;
1172a22285a6SYan, Zheng 	pending->snap = btrfs_read_fs_root_no_name(root->fs_info, &key);
117379787eaaSJeff Mahoney 	if (IS_ERR(pending->snap)) {
117479787eaaSJeff Mahoney 		ret = PTR_ERR(pending->snap);
11758732d44fSMiao Xie 		btrfs_abort_transaction(trans, root, ret);
11768732d44fSMiao Xie 		goto fail;
117779787eaaSJeff Mahoney 	}
1178d68fc57bSYan, Zheng 
117949b25e05SJeff Mahoney 	ret = btrfs_reloc_post_snapshot(trans, pending);
11808732d44fSMiao Xie 	if (ret) {
11818732d44fSMiao Xie 		btrfs_abort_transaction(trans, root, ret);
11828732d44fSMiao Xie 		goto fail;
11838732d44fSMiao Xie 	}
1184361048f5SMiao Xie 
1185361048f5SMiao Xie 	ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
11868732d44fSMiao Xie 	if (ret) {
11878732d44fSMiao Xie 		btrfs_abort_transaction(trans, root, ret);
11888732d44fSMiao Xie 		goto fail;
11898732d44fSMiao Xie 	}
119042874b3dSMiao Xie 
119142874b3dSMiao Xie 	ret = btrfs_insert_dir_item(trans, parent_root,
119242874b3dSMiao Xie 				    dentry->d_name.name, dentry->d_name.len,
119342874b3dSMiao Xie 				    parent_inode, &key,
119442874b3dSMiao Xie 				    BTRFS_FT_DIR, index);
119542874b3dSMiao Xie 	/* We have check then name at the beginning, so it is impossible. */
11969c52057cSChris Mason 	BUG_ON(ret == -EEXIST || ret == -EOVERFLOW);
11978732d44fSMiao Xie 	if (ret) {
11988732d44fSMiao Xie 		btrfs_abort_transaction(trans, root, ret);
11998732d44fSMiao Xie 		goto fail;
12008732d44fSMiao Xie 	}
120142874b3dSMiao Xie 
120242874b3dSMiao Xie 	btrfs_i_size_write(parent_inode, parent_inode->i_size +
120342874b3dSMiao Xie 					 dentry->d_name.len * 2);
120442874b3dSMiao Xie 	parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
1205be6aef60SJosef Bacik 	ret = btrfs_update_inode_fallback(trans, parent_root, parent_inode);
120642874b3dSMiao Xie 	if (ret)
12078732d44fSMiao Xie 		btrfs_abort_transaction(trans, root, ret);
12083063d29fSChris Mason fail:
12096fa9700eSMiao Xie 	dput(parent);
121098c9942aSLiu Bo 	trans->block_rsv = rsv;
12116fa9700eSMiao Xie no_free_objectid:
12126fa9700eSMiao Xie 	kfree(new_root_item);
12136fa9700eSMiao Xie root_item_alloc_fail:
121442874b3dSMiao Xie 	btrfs_free_path(path);
121542874b3dSMiao Xie path_alloc_fail:
1216a22285a6SYan, Zheng 	btrfs_block_rsv_release(root, &pending->block_rsv, (u64)-1);
121749b25e05SJeff Mahoney 	return ret;
12183063d29fSChris Mason }
12193063d29fSChris Mason 
1220d352ac68SChris Mason /*
1221d352ac68SChris Mason  * create all the snapshots we've scheduled for creation
1222d352ac68SChris Mason  */
122380b6794dSChris Mason static noinline int create_pending_snapshots(struct btrfs_trans_handle *trans,
12243063d29fSChris Mason 					     struct btrfs_fs_info *fs_info)
12253063d29fSChris Mason {
12263063d29fSChris Mason 	struct btrfs_pending_snapshot *pending;
12273063d29fSChris Mason 	struct list_head *head = &trans->transaction->pending_snapshots;
12283de4586cSChris Mason 
1229fe66a05aSChris Mason 	list_for_each_entry(pending, head, list)
1230fe66a05aSChris Mason 		create_pending_snapshot(trans, fs_info, pending);
12313de4586cSChris Mason 	return 0;
12323de4586cSChris Mason }
12333de4586cSChris Mason 
12345d4f98a2SYan Zheng static void update_super_roots(struct btrfs_root *root)
12355d4f98a2SYan Zheng {
12365d4f98a2SYan Zheng 	struct btrfs_root_item *root_item;
12375d4f98a2SYan Zheng 	struct btrfs_super_block *super;
12385d4f98a2SYan Zheng 
12396c41761fSDavid Sterba 	super = root->fs_info->super_copy;
12405d4f98a2SYan Zheng 
12415d4f98a2SYan Zheng 	root_item = &root->fs_info->chunk_root->root_item;
12425d4f98a2SYan Zheng 	super->chunk_root = root_item->bytenr;
12435d4f98a2SYan Zheng 	super->chunk_root_generation = root_item->generation;
12445d4f98a2SYan Zheng 	super->chunk_root_level = root_item->level;
12455d4f98a2SYan Zheng 
12465d4f98a2SYan Zheng 	root_item = &root->fs_info->tree_root->root_item;
12475d4f98a2SYan Zheng 	super->root = root_item->bytenr;
12485d4f98a2SYan Zheng 	super->generation = root_item->generation;
12495d4f98a2SYan Zheng 	super->root_level = root_item->level;
125073bc1876SJosef Bacik 	if (btrfs_test_opt(root, SPACE_CACHE))
12510af3d00bSJosef Bacik 		super->cache_generation = root_item->generation;
12525d4f98a2SYan Zheng }
12535d4f98a2SYan Zheng 
1254f36f3042SChris Mason int btrfs_transaction_in_commit(struct btrfs_fs_info *info)
1255f36f3042SChris Mason {
1256f36f3042SChris Mason 	int ret = 0;
1257a4abeea4SJosef Bacik 	spin_lock(&info->trans_lock);
1258f36f3042SChris Mason 	if (info->running_transaction)
1259f36f3042SChris Mason 		ret = info->running_transaction->in_commit;
1260a4abeea4SJosef Bacik 	spin_unlock(&info->trans_lock);
1261f36f3042SChris Mason 	return ret;
1262f36f3042SChris Mason }
1263f36f3042SChris Mason 
12648929ecfaSYan, Zheng int btrfs_transaction_blocked(struct btrfs_fs_info *info)
12658929ecfaSYan, Zheng {
12668929ecfaSYan, Zheng 	int ret = 0;
1267a4abeea4SJosef Bacik 	spin_lock(&info->trans_lock);
12688929ecfaSYan, Zheng 	if (info->running_transaction)
12698929ecfaSYan, Zheng 		ret = info->running_transaction->blocked;
1270a4abeea4SJosef Bacik 	spin_unlock(&info->trans_lock);
12718929ecfaSYan, Zheng 	return ret;
12728929ecfaSYan, Zheng }
12738929ecfaSYan, Zheng 
1274bb9c12c9SSage Weil /*
1275bb9c12c9SSage Weil  * wait for the current transaction commit to start and block subsequent
1276bb9c12c9SSage Weil  * transaction joins
1277bb9c12c9SSage Weil  */
1278bb9c12c9SSage Weil static void wait_current_trans_commit_start(struct btrfs_root *root,
1279bb9c12c9SSage Weil 					    struct btrfs_transaction *trans)
1280bb9c12c9SSage Weil {
128172d63ed6SLi Zefan 	wait_event(root->fs_info->transaction_blocked_wait, trans->in_commit);
1282bb9c12c9SSage Weil }
1283bb9c12c9SSage Weil 
1284bb9c12c9SSage Weil /*
1285bb9c12c9SSage Weil  * wait for the current transaction to start and then become unblocked.
1286bb9c12c9SSage Weil  * caller holds ref.
1287bb9c12c9SSage Weil  */
1288bb9c12c9SSage Weil static void wait_current_trans_commit_start_and_unblock(struct btrfs_root *root,
1289bb9c12c9SSage Weil 					 struct btrfs_transaction *trans)
1290bb9c12c9SSage Weil {
129172d63ed6SLi Zefan 	wait_event(root->fs_info->transaction_wait,
129272d63ed6SLi Zefan 		   trans->commit_done || (trans->in_commit && !trans->blocked));
1293bb9c12c9SSage Weil }
1294bb9c12c9SSage Weil 
1295bb9c12c9SSage Weil /*
1296bb9c12c9SSage Weil  * commit transactions asynchronously. once btrfs_commit_transaction_async
1297bb9c12c9SSage Weil  * returns, any subsequent transaction will not be allowed to join.
1298bb9c12c9SSage Weil  */
1299bb9c12c9SSage Weil struct btrfs_async_commit {
1300bb9c12c9SSage Weil 	struct btrfs_trans_handle *newtrans;
1301bb9c12c9SSage Weil 	struct btrfs_root *root;
1302bb9c12c9SSage Weil 	struct delayed_work work;
1303bb9c12c9SSage Weil };
1304bb9c12c9SSage Weil 
1305bb9c12c9SSage Weil static void do_async_commit(struct work_struct *work)
1306bb9c12c9SSage Weil {
1307bb9c12c9SSage Weil 	struct btrfs_async_commit *ac =
1308bb9c12c9SSage Weil 		container_of(work, struct btrfs_async_commit, work.work);
1309bb9c12c9SSage Weil 
13106fc4e354SSage Weil 	/*
13116fc4e354SSage Weil 	 * We've got freeze protection passed with the transaction.
13126fc4e354SSage Weil 	 * Tell lockdep about it.
13136fc4e354SSage Weil 	 */
1314ff7c1d33SMiao Xie 	if (ac->newtrans->type < TRANS_JOIN_NOLOCK)
13156fc4e354SSage Weil 		rwsem_acquire_read(
13166fc4e354SSage Weil 		     &ac->root->fs_info->sb->s_writers.lock_map[SB_FREEZE_FS-1],
13176fc4e354SSage Weil 		     0, 1, _THIS_IP_);
13186fc4e354SSage Weil 
1319e209db7aSSage Weil 	current->journal_info = ac->newtrans;
1320e209db7aSSage Weil 
1321bb9c12c9SSage Weil 	btrfs_commit_transaction(ac->newtrans, ac->root);
1322bb9c12c9SSage Weil 	kfree(ac);
1323bb9c12c9SSage Weil }
1324bb9c12c9SSage Weil 
1325bb9c12c9SSage Weil int btrfs_commit_transaction_async(struct btrfs_trans_handle *trans,
1326bb9c12c9SSage Weil 				   struct btrfs_root *root,
1327bb9c12c9SSage Weil 				   int wait_for_unblock)
1328bb9c12c9SSage Weil {
1329bb9c12c9SSage Weil 	struct btrfs_async_commit *ac;
1330bb9c12c9SSage Weil 	struct btrfs_transaction *cur_trans;
1331bb9c12c9SSage Weil 
1332bb9c12c9SSage Weil 	ac = kmalloc(sizeof(*ac), GFP_NOFS);
1333db5b493aSTsutomu Itoh 	if (!ac)
1334db5b493aSTsutomu Itoh 		return -ENOMEM;
1335bb9c12c9SSage Weil 
1336bb9c12c9SSage Weil 	INIT_DELAYED_WORK(&ac->work, do_async_commit);
1337bb9c12c9SSage Weil 	ac->root = root;
13387a7eaa40SJosef Bacik 	ac->newtrans = btrfs_join_transaction(root);
13393612b495STsutomu Itoh 	if (IS_ERR(ac->newtrans)) {
13403612b495STsutomu Itoh 		int err = PTR_ERR(ac->newtrans);
13413612b495STsutomu Itoh 		kfree(ac);
13423612b495STsutomu Itoh 		return err;
13433612b495STsutomu Itoh 	}
1344bb9c12c9SSage Weil 
1345bb9c12c9SSage Weil 	/* take transaction reference */
1346bb9c12c9SSage Weil 	cur_trans = trans->transaction;
134713c5a93eSJosef Bacik 	atomic_inc(&cur_trans->use_count);
1348bb9c12c9SSage Weil 
1349bb9c12c9SSage Weil 	btrfs_end_transaction(trans, root);
13506fc4e354SSage Weil 
13516fc4e354SSage Weil 	/*
13526fc4e354SSage Weil 	 * Tell lockdep we've released the freeze rwsem, since the
13536fc4e354SSage Weil 	 * async commit thread will be the one to unlock it.
13546fc4e354SSage Weil 	 */
1355ff7c1d33SMiao Xie 	if (trans->type < TRANS_JOIN_NOLOCK)
1356ff7c1d33SMiao Xie 		rwsem_release(
1357ff7c1d33SMiao Xie 			&root->fs_info->sb->s_writers.lock_map[SB_FREEZE_FS-1],
13586fc4e354SSage Weil 			1, _THIS_IP_);
13596fc4e354SSage Weil 
1360bb9c12c9SSage Weil 	schedule_delayed_work(&ac->work, 0);
1361bb9c12c9SSage Weil 
1362bb9c12c9SSage Weil 	/* wait for transaction to start and unblock */
1363bb9c12c9SSage Weil 	if (wait_for_unblock)
1364bb9c12c9SSage Weil 		wait_current_trans_commit_start_and_unblock(root, cur_trans);
1365bb9c12c9SSage Weil 	else
1366bb9c12c9SSage Weil 		wait_current_trans_commit_start(root, cur_trans);
1367bb9c12c9SSage Weil 
136838e88054SSage Weil 	if (current->journal_info == trans)
136938e88054SSage Weil 		current->journal_info = NULL;
137038e88054SSage Weil 
137138e88054SSage Weil 	put_transaction(cur_trans);
1372bb9c12c9SSage Weil 	return 0;
1373bb9c12c9SSage Weil }
1374bb9c12c9SSage Weil 
137549b25e05SJeff Mahoney 
137649b25e05SJeff Mahoney static void cleanup_transaction(struct btrfs_trans_handle *trans,
13777b8b92afSJosef Bacik 				struct btrfs_root *root, int err)
137849b25e05SJeff Mahoney {
137949b25e05SJeff Mahoney 	struct btrfs_transaction *cur_trans = trans->transaction;
138049b25e05SJeff Mahoney 
138149b25e05SJeff Mahoney 	WARN_ON(trans->use_count > 1);
138249b25e05SJeff Mahoney 
13837b8b92afSJosef Bacik 	btrfs_abort_transaction(trans, root, err);
13847b8b92afSJosef Bacik 
138549b25e05SJeff Mahoney 	spin_lock(&root->fs_info->trans_lock);
138649b25e05SJeff Mahoney 	list_del_init(&cur_trans->list);
1387d7096fc3SJosef Bacik 	if (cur_trans == root->fs_info->running_transaction) {
1388d7096fc3SJosef Bacik 		root->fs_info->running_transaction = NULL;
1389d7096fc3SJosef Bacik 		root->fs_info->trans_no_join = 0;
1390d7096fc3SJosef Bacik 	}
139149b25e05SJeff Mahoney 	spin_unlock(&root->fs_info->trans_lock);
139249b25e05SJeff Mahoney 
139349b25e05SJeff Mahoney 	btrfs_cleanup_one_transaction(trans->transaction, root);
139449b25e05SJeff Mahoney 
139549b25e05SJeff Mahoney 	put_transaction(cur_trans);
139649b25e05SJeff Mahoney 	put_transaction(cur_trans);
139749b25e05SJeff Mahoney 
139849b25e05SJeff Mahoney 	trace_btrfs_transaction_commit(root);
139949b25e05SJeff Mahoney 
140049b25e05SJeff Mahoney 	btrfs_scrub_continue(root);
140149b25e05SJeff Mahoney 
140249b25e05SJeff Mahoney 	if (current->journal_info == trans)
140349b25e05SJeff Mahoney 		current->journal_info = NULL;
140449b25e05SJeff Mahoney 
140549b25e05SJeff Mahoney 	kmem_cache_free(btrfs_trans_handle_cachep, trans);
140649b25e05SJeff Mahoney }
140749b25e05SJeff Mahoney 
1408ca469637SMiao Xie static int btrfs_flush_all_pending_stuffs(struct btrfs_trans_handle *trans,
1409ca469637SMiao Xie 					  struct btrfs_root *root)
1410ca469637SMiao Xie {
1411ca469637SMiao Xie 	int flush_on_commit = btrfs_test_opt(root, FLUSHONCOMMIT);
1412ca469637SMiao Xie 	int snap_pending = 0;
1413ca469637SMiao Xie 	int ret;
1414ca469637SMiao Xie 
1415ca469637SMiao Xie 	if (!flush_on_commit) {
1416ca469637SMiao Xie 		spin_lock(&root->fs_info->trans_lock);
1417ca469637SMiao Xie 		if (!list_empty(&trans->transaction->pending_snapshots))
1418ca469637SMiao Xie 			snap_pending = 1;
1419ca469637SMiao Xie 		spin_unlock(&root->fs_info->trans_lock);
1420ca469637SMiao Xie 	}
1421ca469637SMiao Xie 
1422ca469637SMiao Xie 	if (flush_on_commit || snap_pending) {
1423ca469637SMiao Xie 		btrfs_start_delalloc_inodes(root, 1);
1424ca469637SMiao Xie 		btrfs_wait_ordered_extents(root, 1);
1425ca469637SMiao Xie 	}
1426ca469637SMiao Xie 
1427ca469637SMiao Xie 	ret = btrfs_run_delayed_items(trans, root);
1428ca469637SMiao Xie 	if (ret)
1429ca469637SMiao Xie 		return ret;
1430ca469637SMiao Xie 
1431ca469637SMiao Xie 	/*
1432ca469637SMiao Xie 	 * running the delayed items may have added new refs. account
1433ca469637SMiao Xie 	 * them now so that they hinder processing of more delayed refs
1434ca469637SMiao Xie 	 * as little as possible.
1435ca469637SMiao Xie 	 */
1436ca469637SMiao Xie 	btrfs_delayed_refs_qgroup_accounting(trans, root->fs_info);
1437ca469637SMiao Xie 
1438ca469637SMiao Xie 	/*
1439ca469637SMiao Xie 	 * rename don't use btrfs_join_transaction, so, once we
1440ca469637SMiao Xie 	 * set the transaction to blocked above, we aren't going
1441ca469637SMiao Xie 	 * to get any new ordered operations.  We can safely run
1442ca469637SMiao Xie 	 * it here and no for sure that nothing new will be added
1443ca469637SMiao Xie 	 * to the list
1444ca469637SMiao Xie 	 */
1445ca469637SMiao Xie 	btrfs_run_ordered_operations(root, 1);
1446ca469637SMiao Xie 
1447ca469637SMiao Xie 	return 0;
1448ca469637SMiao Xie }
1449ca469637SMiao Xie 
1450bb9c12c9SSage Weil /*
1451bb9c12c9SSage Weil  * btrfs_transaction state sequence:
1452bb9c12c9SSage Weil  *    in_commit = 0, blocked = 0  (initial)
1453bb9c12c9SSage Weil  *    in_commit = 1, blocked = 1
1454bb9c12c9SSage Weil  *    blocked = 0
1455bb9c12c9SSage Weil  *    commit_done = 1
1456bb9c12c9SSage Weil  */
145779154b1bSChris Mason int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
145879154b1bSChris Mason 			     struct btrfs_root *root)
145979154b1bSChris Mason {
146015ee9bc7SJosef Bacik 	unsigned long joined = 0;
146149b25e05SJeff Mahoney 	struct btrfs_transaction *cur_trans = trans->transaction;
14628fd17795SChris Mason 	struct btrfs_transaction *prev_trans = NULL;
146379154b1bSChris Mason 	DEFINE_WAIT(wait);
146425287e0aSMiao Xie 	int ret;
146589573b9cSChris Mason 	int should_grow = 0;
146689573b9cSChris Mason 	unsigned long now = get_seconds();
146779154b1bSChris Mason 
146825287e0aSMiao Xie 	ret = btrfs_run_ordered_operations(root, 0);
146925287e0aSMiao Xie 	if (ret) {
147025287e0aSMiao Xie 		btrfs_abort_transaction(trans, root, ret);
147149b25e05SJeff Mahoney 		goto cleanup_transaction;
147225287e0aSMiao Xie 	}
147325287e0aSMiao Xie 
147425287e0aSMiao Xie 	if (cur_trans->aborted) {
147525287e0aSMiao Xie 		ret = cur_trans->aborted;
147625287e0aSMiao Xie 		goto cleanup_transaction;
147725287e0aSMiao Xie 	}
147849b25e05SJeff Mahoney 
147956bec294SChris Mason 	/* make a pass through all the delayed refs we have so far
148056bec294SChris Mason 	 * any runnings procs may add more while we are here
148156bec294SChris Mason 	 */
148256bec294SChris Mason 	ret = btrfs_run_delayed_refs(trans, root, 0);
148349b25e05SJeff Mahoney 	if (ret)
148449b25e05SJeff Mahoney 		goto cleanup_transaction;
148556bec294SChris Mason 
14860e721106SJosef Bacik 	btrfs_trans_release_metadata(trans, root);
14870e721106SJosef Bacik 	trans->block_rsv = NULL;
14880e721106SJosef Bacik 
1489b7ec40d7SChris Mason 	cur_trans = trans->transaction;
149049b25e05SJeff Mahoney 
149156bec294SChris Mason 	/*
149256bec294SChris Mason 	 * set the flushing flag so procs in this transaction have to
149356bec294SChris Mason 	 * start sending their work down.
149456bec294SChris Mason 	 */
1495b7ec40d7SChris Mason 	cur_trans->delayed_refs.flushing = 1;
149656bec294SChris Mason 
1497ea658badSJosef Bacik 	if (!list_empty(&trans->new_bgs))
1498ea658badSJosef Bacik 		btrfs_create_pending_block_groups(trans, root);
1499ea658badSJosef Bacik 
1500c3e69d58SChris Mason 	ret = btrfs_run_delayed_refs(trans, root, 0);
150149b25e05SJeff Mahoney 	if (ret)
150249b25e05SJeff Mahoney 		goto cleanup_transaction;
150356bec294SChris Mason 
1504a4abeea4SJosef Bacik 	spin_lock(&cur_trans->commit_lock);
1505b7ec40d7SChris Mason 	if (cur_trans->in_commit) {
1506a4abeea4SJosef Bacik 		spin_unlock(&cur_trans->commit_lock);
150713c5a93eSJosef Bacik 		atomic_inc(&cur_trans->use_count);
150849b25e05SJeff Mahoney 		ret = btrfs_end_transaction(trans, root);
1509ccd467d6SChris Mason 
1510b9c8300cSLi Zefan 		wait_for_commit(root, cur_trans);
151115ee9bc7SJosef Bacik 
151279154b1bSChris Mason 		put_transaction(cur_trans);
151315ee9bc7SJosef Bacik 
151449b25e05SJeff Mahoney 		return ret;
151579154b1bSChris Mason 	}
15164313b399SChris Mason 
15172c90e5d6SChris Mason 	trans->transaction->in_commit = 1;
1518f9295749SChris Mason 	trans->transaction->blocked = 1;
1519a4abeea4SJosef Bacik 	spin_unlock(&cur_trans->commit_lock);
1520bb9c12c9SSage Weil 	wake_up(&root->fs_info->transaction_blocked_wait);
1521bb9c12c9SSage Weil 
1522a4abeea4SJosef Bacik 	spin_lock(&root->fs_info->trans_lock);
1523ccd467d6SChris Mason 	if (cur_trans->list.prev != &root->fs_info->trans_list) {
1524ccd467d6SChris Mason 		prev_trans = list_entry(cur_trans->list.prev,
1525ccd467d6SChris Mason 					struct btrfs_transaction, list);
1526ccd467d6SChris Mason 		if (!prev_trans->commit_done) {
152713c5a93eSJosef Bacik 			atomic_inc(&prev_trans->use_count);
1528a4abeea4SJosef Bacik 			spin_unlock(&root->fs_info->trans_lock);
1529ccd467d6SChris Mason 
1530ccd467d6SChris Mason 			wait_for_commit(root, prev_trans);
1531ccd467d6SChris Mason 
153215ee9bc7SJosef Bacik 			put_transaction(prev_trans);
1533a4abeea4SJosef Bacik 		} else {
1534a4abeea4SJosef Bacik 			spin_unlock(&root->fs_info->trans_lock);
1535ccd467d6SChris Mason 		}
1536a4abeea4SJosef Bacik 	} else {
1537a4abeea4SJosef Bacik 		spin_unlock(&root->fs_info->trans_lock);
1538ccd467d6SChris Mason 	}
153915ee9bc7SJosef Bacik 
1540e39e64acSChris Mason 	if (!btrfs_test_opt(root, SSD) &&
1541e39e64acSChris Mason 	    (now < cur_trans->start_time || now - cur_trans->start_time < 1))
154289573b9cSChris Mason 		should_grow = 1;
154389573b9cSChris Mason 
154415ee9bc7SJosef Bacik 	do {
154515ee9bc7SJosef Bacik 		joined = cur_trans->num_joined;
15467ea394f1SYan Zheng 
15472c90e5d6SChris Mason 		WARN_ON(cur_trans != trans->transaction);
154815ee9bc7SJosef Bacik 
1549ca469637SMiao Xie 		ret = btrfs_flush_all_pending_stuffs(trans, root);
155049b25e05SJeff Mahoney 		if (ret)
155149b25e05SJeff Mahoney 			goto cleanup_transaction;
155216cdcec7SMiao Xie 
1553ed3b3d31SChris Mason 		prepare_to_wait(&cur_trans->writer_wait, &wait,
1554ed3b3d31SChris Mason 				TASK_UNINTERRUPTIBLE);
1555ed3b3d31SChris Mason 
155613c5a93eSJosef Bacik 		if (atomic_read(&cur_trans->num_writers) > 1)
155799d16cbcSSage Weil 			schedule_timeout(MAX_SCHEDULE_TIMEOUT);
155899d16cbcSSage Weil 		else if (should_grow)
155999d16cbcSSage Weil 			schedule_timeout(1);
156015ee9bc7SJosef Bacik 
156115ee9bc7SJosef Bacik 		finish_wait(&cur_trans->writer_wait, &wait);
1562ed0ca140SJosef Bacik 	} while (atomic_read(&cur_trans->num_writers) > 1 ||
1563ed0ca140SJosef Bacik 		 (should_grow && cur_trans->num_joined != joined));
1564ed0ca140SJosef Bacik 
1565ca469637SMiao Xie 	ret = btrfs_flush_all_pending_stuffs(trans, root);
1566ca469637SMiao Xie 	if (ret)
1567ca469637SMiao Xie 		goto cleanup_transaction;
1568ca469637SMiao Xie 
1569ed0ca140SJosef Bacik 	/*
1570ed0ca140SJosef Bacik 	 * Ok now we need to make sure to block out any other joins while we
1571ed0ca140SJosef Bacik 	 * commit the transaction.  We could have started a join before setting
1572ed0ca140SJosef Bacik 	 * no_join so make sure to wait for num_writers to == 1 again.
1573ed0ca140SJosef Bacik 	 */
1574a4abeea4SJosef Bacik 	spin_lock(&root->fs_info->trans_lock);
1575a4abeea4SJosef Bacik 	root->fs_info->trans_no_join = 1;
1576a4abeea4SJosef Bacik 	spin_unlock(&root->fs_info->trans_lock);
1577ed0ca140SJosef Bacik 	wait_event(cur_trans->writer_wait,
1578ed0ca140SJosef Bacik 		   atomic_read(&cur_trans->num_writers) == 1);
157915ee9bc7SJosef Bacik 
15807585717fSChris Mason 	/*
15817585717fSChris Mason 	 * the reloc mutex makes sure that we stop
15827585717fSChris Mason 	 * the balancing code from coming in and moving
15837585717fSChris Mason 	 * extents around in the middle of the commit
15847585717fSChris Mason 	 */
15857585717fSChris Mason 	mutex_lock(&root->fs_info->reloc_mutex);
15867585717fSChris Mason 
158742874b3dSMiao Xie 	/*
158842874b3dSMiao Xie 	 * We needn't worry about the delayed items because we will
158942874b3dSMiao Xie 	 * deal with them in create_pending_snapshot(), which is the
159042874b3dSMiao Xie 	 * core function of the snapshot creation.
159142874b3dSMiao Xie 	 */
159242874b3dSMiao Xie 	ret = create_pending_snapshots(trans, root->fs_info);
159349b25e05SJeff Mahoney 	if (ret) {
159449b25e05SJeff Mahoney 		mutex_unlock(&root->fs_info->reloc_mutex);
159549b25e05SJeff Mahoney 		goto cleanup_transaction;
159649b25e05SJeff Mahoney 	}
15973063d29fSChris Mason 
159842874b3dSMiao Xie 	/*
159942874b3dSMiao Xie 	 * We insert the dir indexes of the snapshots and update the inode
160042874b3dSMiao Xie 	 * of the snapshots' parents after the snapshot creation, so there
160142874b3dSMiao Xie 	 * are some delayed items which are not dealt with. Now deal with
160242874b3dSMiao Xie 	 * them.
160342874b3dSMiao Xie 	 *
160442874b3dSMiao Xie 	 * We needn't worry that this operation will corrupt the snapshots,
160542874b3dSMiao Xie 	 * because all the tree which are snapshoted will be forced to COW
160642874b3dSMiao Xie 	 * the nodes and leaves.
160742874b3dSMiao Xie 	 */
160842874b3dSMiao Xie 	ret = btrfs_run_delayed_items(trans, root);
160949b25e05SJeff Mahoney 	if (ret) {
161049b25e05SJeff Mahoney 		mutex_unlock(&root->fs_info->reloc_mutex);
161149b25e05SJeff Mahoney 		goto cleanup_transaction;
161249b25e05SJeff Mahoney 	}
161316cdcec7SMiao Xie 
161456bec294SChris Mason 	ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
161549b25e05SJeff Mahoney 	if (ret) {
161649b25e05SJeff Mahoney 		mutex_unlock(&root->fs_info->reloc_mutex);
161749b25e05SJeff Mahoney 		goto cleanup_transaction;
161849b25e05SJeff Mahoney 	}
161956bec294SChris Mason 
1620e999376fSChris Mason 	/*
1621e999376fSChris Mason 	 * make sure none of the code above managed to slip in a
1622e999376fSChris Mason 	 * delayed item
1623e999376fSChris Mason 	 */
1624e999376fSChris Mason 	btrfs_assert_delayed_root_empty(root);
1625e999376fSChris Mason 
16262c90e5d6SChris Mason 	WARN_ON(cur_trans != trans->transaction);
1627dc17ff8fSChris Mason 
1628a2de733cSArne Jansen 	btrfs_scrub_pause(root);
1629e02119d5SChris Mason 	/* btrfs_commit_tree_roots is responsible for getting the
1630e02119d5SChris Mason 	 * various roots consistent with each other.  Every pointer
1631e02119d5SChris Mason 	 * in the tree of tree roots has to point to the most up to date
1632e02119d5SChris Mason 	 * root for every subvolume and other tree.  So, we have to keep
1633e02119d5SChris Mason 	 * the tree logging code from jumping in and changing any
1634e02119d5SChris Mason 	 * of the trees.
1635e02119d5SChris Mason 	 *
1636e02119d5SChris Mason 	 * At this point in the commit, there can't be any tree-log
1637e02119d5SChris Mason 	 * writers, but a little lower down we drop the trans mutex
1638e02119d5SChris Mason 	 * and let new people in.  By holding the tree_log_mutex
1639e02119d5SChris Mason 	 * from now until after the super is written, we avoid races
1640e02119d5SChris Mason 	 * with the tree-log code.
1641e02119d5SChris Mason 	 */
1642e02119d5SChris Mason 	mutex_lock(&root->fs_info->tree_log_mutex);
16431a40e23bSZheng Yan 
16445d4f98a2SYan Zheng 	ret = commit_fs_roots(trans, root);
164549b25e05SJeff Mahoney 	if (ret) {
164649b25e05SJeff Mahoney 		mutex_unlock(&root->fs_info->tree_log_mutex);
1647871383beSDavid Sterba 		mutex_unlock(&root->fs_info->reloc_mutex);
164849b25e05SJeff Mahoney 		goto cleanup_transaction;
164949b25e05SJeff Mahoney 	}
165054aa1f4dSChris Mason 
16515d4f98a2SYan Zheng 	/* commit_fs_roots gets rid of all the tree log roots, it is now
1652e02119d5SChris Mason 	 * safe to free the root of tree log roots
1653e02119d5SChris Mason 	 */
1654e02119d5SChris Mason 	btrfs_free_log_root_tree(trans, root->fs_info);
1655e02119d5SChris Mason 
16565d4f98a2SYan Zheng 	ret = commit_cowonly_roots(trans, root);
165749b25e05SJeff Mahoney 	if (ret) {
165849b25e05SJeff Mahoney 		mutex_unlock(&root->fs_info->tree_log_mutex);
1659871383beSDavid Sterba 		mutex_unlock(&root->fs_info->reloc_mutex);
166049b25e05SJeff Mahoney 		goto cleanup_transaction;
166149b25e05SJeff Mahoney 	}
166254aa1f4dSChris Mason 
166311833d66SYan Zheng 	btrfs_prepare_extent_commit(trans, root);
166411833d66SYan Zheng 
166578fae27eSChris Mason 	cur_trans = root->fs_info->running_transaction;
16665f39d397SChris Mason 
16675d4f98a2SYan Zheng 	btrfs_set_root_node(&root->fs_info->tree_root->root_item,
16685d4f98a2SYan Zheng 			    root->fs_info->tree_root->node);
1669817d52f8SJosef Bacik 	switch_commit_root(root->fs_info->tree_root);
16705d4f98a2SYan Zheng 
16715d4f98a2SYan Zheng 	btrfs_set_root_node(&root->fs_info->chunk_root->root_item,
16725d4f98a2SYan Zheng 			    root->fs_info->chunk_root->node);
1673817d52f8SJosef Bacik 	switch_commit_root(root->fs_info->chunk_root);
16745d4f98a2SYan Zheng 
1675edf39272SJan Schmidt 	assert_qgroups_uptodate(trans);
16765d4f98a2SYan Zheng 	update_super_roots(root);
1677e02119d5SChris Mason 
1678e02119d5SChris Mason 	if (!root->fs_info->log_root_recovering) {
16796c41761fSDavid Sterba 		btrfs_set_super_log_root(root->fs_info->super_copy, 0);
16806c41761fSDavid Sterba 		btrfs_set_super_log_root_level(root->fs_info->super_copy, 0);
1681e02119d5SChris Mason 	}
1682e02119d5SChris Mason 
16836c41761fSDavid Sterba 	memcpy(root->fs_info->super_for_commit, root->fs_info->super_copy,
16846c41761fSDavid Sterba 	       sizeof(*root->fs_info->super_copy));
1685ccd467d6SChris Mason 
1686f9295749SChris Mason 	trans->transaction->blocked = 0;
1687a4abeea4SJosef Bacik 	spin_lock(&root->fs_info->trans_lock);
1688a4abeea4SJosef Bacik 	root->fs_info->running_transaction = NULL;
1689a4abeea4SJosef Bacik 	root->fs_info->trans_no_join = 0;
1690a4abeea4SJosef Bacik 	spin_unlock(&root->fs_info->trans_lock);
16917585717fSChris Mason 	mutex_unlock(&root->fs_info->reloc_mutex);
1692b7ec40d7SChris Mason 
1693f9295749SChris Mason 	wake_up(&root->fs_info->transaction_wait);
1694e6dcd2dcSChris Mason 
169579154b1bSChris Mason 	ret = btrfs_write_and_wait_transaction(trans, root);
169649b25e05SJeff Mahoney 	if (ret) {
169749b25e05SJeff Mahoney 		btrfs_error(root->fs_info, ret,
169849b25e05SJeff Mahoney 			    "Error while writing out transaction.");
169949b25e05SJeff Mahoney 		mutex_unlock(&root->fs_info->tree_log_mutex);
170049b25e05SJeff Mahoney 		goto cleanup_transaction;
170149b25e05SJeff Mahoney 	}
170249b25e05SJeff Mahoney 
170349b25e05SJeff Mahoney 	ret = write_ctree_super(trans, root, 0);
170449b25e05SJeff Mahoney 	if (ret) {
170549b25e05SJeff Mahoney 		mutex_unlock(&root->fs_info->tree_log_mutex);
170649b25e05SJeff Mahoney 		goto cleanup_transaction;
170749b25e05SJeff Mahoney 	}
17084313b399SChris Mason 
1709e02119d5SChris Mason 	/*
1710e02119d5SChris Mason 	 * the super is written, we can safely allow the tree-loggers
1711e02119d5SChris Mason 	 * to go about their business
1712e02119d5SChris Mason 	 */
1713e02119d5SChris Mason 	mutex_unlock(&root->fs_info->tree_log_mutex);
1714e02119d5SChris Mason 
171511833d66SYan Zheng 	btrfs_finish_extent_commit(trans, root);
17164313b399SChris Mason 
17172c90e5d6SChris Mason 	cur_trans->commit_done = 1;
1718b7ec40d7SChris Mason 
171915ee9bc7SJosef Bacik 	root->fs_info->last_trans_committed = cur_trans->transid;
1720817d52f8SJosef Bacik 
17212c90e5d6SChris Mason 	wake_up(&cur_trans->commit_wait);
17223de4586cSChris Mason 
1723a4abeea4SJosef Bacik 	spin_lock(&root->fs_info->trans_lock);
172413c5a93eSJosef Bacik 	list_del_init(&cur_trans->list);
1725a4abeea4SJosef Bacik 	spin_unlock(&root->fs_info->trans_lock);
1726a4abeea4SJosef Bacik 
172779154b1bSChris Mason 	put_transaction(cur_trans);
172878fae27eSChris Mason 	put_transaction(cur_trans);
172958176a96SJosef Bacik 
1730354aa0fbSMiao Xie 	if (trans->type < TRANS_JOIN_NOLOCK)
1731b2b5ef5cSJan Kara 		sb_end_intwrite(root->fs_info->sb);
1732b2b5ef5cSJan Kara 
17331abe9b8aSliubo 	trace_btrfs_transaction_commit(root);
17341abe9b8aSliubo 
1735a2de733cSArne Jansen 	btrfs_scrub_continue(root);
1736a2de733cSArne Jansen 
17379ed74f2dSJosef Bacik 	if (current->journal_info == trans)
17389ed74f2dSJosef Bacik 		current->journal_info = NULL;
17399ed74f2dSJosef Bacik 
17402c90e5d6SChris Mason 	kmem_cache_free(btrfs_trans_handle_cachep, trans);
174124bbcf04SYan, Zheng 
174224bbcf04SYan, Zheng 	if (current != root->fs_info->transaction_kthread)
174324bbcf04SYan, Zheng 		btrfs_run_delayed_iputs(root);
174424bbcf04SYan, Zheng 
174579154b1bSChris Mason 	return ret;
174649b25e05SJeff Mahoney 
174749b25e05SJeff Mahoney cleanup_transaction:
17480e721106SJosef Bacik 	btrfs_trans_release_metadata(trans, root);
17490e721106SJosef Bacik 	trans->block_rsv = NULL;
175049b25e05SJeff Mahoney 	btrfs_printk(root->fs_info, "Skipping commit of aborted transaction.\n");
175149b25e05SJeff Mahoney //	WARN_ON(1);
175249b25e05SJeff Mahoney 	if (current->journal_info == trans)
175349b25e05SJeff Mahoney 		current->journal_info = NULL;
17547b8b92afSJosef Bacik 	cleanup_transaction(trans, root, ret);
175549b25e05SJeff Mahoney 
175649b25e05SJeff Mahoney 	return ret;
175779154b1bSChris Mason }
175879154b1bSChris Mason 
1759d352ac68SChris Mason /*
1760d352ac68SChris Mason  * interface function to delete all the snapshots we have scheduled for deletion
1761d352ac68SChris Mason  */
1762e9d0b13bSChris Mason int btrfs_clean_old_snapshots(struct btrfs_root *root)
1763e9d0b13bSChris Mason {
17645d4f98a2SYan Zheng 	LIST_HEAD(list);
17655d4f98a2SYan Zheng 	struct btrfs_fs_info *fs_info = root->fs_info;
1766e9d0b13bSChris Mason 
1767a4abeea4SJosef Bacik 	spin_lock(&fs_info->trans_lock);
17685d4f98a2SYan Zheng 	list_splice_init(&fs_info->dead_roots, &list);
1769a4abeea4SJosef Bacik 	spin_unlock(&fs_info->trans_lock);
17705d4f98a2SYan Zheng 
17715d4f98a2SYan Zheng 	while (!list_empty(&list)) {
17722c536799SJeff Mahoney 		int ret;
17732c536799SJeff Mahoney 
17745d4f98a2SYan Zheng 		root = list_entry(list.next, struct btrfs_root, root_list);
177576dda93cSYan, Zheng 		list_del(&root->root_list);
177676dda93cSYan, Zheng 
177716cdcec7SMiao Xie 		btrfs_kill_all_delayed_nodes(root);
177816cdcec7SMiao Xie 
177976dda93cSYan, Zheng 		if (btrfs_header_backref_rev(root->node) <
178076dda93cSYan, Zheng 		    BTRFS_MIXED_BACKREF_REV)
17812c536799SJeff Mahoney 			ret = btrfs_drop_snapshot(root, NULL, 0, 0);
178276dda93cSYan, Zheng 		else
17832c536799SJeff Mahoney 			ret =btrfs_drop_snapshot(root, NULL, 1, 0);
17842c536799SJeff Mahoney 		BUG_ON(ret < 0);
1785e9d0b13bSChris Mason 	}
1786e9d0b13bSChris Mason 	return 0;
1787e9d0b13bSChris Mason }
1788