xref: /openbmc/linux/fs/btrfs/transaction.c (revision 9d1a2a3a)
16cbd5570SChris Mason /*
26cbd5570SChris Mason  * Copyright (C) 2007 Oracle.  All rights reserved.
36cbd5570SChris Mason  *
46cbd5570SChris Mason  * This program is free software; you can redistribute it and/or
56cbd5570SChris Mason  * modify it under the terms of the GNU General Public
66cbd5570SChris Mason  * License v2 as published by the Free Software Foundation.
76cbd5570SChris Mason  *
86cbd5570SChris Mason  * This program is distributed in the hope that it will be useful,
96cbd5570SChris Mason  * but WITHOUT ANY WARRANTY; without even the implied warranty of
106cbd5570SChris Mason  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
116cbd5570SChris Mason  * General Public License for more details.
126cbd5570SChris Mason  *
136cbd5570SChris Mason  * You should have received a copy of the GNU General Public
146cbd5570SChris Mason  * License along with this program; if not, write to the
156cbd5570SChris Mason  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
166cbd5570SChris Mason  * Boston, MA 021110-1307, USA.
176cbd5570SChris Mason  */
186cbd5570SChris Mason 
1979154b1bSChris Mason #include <linux/fs.h>
205a0e3ad6STejun Heo #include <linux/slab.h>
2134088780SChris Mason #include <linux/sched.h>
22d3c2fdcfSChris Mason #include <linux/writeback.h>
235f39d397SChris Mason #include <linux/pagemap.h>
245f2cc086SChris Mason #include <linux/blkdev.h>
258ea05e3aSAlexander Block #include <linux/uuid.h>
2679154b1bSChris Mason #include "ctree.h"
2779154b1bSChris Mason #include "disk-io.h"
2879154b1bSChris Mason #include "transaction.h"
29925baeddSChris Mason #include "locking.h"
30e02119d5SChris Mason #include "tree-log.h"
31581bb050SLi Zefan #include "inode-map.h"
32733f4fbbSStefan Behrens #include "volumes.h"
338dabb742SStefan Behrens #include "dev-replace.h"
3479154b1bSChris Mason 
350f7d52f4SChris Mason #define BTRFS_ROOT_TRANS_TAG 0
360f7d52f4SChris Mason 
3749b25e05SJeff Mahoney void put_transaction(struct btrfs_transaction *transaction)
3879154b1bSChris Mason {
3913c5a93eSJosef Bacik 	WARN_ON(atomic_read(&transaction->use_count) == 0);
4013c5a93eSJosef Bacik 	if (atomic_dec_and_test(&transaction->use_count)) {
41a4abeea4SJosef Bacik 		BUG_ON(!list_empty(&transaction->list));
4200f04b88SArne Jansen 		WARN_ON(transaction->delayed_refs.root.rb_node);
432c90e5d6SChris Mason 		kmem_cache_free(btrfs_transaction_cachep, transaction);
4479154b1bSChris Mason 	}
4578fae27eSChris Mason }
4679154b1bSChris Mason 
47817d52f8SJosef Bacik static noinline void switch_commit_root(struct btrfs_root *root)
48817d52f8SJosef Bacik {
49817d52f8SJosef Bacik 	free_extent_buffer(root->commit_root);
50817d52f8SJosef Bacik 	root->commit_root = btrfs_root_node(root);
51817d52f8SJosef Bacik }
52817d52f8SJosef Bacik 
53178260b2SMiao Xie static inline int can_join_transaction(struct btrfs_transaction *trans,
54178260b2SMiao Xie 				       int type)
55178260b2SMiao Xie {
56178260b2SMiao Xie 	return !(trans->in_commit &&
57178260b2SMiao Xie 		 type != TRANS_JOIN &&
58178260b2SMiao Xie 		 type != TRANS_JOIN_NOLOCK);
59178260b2SMiao Xie }
60178260b2SMiao Xie 
61d352ac68SChris Mason /*
62d352ac68SChris Mason  * either allocate a new transaction or hop into the existing one
63d352ac68SChris Mason  */
64354aa0fbSMiao Xie static noinline int join_transaction(struct btrfs_root *root, int type)
6579154b1bSChris Mason {
6679154b1bSChris Mason 	struct btrfs_transaction *cur_trans;
6719ae4e81SJan Schmidt 	struct btrfs_fs_info *fs_info = root->fs_info;
68a4abeea4SJosef Bacik 
6919ae4e81SJan Schmidt 	spin_lock(&fs_info->trans_lock);
70d43317dcSChris Mason loop:
7149b25e05SJeff Mahoney 	/* The file system has been taken offline. No new transactions. */
7287533c47SMiao Xie 	if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) {
7319ae4e81SJan Schmidt 		spin_unlock(&fs_info->trans_lock);
7449b25e05SJeff Mahoney 		return -EROFS;
7549b25e05SJeff Mahoney 	}
7649b25e05SJeff Mahoney 
7719ae4e81SJan Schmidt 	if (fs_info->trans_no_join) {
78354aa0fbSMiao Xie 		/*
79354aa0fbSMiao Xie 		 * If we are JOIN_NOLOCK we're already committing a current
80354aa0fbSMiao Xie 		 * transaction, we just need a handle to deal with something
81354aa0fbSMiao Xie 		 * when committing the transaction, such as inode cache and
82354aa0fbSMiao Xie 		 * space cache. It is a special case.
83354aa0fbSMiao Xie 		 */
84354aa0fbSMiao Xie 		if (type != TRANS_JOIN_NOLOCK) {
8519ae4e81SJan Schmidt 			spin_unlock(&fs_info->trans_lock);
86a4abeea4SJosef Bacik 			return -EBUSY;
87a4abeea4SJosef Bacik 		}
88a4abeea4SJosef Bacik 	}
89a4abeea4SJosef Bacik 
9019ae4e81SJan Schmidt 	cur_trans = fs_info->running_transaction;
91a4abeea4SJosef Bacik 	if (cur_trans) {
92871383beSDavid Sterba 		if (cur_trans->aborted) {
9319ae4e81SJan Schmidt 			spin_unlock(&fs_info->trans_lock);
9449b25e05SJeff Mahoney 			return cur_trans->aborted;
95871383beSDavid Sterba 		}
96178260b2SMiao Xie 		if (!can_join_transaction(cur_trans, type)) {
97178260b2SMiao Xie 			spin_unlock(&fs_info->trans_lock);
98178260b2SMiao Xie 			return -EBUSY;
99178260b2SMiao Xie 		}
100a4abeea4SJosef Bacik 		atomic_inc(&cur_trans->use_count);
101a4abeea4SJosef Bacik 		atomic_inc(&cur_trans->num_writers);
102a4abeea4SJosef Bacik 		cur_trans->num_joined++;
10319ae4e81SJan Schmidt 		spin_unlock(&fs_info->trans_lock);
104a4abeea4SJosef Bacik 		return 0;
105a4abeea4SJosef Bacik 	}
10619ae4e81SJan Schmidt 	spin_unlock(&fs_info->trans_lock);
107a4abeea4SJosef Bacik 
108354aa0fbSMiao Xie 	/*
109354aa0fbSMiao Xie 	 * If we are ATTACH, we just want to catch the current transaction,
110354aa0fbSMiao Xie 	 * and commit it. If there is no transaction, just return ENOENT.
111354aa0fbSMiao Xie 	 */
112354aa0fbSMiao Xie 	if (type == TRANS_ATTACH)
113354aa0fbSMiao Xie 		return -ENOENT;
114354aa0fbSMiao Xie 
115a4abeea4SJosef Bacik 	cur_trans = kmem_cache_alloc(btrfs_transaction_cachep, GFP_NOFS);
116db5b493aSTsutomu Itoh 	if (!cur_trans)
117db5b493aSTsutomu Itoh 		return -ENOMEM;
118d43317dcSChris Mason 
11919ae4e81SJan Schmidt 	spin_lock(&fs_info->trans_lock);
12019ae4e81SJan Schmidt 	if (fs_info->running_transaction) {
121d43317dcSChris Mason 		/*
122d43317dcSChris Mason 		 * someone started a transaction after we unlocked.  Make sure
123d43317dcSChris Mason 		 * to redo the trans_no_join checks above
124d43317dcSChris Mason 		 */
125a4abeea4SJosef Bacik 		kmem_cache_free(btrfs_transaction_cachep, cur_trans);
126d43317dcSChris Mason 		goto loop;
12787533c47SMiao Xie 	} else if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) {
128e4b50e14SDan Carpenter 		spin_unlock(&fs_info->trans_lock);
1297b8b92afSJosef Bacik 		kmem_cache_free(btrfs_transaction_cachep, cur_trans);
1307b8b92afSJosef Bacik 		return -EROFS;
131a4abeea4SJosef Bacik 	}
132d43317dcSChris Mason 
13313c5a93eSJosef Bacik 	atomic_set(&cur_trans->num_writers, 1);
13415ee9bc7SJosef Bacik 	cur_trans->num_joined = 0;
13579154b1bSChris Mason 	init_waitqueue_head(&cur_trans->writer_wait);
13679154b1bSChris Mason 	init_waitqueue_head(&cur_trans->commit_wait);
13779154b1bSChris Mason 	cur_trans->in_commit = 0;
138f9295749SChris Mason 	cur_trans->blocked = 0;
139a4abeea4SJosef Bacik 	/*
140a4abeea4SJosef Bacik 	 * One for this trans handle, one so it will live on until we
141a4abeea4SJosef Bacik 	 * commit the transaction.
142a4abeea4SJosef Bacik 	 */
143a4abeea4SJosef Bacik 	atomic_set(&cur_trans->use_count, 2);
14479154b1bSChris Mason 	cur_trans->commit_done = 0;
14508607c1bSChris Mason 	cur_trans->start_time = get_seconds();
14656bec294SChris Mason 
1476bef4d31SEric Paris 	cur_trans->delayed_refs.root = RB_ROOT;
14856bec294SChris Mason 	cur_trans->delayed_refs.num_entries = 0;
149c3e69d58SChris Mason 	cur_trans->delayed_refs.num_heads_ready = 0;
150c3e69d58SChris Mason 	cur_trans->delayed_refs.num_heads = 0;
15156bec294SChris Mason 	cur_trans->delayed_refs.flushing = 0;
152c3e69d58SChris Mason 	cur_trans->delayed_refs.run_delayed_start = 0;
15320b297d6SJan Schmidt 
15420b297d6SJan Schmidt 	/*
15520b297d6SJan Schmidt 	 * although the tree mod log is per file system and not per transaction,
15620b297d6SJan Schmidt 	 * the log must never go across transaction boundaries.
15720b297d6SJan Schmidt 	 */
15820b297d6SJan Schmidt 	smp_mb();
15931b1a2bdSJulia Lawall 	if (!list_empty(&fs_info->tree_mod_seq_list))
16031b1a2bdSJulia Lawall 		WARN(1, KERN_ERR "btrfs: tree_mod_seq_list not empty when "
16120b297d6SJan Schmidt 			"creating a fresh transaction\n");
16231b1a2bdSJulia Lawall 	if (!RB_EMPTY_ROOT(&fs_info->tree_mod_log))
16331b1a2bdSJulia Lawall 		WARN(1, KERN_ERR "btrfs: tree_mod_log rb tree not empty when "
16420b297d6SJan Schmidt 			"creating a fresh transaction\n");
16520b297d6SJan Schmidt 	atomic_set(&fs_info->tree_mod_seq, 0);
16620b297d6SJan Schmidt 
167a4abeea4SJosef Bacik 	spin_lock_init(&cur_trans->commit_lock);
16856bec294SChris Mason 	spin_lock_init(&cur_trans->delayed_refs.lock);
169bb721703SChris Mason 	atomic_set(&cur_trans->delayed_refs.procs_running_refs, 0);
170bb721703SChris Mason 	atomic_set(&cur_trans->delayed_refs.ref_seq, 0);
171bb721703SChris Mason 	init_waitqueue_head(&cur_trans->delayed_refs.wait);
17256bec294SChris Mason 
1733063d29fSChris Mason 	INIT_LIST_HEAD(&cur_trans->pending_snapshots);
174569e0f35SJosef Bacik 	INIT_LIST_HEAD(&cur_trans->ordered_operations);
17519ae4e81SJan Schmidt 	list_add_tail(&cur_trans->list, &fs_info->trans_list);
176d1310b2eSChris Mason 	extent_io_tree_init(&cur_trans->dirty_pages,
17719ae4e81SJan Schmidt 			     fs_info->btree_inode->i_mapping);
17819ae4e81SJan Schmidt 	fs_info->generation++;
17919ae4e81SJan Schmidt 	cur_trans->transid = fs_info->generation;
18019ae4e81SJan Schmidt 	fs_info->running_transaction = cur_trans;
18149b25e05SJeff Mahoney 	cur_trans->aborted = 0;
18219ae4e81SJan Schmidt 	spin_unlock(&fs_info->trans_lock);
18315ee9bc7SJosef Bacik 
18479154b1bSChris Mason 	return 0;
18579154b1bSChris Mason }
18679154b1bSChris Mason 
187d352ac68SChris Mason /*
188d397712bSChris Mason  * this does all the record keeping required to make sure that a reference
189d397712bSChris Mason  * counted root is properly recorded in a given transaction.  This is required
190d397712bSChris Mason  * to make sure the old root from before we joined the transaction is deleted
191d397712bSChris Mason  * when the transaction commits
192d352ac68SChris Mason  */
1937585717fSChris Mason static int record_root_in_trans(struct btrfs_trans_handle *trans,
1945d4f98a2SYan Zheng 			       struct btrfs_root *root)
1956702ed49SChris Mason {
1965d4f98a2SYan Zheng 	if (root->ref_cows && root->last_trans < trans->transid) {
1976702ed49SChris Mason 		WARN_ON(root == root->fs_info->extent_root);
1985d4f98a2SYan Zheng 		WARN_ON(root->commit_root != root->node);
1995d4f98a2SYan Zheng 
2007585717fSChris Mason 		/*
2017585717fSChris Mason 		 * see below for in_trans_setup usage rules
2027585717fSChris Mason 		 * we have the reloc mutex held now, so there
2037585717fSChris Mason 		 * is only one writer in this function
2047585717fSChris Mason 		 */
2057585717fSChris Mason 		root->in_trans_setup = 1;
2067585717fSChris Mason 
2077585717fSChris Mason 		/* make sure readers find in_trans_setup before
2087585717fSChris Mason 		 * they find our root->last_trans update
2097585717fSChris Mason 		 */
2107585717fSChris Mason 		smp_wmb();
2117585717fSChris Mason 
212a4abeea4SJosef Bacik 		spin_lock(&root->fs_info->fs_roots_radix_lock);
213a4abeea4SJosef Bacik 		if (root->last_trans == trans->transid) {
214a4abeea4SJosef Bacik 			spin_unlock(&root->fs_info->fs_roots_radix_lock);
215a4abeea4SJosef Bacik 			return 0;
216a4abeea4SJosef Bacik 		}
2176702ed49SChris Mason 		radix_tree_tag_set(&root->fs_info->fs_roots_radix,
2186702ed49SChris Mason 			   (unsigned long)root->root_key.objectid,
2196702ed49SChris Mason 			   BTRFS_ROOT_TRANS_TAG);
220a4abeea4SJosef Bacik 		spin_unlock(&root->fs_info->fs_roots_radix_lock);
2217585717fSChris Mason 		root->last_trans = trans->transid;
2227585717fSChris Mason 
2237585717fSChris Mason 		/* this is pretty tricky.  We don't want to
2247585717fSChris Mason 		 * take the relocation lock in btrfs_record_root_in_trans
2257585717fSChris Mason 		 * unless we're really doing the first setup for this root in
2267585717fSChris Mason 		 * this transaction.
2277585717fSChris Mason 		 *
2287585717fSChris Mason 		 * Normally we'd use root->last_trans as a flag to decide
2297585717fSChris Mason 		 * if we want to take the expensive mutex.
2307585717fSChris Mason 		 *
2317585717fSChris Mason 		 * But, we have to set root->last_trans before we
2327585717fSChris Mason 		 * init the relocation root, otherwise, we trip over warnings
2337585717fSChris Mason 		 * in ctree.c.  The solution used here is to flag ourselves
2347585717fSChris Mason 		 * with root->in_trans_setup.  When this is 1, we're still
2357585717fSChris Mason 		 * fixing up the reloc trees and everyone must wait.
2367585717fSChris Mason 		 *
2377585717fSChris Mason 		 * When this is zero, they can trust root->last_trans and fly
2387585717fSChris Mason 		 * through btrfs_record_root_in_trans without having to take the
2397585717fSChris Mason 		 * lock.  smp_wmb() makes sure that all the writes above are
2407585717fSChris Mason 		 * done before we pop in the zero below
2417585717fSChris Mason 		 */
2425d4f98a2SYan Zheng 		btrfs_init_reloc_root(trans, root);
2437585717fSChris Mason 		smp_wmb();
2447585717fSChris Mason 		root->in_trans_setup = 0;
2456702ed49SChris Mason 	}
2465d4f98a2SYan Zheng 	return 0;
2476702ed49SChris Mason }
2485d4f98a2SYan Zheng 
2497585717fSChris Mason 
2507585717fSChris Mason int btrfs_record_root_in_trans(struct btrfs_trans_handle *trans,
2517585717fSChris Mason 			       struct btrfs_root *root)
2527585717fSChris Mason {
2537585717fSChris Mason 	if (!root->ref_cows)
2547585717fSChris Mason 		return 0;
2557585717fSChris Mason 
2567585717fSChris Mason 	/*
2577585717fSChris Mason 	 * see record_root_in_trans for comments about in_trans_setup usage
2587585717fSChris Mason 	 * and barriers
2597585717fSChris Mason 	 */
2607585717fSChris Mason 	smp_rmb();
2617585717fSChris Mason 	if (root->last_trans == trans->transid &&
2627585717fSChris Mason 	    !root->in_trans_setup)
2637585717fSChris Mason 		return 0;
2647585717fSChris Mason 
2657585717fSChris Mason 	mutex_lock(&root->fs_info->reloc_mutex);
2667585717fSChris Mason 	record_root_in_trans(trans, root);
2677585717fSChris Mason 	mutex_unlock(&root->fs_info->reloc_mutex);
2687585717fSChris Mason 
2697585717fSChris Mason 	return 0;
2707585717fSChris Mason }
2717585717fSChris Mason 
272d352ac68SChris Mason /* wait for commit against the current transaction to become unblocked
273d352ac68SChris Mason  * when this is done, it is safe to start a new transaction, but the current
274d352ac68SChris Mason  * transaction might not be fully on disk.
275d352ac68SChris Mason  */
27637d1aeeeSChris Mason static void wait_current_trans(struct btrfs_root *root)
27779154b1bSChris Mason {
278f9295749SChris Mason 	struct btrfs_transaction *cur_trans;
27979154b1bSChris Mason 
280a4abeea4SJosef Bacik 	spin_lock(&root->fs_info->trans_lock);
281f9295749SChris Mason 	cur_trans = root->fs_info->running_transaction;
28237d1aeeeSChris Mason 	if (cur_trans && cur_trans->blocked) {
28313c5a93eSJosef Bacik 		atomic_inc(&cur_trans->use_count);
284a4abeea4SJosef Bacik 		spin_unlock(&root->fs_info->trans_lock);
28572d63ed6SLi Zefan 
28672d63ed6SLi Zefan 		wait_event(root->fs_info->transaction_wait,
28772d63ed6SLi Zefan 			   !cur_trans->blocked);
288f9295749SChris Mason 		put_transaction(cur_trans);
289a4abeea4SJosef Bacik 	} else {
290a4abeea4SJosef Bacik 		spin_unlock(&root->fs_info->trans_lock);
291f9295749SChris Mason 	}
29237d1aeeeSChris Mason }
29337d1aeeeSChris Mason 
294a22285a6SYan, Zheng static int may_wait_transaction(struct btrfs_root *root, int type)
29537d1aeeeSChris Mason {
296a4abeea4SJosef Bacik 	if (root->fs_info->log_root_recovering)
297a4abeea4SJosef Bacik 		return 0;
298a4abeea4SJosef Bacik 
299a4abeea4SJosef Bacik 	if (type == TRANS_USERSPACE)
300a22285a6SYan, Zheng 		return 1;
301a4abeea4SJosef Bacik 
302a4abeea4SJosef Bacik 	if (type == TRANS_START &&
303a4abeea4SJosef Bacik 	    !atomic_read(&root->fs_info->open_ioctl_trans))
304a4abeea4SJosef Bacik 		return 1;
305a4abeea4SJosef Bacik 
306a22285a6SYan, Zheng 	return 0;
307a22285a6SYan, Zheng }
308a22285a6SYan, Zheng 
30908e007d2SMiao Xie static struct btrfs_trans_handle *
31008e007d2SMiao Xie start_transaction(struct btrfs_root *root, u64 num_items, int type,
31108e007d2SMiao Xie 		  enum btrfs_reserve_flush_enum flush)
312a22285a6SYan, Zheng {
313a22285a6SYan, Zheng 	struct btrfs_trans_handle *h;
314a22285a6SYan, Zheng 	struct btrfs_transaction *cur_trans;
315b5009945SJosef Bacik 	u64 num_bytes = 0;
316a22285a6SYan, Zheng 	int ret;
317c5567237SArne Jansen 	u64 qgroup_reserved = 0;
318acce952bSliubo 
31987533c47SMiao Xie 	if (test_bit(BTRFS_FS_STATE_ERROR, &root->fs_info->fs_state))
320acce952bSliubo 		return ERR_PTR(-EROFS);
3212a1eb461SJosef Bacik 
3222a1eb461SJosef Bacik 	if (current->journal_info) {
3232a1eb461SJosef Bacik 		WARN_ON(type != TRANS_JOIN && type != TRANS_JOIN_NOLOCK);
3242a1eb461SJosef Bacik 		h = current->journal_info;
3252a1eb461SJosef Bacik 		h->use_count++;
326b7d5b0a8SMiao Xie 		WARN_ON(h->use_count > 2);
3272a1eb461SJosef Bacik 		h->orig_rsv = h->block_rsv;
3282a1eb461SJosef Bacik 		h->block_rsv = NULL;
3292a1eb461SJosef Bacik 		goto got_it;
3302a1eb461SJosef Bacik 	}
331b5009945SJosef Bacik 
332b5009945SJosef Bacik 	/*
333b5009945SJosef Bacik 	 * Do the reservation before we join the transaction so we can do all
334b5009945SJosef Bacik 	 * the appropriate flushing if need be.
335b5009945SJosef Bacik 	 */
336b5009945SJosef Bacik 	if (num_items > 0 && root != root->fs_info->chunk_root) {
337c5567237SArne Jansen 		if (root->fs_info->quota_enabled &&
338c5567237SArne Jansen 		    is_fstree(root->root_key.objectid)) {
339c5567237SArne Jansen 			qgroup_reserved = num_items * root->leafsize;
340c5567237SArne Jansen 			ret = btrfs_qgroup_reserve(root, qgroup_reserved);
341c5567237SArne Jansen 			if (ret)
342c5567237SArne Jansen 				return ERR_PTR(ret);
343c5567237SArne Jansen 		}
344c5567237SArne Jansen 
345b5009945SJosef Bacik 		num_bytes = btrfs_calc_trans_metadata_size(root, num_items);
3464a92b1b8SJosef Bacik 		ret = btrfs_block_rsv_add(root,
347b5009945SJosef Bacik 					  &root->fs_info->trans_block_rsv,
34808e007d2SMiao Xie 					  num_bytes, flush);
349b5009945SJosef Bacik 		if (ret)
350843fcf35SMiao Xie 			goto reserve_fail;
351b5009945SJosef Bacik 	}
352a22285a6SYan, Zheng again:
353a22285a6SYan, Zheng 	h = kmem_cache_alloc(btrfs_trans_handle_cachep, GFP_NOFS);
354843fcf35SMiao Xie 	if (!h) {
355843fcf35SMiao Xie 		ret = -ENOMEM;
356843fcf35SMiao Xie 		goto alloc_fail;
357843fcf35SMiao Xie 	}
358a22285a6SYan, Zheng 
35998114659SJosef Bacik 	/*
36098114659SJosef Bacik 	 * If we are JOIN_NOLOCK we're already committing a transaction and
36198114659SJosef Bacik 	 * waiting on this guy, so we don't need to do the sb_start_intwrite
36298114659SJosef Bacik 	 * because we're already holding a ref.  We need this because we could
36398114659SJosef Bacik 	 * have raced in and did an fsync() on a file which can kick a commit
36498114659SJosef Bacik 	 * and then we deadlock with somebody doing a freeze.
365354aa0fbSMiao Xie 	 *
366354aa0fbSMiao Xie 	 * If we are ATTACH, it means we just want to catch the current
367354aa0fbSMiao Xie 	 * transaction and commit it, so we needn't do sb_start_intwrite().
36898114659SJosef Bacik 	 */
369354aa0fbSMiao Xie 	if (type < TRANS_JOIN_NOLOCK)
370b2b5ef5cSJan Kara 		sb_start_intwrite(root->fs_info->sb);
371b2b5ef5cSJan Kara 
372a22285a6SYan, Zheng 	if (may_wait_transaction(root, type))
37337d1aeeeSChris Mason 		wait_current_trans(root);
374a22285a6SYan, Zheng 
375a4abeea4SJosef Bacik 	do {
376354aa0fbSMiao Xie 		ret = join_transaction(root, type);
377178260b2SMiao Xie 		if (ret == -EBUSY) {
378a4abeea4SJosef Bacik 			wait_current_trans(root);
379178260b2SMiao Xie 			if (unlikely(type == TRANS_ATTACH))
380178260b2SMiao Xie 				ret = -ENOENT;
381178260b2SMiao Xie 		}
382a4abeea4SJosef Bacik 	} while (ret == -EBUSY);
383a4abeea4SJosef Bacik 
384db5b493aSTsutomu Itoh 	if (ret < 0) {
385354aa0fbSMiao Xie 		/* We must get the transaction if we are JOIN_NOLOCK. */
386354aa0fbSMiao Xie 		BUG_ON(type == TRANS_JOIN_NOLOCK);
387843fcf35SMiao Xie 		goto join_fail;
388db5b493aSTsutomu Itoh 	}
3890f7d52f4SChris Mason 
390a22285a6SYan, Zheng 	cur_trans = root->fs_info->running_transaction;
391a22285a6SYan, Zheng 
392a22285a6SYan, Zheng 	h->transid = cur_trans->transid;
393a22285a6SYan, Zheng 	h->transaction = cur_trans;
39479154b1bSChris Mason 	h->blocks_used = 0;
395a22285a6SYan, Zheng 	h->bytes_reserved = 0;
396d13603efSArne Jansen 	h->root = root;
39756bec294SChris Mason 	h->delayed_ref_updates = 0;
3982a1eb461SJosef Bacik 	h->use_count = 1;
3990e721106SJosef Bacik 	h->adding_csums = 0;
400f0486c68SYan, Zheng 	h->block_rsv = NULL;
4012a1eb461SJosef Bacik 	h->orig_rsv = NULL;
40249b25e05SJeff Mahoney 	h->aborted = 0;
4034b824906SMiao Xie 	h->qgroup_reserved = 0;
404bed92eaeSArne Jansen 	h->delayed_ref_elem.seq = 0;
405a698d075SMiao Xie 	h->type = type;
406c6b305a8SJosef Bacik 	h->allocating_chunk = false;
407bed92eaeSArne Jansen 	INIT_LIST_HEAD(&h->qgroup_ref_list);
408ea658badSJosef Bacik 	INIT_LIST_HEAD(&h->new_bgs);
409b7ec40d7SChris Mason 
410a22285a6SYan, Zheng 	smp_mb();
411a22285a6SYan, Zheng 	if (cur_trans->blocked && may_wait_transaction(root, type)) {
412a22285a6SYan, Zheng 		btrfs_commit_transaction(h, root);
413a22285a6SYan, Zheng 		goto again;
414a22285a6SYan, Zheng 	}
4159ed74f2dSJosef Bacik 
416b5009945SJosef Bacik 	if (num_bytes) {
4178c2a3ca2SJosef Bacik 		trace_btrfs_space_reservation(root->fs_info, "transaction",
4182bcc0328SLiu Bo 					      h->transid, num_bytes, 1);
419b5009945SJosef Bacik 		h->block_rsv = &root->fs_info->trans_block_rsv;
420b5009945SJosef Bacik 		h->bytes_reserved = num_bytes;
421a22285a6SYan, Zheng 	}
4224b824906SMiao Xie 	h->qgroup_reserved = qgroup_reserved;
423a22285a6SYan, Zheng 
4242a1eb461SJosef Bacik got_it:
425a4abeea4SJosef Bacik 	btrfs_record_root_in_trans(h, root);
426a22285a6SYan, Zheng 
427a22285a6SYan, Zheng 	if (!current->journal_info && type != TRANS_USERSPACE)
428a22285a6SYan, Zheng 		current->journal_info = h;
42979154b1bSChris Mason 	return h;
430843fcf35SMiao Xie 
431843fcf35SMiao Xie join_fail:
432843fcf35SMiao Xie 	if (type < TRANS_JOIN_NOLOCK)
433843fcf35SMiao Xie 		sb_end_intwrite(root->fs_info->sb);
434843fcf35SMiao Xie 	kmem_cache_free(btrfs_trans_handle_cachep, h);
435843fcf35SMiao Xie alloc_fail:
436843fcf35SMiao Xie 	if (num_bytes)
437843fcf35SMiao Xie 		btrfs_block_rsv_release(root, &root->fs_info->trans_block_rsv,
438843fcf35SMiao Xie 					num_bytes);
439843fcf35SMiao Xie reserve_fail:
440843fcf35SMiao Xie 	if (qgroup_reserved)
441843fcf35SMiao Xie 		btrfs_qgroup_free(root, qgroup_reserved);
442843fcf35SMiao Xie 	return ERR_PTR(ret);
44379154b1bSChris Mason }
44479154b1bSChris Mason 
445f9295749SChris Mason struct btrfs_trans_handle *btrfs_start_transaction(struct btrfs_root *root,
446a22285a6SYan, Zheng 						   int num_items)
447f9295749SChris Mason {
44808e007d2SMiao Xie 	return start_transaction(root, num_items, TRANS_START,
44908e007d2SMiao Xie 				 BTRFS_RESERVE_FLUSH_ALL);
450f9295749SChris Mason }
4518407aa46SMiao Xie 
45208e007d2SMiao Xie struct btrfs_trans_handle *btrfs_start_transaction_lflush(
4538407aa46SMiao Xie 					struct btrfs_root *root, int num_items)
4548407aa46SMiao Xie {
45508e007d2SMiao Xie 	return start_transaction(root, num_items, TRANS_START,
45608e007d2SMiao Xie 				 BTRFS_RESERVE_FLUSH_LIMIT);
4578407aa46SMiao Xie }
4588407aa46SMiao Xie 
4597a7eaa40SJosef Bacik struct btrfs_trans_handle *btrfs_join_transaction(struct btrfs_root *root)
460f9295749SChris Mason {
4618407aa46SMiao Xie 	return start_transaction(root, 0, TRANS_JOIN, 0);
462f9295749SChris Mason }
463f9295749SChris Mason 
4647a7eaa40SJosef Bacik struct btrfs_trans_handle *btrfs_join_transaction_nolock(struct btrfs_root *root)
4650af3d00bSJosef Bacik {
4668407aa46SMiao Xie 	return start_transaction(root, 0, TRANS_JOIN_NOLOCK, 0);
4670af3d00bSJosef Bacik }
4680af3d00bSJosef Bacik 
4697a7eaa40SJosef Bacik struct btrfs_trans_handle *btrfs_start_ioctl_transaction(struct btrfs_root *root)
4709ca9ee09SSage Weil {
4718407aa46SMiao Xie 	return start_transaction(root, 0, TRANS_USERSPACE, 0);
4729ca9ee09SSage Weil }
4739ca9ee09SSage Weil 
474d4edf39bSMiao Xie /*
475d4edf39bSMiao Xie  * btrfs_attach_transaction() - catch the running transaction
476d4edf39bSMiao Xie  *
477d4edf39bSMiao Xie  * It is used when we want to commit the current the transaction, but
478d4edf39bSMiao Xie  * don't want to start a new one.
479d4edf39bSMiao Xie  *
480d4edf39bSMiao Xie  * Note: If this function return -ENOENT, it just means there is no
481d4edf39bSMiao Xie  * running transaction. But it is possible that the inactive transaction
482d4edf39bSMiao Xie  * is still in the memory, not fully on disk. If you hope there is no
483d4edf39bSMiao Xie  * inactive transaction in the fs when -ENOENT is returned, you should
484d4edf39bSMiao Xie  * invoke
485d4edf39bSMiao Xie  *     btrfs_attach_transaction_barrier()
486d4edf39bSMiao Xie  */
487354aa0fbSMiao Xie struct btrfs_trans_handle *btrfs_attach_transaction(struct btrfs_root *root)
48860376ce4SJosef Bacik {
489354aa0fbSMiao Xie 	return start_transaction(root, 0, TRANS_ATTACH, 0);
49060376ce4SJosef Bacik }
49160376ce4SJosef Bacik 
492d4edf39bSMiao Xie /*
493d4edf39bSMiao Xie  * btrfs_attach_transaction() - catch the running transaction
494d4edf39bSMiao Xie  *
495d4edf39bSMiao Xie  * It is similar to the above function, the differentia is this one
496d4edf39bSMiao Xie  * will wait for all the inactive transactions until they fully
497d4edf39bSMiao Xie  * complete.
498d4edf39bSMiao Xie  */
499d4edf39bSMiao Xie struct btrfs_trans_handle *
500d4edf39bSMiao Xie btrfs_attach_transaction_barrier(struct btrfs_root *root)
501d4edf39bSMiao Xie {
502d4edf39bSMiao Xie 	struct btrfs_trans_handle *trans;
503d4edf39bSMiao Xie 
504d4edf39bSMiao Xie 	trans = start_transaction(root, 0, TRANS_ATTACH, 0);
505d4edf39bSMiao Xie 	if (IS_ERR(trans) && PTR_ERR(trans) == -ENOENT)
506d4edf39bSMiao Xie 		btrfs_wait_for_commit(root, 0);
507d4edf39bSMiao Xie 
508d4edf39bSMiao Xie 	return trans;
509d4edf39bSMiao Xie }
510d4edf39bSMiao Xie 
511d352ac68SChris Mason /* wait for a transaction commit to be fully complete */
512b9c8300cSLi Zefan static noinline void wait_for_commit(struct btrfs_root *root,
51389ce8a63SChris Mason 				    struct btrfs_transaction *commit)
51489ce8a63SChris Mason {
51572d63ed6SLi Zefan 	wait_event(commit->commit_wait, commit->commit_done);
51689ce8a63SChris Mason }
51789ce8a63SChris Mason 
51846204592SSage Weil int btrfs_wait_for_commit(struct btrfs_root *root, u64 transid)
51946204592SSage Weil {
52046204592SSage Weil 	struct btrfs_transaction *cur_trans = NULL, *t;
5218cd2807fSMiao Xie 	int ret = 0;
52246204592SSage Weil 
52346204592SSage Weil 	if (transid) {
52446204592SSage Weil 		if (transid <= root->fs_info->last_trans_committed)
525a4abeea4SJosef Bacik 			goto out;
52646204592SSage Weil 
5278cd2807fSMiao Xie 		ret = -EINVAL;
52846204592SSage Weil 		/* find specified transaction */
529a4abeea4SJosef Bacik 		spin_lock(&root->fs_info->trans_lock);
53046204592SSage Weil 		list_for_each_entry(t, &root->fs_info->trans_list, list) {
53146204592SSage Weil 			if (t->transid == transid) {
53246204592SSage Weil 				cur_trans = t;
533a4abeea4SJosef Bacik 				atomic_inc(&cur_trans->use_count);
5348cd2807fSMiao Xie 				ret = 0;
53546204592SSage Weil 				break;
53646204592SSage Weil 			}
5378cd2807fSMiao Xie 			if (t->transid > transid) {
5388cd2807fSMiao Xie 				ret = 0;
53946204592SSage Weil 				break;
54046204592SSage Weil 			}
5418cd2807fSMiao Xie 		}
542a4abeea4SJosef Bacik 		spin_unlock(&root->fs_info->trans_lock);
5438cd2807fSMiao Xie 		/* The specified transaction doesn't exist */
54446204592SSage Weil 		if (!cur_trans)
5458cd2807fSMiao Xie 			goto out;
54646204592SSage Weil 	} else {
54746204592SSage Weil 		/* find newest transaction that is committing | committed */
548a4abeea4SJosef Bacik 		spin_lock(&root->fs_info->trans_lock);
54946204592SSage Weil 		list_for_each_entry_reverse(t, &root->fs_info->trans_list,
55046204592SSage Weil 					    list) {
55146204592SSage Weil 			if (t->in_commit) {
55246204592SSage Weil 				if (t->commit_done)
5533473f3c0SJosef Bacik 					break;
55446204592SSage Weil 				cur_trans = t;
555a4abeea4SJosef Bacik 				atomic_inc(&cur_trans->use_count);
55646204592SSage Weil 				break;
55746204592SSage Weil 			}
55846204592SSage Weil 		}
559a4abeea4SJosef Bacik 		spin_unlock(&root->fs_info->trans_lock);
56046204592SSage Weil 		if (!cur_trans)
561a4abeea4SJosef Bacik 			goto out;  /* nothing committing|committed */
56246204592SSage Weil 	}
56346204592SSage Weil 
56446204592SSage Weil 	wait_for_commit(root, cur_trans);
56546204592SSage Weil 	put_transaction(cur_trans);
566a4abeea4SJosef Bacik out:
56746204592SSage Weil 	return ret;
56846204592SSage Weil }
56946204592SSage Weil 
57037d1aeeeSChris Mason void btrfs_throttle(struct btrfs_root *root)
57137d1aeeeSChris Mason {
572a4abeea4SJosef Bacik 	if (!atomic_read(&root->fs_info->open_ioctl_trans))
57337d1aeeeSChris Mason 		wait_current_trans(root);
57437d1aeeeSChris Mason }
57537d1aeeeSChris Mason 
5768929ecfaSYan, Zheng static int should_end_transaction(struct btrfs_trans_handle *trans,
5778929ecfaSYan, Zheng 				  struct btrfs_root *root)
5788929ecfaSYan, Zheng {
5798929ecfaSYan, Zheng 	int ret;
58036ba022aSJosef Bacik 
58136ba022aSJosef Bacik 	ret = btrfs_block_rsv_check(root, &root->fs_info->global_block_rsv, 5);
5828929ecfaSYan, Zheng 	return ret ? 1 : 0;
5838929ecfaSYan, Zheng }
5848929ecfaSYan, Zheng 
5858929ecfaSYan, Zheng int btrfs_should_end_transaction(struct btrfs_trans_handle *trans,
5868929ecfaSYan, Zheng 				 struct btrfs_root *root)
5878929ecfaSYan, Zheng {
5888929ecfaSYan, Zheng 	struct btrfs_transaction *cur_trans = trans->transaction;
5898929ecfaSYan, Zheng 	int updates;
59049b25e05SJeff Mahoney 	int err;
5918929ecfaSYan, Zheng 
592a4abeea4SJosef Bacik 	smp_mb();
5938929ecfaSYan, Zheng 	if (cur_trans->blocked || cur_trans->delayed_refs.flushing)
5948929ecfaSYan, Zheng 		return 1;
5958929ecfaSYan, Zheng 
5968929ecfaSYan, Zheng 	updates = trans->delayed_ref_updates;
5978929ecfaSYan, Zheng 	trans->delayed_ref_updates = 0;
59849b25e05SJeff Mahoney 	if (updates) {
59949b25e05SJeff Mahoney 		err = btrfs_run_delayed_refs(trans, root, updates);
60049b25e05SJeff Mahoney 		if (err) /* Error code will also eval true */
60149b25e05SJeff Mahoney 			return err;
60249b25e05SJeff Mahoney 	}
6038929ecfaSYan, Zheng 
6048929ecfaSYan, Zheng 	return should_end_transaction(trans, root);
6058929ecfaSYan, Zheng }
6068929ecfaSYan, Zheng 
60789ce8a63SChris Mason static int __btrfs_end_transaction(struct btrfs_trans_handle *trans,
608a698d075SMiao Xie 			  struct btrfs_root *root, int throttle)
60979154b1bSChris Mason {
6108929ecfaSYan, Zheng 	struct btrfs_transaction *cur_trans = trans->transaction;
611ab78c84dSChris Mason 	struct btrfs_fs_info *info = root->fs_info;
612c3e69d58SChris Mason 	int count = 0;
613a698d075SMiao Xie 	int lock = (trans->type != TRANS_JOIN_NOLOCK);
6144edc2ca3SDave Jones 	int err = 0;
615d6e4a428SChris Mason 
6162a1eb461SJosef Bacik 	if (--trans->use_count) {
6172a1eb461SJosef Bacik 		trans->block_rsv = trans->orig_rsv;
6182a1eb461SJosef Bacik 		return 0;
6192a1eb461SJosef Bacik 	}
6202a1eb461SJosef Bacik 
621edf39272SJan Schmidt 	/*
622edf39272SJan Schmidt 	 * do the qgroup accounting as early as possible
623edf39272SJan Schmidt 	 */
624edf39272SJan Schmidt 	err = btrfs_delayed_refs_qgroup_accounting(trans, info);
625edf39272SJan Schmidt 
626b24e03dbSJosef Bacik 	btrfs_trans_release_metadata(trans, root);
6274c13d758SJosef Bacik 	trans->block_rsv = NULL;
628c5567237SArne Jansen 
629c5567237SArne Jansen 	if (trans->qgroup_reserved) {
6307c2ec3f0SLiu Bo 		/*
6317c2ec3f0SLiu Bo 		 * the same root has to be passed here between start_transaction
6327c2ec3f0SLiu Bo 		 * and end_transaction. Subvolume quota depends on this.
6337c2ec3f0SLiu Bo 		 */
6347c2ec3f0SLiu Bo 		btrfs_qgroup_free(trans->root, trans->qgroup_reserved);
635c5567237SArne Jansen 		trans->qgroup_reserved = 0;
636c5567237SArne Jansen 	}
637c5567237SArne Jansen 
638ea658badSJosef Bacik 	if (!list_empty(&trans->new_bgs))
639ea658badSJosef Bacik 		btrfs_create_pending_block_groups(trans, root);
640ea658badSJosef Bacik 
641bb721703SChris Mason 	while (count < 1) {
642c3e69d58SChris Mason 		unsigned long cur = trans->delayed_ref_updates;
643c3e69d58SChris Mason 		trans->delayed_ref_updates = 0;
644c3e69d58SChris Mason 		if (cur &&
645c3e69d58SChris Mason 		    trans->transaction->delayed_refs.num_heads_ready > 64) {
646c3e69d58SChris Mason 			trans->delayed_ref_updates = 0;
647c3e69d58SChris Mason 			btrfs_run_delayed_refs(trans, root, cur);
648c3e69d58SChris Mason 		} else {
649c3e69d58SChris Mason 			break;
650c3e69d58SChris Mason 		}
651c3e69d58SChris Mason 		count++;
65256bec294SChris Mason 	}
653bb721703SChris Mason 
6540e721106SJosef Bacik 	btrfs_trans_release_metadata(trans, root);
6550e721106SJosef Bacik 	trans->block_rsv = NULL;
65656bec294SChris Mason 
657ea658badSJosef Bacik 	if (!list_empty(&trans->new_bgs))
658ea658badSJosef Bacik 		btrfs_create_pending_block_groups(trans, root);
659ea658badSJosef Bacik 
660a4abeea4SJosef Bacik 	if (lock && !atomic_read(&root->fs_info->open_ioctl_trans) &&
661a4abeea4SJosef Bacik 	    should_end_transaction(trans, root)) {
6628929ecfaSYan, Zheng 		trans->transaction->blocked = 1;
663a4abeea4SJosef Bacik 		smp_wmb();
664a4abeea4SJosef Bacik 	}
6658929ecfaSYan, Zheng 
6660af3d00bSJosef Bacik 	if (lock && cur_trans->blocked && !cur_trans->in_commit) {
66781317fdeSJosef Bacik 		if (throttle) {
66881317fdeSJosef Bacik 			/*
66981317fdeSJosef Bacik 			 * We may race with somebody else here so end up having
67081317fdeSJosef Bacik 			 * to call end_transaction on ourselves again, so inc
67181317fdeSJosef Bacik 			 * our use_count.
67281317fdeSJosef Bacik 			 */
67381317fdeSJosef Bacik 			trans->use_count++;
6748929ecfaSYan, Zheng 			return btrfs_commit_transaction(trans, root);
67581317fdeSJosef Bacik 		} else {
6768929ecfaSYan, Zheng 			wake_up_process(info->transaction_kthread);
6778929ecfaSYan, Zheng 		}
67881317fdeSJosef Bacik 	}
6798929ecfaSYan, Zheng 
680354aa0fbSMiao Xie 	if (trans->type < TRANS_JOIN_NOLOCK)
6816df7881aSJosef Bacik 		sb_end_intwrite(root->fs_info->sb);
6826df7881aSJosef Bacik 
6838929ecfaSYan, Zheng 	WARN_ON(cur_trans != info->running_transaction);
68413c5a93eSJosef Bacik 	WARN_ON(atomic_read(&cur_trans->num_writers) < 1);
68513c5a93eSJosef Bacik 	atomic_dec(&cur_trans->num_writers);
68689ce8a63SChris Mason 
68799d16cbcSSage Weil 	smp_mb();
68879154b1bSChris Mason 	if (waitqueue_active(&cur_trans->writer_wait))
68979154b1bSChris Mason 		wake_up(&cur_trans->writer_wait);
69079154b1bSChris Mason 	put_transaction(cur_trans);
6919ed74f2dSJosef Bacik 
6929ed74f2dSJosef Bacik 	if (current->journal_info == trans)
6939ed74f2dSJosef Bacik 		current->journal_info = NULL;
694ab78c84dSChris Mason 
69524bbcf04SYan, Zheng 	if (throttle)
69624bbcf04SYan, Zheng 		btrfs_run_delayed_iputs(root);
69724bbcf04SYan, Zheng 
69849b25e05SJeff Mahoney 	if (trans->aborted ||
69987533c47SMiao Xie 	    test_bit(BTRFS_FS_STATE_ERROR, &root->fs_info->fs_state))
7004edc2ca3SDave Jones 		err = -EIO;
701edf39272SJan Schmidt 	assert_qgroups_uptodate(trans);
70249b25e05SJeff Mahoney 
7034edc2ca3SDave Jones 	kmem_cache_free(btrfs_trans_handle_cachep, trans);
7044edc2ca3SDave Jones 	return err;
70579154b1bSChris Mason }
70679154b1bSChris Mason 
70789ce8a63SChris Mason int btrfs_end_transaction(struct btrfs_trans_handle *trans,
70889ce8a63SChris Mason 			  struct btrfs_root *root)
70989ce8a63SChris Mason {
71016cdcec7SMiao Xie 	int ret;
71116cdcec7SMiao Xie 
712a698d075SMiao Xie 	ret = __btrfs_end_transaction(trans, root, 0);
71316cdcec7SMiao Xie 	if (ret)
71416cdcec7SMiao Xie 		return ret;
71516cdcec7SMiao Xie 	return 0;
71689ce8a63SChris Mason }
71789ce8a63SChris Mason 
71889ce8a63SChris Mason int btrfs_end_transaction_throttle(struct btrfs_trans_handle *trans,
71989ce8a63SChris Mason 				   struct btrfs_root *root)
72089ce8a63SChris Mason {
72116cdcec7SMiao Xie 	int ret;
72216cdcec7SMiao Xie 
723a698d075SMiao Xie 	ret = __btrfs_end_transaction(trans, root, 1);
72416cdcec7SMiao Xie 	if (ret)
72516cdcec7SMiao Xie 		return ret;
72616cdcec7SMiao Xie 	return 0;
72716cdcec7SMiao Xie }
72816cdcec7SMiao Xie 
72916cdcec7SMiao Xie int btrfs_end_transaction_dmeta(struct btrfs_trans_handle *trans,
73016cdcec7SMiao Xie 				struct btrfs_root *root)
73116cdcec7SMiao Xie {
732a698d075SMiao Xie 	return __btrfs_end_transaction(trans, root, 1);
73389ce8a63SChris Mason }
73489ce8a63SChris Mason 
735d352ac68SChris Mason /*
736d352ac68SChris Mason  * when btree blocks are allocated, they have some corresponding bits set for
737d352ac68SChris Mason  * them in one of two extent_io trees.  This is used to make sure all of
738690587d1SChris Mason  * those extents are sent to disk but does not wait on them
739d352ac68SChris Mason  */
740690587d1SChris Mason int btrfs_write_marked_extents(struct btrfs_root *root,
7418cef4e16SYan, Zheng 			       struct extent_io_tree *dirty_pages, int mark)
74279154b1bSChris Mason {
743777e6bd7SChris Mason 	int err = 0;
7447c4452b9SChris Mason 	int werr = 0;
7451728366eSJosef Bacik 	struct address_space *mapping = root->fs_info->btree_inode->i_mapping;
746e6138876SJosef Bacik 	struct extent_state *cached_state = NULL;
747777e6bd7SChris Mason 	u64 start = 0;
7485f39d397SChris Mason 	u64 end;
74953b381b3SDavid Woodhouse 	struct blk_plug plug;
7507c4452b9SChris Mason 
75153b381b3SDavid Woodhouse 	blk_start_plug(&plug);
7521728366eSJosef Bacik 	while (!find_first_extent_bit(dirty_pages, start, &start, &end,
753e6138876SJosef Bacik 				      mark, &cached_state)) {
754e6138876SJosef Bacik 		convert_extent_bit(dirty_pages, start, end, EXTENT_NEED_WAIT,
755e6138876SJosef Bacik 				   mark, &cached_state, GFP_NOFS);
756e6138876SJosef Bacik 		cached_state = NULL;
7571728366eSJosef Bacik 		err = filemap_fdatawrite_range(mapping, start, end);
7587c4452b9SChris Mason 		if (err)
7597c4452b9SChris Mason 			werr = err;
7601728366eSJosef Bacik 		cond_resched();
7611728366eSJosef Bacik 		start = end + 1;
7627c4452b9SChris Mason 	}
763690587d1SChris Mason 	if (err)
764690587d1SChris Mason 		werr = err;
76553b381b3SDavid Woodhouse 	blk_finish_plug(&plug);
766690587d1SChris Mason 	return werr;
767690587d1SChris Mason }
768690587d1SChris Mason 
769690587d1SChris Mason /*
770690587d1SChris Mason  * when btree blocks are allocated, they have some corresponding bits set for
771690587d1SChris Mason  * them in one of two extent_io trees.  This is used to make sure all of
772690587d1SChris Mason  * those extents are on disk for transaction or log commit.  We wait
773690587d1SChris Mason  * on all the pages and clear them from the dirty pages state tree
774690587d1SChris Mason  */
775690587d1SChris Mason int btrfs_wait_marked_extents(struct btrfs_root *root,
7768cef4e16SYan, Zheng 			      struct extent_io_tree *dirty_pages, int mark)
777690587d1SChris Mason {
778690587d1SChris Mason 	int err = 0;
779690587d1SChris Mason 	int werr = 0;
7801728366eSJosef Bacik 	struct address_space *mapping = root->fs_info->btree_inode->i_mapping;
781e6138876SJosef Bacik 	struct extent_state *cached_state = NULL;
782690587d1SChris Mason 	u64 start = 0;
783690587d1SChris Mason 	u64 end;
784690587d1SChris Mason 
7851728366eSJosef Bacik 	while (!find_first_extent_bit(dirty_pages, start, &start, &end,
786e6138876SJosef Bacik 				      EXTENT_NEED_WAIT, &cached_state)) {
787e6138876SJosef Bacik 		clear_extent_bit(dirty_pages, start, end, EXTENT_NEED_WAIT,
788e6138876SJosef Bacik 				 0, 0, &cached_state, GFP_NOFS);
7891728366eSJosef Bacik 		err = filemap_fdatawait_range(mapping, start, end);
790777e6bd7SChris Mason 		if (err)
791777e6bd7SChris Mason 			werr = err;
792777e6bd7SChris Mason 		cond_resched();
7931728366eSJosef Bacik 		start = end + 1;
794777e6bd7SChris Mason 	}
7957c4452b9SChris Mason 	if (err)
7967c4452b9SChris Mason 		werr = err;
7977c4452b9SChris Mason 	return werr;
79879154b1bSChris Mason }
79979154b1bSChris Mason 
800690587d1SChris Mason /*
801690587d1SChris Mason  * when btree blocks are allocated, they have some corresponding bits set for
802690587d1SChris Mason  * them in one of two extent_io trees.  This is used to make sure all of
803690587d1SChris Mason  * those extents are on disk for transaction or log commit
804690587d1SChris Mason  */
805690587d1SChris Mason int btrfs_write_and_wait_marked_extents(struct btrfs_root *root,
8068cef4e16SYan, Zheng 				struct extent_io_tree *dirty_pages, int mark)
807690587d1SChris Mason {
808690587d1SChris Mason 	int ret;
809690587d1SChris Mason 	int ret2;
810690587d1SChris Mason 
8118cef4e16SYan, Zheng 	ret = btrfs_write_marked_extents(root, dirty_pages, mark);
8128cef4e16SYan, Zheng 	ret2 = btrfs_wait_marked_extents(root, dirty_pages, mark);
813bf0da8c1SChris Mason 
814bf0da8c1SChris Mason 	if (ret)
815bf0da8c1SChris Mason 		return ret;
816bf0da8c1SChris Mason 	if (ret2)
817bf0da8c1SChris Mason 		return ret2;
818bf0da8c1SChris Mason 	return 0;
819690587d1SChris Mason }
820690587d1SChris Mason 
821d0c803c4SChris Mason int btrfs_write_and_wait_transaction(struct btrfs_trans_handle *trans,
822d0c803c4SChris Mason 				     struct btrfs_root *root)
823d0c803c4SChris Mason {
824d0c803c4SChris Mason 	if (!trans || !trans->transaction) {
825d0c803c4SChris Mason 		struct inode *btree_inode;
826d0c803c4SChris Mason 		btree_inode = root->fs_info->btree_inode;
827d0c803c4SChris Mason 		return filemap_write_and_wait(btree_inode->i_mapping);
828d0c803c4SChris Mason 	}
829d0c803c4SChris Mason 	return btrfs_write_and_wait_marked_extents(root,
8308cef4e16SYan, Zheng 					   &trans->transaction->dirty_pages,
8318cef4e16SYan, Zheng 					   EXTENT_DIRTY);
832d0c803c4SChris Mason }
833d0c803c4SChris Mason 
834d352ac68SChris Mason /*
835d352ac68SChris Mason  * this is used to update the root pointer in the tree of tree roots.
836d352ac68SChris Mason  *
837d352ac68SChris Mason  * But, in the case of the extent allocation tree, updating the root
838d352ac68SChris Mason  * pointer may allocate blocks which may change the root of the extent
839d352ac68SChris Mason  * allocation tree.
840d352ac68SChris Mason  *
841d352ac68SChris Mason  * So, this loops and repeats and makes sure the cowonly root didn't
842d352ac68SChris Mason  * change while the root pointer was being updated in the metadata.
843d352ac68SChris Mason  */
8440b86a832SChris Mason static int update_cowonly_root(struct btrfs_trans_handle *trans,
84579154b1bSChris Mason 			       struct btrfs_root *root)
84679154b1bSChris Mason {
84779154b1bSChris Mason 	int ret;
8480b86a832SChris Mason 	u64 old_root_bytenr;
84986b9f2ecSYan, Zheng 	u64 old_root_used;
8500b86a832SChris Mason 	struct btrfs_root *tree_root = root->fs_info->tree_root;
85179154b1bSChris Mason 
85286b9f2ecSYan, Zheng 	old_root_used = btrfs_root_used(&root->root_item);
8530b86a832SChris Mason 	btrfs_write_dirty_block_groups(trans, root);
85456bec294SChris Mason 
85579154b1bSChris Mason 	while (1) {
8560b86a832SChris Mason 		old_root_bytenr = btrfs_root_bytenr(&root->root_item);
85786b9f2ecSYan, Zheng 		if (old_root_bytenr == root->node->start &&
85886b9f2ecSYan, Zheng 		    old_root_used == btrfs_root_used(&root->root_item))
85979154b1bSChris Mason 			break;
86087ef2bb4SChris Mason 
8615d4f98a2SYan Zheng 		btrfs_set_root_node(&root->root_item, root->node);
86279154b1bSChris Mason 		ret = btrfs_update_root(trans, tree_root,
8630b86a832SChris Mason 					&root->root_key,
8640b86a832SChris Mason 					&root->root_item);
86549b25e05SJeff Mahoney 		if (ret)
86649b25e05SJeff Mahoney 			return ret;
86756bec294SChris Mason 
86886b9f2ecSYan, Zheng 		old_root_used = btrfs_root_used(&root->root_item);
8694a8c9a62SYan Zheng 		ret = btrfs_write_dirty_block_groups(trans, root);
87049b25e05SJeff Mahoney 		if (ret)
87149b25e05SJeff Mahoney 			return ret;
8720b86a832SChris Mason 	}
873276e680dSYan Zheng 
874276e680dSYan Zheng 	if (root != root->fs_info->extent_root)
875817d52f8SJosef Bacik 		switch_commit_root(root);
876276e680dSYan Zheng 
8770b86a832SChris Mason 	return 0;
8780b86a832SChris Mason }
8790b86a832SChris Mason 
880d352ac68SChris Mason /*
881d352ac68SChris Mason  * update all the cowonly tree roots on disk
88249b25e05SJeff Mahoney  *
88349b25e05SJeff Mahoney  * The error handling in this function may not be obvious. Any of the
88449b25e05SJeff Mahoney  * failures will cause the file system to go offline. We still need
88549b25e05SJeff Mahoney  * to clean up the delayed refs.
886d352ac68SChris Mason  */
8875d4f98a2SYan Zheng static noinline int commit_cowonly_roots(struct btrfs_trans_handle *trans,
8880b86a832SChris Mason 					 struct btrfs_root *root)
8890b86a832SChris Mason {
8900b86a832SChris Mason 	struct btrfs_fs_info *fs_info = root->fs_info;
8910b86a832SChris Mason 	struct list_head *next;
89284234f3aSYan Zheng 	struct extent_buffer *eb;
89356bec294SChris Mason 	int ret;
89484234f3aSYan Zheng 
89556bec294SChris Mason 	ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
89649b25e05SJeff Mahoney 	if (ret)
89749b25e05SJeff Mahoney 		return ret;
89887ef2bb4SChris Mason 
89984234f3aSYan Zheng 	eb = btrfs_lock_root_node(fs_info->tree_root);
90049b25e05SJeff Mahoney 	ret = btrfs_cow_block(trans, fs_info->tree_root, eb, NULL,
90149b25e05SJeff Mahoney 			      0, &eb);
90284234f3aSYan Zheng 	btrfs_tree_unlock(eb);
90384234f3aSYan Zheng 	free_extent_buffer(eb);
9040b86a832SChris Mason 
90549b25e05SJeff Mahoney 	if (ret)
90649b25e05SJeff Mahoney 		return ret;
90749b25e05SJeff Mahoney 
90856bec294SChris Mason 	ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
90949b25e05SJeff Mahoney 	if (ret)
91049b25e05SJeff Mahoney 		return ret;
91187ef2bb4SChris Mason 
912733f4fbbSStefan Behrens 	ret = btrfs_run_dev_stats(trans, root->fs_info);
9138dabb742SStefan Behrens 	WARN_ON(ret);
9148dabb742SStefan Behrens 	ret = btrfs_run_dev_replace(trans, root->fs_info);
9158dabb742SStefan Behrens 	WARN_ON(ret);
916733f4fbbSStefan Behrens 
917546adb0dSJan Schmidt 	ret = btrfs_run_qgroups(trans, root->fs_info);
918546adb0dSJan Schmidt 	BUG_ON(ret);
919546adb0dSJan Schmidt 
920546adb0dSJan Schmidt 	/* run_qgroups might have added some more refs */
921546adb0dSJan Schmidt 	ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
922546adb0dSJan Schmidt 	BUG_ON(ret);
923546adb0dSJan Schmidt 
9240b86a832SChris Mason 	while (!list_empty(&fs_info->dirty_cowonly_roots)) {
9250b86a832SChris Mason 		next = fs_info->dirty_cowonly_roots.next;
9260b86a832SChris Mason 		list_del_init(next);
9270b86a832SChris Mason 		root = list_entry(next, struct btrfs_root, dirty_list);
92887ef2bb4SChris Mason 
92949b25e05SJeff Mahoney 		ret = update_cowonly_root(trans, root);
93049b25e05SJeff Mahoney 		if (ret)
93149b25e05SJeff Mahoney 			return ret;
93279154b1bSChris Mason 	}
933276e680dSYan Zheng 
934276e680dSYan Zheng 	down_write(&fs_info->extent_commit_sem);
935276e680dSYan Zheng 	switch_commit_root(fs_info->extent_root);
936276e680dSYan Zheng 	up_write(&fs_info->extent_commit_sem);
937276e680dSYan Zheng 
9388dabb742SStefan Behrens 	btrfs_after_dev_replace_commit(fs_info);
9398dabb742SStefan Behrens 
94079154b1bSChris Mason 	return 0;
94179154b1bSChris Mason }
94279154b1bSChris Mason 
943d352ac68SChris Mason /*
944d352ac68SChris Mason  * dead roots are old snapshots that need to be deleted.  This allocates
945d352ac68SChris Mason  * a dirty root struct and adds it into the list of dead roots that need to
946d352ac68SChris Mason  * be deleted
947d352ac68SChris Mason  */
9485d4f98a2SYan Zheng int btrfs_add_dead_root(struct btrfs_root *root)
9495eda7b5eSChris Mason {
950a4abeea4SJosef Bacik 	spin_lock(&root->fs_info->trans_lock);
9519d1a2a3aSDavid Sterba 	list_add_tail(&root->root_list, &root->fs_info->dead_roots);
952a4abeea4SJosef Bacik 	spin_unlock(&root->fs_info->trans_lock);
9535eda7b5eSChris Mason 	return 0;
9545eda7b5eSChris Mason }
9555eda7b5eSChris Mason 
956d352ac68SChris Mason /*
9575d4f98a2SYan Zheng  * update all the cowonly tree roots on disk
958d352ac68SChris Mason  */
9595d4f98a2SYan Zheng static noinline int commit_fs_roots(struct btrfs_trans_handle *trans,
9605d4f98a2SYan Zheng 				    struct btrfs_root *root)
9610f7d52f4SChris Mason {
9620f7d52f4SChris Mason 	struct btrfs_root *gang[8];
9635d4f98a2SYan Zheng 	struct btrfs_fs_info *fs_info = root->fs_info;
9640f7d52f4SChris Mason 	int i;
9650f7d52f4SChris Mason 	int ret;
96654aa1f4dSChris Mason 	int err = 0;
96754aa1f4dSChris Mason 
968a4abeea4SJosef Bacik 	spin_lock(&fs_info->fs_roots_radix_lock);
9690f7d52f4SChris Mason 	while (1) {
9705d4f98a2SYan Zheng 		ret = radix_tree_gang_lookup_tag(&fs_info->fs_roots_radix,
9715d4f98a2SYan Zheng 						 (void **)gang, 0,
9720f7d52f4SChris Mason 						 ARRAY_SIZE(gang),
9730f7d52f4SChris Mason 						 BTRFS_ROOT_TRANS_TAG);
9740f7d52f4SChris Mason 		if (ret == 0)
9750f7d52f4SChris Mason 			break;
9760f7d52f4SChris Mason 		for (i = 0; i < ret; i++) {
9770f7d52f4SChris Mason 			root = gang[i];
9785d4f98a2SYan Zheng 			radix_tree_tag_clear(&fs_info->fs_roots_radix,
9792619ba1fSChris Mason 					(unsigned long)root->root_key.objectid,
9800f7d52f4SChris Mason 					BTRFS_ROOT_TRANS_TAG);
981a4abeea4SJosef Bacik 			spin_unlock(&fs_info->fs_roots_radix_lock);
98231153d81SYan Zheng 
983e02119d5SChris Mason 			btrfs_free_log(trans, root);
9845d4f98a2SYan Zheng 			btrfs_update_reloc_root(trans, root);
985d68fc57bSYan, Zheng 			btrfs_orphan_commit_root(trans, root);
986e02119d5SChris Mason 
98782d5902dSLi Zefan 			btrfs_save_ino_cache(root, trans);
98882d5902dSLi Zefan 
989f1ebcc74SLiu Bo 			/* see comments in should_cow_block() */
990f1ebcc74SLiu Bo 			root->force_cow = 0;
991f1ebcc74SLiu Bo 			smp_wmb();
992f1ebcc74SLiu Bo 
993978d910dSYan Zheng 			if (root->commit_root != root->node) {
994581bb050SLi Zefan 				mutex_lock(&root->fs_commit_mutex);
995817d52f8SJosef Bacik 				switch_commit_root(root);
996581bb050SLi Zefan 				btrfs_unpin_free_ino(root);
997581bb050SLi Zefan 				mutex_unlock(&root->fs_commit_mutex);
998581bb050SLi Zefan 
999978d910dSYan Zheng 				btrfs_set_root_node(&root->root_item,
1000978d910dSYan Zheng 						    root->node);
1001978d910dSYan Zheng 			}
100231153d81SYan Zheng 
10035d4f98a2SYan Zheng 			err = btrfs_update_root(trans, fs_info->tree_root,
10040f7d52f4SChris Mason 						&root->root_key,
10050f7d52f4SChris Mason 						&root->root_item);
1006a4abeea4SJosef Bacik 			spin_lock(&fs_info->fs_roots_radix_lock);
100754aa1f4dSChris Mason 			if (err)
100854aa1f4dSChris Mason 				break;
10090f7d52f4SChris Mason 		}
10109f3a7427SChris Mason 	}
1011a4abeea4SJosef Bacik 	spin_unlock(&fs_info->fs_roots_radix_lock);
101254aa1f4dSChris Mason 	return err;
10130f7d52f4SChris Mason }
10140f7d52f4SChris Mason 
1015d352ac68SChris Mason /*
1016de78b51aSEric Sandeen  * defrag a given btree.
1017de78b51aSEric Sandeen  * Every leaf in the btree is read and defragged.
1018d352ac68SChris Mason  */
1019de78b51aSEric Sandeen int btrfs_defrag_root(struct btrfs_root *root)
1020e9d0b13bSChris Mason {
1021e9d0b13bSChris Mason 	struct btrfs_fs_info *info = root->fs_info;
1022e9d0b13bSChris Mason 	struct btrfs_trans_handle *trans;
10238929ecfaSYan, Zheng 	int ret;
1024e9d0b13bSChris Mason 
10258929ecfaSYan, Zheng 	if (xchg(&root->defrag_running, 1))
1026e9d0b13bSChris Mason 		return 0;
10278929ecfaSYan, Zheng 
10286b80053dSChris Mason 	while (1) {
10298929ecfaSYan, Zheng 		trans = btrfs_start_transaction(root, 0);
10308929ecfaSYan, Zheng 		if (IS_ERR(trans))
10318929ecfaSYan, Zheng 			return PTR_ERR(trans);
10328929ecfaSYan, Zheng 
1033de78b51aSEric Sandeen 		ret = btrfs_defrag_leaves(trans, root);
10348929ecfaSYan, Zheng 
1035e9d0b13bSChris Mason 		btrfs_end_transaction(trans, root);
1036b53d3f5dSLiu Bo 		btrfs_btree_balance_dirty(info->tree_root);
1037e9d0b13bSChris Mason 		cond_resched();
1038e9d0b13bSChris Mason 
10397841cb28SDavid Sterba 		if (btrfs_fs_closing(root->fs_info) || ret != -EAGAIN)
1040e9d0b13bSChris Mason 			break;
1041210549ebSDavid Sterba 
1042210549ebSDavid Sterba 		if (btrfs_defrag_cancelled(root->fs_info)) {
1043210549ebSDavid Sterba 			printk(KERN_DEBUG "btrfs: defrag_root cancelled\n");
1044210549ebSDavid Sterba 			ret = -EAGAIN;
1045210549ebSDavid Sterba 			break;
1046210549ebSDavid Sterba 		}
1047e9d0b13bSChris Mason 	}
1048e9d0b13bSChris Mason 	root->defrag_running = 0;
10498929ecfaSYan, Zheng 	return ret;
1050e9d0b13bSChris Mason }
1051e9d0b13bSChris Mason 
1052d352ac68SChris Mason /*
1053d352ac68SChris Mason  * new snapshots need to be created at a very specific time in the
1054aec8030aSMiao Xie  * transaction commit.  This does the actual creation.
1055aec8030aSMiao Xie  *
1056aec8030aSMiao Xie  * Note:
1057aec8030aSMiao Xie  * If the error which may affect the commitment of the current transaction
1058aec8030aSMiao Xie  * happens, we should return the error number. If the error which just affect
1059aec8030aSMiao Xie  * the creation of the pending snapshots, just return 0.
1060d352ac68SChris Mason  */
106180b6794dSChris Mason static noinline int create_pending_snapshot(struct btrfs_trans_handle *trans,
10623063d29fSChris Mason 				   struct btrfs_fs_info *fs_info,
10633063d29fSChris Mason 				   struct btrfs_pending_snapshot *pending)
10643063d29fSChris Mason {
10653063d29fSChris Mason 	struct btrfs_key key;
106680b6794dSChris Mason 	struct btrfs_root_item *new_root_item;
10673063d29fSChris Mason 	struct btrfs_root *tree_root = fs_info->tree_root;
10683063d29fSChris Mason 	struct btrfs_root *root = pending->root;
10696bdb72deSSage Weil 	struct btrfs_root *parent_root;
107098c9942aSLiu Bo 	struct btrfs_block_rsv *rsv;
10716bdb72deSSage Weil 	struct inode *parent_inode;
107242874b3dSMiao Xie 	struct btrfs_path *path;
107342874b3dSMiao Xie 	struct btrfs_dir_item *dir_item;
1074a22285a6SYan, Zheng 	struct dentry *dentry;
10753063d29fSChris Mason 	struct extent_buffer *tmp;
1076925baeddSChris Mason 	struct extent_buffer *old;
10778ea05e3aSAlexander Block 	struct timespec cur_time = CURRENT_TIME;
1078aec8030aSMiao Xie 	int ret = 0;
1079d68fc57bSYan, Zheng 	u64 to_reserve = 0;
10806bdb72deSSage Weil 	u64 index = 0;
1081a22285a6SYan, Zheng 	u64 objectid;
1082b83cc969SLi Zefan 	u64 root_flags;
10838ea05e3aSAlexander Block 	uuid_le new_uuid;
10843063d29fSChris Mason 
108542874b3dSMiao Xie 	path = btrfs_alloc_path();
108642874b3dSMiao Xie 	if (!path) {
1087aec8030aSMiao Xie 		pending->error = -ENOMEM;
1088aec8030aSMiao Xie 		return 0;
108942874b3dSMiao Xie 	}
109042874b3dSMiao Xie 
109180b6794dSChris Mason 	new_root_item = kmalloc(sizeof(*new_root_item), GFP_NOFS);
109280b6794dSChris Mason 	if (!new_root_item) {
1093aec8030aSMiao Xie 		pending->error = -ENOMEM;
10946fa9700eSMiao Xie 		goto root_item_alloc_fail;
109580b6794dSChris Mason 	}
1096a22285a6SYan, Zheng 
1097aec8030aSMiao Xie 	pending->error = btrfs_find_free_objectid(tree_root, &objectid);
1098aec8030aSMiao Xie 	if (pending->error)
10996fa9700eSMiao Xie 		goto no_free_objectid;
11003063d29fSChris Mason 
11013fd0a558SYan, Zheng 	btrfs_reloc_pre_snapshot(trans, pending, &to_reserve);
1102d68fc57bSYan, Zheng 
1103d68fc57bSYan, Zheng 	if (to_reserve > 0) {
1104aec8030aSMiao Xie 		pending->error = btrfs_block_rsv_add(root,
1105aec8030aSMiao Xie 						     &pending->block_rsv,
110608e007d2SMiao Xie 						     to_reserve,
110708e007d2SMiao Xie 						     BTRFS_RESERVE_NO_FLUSH);
1108aec8030aSMiao Xie 		if (pending->error)
11096fa9700eSMiao Xie 			goto no_free_objectid;
1110d68fc57bSYan, Zheng 	}
1111d68fc57bSYan, Zheng 
1112aec8030aSMiao Xie 	pending->error = btrfs_qgroup_inherit(trans, fs_info,
1113aec8030aSMiao Xie 					      root->root_key.objectid,
11146f72c7e2SArne Jansen 					      objectid, pending->inherit);
1115aec8030aSMiao Xie 	if (pending->error)
11166fa9700eSMiao Xie 		goto no_free_objectid;
11176f72c7e2SArne Jansen 
11183063d29fSChris Mason 	key.objectid = objectid;
1119a22285a6SYan, Zheng 	key.offset = (u64)-1;
1120a22285a6SYan, Zheng 	key.type = BTRFS_ROOT_ITEM_KEY;
11213063d29fSChris Mason 
11226fa9700eSMiao Xie 	rsv = trans->block_rsv;
1123a22285a6SYan, Zheng 	trans->block_rsv = &pending->block_rsv;
11242382c5ccSLiu Bo 	trans->bytes_reserved = trans->block_rsv->reserved;
11256bdb72deSSage Weil 
1126a22285a6SYan, Zheng 	dentry = pending->dentry;
1127e9662f70SMiao Xie 	parent_inode = pending->dir;
1128a22285a6SYan, Zheng 	parent_root = BTRFS_I(parent_inode)->root;
11297585717fSChris Mason 	record_root_in_trans(trans, parent_root);
1130a22285a6SYan, Zheng 
11316bdb72deSSage Weil 	/*
11326bdb72deSSage Weil 	 * insert the directory item
11336bdb72deSSage Weil 	 */
11346bdb72deSSage Weil 	ret = btrfs_set_inode_index(parent_inode, &index);
113549b25e05SJeff Mahoney 	BUG_ON(ret); /* -ENOMEM */
113642874b3dSMiao Xie 
113742874b3dSMiao Xie 	/* check if there is a file/dir which has the same name. */
113842874b3dSMiao Xie 	dir_item = btrfs_lookup_dir_item(NULL, parent_root, path,
113942874b3dSMiao Xie 					 btrfs_ino(parent_inode),
114042874b3dSMiao Xie 					 dentry->d_name.name,
114142874b3dSMiao Xie 					 dentry->d_name.len, 0);
114242874b3dSMiao Xie 	if (dir_item != NULL && !IS_ERR(dir_item)) {
1143fe66a05aSChris Mason 		pending->error = -EEXIST;
1144aec8030aSMiao Xie 		goto dir_item_existed;
114542874b3dSMiao Xie 	} else if (IS_ERR(dir_item)) {
114642874b3dSMiao Xie 		ret = PTR_ERR(dir_item);
11478732d44fSMiao Xie 		btrfs_abort_transaction(trans, root, ret);
11488732d44fSMiao Xie 		goto fail;
114979787eaaSJeff Mahoney 	}
115042874b3dSMiao Xie 	btrfs_release_path(path);
11516bdb72deSSage Weil 
1152e999376fSChris Mason 	/*
1153e999376fSChris Mason 	 * pull in the delayed directory update
1154e999376fSChris Mason 	 * and the delayed inode item
1155e999376fSChris Mason 	 * otherwise we corrupt the FS during
1156e999376fSChris Mason 	 * snapshot
1157e999376fSChris Mason 	 */
1158e999376fSChris Mason 	ret = btrfs_run_delayed_items(trans, root);
11598732d44fSMiao Xie 	if (ret) {	/* Transaction aborted */
11608732d44fSMiao Xie 		btrfs_abort_transaction(trans, root, ret);
11618732d44fSMiao Xie 		goto fail;
11628732d44fSMiao Xie 	}
1163e999376fSChris Mason 
11647585717fSChris Mason 	record_root_in_trans(trans, root);
11656bdb72deSSage Weil 	btrfs_set_root_last_snapshot(&root->root_item, trans->transid);
11666bdb72deSSage Weil 	memcpy(new_root_item, &root->root_item, sizeof(*new_root_item));
116708fe4db1SLi Zefan 	btrfs_check_and_init_root_item(new_root_item);
11686bdb72deSSage Weil 
1169b83cc969SLi Zefan 	root_flags = btrfs_root_flags(new_root_item);
1170b83cc969SLi Zefan 	if (pending->readonly)
1171b83cc969SLi Zefan 		root_flags |= BTRFS_ROOT_SUBVOL_RDONLY;
1172b83cc969SLi Zefan 	else
1173b83cc969SLi Zefan 		root_flags &= ~BTRFS_ROOT_SUBVOL_RDONLY;
1174b83cc969SLi Zefan 	btrfs_set_root_flags(new_root_item, root_flags);
1175b83cc969SLi Zefan 
11768ea05e3aSAlexander Block 	btrfs_set_root_generation_v2(new_root_item,
11778ea05e3aSAlexander Block 			trans->transid);
11788ea05e3aSAlexander Block 	uuid_le_gen(&new_uuid);
11798ea05e3aSAlexander Block 	memcpy(new_root_item->uuid, new_uuid.b, BTRFS_UUID_SIZE);
11808ea05e3aSAlexander Block 	memcpy(new_root_item->parent_uuid, root->root_item.uuid,
11818ea05e3aSAlexander Block 			BTRFS_UUID_SIZE);
11828ea05e3aSAlexander Block 	new_root_item->otime.sec = cpu_to_le64(cur_time.tv_sec);
1183dadd1105SDan Carpenter 	new_root_item->otime.nsec = cpu_to_le32(cur_time.tv_nsec);
11848ea05e3aSAlexander Block 	btrfs_set_root_otransid(new_root_item, trans->transid);
11858ea05e3aSAlexander Block 	memset(&new_root_item->stime, 0, sizeof(new_root_item->stime));
11868ea05e3aSAlexander Block 	memset(&new_root_item->rtime, 0, sizeof(new_root_item->rtime));
11878ea05e3aSAlexander Block 	btrfs_set_root_stransid(new_root_item, 0);
11888ea05e3aSAlexander Block 	btrfs_set_root_rtransid(new_root_item, 0);
11898ea05e3aSAlexander Block 
1190925baeddSChris Mason 	old = btrfs_lock_root_node(root);
119149b25e05SJeff Mahoney 	ret = btrfs_cow_block(trans, root, old, NULL, 0, &old);
119279787eaaSJeff Mahoney 	if (ret) {
119379787eaaSJeff Mahoney 		btrfs_tree_unlock(old);
119479787eaaSJeff Mahoney 		free_extent_buffer(old);
11958732d44fSMiao Xie 		btrfs_abort_transaction(trans, root, ret);
11968732d44fSMiao Xie 		goto fail;
119779787eaaSJeff Mahoney 	}
119849b25e05SJeff Mahoney 
11995d4f98a2SYan Zheng 	btrfs_set_lock_blocking(old);
12003063d29fSChris Mason 
120149b25e05SJeff Mahoney 	ret = btrfs_copy_root(trans, root, old, &tmp, objectid);
120279787eaaSJeff Mahoney 	/* clean up in any case */
1203925baeddSChris Mason 	btrfs_tree_unlock(old);
1204925baeddSChris Mason 	free_extent_buffer(old);
12058732d44fSMiao Xie 	if (ret) {
12068732d44fSMiao Xie 		btrfs_abort_transaction(trans, root, ret);
12078732d44fSMiao Xie 		goto fail;
12088732d44fSMiao Xie 	}
12093063d29fSChris Mason 
1210f1ebcc74SLiu Bo 	/* see comments in should_cow_block() */
1211f1ebcc74SLiu Bo 	root->force_cow = 1;
1212f1ebcc74SLiu Bo 	smp_wmb();
1213f1ebcc74SLiu Bo 
12145d4f98a2SYan Zheng 	btrfs_set_root_node(new_root_item, tmp);
1215a22285a6SYan, Zheng 	/* record when the snapshot was created in key.offset */
1216a22285a6SYan, Zheng 	key.offset = trans->transid;
1217a22285a6SYan, Zheng 	ret = btrfs_insert_root(trans, tree_root, &key, new_root_item);
1218925baeddSChris Mason 	btrfs_tree_unlock(tmp);
12193063d29fSChris Mason 	free_extent_buffer(tmp);
12208732d44fSMiao Xie 	if (ret) {
12218732d44fSMiao Xie 		btrfs_abort_transaction(trans, root, ret);
12228732d44fSMiao Xie 		goto fail;
12238732d44fSMiao Xie 	}
12240660b5afSChris Mason 
1225a22285a6SYan, Zheng 	/*
1226a22285a6SYan, Zheng 	 * insert root back/forward references
1227a22285a6SYan, Zheng 	 */
1228a22285a6SYan, Zheng 	ret = btrfs_add_root_ref(trans, tree_root, objectid,
1229a22285a6SYan, Zheng 				 parent_root->root_key.objectid,
123033345d01SLi Zefan 				 btrfs_ino(parent_inode), index,
1231a22285a6SYan, Zheng 				 dentry->d_name.name, dentry->d_name.len);
12328732d44fSMiao Xie 	if (ret) {
12338732d44fSMiao Xie 		btrfs_abort_transaction(trans, root, ret);
12348732d44fSMiao Xie 		goto fail;
12358732d44fSMiao Xie 	}
1236a22285a6SYan, Zheng 
1237a22285a6SYan, Zheng 	key.offset = (u64)-1;
1238a22285a6SYan, Zheng 	pending->snap = btrfs_read_fs_root_no_name(root->fs_info, &key);
123979787eaaSJeff Mahoney 	if (IS_ERR(pending->snap)) {
124079787eaaSJeff Mahoney 		ret = PTR_ERR(pending->snap);
12418732d44fSMiao Xie 		btrfs_abort_transaction(trans, root, ret);
12428732d44fSMiao Xie 		goto fail;
124379787eaaSJeff Mahoney 	}
1244d68fc57bSYan, Zheng 
124549b25e05SJeff Mahoney 	ret = btrfs_reloc_post_snapshot(trans, pending);
12468732d44fSMiao Xie 	if (ret) {
12478732d44fSMiao Xie 		btrfs_abort_transaction(trans, root, ret);
12488732d44fSMiao Xie 		goto fail;
12498732d44fSMiao Xie 	}
1250361048f5SMiao Xie 
1251361048f5SMiao Xie 	ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
12528732d44fSMiao Xie 	if (ret) {
12538732d44fSMiao Xie 		btrfs_abort_transaction(trans, root, ret);
12548732d44fSMiao Xie 		goto fail;
12558732d44fSMiao Xie 	}
125642874b3dSMiao Xie 
125742874b3dSMiao Xie 	ret = btrfs_insert_dir_item(trans, parent_root,
125842874b3dSMiao Xie 				    dentry->d_name.name, dentry->d_name.len,
125942874b3dSMiao Xie 				    parent_inode, &key,
126042874b3dSMiao Xie 				    BTRFS_FT_DIR, index);
126142874b3dSMiao Xie 	/* We have check then name at the beginning, so it is impossible. */
12629c52057cSChris Mason 	BUG_ON(ret == -EEXIST || ret == -EOVERFLOW);
12638732d44fSMiao Xie 	if (ret) {
12648732d44fSMiao Xie 		btrfs_abort_transaction(trans, root, ret);
12658732d44fSMiao Xie 		goto fail;
12668732d44fSMiao Xie 	}
126742874b3dSMiao Xie 
126842874b3dSMiao Xie 	btrfs_i_size_write(parent_inode, parent_inode->i_size +
126942874b3dSMiao Xie 					 dentry->d_name.len * 2);
127042874b3dSMiao Xie 	parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
1271be6aef60SJosef Bacik 	ret = btrfs_update_inode_fallback(trans, parent_root, parent_inode);
127242874b3dSMiao Xie 	if (ret)
12738732d44fSMiao Xie 		btrfs_abort_transaction(trans, root, ret);
12743063d29fSChris Mason fail:
1275aec8030aSMiao Xie 	pending->error = ret;
1276aec8030aSMiao Xie dir_item_existed:
127798c9942aSLiu Bo 	trans->block_rsv = rsv;
12782382c5ccSLiu Bo 	trans->bytes_reserved = 0;
12796fa9700eSMiao Xie no_free_objectid:
12806fa9700eSMiao Xie 	kfree(new_root_item);
12816fa9700eSMiao Xie root_item_alloc_fail:
128242874b3dSMiao Xie 	btrfs_free_path(path);
128349b25e05SJeff Mahoney 	return ret;
12843063d29fSChris Mason }
12853063d29fSChris Mason 
1286d352ac68SChris Mason /*
1287d352ac68SChris Mason  * create all the snapshots we've scheduled for creation
1288d352ac68SChris Mason  */
128980b6794dSChris Mason static noinline int create_pending_snapshots(struct btrfs_trans_handle *trans,
12903063d29fSChris Mason 					     struct btrfs_fs_info *fs_info)
12913063d29fSChris Mason {
1292aec8030aSMiao Xie 	struct btrfs_pending_snapshot *pending, *next;
12933063d29fSChris Mason 	struct list_head *head = &trans->transaction->pending_snapshots;
1294aec8030aSMiao Xie 	int ret = 0;
12953de4586cSChris Mason 
1296aec8030aSMiao Xie 	list_for_each_entry_safe(pending, next, head, list) {
1297aec8030aSMiao Xie 		list_del(&pending->list);
1298aec8030aSMiao Xie 		ret = create_pending_snapshot(trans, fs_info, pending);
1299aec8030aSMiao Xie 		if (ret)
1300aec8030aSMiao Xie 			break;
1301aec8030aSMiao Xie 	}
1302aec8030aSMiao Xie 	return ret;
13033de4586cSChris Mason }
13043de4586cSChris Mason 
13055d4f98a2SYan Zheng static void update_super_roots(struct btrfs_root *root)
13065d4f98a2SYan Zheng {
13075d4f98a2SYan Zheng 	struct btrfs_root_item *root_item;
13085d4f98a2SYan Zheng 	struct btrfs_super_block *super;
13095d4f98a2SYan Zheng 
13106c41761fSDavid Sterba 	super = root->fs_info->super_copy;
13115d4f98a2SYan Zheng 
13125d4f98a2SYan Zheng 	root_item = &root->fs_info->chunk_root->root_item;
13135d4f98a2SYan Zheng 	super->chunk_root = root_item->bytenr;
13145d4f98a2SYan Zheng 	super->chunk_root_generation = root_item->generation;
13155d4f98a2SYan Zheng 	super->chunk_root_level = root_item->level;
13165d4f98a2SYan Zheng 
13175d4f98a2SYan Zheng 	root_item = &root->fs_info->tree_root->root_item;
13185d4f98a2SYan Zheng 	super->root = root_item->bytenr;
13195d4f98a2SYan Zheng 	super->generation = root_item->generation;
13205d4f98a2SYan Zheng 	super->root_level = root_item->level;
132173bc1876SJosef Bacik 	if (btrfs_test_opt(root, SPACE_CACHE))
13220af3d00bSJosef Bacik 		super->cache_generation = root_item->generation;
13235d4f98a2SYan Zheng }
13245d4f98a2SYan Zheng 
1325f36f3042SChris Mason int btrfs_transaction_in_commit(struct btrfs_fs_info *info)
1326f36f3042SChris Mason {
1327f36f3042SChris Mason 	int ret = 0;
1328a4abeea4SJosef Bacik 	spin_lock(&info->trans_lock);
1329f36f3042SChris Mason 	if (info->running_transaction)
1330f36f3042SChris Mason 		ret = info->running_transaction->in_commit;
1331a4abeea4SJosef Bacik 	spin_unlock(&info->trans_lock);
1332f36f3042SChris Mason 	return ret;
1333f36f3042SChris Mason }
1334f36f3042SChris Mason 
13358929ecfaSYan, Zheng int btrfs_transaction_blocked(struct btrfs_fs_info *info)
13368929ecfaSYan, Zheng {
13378929ecfaSYan, Zheng 	int ret = 0;
1338a4abeea4SJosef Bacik 	spin_lock(&info->trans_lock);
13398929ecfaSYan, Zheng 	if (info->running_transaction)
13408929ecfaSYan, Zheng 		ret = info->running_transaction->blocked;
1341a4abeea4SJosef Bacik 	spin_unlock(&info->trans_lock);
13428929ecfaSYan, Zheng 	return ret;
13438929ecfaSYan, Zheng }
13448929ecfaSYan, Zheng 
1345bb9c12c9SSage Weil /*
1346bb9c12c9SSage Weil  * wait for the current transaction commit to start and block subsequent
1347bb9c12c9SSage Weil  * transaction joins
1348bb9c12c9SSage Weil  */
1349bb9c12c9SSage Weil static void wait_current_trans_commit_start(struct btrfs_root *root,
1350bb9c12c9SSage Weil 					    struct btrfs_transaction *trans)
1351bb9c12c9SSage Weil {
135272d63ed6SLi Zefan 	wait_event(root->fs_info->transaction_blocked_wait, trans->in_commit);
1353bb9c12c9SSage Weil }
1354bb9c12c9SSage Weil 
1355bb9c12c9SSage Weil /*
1356bb9c12c9SSage Weil  * wait for the current transaction to start and then become unblocked.
1357bb9c12c9SSage Weil  * caller holds ref.
1358bb9c12c9SSage Weil  */
1359bb9c12c9SSage Weil static void wait_current_trans_commit_start_and_unblock(struct btrfs_root *root,
1360bb9c12c9SSage Weil 					 struct btrfs_transaction *trans)
1361bb9c12c9SSage Weil {
136272d63ed6SLi Zefan 	wait_event(root->fs_info->transaction_wait,
136372d63ed6SLi Zefan 		   trans->commit_done || (trans->in_commit && !trans->blocked));
1364bb9c12c9SSage Weil }
1365bb9c12c9SSage Weil 
1366bb9c12c9SSage Weil /*
1367bb9c12c9SSage Weil  * commit transactions asynchronously. once btrfs_commit_transaction_async
1368bb9c12c9SSage Weil  * returns, any subsequent transaction will not be allowed to join.
1369bb9c12c9SSage Weil  */
1370bb9c12c9SSage Weil struct btrfs_async_commit {
1371bb9c12c9SSage Weil 	struct btrfs_trans_handle *newtrans;
1372bb9c12c9SSage Weil 	struct btrfs_root *root;
13737892b5afSMiao Xie 	struct work_struct work;
1374bb9c12c9SSage Weil };
1375bb9c12c9SSage Weil 
1376bb9c12c9SSage Weil static void do_async_commit(struct work_struct *work)
1377bb9c12c9SSage Weil {
1378bb9c12c9SSage Weil 	struct btrfs_async_commit *ac =
13797892b5afSMiao Xie 		container_of(work, struct btrfs_async_commit, work);
1380bb9c12c9SSage Weil 
13816fc4e354SSage Weil 	/*
13826fc4e354SSage Weil 	 * We've got freeze protection passed with the transaction.
13836fc4e354SSage Weil 	 * Tell lockdep about it.
13846fc4e354SSage Weil 	 */
1385ff7c1d33SMiao Xie 	if (ac->newtrans->type < TRANS_JOIN_NOLOCK)
13866fc4e354SSage Weil 		rwsem_acquire_read(
13876fc4e354SSage Weil 		     &ac->root->fs_info->sb->s_writers.lock_map[SB_FREEZE_FS-1],
13886fc4e354SSage Weil 		     0, 1, _THIS_IP_);
13896fc4e354SSage Weil 
1390e209db7aSSage Weil 	current->journal_info = ac->newtrans;
1391e209db7aSSage Weil 
1392bb9c12c9SSage Weil 	btrfs_commit_transaction(ac->newtrans, ac->root);
1393bb9c12c9SSage Weil 	kfree(ac);
1394bb9c12c9SSage Weil }
1395bb9c12c9SSage Weil 
1396bb9c12c9SSage Weil int btrfs_commit_transaction_async(struct btrfs_trans_handle *trans,
1397bb9c12c9SSage Weil 				   struct btrfs_root *root,
1398bb9c12c9SSage Weil 				   int wait_for_unblock)
1399bb9c12c9SSage Weil {
1400bb9c12c9SSage Weil 	struct btrfs_async_commit *ac;
1401bb9c12c9SSage Weil 	struct btrfs_transaction *cur_trans;
1402bb9c12c9SSage Weil 
1403bb9c12c9SSage Weil 	ac = kmalloc(sizeof(*ac), GFP_NOFS);
1404db5b493aSTsutomu Itoh 	if (!ac)
1405db5b493aSTsutomu Itoh 		return -ENOMEM;
1406bb9c12c9SSage Weil 
14077892b5afSMiao Xie 	INIT_WORK(&ac->work, do_async_commit);
1408bb9c12c9SSage Weil 	ac->root = root;
14097a7eaa40SJosef Bacik 	ac->newtrans = btrfs_join_transaction(root);
14103612b495STsutomu Itoh 	if (IS_ERR(ac->newtrans)) {
14113612b495STsutomu Itoh 		int err = PTR_ERR(ac->newtrans);
14123612b495STsutomu Itoh 		kfree(ac);
14133612b495STsutomu Itoh 		return err;
14143612b495STsutomu Itoh 	}
1415bb9c12c9SSage Weil 
1416bb9c12c9SSage Weil 	/* take transaction reference */
1417bb9c12c9SSage Weil 	cur_trans = trans->transaction;
141813c5a93eSJosef Bacik 	atomic_inc(&cur_trans->use_count);
1419bb9c12c9SSage Weil 
1420bb9c12c9SSage Weil 	btrfs_end_transaction(trans, root);
14216fc4e354SSage Weil 
14226fc4e354SSage Weil 	/*
14236fc4e354SSage Weil 	 * Tell lockdep we've released the freeze rwsem, since the
14246fc4e354SSage Weil 	 * async commit thread will be the one to unlock it.
14256fc4e354SSage Weil 	 */
1426ff7c1d33SMiao Xie 	if (trans->type < TRANS_JOIN_NOLOCK)
1427ff7c1d33SMiao Xie 		rwsem_release(
1428ff7c1d33SMiao Xie 			&root->fs_info->sb->s_writers.lock_map[SB_FREEZE_FS-1],
14296fc4e354SSage Weil 			1, _THIS_IP_);
14306fc4e354SSage Weil 
14317892b5afSMiao Xie 	schedule_work(&ac->work);
1432bb9c12c9SSage Weil 
1433bb9c12c9SSage Weil 	/* wait for transaction to start and unblock */
1434bb9c12c9SSage Weil 	if (wait_for_unblock)
1435bb9c12c9SSage Weil 		wait_current_trans_commit_start_and_unblock(root, cur_trans);
1436bb9c12c9SSage Weil 	else
1437bb9c12c9SSage Weil 		wait_current_trans_commit_start(root, cur_trans);
1438bb9c12c9SSage Weil 
143938e88054SSage Weil 	if (current->journal_info == trans)
144038e88054SSage Weil 		current->journal_info = NULL;
144138e88054SSage Weil 
144238e88054SSage Weil 	put_transaction(cur_trans);
1443bb9c12c9SSage Weil 	return 0;
1444bb9c12c9SSage Weil }
1445bb9c12c9SSage Weil 
144649b25e05SJeff Mahoney 
144749b25e05SJeff Mahoney static void cleanup_transaction(struct btrfs_trans_handle *trans,
14487b8b92afSJosef Bacik 				struct btrfs_root *root, int err)
144949b25e05SJeff Mahoney {
145049b25e05SJeff Mahoney 	struct btrfs_transaction *cur_trans = trans->transaction;
1451f094ac32SLiu Bo 	DEFINE_WAIT(wait);
145249b25e05SJeff Mahoney 
145349b25e05SJeff Mahoney 	WARN_ON(trans->use_count > 1);
145449b25e05SJeff Mahoney 
14557b8b92afSJosef Bacik 	btrfs_abort_transaction(trans, root, err);
14567b8b92afSJosef Bacik 
145749b25e05SJeff Mahoney 	spin_lock(&root->fs_info->trans_lock);
145866b6135bSLiu Bo 
145966b6135bSLiu Bo 	if (list_empty(&cur_trans->list)) {
146066b6135bSLiu Bo 		spin_unlock(&root->fs_info->trans_lock);
146166b6135bSLiu Bo 		btrfs_end_transaction(trans, root);
146266b6135bSLiu Bo 		return;
146366b6135bSLiu Bo 	}
146466b6135bSLiu Bo 
146549b25e05SJeff Mahoney 	list_del_init(&cur_trans->list);
1466d7096fc3SJosef Bacik 	if (cur_trans == root->fs_info->running_transaction) {
1467f094ac32SLiu Bo 		root->fs_info->trans_no_join = 1;
1468f094ac32SLiu Bo 		spin_unlock(&root->fs_info->trans_lock);
1469f094ac32SLiu Bo 		wait_event(cur_trans->writer_wait,
1470f094ac32SLiu Bo 			   atomic_read(&cur_trans->num_writers) == 1);
1471f094ac32SLiu Bo 
1472f094ac32SLiu Bo 		spin_lock(&root->fs_info->trans_lock);
1473d7096fc3SJosef Bacik 		root->fs_info->running_transaction = NULL;
1474d7096fc3SJosef Bacik 	}
147549b25e05SJeff Mahoney 	spin_unlock(&root->fs_info->trans_lock);
147649b25e05SJeff Mahoney 
147749b25e05SJeff Mahoney 	btrfs_cleanup_one_transaction(trans->transaction, root);
147849b25e05SJeff Mahoney 
147949b25e05SJeff Mahoney 	put_transaction(cur_trans);
148049b25e05SJeff Mahoney 	put_transaction(cur_trans);
148149b25e05SJeff Mahoney 
148249b25e05SJeff Mahoney 	trace_btrfs_transaction_commit(root);
148349b25e05SJeff Mahoney 
148449b25e05SJeff Mahoney 	btrfs_scrub_continue(root);
148549b25e05SJeff Mahoney 
148649b25e05SJeff Mahoney 	if (current->journal_info == trans)
148749b25e05SJeff Mahoney 		current->journal_info = NULL;
148849b25e05SJeff Mahoney 
148949b25e05SJeff Mahoney 	kmem_cache_free(btrfs_trans_handle_cachep, trans);
149049b25e05SJeff Mahoney }
149149b25e05SJeff Mahoney 
1492ca469637SMiao Xie static int btrfs_flush_all_pending_stuffs(struct btrfs_trans_handle *trans,
1493ca469637SMiao Xie 					  struct btrfs_root *root)
1494ca469637SMiao Xie {
1495ca469637SMiao Xie 	int flush_on_commit = btrfs_test_opt(root, FLUSHONCOMMIT);
1496ca469637SMiao Xie 	int snap_pending = 0;
1497ca469637SMiao Xie 	int ret;
1498ca469637SMiao Xie 
1499ca469637SMiao Xie 	if (!flush_on_commit) {
1500ca469637SMiao Xie 		spin_lock(&root->fs_info->trans_lock);
1501ca469637SMiao Xie 		if (!list_empty(&trans->transaction->pending_snapshots))
1502ca469637SMiao Xie 			snap_pending = 1;
1503ca469637SMiao Xie 		spin_unlock(&root->fs_info->trans_lock);
1504ca469637SMiao Xie 	}
1505ca469637SMiao Xie 
1506ca469637SMiao Xie 	if (flush_on_commit || snap_pending) {
15073edb2a68SMiao Xie 		ret = btrfs_start_delalloc_inodes(root, 1);
15083edb2a68SMiao Xie 		if (ret)
15093edb2a68SMiao Xie 			return ret;
1510ca469637SMiao Xie 		btrfs_wait_ordered_extents(root, 1);
1511ca469637SMiao Xie 	}
1512ca469637SMiao Xie 
1513ca469637SMiao Xie 	ret = btrfs_run_delayed_items(trans, root);
1514ca469637SMiao Xie 	if (ret)
1515ca469637SMiao Xie 		return ret;
1516ca469637SMiao Xie 
1517ca469637SMiao Xie 	/*
1518ca469637SMiao Xie 	 * running the delayed items may have added new refs. account
1519ca469637SMiao Xie 	 * them now so that they hinder processing of more delayed refs
1520ca469637SMiao Xie 	 * as little as possible.
1521ca469637SMiao Xie 	 */
1522ca469637SMiao Xie 	btrfs_delayed_refs_qgroup_accounting(trans, root->fs_info);
1523ca469637SMiao Xie 
1524ca469637SMiao Xie 	/*
1525ca469637SMiao Xie 	 * rename don't use btrfs_join_transaction, so, once we
1526ca469637SMiao Xie 	 * set the transaction to blocked above, we aren't going
1527ca469637SMiao Xie 	 * to get any new ordered operations.  We can safely run
1528ca469637SMiao Xie 	 * it here and no for sure that nothing new will be added
1529ca469637SMiao Xie 	 * to the list
1530ca469637SMiao Xie 	 */
1531569e0f35SJosef Bacik 	ret = btrfs_run_ordered_operations(trans, root, 1);
1532ca469637SMiao Xie 
1533eebc6084SMiao Xie 	return ret;
1534ca469637SMiao Xie }
1535ca469637SMiao Xie 
1536bb9c12c9SSage Weil /*
1537bb9c12c9SSage Weil  * btrfs_transaction state sequence:
1538bb9c12c9SSage Weil  *    in_commit = 0, blocked = 0  (initial)
1539bb9c12c9SSage Weil  *    in_commit = 1, blocked = 1
1540bb9c12c9SSage Weil  *    blocked = 0
1541bb9c12c9SSage Weil  *    commit_done = 1
1542bb9c12c9SSage Weil  */
154379154b1bSChris Mason int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
154479154b1bSChris Mason 			     struct btrfs_root *root)
154579154b1bSChris Mason {
154615ee9bc7SJosef Bacik 	unsigned long joined = 0;
154749b25e05SJeff Mahoney 	struct btrfs_transaction *cur_trans = trans->transaction;
15488fd17795SChris Mason 	struct btrfs_transaction *prev_trans = NULL;
154979154b1bSChris Mason 	DEFINE_WAIT(wait);
155025287e0aSMiao Xie 	int ret;
155189573b9cSChris Mason 	int should_grow = 0;
155289573b9cSChris Mason 	unsigned long now = get_seconds();
155379154b1bSChris Mason 
1554569e0f35SJosef Bacik 	ret = btrfs_run_ordered_operations(trans, root, 0);
155525287e0aSMiao Xie 	if (ret) {
155625287e0aSMiao Xie 		btrfs_abort_transaction(trans, root, ret);
1557e4a2bcacSJosef Bacik 		btrfs_end_transaction(trans, root);
1558e4a2bcacSJosef Bacik 		return ret;
155925287e0aSMiao Xie 	}
156025287e0aSMiao Xie 
15618d25a086SMiao Xie 	/* Stop the commit early if ->aborted is set */
15628d25a086SMiao Xie 	if (unlikely(ACCESS_ONCE(cur_trans->aborted))) {
156325287e0aSMiao Xie 		ret = cur_trans->aborted;
1564e4a2bcacSJosef Bacik 		btrfs_end_transaction(trans, root);
1565e4a2bcacSJosef Bacik 		return ret;
156625287e0aSMiao Xie 	}
156749b25e05SJeff Mahoney 
156856bec294SChris Mason 	/* make a pass through all the delayed refs we have so far
156956bec294SChris Mason 	 * any runnings procs may add more while we are here
157056bec294SChris Mason 	 */
157156bec294SChris Mason 	ret = btrfs_run_delayed_refs(trans, root, 0);
1572e4a2bcacSJosef Bacik 	if (ret) {
1573e4a2bcacSJosef Bacik 		btrfs_end_transaction(trans, root);
1574e4a2bcacSJosef Bacik 		return ret;
1575e4a2bcacSJosef Bacik 	}
157656bec294SChris Mason 
15770e721106SJosef Bacik 	btrfs_trans_release_metadata(trans, root);
15780e721106SJosef Bacik 	trans->block_rsv = NULL;
1579272d26d0SMiao Xie 	if (trans->qgroup_reserved) {
1580272d26d0SMiao Xie 		btrfs_qgroup_free(root, trans->qgroup_reserved);
1581272d26d0SMiao Xie 		trans->qgroup_reserved = 0;
1582272d26d0SMiao Xie 	}
15830e721106SJosef Bacik 
1584b7ec40d7SChris Mason 	cur_trans = trans->transaction;
158549b25e05SJeff Mahoney 
158656bec294SChris Mason 	/*
158756bec294SChris Mason 	 * set the flushing flag so procs in this transaction have to
158856bec294SChris Mason 	 * start sending their work down.
158956bec294SChris Mason 	 */
1590b7ec40d7SChris Mason 	cur_trans->delayed_refs.flushing = 1;
159156bec294SChris Mason 
1592ea658badSJosef Bacik 	if (!list_empty(&trans->new_bgs))
1593ea658badSJosef Bacik 		btrfs_create_pending_block_groups(trans, root);
1594ea658badSJosef Bacik 
1595c3e69d58SChris Mason 	ret = btrfs_run_delayed_refs(trans, root, 0);
1596e4a2bcacSJosef Bacik 	if (ret) {
1597e4a2bcacSJosef Bacik 		btrfs_end_transaction(trans, root);
1598e4a2bcacSJosef Bacik 		return ret;
1599e4a2bcacSJosef Bacik 	}
160056bec294SChris Mason 
1601a4abeea4SJosef Bacik 	spin_lock(&cur_trans->commit_lock);
1602b7ec40d7SChris Mason 	if (cur_trans->in_commit) {
1603a4abeea4SJosef Bacik 		spin_unlock(&cur_trans->commit_lock);
160413c5a93eSJosef Bacik 		atomic_inc(&cur_trans->use_count);
160549b25e05SJeff Mahoney 		ret = btrfs_end_transaction(trans, root);
1606ccd467d6SChris Mason 
1607b9c8300cSLi Zefan 		wait_for_commit(root, cur_trans);
160815ee9bc7SJosef Bacik 
160979154b1bSChris Mason 		put_transaction(cur_trans);
161015ee9bc7SJosef Bacik 
161149b25e05SJeff Mahoney 		return ret;
161279154b1bSChris Mason 	}
16134313b399SChris Mason 
16142c90e5d6SChris Mason 	trans->transaction->in_commit = 1;
1615f9295749SChris Mason 	trans->transaction->blocked = 1;
1616a4abeea4SJosef Bacik 	spin_unlock(&cur_trans->commit_lock);
1617bb9c12c9SSage Weil 	wake_up(&root->fs_info->transaction_blocked_wait);
1618bb9c12c9SSage Weil 
1619a4abeea4SJosef Bacik 	spin_lock(&root->fs_info->trans_lock);
1620ccd467d6SChris Mason 	if (cur_trans->list.prev != &root->fs_info->trans_list) {
1621ccd467d6SChris Mason 		prev_trans = list_entry(cur_trans->list.prev,
1622ccd467d6SChris Mason 					struct btrfs_transaction, list);
1623ccd467d6SChris Mason 		if (!prev_trans->commit_done) {
162413c5a93eSJosef Bacik 			atomic_inc(&prev_trans->use_count);
1625a4abeea4SJosef Bacik 			spin_unlock(&root->fs_info->trans_lock);
1626ccd467d6SChris Mason 
1627ccd467d6SChris Mason 			wait_for_commit(root, prev_trans);
1628ccd467d6SChris Mason 
162915ee9bc7SJosef Bacik 			put_transaction(prev_trans);
1630a4abeea4SJosef Bacik 		} else {
1631a4abeea4SJosef Bacik 			spin_unlock(&root->fs_info->trans_lock);
1632ccd467d6SChris Mason 		}
1633a4abeea4SJosef Bacik 	} else {
1634a4abeea4SJosef Bacik 		spin_unlock(&root->fs_info->trans_lock);
1635ccd467d6SChris Mason 	}
163615ee9bc7SJosef Bacik 
1637e39e64acSChris Mason 	if (!btrfs_test_opt(root, SSD) &&
1638e39e64acSChris Mason 	    (now < cur_trans->start_time || now - cur_trans->start_time < 1))
163989573b9cSChris Mason 		should_grow = 1;
164089573b9cSChris Mason 
164115ee9bc7SJosef Bacik 	do {
164215ee9bc7SJosef Bacik 		joined = cur_trans->num_joined;
16437ea394f1SYan Zheng 
16442c90e5d6SChris Mason 		WARN_ON(cur_trans != trans->transaction);
164515ee9bc7SJosef Bacik 
1646ca469637SMiao Xie 		ret = btrfs_flush_all_pending_stuffs(trans, root);
164749b25e05SJeff Mahoney 		if (ret)
164849b25e05SJeff Mahoney 			goto cleanup_transaction;
164916cdcec7SMiao Xie 
1650ed3b3d31SChris Mason 		prepare_to_wait(&cur_trans->writer_wait, &wait,
1651ed3b3d31SChris Mason 				TASK_UNINTERRUPTIBLE);
1652ed3b3d31SChris Mason 
165313c5a93eSJosef Bacik 		if (atomic_read(&cur_trans->num_writers) > 1)
165499d16cbcSSage Weil 			schedule_timeout(MAX_SCHEDULE_TIMEOUT);
165599d16cbcSSage Weil 		else if (should_grow)
165699d16cbcSSage Weil 			schedule_timeout(1);
165715ee9bc7SJosef Bacik 
165815ee9bc7SJosef Bacik 		finish_wait(&cur_trans->writer_wait, &wait);
1659ed0ca140SJosef Bacik 	} while (atomic_read(&cur_trans->num_writers) > 1 ||
1660ed0ca140SJosef Bacik 		 (should_grow && cur_trans->num_joined != joined));
1661ed0ca140SJosef Bacik 
1662ca469637SMiao Xie 	ret = btrfs_flush_all_pending_stuffs(trans, root);
1663ca469637SMiao Xie 	if (ret)
1664ca469637SMiao Xie 		goto cleanup_transaction;
1665ca469637SMiao Xie 
1666ed0ca140SJosef Bacik 	/*
1667ed0ca140SJosef Bacik 	 * Ok now we need to make sure to block out any other joins while we
1668ed0ca140SJosef Bacik 	 * commit the transaction.  We could have started a join before setting
1669ed0ca140SJosef Bacik 	 * no_join so make sure to wait for num_writers to == 1 again.
1670ed0ca140SJosef Bacik 	 */
1671a4abeea4SJosef Bacik 	spin_lock(&root->fs_info->trans_lock);
1672a4abeea4SJosef Bacik 	root->fs_info->trans_no_join = 1;
1673a4abeea4SJosef Bacik 	spin_unlock(&root->fs_info->trans_lock);
1674ed0ca140SJosef Bacik 	wait_event(cur_trans->writer_wait,
1675ed0ca140SJosef Bacik 		   atomic_read(&cur_trans->num_writers) == 1);
167615ee9bc7SJosef Bacik 
16772cba30f1SMiao Xie 	/* ->aborted might be set after the previous check, so check it */
16782cba30f1SMiao Xie 	if (unlikely(ACCESS_ONCE(cur_trans->aborted))) {
16792cba30f1SMiao Xie 		ret = cur_trans->aborted;
16802cba30f1SMiao Xie 		goto cleanup_transaction;
16812cba30f1SMiao Xie 	}
16827585717fSChris Mason 	/*
16837585717fSChris Mason 	 * the reloc mutex makes sure that we stop
16847585717fSChris Mason 	 * the balancing code from coming in and moving
16857585717fSChris Mason 	 * extents around in the middle of the commit
16867585717fSChris Mason 	 */
16877585717fSChris Mason 	mutex_lock(&root->fs_info->reloc_mutex);
16887585717fSChris Mason 
168942874b3dSMiao Xie 	/*
169042874b3dSMiao Xie 	 * We needn't worry about the delayed items because we will
169142874b3dSMiao Xie 	 * deal with them in create_pending_snapshot(), which is the
169242874b3dSMiao Xie 	 * core function of the snapshot creation.
169342874b3dSMiao Xie 	 */
169442874b3dSMiao Xie 	ret = create_pending_snapshots(trans, root->fs_info);
169549b25e05SJeff Mahoney 	if (ret) {
169649b25e05SJeff Mahoney 		mutex_unlock(&root->fs_info->reloc_mutex);
169749b25e05SJeff Mahoney 		goto cleanup_transaction;
169849b25e05SJeff Mahoney 	}
16993063d29fSChris Mason 
170042874b3dSMiao Xie 	/*
170142874b3dSMiao Xie 	 * We insert the dir indexes of the snapshots and update the inode
170242874b3dSMiao Xie 	 * of the snapshots' parents after the snapshot creation, so there
170342874b3dSMiao Xie 	 * are some delayed items which are not dealt with. Now deal with
170442874b3dSMiao Xie 	 * them.
170542874b3dSMiao Xie 	 *
170642874b3dSMiao Xie 	 * We needn't worry that this operation will corrupt the snapshots,
170742874b3dSMiao Xie 	 * because all the tree which are snapshoted will be forced to COW
170842874b3dSMiao Xie 	 * the nodes and leaves.
170942874b3dSMiao Xie 	 */
171042874b3dSMiao Xie 	ret = btrfs_run_delayed_items(trans, root);
171149b25e05SJeff Mahoney 	if (ret) {
171249b25e05SJeff Mahoney 		mutex_unlock(&root->fs_info->reloc_mutex);
171349b25e05SJeff Mahoney 		goto cleanup_transaction;
171449b25e05SJeff Mahoney 	}
171516cdcec7SMiao Xie 
171656bec294SChris Mason 	ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
171749b25e05SJeff Mahoney 	if (ret) {
171849b25e05SJeff Mahoney 		mutex_unlock(&root->fs_info->reloc_mutex);
171949b25e05SJeff Mahoney 		goto cleanup_transaction;
172049b25e05SJeff Mahoney 	}
172156bec294SChris Mason 
1722e999376fSChris Mason 	/*
1723e999376fSChris Mason 	 * make sure none of the code above managed to slip in a
1724e999376fSChris Mason 	 * delayed item
1725e999376fSChris Mason 	 */
1726e999376fSChris Mason 	btrfs_assert_delayed_root_empty(root);
1727e999376fSChris Mason 
17282c90e5d6SChris Mason 	WARN_ON(cur_trans != trans->transaction);
1729dc17ff8fSChris Mason 
1730a2de733cSArne Jansen 	btrfs_scrub_pause(root);
1731e02119d5SChris Mason 	/* btrfs_commit_tree_roots is responsible for getting the
1732e02119d5SChris Mason 	 * various roots consistent with each other.  Every pointer
1733e02119d5SChris Mason 	 * in the tree of tree roots has to point to the most up to date
1734e02119d5SChris Mason 	 * root for every subvolume and other tree.  So, we have to keep
1735e02119d5SChris Mason 	 * the tree logging code from jumping in and changing any
1736e02119d5SChris Mason 	 * of the trees.
1737e02119d5SChris Mason 	 *
1738e02119d5SChris Mason 	 * At this point in the commit, there can't be any tree-log
1739e02119d5SChris Mason 	 * writers, but a little lower down we drop the trans mutex
1740e02119d5SChris Mason 	 * and let new people in.  By holding the tree_log_mutex
1741e02119d5SChris Mason 	 * from now until after the super is written, we avoid races
1742e02119d5SChris Mason 	 * with the tree-log code.
1743e02119d5SChris Mason 	 */
1744e02119d5SChris Mason 	mutex_lock(&root->fs_info->tree_log_mutex);
17451a40e23bSZheng Yan 
17465d4f98a2SYan Zheng 	ret = commit_fs_roots(trans, root);
174749b25e05SJeff Mahoney 	if (ret) {
174849b25e05SJeff Mahoney 		mutex_unlock(&root->fs_info->tree_log_mutex);
1749871383beSDavid Sterba 		mutex_unlock(&root->fs_info->reloc_mutex);
175049b25e05SJeff Mahoney 		goto cleanup_transaction;
175149b25e05SJeff Mahoney 	}
175254aa1f4dSChris Mason 
17535d4f98a2SYan Zheng 	/* commit_fs_roots gets rid of all the tree log roots, it is now
1754e02119d5SChris Mason 	 * safe to free the root of tree log roots
1755e02119d5SChris Mason 	 */
1756e02119d5SChris Mason 	btrfs_free_log_root_tree(trans, root->fs_info);
1757e02119d5SChris Mason 
17585d4f98a2SYan Zheng 	ret = commit_cowonly_roots(trans, root);
175949b25e05SJeff Mahoney 	if (ret) {
176049b25e05SJeff Mahoney 		mutex_unlock(&root->fs_info->tree_log_mutex);
1761871383beSDavid Sterba 		mutex_unlock(&root->fs_info->reloc_mutex);
176249b25e05SJeff Mahoney 		goto cleanup_transaction;
176349b25e05SJeff Mahoney 	}
176454aa1f4dSChris Mason 
17652cba30f1SMiao Xie 	/*
17662cba30f1SMiao Xie 	 * The tasks which save the space cache and inode cache may also
17672cba30f1SMiao Xie 	 * update ->aborted, check it.
17682cba30f1SMiao Xie 	 */
17692cba30f1SMiao Xie 	if (unlikely(ACCESS_ONCE(cur_trans->aborted))) {
17702cba30f1SMiao Xie 		ret = cur_trans->aborted;
17712cba30f1SMiao Xie 		mutex_unlock(&root->fs_info->tree_log_mutex);
17722cba30f1SMiao Xie 		mutex_unlock(&root->fs_info->reloc_mutex);
17732cba30f1SMiao Xie 		goto cleanup_transaction;
17742cba30f1SMiao Xie 	}
17752cba30f1SMiao Xie 
177611833d66SYan Zheng 	btrfs_prepare_extent_commit(trans, root);
177711833d66SYan Zheng 
177878fae27eSChris Mason 	cur_trans = root->fs_info->running_transaction;
17795f39d397SChris Mason 
17805d4f98a2SYan Zheng 	btrfs_set_root_node(&root->fs_info->tree_root->root_item,
17815d4f98a2SYan Zheng 			    root->fs_info->tree_root->node);
1782817d52f8SJosef Bacik 	switch_commit_root(root->fs_info->tree_root);
17835d4f98a2SYan Zheng 
17845d4f98a2SYan Zheng 	btrfs_set_root_node(&root->fs_info->chunk_root->root_item,
17855d4f98a2SYan Zheng 			    root->fs_info->chunk_root->node);
1786817d52f8SJosef Bacik 	switch_commit_root(root->fs_info->chunk_root);
17875d4f98a2SYan Zheng 
1788edf39272SJan Schmidt 	assert_qgroups_uptodate(trans);
17895d4f98a2SYan Zheng 	update_super_roots(root);
1790e02119d5SChris Mason 
1791e02119d5SChris Mason 	if (!root->fs_info->log_root_recovering) {
17926c41761fSDavid Sterba 		btrfs_set_super_log_root(root->fs_info->super_copy, 0);
17936c41761fSDavid Sterba 		btrfs_set_super_log_root_level(root->fs_info->super_copy, 0);
1794e02119d5SChris Mason 	}
1795e02119d5SChris Mason 
17966c41761fSDavid Sterba 	memcpy(root->fs_info->super_for_commit, root->fs_info->super_copy,
17976c41761fSDavid Sterba 	       sizeof(*root->fs_info->super_copy));
1798ccd467d6SChris Mason 
1799f9295749SChris Mason 	trans->transaction->blocked = 0;
1800a4abeea4SJosef Bacik 	spin_lock(&root->fs_info->trans_lock);
1801a4abeea4SJosef Bacik 	root->fs_info->running_transaction = NULL;
1802a4abeea4SJosef Bacik 	root->fs_info->trans_no_join = 0;
1803a4abeea4SJosef Bacik 	spin_unlock(&root->fs_info->trans_lock);
18047585717fSChris Mason 	mutex_unlock(&root->fs_info->reloc_mutex);
1805b7ec40d7SChris Mason 
1806f9295749SChris Mason 	wake_up(&root->fs_info->transaction_wait);
1807e6dcd2dcSChris Mason 
180879154b1bSChris Mason 	ret = btrfs_write_and_wait_transaction(trans, root);
180949b25e05SJeff Mahoney 	if (ret) {
181049b25e05SJeff Mahoney 		btrfs_error(root->fs_info, ret,
181108748810SDavid Sterba 			    "Error while writing out transaction");
181249b25e05SJeff Mahoney 		mutex_unlock(&root->fs_info->tree_log_mutex);
181349b25e05SJeff Mahoney 		goto cleanup_transaction;
181449b25e05SJeff Mahoney 	}
181549b25e05SJeff Mahoney 
181649b25e05SJeff Mahoney 	ret = write_ctree_super(trans, root, 0);
181749b25e05SJeff Mahoney 	if (ret) {
181849b25e05SJeff Mahoney 		mutex_unlock(&root->fs_info->tree_log_mutex);
181949b25e05SJeff Mahoney 		goto cleanup_transaction;
182049b25e05SJeff Mahoney 	}
18214313b399SChris Mason 
1822e02119d5SChris Mason 	/*
1823e02119d5SChris Mason 	 * the super is written, we can safely allow the tree-loggers
1824e02119d5SChris Mason 	 * to go about their business
1825e02119d5SChris Mason 	 */
1826e02119d5SChris Mason 	mutex_unlock(&root->fs_info->tree_log_mutex);
1827e02119d5SChris Mason 
182811833d66SYan Zheng 	btrfs_finish_extent_commit(trans, root);
18294313b399SChris Mason 
18302c90e5d6SChris Mason 	cur_trans->commit_done = 1;
1831b7ec40d7SChris Mason 
183215ee9bc7SJosef Bacik 	root->fs_info->last_trans_committed = cur_trans->transid;
1833817d52f8SJosef Bacik 
18342c90e5d6SChris Mason 	wake_up(&cur_trans->commit_wait);
18353de4586cSChris Mason 
1836a4abeea4SJosef Bacik 	spin_lock(&root->fs_info->trans_lock);
183713c5a93eSJosef Bacik 	list_del_init(&cur_trans->list);
1838a4abeea4SJosef Bacik 	spin_unlock(&root->fs_info->trans_lock);
1839a4abeea4SJosef Bacik 
184079154b1bSChris Mason 	put_transaction(cur_trans);
184178fae27eSChris Mason 	put_transaction(cur_trans);
184258176a96SJosef Bacik 
1843354aa0fbSMiao Xie 	if (trans->type < TRANS_JOIN_NOLOCK)
1844b2b5ef5cSJan Kara 		sb_end_intwrite(root->fs_info->sb);
1845b2b5ef5cSJan Kara 
18461abe9b8aSliubo 	trace_btrfs_transaction_commit(root);
18471abe9b8aSliubo 
1848a2de733cSArne Jansen 	btrfs_scrub_continue(root);
1849a2de733cSArne Jansen 
18509ed74f2dSJosef Bacik 	if (current->journal_info == trans)
18519ed74f2dSJosef Bacik 		current->journal_info = NULL;
18529ed74f2dSJosef Bacik 
18532c90e5d6SChris Mason 	kmem_cache_free(btrfs_trans_handle_cachep, trans);
185424bbcf04SYan, Zheng 
185524bbcf04SYan, Zheng 	if (current != root->fs_info->transaction_kthread)
185624bbcf04SYan, Zheng 		btrfs_run_delayed_iputs(root);
185724bbcf04SYan, Zheng 
185879154b1bSChris Mason 	return ret;
185949b25e05SJeff Mahoney 
186049b25e05SJeff Mahoney cleanup_transaction:
18610e721106SJosef Bacik 	btrfs_trans_release_metadata(trans, root);
18620e721106SJosef Bacik 	trans->block_rsv = NULL;
1863272d26d0SMiao Xie 	if (trans->qgroup_reserved) {
1864272d26d0SMiao Xie 		btrfs_qgroup_free(root, trans->qgroup_reserved);
1865272d26d0SMiao Xie 		trans->qgroup_reserved = 0;
1866272d26d0SMiao Xie 	}
186708748810SDavid Sterba 	btrfs_printk(root->fs_info, "Skipping commit of aborted transaction\n");
186849b25e05SJeff Mahoney 	if (current->journal_info == trans)
186949b25e05SJeff Mahoney 		current->journal_info = NULL;
18707b8b92afSJosef Bacik 	cleanup_transaction(trans, root, ret);
187149b25e05SJeff Mahoney 
187249b25e05SJeff Mahoney 	return ret;
187379154b1bSChris Mason }
187479154b1bSChris Mason 
1875d352ac68SChris Mason /*
18769d1a2a3aSDavid Sterba  * return < 0 if error
18779d1a2a3aSDavid Sterba  * 0 if there are no more dead_roots at the time of call
18789d1a2a3aSDavid Sterba  * 1 there are more to be processed, call me again
18799d1a2a3aSDavid Sterba  *
18809d1a2a3aSDavid Sterba  * The return value indicates there are certainly more snapshots to delete, but
18819d1a2a3aSDavid Sterba  * if there comes a new one during processing, it may return 0. We don't mind,
18829d1a2a3aSDavid Sterba  * because btrfs_commit_super will poke cleaner thread and it will process it a
18839d1a2a3aSDavid Sterba  * few seconds later.
1884d352ac68SChris Mason  */
18859d1a2a3aSDavid Sterba int btrfs_clean_one_deleted_snapshot(struct btrfs_root *root)
1886e9d0b13bSChris Mason {
18879d1a2a3aSDavid Sterba 	int ret;
18885d4f98a2SYan Zheng 	struct btrfs_fs_info *fs_info = root->fs_info;
1889e9d0b13bSChris Mason 
18909d1a2a3aSDavid Sterba 	if (fs_info->sb->s_flags & MS_RDONLY) {
18919d1a2a3aSDavid Sterba 		pr_debug("btrfs: cleaner called for RO fs!\n");
18929d1a2a3aSDavid Sterba 		return 0;
18939d1a2a3aSDavid Sterba 	}
18949d1a2a3aSDavid Sterba 
1895a4abeea4SJosef Bacik 	spin_lock(&fs_info->trans_lock);
18969d1a2a3aSDavid Sterba 	if (list_empty(&fs_info->dead_roots)) {
18979d1a2a3aSDavid Sterba 		spin_unlock(&fs_info->trans_lock);
18989d1a2a3aSDavid Sterba 		return 0;
18999d1a2a3aSDavid Sterba 	}
19009d1a2a3aSDavid Sterba 	root = list_first_entry(&fs_info->dead_roots,
19019d1a2a3aSDavid Sterba 			struct btrfs_root, root_list);
19029d1a2a3aSDavid Sterba 	list_del(&root->root_list);
1903a4abeea4SJosef Bacik 	spin_unlock(&fs_info->trans_lock);
19045d4f98a2SYan Zheng 
19059d1a2a3aSDavid Sterba 	pr_debug("btrfs: cleaner removing %llu\n",
19069d1a2a3aSDavid Sterba 			(unsigned long long)root->objectid);
190776dda93cSYan, Zheng 
190816cdcec7SMiao Xie 	btrfs_kill_all_delayed_nodes(root);
190916cdcec7SMiao Xie 
191076dda93cSYan, Zheng 	if (btrfs_header_backref_rev(root->node) <
191176dda93cSYan, Zheng 			BTRFS_MIXED_BACKREF_REV)
19122c536799SJeff Mahoney 		ret = btrfs_drop_snapshot(root, NULL, 0, 0);
191376dda93cSYan, Zheng 	else
19142c536799SJeff Mahoney 		ret = btrfs_drop_snapshot(root, NULL, 1, 0);
19159d1a2a3aSDavid Sterba 	/*
19169d1a2a3aSDavid Sterba 	 * If we encounter a transaction abort during snapshot cleaning, we
19179d1a2a3aSDavid Sterba 	 * don't want to crash here
19189d1a2a3aSDavid Sterba 	 */
19199d1a2a3aSDavid Sterba 	BUG_ON(ret < 0 && ret != -EAGAIN && ret != -EROFS);
19209d1a2a3aSDavid Sterba 	return 1;
1921e9d0b13bSChris Mason }
1922