xref: /openbmc/linux/fs/btrfs/transaction.c (revision 4b824906)
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 
53d352ac68SChris Mason /*
54d352ac68SChris Mason  * either allocate a new transaction or hop into the existing one
55d352ac68SChris Mason  */
56354aa0fbSMiao Xie static noinline int join_transaction(struct btrfs_root *root, int type)
5779154b1bSChris Mason {
5879154b1bSChris Mason 	struct btrfs_transaction *cur_trans;
5919ae4e81SJan Schmidt 	struct btrfs_fs_info *fs_info = root->fs_info;
60a4abeea4SJosef Bacik 
6119ae4e81SJan Schmidt 	spin_lock(&fs_info->trans_lock);
62d43317dcSChris Mason loop:
6349b25e05SJeff Mahoney 	/* The file system has been taken offline. No new transactions. */
6487533c47SMiao Xie 	if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) {
6519ae4e81SJan Schmidt 		spin_unlock(&fs_info->trans_lock);
6649b25e05SJeff Mahoney 		return -EROFS;
6749b25e05SJeff Mahoney 	}
6849b25e05SJeff Mahoney 
6919ae4e81SJan Schmidt 	if (fs_info->trans_no_join) {
70354aa0fbSMiao Xie 		/*
71354aa0fbSMiao Xie 		 * If we are JOIN_NOLOCK we're already committing a current
72354aa0fbSMiao Xie 		 * transaction, we just need a handle to deal with something
73354aa0fbSMiao Xie 		 * when committing the transaction, such as inode cache and
74354aa0fbSMiao Xie 		 * space cache. It is a special case.
75354aa0fbSMiao Xie 		 */
76354aa0fbSMiao Xie 		if (type != TRANS_JOIN_NOLOCK) {
7719ae4e81SJan Schmidt 			spin_unlock(&fs_info->trans_lock);
78a4abeea4SJosef Bacik 			return -EBUSY;
79a4abeea4SJosef Bacik 		}
80a4abeea4SJosef Bacik 	}
81a4abeea4SJosef Bacik 
8219ae4e81SJan Schmidt 	cur_trans = fs_info->running_transaction;
83a4abeea4SJosef Bacik 	if (cur_trans) {
84871383beSDavid Sterba 		if (cur_trans->aborted) {
8519ae4e81SJan Schmidt 			spin_unlock(&fs_info->trans_lock);
8649b25e05SJeff Mahoney 			return cur_trans->aborted;
87871383beSDavid Sterba 		}
88a4abeea4SJosef Bacik 		atomic_inc(&cur_trans->use_count);
89a4abeea4SJosef Bacik 		atomic_inc(&cur_trans->num_writers);
90a4abeea4SJosef Bacik 		cur_trans->num_joined++;
9119ae4e81SJan Schmidt 		spin_unlock(&fs_info->trans_lock);
92a4abeea4SJosef Bacik 		return 0;
93a4abeea4SJosef Bacik 	}
9419ae4e81SJan Schmidt 	spin_unlock(&fs_info->trans_lock);
95a4abeea4SJosef Bacik 
96354aa0fbSMiao Xie 	/*
97354aa0fbSMiao Xie 	 * If we are ATTACH, we just want to catch the current transaction,
98354aa0fbSMiao Xie 	 * and commit it. If there is no transaction, just return ENOENT.
99354aa0fbSMiao Xie 	 */
100354aa0fbSMiao Xie 	if (type == TRANS_ATTACH)
101354aa0fbSMiao Xie 		return -ENOENT;
102354aa0fbSMiao Xie 
103a4abeea4SJosef Bacik 	cur_trans = kmem_cache_alloc(btrfs_transaction_cachep, GFP_NOFS);
104db5b493aSTsutomu Itoh 	if (!cur_trans)
105db5b493aSTsutomu Itoh 		return -ENOMEM;
106d43317dcSChris Mason 
10719ae4e81SJan Schmidt 	spin_lock(&fs_info->trans_lock);
10819ae4e81SJan Schmidt 	if (fs_info->running_transaction) {
109d43317dcSChris Mason 		/*
110d43317dcSChris Mason 		 * someone started a transaction after we unlocked.  Make sure
111d43317dcSChris Mason 		 * to redo the trans_no_join checks above
112d43317dcSChris Mason 		 */
113a4abeea4SJosef Bacik 		kmem_cache_free(btrfs_transaction_cachep, cur_trans);
11419ae4e81SJan Schmidt 		cur_trans = fs_info->running_transaction;
115d43317dcSChris Mason 		goto loop;
11687533c47SMiao Xie 	} else if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) {
117e4b50e14SDan Carpenter 		spin_unlock(&fs_info->trans_lock);
1187b8b92afSJosef Bacik 		kmem_cache_free(btrfs_transaction_cachep, cur_trans);
1197b8b92afSJosef Bacik 		return -EROFS;
120a4abeea4SJosef Bacik 	}
121d43317dcSChris Mason 
12213c5a93eSJosef Bacik 	atomic_set(&cur_trans->num_writers, 1);
12315ee9bc7SJosef Bacik 	cur_trans->num_joined = 0;
12479154b1bSChris Mason 	init_waitqueue_head(&cur_trans->writer_wait);
12579154b1bSChris Mason 	init_waitqueue_head(&cur_trans->commit_wait);
12679154b1bSChris Mason 	cur_trans->in_commit = 0;
127f9295749SChris Mason 	cur_trans->blocked = 0;
128a4abeea4SJosef Bacik 	/*
129a4abeea4SJosef Bacik 	 * One for this trans handle, one so it will live on until we
130a4abeea4SJosef Bacik 	 * commit the transaction.
131a4abeea4SJosef Bacik 	 */
132a4abeea4SJosef Bacik 	atomic_set(&cur_trans->use_count, 2);
13379154b1bSChris Mason 	cur_trans->commit_done = 0;
13408607c1bSChris Mason 	cur_trans->start_time = get_seconds();
13556bec294SChris Mason 
1366bef4d31SEric Paris 	cur_trans->delayed_refs.root = RB_ROOT;
13756bec294SChris Mason 	cur_trans->delayed_refs.num_entries = 0;
138c3e69d58SChris Mason 	cur_trans->delayed_refs.num_heads_ready = 0;
139c3e69d58SChris Mason 	cur_trans->delayed_refs.num_heads = 0;
14056bec294SChris Mason 	cur_trans->delayed_refs.flushing = 0;
141c3e69d58SChris Mason 	cur_trans->delayed_refs.run_delayed_start = 0;
14220b297d6SJan Schmidt 
14320b297d6SJan Schmidt 	/*
14420b297d6SJan Schmidt 	 * although the tree mod log is per file system and not per transaction,
14520b297d6SJan Schmidt 	 * the log must never go across transaction boundaries.
14620b297d6SJan Schmidt 	 */
14720b297d6SJan Schmidt 	smp_mb();
14831b1a2bdSJulia Lawall 	if (!list_empty(&fs_info->tree_mod_seq_list))
14931b1a2bdSJulia Lawall 		WARN(1, KERN_ERR "btrfs: tree_mod_seq_list not empty when "
15020b297d6SJan Schmidt 			"creating a fresh transaction\n");
15131b1a2bdSJulia Lawall 	if (!RB_EMPTY_ROOT(&fs_info->tree_mod_log))
15231b1a2bdSJulia Lawall 		WARN(1, KERN_ERR "btrfs: tree_mod_log rb tree not empty when "
15320b297d6SJan Schmidt 			"creating a fresh transaction\n");
15420b297d6SJan Schmidt 	atomic_set(&fs_info->tree_mod_seq, 0);
15520b297d6SJan Schmidt 
156a4abeea4SJosef Bacik 	spin_lock_init(&cur_trans->commit_lock);
15756bec294SChris Mason 	spin_lock_init(&cur_trans->delayed_refs.lock);
15856bec294SChris Mason 
1593063d29fSChris Mason 	INIT_LIST_HEAD(&cur_trans->pending_snapshots);
160569e0f35SJosef Bacik 	INIT_LIST_HEAD(&cur_trans->ordered_operations);
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 
30587533c47SMiao Xie 	if (test_bit(BTRFS_FS_STATE_ERROR, &root->fs_info->fs_state))
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)
336843fcf35SMiao Xie 			goto reserve_fail;
337b5009945SJosef Bacik 	}
338a22285a6SYan, Zheng again:
339a22285a6SYan, Zheng 	h = kmem_cache_alloc(btrfs_trans_handle_cachep, GFP_NOFS);
340843fcf35SMiao Xie 	if (!h) {
341843fcf35SMiao Xie 		ret = -ENOMEM;
342843fcf35SMiao Xie 		goto alloc_fail;
343843fcf35SMiao Xie 	}
344a22285a6SYan, Zheng 
34598114659SJosef Bacik 	/*
34698114659SJosef Bacik 	 * If we are JOIN_NOLOCK we're already committing a transaction and
34798114659SJosef Bacik 	 * waiting on this guy, so we don't need to do the sb_start_intwrite
34898114659SJosef Bacik 	 * because we're already holding a ref.  We need this because we could
34998114659SJosef Bacik 	 * have raced in and did an fsync() on a file which can kick a commit
35098114659SJosef Bacik 	 * and then we deadlock with somebody doing a freeze.
351354aa0fbSMiao Xie 	 *
352354aa0fbSMiao Xie 	 * If we are ATTACH, it means we just want to catch the current
353354aa0fbSMiao Xie 	 * transaction and commit it, so we needn't do sb_start_intwrite().
35498114659SJosef Bacik 	 */
355354aa0fbSMiao Xie 	if (type < TRANS_JOIN_NOLOCK)
356b2b5ef5cSJan Kara 		sb_start_intwrite(root->fs_info->sb);
357b2b5ef5cSJan Kara 
358a22285a6SYan, Zheng 	if (may_wait_transaction(root, type))
35937d1aeeeSChris Mason 		wait_current_trans(root);
360a22285a6SYan, Zheng 
361a4abeea4SJosef Bacik 	do {
362354aa0fbSMiao Xie 		ret = join_transaction(root, type);
363a4abeea4SJosef Bacik 		if (ret == -EBUSY)
364a4abeea4SJosef Bacik 			wait_current_trans(root);
365a4abeea4SJosef Bacik 	} while (ret == -EBUSY);
366a4abeea4SJosef Bacik 
367db5b493aSTsutomu Itoh 	if (ret < 0) {
368354aa0fbSMiao Xie 		/* We must get the transaction if we are JOIN_NOLOCK. */
369354aa0fbSMiao Xie 		BUG_ON(type == TRANS_JOIN_NOLOCK);
370843fcf35SMiao Xie 		goto join_fail;
371db5b493aSTsutomu Itoh 	}
3720f7d52f4SChris Mason 
373a22285a6SYan, Zheng 	cur_trans = root->fs_info->running_transaction;
374a22285a6SYan, Zheng 
375a22285a6SYan, Zheng 	h->transid = cur_trans->transid;
376a22285a6SYan, Zheng 	h->transaction = cur_trans;
37779154b1bSChris Mason 	h->blocks_used = 0;
378a22285a6SYan, Zheng 	h->bytes_reserved = 0;
379d13603efSArne Jansen 	h->root = root;
38056bec294SChris Mason 	h->delayed_ref_updates = 0;
3812a1eb461SJosef Bacik 	h->use_count = 1;
3820e721106SJosef Bacik 	h->adding_csums = 0;
383f0486c68SYan, Zheng 	h->block_rsv = NULL;
3842a1eb461SJosef Bacik 	h->orig_rsv = NULL;
38549b25e05SJeff Mahoney 	h->aborted = 0;
3864b824906SMiao Xie 	h->qgroup_reserved = 0;
387bed92eaeSArne Jansen 	h->delayed_ref_elem.seq = 0;
388a698d075SMiao Xie 	h->type = type;
389c6b305a8SJosef Bacik 	h->allocating_chunk = false;
390bed92eaeSArne Jansen 	INIT_LIST_HEAD(&h->qgroup_ref_list);
391ea658badSJosef Bacik 	INIT_LIST_HEAD(&h->new_bgs);
392b7ec40d7SChris Mason 
393a22285a6SYan, Zheng 	smp_mb();
394a22285a6SYan, Zheng 	if (cur_trans->blocked && may_wait_transaction(root, type)) {
395a22285a6SYan, Zheng 		btrfs_commit_transaction(h, root);
396a22285a6SYan, Zheng 		goto again;
397a22285a6SYan, Zheng 	}
3989ed74f2dSJosef Bacik 
399b5009945SJosef Bacik 	if (num_bytes) {
4008c2a3ca2SJosef Bacik 		trace_btrfs_space_reservation(root->fs_info, "transaction",
4012bcc0328SLiu Bo 					      h->transid, num_bytes, 1);
402b5009945SJosef Bacik 		h->block_rsv = &root->fs_info->trans_block_rsv;
403b5009945SJosef Bacik 		h->bytes_reserved = num_bytes;
404a22285a6SYan, Zheng 	}
4054b824906SMiao Xie 	h->qgroup_reserved = qgroup_reserved;
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;
413843fcf35SMiao Xie 
414843fcf35SMiao Xie join_fail:
415843fcf35SMiao Xie 	if (type < TRANS_JOIN_NOLOCK)
416843fcf35SMiao Xie 		sb_end_intwrite(root->fs_info->sb);
417843fcf35SMiao Xie 	kmem_cache_free(btrfs_trans_handle_cachep, h);
418843fcf35SMiao Xie alloc_fail:
419843fcf35SMiao Xie 	if (num_bytes)
420843fcf35SMiao Xie 		btrfs_block_rsv_release(root, &root->fs_info->trans_block_rsv,
421843fcf35SMiao Xie 					num_bytes);
422843fcf35SMiao Xie reserve_fail:
423843fcf35SMiao Xie 	if (qgroup_reserved)
424843fcf35SMiao Xie 		btrfs_qgroup_free(root, qgroup_reserved);
425843fcf35SMiao Xie 	return ERR_PTR(ret);
42679154b1bSChris Mason }
42779154b1bSChris Mason 
428f9295749SChris Mason struct btrfs_trans_handle *btrfs_start_transaction(struct btrfs_root *root,
429a22285a6SYan, Zheng 						   int num_items)
430f9295749SChris Mason {
43108e007d2SMiao Xie 	return start_transaction(root, num_items, TRANS_START,
43208e007d2SMiao Xie 				 BTRFS_RESERVE_FLUSH_ALL);
433f9295749SChris Mason }
4348407aa46SMiao Xie 
43508e007d2SMiao Xie struct btrfs_trans_handle *btrfs_start_transaction_lflush(
4368407aa46SMiao Xie 					struct btrfs_root *root, int num_items)
4378407aa46SMiao Xie {
43808e007d2SMiao Xie 	return start_transaction(root, num_items, TRANS_START,
43908e007d2SMiao Xie 				 BTRFS_RESERVE_FLUSH_LIMIT);
4408407aa46SMiao Xie }
4418407aa46SMiao Xie 
4427a7eaa40SJosef Bacik struct btrfs_trans_handle *btrfs_join_transaction(struct btrfs_root *root)
443f9295749SChris Mason {
4448407aa46SMiao Xie 	return start_transaction(root, 0, TRANS_JOIN, 0);
445f9295749SChris Mason }
446f9295749SChris Mason 
4477a7eaa40SJosef Bacik struct btrfs_trans_handle *btrfs_join_transaction_nolock(struct btrfs_root *root)
4480af3d00bSJosef Bacik {
4498407aa46SMiao Xie 	return start_transaction(root, 0, TRANS_JOIN_NOLOCK, 0);
4500af3d00bSJosef Bacik }
4510af3d00bSJosef Bacik 
4527a7eaa40SJosef Bacik struct btrfs_trans_handle *btrfs_start_ioctl_transaction(struct btrfs_root *root)
4539ca9ee09SSage Weil {
4548407aa46SMiao Xie 	return start_transaction(root, 0, TRANS_USERSPACE, 0);
4559ca9ee09SSage Weil }
4569ca9ee09SSage Weil 
457354aa0fbSMiao Xie struct btrfs_trans_handle *btrfs_attach_transaction(struct btrfs_root *root)
45860376ce4SJosef Bacik {
459354aa0fbSMiao Xie 	return start_transaction(root, 0, TRANS_ATTACH, 0);
46060376ce4SJosef Bacik }
46160376ce4SJosef Bacik 
462d352ac68SChris Mason /* wait for a transaction commit to be fully complete */
463b9c8300cSLi Zefan static noinline void wait_for_commit(struct btrfs_root *root,
46489ce8a63SChris Mason 				    struct btrfs_transaction *commit)
46589ce8a63SChris Mason {
46672d63ed6SLi Zefan 	wait_event(commit->commit_wait, commit->commit_done);
46789ce8a63SChris Mason }
46889ce8a63SChris Mason 
46946204592SSage Weil int btrfs_wait_for_commit(struct btrfs_root *root, u64 transid)
47046204592SSage Weil {
47146204592SSage Weil 	struct btrfs_transaction *cur_trans = NULL, *t;
4728cd2807fSMiao Xie 	int ret = 0;
47346204592SSage Weil 
47446204592SSage Weil 	if (transid) {
47546204592SSage Weil 		if (transid <= root->fs_info->last_trans_committed)
476a4abeea4SJosef Bacik 			goto out;
47746204592SSage Weil 
4788cd2807fSMiao Xie 		ret = -EINVAL;
47946204592SSage Weil 		/* find specified transaction */
480a4abeea4SJosef Bacik 		spin_lock(&root->fs_info->trans_lock);
48146204592SSage Weil 		list_for_each_entry(t, &root->fs_info->trans_list, list) {
48246204592SSage Weil 			if (t->transid == transid) {
48346204592SSage Weil 				cur_trans = t;
484a4abeea4SJosef Bacik 				atomic_inc(&cur_trans->use_count);
4858cd2807fSMiao Xie 				ret = 0;
48646204592SSage Weil 				break;
48746204592SSage Weil 			}
4888cd2807fSMiao Xie 			if (t->transid > transid) {
4898cd2807fSMiao Xie 				ret = 0;
49046204592SSage Weil 				break;
49146204592SSage Weil 			}
4928cd2807fSMiao Xie 		}
493a4abeea4SJosef Bacik 		spin_unlock(&root->fs_info->trans_lock);
4948cd2807fSMiao Xie 		/* The specified transaction doesn't exist */
49546204592SSage Weil 		if (!cur_trans)
4968cd2807fSMiao Xie 			goto out;
49746204592SSage Weil 	} else {
49846204592SSage Weil 		/* find newest transaction that is committing | committed */
499a4abeea4SJosef Bacik 		spin_lock(&root->fs_info->trans_lock);
50046204592SSage Weil 		list_for_each_entry_reverse(t, &root->fs_info->trans_list,
50146204592SSage Weil 					    list) {
50246204592SSage Weil 			if (t->in_commit) {
50346204592SSage Weil 				if (t->commit_done)
5043473f3c0SJosef Bacik 					break;
50546204592SSage Weil 				cur_trans = t;
506a4abeea4SJosef Bacik 				atomic_inc(&cur_trans->use_count);
50746204592SSage Weil 				break;
50846204592SSage Weil 			}
50946204592SSage Weil 		}
510a4abeea4SJosef Bacik 		spin_unlock(&root->fs_info->trans_lock);
51146204592SSage Weil 		if (!cur_trans)
512a4abeea4SJosef Bacik 			goto out;  /* nothing committing|committed */
51346204592SSage Weil 	}
51446204592SSage Weil 
51546204592SSage Weil 	wait_for_commit(root, cur_trans);
51646204592SSage Weil 	put_transaction(cur_trans);
517a4abeea4SJosef Bacik out:
51846204592SSage Weil 	return ret;
51946204592SSage Weil }
52046204592SSage Weil 
52137d1aeeeSChris Mason void btrfs_throttle(struct btrfs_root *root)
52237d1aeeeSChris Mason {
523a4abeea4SJosef Bacik 	if (!atomic_read(&root->fs_info->open_ioctl_trans))
52437d1aeeeSChris Mason 		wait_current_trans(root);
52537d1aeeeSChris Mason }
52637d1aeeeSChris Mason 
5278929ecfaSYan, Zheng static int should_end_transaction(struct btrfs_trans_handle *trans,
5288929ecfaSYan, Zheng 				  struct btrfs_root *root)
5298929ecfaSYan, Zheng {
5308929ecfaSYan, Zheng 	int ret;
53136ba022aSJosef Bacik 
53236ba022aSJosef Bacik 	ret = btrfs_block_rsv_check(root, &root->fs_info->global_block_rsv, 5);
5338929ecfaSYan, Zheng 	return ret ? 1 : 0;
5348929ecfaSYan, Zheng }
5358929ecfaSYan, Zheng 
5368929ecfaSYan, Zheng int btrfs_should_end_transaction(struct btrfs_trans_handle *trans,
5378929ecfaSYan, Zheng 				 struct btrfs_root *root)
5388929ecfaSYan, Zheng {
5398929ecfaSYan, Zheng 	struct btrfs_transaction *cur_trans = trans->transaction;
5408929ecfaSYan, Zheng 	int updates;
54149b25e05SJeff Mahoney 	int err;
5428929ecfaSYan, Zheng 
543a4abeea4SJosef Bacik 	smp_mb();
5448929ecfaSYan, Zheng 	if (cur_trans->blocked || cur_trans->delayed_refs.flushing)
5458929ecfaSYan, Zheng 		return 1;
5468929ecfaSYan, Zheng 
5478929ecfaSYan, Zheng 	updates = trans->delayed_ref_updates;
5488929ecfaSYan, Zheng 	trans->delayed_ref_updates = 0;
54949b25e05SJeff Mahoney 	if (updates) {
55049b25e05SJeff Mahoney 		err = btrfs_run_delayed_refs(trans, root, updates);
55149b25e05SJeff Mahoney 		if (err) /* Error code will also eval true */
55249b25e05SJeff Mahoney 			return err;
55349b25e05SJeff Mahoney 	}
5548929ecfaSYan, Zheng 
5558929ecfaSYan, Zheng 	return should_end_transaction(trans, root);
5568929ecfaSYan, Zheng }
5578929ecfaSYan, Zheng 
55889ce8a63SChris Mason static int __btrfs_end_transaction(struct btrfs_trans_handle *trans,
559a698d075SMiao Xie 			  struct btrfs_root *root, int throttle)
56079154b1bSChris Mason {
5618929ecfaSYan, Zheng 	struct btrfs_transaction *cur_trans = trans->transaction;
562ab78c84dSChris Mason 	struct btrfs_fs_info *info = root->fs_info;
563c3e69d58SChris Mason 	int count = 0;
564a698d075SMiao Xie 	int lock = (trans->type != TRANS_JOIN_NOLOCK);
5654edc2ca3SDave Jones 	int err = 0;
566d6e4a428SChris Mason 
5672a1eb461SJosef Bacik 	if (--trans->use_count) {
5682a1eb461SJosef Bacik 		trans->block_rsv = trans->orig_rsv;
5692a1eb461SJosef Bacik 		return 0;
5702a1eb461SJosef Bacik 	}
5712a1eb461SJosef Bacik 
572edf39272SJan Schmidt 	/*
573edf39272SJan Schmidt 	 * do the qgroup accounting as early as possible
574edf39272SJan Schmidt 	 */
575edf39272SJan Schmidt 	err = btrfs_delayed_refs_qgroup_accounting(trans, info);
576edf39272SJan Schmidt 
577b24e03dbSJosef Bacik 	btrfs_trans_release_metadata(trans, root);
5784c13d758SJosef Bacik 	trans->block_rsv = NULL;
579d13603efSArne Jansen 	/*
580d13603efSArne Jansen 	 * the same root has to be passed to start_transaction and
581d13603efSArne Jansen 	 * end_transaction. Subvolume quota depends on this.
582d13603efSArne Jansen 	 */
583d13603efSArne Jansen 	WARN_ON(trans->root != root);
584c5567237SArne Jansen 
585c5567237SArne Jansen 	if (trans->qgroup_reserved) {
586c5567237SArne Jansen 		btrfs_qgroup_free(root, trans->qgroup_reserved);
587c5567237SArne Jansen 		trans->qgroup_reserved = 0;
588c5567237SArne Jansen 	}
589c5567237SArne Jansen 
590ea658badSJosef Bacik 	if (!list_empty(&trans->new_bgs))
591ea658badSJosef Bacik 		btrfs_create_pending_block_groups(trans, root);
592ea658badSJosef Bacik 
593203bf287SChris Mason 	while (count < 2) {
594c3e69d58SChris Mason 		unsigned long cur = trans->delayed_ref_updates;
595c3e69d58SChris Mason 		trans->delayed_ref_updates = 0;
596c3e69d58SChris Mason 		if (cur &&
597c3e69d58SChris Mason 		    trans->transaction->delayed_refs.num_heads_ready > 64) {
598c3e69d58SChris Mason 			trans->delayed_ref_updates = 0;
599c3e69d58SChris Mason 			btrfs_run_delayed_refs(trans, root, cur);
600c3e69d58SChris Mason 		} else {
601c3e69d58SChris Mason 			break;
602c3e69d58SChris Mason 		}
603c3e69d58SChris Mason 		count++;
60456bec294SChris Mason 	}
6050e721106SJosef Bacik 	btrfs_trans_release_metadata(trans, root);
6060e721106SJosef Bacik 	trans->block_rsv = NULL;
60756bec294SChris Mason 
608ea658badSJosef Bacik 	if (!list_empty(&trans->new_bgs))
609ea658badSJosef Bacik 		btrfs_create_pending_block_groups(trans, root);
610ea658badSJosef Bacik 
611a4abeea4SJosef Bacik 	if (lock && !atomic_read(&root->fs_info->open_ioctl_trans) &&
612a4abeea4SJosef Bacik 	    should_end_transaction(trans, root)) {
6138929ecfaSYan, Zheng 		trans->transaction->blocked = 1;
614a4abeea4SJosef Bacik 		smp_wmb();
615a4abeea4SJosef Bacik 	}
6168929ecfaSYan, Zheng 
6170af3d00bSJosef Bacik 	if (lock && cur_trans->blocked && !cur_trans->in_commit) {
61881317fdeSJosef Bacik 		if (throttle) {
61981317fdeSJosef Bacik 			/*
62081317fdeSJosef Bacik 			 * We may race with somebody else here so end up having
62181317fdeSJosef Bacik 			 * to call end_transaction on ourselves again, so inc
62281317fdeSJosef Bacik 			 * our use_count.
62381317fdeSJosef Bacik 			 */
62481317fdeSJosef Bacik 			trans->use_count++;
6258929ecfaSYan, Zheng 			return btrfs_commit_transaction(trans, root);
62681317fdeSJosef Bacik 		} else {
6278929ecfaSYan, Zheng 			wake_up_process(info->transaction_kthread);
6288929ecfaSYan, Zheng 		}
62981317fdeSJosef Bacik 	}
6308929ecfaSYan, Zheng 
631354aa0fbSMiao Xie 	if (trans->type < TRANS_JOIN_NOLOCK)
6326df7881aSJosef Bacik 		sb_end_intwrite(root->fs_info->sb);
6336df7881aSJosef Bacik 
6348929ecfaSYan, Zheng 	WARN_ON(cur_trans != info->running_transaction);
63513c5a93eSJosef Bacik 	WARN_ON(atomic_read(&cur_trans->num_writers) < 1);
63613c5a93eSJosef Bacik 	atomic_dec(&cur_trans->num_writers);
63789ce8a63SChris Mason 
63899d16cbcSSage Weil 	smp_mb();
63979154b1bSChris Mason 	if (waitqueue_active(&cur_trans->writer_wait))
64079154b1bSChris Mason 		wake_up(&cur_trans->writer_wait);
64179154b1bSChris Mason 	put_transaction(cur_trans);
6429ed74f2dSJosef Bacik 
6439ed74f2dSJosef Bacik 	if (current->journal_info == trans)
6449ed74f2dSJosef Bacik 		current->journal_info = NULL;
645ab78c84dSChris Mason 
64624bbcf04SYan, Zheng 	if (throttle)
64724bbcf04SYan, Zheng 		btrfs_run_delayed_iputs(root);
64824bbcf04SYan, Zheng 
64949b25e05SJeff Mahoney 	if (trans->aborted ||
65087533c47SMiao Xie 	    test_bit(BTRFS_FS_STATE_ERROR, &root->fs_info->fs_state))
6514edc2ca3SDave Jones 		err = -EIO;
652edf39272SJan Schmidt 	assert_qgroups_uptodate(trans);
65349b25e05SJeff Mahoney 
6544edc2ca3SDave Jones 	kmem_cache_free(btrfs_trans_handle_cachep, trans);
6554edc2ca3SDave Jones 	return err;
65679154b1bSChris Mason }
65779154b1bSChris Mason 
65889ce8a63SChris Mason int btrfs_end_transaction(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, 0);
66416cdcec7SMiao Xie 	if (ret)
66516cdcec7SMiao Xie 		return ret;
66616cdcec7SMiao Xie 	return 0;
66789ce8a63SChris Mason }
66889ce8a63SChris Mason 
66989ce8a63SChris Mason int btrfs_end_transaction_throttle(struct btrfs_trans_handle *trans,
67089ce8a63SChris Mason 				   struct btrfs_root *root)
67189ce8a63SChris Mason {
67216cdcec7SMiao Xie 	int ret;
67316cdcec7SMiao Xie 
674a698d075SMiao Xie 	ret = __btrfs_end_transaction(trans, root, 1);
67516cdcec7SMiao Xie 	if (ret)
67616cdcec7SMiao Xie 		return ret;
67716cdcec7SMiao Xie 	return 0;
67816cdcec7SMiao Xie }
67916cdcec7SMiao Xie 
68016cdcec7SMiao Xie int btrfs_end_transaction_dmeta(struct btrfs_trans_handle *trans,
68116cdcec7SMiao Xie 				struct btrfs_root *root)
68216cdcec7SMiao Xie {
683a698d075SMiao Xie 	return __btrfs_end_transaction(trans, root, 1);
68489ce8a63SChris Mason }
68589ce8a63SChris Mason 
686d352ac68SChris Mason /*
687d352ac68SChris Mason  * when btree blocks are allocated, they have some corresponding bits set for
688d352ac68SChris Mason  * them in one of two extent_io trees.  This is used to make sure all of
689690587d1SChris Mason  * those extents are sent to disk but does not wait on them
690d352ac68SChris Mason  */
691690587d1SChris Mason int btrfs_write_marked_extents(struct btrfs_root *root,
6928cef4e16SYan, Zheng 			       struct extent_io_tree *dirty_pages, int mark)
69379154b1bSChris Mason {
694777e6bd7SChris Mason 	int err = 0;
6957c4452b9SChris Mason 	int werr = 0;
6961728366eSJosef Bacik 	struct address_space *mapping = root->fs_info->btree_inode->i_mapping;
697e6138876SJosef Bacik 	struct extent_state *cached_state = NULL;
698777e6bd7SChris Mason 	u64 start = 0;
6995f39d397SChris Mason 	u64 end;
7007c4452b9SChris Mason 
7011728366eSJosef Bacik 	while (!find_first_extent_bit(dirty_pages, start, &start, &end,
702e6138876SJosef Bacik 				      mark, &cached_state)) {
703e6138876SJosef Bacik 		convert_extent_bit(dirty_pages, start, end, EXTENT_NEED_WAIT,
704e6138876SJosef Bacik 				   mark, &cached_state, GFP_NOFS);
705e6138876SJosef Bacik 		cached_state = NULL;
7061728366eSJosef Bacik 		err = filemap_fdatawrite_range(mapping, start, end);
7077c4452b9SChris Mason 		if (err)
7087c4452b9SChris Mason 			werr = err;
7091728366eSJosef Bacik 		cond_resched();
7101728366eSJosef Bacik 		start = end + 1;
7117c4452b9SChris Mason 	}
712690587d1SChris Mason 	if (err)
713690587d1SChris Mason 		werr = err;
714690587d1SChris Mason 	return werr;
715690587d1SChris Mason }
716690587d1SChris Mason 
717690587d1SChris Mason /*
718690587d1SChris Mason  * when btree blocks are allocated, they have some corresponding bits set for
719690587d1SChris Mason  * them in one of two extent_io trees.  This is used to make sure all of
720690587d1SChris Mason  * those extents are on disk for transaction or log commit.  We wait
721690587d1SChris Mason  * on all the pages and clear them from the dirty pages state tree
722690587d1SChris Mason  */
723690587d1SChris Mason int btrfs_wait_marked_extents(struct btrfs_root *root,
7248cef4e16SYan, Zheng 			      struct extent_io_tree *dirty_pages, int mark)
725690587d1SChris Mason {
726690587d1SChris Mason 	int err = 0;
727690587d1SChris Mason 	int werr = 0;
7281728366eSJosef Bacik 	struct address_space *mapping = root->fs_info->btree_inode->i_mapping;
729e6138876SJosef Bacik 	struct extent_state *cached_state = NULL;
730690587d1SChris Mason 	u64 start = 0;
731690587d1SChris Mason 	u64 end;
732690587d1SChris Mason 
7331728366eSJosef Bacik 	while (!find_first_extent_bit(dirty_pages, start, &start, &end,
734e6138876SJosef Bacik 				      EXTENT_NEED_WAIT, &cached_state)) {
735e6138876SJosef Bacik 		clear_extent_bit(dirty_pages, start, end, EXTENT_NEED_WAIT,
736e6138876SJosef Bacik 				 0, 0, &cached_state, GFP_NOFS);
7371728366eSJosef Bacik 		err = filemap_fdatawait_range(mapping, start, end);
738777e6bd7SChris Mason 		if (err)
739777e6bd7SChris Mason 			werr = err;
740777e6bd7SChris Mason 		cond_resched();
7411728366eSJosef Bacik 		start = end + 1;
742777e6bd7SChris Mason 	}
7437c4452b9SChris Mason 	if (err)
7447c4452b9SChris Mason 		werr = err;
7457c4452b9SChris Mason 	return werr;
74679154b1bSChris Mason }
74779154b1bSChris Mason 
748690587d1SChris Mason /*
749690587d1SChris Mason  * when btree blocks are allocated, they have some corresponding bits set for
750690587d1SChris Mason  * them in one of two extent_io trees.  This is used to make sure all of
751690587d1SChris Mason  * those extents are on disk for transaction or log commit
752690587d1SChris Mason  */
753690587d1SChris Mason int btrfs_write_and_wait_marked_extents(struct btrfs_root *root,
7548cef4e16SYan, Zheng 				struct extent_io_tree *dirty_pages, int mark)
755690587d1SChris Mason {
756690587d1SChris Mason 	int ret;
757690587d1SChris Mason 	int ret2;
758690587d1SChris Mason 
7598cef4e16SYan, Zheng 	ret = btrfs_write_marked_extents(root, dirty_pages, mark);
7608cef4e16SYan, Zheng 	ret2 = btrfs_wait_marked_extents(root, dirty_pages, mark);
761bf0da8c1SChris Mason 
762bf0da8c1SChris Mason 	if (ret)
763bf0da8c1SChris Mason 		return ret;
764bf0da8c1SChris Mason 	if (ret2)
765bf0da8c1SChris Mason 		return ret2;
766bf0da8c1SChris Mason 	return 0;
767690587d1SChris Mason }
768690587d1SChris Mason 
769d0c803c4SChris Mason int btrfs_write_and_wait_transaction(struct btrfs_trans_handle *trans,
770d0c803c4SChris Mason 				     struct btrfs_root *root)
771d0c803c4SChris Mason {
772d0c803c4SChris Mason 	if (!trans || !trans->transaction) {
773d0c803c4SChris Mason 		struct inode *btree_inode;
774d0c803c4SChris Mason 		btree_inode = root->fs_info->btree_inode;
775d0c803c4SChris Mason 		return filemap_write_and_wait(btree_inode->i_mapping);
776d0c803c4SChris Mason 	}
777d0c803c4SChris Mason 	return btrfs_write_and_wait_marked_extents(root,
7788cef4e16SYan, Zheng 					   &trans->transaction->dirty_pages,
7798cef4e16SYan, Zheng 					   EXTENT_DIRTY);
780d0c803c4SChris Mason }
781d0c803c4SChris Mason 
782d352ac68SChris Mason /*
783d352ac68SChris Mason  * this is used to update the root pointer in the tree of tree roots.
784d352ac68SChris Mason  *
785d352ac68SChris Mason  * But, in the case of the extent allocation tree, updating the root
786d352ac68SChris Mason  * pointer may allocate blocks which may change the root of the extent
787d352ac68SChris Mason  * allocation tree.
788d352ac68SChris Mason  *
789d352ac68SChris Mason  * So, this loops and repeats and makes sure the cowonly root didn't
790d352ac68SChris Mason  * change while the root pointer was being updated in the metadata.
791d352ac68SChris Mason  */
7920b86a832SChris Mason static int update_cowonly_root(struct btrfs_trans_handle *trans,
79379154b1bSChris Mason 			       struct btrfs_root *root)
79479154b1bSChris Mason {
79579154b1bSChris Mason 	int ret;
7960b86a832SChris Mason 	u64 old_root_bytenr;
79786b9f2ecSYan, Zheng 	u64 old_root_used;
7980b86a832SChris Mason 	struct btrfs_root *tree_root = root->fs_info->tree_root;
79979154b1bSChris Mason 
80086b9f2ecSYan, Zheng 	old_root_used = btrfs_root_used(&root->root_item);
8010b86a832SChris Mason 	btrfs_write_dirty_block_groups(trans, root);
80256bec294SChris Mason 
80379154b1bSChris Mason 	while (1) {
8040b86a832SChris Mason 		old_root_bytenr = btrfs_root_bytenr(&root->root_item);
80586b9f2ecSYan, Zheng 		if (old_root_bytenr == root->node->start &&
80686b9f2ecSYan, Zheng 		    old_root_used == btrfs_root_used(&root->root_item))
80779154b1bSChris Mason 			break;
80887ef2bb4SChris Mason 
8095d4f98a2SYan Zheng 		btrfs_set_root_node(&root->root_item, root->node);
81079154b1bSChris Mason 		ret = btrfs_update_root(trans, tree_root,
8110b86a832SChris Mason 					&root->root_key,
8120b86a832SChris Mason 					&root->root_item);
81349b25e05SJeff Mahoney 		if (ret)
81449b25e05SJeff Mahoney 			return ret;
81556bec294SChris Mason 
81686b9f2ecSYan, Zheng 		old_root_used = btrfs_root_used(&root->root_item);
8174a8c9a62SYan Zheng 		ret = btrfs_write_dirty_block_groups(trans, root);
81849b25e05SJeff Mahoney 		if (ret)
81949b25e05SJeff Mahoney 			return ret;
8200b86a832SChris Mason 	}
821276e680dSYan Zheng 
822276e680dSYan Zheng 	if (root != root->fs_info->extent_root)
823817d52f8SJosef Bacik 		switch_commit_root(root);
824276e680dSYan Zheng 
8250b86a832SChris Mason 	return 0;
8260b86a832SChris Mason }
8270b86a832SChris Mason 
828d352ac68SChris Mason /*
829d352ac68SChris Mason  * update all the cowonly tree roots on disk
83049b25e05SJeff Mahoney  *
83149b25e05SJeff Mahoney  * The error handling in this function may not be obvious. Any of the
83249b25e05SJeff Mahoney  * failures will cause the file system to go offline. We still need
83349b25e05SJeff Mahoney  * to clean up the delayed refs.
834d352ac68SChris Mason  */
8355d4f98a2SYan Zheng static noinline int commit_cowonly_roots(struct btrfs_trans_handle *trans,
8360b86a832SChris Mason 					 struct btrfs_root *root)
8370b86a832SChris Mason {
8380b86a832SChris Mason 	struct btrfs_fs_info *fs_info = root->fs_info;
8390b86a832SChris Mason 	struct list_head *next;
84084234f3aSYan Zheng 	struct extent_buffer *eb;
84156bec294SChris Mason 	int ret;
84284234f3aSYan Zheng 
84356bec294SChris Mason 	ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
84449b25e05SJeff Mahoney 	if (ret)
84549b25e05SJeff Mahoney 		return ret;
84687ef2bb4SChris Mason 
84784234f3aSYan Zheng 	eb = btrfs_lock_root_node(fs_info->tree_root);
84849b25e05SJeff Mahoney 	ret = btrfs_cow_block(trans, fs_info->tree_root, eb, NULL,
84949b25e05SJeff Mahoney 			      0, &eb);
85084234f3aSYan Zheng 	btrfs_tree_unlock(eb);
85184234f3aSYan Zheng 	free_extent_buffer(eb);
8520b86a832SChris Mason 
85349b25e05SJeff Mahoney 	if (ret)
85449b25e05SJeff Mahoney 		return ret;
85549b25e05SJeff Mahoney 
85656bec294SChris Mason 	ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
85749b25e05SJeff Mahoney 	if (ret)
85849b25e05SJeff Mahoney 		return ret;
85987ef2bb4SChris Mason 
860733f4fbbSStefan Behrens 	ret = btrfs_run_dev_stats(trans, root->fs_info);
8618dabb742SStefan Behrens 	WARN_ON(ret);
8628dabb742SStefan Behrens 	ret = btrfs_run_dev_replace(trans, root->fs_info);
8638dabb742SStefan Behrens 	WARN_ON(ret);
864733f4fbbSStefan Behrens 
865546adb0dSJan Schmidt 	ret = btrfs_run_qgroups(trans, root->fs_info);
866546adb0dSJan Schmidt 	BUG_ON(ret);
867546adb0dSJan Schmidt 
868546adb0dSJan Schmidt 	/* run_qgroups might have added some more refs */
869546adb0dSJan Schmidt 	ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
870546adb0dSJan Schmidt 	BUG_ON(ret);
871546adb0dSJan Schmidt 
8720b86a832SChris Mason 	while (!list_empty(&fs_info->dirty_cowonly_roots)) {
8730b86a832SChris Mason 		next = fs_info->dirty_cowonly_roots.next;
8740b86a832SChris Mason 		list_del_init(next);
8750b86a832SChris Mason 		root = list_entry(next, struct btrfs_root, dirty_list);
87687ef2bb4SChris Mason 
87749b25e05SJeff Mahoney 		ret = update_cowonly_root(trans, root);
87849b25e05SJeff Mahoney 		if (ret)
87949b25e05SJeff Mahoney 			return ret;
88079154b1bSChris Mason 	}
881276e680dSYan Zheng 
882276e680dSYan Zheng 	down_write(&fs_info->extent_commit_sem);
883276e680dSYan Zheng 	switch_commit_root(fs_info->extent_root);
884276e680dSYan Zheng 	up_write(&fs_info->extent_commit_sem);
885276e680dSYan Zheng 
8868dabb742SStefan Behrens 	btrfs_after_dev_replace_commit(fs_info);
8878dabb742SStefan Behrens 
88879154b1bSChris Mason 	return 0;
88979154b1bSChris Mason }
89079154b1bSChris Mason 
891d352ac68SChris Mason /*
892d352ac68SChris Mason  * dead roots are old snapshots that need to be deleted.  This allocates
893d352ac68SChris Mason  * a dirty root struct and adds it into the list of dead roots that need to
894d352ac68SChris Mason  * be deleted
895d352ac68SChris Mason  */
8965d4f98a2SYan Zheng int btrfs_add_dead_root(struct btrfs_root *root)
8975eda7b5eSChris Mason {
898a4abeea4SJosef Bacik 	spin_lock(&root->fs_info->trans_lock);
8995d4f98a2SYan Zheng 	list_add(&root->root_list, &root->fs_info->dead_roots);
900a4abeea4SJosef Bacik 	spin_unlock(&root->fs_info->trans_lock);
9015eda7b5eSChris Mason 	return 0;
9025eda7b5eSChris Mason }
9035eda7b5eSChris Mason 
904d352ac68SChris Mason /*
9055d4f98a2SYan Zheng  * update all the cowonly tree roots on disk
906d352ac68SChris Mason  */
9075d4f98a2SYan Zheng static noinline int commit_fs_roots(struct btrfs_trans_handle *trans,
9085d4f98a2SYan Zheng 				    struct btrfs_root *root)
9090f7d52f4SChris Mason {
9100f7d52f4SChris Mason 	struct btrfs_root *gang[8];
9115d4f98a2SYan Zheng 	struct btrfs_fs_info *fs_info = root->fs_info;
9120f7d52f4SChris Mason 	int i;
9130f7d52f4SChris Mason 	int ret;
91454aa1f4dSChris Mason 	int err = 0;
91554aa1f4dSChris Mason 
916a4abeea4SJosef Bacik 	spin_lock(&fs_info->fs_roots_radix_lock);
9170f7d52f4SChris Mason 	while (1) {
9185d4f98a2SYan Zheng 		ret = radix_tree_gang_lookup_tag(&fs_info->fs_roots_radix,
9195d4f98a2SYan Zheng 						 (void **)gang, 0,
9200f7d52f4SChris Mason 						 ARRAY_SIZE(gang),
9210f7d52f4SChris Mason 						 BTRFS_ROOT_TRANS_TAG);
9220f7d52f4SChris Mason 		if (ret == 0)
9230f7d52f4SChris Mason 			break;
9240f7d52f4SChris Mason 		for (i = 0; i < ret; i++) {
9250f7d52f4SChris Mason 			root = gang[i];
9265d4f98a2SYan Zheng 			radix_tree_tag_clear(&fs_info->fs_roots_radix,
9272619ba1fSChris Mason 					(unsigned long)root->root_key.objectid,
9280f7d52f4SChris Mason 					BTRFS_ROOT_TRANS_TAG);
929a4abeea4SJosef Bacik 			spin_unlock(&fs_info->fs_roots_radix_lock);
93031153d81SYan Zheng 
931e02119d5SChris Mason 			btrfs_free_log(trans, root);
9325d4f98a2SYan Zheng 			btrfs_update_reloc_root(trans, root);
933d68fc57bSYan, Zheng 			btrfs_orphan_commit_root(trans, root);
934e02119d5SChris Mason 
93582d5902dSLi Zefan 			btrfs_save_ino_cache(root, trans);
93682d5902dSLi Zefan 
937f1ebcc74SLiu Bo 			/* see comments in should_cow_block() */
938f1ebcc74SLiu Bo 			root->force_cow = 0;
939f1ebcc74SLiu Bo 			smp_wmb();
940f1ebcc74SLiu Bo 
941978d910dSYan Zheng 			if (root->commit_root != root->node) {
942581bb050SLi Zefan 				mutex_lock(&root->fs_commit_mutex);
943817d52f8SJosef Bacik 				switch_commit_root(root);
944581bb050SLi Zefan 				btrfs_unpin_free_ino(root);
945581bb050SLi Zefan 				mutex_unlock(&root->fs_commit_mutex);
946581bb050SLi Zefan 
947978d910dSYan Zheng 				btrfs_set_root_node(&root->root_item,
948978d910dSYan Zheng 						    root->node);
949978d910dSYan Zheng 			}
95031153d81SYan Zheng 
9515d4f98a2SYan Zheng 			err = btrfs_update_root(trans, fs_info->tree_root,
9520f7d52f4SChris Mason 						&root->root_key,
9530f7d52f4SChris Mason 						&root->root_item);
954a4abeea4SJosef Bacik 			spin_lock(&fs_info->fs_roots_radix_lock);
95554aa1f4dSChris Mason 			if (err)
95654aa1f4dSChris Mason 				break;
9570f7d52f4SChris Mason 		}
9589f3a7427SChris Mason 	}
959a4abeea4SJosef Bacik 	spin_unlock(&fs_info->fs_roots_radix_lock);
96054aa1f4dSChris Mason 	return err;
9610f7d52f4SChris Mason }
9620f7d52f4SChris Mason 
963d352ac68SChris Mason /*
964de78b51aSEric Sandeen  * defrag a given btree.
965de78b51aSEric Sandeen  * Every leaf in the btree is read and defragged.
966d352ac68SChris Mason  */
967de78b51aSEric Sandeen int btrfs_defrag_root(struct btrfs_root *root)
968e9d0b13bSChris Mason {
969e9d0b13bSChris Mason 	struct btrfs_fs_info *info = root->fs_info;
970e9d0b13bSChris Mason 	struct btrfs_trans_handle *trans;
9718929ecfaSYan, Zheng 	int ret;
972e9d0b13bSChris Mason 
9738929ecfaSYan, Zheng 	if (xchg(&root->defrag_running, 1))
974e9d0b13bSChris Mason 		return 0;
9758929ecfaSYan, Zheng 
9766b80053dSChris Mason 	while (1) {
9778929ecfaSYan, Zheng 		trans = btrfs_start_transaction(root, 0);
9788929ecfaSYan, Zheng 		if (IS_ERR(trans))
9798929ecfaSYan, Zheng 			return PTR_ERR(trans);
9808929ecfaSYan, Zheng 
981de78b51aSEric Sandeen 		ret = btrfs_defrag_leaves(trans, root);
9828929ecfaSYan, Zheng 
983e9d0b13bSChris Mason 		btrfs_end_transaction(trans, root);
984b53d3f5dSLiu Bo 		btrfs_btree_balance_dirty(info->tree_root);
985e9d0b13bSChris Mason 		cond_resched();
986e9d0b13bSChris Mason 
9877841cb28SDavid Sterba 		if (btrfs_fs_closing(root->fs_info) || ret != -EAGAIN)
988e9d0b13bSChris Mason 			break;
989210549ebSDavid Sterba 
990210549ebSDavid Sterba 		if (btrfs_defrag_cancelled(root->fs_info)) {
991210549ebSDavid Sterba 			printk(KERN_DEBUG "btrfs: defrag_root cancelled\n");
992210549ebSDavid Sterba 			ret = -EAGAIN;
993210549ebSDavid Sterba 			break;
994210549ebSDavid Sterba 		}
995e9d0b13bSChris Mason 	}
996e9d0b13bSChris Mason 	root->defrag_running = 0;
9978929ecfaSYan, Zheng 	return ret;
998e9d0b13bSChris Mason }
999e9d0b13bSChris Mason 
1000d352ac68SChris Mason /*
1001d352ac68SChris Mason  * new snapshots need to be created at a very specific time in the
1002d352ac68SChris Mason  * transaction commit.  This does the actual creation
1003d352ac68SChris Mason  */
100480b6794dSChris Mason static noinline int create_pending_snapshot(struct btrfs_trans_handle *trans,
10053063d29fSChris Mason 				   struct btrfs_fs_info *fs_info,
10063063d29fSChris Mason 				   struct btrfs_pending_snapshot *pending)
10073063d29fSChris Mason {
10083063d29fSChris Mason 	struct btrfs_key key;
100980b6794dSChris Mason 	struct btrfs_root_item *new_root_item;
10103063d29fSChris Mason 	struct btrfs_root *tree_root = fs_info->tree_root;
10113063d29fSChris Mason 	struct btrfs_root *root = pending->root;
10126bdb72deSSage Weil 	struct btrfs_root *parent_root;
101398c9942aSLiu Bo 	struct btrfs_block_rsv *rsv;
10146bdb72deSSage Weil 	struct inode *parent_inode;
101542874b3dSMiao Xie 	struct btrfs_path *path;
101642874b3dSMiao Xie 	struct btrfs_dir_item *dir_item;
10176a912213SJosef Bacik 	struct dentry *parent;
1018a22285a6SYan, Zheng 	struct dentry *dentry;
10193063d29fSChris Mason 	struct extent_buffer *tmp;
1020925baeddSChris Mason 	struct extent_buffer *old;
10218ea05e3aSAlexander Block 	struct timespec cur_time = CURRENT_TIME;
10223063d29fSChris Mason 	int ret;
1023d68fc57bSYan, Zheng 	u64 to_reserve = 0;
10246bdb72deSSage Weil 	u64 index = 0;
1025a22285a6SYan, Zheng 	u64 objectid;
1026b83cc969SLi Zefan 	u64 root_flags;
10278ea05e3aSAlexander Block 	uuid_le new_uuid;
10283063d29fSChris Mason 
102942874b3dSMiao Xie 	path = btrfs_alloc_path();
103042874b3dSMiao Xie 	if (!path) {
103142874b3dSMiao Xie 		ret = pending->error = -ENOMEM;
103242874b3dSMiao Xie 		goto path_alloc_fail;
103342874b3dSMiao Xie 	}
103442874b3dSMiao Xie 
103580b6794dSChris Mason 	new_root_item = kmalloc(sizeof(*new_root_item), GFP_NOFS);
103680b6794dSChris Mason 	if (!new_root_item) {
103749b25e05SJeff Mahoney 		ret = pending->error = -ENOMEM;
10386fa9700eSMiao Xie 		goto root_item_alloc_fail;
103980b6794dSChris Mason 	}
1040a22285a6SYan, Zheng 
1041581bb050SLi Zefan 	ret = btrfs_find_free_objectid(tree_root, &objectid);
1042a22285a6SYan, Zheng 	if (ret) {
1043a22285a6SYan, Zheng 		pending->error = ret;
10446fa9700eSMiao Xie 		goto no_free_objectid;
1045a22285a6SYan, Zheng 	}
10463063d29fSChris Mason 
10473fd0a558SYan, Zheng 	btrfs_reloc_pre_snapshot(trans, pending, &to_reserve);
1048d68fc57bSYan, Zheng 
1049d68fc57bSYan, Zheng 	if (to_reserve > 0) {
105008e007d2SMiao Xie 		ret = btrfs_block_rsv_add(root, &pending->block_rsv,
105108e007d2SMiao Xie 					  to_reserve,
105208e007d2SMiao Xie 					  BTRFS_RESERVE_NO_FLUSH);
1053d68fc57bSYan, Zheng 		if (ret) {
1054d68fc57bSYan, Zheng 			pending->error = ret;
10556fa9700eSMiao Xie 			goto no_free_objectid;
1056d68fc57bSYan, Zheng 		}
1057d68fc57bSYan, Zheng 	}
1058d68fc57bSYan, Zheng 
10596f72c7e2SArne Jansen 	ret = btrfs_qgroup_inherit(trans, fs_info, root->root_key.objectid,
10606f72c7e2SArne Jansen 				   objectid, pending->inherit);
10616f72c7e2SArne Jansen 	if (ret) {
10626f72c7e2SArne Jansen 		pending->error = ret;
10636fa9700eSMiao Xie 		goto no_free_objectid;
10646f72c7e2SArne Jansen 	}
10656f72c7e2SArne Jansen 
10663063d29fSChris Mason 	key.objectid = objectid;
1067a22285a6SYan, Zheng 	key.offset = (u64)-1;
1068a22285a6SYan, Zheng 	key.type = BTRFS_ROOT_ITEM_KEY;
10693063d29fSChris Mason 
10706fa9700eSMiao Xie 	rsv = trans->block_rsv;
1071a22285a6SYan, Zheng 	trans->block_rsv = &pending->block_rsv;
10726bdb72deSSage Weil 
1073a22285a6SYan, Zheng 	dentry = pending->dentry;
10746a912213SJosef Bacik 	parent = dget_parent(dentry);
10756a912213SJosef Bacik 	parent_inode = parent->d_inode;
1076a22285a6SYan, Zheng 	parent_root = BTRFS_I(parent_inode)->root;
10777585717fSChris Mason 	record_root_in_trans(trans, parent_root);
1078a22285a6SYan, Zheng 
10796bdb72deSSage Weil 	/*
10806bdb72deSSage Weil 	 * insert the directory item
10816bdb72deSSage Weil 	 */
10826bdb72deSSage Weil 	ret = btrfs_set_inode_index(parent_inode, &index);
108349b25e05SJeff Mahoney 	BUG_ON(ret); /* -ENOMEM */
108442874b3dSMiao Xie 
108542874b3dSMiao Xie 	/* check if there is a file/dir which has the same name. */
108642874b3dSMiao Xie 	dir_item = btrfs_lookup_dir_item(NULL, parent_root, path,
108742874b3dSMiao Xie 					 btrfs_ino(parent_inode),
108842874b3dSMiao Xie 					 dentry->d_name.name,
108942874b3dSMiao Xie 					 dentry->d_name.len, 0);
109042874b3dSMiao Xie 	if (dir_item != NULL && !IS_ERR(dir_item)) {
1091fe66a05aSChris Mason 		pending->error = -EEXIST;
1092fe66a05aSChris Mason 		goto fail;
109342874b3dSMiao Xie 	} else if (IS_ERR(dir_item)) {
109442874b3dSMiao Xie 		ret = PTR_ERR(dir_item);
10958732d44fSMiao Xie 		btrfs_abort_transaction(trans, root, ret);
10968732d44fSMiao Xie 		goto fail;
109779787eaaSJeff Mahoney 	}
109842874b3dSMiao Xie 	btrfs_release_path(path);
10996bdb72deSSage Weil 
1100e999376fSChris Mason 	/*
1101e999376fSChris Mason 	 * pull in the delayed directory update
1102e999376fSChris Mason 	 * and the delayed inode item
1103e999376fSChris Mason 	 * otherwise we corrupt the FS during
1104e999376fSChris Mason 	 * snapshot
1105e999376fSChris Mason 	 */
1106e999376fSChris Mason 	ret = btrfs_run_delayed_items(trans, root);
11078732d44fSMiao Xie 	if (ret) {	/* Transaction aborted */
11088732d44fSMiao Xie 		btrfs_abort_transaction(trans, root, ret);
11098732d44fSMiao Xie 		goto fail;
11108732d44fSMiao Xie 	}
1111e999376fSChris Mason 
11127585717fSChris Mason 	record_root_in_trans(trans, root);
11136bdb72deSSage Weil 	btrfs_set_root_last_snapshot(&root->root_item, trans->transid);
11146bdb72deSSage Weil 	memcpy(new_root_item, &root->root_item, sizeof(*new_root_item));
111508fe4db1SLi Zefan 	btrfs_check_and_init_root_item(new_root_item);
11166bdb72deSSage Weil 
1117b83cc969SLi Zefan 	root_flags = btrfs_root_flags(new_root_item);
1118b83cc969SLi Zefan 	if (pending->readonly)
1119b83cc969SLi Zefan 		root_flags |= BTRFS_ROOT_SUBVOL_RDONLY;
1120b83cc969SLi Zefan 	else
1121b83cc969SLi Zefan 		root_flags &= ~BTRFS_ROOT_SUBVOL_RDONLY;
1122b83cc969SLi Zefan 	btrfs_set_root_flags(new_root_item, root_flags);
1123b83cc969SLi Zefan 
11248ea05e3aSAlexander Block 	btrfs_set_root_generation_v2(new_root_item,
11258ea05e3aSAlexander Block 			trans->transid);
11268ea05e3aSAlexander Block 	uuid_le_gen(&new_uuid);
11278ea05e3aSAlexander Block 	memcpy(new_root_item->uuid, new_uuid.b, BTRFS_UUID_SIZE);
11288ea05e3aSAlexander Block 	memcpy(new_root_item->parent_uuid, root->root_item.uuid,
11298ea05e3aSAlexander Block 			BTRFS_UUID_SIZE);
11308ea05e3aSAlexander Block 	new_root_item->otime.sec = cpu_to_le64(cur_time.tv_sec);
1131dadd1105SDan Carpenter 	new_root_item->otime.nsec = cpu_to_le32(cur_time.tv_nsec);
11328ea05e3aSAlexander Block 	btrfs_set_root_otransid(new_root_item, trans->transid);
11338ea05e3aSAlexander Block 	memset(&new_root_item->stime, 0, sizeof(new_root_item->stime));
11348ea05e3aSAlexander Block 	memset(&new_root_item->rtime, 0, sizeof(new_root_item->rtime));
11358ea05e3aSAlexander Block 	btrfs_set_root_stransid(new_root_item, 0);
11368ea05e3aSAlexander Block 	btrfs_set_root_rtransid(new_root_item, 0);
11378ea05e3aSAlexander Block 
1138925baeddSChris Mason 	old = btrfs_lock_root_node(root);
113949b25e05SJeff Mahoney 	ret = btrfs_cow_block(trans, root, old, NULL, 0, &old);
114079787eaaSJeff Mahoney 	if (ret) {
114179787eaaSJeff Mahoney 		btrfs_tree_unlock(old);
114279787eaaSJeff Mahoney 		free_extent_buffer(old);
11438732d44fSMiao Xie 		btrfs_abort_transaction(trans, root, ret);
11448732d44fSMiao Xie 		goto fail;
114579787eaaSJeff Mahoney 	}
114649b25e05SJeff Mahoney 
11475d4f98a2SYan Zheng 	btrfs_set_lock_blocking(old);
11483063d29fSChris Mason 
114949b25e05SJeff Mahoney 	ret = btrfs_copy_root(trans, root, old, &tmp, objectid);
115079787eaaSJeff Mahoney 	/* clean up in any case */
1151925baeddSChris Mason 	btrfs_tree_unlock(old);
1152925baeddSChris Mason 	free_extent_buffer(old);
11538732d44fSMiao Xie 	if (ret) {
11548732d44fSMiao Xie 		btrfs_abort_transaction(trans, root, ret);
11558732d44fSMiao Xie 		goto fail;
11568732d44fSMiao Xie 	}
11573063d29fSChris Mason 
1158f1ebcc74SLiu Bo 	/* see comments in should_cow_block() */
1159f1ebcc74SLiu Bo 	root->force_cow = 1;
1160f1ebcc74SLiu Bo 	smp_wmb();
1161f1ebcc74SLiu Bo 
11625d4f98a2SYan Zheng 	btrfs_set_root_node(new_root_item, tmp);
1163a22285a6SYan, Zheng 	/* record when the snapshot was created in key.offset */
1164a22285a6SYan, Zheng 	key.offset = trans->transid;
1165a22285a6SYan, Zheng 	ret = btrfs_insert_root(trans, tree_root, &key, new_root_item);
1166925baeddSChris Mason 	btrfs_tree_unlock(tmp);
11673063d29fSChris Mason 	free_extent_buffer(tmp);
11688732d44fSMiao Xie 	if (ret) {
11698732d44fSMiao Xie 		btrfs_abort_transaction(trans, root, ret);
11708732d44fSMiao Xie 		goto fail;
11718732d44fSMiao Xie 	}
11720660b5afSChris Mason 
1173a22285a6SYan, Zheng 	/*
1174a22285a6SYan, Zheng 	 * insert root back/forward references
1175a22285a6SYan, Zheng 	 */
1176a22285a6SYan, Zheng 	ret = btrfs_add_root_ref(trans, tree_root, objectid,
1177a22285a6SYan, Zheng 				 parent_root->root_key.objectid,
117833345d01SLi Zefan 				 btrfs_ino(parent_inode), index,
1179a22285a6SYan, Zheng 				 dentry->d_name.name, dentry->d_name.len);
11808732d44fSMiao Xie 	if (ret) {
11818732d44fSMiao Xie 		btrfs_abort_transaction(trans, root, ret);
11828732d44fSMiao Xie 		goto fail;
11838732d44fSMiao Xie 	}
1184a22285a6SYan, Zheng 
1185a22285a6SYan, Zheng 	key.offset = (u64)-1;
1186a22285a6SYan, Zheng 	pending->snap = btrfs_read_fs_root_no_name(root->fs_info, &key);
118779787eaaSJeff Mahoney 	if (IS_ERR(pending->snap)) {
118879787eaaSJeff Mahoney 		ret = PTR_ERR(pending->snap);
11898732d44fSMiao Xie 		btrfs_abort_transaction(trans, root, ret);
11908732d44fSMiao Xie 		goto fail;
119179787eaaSJeff Mahoney 	}
1192d68fc57bSYan, Zheng 
119349b25e05SJeff Mahoney 	ret = btrfs_reloc_post_snapshot(trans, pending);
11948732d44fSMiao Xie 	if (ret) {
11958732d44fSMiao Xie 		btrfs_abort_transaction(trans, root, ret);
11968732d44fSMiao Xie 		goto fail;
11978732d44fSMiao Xie 	}
1198361048f5SMiao Xie 
1199361048f5SMiao Xie 	ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
12008732d44fSMiao Xie 	if (ret) {
12018732d44fSMiao Xie 		btrfs_abort_transaction(trans, root, ret);
12028732d44fSMiao Xie 		goto fail;
12038732d44fSMiao Xie 	}
120442874b3dSMiao Xie 
120542874b3dSMiao Xie 	ret = btrfs_insert_dir_item(trans, parent_root,
120642874b3dSMiao Xie 				    dentry->d_name.name, dentry->d_name.len,
120742874b3dSMiao Xie 				    parent_inode, &key,
120842874b3dSMiao Xie 				    BTRFS_FT_DIR, index);
120942874b3dSMiao Xie 	/* We have check then name at the beginning, so it is impossible. */
12109c52057cSChris Mason 	BUG_ON(ret == -EEXIST || ret == -EOVERFLOW);
12118732d44fSMiao Xie 	if (ret) {
12128732d44fSMiao Xie 		btrfs_abort_transaction(trans, root, ret);
12138732d44fSMiao Xie 		goto fail;
12148732d44fSMiao Xie 	}
121542874b3dSMiao Xie 
121642874b3dSMiao Xie 	btrfs_i_size_write(parent_inode, parent_inode->i_size +
121742874b3dSMiao Xie 					 dentry->d_name.len * 2);
121842874b3dSMiao Xie 	parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
1219be6aef60SJosef Bacik 	ret = btrfs_update_inode_fallback(trans, parent_root, parent_inode);
122042874b3dSMiao Xie 	if (ret)
12218732d44fSMiao Xie 		btrfs_abort_transaction(trans, root, ret);
12223063d29fSChris Mason fail:
12236fa9700eSMiao Xie 	dput(parent);
122498c9942aSLiu Bo 	trans->block_rsv = rsv;
12256fa9700eSMiao Xie no_free_objectid:
12266fa9700eSMiao Xie 	kfree(new_root_item);
12276fa9700eSMiao Xie root_item_alloc_fail:
122842874b3dSMiao Xie 	btrfs_free_path(path);
122942874b3dSMiao Xie path_alloc_fail:
1230a22285a6SYan, Zheng 	btrfs_block_rsv_release(root, &pending->block_rsv, (u64)-1);
123149b25e05SJeff Mahoney 	return ret;
12323063d29fSChris Mason }
12333063d29fSChris Mason 
1234d352ac68SChris Mason /*
1235d352ac68SChris Mason  * create all the snapshots we've scheduled for creation
1236d352ac68SChris Mason  */
123780b6794dSChris Mason static noinline int create_pending_snapshots(struct btrfs_trans_handle *trans,
12383063d29fSChris Mason 					     struct btrfs_fs_info *fs_info)
12393063d29fSChris Mason {
12403063d29fSChris Mason 	struct btrfs_pending_snapshot *pending;
12413063d29fSChris Mason 	struct list_head *head = &trans->transaction->pending_snapshots;
12423de4586cSChris Mason 
1243fe66a05aSChris Mason 	list_for_each_entry(pending, head, list)
1244fe66a05aSChris Mason 		create_pending_snapshot(trans, fs_info, pending);
12453de4586cSChris Mason 	return 0;
12463de4586cSChris Mason }
12473de4586cSChris Mason 
12485d4f98a2SYan Zheng static void update_super_roots(struct btrfs_root *root)
12495d4f98a2SYan Zheng {
12505d4f98a2SYan Zheng 	struct btrfs_root_item *root_item;
12515d4f98a2SYan Zheng 	struct btrfs_super_block *super;
12525d4f98a2SYan Zheng 
12536c41761fSDavid Sterba 	super = root->fs_info->super_copy;
12545d4f98a2SYan Zheng 
12555d4f98a2SYan Zheng 	root_item = &root->fs_info->chunk_root->root_item;
12565d4f98a2SYan Zheng 	super->chunk_root = root_item->bytenr;
12575d4f98a2SYan Zheng 	super->chunk_root_generation = root_item->generation;
12585d4f98a2SYan Zheng 	super->chunk_root_level = root_item->level;
12595d4f98a2SYan Zheng 
12605d4f98a2SYan Zheng 	root_item = &root->fs_info->tree_root->root_item;
12615d4f98a2SYan Zheng 	super->root = root_item->bytenr;
12625d4f98a2SYan Zheng 	super->generation = root_item->generation;
12635d4f98a2SYan Zheng 	super->root_level = root_item->level;
126473bc1876SJosef Bacik 	if (btrfs_test_opt(root, SPACE_CACHE))
12650af3d00bSJosef Bacik 		super->cache_generation = root_item->generation;
12665d4f98a2SYan Zheng }
12675d4f98a2SYan Zheng 
1268f36f3042SChris Mason int btrfs_transaction_in_commit(struct btrfs_fs_info *info)
1269f36f3042SChris Mason {
1270f36f3042SChris Mason 	int ret = 0;
1271a4abeea4SJosef Bacik 	spin_lock(&info->trans_lock);
1272f36f3042SChris Mason 	if (info->running_transaction)
1273f36f3042SChris Mason 		ret = info->running_transaction->in_commit;
1274a4abeea4SJosef Bacik 	spin_unlock(&info->trans_lock);
1275f36f3042SChris Mason 	return ret;
1276f36f3042SChris Mason }
1277f36f3042SChris Mason 
12788929ecfaSYan, Zheng int btrfs_transaction_blocked(struct btrfs_fs_info *info)
12798929ecfaSYan, Zheng {
12808929ecfaSYan, Zheng 	int ret = 0;
1281a4abeea4SJosef Bacik 	spin_lock(&info->trans_lock);
12828929ecfaSYan, Zheng 	if (info->running_transaction)
12838929ecfaSYan, Zheng 		ret = info->running_transaction->blocked;
1284a4abeea4SJosef Bacik 	spin_unlock(&info->trans_lock);
12858929ecfaSYan, Zheng 	return ret;
12868929ecfaSYan, Zheng }
12878929ecfaSYan, Zheng 
1288bb9c12c9SSage Weil /*
1289bb9c12c9SSage Weil  * wait for the current transaction commit to start and block subsequent
1290bb9c12c9SSage Weil  * transaction joins
1291bb9c12c9SSage Weil  */
1292bb9c12c9SSage Weil static void wait_current_trans_commit_start(struct btrfs_root *root,
1293bb9c12c9SSage Weil 					    struct btrfs_transaction *trans)
1294bb9c12c9SSage Weil {
129572d63ed6SLi Zefan 	wait_event(root->fs_info->transaction_blocked_wait, trans->in_commit);
1296bb9c12c9SSage Weil }
1297bb9c12c9SSage Weil 
1298bb9c12c9SSage Weil /*
1299bb9c12c9SSage Weil  * wait for the current transaction to start and then become unblocked.
1300bb9c12c9SSage Weil  * caller holds ref.
1301bb9c12c9SSage Weil  */
1302bb9c12c9SSage Weil static void wait_current_trans_commit_start_and_unblock(struct btrfs_root *root,
1303bb9c12c9SSage Weil 					 struct btrfs_transaction *trans)
1304bb9c12c9SSage Weil {
130572d63ed6SLi Zefan 	wait_event(root->fs_info->transaction_wait,
130672d63ed6SLi Zefan 		   trans->commit_done || (trans->in_commit && !trans->blocked));
1307bb9c12c9SSage Weil }
1308bb9c12c9SSage Weil 
1309bb9c12c9SSage Weil /*
1310bb9c12c9SSage Weil  * commit transactions asynchronously. once btrfs_commit_transaction_async
1311bb9c12c9SSage Weil  * returns, any subsequent transaction will not be allowed to join.
1312bb9c12c9SSage Weil  */
1313bb9c12c9SSage Weil struct btrfs_async_commit {
1314bb9c12c9SSage Weil 	struct btrfs_trans_handle *newtrans;
1315bb9c12c9SSage Weil 	struct btrfs_root *root;
13167892b5afSMiao Xie 	struct work_struct work;
1317bb9c12c9SSage Weil };
1318bb9c12c9SSage Weil 
1319bb9c12c9SSage Weil static void do_async_commit(struct work_struct *work)
1320bb9c12c9SSage Weil {
1321bb9c12c9SSage Weil 	struct btrfs_async_commit *ac =
13227892b5afSMiao Xie 		container_of(work, struct btrfs_async_commit, work);
1323bb9c12c9SSage Weil 
13246fc4e354SSage Weil 	/*
13256fc4e354SSage Weil 	 * We've got freeze protection passed with the transaction.
13266fc4e354SSage Weil 	 * Tell lockdep about it.
13276fc4e354SSage Weil 	 */
1328ff7c1d33SMiao Xie 	if (ac->newtrans->type < TRANS_JOIN_NOLOCK)
13296fc4e354SSage Weil 		rwsem_acquire_read(
13306fc4e354SSage Weil 		     &ac->root->fs_info->sb->s_writers.lock_map[SB_FREEZE_FS-1],
13316fc4e354SSage Weil 		     0, 1, _THIS_IP_);
13326fc4e354SSage Weil 
1333e209db7aSSage Weil 	current->journal_info = ac->newtrans;
1334e209db7aSSage Weil 
1335bb9c12c9SSage Weil 	btrfs_commit_transaction(ac->newtrans, ac->root);
1336bb9c12c9SSage Weil 	kfree(ac);
1337bb9c12c9SSage Weil }
1338bb9c12c9SSage Weil 
1339bb9c12c9SSage Weil int btrfs_commit_transaction_async(struct btrfs_trans_handle *trans,
1340bb9c12c9SSage Weil 				   struct btrfs_root *root,
1341bb9c12c9SSage Weil 				   int wait_for_unblock)
1342bb9c12c9SSage Weil {
1343bb9c12c9SSage Weil 	struct btrfs_async_commit *ac;
1344bb9c12c9SSage Weil 	struct btrfs_transaction *cur_trans;
1345bb9c12c9SSage Weil 
1346bb9c12c9SSage Weil 	ac = kmalloc(sizeof(*ac), GFP_NOFS);
1347db5b493aSTsutomu Itoh 	if (!ac)
1348db5b493aSTsutomu Itoh 		return -ENOMEM;
1349bb9c12c9SSage Weil 
13507892b5afSMiao Xie 	INIT_WORK(&ac->work, do_async_commit);
1351bb9c12c9SSage Weil 	ac->root = root;
13527a7eaa40SJosef Bacik 	ac->newtrans = btrfs_join_transaction(root);
13533612b495STsutomu Itoh 	if (IS_ERR(ac->newtrans)) {
13543612b495STsutomu Itoh 		int err = PTR_ERR(ac->newtrans);
13553612b495STsutomu Itoh 		kfree(ac);
13563612b495STsutomu Itoh 		return err;
13573612b495STsutomu Itoh 	}
1358bb9c12c9SSage Weil 
1359bb9c12c9SSage Weil 	/* take transaction reference */
1360bb9c12c9SSage Weil 	cur_trans = trans->transaction;
136113c5a93eSJosef Bacik 	atomic_inc(&cur_trans->use_count);
1362bb9c12c9SSage Weil 
1363bb9c12c9SSage Weil 	btrfs_end_transaction(trans, root);
13646fc4e354SSage Weil 
13656fc4e354SSage Weil 	/*
13666fc4e354SSage Weil 	 * Tell lockdep we've released the freeze rwsem, since the
13676fc4e354SSage Weil 	 * async commit thread will be the one to unlock it.
13686fc4e354SSage Weil 	 */
1369ff7c1d33SMiao Xie 	if (trans->type < TRANS_JOIN_NOLOCK)
1370ff7c1d33SMiao Xie 		rwsem_release(
1371ff7c1d33SMiao Xie 			&root->fs_info->sb->s_writers.lock_map[SB_FREEZE_FS-1],
13726fc4e354SSage Weil 			1, _THIS_IP_);
13736fc4e354SSage Weil 
13747892b5afSMiao Xie 	schedule_work(&ac->work);
1375bb9c12c9SSage Weil 
1376bb9c12c9SSage Weil 	/* wait for transaction to start and unblock */
1377bb9c12c9SSage Weil 	if (wait_for_unblock)
1378bb9c12c9SSage Weil 		wait_current_trans_commit_start_and_unblock(root, cur_trans);
1379bb9c12c9SSage Weil 	else
1380bb9c12c9SSage Weil 		wait_current_trans_commit_start(root, cur_trans);
1381bb9c12c9SSage Weil 
138238e88054SSage Weil 	if (current->journal_info == trans)
138338e88054SSage Weil 		current->journal_info = NULL;
138438e88054SSage Weil 
138538e88054SSage Weil 	put_transaction(cur_trans);
1386bb9c12c9SSage Weil 	return 0;
1387bb9c12c9SSage Weil }
1388bb9c12c9SSage Weil 
138949b25e05SJeff Mahoney 
139049b25e05SJeff Mahoney static void cleanup_transaction(struct btrfs_trans_handle *trans,
13917b8b92afSJosef Bacik 				struct btrfs_root *root, int err)
139249b25e05SJeff Mahoney {
139349b25e05SJeff Mahoney 	struct btrfs_transaction *cur_trans = trans->transaction;
139449b25e05SJeff Mahoney 
139549b25e05SJeff Mahoney 	WARN_ON(trans->use_count > 1);
139649b25e05SJeff Mahoney 
13977b8b92afSJosef Bacik 	btrfs_abort_transaction(trans, root, err);
13987b8b92afSJosef Bacik 
139949b25e05SJeff Mahoney 	spin_lock(&root->fs_info->trans_lock);
140049b25e05SJeff Mahoney 	list_del_init(&cur_trans->list);
1401d7096fc3SJosef Bacik 	if (cur_trans == root->fs_info->running_transaction) {
1402d7096fc3SJosef Bacik 		root->fs_info->running_transaction = NULL;
1403d7096fc3SJosef Bacik 		root->fs_info->trans_no_join = 0;
1404d7096fc3SJosef Bacik 	}
140549b25e05SJeff Mahoney 	spin_unlock(&root->fs_info->trans_lock);
140649b25e05SJeff Mahoney 
140749b25e05SJeff Mahoney 	btrfs_cleanup_one_transaction(trans->transaction, root);
140849b25e05SJeff Mahoney 
140949b25e05SJeff Mahoney 	put_transaction(cur_trans);
141049b25e05SJeff Mahoney 	put_transaction(cur_trans);
141149b25e05SJeff Mahoney 
141249b25e05SJeff Mahoney 	trace_btrfs_transaction_commit(root);
141349b25e05SJeff Mahoney 
141449b25e05SJeff Mahoney 	btrfs_scrub_continue(root);
141549b25e05SJeff Mahoney 
141649b25e05SJeff Mahoney 	if (current->journal_info == trans)
141749b25e05SJeff Mahoney 		current->journal_info = NULL;
141849b25e05SJeff Mahoney 
141949b25e05SJeff Mahoney 	kmem_cache_free(btrfs_trans_handle_cachep, trans);
142049b25e05SJeff Mahoney }
142149b25e05SJeff Mahoney 
1422ca469637SMiao Xie static int btrfs_flush_all_pending_stuffs(struct btrfs_trans_handle *trans,
1423ca469637SMiao Xie 					  struct btrfs_root *root)
1424ca469637SMiao Xie {
1425ca469637SMiao Xie 	int flush_on_commit = btrfs_test_opt(root, FLUSHONCOMMIT);
1426ca469637SMiao Xie 	int snap_pending = 0;
1427ca469637SMiao Xie 	int ret;
1428ca469637SMiao Xie 
1429ca469637SMiao Xie 	if (!flush_on_commit) {
1430ca469637SMiao Xie 		spin_lock(&root->fs_info->trans_lock);
1431ca469637SMiao Xie 		if (!list_empty(&trans->transaction->pending_snapshots))
1432ca469637SMiao Xie 			snap_pending = 1;
1433ca469637SMiao Xie 		spin_unlock(&root->fs_info->trans_lock);
1434ca469637SMiao Xie 	}
1435ca469637SMiao Xie 
1436ca469637SMiao Xie 	if (flush_on_commit || snap_pending) {
14373edb2a68SMiao Xie 		ret = btrfs_start_delalloc_inodes(root, 1);
14383edb2a68SMiao Xie 		if (ret)
14393edb2a68SMiao Xie 			return ret;
1440ca469637SMiao Xie 		btrfs_wait_ordered_extents(root, 1);
1441ca469637SMiao Xie 	}
1442ca469637SMiao Xie 
1443ca469637SMiao Xie 	ret = btrfs_run_delayed_items(trans, root);
1444ca469637SMiao Xie 	if (ret)
1445ca469637SMiao Xie 		return ret;
1446ca469637SMiao Xie 
1447ca469637SMiao Xie 	/*
1448ca469637SMiao Xie 	 * running the delayed items may have added new refs. account
1449ca469637SMiao Xie 	 * them now so that they hinder processing of more delayed refs
1450ca469637SMiao Xie 	 * as little as possible.
1451ca469637SMiao Xie 	 */
1452ca469637SMiao Xie 	btrfs_delayed_refs_qgroup_accounting(trans, root->fs_info);
1453ca469637SMiao Xie 
1454ca469637SMiao Xie 	/*
1455ca469637SMiao Xie 	 * rename don't use btrfs_join_transaction, so, once we
1456ca469637SMiao Xie 	 * set the transaction to blocked above, we aren't going
1457ca469637SMiao Xie 	 * to get any new ordered operations.  We can safely run
1458ca469637SMiao Xie 	 * it here and no for sure that nothing new will be added
1459ca469637SMiao Xie 	 * to the list
1460ca469637SMiao Xie 	 */
1461569e0f35SJosef Bacik 	ret = btrfs_run_ordered_operations(trans, root, 1);
1462ca469637SMiao Xie 
1463eebc6084SMiao Xie 	return ret;
1464ca469637SMiao Xie }
1465ca469637SMiao Xie 
1466bb9c12c9SSage Weil /*
1467bb9c12c9SSage Weil  * btrfs_transaction state sequence:
1468bb9c12c9SSage Weil  *    in_commit = 0, blocked = 0  (initial)
1469bb9c12c9SSage Weil  *    in_commit = 1, blocked = 1
1470bb9c12c9SSage Weil  *    blocked = 0
1471bb9c12c9SSage Weil  *    commit_done = 1
1472bb9c12c9SSage Weil  */
147379154b1bSChris Mason int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
147479154b1bSChris Mason 			     struct btrfs_root *root)
147579154b1bSChris Mason {
147615ee9bc7SJosef Bacik 	unsigned long joined = 0;
147749b25e05SJeff Mahoney 	struct btrfs_transaction *cur_trans = trans->transaction;
14788fd17795SChris Mason 	struct btrfs_transaction *prev_trans = NULL;
147979154b1bSChris Mason 	DEFINE_WAIT(wait);
148025287e0aSMiao Xie 	int ret;
148189573b9cSChris Mason 	int should_grow = 0;
148289573b9cSChris Mason 	unsigned long now = get_seconds();
148379154b1bSChris Mason 
1484569e0f35SJosef Bacik 	ret = btrfs_run_ordered_operations(trans, root, 0);
148525287e0aSMiao Xie 	if (ret) {
148625287e0aSMiao Xie 		btrfs_abort_transaction(trans, root, ret);
1487e4a2bcacSJosef Bacik 		btrfs_end_transaction(trans, root);
1488e4a2bcacSJosef Bacik 		return ret;
148925287e0aSMiao Xie 	}
149025287e0aSMiao Xie 
14918d25a086SMiao Xie 	/* Stop the commit early if ->aborted is set */
14928d25a086SMiao Xie 	if (unlikely(ACCESS_ONCE(cur_trans->aborted))) {
149325287e0aSMiao Xie 		ret = cur_trans->aborted;
1494e4a2bcacSJosef Bacik 		btrfs_end_transaction(trans, root);
1495e4a2bcacSJosef Bacik 		return ret;
149625287e0aSMiao Xie 	}
149749b25e05SJeff Mahoney 
149856bec294SChris Mason 	/* make a pass through all the delayed refs we have so far
149956bec294SChris Mason 	 * any runnings procs may add more while we are here
150056bec294SChris Mason 	 */
150156bec294SChris Mason 	ret = btrfs_run_delayed_refs(trans, root, 0);
1502e4a2bcacSJosef Bacik 	if (ret) {
1503e4a2bcacSJosef Bacik 		btrfs_end_transaction(trans, root);
1504e4a2bcacSJosef Bacik 		return ret;
1505e4a2bcacSJosef Bacik 	}
150656bec294SChris Mason 
15070e721106SJosef Bacik 	btrfs_trans_release_metadata(trans, root);
15080e721106SJosef Bacik 	trans->block_rsv = NULL;
15090e721106SJosef Bacik 
1510b7ec40d7SChris Mason 	cur_trans = trans->transaction;
151149b25e05SJeff Mahoney 
151256bec294SChris Mason 	/*
151356bec294SChris Mason 	 * set the flushing flag so procs in this transaction have to
151456bec294SChris Mason 	 * start sending their work down.
151556bec294SChris Mason 	 */
1516b7ec40d7SChris Mason 	cur_trans->delayed_refs.flushing = 1;
151756bec294SChris Mason 
1518ea658badSJosef Bacik 	if (!list_empty(&trans->new_bgs))
1519ea658badSJosef Bacik 		btrfs_create_pending_block_groups(trans, root);
1520ea658badSJosef Bacik 
1521c3e69d58SChris Mason 	ret = btrfs_run_delayed_refs(trans, root, 0);
1522e4a2bcacSJosef Bacik 	if (ret) {
1523e4a2bcacSJosef Bacik 		btrfs_end_transaction(trans, root);
1524e4a2bcacSJosef Bacik 		return ret;
1525e4a2bcacSJosef Bacik 	}
152656bec294SChris Mason 
1527a4abeea4SJosef Bacik 	spin_lock(&cur_trans->commit_lock);
1528b7ec40d7SChris Mason 	if (cur_trans->in_commit) {
1529a4abeea4SJosef Bacik 		spin_unlock(&cur_trans->commit_lock);
153013c5a93eSJosef Bacik 		atomic_inc(&cur_trans->use_count);
153149b25e05SJeff Mahoney 		ret = btrfs_end_transaction(trans, root);
1532ccd467d6SChris Mason 
1533b9c8300cSLi Zefan 		wait_for_commit(root, cur_trans);
153415ee9bc7SJosef Bacik 
153579154b1bSChris Mason 		put_transaction(cur_trans);
153615ee9bc7SJosef Bacik 
153749b25e05SJeff Mahoney 		return ret;
153879154b1bSChris Mason 	}
15394313b399SChris Mason 
15402c90e5d6SChris Mason 	trans->transaction->in_commit = 1;
1541f9295749SChris Mason 	trans->transaction->blocked = 1;
1542a4abeea4SJosef Bacik 	spin_unlock(&cur_trans->commit_lock);
1543bb9c12c9SSage Weil 	wake_up(&root->fs_info->transaction_blocked_wait);
1544bb9c12c9SSage Weil 
1545a4abeea4SJosef Bacik 	spin_lock(&root->fs_info->trans_lock);
1546ccd467d6SChris Mason 	if (cur_trans->list.prev != &root->fs_info->trans_list) {
1547ccd467d6SChris Mason 		prev_trans = list_entry(cur_trans->list.prev,
1548ccd467d6SChris Mason 					struct btrfs_transaction, list);
1549ccd467d6SChris Mason 		if (!prev_trans->commit_done) {
155013c5a93eSJosef Bacik 			atomic_inc(&prev_trans->use_count);
1551a4abeea4SJosef Bacik 			spin_unlock(&root->fs_info->trans_lock);
1552ccd467d6SChris Mason 
1553ccd467d6SChris Mason 			wait_for_commit(root, prev_trans);
1554ccd467d6SChris Mason 
155515ee9bc7SJosef Bacik 			put_transaction(prev_trans);
1556a4abeea4SJosef Bacik 		} else {
1557a4abeea4SJosef Bacik 			spin_unlock(&root->fs_info->trans_lock);
1558ccd467d6SChris Mason 		}
1559a4abeea4SJosef Bacik 	} else {
1560a4abeea4SJosef Bacik 		spin_unlock(&root->fs_info->trans_lock);
1561ccd467d6SChris Mason 	}
156215ee9bc7SJosef Bacik 
1563e39e64acSChris Mason 	if (!btrfs_test_opt(root, SSD) &&
1564e39e64acSChris Mason 	    (now < cur_trans->start_time || now - cur_trans->start_time < 1))
156589573b9cSChris Mason 		should_grow = 1;
156689573b9cSChris Mason 
156715ee9bc7SJosef Bacik 	do {
156815ee9bc7SJosef Bacik 		joined = cur_trans->num_joined;
15697ea394f1SYan Zheng 
15702c90e5d6SChris Mason 		WARN_ON(cur_trans != trans->transaction);
157115ee9bc7SJosef Bacik 
1572ca469637SMiao Xie 		ret = btrfs_flush_all_pending_stuffs(trans, root);
157349b25e05SJeff Mahoney 		if (ret)
157449b25e05SJeff Mahoney 			goto cleanup_transaction;
157516cdcec7SMiao Xie 
1576ed3b3d31SChris Mason 		prepare_to_wait(&cur_trans->writer_wait, &wait,
1577ed3b3d31SChris Mason 				TASK_UNINTERRUPTIBLE);
1578ed3b3d31SChris Mason 
157913c5a93eSJosef Bacik 		if (atomic_read(&cur_trans->num_writers) > 1)
158099d16cbcSSage Weil 			schedule_timeout(MAX_SCHEDULE_TIMEOUT);
158199d16cbcSSage Weil 		else if (should_grow)
158299d16cbcSSage Weil 			schedule_timeout(1);
158315ee9bc7SJosef Bacik 
158415ee9bc7SJosef Bacik 		finish_wait(&cur_trans->writer_wait, &wait);
1585ed0ca140SJosef Bacik 	} while (atomic_read(&cur_trans->num_writers) > 1 ||
1586ed0ca140SJosef Bacik 		 (should_grow && cur_trans->num_joined != joined));
1587ed0ca140SJosef Bacik 
1588ca469637SMiao Xie 	ret = btrfs_flush_all_pending_stuffs(trans, root);
1589ca469637SMiao Xie 	if (ret)
1590ca469637SMiao Xie 		goto cleanup_transaction;
1591ca469637SMiao Xie 
1592ed0ca140SJosef Bacik 	/*
1593ed0ca140SJosef Bacik 	 * Ok now we need to make sure to block out any other joins while we
1594ed0ca140SJosef Bacik 	 * commit the transaction.  We could have started a join before setting
1595ed0ca140SJosef Bacik 	 * no_join so make sure to wait for num_writers to == 1 again.
1596ed0ca140SJosef Bacik 	 */
1597a4abeea4SJosef Bacik 	spin_lock(&root->fs_info->trans_lock);
1598a4abeea4SJosef Bacik 	root->fs_info->trans_no_join = 1;
1599a4abeea4SJosef Bacik 	spin_unlock(&root->fs_info->trans_lock);
1600ed0ca140SJosef Bacik 	wait_event(cur_trans->writer_wait,
1601ed0ca140SJosef Bacik 		   atomic_read(&cur_trans->num_writers) == 1);
160215ee9bc7SJosef Bacik 
16032cba30f1SMiao Xie 	/* ->aborted might be set after the previous check, so check it */
16042cba30f1SMiao Xie 	if (unlikely(ACCESS_ONCE(cur_trans->aborted))) {
16052cba30f1SMiao Xie 		ret = cur_trans->aborted;
16062cba30f1SMiao Xie 		goto cleanup_transaction;
16072cba30f1SMiao Xie 	}
16087585717fSChris Mason 	/*
16097585717fSChris Mason 	 * the reloc mutex makes sure that we stop
16107585717fSChris Mason 	 * the balancing code from coming in and moving
16117585717fSChris Mason 	 * extents around in the middle of the commit
16127585717fSChris Mason 	 */
16137585717fSChris Mason 	mutex_lock(&root->fs_info->reloc_mutex);
16147585717fSChris Mason 
161542874b3dSMiao Xie 	/*
161642874b3dSMiao Xie 	 * We needn't worry about the delayed items because we will
161742874b3dSMiao Xie 	 * deal with them in create_pending_snapshot(), which is the
161842874b3dSMiao Xie 	 * core function of the snapshot creation.
161942874b3dSMiao Xie 	 */
162042874b3dSMiao Xie 	ret = create_pending_snapshots(trans, root->fs_info);
162149b25e05SJeff Mahoney 	if (ret) {
162249b25e05SJeff Mahoney 		mutex_unlock(&root->fs_info->reloc_mutex);
162349b25e05SJeff Mahoney 		goto cleanup_transaction;
162449b25e05SJeff Mahoney 	}
16253063d29fSChris Mason 
162642874b3dSMiao Xie 	/*
162742874b3dSMiao Xie 	 * We insert the dir indexes of the snapshots and update the inode
162842874b3dSMiao Xie 	 * of the snapshots' parents after the snapshot creation, so there
162942874b3dSMiao Xie 	 * are some delayed items which are not dealt with. Now deal with
163042874b3dSMiao Xie 	 * them.
163142874b3dSMiao Xie 	 *
163242874b3dSMiao Xie 	 * We needn't worry that this operation will corrupt the snapshots,
163342874b3dSMiao Xie 	 * because all the tree which are snapshoted will be forced to COW
163442874b3dSMiao Xie 	 * the nodes and leaves.
163542874b3dSMiao Xie 	 */
163642874b3dSMiao Xie 	ret = btrfs_run_delayed_items(trans, root);
163749b25e05SJeff Mahoney 	if (ret) {
163849b25e05SJeff Mahoney 		mutex_unlock(&root->fs_info->reloc_mutex);
163949b25e05SJeff Mahoney 		goto cleanup_transaction;
164049b25e05SJeff Mahoney 	}
164116cdcec7SMiao Xie 
164256bec294SChris Mason 	ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
164349b25e05SJeff Mahoney 	if (ret) {
164449b25e05SJeff Mahoney 		mutex_unlock(&root->fs_info->reloc_mutex);
164549b25e05SJeff Mahoney 		goto cleanup_transaction;
164649b25e05SJeff Mahoney 	}
164756bec294SChris Mason 
1648e999376fSChris Mason 	/*
1649e999376fSChris Mason 	 * make sure none of the code above managed to slip in a
1650e999376fSChris Mason 	 * delayed item
1651e999376fSChris Mason 	 */
1652e999376fSChris Mason 	btrfs_assert_delayed_root_empty(root);
1653e999376fSChris Mason 
16542c90e5d6SChris Mason 	WARN_ON(cur_trans != trans->transaction);
1655dc17ff8fSChris Mason 
1656a2de733cSArne Jansen 	btrfs_scrub_pause(root);
1657e02119d5SChris Mason 	/* btrfs_commit_tree_roots is responsible for getting the
1658e02119d5SChris Mason 	 * various roots consistent with each other.  Every pointer
1659e02119d5SChris Mason 	 * in the tree of tree roots has to point to the most up to date
1660e02119d5SChris Mason 	 * root for every subvolume and other tree.  So, we have to keep
1661e02119d5SChris Mason 	 * the tree logging code from jumping in and changing any
1662e02119d5SChris Mason 	 * of the trees.
1663e02119d5SChris Mason 	 *
1664e02119d5SChris Mason 	 * At this point in the commit, there can't be any tree-log
1665e02119d5SChris Mason 	 * writers, but a little lower down we drop the trans mutex
1666e02119d5SChris Mason 	 * and let new people in.  By holding the tree_log_mutex
1667e02119d5SChris Mason 	 * from now until after the super is written, we avoid races
1668e02119d5SChris Mason 	 * with the tree-log code.
1669e02119d5SChris Mason 	 */
1670e02119d5SChris Mason 	mutex_lock(&root->fs_info->tree_log_mutex);
16711a40e23bSZheng Yan 
16725d4f98a2SYan Zheng 	ret = commit_fs_roots(trans, root);
167349b25e05SJeff Mahoney 	if (ret) {
167449b25e05SJeff Mahoney 		mutex_unlock(&root->fs_info->tree_log_mutex);
1675871383beSDavid Sterba 		mutex_unlock(&root->fs_info->reloc_mutex);
167649b25e05SJeff Mahoney 		goto cleanup_transaction;
167749b25e05SJeff Mahoney 	}
167854aa1f4dSChris Mason 
16795d4f98a2SYan Zheng 	/* commit_fs_roots gets rid of all the tree log roots, it is now
1680e02119d5SChris Mason 	 * safe to free the root of tree log roots
1681e02119d5SChris Mason 	 */
1682e02119d5SChris Mason 	btrfs_free_log_root_tree(trans, root->fs_info);
1683e02119d5SChris Mason 
16845d4f98a2SYan Zheng 	ret = commit_cowonly_roots(trans, root);
168549b25e05SJeff Mahoney 	if (ret) {
168649b25e05SJeff Mahoney 		mutex_unlock(&root->fs_info->tree_log_mutex);
1687871383beSDavid Sterba 		mutex_unlock(&root->fs_info->reloc_mutex);
168849b25e05SJeff Mahoney 		goto cleanup_transaction;
168949b25e05SJeff Mahoney 	}
169054aa1f4dSChris Mason 
16912cba30f1SMiao Xie 	/*
16922cba30f1SMiao Xie 	 * The tasks which save the space cache and inode cache may also
16932cba30f1SMiao Xie 	 * update ->aborted, check it.
16942cba30f1SMiao Xie 	 */
16952cba30f1SMiao Xie 	if (unlikely(ACCESS_ONCE(cur_trans->aborted))) {
16962cba30f1SMiao Xie 		ret = cur_trans->aborted;
16972cba30f1SMiao Xie 		mutex_unlock(&root->fs_info->tree_log_mutex);
16982cba30f1SMiao Xie 		mutex_unlock(&root->fs_info->reloc_mutex);
16992cba30f1SMiao Xie 		goto cleanup_transaction;
17002cba30f1SMiao Xie 	}
17012cba30f1SMiao Xie 
170211833d66SYan Zheng 	btrfs_prepare_extent_commit(trans, root);
170311833d66SYan Zheng 
170478fae27eSChris Mason 	cur_trans = root->fs_info->running_transaction;
17055f39d397SChris Mason 
17065d4f98a2SYan Zheng 	btrfs_set_root_node(&root->fs_info->tree_root->root_item,
17075d4f98a2SYan Zheng 			    root->fs_info->tree_root->node);
1708817d52f8SJosef Bacik 	switch_commit_root(root->fs_info->tree_root);
17095d4f98a2SYan Zheng 
17105d4f98a2SYan Zheng 	btrfs_set_root_node(&root->fs_info->chunk_root->root_item,
17115d4f98a2SYan Zheng 			    root->fs_info->chunk_root->node);
1712817d52f8SJosef Bacik 	switch_commit_root(root->fs_info->chunk_root);
17135d4f98a2SYan Zheng 
1714edf39272SJan Schmidt 	assert_qgroups_uptodate(trans);
17155d4f98a2SYan Zheng 	update_super_roots(root);
1716e02119d5SChris Mason 
1717e02119d5SChris Mason 	if (!root->fs_info->log_root_recovering) {
17186c41761fSDavid Sterba 		btrfs_set_super_log_root(root->fs_info->super_copy, 0);
17196c41761fSDavid Sterba 		btrfs_set_super_log_root_level(root->fs_info->super_copy, 0);
1720e02119d5SChris Mason 	}
1721e02119d5SChris Mason 
17226c41761fSDavid Sterba 	memcpy(root->fs_info->super_for_commit, root->fs_info->super_copy,
17236c41761fSDavid Sterba 	       sizeof(*root->fs_info->super_copy));
1724ccd467d6SChris Mason 
1725f9295749SChris Mason 	trans->transaction->blocked = 0;
1726a4abeea4SJosef Bacik 	spin_lock(&root->fs_info->trans_lock);
1727a4abeea4SJosef Bacik 	root->fs_info->running_transaction = NULL;
1728a4abeea4SJosef Bacik 	root->fs_info->trans_no_join = 0;
1729a4abeea4SJosef Bacik 	spin_unlock(&root->fs_info->trans_lock);
17307585717fSChris Mason 	mutex_unlock(&root->fs_info->reloc_mutex);
1731b7ec40d7SChris Mason 
1732f9295749SChris Mason 	wake_up(&root->fs_info->transaction_wait);
1733e6dcd2dcSChris Mason 
173479154b1bSChris Mason 	ret = btrfs_write_and_wait_transaction(trans, root);
173549b25e05SJeff Mahoney 	if (ret) {
173649b25e05SJeff Mahoney 		btrfs_error(root->fs_info, ret,
173749b25e05SJeff Mahoney 			    "Error while writing out transaction.");
173849b25e05SJeff Mahoney 		mutex_unlock(&root->fs_info->tree_log_mutex);
173949b25e05SJeff Mahoney 		goto cleanup_transaction;
174049b25e05SJeff Mahoney 	}
174149b25e05SJeff Mahoney 
174249b25e05SJeff Mahoney 	ret = write_ctree_super(trans, root, 0);
174349b25e05SJeff Mahoney 	if (ret) {
174449b25e05SJeff Mahoney 		mutex_unlock(&root->fs_info->tree_log_mutex);
174549b25e05SJeff Mahoney 		goto cleanup_transaction;
174649b25e05SJeff Mahoney 	}
17474313b399SChris Mason 
1748e02119d5SChris Mason 	/*
1749e02119d5SChris Mason 	 * the super is written, we can safely allow the tree-loggers
1750e02119d5SChris Mason 	 * to go about their business
1751e02119d5SChris Mason 	 */
1752e02119d5SChris Mason 	mutex_unlock(&root->fs_info->tree_log_mutex);
1753e02119d5SChris Mason 
175411833d66SYan Zheng 	btrfs_finish_extent_commit(trans, root);
17554313b399SChris Mason 
17562c90e5d6SChris Mason 	cur_trans->commit_done = 1;
1757b7ec40d7SChris Mason 
175815ee9bc7SJosef Bacik 	root->fs_info->last_trans_committed = cur_trans->transid;
1759817d52f8SJosef Bacik 
17602c90e5d6SChris Mason 	wake_up(&cur_trans->commit_wait);
17613de4586cSChris Mason 
1762a4abeea4SJosef Bacik 	spin_lock(&root->fs_info->trans_lock);
176313c5a93eSJosef Bacik 	list_del_init(&cur_trans->list);
1764a4abeea4SJosef Bacik 	spin_unlock(&root->fs_info->trans_lock);
1765a4abeea4SJosef Bacik 
176679154b1bSChris Mason 	put_transaction(cur_trans);
176778fae27eSChris Mason 	put_transaction(cur_trans);
176858176a96SJosef Bacik 
1769354aa0fbSMiao Xie 	if (trans->type < TRANS_JOIN_NOLOCK)
1770b2b5ef5cSJan Kara 		sb_end_intwrite(root->fs_info->sb);
1771b2b5ef5cSJan Kara 
17721abe9b8aSliubo 	trace_btrfs_transaction_commit(root);
17731abe9b8aSliubo 
1774a2de733cSArne Jansen 	btrfs_scrub_continue(root);
1775a2de733cSArne Jansen 
17769ed74f2dSJosef Bacik 	if (current->journal_info == trans)
17779ed74f2dSJosef Bacik 		current->journal_info = NULL;
17789ed74f2dSJosef Bacik 
17792c90e5d6SChris Mason 	kmem_cache_free(btrfs_trans_handle_cachep, trans);
178024bbcf04SYan, Zheng 
178124bbcf04SYan, Zheng 	if (current != root->fs_info->transaction_kthread)
178224bbcf04SYan, Zheng 		btrfs_run_delayed_iputs(root);
178324bbcf04SYan, Zheng 
178479154b1bSChris Mason 	return ret;
178549b25e05SJeff Mahoney 
178649b25e05SJeff Mahoney cleanup_transaction:
17870e721106SJosef Bacik 	btrfs_trans_release_metadata(trans, root);
17880e721106SJosef Bacik 	trans->block_rsv = NULL;
178949b25e05SJeff Mahoney 	btrfs_printk(root->fs_info, "Skipping commit of aborted transaction.\n");
179049b25e05SJeff Mahoney //	WARN_ON(1);
179149b25e05SJeff Mahoney 	if (current->journal_info == trans)
179249b25e05SJeff Mahoney 		current->journal_info = NULL;
17937b8b92afSJosef Bacik 	cleanup_transaction(trans, root, ret);
179449b25e05SJeff Mahoney 
179549b25e05SJeff Mahoney 	return ret;
179679154b1bSChris Mason }
179779154b1bSChris Mason 
1798d352ac68SChris Mason /*
1799d352ac68SChris Mason  * interface function to delete all the snapshots we have scheduled for deletion
1800d352ac68SChris Mason  */
1801e9d0b13bSChris Mason int btrfs_clean_old_snapshots(struct btrfs_root *root)
1802e9d0b13bSChris Mason {
18035d4f98a2SYan Zheng 	LIST_HEAD(list);
18045d4f98a2SYan Zheng 	struct btrfs_fs_info *fs_info = root->fs_info;
1805e9d0b13bSChris Mason 
1806a4abeea4SJosef Bacik 	spin_lock(&fs_info->trans_lock);
18075d4f98a2SYan Zheng 	list_splice_init(&fs_info->dead_roots, &list);
1808a4abeea4SJosef Bacik 	spin_unlock(&fs_info->trans_lock);
18095d4f98a2SYan Zheng 
18105d4f98a2SYan Zheng 	while (!list_empty(&list)) {
18112c536799SJeff Mahoney 		int ret;
18122c536799SJeff Mahoney 
18135d4f98a2SYan Zheng 		root = list_entry(list.next, struct btrfs_root, root_list);
181476dda93cSYan, Zheng 		list_del(&root->root_list);
181576dda93cSYan, Zheng 
181616cdcec7SMiao Xie 		btrfs_kill_all_delayed_nodes(root);
181716cdcec7SMiao Xie 
181876dda93cSYan, Zheng 		if (btrfs_header_backref_rev(root->node) <
181976dda93cSYan, Zheng 		    BTRFS_MIXED_BACKREF_REV)
18202c536799SJeff Mahoney 			ret = btrfs_drop_snapshot(root, NULL, 0, 0);
182176dda93cSYan, Zheng 		else
18222c536799SJeff Mahoney 			ret =btrfs_drop_snapshot(root, NULL, 1, 0);
18232c536799SJeff Mahoney 		BUG_ON(ret < 0);
1824e9d0b13bSChris Mason 	}
1825e9d0b13bSChris Mason 	return 0;
1826e9d0b13bSChris Mason }
1827