xref: /openbmc/linux/fs/btrfs/transaction.c (revision 4a9d8bde)
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 
374a9d8bdeSMiao Xie static unsigned int btrfs_blocked_trans_types[TRANS_STATE_MAX] = {
384a9d8bdeSMiao Xie 	[TRANS_STATE_RUNNING]		= 0U,
394a9d8bdeSMiao Xie 	[TRANS_STATE_BLOCKED]		= (__TRANS_USERSPACE |
404a9d8bdeSMiao Xie 					   __TRANS_START),
414a9d8bdeSMiao Xie 	[TRANS_STATE_COMMIT_START]	= (__TRANS_USERSPACE |
424a9d8bdeSMiao Xie 					   __TRANS_START |
434a9d8bdeSMiao Xie 					   __TRANS_ATTACH),
444a9d8bdeSMiao Xie 	[TRANS_STATE_COMMIT_DOING]	= (__TRANS_USERSPACE |
454a9d8bdeSMiao Xie 					   __TRANS_START |
464a9d8bdeSMiao Xie 					   __TRANS_ATTACH |
474a9d8bdeSMiao Xie 					   __TRANS_JOIN),
484a9d8bdeSMiao Xie 	[TRANS_STATE_UNBLOCKED]		= (__TRANS_USERSPACE |
494a9d8bdeSMiao Xie 					   __TRANS_START |
504a9d8bdeSMiao Xie 					   __TRANS_ATTACH |
514a9d8bdeSMiao Xie 					   __TRANS_JOIN |
524a9d8bdeSMiao Xie 					   __TRANS_JOIN_NOLOCK),
534a9d8bdeSMiao Xie 	[TRANS_STATE_COMPLETED]		= (__TRANS_USERSPACE |
544a9d8bdeSMiao Xie 					   __TRANS_START |
554a9d8bdeSMiao Xie 					   __TRANS_ATTACH |
564a9d8bdeSMiao Xie 					   __TRANS_JOIN |
574a9d8bdeSMiao Xie 					   __TRANS_JOIN_NOLOCK),
584a9d8bdeSMiao Xie };
594a9d8bdeSMiao Xie 
6048a3b636SEric Sandeen static void put_transaction(struct btrfs_transaction *transaction)
6179154b1bSChris Mason {
6213c5a93eSJosef Bacik 	WARN_ON(atomic_read(&transaction->use_count) == 0);
6313c5a93eSJosef Bacik 	if (atomic_dec_and_test(&transaction->use_count)) {
64a4abeea4SJosef Bacik 		BUG_ON(!list_empty(&transaction->list));
6500f04b88SArne Jansen 		WARN_ON(transaction->delayed_refs.root.rb_node);
662c90e5d6SChris Mason 		kmem_cache_free(btrfs_transaction_cachep, transaction);
6779154b1bSChris Mason 	}
6878fae27eSChris Mason }
6979154b1bSChris Mason 
70817d52f8SJosef Bacik static noinline void switch_commit_root(struct btrfs_root *root)
71817d52f8SJosef Bacik {
72817d52f8SJosef Bacik 	free_extent_buffer(root->commit_root);
73817d52f8SJosef Bacik 	root->commit_root = btrfs_root_node(root);
74817d52f8SJosef Bacik }
75817d52f8SJosef Bacik 
760860adfdSMiao Xie static inline void extwriter_counter_inc(struct btrfs_transaction *trans,
770860adfdSMiao Xie 					 unsigned int type)
780860adfdSMiao Xie {
790860adfdSMiao Xie 	if (type & TRANS_EXTWRITERS)
800860adfdSMiao Xie 		atomic_inc(&trans->num_extwriters);
810860adfdSMiao Xie }
820860adfdSMiao Xie 
830860adfdSMiao Xie static inline void extwriter_counter_dec(struct btrfs_transaction *trans,
840860adfdSMiao Xie 					 unsigned int type)
850860adfdSMiao Xie {
860860adfdSMiao Xie 	if (type & TRANS_EXTWRITERS)
870860adfdSMiao Xie 		atomic_dec(&trans->num_extwriters);
880860adfdSMiao Xie }
890860adfdSMiao Xie 
900860adfdSMiao Xie static inline void extwriter_counter_init(struct btrfs_transaction *trans,
910860adfdSMiao Xie 					  unsigned int type)
920860adfdSMiao Xie {
930860adfdSMiao Xie 	atomic_set(&trans->num_extwriters, ((type & TRANS_EXTWRITERS) ? 1 : 0));
940860adfdSMiao Xie }
950860adfdSMiao Xie 
960860adfdSMiao Xie static inline int extwriter_counter_read(struct btrfs_transaction *trans)
970860adfdSMiao Xie {
980860adfdSMiao Xie 	return atomic_read(&trans->num_extwriters);
99178260b2SMiao Xie }
100178260b2SMiao Xie 
101d352ac68SChris Mason /*
102d352ac68SChris Mason  * either allocate a new transaction or hop into the existing one
103d352ac68SChris Mason  */
1040860adfdSMiao Xie static noinline int join_transaction(struct btrfs_root *root, unsigned int type)
10579154b1bSChris Mason {
10679154b1bSChris Mason 	struct btrfs_transaction *cur_trans;
10719ae4e81SJan Schmidt 	struct btrfs_fs_info *fs_info = root->fs_info;
108a4abeea4SJosef Bacik 
10919ae4e81SJan Schmidt 	spin_lock(&fs_info->trans_lock);
110d43317dcSChris Mason loop:
11149b25e05SJeff Mahoney 	/* The file system has been taken offline. No new transactions. */
11287533c47SMiao Xie 	if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) {
11319ae4e81SJan Schmidt 		spin_unlock(&fs_info->trans_lock);
11449b25e05SJeff Mahoney 		return -EROFS;
11549b25e05SJeff Mahoney 	}
11649b25e05SJeff Mahoney 
11719ae4e81SJan Schmidt 	cur_trans = fs_info->running_transaction;
118a4abeea4SJosef Bacik 	if (cur_trans) {
119871383beSDavid Sterba 		if (cur_trans->aborted) {
12019ae4e81SJan Schmidt 			spin_unlock(&fs_info->trans_lock);
12149b25e05SJeff Mahoney 			return cur_trans->aborted;
122871383beSDavid Sterba 		}
1234a9d8bdeSMiao Xie 		if (btrfs_blocked_trans_types[cur_trans->state] & type) {
124178260b2SMiao Xie 			spin_unlock(&fs_info->trans_lock);
125178260b2SMiao Xie 			return -EBUSY;
126178260b2SMiao Xie 		}
127a4abeea4SJosef Bacik 		atomic_inc(&cur_trans->use_count);
128a4abeea4SJosef Bacik 		atomic_inc(&cur_trans->num_writers);
1290860adfdSMiao Xie 		extwriter_counter_inc(cur_trans, type);
13019ae4e81SJan Schmidt 		spin_unlock(&fs_info->trans_lock);
131a4abeea4SJosef Bacik 		return 0;
132a4abeea4SJosef Bacik 	}
13319ae4e81SJan Schmidt 	spin_unlock(&fs_info->trans_lock);
134a4abeea4SJosef Bacik 
135354aa0fbSMiao Xie 	/*
136354aa0fbSMiao Xie 	 * If we are ATTACH, we just want to catch the current transaction,
137354aa0fbSMiao Xie 	 * and commit it. If there is no transaction, just return ENOENT.
138354aa0fbSMiao Xie 	 */
139354aa0fbSMiao Xie 	if (type == TRANS_ATTACH)
140354aa0fbSMiao Xie 		return -ENOENT;
141354aa0fbSMiao Xie 
1424a9d8bdeSMiao Xie 	/*
1434a9d8bdeSMiao Xie 	 * JOIN_NOLOCK only happens during the transaction commit, so
1444a9d8bdeSMiao Xie 	 * it is impossible that ->running_transaction is NULL
1454a9d8bdeSMiao Xie 	 */
1464a9d8bdeSMiao Xie 	BUG_ON(type == TRANS_JOIN_NOLOCK);
1474a9d8bdeSMiao Xie 
148a4abeea4SJosef Bacik 	cur_trans = kmem_cache_alloc(btrfs_transaction_cachep, GFP_NOFS);
149db5b493aSTsutomu Itoh 	if (!cur_trans)
150db5b493aSTsutomu Itoh 		return -ENOMEM;
151d43317dcSChris Mason 
15219ae4e81SJan Schmidt 	spin_lock(&fs_info->trans_lock);
15319ae4e81SJan Schmidt 	if (fs_info->running_transaction) {
154d43317dcSChris Mason 		/*
155d43317dcSChris Mason 		 * someone started a transaction after we unlocked.  Make sure
1564a9d8bdeSMiao Xie 		 * to redo the checks above
157d43317dcSChris Mason 		 */
158a4abeea4SJosef Bacik 		kmem_cache_free(btrfs_transaction_cachep, cur_trans);
159d43317dcSChris Mason 		goto loop;
16087533c47SMiao Xie 	} else if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) {
161e4b50e14SDan Carpenter 		spin_unlock(&fs_info->trans_lock);
1627b8b92afSJosef Bacik 		kmem_cache_free(btrfs_transaction_cachep, cur_trans);
1637b8b92afSJosef Bacik 		return -EROFS;
164a4abeea4SJosef Bacik 	}
165d43317dcSChris Mason 
16613c5a93eSJosef Bacik 	atomic_set(&cur_trans->num_writers, 1);
1670860adfdSMiao Xie 	extwriter_counter_init(cur_trans, type);
16879154b1bSChris Mason 	init_waitqueue_head(&cur_trans->writer_wait);
16979154b1bSChris Mason 	init_waitqueue_head(&cur_trans->commit_wait);
1704a9d8bdeSMiao Xie 	cur_trans->state = TRANS_STATE_RUNNING;
171a4abeea4SJosef Bacik 	/*
172a4abeea4SJosef Bacik 	 * One for this trans handle, one so it will live on until we
173a4abeea4SJosef Bacik 	 * commit the transaction.
174a4abeea4SJosef Bacik 	 */
175a4abeea4SJosef Bacik 	atomic_set(&cur_trans->use_count, 2);
17608607c1bSChris Mason 	cur_trans->start_time = get_seconds();
17756bec294SChris Mason 
1786bef4d31SEric Paris 	cur_trans->delayed_refs.root = RB_ROOT;
17956bec294SChris Mason 	cur_trans->delayed_refs.num_entries = 0;
180c3e69d58SChris Mason 	cur_trans->delayed_refs.num_heads_ready = 0;
181c3e69d58SChris Mason 	cur_trans->delayed_refs.num_heads = 0;
18256bec294SChris Mason 	cur_trans->delayed_refs.flushing = 0;
183c3e69d58SChris Mason 	cur_trans->delayed_refs.run_delayed_start = 0;
18420b297d6SJan Schmidt 
18520b297d6SJan Schmidt 	/*
18620b297d6SJan Schmidt 	 * although the tree mod log is per file system and not per transaction,
18720b297d6SJan Schmidt 	 * the log must never go across transaction boundaries.
18820b297d6SJan Schmidt 	 */
18920b297d6SJan Schmidt 	smp_mb();
19031b1a2bdSJulia Lawall 	if (!list_empty(&fs_info->tree_mod_seq_list))
19131b1a2bdSJulia Lawall 		WARN(1, KERN_ERR "btrfs: tree_mod_seq_list not empty when "
19220b297d6SJan Schmidt 			"creating a fresh transaction\n");
19331b1a2bdSJulia Lawall 	if (!RB_EMPTY_ROOT(&fs_info->tree_mod_log))
19431b1a2bdSJulia Lawall 		WARN(1, KERN_ERR "btrfs: tree_mod_log rb tree not empty when "
19520b297d6SJan Schmidt 			"creating a fresh transaction\n");
196fc36ed7eSJan Schmidt 	atomic64_set(&fs_info->tree_mod_seq, 0);
19720b297d6SJan Schmidt 
19856bec294SChris Mason 	spin_lock_init(&cur_trans->delayed_refs.lock);
199bb721703SChris Mason 	atomic_set(&cur_trans->delayed_refs.procs_running_refs, 0);
200bb721703SChris Mason 	atomic_set(&cur_trans->delayed_refs.ref_seq, 0);
201bb721703SChris Mason 	init_waitqueue_head(&cur_trans->delayed_refs.wait);
20256bec294SChris Mason 
2033063d29fSChris Mason 	INIT_LIST_HEAD(&cur_trans->pending_snapshots);
204569e0f35SJosef Bacik 	INIT_LIST_HEAD(&cur_trans->ordered_operations);
20519ae4e81SJan Schmidt 	list_add_tail(&cur_trans->list, &fs_info->trans_list);
206d1310b2eSChris Mason 	extent_io_tree_init(&cur_trans->dirty_pages,
20719ae4e81SJan Schmidt 			     fs_info->btree_inode->i_mapping);
20819ae4e81SJan Schmidt 	fs_info->generation++;
20919ae4e81SJan Schmidt 	cur_trans->transid = fs_info->generation;
21019ae4e81SJan Schmidt 	fs_info->running_transaction = cur_trans;
21149b25e05SJeff Mahoney 	cur_trans->aborted = 0;
21219ae4e81SJan Schmidt 	spin_unlock(&fs_info->trans_lock);
21315ee9bc7SJosef Bacik 
21479154b1bSChris Mason 	return 0;
21579154b1bSChris Mason }
21679154b1bSChris Mason 
217d352ac68SChris Mason /*
218d397712bSChris Mason  * this does all the record keeping required to make sure that a reference
219d397712bSChris Mason  * counted root is properly recorded in a given transaction.  This is required
220d397712bSChris Mason  * to make sure the old root from before we joined the transaction is deleted
221d397712bSChris Mason  * when the transaction commits
222d352ac68SChris Mason  */
2237585717fSChris Mason static int record_root_in_trans(struct btrfs_trans_handle *trans,
2245d4f98a2SYan Zheng 			       struct btrfs_root *root)
2256702ed49SChris Mason {
2265d4f98a2SYan Zheng 	if (root->ref_cows && root->last_trans < trans->transid) {
2276702ed49SChris Mason 		WARN_ON(root == root->fs_info->extent_root);
2285d4f98a2SYan Zheng 		WARN_ON(root->commit_root != root->node);
2295d4f98a2SYan Zheng 
2307585717fSChris Mason 		/*
2317585717fSChris Mason 		 * see below for in_trans_setup usage rules
2327585717fSChris Mason 		 * we have the reloc mutex held now, so there
2337585717fSChris Mason 		 * is only one writer in this function
2347585717fSChris Mason 		 */
2357585717fSChris Mason 		root->in_trans_setup = 1;
2367585717fSChris Mason 
2377585717fSChris Mason 		/* make sure readers find in_trans_setup before
2387585717fSChris Mason 		 * they find our root->last_trans update
2397585717fSChris Mason 		 */
2407585717fSChris Mason 		smp_wmb();
2417585717fSChris Mason 
242a4abeea4SJosef Bacik 		spin_lock(&root->fs_info->fs_roots_radix_lock);
243a4abeea4SJosef Bacik 		if (root->last_trans == trans->transid) {
244a4abeea4SJosef Bacik 			spin_unlock(&root->fs_info->fs_roots_radix_lock);
245a4abeea4SJosef Bacik 			return 0;
246a4abeea4SJosef Bacik 		}
2476702ed49SChris Mason 		radix_tree_tag_set(&root->fs_info->fs_roots_radix,
2486702ed49SChris Mason 			   (unsigned long)root->root_key.objectid,
2496702ed49SChris Mason 			   BTRFS_ROOT_TRANS_TAG);
250a4abeea4SJosef Bacik 		spin_unlock(&root->fs_info->fs_roots_radix_lock);
2517585717fSChris Mason 		root->last_trans = trans->transid;
2527585717fSChris Mason 
2537585717fSChris Mason 		/* this is pretty tricky.  We don't want to
2547585717fSChris Mason 		 * take the relocation lock in btrfs_record_root_in_trans
2557585717fSChris Mason 		 * unless we're really doing the first setup for this root in
2567585717fSChris Mason 		 * this transaction.
2577585717fSChris Mason 		 *
2587585717fSChris Mason 		 * Normally we'd use root->last_trans as a flag to decide
2597585717fSChris Mason 		 * if we want to take the expensive mutex.
2607585717fSChris Mason 		 *
2617585717fSChris Mason 		 * But, we have to set root->last_trans before we
2627585717fSChris Mason 		 * init the relocation root, otherwise, we trip over warnings
2637585717fSChris Mason 		 * in ctree.c.  The solution used here is to flag ourselves
2647585717fSChris Mason 		 * with root->in_trans_setup.  When this is 1, we're still
2657585717fSChris Mason 		 * fixing up the reloc trees and everyone must wait.
2667585717fSChris Mason 		 *
2677585717fSChris Mason 		 * When this is zero, they can trust root->last_trans and fly
2687585717fSChris Mason 		 * through btrfs_record_root_in_trans without having to take the
2697585717fSChris Mason 		 * lock.  smp_wmb() makes sure that all the writes above are
2707585717fSChris Mason 		 * done before we pop in the zero below
2717585717fSChris Mason 		 */
2725d4f98a2SYan Zheng 		btrfs_init_reloc_root(trans, root);
2737585717fSChris Mason 		smp_wmb();
2747585717fSChris Mason 		root->in_trans_setup = 0;
2756702ed49SChris Mason 	}
2765d4f98a2SYan Zheng 	return 0;
2776702ed49SChris Mason }
2785d4f98a2SYan Zheng 
2797585717fSChris Mason 
2807585717fSChris Mason int btrfs_record_root_in_trans(struct btrfs_trans_handle *trans,
2817585717fSChris Mason 			       struct btrfs_root *root)
2827585717fSChris Mason {
2837585717fSChris Mason 	if (!root->ref_cows)
2847585717fSChris Mason 		return 0;
2857585717fSChris Mason 
2867585717fSChris Mason 	/*
2877585717fSChris Mason 	 * see record_root_in_trans for comments about in_trans_setup usage
2887585717fSChris Mason 	 * and barriers
2897585717fSChris Mason 	 */
2907585717fSChris Mason 	smp_rmb();
2917585717fSChris Mason 	if (root->last_trans == trans->transid &&
2927585717fSChris Mason 	    !root->in_trans_setup)
2937585717fSChris Mason 		return 0;
2947585717fSChris Mason 
2957585717fSChris Mason 	mutex_lock(&root->fs_info->reloc_mutex);
2967585717fSChris Mason 	record_root_in_trans(trans, root);
2977585717fSChris Mason 	mutex_unlock(&root->fs_info->reloc_mutex);
2987585717fSChris Mason 
2997585717fSChris Mason 	return 0;
3007585717fSChris Mason }
3017585717fSChris Mason 
3024a9d8bdeSMiao Xie static inline int is_transaction_blocked(struct btrfs_transaction *trans)
3034a9d8bdeSMiao Xie {
3044a9d8bdeSMiao Xie 	return (trans->state >= TRANS_STATE_BLOCKED &&
3054a9d8bdeSMiao Xie 		trans->state < TRANS_STATE_UNBLOCKED);
3064a9d8bdeSMiao Xie }
3074a9d8bdeSMiao Xie 
308d352ac68SChris Mason /* wait for commit against the current transaction to become unblocked
309d352ac68SChris Mason  * when this is done, it is safe to start a new transaction, but the current
310d352ac68SChris Mason  * transaction might not be fully on disk.
311d352ac68SChris Mason  */
31237d1aeeeSChris Mason static void wait_current_trans(struct btrfs_root *root)
31379154b1bSChris Mason {
314f9295749SChris Mason 	struct btrfs_transaction *cur_trans;
31579154b1bSChris Mason 
316a4abeea4SJosef Bacik 	spin_lock(&root->fs_info->trans_lock);
317f9295749SChris Mason 	cur_trans = root->fs_info->running_transaction;
3184a9d8bdeSMiao Xie 	if (cur_trans && is_transaction_blocked(cur_trans)) {
31913c5a93eSJosef Bacik 		atomic_inc(&cur_trans->use_count);
320a4abeea4SJosef Bacik 		spin_unlock(&root->fs_info->trans_lock);
32172d63ed6SLi Zefan 
32272d63ed6SLi Zefan 		wait_event(root->fs_info->transaction_wait,
3234a9d8bdeSMiao Xie 			   cur_trans->state >= TRANS_STATE_UNBLOCKED);
324f9295749SChris Mason 		put_transaction(cur_trans);
325a4abeea4SJosef Bacik 	} else {
326a4abeea4SJosef Bacik 		spin_unlock(&root->fs_info->trans_lock);
327f9295749SChris Mason 	}
32837d1aeeeSChris Mason }
32937d1aeeeSChris Mason 
330a22285a6SYan, Zheng static int may_wait_transaction(struct btrfs_root *root, int type)
33137d1aeeeSChris Mason {
332a4abeea4SJosef Bacik 	if (root->fs_info->log_root_recovering)
333a4abeea4SJosef Bacik 		return 0;
334a4abeea4SJosef Bacik 
335a4abeea4SJosef Bacik 	if (type == TRANS_USERSPACE)
336a22285a6SYan, Zheng 		return 1;
337a4abeea4SJosef Bacik 
338a4abeea4SJosef Bacik 	if (type == TRANS_START &&
339a4abeea4SJosef Bacik 	    !atomic_read(&root->fs_info->open_ioctl_trans))
340a4abeea4SJosef Bacik 		return 1;
341a4abeea4SJosef Bacik 
342a22285a6SYan, Zheng 	return 0;
343a22285a6SYan, Zheng }
344a22285a6SYan, Zheng 
34508e007d2SMiao Xie static struct btrfs_trans_handle *
3460860adfdSMiao Xie start_transaction(struct btrfs_root *root, u64 num_items, unsigned int type,
34708e007d2SMiao Xie 		  enum btrfs_reserve_flush_enum flush)
348a22285a6SYan, Zheng {
349a22285a6SYan, Zheng 	struct btrfs_trans_handle *h;
350a22285a6SYan, Zheng 	struct btrfs_transaction *cur_trans;
351b5009945SJosef Bacik 	u64 num_bytes = 0;
352a22285a6SYan, Zheng 	int ret;
353c5567237SArne Jansen 	u64 qgroup_reserved = 0;
354acce952bSliubo 
35587533c47SMiao Xie 	if (test_bit(BTRFS_FS_STATE_ERROR, &root->fs_info->fs_state))
356acce952bSliubo 		return ERR_PTR(-EROFS);
3572a1eb461SJosef Bacik 
3582a1eb461SJosef Bacik 	if (current->journal_info) {
3590860adfdSMiao Xie 		WARN_ON(type & TRANS_EXTWRITERS);
3602a1eb461SJosef Bacik 		h = current->journal_info;
3612a1eb461SJosef Bacik 		h->use_count++;
362b7d5b0a8SMiao Xie 		WARN_ON(h->use_count > 2);
3632a1eb461SJosef Bacik 		h->orig_rsv = h->block_rsv;
3642a1eb461SJosef Bacik 		h->block_rsv = NULL;
3652a1eb461SJosef Bacik 		goto got_it;
3662a1eb461SJosef Bacik 	}
367b5009945SJosef Bacik 
368b5009945SJosef Bacik 	/*
369b5009945SJosef Bacik 	 * Do the reservation before we join the transaction so we can do all
370b5009945SJosef Bacik 	 * the appropriate flushing if need be.
371b5009945SJosef Bacik 	 */
372b5009945SJosef Bacik 	if (num_items > 0 && root != root->fs_info->chunk_root) {
373c5567237SArne Jansen 		if (root->fs_info->quota_enabled &&
374c5567237SArne Jansen 		    is_fstree(root->root_key.objectid)) {
375c5567237SArne Jansen 			qgroup_reserved = num_items * root->leafsize;
376c5567237SArne Jansen 			ret = btrfs_qgroup_reserve(root, qgroup_reserved);
377c5567237SArne Jansen 			if (ret)
378c5567237SArne Jansen 				return ERR_PTR(ret);
379c5567237SArne Jansen 		}
380c5567237SArne Jansen 
381b5009945SJosef Bacik 		num_bytes = btrfs_calc_trans_metadata_size(root, num_items);
3824a92b1b8SJosef Bacik 		ret = btrfs_block_rsv_add(root,
383b5009945SJosef Bacik 					  &root->fs_info->trans_block_rsv,
38408e007d2SMiao Xie 					  num_bytes, flush);
385b5009945SJosef Bacik 		if (ret)
386843fcf35SMiao Xie 			goto reserve_fail;
387b5009945SJosef Bacik 	}
388a22285a6SYan, Zheng again:
389a22285a6SYan, Zheng 	h = kmem_cache_alloc(btrfs_trans_handle_cachep, GFP_NOFS);
390843fcf35SMiao Xie 	if (!h) {
391843fcf35SMiao Xie 		ret = -ENOMEM;
392843fcf35SMiao Xie 		goto alloc_fail;
393843fcf35SMiao Xie 	}
394a22285a6SYan, Zheng 
39598114659SJosef Bacik 	/*
39698114659SJosef Bacik 	 * If we are JOIN_NOLOCK we're already committing a transaction and
39798114659SJosef Bacik 	 * waiting on this guy, so we don't need to do the sb_start_intwrite
39898114659SJosef Bacik 	 * because we're already holding a ref.  We need this because we could
39998114659SJosef Bacik 	 * have raced in and did an fsync() on a file which can kick a commit
40098114659SJosef Bacik 	 * and then we deadlock with somebody doing a freeze.
401354aa0fbSMiao Xie 	 *
402354aa0fbSMiao Xie 	 * If we are ATTACH, it means we just want to catch the current
403354aa0fbSMiao Xie 	 * transaction and commit it, so we needn't do sb_start_intwrite().
40498114659SJosef Bacik 	 */
4050860adfdSMiao Xie 	if (type & __TRANS_FREEZABLE)
406b2b5ef5cSJan Kara 		sb_start_intwrite(root->fs_info->sb);
407b2b5ef5cSJan Kara 
408a22285a6SYan, Zheng 	if (may_wait_transaction(root, type))
40937d1aeeeSChris Mason 		wait_current_trans(root);
410a22285a6SYan, Zheng 
411a4abeea4SJosef Bacik 	do {
412354aa0fbSMiao Xie 		ret = join_transaction(root, type);
413178260b2SMiao Xie 		if (ret == -EBUSY) {
414a4abeea4SJosef Bacik 			wait_current_trans(root);
415178260b2SMiao Xie 			if (unlikely(type == TRANS_ATTACH))
416178260b2SMiao Xie 				ret = -ENOENT;
417178260b2SMiao Xie 		}
418a4abeea4SJosef Bacik 	} while (ret == -EBUSY);
419a4abeea4SJosef Bacik 
420db5b493aSTsutomu Itoh 	if (ret < 0) {
421354aa0fbSMiao Xie 		/* We must get the transaction if we are JOIN_NOLOCK. */
422354aa0fbSMiao Xie 		BUG_ON(type == TRANS_JOIN_NOLOCK);
423843fcf35SMiao Xie 		goto join_fail;
424db5b493aSTsutomu Itoh 	}
4250f7d52f4SChris Mason 
426a22285a6SYan, Zheng 	cur_trans = root->fs_info->running_transaction;
427a22285a6SYan, Zheng 
428a22285a6SYan, Zheng 	h->transid = cur_trans->transid;
429a22285a6SYan, Zheng 	h->transaction = cur_trans;
43079154b1bSChris Mason 	h->blocks_used = 0;
431a22285a6SYan, Zheng 	h->bytes_reserved = 0;
432d13603efSArne Jansen 	h->root = root;
43356bec294SChris Mason 	h->delayed_ref_updates = 0;
4342a1eb461SJosef Bacik 	h->use_count = 1;
4350e721106SJosef Bacik 	h->adding_csums = 0;
436f0486c68SYan, Zheng 	h->block_rsv = NULL;
4372a1eb461SJosef Bacik 	h->orig_rsv = NULL;
43849b25e05SJeff Mahoney 	h->aborted = 0;
4394b824906SMiao Xie 	h->qgroup_reserved = 0;
440bed92eaeSArne Jansen 	h->delayed_ref_elem.seq = 0;
441a698d075SMiao Xie 	h->type = type;
442c6b305a8SJosef Bacik 	h->allocating_chunk = false;
443bed92eaeSArne Jansen 	INIT_LIST_HEAD(&h->qgroup_ref_list);
444ea658badSJosef Bacik 	INIT_LIST_HEAD(&h->new_bgs);
445b7ec40d7SChris Mason 
446a22285a6SYan, Zheng 	smp_mb();
4474a9d8bdeSMiao Xie 	if (cur_trans->state >= TRANS_STATE_BLOCKED &&
4484a9d8bdeSMiao Xie 	    may_wait_transaction(root, type)) {
449a22285a6SYan, Zheng 		btrfs_commit_transaction(h, root);
450a22285a6SYan, Zheng 		goto again;
451a22285a6SYan, Zheng 	}
4529ed74f2dSJosef Bacik 
453b5009945SJosef Bacik 	if (num_bytes) {
4548c2a3ca2SJosef Bacik 		trace_btrfs_space_reservation(root->fs_info, "transaction",
4552bcc0328SLiu Bo 					      h->transid, num_bytes, 1);
456b5009945SJosef Bacik 		h->block_rsv = &root->fs_info->trans_block_rsv;
457b5009945SJosef Bacik 		h->bytes_reserved = num_bytes;
458a22285a6SYan, Zheng 	}
4594b824906SMiao Xie 	h->qgroup_reserved = qgroup_reserved;
460a22285a6SYan, Zheng 
4612a1eb461SJosef Bacik got_it:
462a4abeea4SJosef Bacik 	btrfs_record_root_in_trans(h, root);
463a22285a6SYan, Zheng 
464a22285a6SYan, Zheng 	if (!current->journal_info && type != TRANS_USERSPACE)
465a22285a6SYan, Zheng 		current->journal_info = h;
46679154b1bSChris Mason 	return h;
467843fcf35SMiao Xie 
468843fcf35SMiao Xie join_fail:
4690860adfdSMiao Xie 	if (type & __TRANS_FREEZABLE)
470843fcf35SMiao Xie 		sb_end_intwrite(root->fs_info->sb);
471843fcf35SMiao Xie 	kmem_cache_free(btrfs_trans_handle_cachep, h);
472843fcf35SMiao Xie alloc_fail:
473843fcf35SMiao Xie 	if (num_bytes)
474843fcf35SMiao Xie 		btrfs_block_rsv_release(root, &root->fs_info->trans_block_rsv,
475843fcf35SMiao Xie 					num_bytes);
476843fcf35SMiao Xie reserve_fail:
477843fcf35SMiao Xie 	if (qgroup_reserved)
478843fcf35SMiao Xie 		btrfs_qgroup_free(root, qgroup_reserved);
479843fcf35SMiao Xie 	return ERR_PTR(ret);
48079154b1bSChris Mason }
48179154b1bSChris Mason 
482f9295749SChris Mason struct btrfs_trans_handle *btrfs_start_transaction(struct btrfs_root *root,
483a22285a6SYan, Zheng 						   int num_items)
484f9295749SChris Mason {
48508e007d2SMiao Xie 	return start_transaction(root, num_items, TRANS_START,
48608e007d2SMiao Xie 				 BTRFS_RESERVE_FLUSH_ALL);
487f9295749SChris Mason }
4888407aa46SMiao Xie 
48908e007d2SMiao Xie struct btrfs_trans_handle *btrfs_start_transaction_lflush(
4908407aa46SMiao Xie 					struct btrfs_root *root, int num_items)
4918407aa46SMiao Xie {
49208e007d2SMiao Xie 	return start_transaction(root, num_items, TRANS_START,
49308e007d2SMiao Xie 				 BTRFS_RESERVE_FLUSH_LIMIT);
4948407aa46SMiao Xie }
4958407aa46SMiao Xie 
4967a7eaa40SJosef Bacik struct btrfs_trans_handle *btrfs_join_transaction(struct btrfs_root *root)
497f9295749SChris Mason {
4988407aa46SMiao Xie 	return start_transaction(root, 0, TRANS_JOIN, 0);
499f9295749SChris Mason }
500f9295749SChris Mason 
5017a7eaa40SJosef Bacik struct btrfs_trans_handle *btrfs_join_transaction_nolock(struct btrfs_root *root)
5020af3d00bSJosef Bacik {
5038407aa46SMiao Xie 	return start_transaction(root, 0, TRANS_JOIN_NOLOCK, 0);
5040af3d00bSJosef Bacik }
5050af3d00bSJosef Bacik 
5067a7eaa40SJosef Bacik struct btrfs_trans_handle *btrfs_start_ioctl_transaction(struct btrfs_root *root)
5079ca9ee09SSage Weil {
5088407aa46SMiao Xie 	return start_transaction(root, 0, TRANS_USERSPACE, 0);
5099ca9ee09SSage Weil }
5109ca9ee09SSage Weil 
511d4edf39bSMiao Xie /*
512d4edf39bSMiao Xie  * btrfs_attach_transaction() - catch the running transaction
513d4edf39bSMiao Xie  *
514d4edf39bSMiao Xie  * It is used when we want to commit the current the transaction, but
515d4edf39bSMiao Xie  * don't want to start a new one.
516d4edf39bSMiao Xie  *
517d4edf39bSMiao Xie  * Note: If this function return -ENOENT, it just means there is no
518d4edf39bSMiao Xie  * running transaction. But it is possible that the inactive transaction
519d4edf39bSMiao Xie  * is still in the memory, not fully on disk. If you hope there is no
520d4edf39bSMiao Xie  * inactive transaction in the fs when -ENOENT is returned, you should
521d4edf39bSMiao Xie  * invoke
522d4edf39bSMiao Xie  *     btrfs_attach_transaction_barrier()
523d4edf39bSMiao Xie  */
524354aa0fbSMiao Xie struct btrfs_trans_handle *btrfs_attach_transaction(struct btrfs_root *root)
52560376ce4SJosef Bacik {
526354aa0fbSMiao Xie 	return start_transaction(root, 0, TRANS_ATTACH, 0);
52760376ce4SJosef Bacik }
52860376ce4SJosef Bacik 
529d4edf39bSMiao Xie /*
530d4edf39bSMiao Xie  * btrfs_attach_transaction() - catch the running transaction
531d4edf39bSMiao Xie  *
532d4edf39bSMiao Xie  * It is similar to the above function, the differentia is this one
533d4edf39bSMiao Xie  * will wait for all the inactive transactions until they fully
534d4edf39bSMiao Xie  * complete.
535d4edf39bSMiao Xie  */
536d4edf39bSMiao Xie struct btrfs_trans_handle *
537d4edf39bSMiao Xie btrfs_attach_transaction_barrier(struct btrfs_root *root)
538d4edf39bSMiao Xie {
539d4edf39bSMiao Xie 	struct btrfs_trans_handle *trans;
540d4edf39bSMiao Xie 
541d4edf39bSMiao Xie 	trans = start_transaction(root, 0, TRANS_ATTACH, 0);
542d4edf39bSMiao Xie 	if (IS_ERR(trans) && PTR_ERR(trans) == -ENOENT)
543d4edf39bSMiao Xie 		btrfs_wait_for_commit(root, 0);
544d4edf39bSMiao Xie 
545d4edf39bSMiao Xie 	return trans;
546d4edf39bSMiao Xie }
547d4edf39bSMiao Xie 
548d352ac68SChris Mason /* wait for a transaction commit to be fully complete */
549b9c8300cSLi Zefan static noinline void wait_for_commit(struct btrfs_root *root,
55089ce8a63SChris Mason 				    struct btrfs_transaction *commit)
55189ce8a63SChris Mason {
5524a9d8bdeSMiao Xie 	wait_event(commit->commit_wait, commit->state == TRANS_STATE_COMPLETED);
55389ce8a63SChris Mason }
55489ce8a63SChris Mason 
55546204592SSage Weil int btrfs_wait_for_commit(struct btrfs_root *root, u64 transid)
55646204592SSage Weil {
55746204592SSage Weil 	struct btrfs_transaction *cur_trans = NULL, *t;
5588cd2807fSMiao Xie 	int ret = 0;
55946204592SSage Weil 
56046204592SSage Weil 	if (transid) {
56146204592SSage Weil 		if (transid <= root->fs_info->last_trans_committed)
562a4abeea4SJosef Bacik 			goto out;
56346204592SSage Weil 
5648cd2807fSMiao Xie 		ret = -EINVAL;
56546204592SSage Weil 		/* find specified transaction */
566a4abeea4SJosef Bacik 		spin_lock(&root->fs_info->trans_lock);
56746204592SSage Weil 		list_for_each_entry(t, &root->fs_info->trans_list, list) {
56846204592SSage Weil 			if (t->transid == transid) {
56946204592SSage Weil 				cur_trans = t;
570a4abeea4SJosef Bacik 				atomic_inc(&cur_trans->use_count);
5718cd2807fSMiao Xie 				ret = 0;
57246204592SSage Weil 				break;
57346204592SSage Weil 			}
5748cd2807fSMiao Xie 			if (t->transid > transid) {
5758cd2807fSMiao Xie 				ret = 0;
57646204592SSage Weil 				break;
57746204592SSage Weil 			}
5788cd2807fSMiao Xie 		}
579a4abeea4SJosef Bacik 		spin_unlock(&root->fs_info->trans_lock);
5808cd2807fSMiao Xie 		/* The specified transaction doesn't exist */
58146204592SSage Weil 		if (!cur_trans)
5828cd2807fSMiao Xie 			goto out;
58346204592SSage Weil 	} else {
58446204592SSage Weil 		/* find newest transaction that is committing | committed */
585a4abeea4SJosef Bacik 		spin_lock(&root->fs_info->trans_lock);
58646204592SSage Weil 		list_for_each_entry_reverse(t, &root->fs_info->trans_list,
58746204592SSage Weil 					    list) {
5884a9d8bdeSMiao Xie 			if (t->state >= TRANS_STATE_COMMIT_START) {
5894a9d8bdeSMiao Xie 				if (t->state == TRANS_STATE_COMPLETED)
5903473f3c0SJosef Bacik 					break;
59146204592SSage Weil 				cur_trans = t;
592a4abeea4SJosef Bacik 				atomic_inc(&cur_trans->use_count);
59346204592SSage Weil 				break;
59446204592SSage Weil 			}
59546204592SSage Weil 		}
596a4abeea4SJosef Bacik 		spin_unlock(&root->fs_info->trans_lock);
59746204592SSage Weil 		if (!cur_trans)
598a4abeea4SJosef Bacik 			goto out;  /* nothing committing|committed */
59946204592SSage Weil 	}
60046204592SSage Weil 
60146204592SSage Weil 	wait_for_commit(root, cur_trans);
60246204592SSage Weil 	put_transaction(cur_trans);
603a4abeea4SJosef Bacik out:
60446204592SSage Weil 	return ret;
60546204592SSage Weil }
60646204592SSage Weil 
60737d1aeeeSChris Mason void btrfs_throttle(struct btrfs_root *root)
60837d1aeeeSChris Mason {
609a4abeea4SJosef Bacik 	if (!atomic_read(&root->fs_info->open_ioctl_trans))
61037d1aeeeSChris Mason 		wait_current_trans(root);
61137d1aeeeSChris Mason }
61237d1aeeeSChris Mason 
6138929ecfaSYan, Zheng static int should_end_transaction(struct btrfs_trans_handle *trans,
6148929ecfaSYan, Zheng 				  struct btrfs_root *root)
6158929ecfaSYan, Zheng {
6168929ecfaSYan, Zheng 	int ret;
61736ba022aSJosef Bacik 
61836ba022aSJosef Bacik 	ret = btrfs_block_rsv_check(root, &root->fs_info->global_block_rsv, 5);
6198929ecfaSYan, Zheng 	return ret ? 1 : 0;
6208929ecfaSYan, Zheng }
6218929ecfaSYan, Zheng 
6228929ecfaSYan, Zheng int btrfs_should_end_transaction(struct btrfs_trans_handle *trans,
6238929ecfaSYan, Zheng 				 struct btrfs_root *root)
6248929ecfaSYan, Zheng {
6258929ecfaSYan, Zheng 	struct btrfs_transaction *cur_trans = trans->transaction;
6268929ecfaSYan, Zheng 	int updates;
62749b25e05SJeff Mahoney 	int err;
6288929ecfaSYan, Zheng 
629a4abeea4SJosef Bacik 	smp_mb();
6304a9d8bdeSMiao Xie 	if (cur_trans->state >= TRANS_STATE_BLOCKED ||
6314a9d8bdeSMiao Xie 	    cur_trans->delayed_refs.flushing)
6328929ecfaSYan, Zheng 		return 1;
6338929ecfaSYan, Zheng 
6348929ecfaSYan, Zheng 	updates = trans->delayed_ref_updates;
6358929ecfaSYan, Zheng 	trans->delayed_ref_updates = 0;
63649b25e05SJeff Mahoney 	if (updates) {
63749b25e05SJeff Mahoney 		err = btrfs_run_delayed_refs(trans, root, updates);
63849b25e05SJeff Mahoney 		if (err) /* Error code will also eval true */
63949b25e05SJeff Mahoney 			return err;
64049b25e05SJeff Mahoney 	}
6418929ecfaSYan, Zheng 
6428929ecfaSYan, Zheng 	return should_end_transaction(trans, root);
6438929ecfaSYan, Zheng }
6448929ecfaSYan, Zheng 
64589ce8a63SChris Mason static int __btrfs_end_transaction(struct btrfs_trans_handle *trans,
646a698d075SMiao Xie 			  struct btrfs_root *root, int throttle)
64779154b1bSChris Mason {
6488929ecfaSYan, Zheng 	struct btrfs_transaction *cur_trans = trans->transaction;
649ab78c84dSChris Mason 	struct btrfs_fs_info *info = root->fs_info;
650c3e69d58SChris Mason 	int count = 0;
651a698d075SMiao Xie 	int lock = (trans->type != TRANS_JOIN_NOLOCK);
6524edc2ca3SDave Jones 	int err = 0;
653d6e4a428SChris Mason 
6542a1eb461SJosef Bacik 	if (--trans->use_count) {
6552a1eb461SJosef Bacik 		trans->block_rsv = trans->orig_rsv;
6562a1eb461SJosef Bacik 		return 0;
6572a1eb461SJosef Bacik 	}
6582a1eb461SJosef Bacik 
659edf39272SJan Schmidt 	/*
660edf39272SJan Schmidt 	 * do the qgroup accounting as early as possible
661edf39272SJan Schmidt 	 */
662edf39272SJan Schmidt 	err = btrfs_delayed_refs_qgroup_accounting(trans, info);
663edf39272SJan Schmidt 
664b24e03dbSJosef Bacik 	btrfs_trans_release_metadata(trans, root);
6654c13d758SJosef Bacik 	trans->block_rsv = NULL;
666c5567237SArne Jansen 
667c5567237SArne Jansen 	if (trans->qgroup_reserved) {
6687c2ec3f0SLiu Bo 		/*
6697c2ec3f0SLiu Bo 		 * the same root has to be passed here between start_transaction
6707c2ec3f0SLiu Bo 		 * and end_transaction. Subvolume quota depends on this.
6717c2ec3f0SLiu Bo 		 */
6727c2ec3f0SLiu Bo 		btrfs_qgroup_free(trans->root, trans->qgroup_reserved);
673c5567237SArne Jansen 		trans->qgroup_reserved = 0;
674c5567237SArne Jansen 	}
675c5567237SArne Jansen 
676ea658badSJosef Bacik 	if (!list_empty(&trans->new_bgs))
677ea658badSJosef Bacik 		btrfs_create_pending_block_groups(trans, root);
678ea658badSJosef Bacik 
679bb721703SChris Mason 	while (count < 1) {
680c3e69d58SChris Mason 		unsigned long cur = trans->delayed_ref_updates;
681c3e69d58SChris Mason 		trans->delayed_ref_updates = 0;
682c3e69d58SChris Mason 		if (cur &&
683c3e69d58SChris Mason 		    trans->transaction->delayed_refs.num_heads_ready > 64) {
684c3e69d58SChris Mason 			trans->delayed_ref_updates = 0;
685c3e69d58SChris Mason 			btrfs_run_delayed_refs(trans, root, cur);
686c3e69d58SChris Mason 		} else {
687c3e69d58SChris Mason 			break;
688c3e69d58SChris Mason 		}
689c3e69d58SChris Mason 		count++;
69056bec294SChris Mason 	}
691bb721703SChris Mason 
6920e721106SJosef Bacik 	btrfs_trans_release_metadata(trans, root);
6930e721106SJosef Bacik 	trans->block_rsv = NULL;
69456bec294SChris Mason 
695ea658badSJosef Bacik 	if (!list_empty(&trans->new_bgs))
696ea658badSJosef Bacik 		btrfs_create_pending_block_groups(trans, root);
697ea658badSJosef Bacik 
698a4abeea4SJosef Bacik 	if (lock && !atomic_read(&root->fs_info->open_ioctl_trans) &&
6994a9d8bdeSMiao Xie 	    should_end_transaction(trans, root) &&
7004a9d8bdeSMiao Xie 	    ACCESS_ONCE(cur_trans->state) == TRANS_STATE_RUNNING) {
7014a9d8bdeSMiao Xie 		spin_lock(&info->trans_lock);
7024a9d8bdeSMiao Xie 		if (cur_trans->state == TRANS_STATE_RUNNING)
7034a9d8bdeSMiao Xie 			cur_trans->state = TRANS_STATE_BLOCKED;
7044a9d8bdeSMiao Xie 		spin_unlock(&info->trans_lock);
705a4abeea4SJosef Bacik 	}
7068929ecfaSYan, Zheng 
7074a9d8bdeSMiao Xie 	if (lock && ACCESS_ONCE(cur_trans->state) == TRANS_STATE_BLOCKED) {
70881317fdeSJosef Bacik 		if (throttle) {
70981317fdeSJosef Bacik 			/*
71081317fdeSJosef Bacik 			 * We may race with somebody else here so end up having
71181317fdeSJosef Bacik 			 * to call end_transaction on ourselves again, so inc
71281317fdeSJosef Bacik 			 * our use_count.
71381317fdeSJosef Bacik 			 */
71481317fdeSJosef Bacik 			trans->use_count++;
7158929ecfaSYan, Zheng 			return btrfs_commit_transaction(trans, root);
71681317fdeSJosef Bacik 		} else {
7178929ecfaSYan, Zheng 			wake_up_process(info->transaction_kthread);
7188929ecfaSYan, Zheng 		}
71981317fdeSJosef Bacik 	}
7208929ecfaSYan, Zheng 
7210860adfdSMiao Xie 	if (trans->type & __TRANS_FREEZABLE)
7226df7881aSJosef Bacik 		sb_end_intwrite(root->fs_info->sb);
7236df7881aSJosef Bacik 
7248929ecfaSYan, Zheng 	WARN_ON(cur_trans != info->running_transaction);
72513c5a93eSJosef Bacik 	WARN_ON(atomic_read(&cur_trans->num_writers) < 1);
72613c5a93eSJosef Bacik 	atomic_dec(&cur_trans->num_writers);
7270860adfdSMiao Xie 	extwriter_counter_dec(cur_trans, trans->type);
72889ce8a63SChris Mason 
72999d16cbcSSage Weil 	smp_mb();
73079154b1bSChris Mason 	if (waitqueue_active(&cur_trans->writer_wait))
73179154b1bSChris Mason 		wake_up(&cur_trans->writer_wait);
73279154b1bSChris Mason 	put_transaction(cur_trans);
7339ed74f2dSJosef Bacik 
7349ed74f2dSJosef Bacik 	if (current->journal_info == trans)
7359ed74f2dSJosef Bacik 		current->journal_info = NULL;
736ab78c84dSChris Mason 
73724bbcf04SYan, Zheng 	if (throttle)
73824bbcf04SYan, Zheng 		btrfs_run_delayed_iputs(root);
73924bbcf04SYan, Zheng 
74049b25e05SJeff Mahoney 	if (trans->aborted ||
74187533c47SMiao Xie 	    test_bit(BTRFS_FS_STATE_ERROR, &root->fs_info->fs_state))
7424edc2ca3SDave Jones 		err = -EIO;
743edf39272SJan Schmidt 	assert_qgroups_uptodate(trans);
74449b25e05SJeff Mahoney 
7454edc2ca3SDave Jones 	kmem_cache_free(btrfs_trans_handle_cachep, trans);
7464edc2ca3SDave Jones 	return err;
74779154b1bSChris Mason }
74879154b1bSChris Mason 
74989ce8a63SChris Mason int btrfs_end_transaction(struct btrfs_trans_handle *trans,
75089ce8a63SChris Mason 			  struct btrfs_root *root)
75189ce8a63SChris Mason {
75298ad43beSWang Shilong 	return __btrfs_end_transaction(trans, root, 0);
75389ce8a63SChris Mason }
75489ce8a63SChris Mason 
75589ce8a63SChris Mason int btrfs_end_transaction_throttle(struct btrfs_trans_handle *trans,
75689ce8a63SChris Mason 				   struct btrfs_root *root)
75789ce8a63SChris Mason {
75898ad43beSWang Shilong 	return __btrfs_end_transaction(trans, root, 1);
75916cdcec7SMiao Xie }
76016cdcec7SMiao Xie 
76116cdcec7SMiao Xie int btrfs_end_transaction_dmeta(struct btrfs_trans_handle *trans,
76216cdcec7SMiao Xie 				struct btrfs_root *root)
76316cdcec7SMiao Xie {
764a698d075SMiao Xie 	return __btrfs_end_transaction(trans, root, 1);
76589ce8a63SChris Mason }
76689ce8a63SChris Mason 
767d352ac68SChris Mason /*
768d352ac68SChris Mason  * when btree blocks are allocated, they have some corresponding bits set for
769d352ac68SChris Mason  * them in one of two extent_io trees.  This is used to make sure all of
770690587d1SChris Mason  * those extents are sent to disk but does not wait on them
771d352ac68SChris Mason  */
772690587d1SChris Mason int btrfs_write_marked_extents(struct btrfs_root *root,
7738cef4e16SYan, Zheng 			       struct extent_io_tree *dirty_pages, int mark)
77479154b1bSChris Mason {
775777e6bd7SChris Mason 	int err = 0;
7767c4452b9SChris Mason 	int werr = 0;
7771728366eSJosef Bacik 	struct address_space *mapping = root->fs_info->btree_inode->i_mapping;
778e6138876SJosef Bacik 	struct extent_state *cached_state = NULL;
779777e6bd7SChris Mason 	u64 start = 0;
7805f39d397SChris Mason 	u64 end;
78153b381b3SDavid Woodhouse 	struct blk_plug plug;
7827c4452b9SChris Mason 
78353b381b3SDavid Woodhouse 	blk_start_plug(&plug);
7841728366eSJosef Bacik 	while (!find_first_extent_bit(dirty_pages, start, &start, &end,
785e6138876SJosef Bacik 				      mark, &cached_state)) {
786e6138876SJosef Bacik 		convert_extent_bit(dirty_pages, start, end, EXTENT_NEED_WAIT,
787e6138876SJosef Bacik 				   mark, &cached_state, GFP_NOFS);
788e6138876SJosef Bacik 		cached_state = NULL;
7891728366eSJosef Bacik 		err = filemap_fdatawrite_range(mapping, start, end);
7907c4452b9SChris Mason 		if (err)
7917c4452b9SChris Mason 			werr = err;
7921728366eSJosef Bacik 		cond_resched();
7931728366eSJosef Bacik 		start = end + 1;
7947c4452b9SChris Mason 	}
795690587d1SChris Mason 	if (err)
796690587d1SChris Mason 		werr = err;
79753b381b3SDavid Woodhouse 	blk_finish_plug(&plug);
798690587d1SChris Mason 	return werr;
799690587d1SChris Mason }
800690587d1SChris Mason 
801690587d1SChris Mason /*
802690587d1SChris Mason  * when btree blocks are allocated, they have some corresponding bits set for
803690587d1SChris Mason  * them in one of two extent_io trees.  This is used to make sure all of
804690587d1SChris Mason  * those extents are on disk for transaction or log commit.  We wait
805690587d1SChris Mason  * on all the pages and clear them from the dirty pages state tree
806690587d1SChris Mason  */
807690587d1SChris Mason int btrfs_wait_marked_extents(struct btrfs_root *root,
8088cef4e16SYan, Zheng 			      struct extent_io_tree *dirty_pages, int mark)
809690587d1SChris Mason {
810690587d1SChris Mason 	int err = 0;
811690587d1SChris Mason 	int werr = 0;
8121728366eSJosef Bacik 	struct address_space *mapping = root->fs_info->btree_inode->i_mapping;
813e6138876SJosef Bacik 	struct extent_state *cached_state = NULL;
814690587d1SChris Mason 	u64 start = 0;
815690587d1SChris Mason 	u64 end;
816690587d1SChris Mason 
8171728366eSJosef Bacik 	while (!find_first_extent_bit(dirty_pages, start, &start, &end,
818e6138876SJosef Bacik 				      EXTENT_NEED_WAIT, &cached_state)) {
819e6138876SJosef Bacik 		clear_extent_bit(dirty_pages, start, end, EXTENT_NEED_WAIT,
820e6138876SJosef Bacik 				 0, 0, &cached_state, GFP_NOFS);
8211728366eSJosef Bacik 		err = filemap_fdatawait_range(mapping, start, end);
822777e6bd7SChris Mason 		if (err)
823777e6bd7SChris Mason 			werr = err;
824777e6bd7SChris Mason 		cond_resched();
8251728366eSJosef Bacik 		start = end + 1;
826777e6bd7SChris Mason 	}
8277c4452b9SChris Mason 	if (err)
8287c4452b9SChris Mason 		werr = err;
8297c4452b9SChris Mason 	return werr;
83079154b1bSChris Mason }
83179154b1bSChris Mason 
832690587d1SChris Mason /*
833690587d1SChris Mason  * when btree blocks are allocated, they have some corresponding bits set for
834690587d1SChris Mason  * them in one of two extent_io trees.  This is used to make sure all of
835690587d1SChris Mason  * those extents are on disk for transaction or log commit
836690587d1SChris Mason  */
837690587d1SChris Mason int btrfs_write_and_wait_marked_extents(struct btrfs_root *root,
8388cef4e16SYan, Zheng 				struct extent_io_tree *dirty_pages, int mark)
839690587d1SChris Mason {
840690587d1SChris Mason 	int ret;
841690587d1SChris Mason 	int ret2;
842690587d1SChris Mason 
8438cef4e16SYan, Zheng 	ret = btrfs_write_marked_extents(root, dirty_pages, mark);
8448cef4e16SYan, Zheng 	ret2 = btrfs_wait_marked_extents(root, dirty_pages, mark);
845bf0da8c1SChris Mason 
846bf0da8c1SChris Mason 	if (ret)
847bf0da8c1SChris Mason 		return ret;
848bf0da8c1SChris Mason 	if (ret2)
849bf0da8c1SChris Mason 		return ret2;
850bf0da8c1SChris Mason 	return 0;
851690587d1SChris Mason }
852690587d1SChris Mason 
853d0c803c4SChris Mason int btrfs_write_and_wait_transaction(struct btrfs_trans_handle *trans,
854d0c803c4SChris Mason 				     struct btrfs_root *root)
855d0c803c4SChris Mason {
856d0c803c4SChris Mason 	if (!trans || !trans->transaction) {
857d0c803c4SChris Mason 		struct inode *btree_inode;
858d0c803c4SChris Mason 		btree_inode = root->fs_info->btree_inode;
859d0c803c4SChris Mason 		return filemap_write_and_wait(btree_inode->i_mapping);
860d0c803c4SChris Mason 	}
861d0c803c4SChris Mason 	return btrfs_write_and_wait_marked_extents(root,
8628cef4e16SYan, Zheng 					   &trans->transaction->dirty_pages,
8638cef4e16SYan, Zheng 					   EXTENT_DIRTY);
864d0c803c4SChris Mason }
865d0c803c4SChris Mason 
866d352ac68SChris Mason /*
867d352ac68SChris Mason  * this is used to update the root pointer in the tree of tree roots.
868d352ac68SChris Mason  *
869d352ac68SChris Mason  * But, in the case of the extent allocation tree, updating the root
870d352ac68SChris Mason  * pointer may allocate blocks which may change the root of the extent
871d352ac68SChris Mason  * allocation tree.
872d352ac68SChris Mason  *
873d352ac68SChris Mason  * So, this loops and repeats and makes sure the cowonly root didn't
874d352ac68SChris Mason  * change while the root pointer was being updated in the metadata.
875d352ac68SChris Mason  */
8760b86a832SChris Mason static int update_cowonly_root(struct btrfs_trans_handle *trans,
87779154b1bSChris Mason 			       struct btrfs_root *root)
87879154b1bSChris Mason {
87979154b1bSChris Mason 	int ret;
8800b86a832SChris Mason 	u64 old_root_bytenr;
88186b9f2ecSYan, Zheng 	u64 old_root_used;
8820b86a832SChris Mason 	struct btrfs_root *tree_root = root->fs_info->tree_root;
88379154b1bSChris Mason 
88486b9f2ecSYan, Zheng 	old_root_used = btrfs_root_used(&root->root_item);
8850b86a832SChris Mason 	btrfs_write_dirty_block_groups(trans, root);
88656bec294SChris Mason 
88779154b1bSChris Mason 	while (1) {
8880b86a832SChris Mason 		old_root_bytenr = btrfs_root_bytenr(&root->root_item);
88986b9f2ecSYan, Zheng 		if (old_root_bytenr == root->node->start &&
89086b9f2ecSYan, Zheng 		    old_root_used == btrfs_root_used(&root->root_item))
89179154b1bSChris Mason 			break;
89287ef2bb4SChris Mason 
8935d4f98a2SYan Zheng 		btrfs_set_root_node(&root->root_item, root->node);
89479154b1bSChris Mason 		ret = btrfs_update_root(trans, tree_root,
8950b86a832SChris Mason 					&root->root_key,
8960b86a832SChris Mason 					&root->root_item);
89749b25e05SJeff Mahoney 		if (ret)
89849b25e05SJeff Mahoney 			return ret;
89956bec294SChris Mason 
90086b9f2ecSYan, Zheng 		old_root_used = btrfs_root_used(&root->root_item);
9014a8c9a62SYan Zheng 		ret = btrfs_write_dirty_block_groups(trans, root);
90249b25e05SJeff Mahoney 		if (ret)
90349b25e05SJeff Mahoney 			return ret;
9040b86a832SChris Mason 	}
905276e680dSYan Zheng 
906276e680dSYan Zheng 	if (root != root->fs_info->extent_root)
907817d52f8SJosef Bacik 		switch_commit_root(root);
908276e680dSYan Zheng 
9090b86a832SChris Mason 	return 0;
9100b86a832SChris Mason }
9110b86a832SChris Mason 
912d352ac68SChris Mason /*
913d352ac68SChris Mason  * update all the cowonly tree roots on disk
91449b25e05SJeff Mahoney  *
91549b25e05SJeff Mahoney  * The error handling in this function may not be obvious. Any of the
91649b25e05SJeff Mahoney  * failures will cause the file system to go offline. We still need
91749b25e05SJeff Mahoney  * to clean up the delayed refs.
918d352ac68SChris Mason  */
9195d4f98a2SYan Zheng static noinline int commit_cowonly_roots(struct btrfs_trans_handle *trans,
9200b86a832SChris Mason 					 struct btrfs_root *root)
9210b86a832SChris Mason {
9220b86a832SChris Mason 	struct btrfs_fs_info *fs_info = root->fs_info;
9230b86a832SChris Mason 	struct list_head *next;
92484234f3aSYan Zheng 	struct extent_buffer *eb;
92556bec294SChris Mason 	int ret;
92684234f3aSYan Zheng 
92756bec294SChris Mason 	ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
92849b25e05SJeff Mahoney 	if (ret)
92949b25e05SJeff Mahoney 		return ret;
93087ef2bb4SChris Mason 
93184234f3aSYan Zheng 	eb = btrfs_lock_root_node(fs_info->tree_root);
93249b25e05SJeff Mahoney 	ret = btrfs_cow_block(trans, fs_info->tree_root, eb, NULL,
93349b25e05SJeff Mahoney 			      0, &eb);
93484234f3aSYan Zheng 	btrfs_tree_unlock(eb);
93584234f3aSYan Zheng 	free_extent_buffer(eb);
9360b86a832SChris Mason 
93749b25e05SJeff Mahoney 	if (ret)
93849b25e05SJeff Mahoney 		return ret;
93949b25e05SJeff Mahoney 
94056bec294SChris Mason 	ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
94149b25e05SJeff Mahoney 	if (ret)
94249b25e05SJeff Mahoney 		return ret;
94387ef2bb4SChris Mason 
944733f4fbbSStefan Behrens 	ret = btrfs_run_dev_stats(trans, root->fs_info);
9458dabb742SStefan Behrens 	WARN_ON(ret);
9468dabb742SStefan Behrens 	ret = btrfs_run_dev_replace(trans, root->fs_info);
9478dabb742SStefan Behrens 	WARN_ON(ret);
948733f4fbbSStefan Behrens 
949546adb0dSJan Schmidt 	ret = btrfs_run_qgroups(trans, root->fs_info);
950546adb0dSJan Schmidt 	BUG_ON(ret);
951546adb0dSJan Schmidt 
952546adb0dSJan Schmidt 	/* run_qgroups might have added some more refs */
953546adb0dSJan Schmidt 	ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
954546adb0dSJan Schmidt 	BUG_ON(ret);
955546adb0dSJan Schmidt 
9560b86a832SChris Mason 	while (!list_empty(&fs_info->dirty_cowonly_roots)) {
9570b86a832SChris Mason 		next = fs_info->dirty_cowonly_roots.next;
9580b86a832SChris Mason 		list_del_init(next);
9590b86a832SChris Mason 		root = list_entry(next, struct btrfs_root, dirty_list);
96087ef2bb4SChris Mason 
96149b25e05SJeff Mahoney 		ret = update_cowonly_root(trans, root);
96249b25e05SJeff Mahoney 		if (ret)
96349b25e05SJeff Mahoney 			return ret;
96479154b1bSChris Mason 	}
965276e680dSYan Zheng 
966276e680dSYan Zheng 	down_write(&fs_info->extent_commit_sem);
967276e680dSYan Zheng 	switch_commit_root(fs_info->extent_root);
968276e680dSYan Zheng 	up_write(&fs_info->extent_commit_sem);
969276e680dSYan Zheng 
9708dabb742SStefan Behrens 	btrfs_after_dev_replace_commit(fs_info);
9718dabb742SStefan Behrens 
97279154b1bSChris Mason 	return 0;
97379154b1bSChris Mason }
97479154b1bSChris Mason 
975d352ac68SChris Mason /*
976d352ac68SChris Mason  * dead roots are old snapshots that need to be deleted.  This allocates
977d352ac68SChris Mason  * a dirty root struct and adds it into the list of dead roots that need to
978d352ac68SChris Mason  * be deleted
979d352ac68SChris Mason  */
9805d4f98a2SYan Zheng int btrfs_add_dead_root(struct btrfs_root *root)
9815eda7b5eSChris Mason {
982a4abeea4SJosef Bacik 	spin_lock(&root->fs_info->trans_lock);
9839d1a2a3aSDavid Sterba 	list_add_tail(&root->root_list, &root->fs_info->dead_roots);
984a4abeea4SJosef Bacik 	spin_unlock(&root->fs_info->trans_lock);
9855eda7b5eSChris Mason 	return 0;
9865eda7b5eSChris Mason }
9875eda7b5eSChris Mason 
988d352ac68SChris Mason /*
9895d4f98a2SYan Zheng  * update all the cowonly tree roots on disk
990d352ac68SChris Mason  */
9915d4f98a2SYan Zheng static noinline int commit_fs_roots(struct btrfs_trans_handle *trans,
9925d4f98a2SYan Zheng 				    struct btrfs_root *root)
9930f7d52f4SChris Mason {
9940f7d52f4SChris Mason 	struct btrfs_root *gang[8];
9955d4f98a2SYan Zheng 	struct btrfs_fs_info *fs_info = root->fs_info;
9960f7d52f4SChris Mason 	int i;
9970f7d52f4SChris Mason 	int ret;
99854aa1f4dSChris Mason 	int err = 0;
99954aa1f4dSChris Mason 
1000a4abeea4SJosef Bacik 	spin_lock(&fs_info->fs_roots_radix_lock);
10010f7d52f4SChris Mason 	while (1) {
10025d4f98a2SYan Zheng 		ret = radix_tree_gang_lookup_tag(&fs_info->fs_roots_radix,
10035d4f98a2SYan Zheng 						 (void **)gang, 0,
10040f7d52f4SChris Mason 						 ARRAY_SIZE(gang),
10050f7d52f4SChris Mason 						 BTRFS_ROOT_TRANS_TAG);
10060f7d52f4SChris Mason 		if (ret == 0)
10070f7d52f4SChris Mason 			break;
10080f7d52f4SChris Mason 		for (i = 0; i < ret; i++) {
10090f7d52f4SChris Mason 			root = gang[i];
10105d4f98a2SYan Zheng 			radix_tree_tag_clear(&fs_info->fs_roots_radix,
10112619ba1fSChris Mason 					(unsigned long)root->root_key.objectid,
10120f7d52f4SChris Mason 					BTRFS_ROOT_TRANS_TAG);
1013a4abeea4SJosef Bacik 			spin_unlock(&fs_info->fs_roots_radix_lock);
101431153d81SYan Zheng 
1015e02119d5SChris Mason 			btrfs_free_log(trans, root);
10165d4f98a2SYan Zheng 			btrfs_update_reloc_root(trans, root);
1017d68fc57bSYan, Zheng 			btrfs_orphan_commit_root(trans, root);
1018e02119d5SChris Mason 
101982d5902dSLi Zefan 			btrfs_save_ino_cache(root, trans);
102082d5902dSLi Zefan 
1021f1ebcc74SLiu Bo 			/* see comments in should_cow_block() */
1022f1ebcc74SLiu Bo 			root->force_cow = 0;
1023f1ebcc74SLiu Bo 			smp_wmb();
1024f1ebcc74SLiu Bo 
1025978d910dSYan Zheng 			if (root->commit_root != root->node) {
1026581bb050SLi Zefan 				mutex_lock(&root->fs_commit_mutex);
1027817d52f8SJosef Bacik 				switch_commit_root(root);
1028581bb050SLi Zefan 				btrfs_unpin_free_ino(root);
1029581bb050SLi Zefan 				mutex_unlock(&root->fs_commit_mutex);
1030581bb050SLi Zefan 
1031978d910dSYan Zheng 				btrfs_set_root_node(&root->root_item,
1032978d910dSYan Zheng 						    root->node);
1033978d910dSYan Zheng 			}
103431153d81SYan Zheng 
10355d4f98a2SYan Zheng 			err = btrfs_update_root(trans, fs_info->tree_root,
10360f7d52f4SChris Mason 						&root->root_key,
10370f7d52f4SChris Mason 						&root->root_item);
1038a4abeea4SJosef Bacik 			spin_lock(&fs_info->fs_roots_radix_lock);
103954aa1f4dSChris Mason 			if (err)
104054aa1f4dSChris Mason 				break;
10410f7d52f4SChris Mason 		}
10429f3a7427SChris Mason 	}
1043a4abeea4SJosef Bacik 	spin_unlock(&fs_info->fs_roots_radix_lock);
104454aa1f4dSChris Mason 	return err;
10450f7d52f4SChris Mason }
10460f7d52f4SChris Mason 
1047d352ac68SChris Mason /*
1048de78b51aSEric Sandeen  * defrag a given btree.
1049de78b51aSEric Sandeen  * Every leaf in the btree is read and defragged.
1050d352ac68SChris Mason  */
1051de78b51aSEric Sandeen int btrfs_defrag_root(struct btrfs_root *root)
1052e9d0b13bSChris Mason {
1053e9d0b13bSChris Mason 	struct btrfs_fs_info *info = root->fs_info;
1054e9d0b13bSChris Mason 	struct btrfs_trans_handle *trans;
10558929ecfaSYan, Zheng 	int ret;
1056e9d0b13bSChris Mason 
10578929ecfaSYan, Zheng 	if (xchg(&root->defrag_running, 1))
1058e9d0b13bSChris Mason 		return 0;
10598929ecfaSYan, Zheng 
10606b80053dSChris Mason 	while (1) {
10618929ecfaSYan, Zheng 		trans = btrfs_start_transaction(root, 0);
10628929ecfaSYan, Zheng 		if (IS_ERR(trans))
10638929ecfaSYan, Zheng 			return PTR_ERR(trans);
10648929ecfaSYan, Zheng 
1065de78b51aSEric Sandeen 		ret = btrfs_defrag_leaves(trans, root);
10668929ecfaSYan, Zheng 
1067e9d0b13bSChris Mason 		btrfs_end_transaction(trans, root);
1068b53d3f5dSLiu Bo 		btrfs_btree_balance_dirty(info->tree_root);
1069e9d0b13bSChris Mason 		cond_resched();
1070e9d0b13bSChris Mason 
10717841cb28SDavid Sterba 		if (btrfs_fs_closing(root->fs_info) || ret != -EAGAIN)
1072e9d0b13bSChris Mason 			break;
1073210549ebSDavid Sterba 
1074210549ebSDavid Sterba 		if (btrfs_defrag_cancelled(root->fs_info)) {
1075210549ebSDavid Sterba 			printk(KERN_DEBUG "btrfs: defrag_root cancelled\n");
1076210549ebSDavid Sterba 			ret = -EAGAIN;
1077210549ebSDavid Sterba 			break;
1078210549ebSDavid Sterba 		}
1079e9d0b13bSChris Mason 	}
1080e9d0b13bSChris Mason 	root->defrag_running = 0;
10818929ecfaSYan, Zheng 	return ret;
1082e9d0b13bSChris Mason }
1083e9d0b13bSChris Mason 
1084d352ac68SChris Mason /*
1085d352ac68SChris Mason  * new snapshots need to be created at a very specific time in the
1086aec8030aSMiao Xie  * transaction commit.  This does the actual creation.
1087aec8030aSMiao Xie  *
1088aec8030aSMiao Xie  * Note:
1089aec8030aSMiao Xie  * If the error which may affect the commitment of the current transaction
1090aec8030aSMiao Xie  * happens, we should return the error number. If the error which just affect
1091aec8030aSMiao Xie  * the creation of the pending snapshots, just return 0.
1092d352ac68SChris Mason  */
109380b6794dSChris Mason static noinline int create_pending_snapshot(struct btrfs_trans_handle *trans,
10943063d29fSChris Mason 				   struct btrfs_fs_info *fs_info,
10953063d29fSChris Mason 				   struct btrfs_pending_snapshot *pending)
10963063d29fSChris Mason {
10973063d29fSChris Mason 	struct btrfs_key key;
109880b6794dSChris Mason 	struct btrfs_root_item *new_root_item;
10993063d29fSChris Mason 	struct btrfs_root *tree_root = fs_info->tree_root;
11003063d29fSChris Mason 	struct btrfs_root *root = pending->root;
11016bdb72deSSage Weil 	struct btrfs_root *parent_root;
110298c9942aSLiu Bo 	struct btrfs_block_rsv *rsv;
11036bdb72deSSage Weil 	struct inode *parent_inode;
110442874b3dSMiao Xie 	struct btrfs_path *path;
110542874b3dSMiao Xie 	struct btrfs_dir_item *dir_item;
1106a22285a6SYan, Zheng 	struct dentry *dentry;
11073063d29fSChris Mason 	struct extent_buffer *tmp;
1108925baeddSChris Mason 	struct extent_buffer *old;
11098ea05e3aSAlexander Block 	struct timespec cur_time = CURRENT_TIME;
1110aec8030aSMiao Xie 	int ret = 0;
1111d68fc57bSYan, Zheng 	u64 to_reserve = 0;
11126bdb72deSSage Weil 	u64 index = 0;
1113a22285a6SYan, Zheng 	u64 objectid;
1114b83cc969SLi Zefan 	u64 root_flags;
11158ea05e3aSAlexander Block 	uuid_le new_uuid;
11163063d29fSChris Mason 
111742874b3dSMiao Xie 	path = btrfs_alloc_path();
111842874b3dSMiao Xie 	if (!path) {
1119aec8030aSMiao Xie 		pending->error = -ENOMEM;
1120aec8030aSMiao Xie 		return 0;
112142874b3dSMiao Xie 	}
112242874b3dSMiao Xie 
112380b6794dSChris Mason 	new_root_item = kmalloc(sizeof(*new_root_item), GFP_NOFS);
112480b6794dSChris Mason 	if (!new_root_item) {
1125aec8030aSMiao Xie 		pending->error = -ENOMEM;
11266fa9700eSMiao Xie 		goto root_item_alloc_fail;
112780b6794dSChris Mason 	}
1128a22285a6SYan, Zheng 
1129aec8030aSMiao Xie 	pending->error = btrfs_find_free_objectid(tree_root, &objectid);
1130aec8030aSMiao Xie 	if (pending->error)
11316fa9700eSMiao Xie 		goto no_free_objectid;
11323063d29fSChris Mason 
11333fd0a558SYan, Zheng 	btrfs_reloc_pre_snapshot(trans, pending, &to_reserve);
1134d68fc57bSYan, Zheng 
1135d68fc57bSYan, Zheng 	if (to_reserve > 0) {
1136aec8030aSMiao Xie 		pending->error = btrfs_block_rsv_add(root,
1137aec8030aSMiao Xie 						     &pending->block_rsv,
113808e007d2SMiao Xie 						     to_reserve,
113908e007d2SMiao Xie 						     BTRFS_RESERVE_NO_FLUSH);
1140aec8030aSMiao Xie 		if (pending->error)
11416fa9700eSMiao Xie 			goto no_free_objectid;
1142d68fc57bSYan, Zheng 	}
1143d68fc57bSYan, Zheng 
1144aec8030aSMiao Xie 	pending->error = btrfs_qgroup_inherit(trans, fs_info,
1145aec8030aSMiao Xie 					      root->root_key.objectid,
11466f72c7e2SArne Jansen 					      objectid, pending->inherit);
1147aec8030aSMiao Xie 	if (pending->error)
11486fa9700eSMiao Xie 		goto no_free_objectid;
11496f72c7e2SArne Jansen 
11503063d29fSChris Mason 	key.objectid = objectid;
1151a22285a6SYan, Zheng 	key.offset = (u64)-1;
1152a22285a6SYan, Zheng 	key.type = BTRFS_ROOT_ITEM_KEY;
11533063d29fSChris Mason 
11546fa9700eSMiao Xie 	rsv = trans->block_rsv;
1155a22285a6SYan, Zheng 	trans->block_rsv = &pending->block_rsv;
11562382c5ccSLiu Bo 	trans->bytes_reserved = trans->block_rsv->reserved;
11576bdb72deSSage Weil 
1158a22285a6SYan, Zheng 	dentry = pending->dentry;
1159e9662f70SMiao Xie 	parent_inode = pending->dir;
1160a22285a6SYan, Zheng 	parent_root = BTRFS_I(parent_inode)->root;
11617585717fSChris Mason 	record_root_in_trans(trans, parent_root);
1162a22285a6SYan, Zheng 
11636bdb72deSSage Weil 	/*
11646bdb72deSSage Weil 	 * insert the directory item
11656bdb72deSSage Weil 	 */
11666bdb72deSSage Weil 	ret = btrfs_set_inode_index(parent_inode, &index);
116749b25e05SJeff Mahoney 	BUG_ON(ret); /* -ENOMEM */
116842874b3dSMiao Xie 
116942874b3dSMiao Xie 	/* check if there is a file/dir which has the same name. */
117042874b3dSMiao Xie 	dir_item = btrfs_lookup_dir_item(NULL, parent_root, path,
117142874b3dSMiao Xie 					 btrfs_ino(parent_inode),
117242874b3dSMiao Xie 					 dentry->d_name.name,
117342874b3dSMiao Xie 					 dentry->d_name.len, 0);
117442874b3dSMiao Xie 	if (dir_item != NULL && !IS_ERR(dir_item)) {
1175fe66a05aSChris Mason 		pending->error = -EEXIST;
1176aec8030aSMiao Xie 		goto dir_item_existed;
117742874b3dSMiao Xie 	} else if (IS_ERR(dir_item)) {
117842874b3dSMiao Xie 		ret = PTR_ERR(dir_item);
11798732d44fSMiao Xie 		btrfs_abort_transaction(trans, root, ret);
11808732d44fSMiao Xie 		goto fail;
118179787eaaSJeff Mahoney 	}
118242874b3dSMiao Xie 	btrfs_release_path(path);
11836bdb72deSSage Weil 
1184e999376fSChris Mason 	/*
1185e999376fSChris Mason 	 * pull in the delayed directory update
1186e999376fSChris Mason 	 * and the delayed inode item
1187e999376fSChris Mason 	 * otherwise we corrupt the FS during
1188e999376fSChris Mason 	 * snapshot
1189e999376fSChris Mason 	 */
1190e999376fSChris Mason 	ret = btrfs_run_delayed_items(trans, root);
11918732d44fSMiao Xie 	if (ret) {	/* Transaction aborted */
11928732d44fSMiao Xie 		btrfs_abort_transaction(trans, root, ret);
11938732d44fSMiao Xie 		goto fail;
11948732d44fSMiao Xie 	}
1195e999376fSChris Mason 
11967585717fSChris Mason 	record_root_in_trans(trans, root);
11976bdb72deSSage Weil 	btrfs_set_root_last_snapshot(&root->root_item, trans->transid);
11986bdb72deSSage Weil 	memcpy(new_root_item, &root->root_item, sizeof(*new_root_item));
119908fe4db1SLi Zefan 	btrfs_check_and_init_root_item(new_root_item);
12006bdb72deSSage Weil 
1201b83cc969SLi Zefan 	root_flags = btrfs_root_flags(new_root_item);
1202b83cc969SLi Zefan 	if (pending->readonly)
1203b83cc969SLi Zefan 		root_flags |= BTRFS_ROOT_SUBVOL_RDONLY;
1204b83cc969SLi Zefan 	else
1205b83cc969SLi Zefan 		root_flags &= ~BTRFS_ROOT_SUBVOL_RDONLY;
1206b83cc969SLi Zefan 	btrfs_set_root_flags(new_root_item, root_flags);
1207b83cc969SLi Zefan 
12088ea05e3aSAlexander Block 	btrfs_set_root_generation_v2(new_root_item,
12098ea05e3aSAlexander Block 			trans->transid);
12108ea05e3aSAlexander Block 	uuid_le_gen(&new_uuid);
12118ea05e3aSAlexander Block 	memcpy(new_root_item->uuid, new_uuid.b, BTRFS_UUID_SIZE);
12128ea05e3aSAlexander Block 	memcpy(new_root_item->parent_uuid, root->root_item.uuid,
12138ea05e3aSAlexander Block 			BTRFS_UUID_SIZE);
121470023da2SStefan Behrens 	if (!(root_flags & BTRFS_ROOT_SUBVOL_RDONLY)) {
121570023da2SStefan Behrens 		memset(new_root_item->received_uuid, 0,
121670023da2SStefan Behrens 		       sizeof(new_root_item->received_uuid));
12178ea05e3aSAlexander Block 		memset(&new_root_item->stime, 0, sizeof(new_root_item->stime));
12188ea05e3aSAlexander Block 		memset(&new_root_item->rtime, 0, sizeof(new_root_item->rtime));
12198ea05e3aSAlexander Block 		btrfs_set_root_stransid(new_root_item, 0);
12208ea05e3aSAlexander Block 		btrfs_set_root_rtransid(new_root_item, 0);
122170023da2SStefan Behrens 	}
122270023da2SStefan Behrens 	new_root_item->otime.sec = cpu_to_le64(cur_time.tv_sec);
122370023da2SStefan Behrens 	new_root_item->otime.nsec = cpu_to_le32(cur_time.tv_nsec);
122470023da2SStefan Behrens 	btrfs_set_root_otransid(new_root_item, trans->transid);
12258ea05e3aSAlexander Block 
1226925baeddSChris Mason 	old = btrfs_lock_root_node(root);
122749b25e05SJeff Mahoney 	ret = btrfs_cow_block(trans, root, old, NULL, 0, &old);
122879787eaaSJeff Mahoney 	if (ret) {
122979787eaaSJeff Mahoney 		btrfs_tree_unlock(old);
123079787eaaSJeff Mahoney 		free_extent_buffer(old);
12318732d44fSMiao Xie 		btrfs_abort_transaction(trans, root, ret);
12328732d44fSMiao Xie 		goto fail;
123379787eaaSJeff Mahoney 	}
123449b25e05SJeff Mahoney 
12355d4f98a2SYan Zheng 	btrfs_set_lock_blocking(old);
12363063d29fSChris Mason 
123749b25e05SJeff Mahoney 	ret = btrfs_copy_root(trans, root, old, &tmp, objectid);
123879787eaaSJeff Mahoney 	/* clean up in any case */
1239925baeddSChris Mason 	btrfs_tree_unlock(old);
1240925baeddSChris Mason 	free_extent_buffer(old);
12418732d44fSMiao Xie 	if (ret) {
12428732d44fSMiao Xie 		btrfs_abort_transaction(trans, root, ret);
12438732d44fSMiao Xie 		goto fail;
12448732d44fSMiao Xie 	}
12453063d29fSChris Mason 
1246f1ebcc74SLiu Bo 	/* see comments in should_cow_block() */
1247f1ebcc74SLiu Bo 	root->force_cow = 1;
1248f1ebcc74SLiu Bo 	smp_wmb();
1249f1ebcc74SLiu Bo 
12505d4f98a2SYan Zheng 	btrfs_set_root_node(new_root_item, tmp);
1251a22285a6SYan, Zheng 	/* record when the snapshot was created in key.offset */
1252a22285a6SYan, Zheng 	key.offset = trans->transid;
1253a22285a6SYan, Zheng 	ret = btrfs_insert_root(trans, tree_root, &key, new_root_item);
1254925baeddSChris Mason 	btrfs_tree_unlock(tmp);
12553063d29fSChris Mason 	free_extent_buffer(tmp);
12568732d44fSMiao Xie 	if (ret) {
12578732d44fSMiao Xie 		btrfs_abort_transaction(trans, root, ret);
12588732d44fSMiao Xie 		goto fail;
12598732d44fSMiao Xie 	}
12600660b5afSChris Mason 
1261a22285a6SYan, Zheng 	/*
1262a22285a6SYan, Zheng 	 * insert root back/forward references
1263a22285a6SYan, Zheng 	 */
1264a22285a6SYan, Zheng 	ret = btrfs_add_root_ref(trans, tree_root, objectid,
1265a22285a6SYan, Zheng 				 parent_root->root_key.objectid,
126633345d01SLi Zefan 				 btrfs_ino(parent_inode), index,
1267a22285a6SYan, Zheng 				 dentry->d_name.name, dentry->d_name.len);
12688732d44fSMiao Xie 	if (ret) {
12698732d44fSMiao Xie 		btrfs_abort_transaction(trans, root, ret);
12708732d44fSMiao Xie 		goto fail;
12718732d44fSMiao Xie 	}
1272a22285a6SYan, Zheng 
1273a22285a6SYan, Zheng 	key.offset = (u64)-1;
1274a22285a6SYan, Zheng 	pending->snap = btrfs_read_fs_root_no_name(root->fs_info, &key);
127579787eaaSJeff Mahoney 	if (IS_ERR(pending->snap)) {
127679787eaaSJeff Mahoney 		ret = PTR_ERR(pending->snap);
12778732d44fSMiao Xie 		btrfs_abort_transaction(trans, root, ret);
12788732d44fSMiao Xie 		goto fail;
127979787eaaSJeff Mahoney 	}
1280d68fc57bSYan, Zheng 
128149b25e05SJeff Mahoney 	ret = btrfs_reloc_post_snapshot(trans, pending);
12828732d44fSMiao Xie 	if (ret) {
12838732d44fSMiao Xie 		btrfs_abort_transaction(trans, root, ret);
12848732d44fSMiao Xie 		goto fail;
12858732d44fSMiao Xie 	}
1286361048f5SMiao Xie 
1287361048f5SMiao Xie 	ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
12888732d44fSMiao Xie 	if (ret) {
12898732d44fSMiao Xie 		btrfs_abort_transaction(trans, root, ret);
12908732d44fSMiao Xie 		goto fail;
12918732d44fSMiao Xie 	}
129242874b3dSMiao Xie 
129342874b3dSMiao Xie 	ret = btrfs_insert_dir_item(trans, parent_root,
129442874b3dSMiao Xie 				    dentry->d_name.name, dentry->d_name.len,
129542874b3dSMiao Xie 				    parent_inode, &key,
129642874b3dSMiao Xie 				    BTRFS_FT_DIR, index);
129742874b3dSMiao Xie 	/* We have check then name at the beginning, so it is impossible. */
12989c52057cSChris Mason 	BUG_ON(ret == -EEXIST || ret == -EOVERFLOW);
12998732d44fSMiao Xie 	if (ret) {
13008732d44fSMiao Xie 		btrfs_abort_transaction(trans, root, ret);
13018732d44fSMiao Xie 		goto fail;
13028732d44fSMiao Xie 	}
130342874b3dSMiao Xie 
130442874b3dSMiao Xie 	btrfs_i_size_write(parent_inode, parent_inode->i_size +
130542874b3dSMiao Xie 					 dentry->d_name.len * 2);
130642874b3dSMiao Xie 	parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
1307be6aef60SJosef Bacik 	ret = btrfs_update_inode_fallback(trans, parent_root, parent_inode);
130842874b3dSMiao Xie 	if (ret)
13098732d44fSMiao Xie 		btrfs_abort_transaction(trans, root, ret);
13103063d29fSChris Mason fail:
1311aec8030aSMiao Xie 	pending->error = ret;
1312aec8030aSMiao Xie dir_item_existed:
131398c9942aSLiu Bo 	trans->block_rsv = rsv;
13142382c5ccSLiu Bo 	trans->bytes_reserved = 0;
13156fa9700eSMiao Xie no_free_objectid:
13166fa9700eSMiao Xie 	kfree(new_root_item);
13176fa9700eSMiao Xie root_item_alloc_fail:
131842874b3dSMiao Xie 	btrfs_free_path(path);
131949b25e05SJeff Mahoney 	return ret;
13203063d29fSChris Mason }
13213063d29fSChris Mason 
1322d352ac68SChris Mason /*
1323d352ac68SChris Mason  * create all the snapshots we've scheduled for creation
1324d352ac68SChris Mason  */
132580b6794dSChris Mason static noinline int create_pending_snapshots(struct btrfs_trans_handle *trans,
13263063d29fSChris Mason 					     struct btrfs_fs_info *fs_info)
13273063d29fSChris Mason {
1328aec8030aSMiao Xie 	struct btrfs_pending_snapshot *pending, *next;
13293063d29fSChris Mason 	struct list_head *head = &trans->transaction->pending_snapshots;
1330aec8030aSMiao Xie 	int ret = 0;
13313de4586cSChris Mason 
1332aec8030aSMiao Xie 	list_for_each_entry_safe(pending, next, head, list) {
1333aec8030aSMiao Xie 		list_del(&pending->list);
1334aec8030aSMiao Xie 		ret = create_pending_snapshot(trans, fs_info, pending);
1335aec8030aSMiao Xie 		if (ret)
1336aec8030aSMiao Xie 			break;
1337aec8030aSMiao Xie 	}
1338aec8030aSMiao Xie 	return ret;
13393de4586cSChris Mason }
13403de4586cSChris Mason 
13415d4f98a2SYan Zheng static void update_super_roots(struct btrfs_root *root)
13425d4f98a2SYan Zheng {
13435d4f98a2SYan Zheng 	struct btrfs_root_item *root_item;
13445d4f98a2SYan Zheng 	struct btrfs_super_block *super;
13455d4f98a2SYan Zheng 
13466c41761fSDavid Sterba 	super = root->fs_info->super_copy;
13475d4f98a2SYan Zheng 
13485d4f98a2SYan Zheng 	root_item = &root->fs_info->chunk_root->root_item;
13495d4f98a2SYan Zheng 	super->chunk_root = root_item->bytenr;
13505d4f98a2SYan Zheng 	super->chunk_root_generation = root_item->generation;
13515d4f98a2SYan Zheng 	super->chunk_root_level = root_item->level;
13525d4f98a2SYan Zheng 
13535d4f98a2SYan Zheng 	root_item = &root->fs_info->tree_root->root_item;
13545d4f98a2SYan Zheng 	super->root = root_item->bytenr;
13555d4f98a2SYan Zheng 	super->generation = root_item->generation;
13565d4f98a2SYan Zheng 	super->root_level = root_item->level;
135773bc1876SJosef Bacik 	if (btrfs_test_opt(root, SPACE_CACHE))
13580af3d00bSJosef Bacik 		super->cache_generation = root_item->generation;
13595d4f98a2SYan Zheng }
13605d4f98a2SYan Zheng 
1361f36f3042SChris Mason int btrfs_transaction_in_commit(struct btrfs_fs_info *info)
1362f36f3042SChris Mason {
13634a9d8bdeSMiao Xie 	struct btrfs_transaction *trans;
1364f36f3042SChris Mason 	int ret = 0;
13654a9d8bdeSMiao Xie 
1366a4abeea4SJosef Bacik 	spin_lock(&info->trans_lock);
13674a9d8bdeSMiao Xie 	trans = info->running_transaction;
13684a9d8bdeSMiao Xie 	if (trans)
13694a9d8bdeSMiao Xie 		ret = (trans->state >= TRANS_STATE_COMMIT_START);
1370a4abeea4SJosef Bacik 	spin_unlock(&info->trans_lock);
1371f36f3042SChris Mason 	return ret;
1372f36f3042SChris Mason }
1373f36f3042SChris Mason 
13748929ecfaSYan, Zheng int btrfs_transaction_blocked(struct btrfs_fs_info *info)
13758929ecfaSYan, Zheng {
13764a9d8bdeSMiao Xie 	struct btrfs_transaction *trans;
13778929ecfaSYan, Zheng 	int ret = 0;
13784a9d8bdeSMiao Xie 
1379a4abeea4SJosef Bacik 	spin_lock(&info->trans_lock);
13804a9d8bdeSMiao Xie 	trans = info->running_transaction;
13814a9d8bdeSMiao Xie 	if (trans)
13824a9d8bdeSMiao Xie 		ret = is_transaction_blocked(trans);
1383a4abeea4SJosef Bacik 	spin_unlock(&info->trans_lock);
13848929ecfaSYan, Zheng 	return ret;
13858929ecfaSYan, Zheng }
13868929ecfaSYan, Zheng 
1387bb9c12c9SSage Weil /*
1388bb9c12c9SSage Weil  * wait for the current transaction commit to start and block subsequent
1389bb9c12c9SSage Weil  * transaction joins
1390bb9c12c9SSage Weil  */
1391bb9c12c9SSage Weil static void wait_current_trans_commit_start(struct btrfs_root *root,
1392bb9c12c9SSage Weil 					    struct btrfs_transaction *trans)
1393bb9c12c9SSage Weil {
13944a9d8bdeSMiao Xie 	wait_event(root->fs_info->transaction_blocked_wait,
13954a9d8bdeSMiao Xie 		   trans->state >= TRANS_STATE_COMMIT_START);
1396bb9c12c9SSage Weil }
1397bb9c12c9SSage Weil 
1398bb9c12c9SSage Weil /*
1399bb9c12c9SSage Weil  * wait for the current transaction to start and then become unblocked.
1400bb9c12c9SSage Weil  * caller holds ref.
1401bb9c12c9SSage Weil  */
1402bb9c12c9SSage Weil static void wait_current_trans_commit_start_and_unblock(struct btrfs_root *root,
1403bb9c12c9SSage Weil 					 struct btrfs_transaction *trans)
1404bb9c12c9SSage Weil {
140572d63ed6SLi Zefan 	wait_event(root->fs_info->transaction_wait,
14064a9d8bdeSMiao Xie 		   trans->state >= TRANS_STATE_UNBLOCKED);
1407bb9c12c9SSage Weil }
1408bb9c12c9SSage Weil 
1409bb9c12c9SSage Weil /*
1410bb9c12c9SSage Weil  * commit transactions asynchronously. once btrfs_commit_transaction_async
1411bb9c12c9SSage Weil  * returns, any subsequent transaction will not be allowed to join.
1412bb9c12c9SSage Weil  */
1413bb9c12c9SSage Weil struct btrfs_async_commit {
1414bb9c12c9SSage Weil 	struct btrfs_trans_handle *newtrans;
1415bb9c12c9SSage Weil 	struct btrfs_root *root;
14167892b5afSMiao Xie 	struct work_struct work;
1417bb9c12c9SSage Weil };
1418bb9c12c9SSage Weil 
1419bb9c12c9SSage Weil static void do_async_commit(struct work_struct *work)
1420bb9c12c9SSage Weil {
1421bb9c12c9SSage Weil 	struct btrfs_async_commit *ac =
14227892b5afSMiao Xie 		container_of(work, struct btrfs_async_commit, work);
1423bb9c12c9SSage Weil 
14246fc4e354SSage Weil 	/*
14256fc4e354SSage Weil 	 * We've got freeze protection passed with the transaction.
14266fc4e354SSage Weil 	 * Tell lockdep about it.
14276fc4e354SSage Weil 	 */
1428ff7c1d33SMiao Xie 	if (ac->newtrans->type < TRANS_JOIN_NOLOCK)
14296fc4e354SSage Weil 		rwsem_acquire_read(
14306fc4e354SSage Weil 		     &ac->root->fs_info->sb->s_writers.lock_map[SB_FREEZE_FS-1],
14316fc4e354SSage Weil 		     0, 1, _THIS_IP_);
14326fc4e354SSage Weil 
1433e209db7aSSage Weil 	current->journal_info = ac->newtrans;
1434e209db7aSSage Weil 
1435bb9c12c9SSage Weil 	btrfs_commit_transaction(ac->newtrans, ac->root);
1436bb9c12c9SSage Weil 	kfree(ac);
1437bb9c12c9SSage Weil }
1438bb9c12c9SSage Weil 
1439bb9c12c9SSage Weil int btrfs_commit_transaction_async(struct btrfs_trans_handle *trans,
1440bb9c12c9SSage Weil 				   struct btrfs_root *root,
1441bb9c12c9SSage Weil 				   int wait_for_unblock)
1442bb9c12c9SSage Weil {
1443bb9c12c9SSage Weil 	struct btrfs_async_commit *ac;
1444bb9c12c9SSage Weil 	struct btrfs_transaction *cur_trans;
1445bb9c12c9SSage Weil 
1446bb9c12c9SSage Weil 	ac = kmalloc(sizeof(*ac), GFP_NOFS);
1447db5b493aSTsutomu Itoh 	if (!ac)
1448db5b493aSTsutomu Itoh 		return -ENOMEM;
1449bb9c12c9SSage Weil 
14507892b5afSMiao Xie 	INIT_WORK(&ac->work, do_async_commit);
1451bb9c12c9SSage Weil 	ac->root = root;
14527a7eaa40SJosef Bacik 	ac->newtrans = btrfs_join_transaction(root);
14533612b495STsutomu Itoh 	if (IS_ERR(ac->newtrans)) {
14543612b495STsutomu Itoh 		int err = PTR_ERR(ac->newtrans);
14553612b495STsutomu Itoh 		kfree(ac);
14563612b495STsutomu Itoh 		return err;
14573612b495STsutomu Itoh 	}
1458bb9c12c9SSage Weil 
1459bb9c12c9SSage Weil 	/* take transaction reference */
1460bb9c12c9SSage Weil 	cur_trans = trans->transaction;
146113c5a93eSJosef Bacik 	atomic_inc(&cur_trans->use_count);
1462bb9c12c9SSage Weil 
1463bb9c12c9SSage Weil 	btrfs_end_transaction(trans, root);
14646fc4e354SSage Weil 
14656fc4e354SSage Weil 	/*
14666fc4e354SSage Weil 	 * Tell lockdep we've released the freeze rwsem, since the
14676fc4e354SSage Weil 	 * async commit thread will be the one to unlock it.
14686fc4e354SSage Weil 	 */
1469ff7c1d33SMiao Xie 	if (trans->type < TRANS_JOIN_NOLOCK)
1470ff7c1d33SMiao Xie 		rwsem_release(
1471ff7c1d33SMiao Xie 			&root->fs_info->sb->s_writers.lock_map[SB_FREEZE_FS-1],
14726fc4e354SSage Weil 			1, _THIS_IP_);
14736fc4e354SSage Weil 
14747892b5afSMiao Xie 	schedule_work(&ac->work);
1475bb9c12c9SSage Weil 
1476bb9c12c9SSage Weil 	/* wait for transaction to start and unblock */
1477bb9c12c9SSage Weil 	if (wait_for_unblock)
1478bb9c12c9SSage Weil 		wait_current_trans_commit_start_and_unblock(root, cur_trans);
1479bb9c12c9SSage Weil 	else
1480bb9c12c9SSage Weil 		wait_current_trans_commit_start(root, cur_trans);
1481bb9c12c9SSage Weil 
148238e88054SSage Weil 	if (current->journal_info == trans)
148338e88054SSage Weil 		current->journal_info = NULL;
148438e88054SSage Weil 
148538e88054SSage Weil 	put_transaction(cur_trans);
1486bb9c12c9SSage Weil 	return 0;
1487bb9c12c9SSage Weil }
1488bb9c12c9SSage Weil 
148949b25e05SJeff Mahoney 
149049b25e05SJeff Mahoney static void cleanup_transaction(struct btrfs_trans_handle *trans,
14917b8b92afSJosef Bacik 				struct btrfs_root *root, int err)
149249b25e05SJeff Mahoney {
149349b25e05SJeff Mahoney 	struct btrfs_transaction *cur_trans = trans->transaction;
1494f094ac32SLiu Bo 	DEFINE_WAIT(wait);
149549b25e05SJeff Mahoney 
149649b25e05SJeff Mahoney 	WARN_ON(trans->use_count > 1);
149749b25e05SJeff Mahoney 
14987b8b92afSJosef Bacik 	btrfs_abort_transaction(trans, root, err);
14997b8b92afSJosef Bacik 
150049b25e05SJeff Mahoney 	spin_lock(&root->fs_info->trans_lock);
150166b6135bSLiu Bo 
150225d8c284SMiao Xie 	/*
150325d8c284SMiao Xie 	 * If the transaction is removed from the list, it means this
150425d8c284SMiao Xie 	 * transaction has been committed successfully, so it is impossible
150525d8c284SMiao Xie 	 * to call the cleanup function.
150625d8c284SMiao Xie 	 */
150725d8c284SMiao Xie 	BUG_ON(list_empty(&cur_trans->list));
150866b6135bSLiu Bo 
150949b25e05SJeff Mahoney 	list_del_init(&cur_trans->list);
1510d7096fc3SJosef Bacik 	if (cur_trans == root->fs_info->running_transaction) {
15114a9d8bdeSMiao Xie 		cur_trans->state = TRANS_STATE_COMMIT_DOING;
1512f094ac32SLiu Bo 		spin_unlock(&root->fs_info->trans_lock);
1513f094ac32SLiu Bo 		wait_event(cur_trans->writer_wait,
1514f094ac32SLiu Bo 			   atomic_read(&cur_trans->num_writers) == 1);
1515f094ac32SLiu Bo 
1516f094ac32SLiu Bo 		spin_lock(&root->fs_info->trans_lock);
1517d7096fc3SJosef Bacik 	}
151849b25e05SJeff Mahoney 	spin_unlock(&root->fs_info->trans_lock);
151949b25e05SJeff Mahoney 
152049b25e05SJeff Mahoney 	btrfs_cleanup_one_transaction(trans->transaction, root);
152149b25e05SJeff Mahoney 
15224a9d8bdeSMiao Xie 	spin_lock(&root->fs_info->trans_lock);
15234a9d8bdeSMiao Xie 	if (cur_trans == root->fs_info->running_transaction)
15244a9d8bdeSMiao Xie 		root->fs_info->running_transaction = NULL;
15254a9d8bdeSMiao Xie 	spin_unlock(&root->fs_info->trans_lock);
15264a9d8bdeSMiao Xie 
152749b25e05SJeff Mahoney 	put_transaction(cur_trans);
152849b25e05SJeff Mahoney 	put_transaction(cur_trans);
152949b25e05SJeff Mahoney 
153049b25e05SJeff Mahoney 	trace_btrfs_transaction_commit(root);
153149b25e05SJeff Mahoney 
153249b25e05SJeff Mahoney 	btrfs_scrub_continue(root);
153349b25e05SJeff Mahoney 
153449b25e05SJeff Mahoney 	if (current->journal_info == trans)
153549b25e05SJeff Mahoney 		current->journal_info = NULL;
153649b25e05SJeff Mahoney 
153749b25e05SJeff Mahoney 	kmem_cache_free(btrfs_trans_handle_cachep, trans);
153849b25e05SJeff Mahoney }
153949b25e05SJeff Mahoney 
1540ca469637SMiao Xie static int btrfs_flush_all_pending_stuffs(struct btrfs_trans_handle *trans,
1541ca469637SMiao Xie 					  struct btrfs_root *root)
1542ca469637SMiao Xie {
1543ca469637SMiao Xie 	int ret;
1544ca469637SMiao Xie 
1545ca469637SMiao Xie 	ret = btrfs_run_delayed_items(trans, root);
1546ca469637SMiao Xie 	if (ret)
1547ca469637SMiao Xie 		return ret;
1548ca469637SMiao Xie 
1549ca469637SMiao Xie 	/*
1550ca469637SMiao Xie 	 * running the delayed items may have added new refs. account
1551ca469637SMiao Xie 	 * them now so that they hinder processing of more delayed refs
1552ca469637SMiao Xie 	 * as little as possible.
1553ca469637SMiao Xie 	 */
1554ca469637SMiao Xie 	btrfs_delayed_refs_qgroup_accounting(trans, root->fs_info);
1555ca469637SMiao Xie 
1556ca469637SMiao Xie 	/*
1557ca469637SMiao Xie 	 * rename don't use btrfs_join_transaction, so, once we
1558ca469637SMiao Xie 	 * set the transaction to blocked above, we aren't going
1559ca469637SMiao Xie 	 * to get any new ordered operations.  We can safely run
1560ca469637SMiao Xie 	 * it here and no for sure that nothing new will be added
1561ca469637SMiao Xie 	 * to the list
1562ca469637SMiao Xie 	 */
1563569e0f35SJosef Bacik 	ret = btrfs_run_ordered_operations(trans, root, 1);
1564ca469637SMiao Xie 
1565eebc6084SMiao Xie 	return ret;
1566ca469637SMiao Xie }
1567ca469637SMiao Xie 
156882436617SMiao Xie static inline int btrfs_start_delalloc_flush(struct btrfs_fs_info *fs_info)
156982436617SMiao Xie {
157082436617SMiao Xie 	if (btrfs_test_opt(fs_info->tree_root, FLUSHONCOMMIT))
157182436617SMiao Xie 		return btrfs_start_all_delalloc_inodes(fs_info, 1);
157282436617SMiao Xie 	return 0;
157382436617SMiao Xie }
157482436617SMiao Xie 
157582436617SMiao Xie static inline void btrfs_wait_delalloc_flush(struct btrfs_fs_info *fs_info)
157682436617SMiao Xie {
157782436617SMiao Xie 	if (btrfs_test_opt(fs_info->tree_root, FLUSHONCOMMIT))
157882436617SMiao Xie 		btrfs_wait_all_ordered_extents(fs_info, 1);
157982436617SMiao Xie }
158082436617SMiao Xie 
158179154b1bSChris Mason int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
158279154b1bSChris Mason 			     struct btrfs_root *root)
158379154b1bSChris Mason {
158449b25e05SJeff Mahoney 	struct btrfs_transaction *cur_trans = trans->transaction;
15858fd17795SChris Mason 	struct btrfs_transaction *prev_trans = NULL;
158625287e0aSMiao Xie 	int ret;
158779154b1bSChris Mason 
1588569e0f35SJosef Bacik 	ret = btrfs_run_ordered_operations(trans, root, 0);
158925287e0aSMiao Xie 	if (ret) {
159025287e0aSMiao Xie 		btrfs_abort_transaction(trans, root, ret);
1591e4a2bcacSJosef Bacik 		btrfs_end_transaction(trans, root);
1592e4a2bcacSJosef Bacik 		return ret;
159325287e0aSMiao Xie 	}
159425287e0aSMiao Xie 
15958d25a086SMiao Xie 	/* Stop the commit early if ->aborted is set */
15968d25a086SMiao Xie 	if (unlikely(ACCESS_ONCE(cur_trans->aborted))) {
159725287e0aSMiao Xie 		ret = cur_trans->aborted;
1598e4a2bcacSJosef Bacik 		btrfs_end_transaction(trans, root);
1599e4a2bcacSJosef Bacik 		return ret;
160025287e0aSMiao Xie 	}
160149b25e05SJeff Mahoney 
160256bec294SChris Mason 	/* make a pass through all the delayed refs we have so far
160356bec294SChris Mason 	 * any runnings procs may add more while we are here
160456bec294SChris Mason 	 */
160556bec294SChris Mason 	ret = btrfs_run_delayed_refs(trans, root, 0);
1606e4a2bcacSJosef Bacik 	if (ret) {
1607e4a2bcacSJosef Bacik 		btrfs_end_transaction(trans, root);
1608e4a2bcacSJosef Bacik 		return ret;
1609e4a2bcacSJosef Bacik 	}
161056bec294SChris Mason 
16110e721106SJosef Bacik 	btrfs_trans_release_metadata(trans, root);
16120e721106SJosef Bacik 	trans->block_rsv = NULL;
1613272d26d0SMiao Xie 	if (trans->qgroup_reserved) {
1614272d26d0SMiao Xie 		btrfs_qgroup_free(root, trans->qgroup_reserved);
1615272d26d0SMiao Xie 		trans->qgroup_reserved = 0;
1616272d26d0SMiao Xie 	}
16170e721106SJosef Bacik 
1618b7ec40d7SChris Mason 	cur_trans = trans->transaction;
161949b25e05SJeff Mahoney 
162056bec294SChris Mason 	/*
162156bec294SChris Mason 	 * set the flushing flag so procs in this transaction have to
162256bec294SChris Mason 	 * start sending their work down.
162356bec294SChris Mason 	 */
1624b7ec40d7SChris Mason 	cur_trans->delayed_refs.flushing = 1;
162556bec294SChris Mason 
1626ea658badSJosef Bacik 	if (!list_empty(&trans->new_bgs))
1627ea658badSJosef Bacik 		btrfs_create_pending_block_groups(trans, root);
1628ea658badSJosef Bacik 
1629c3e69d58SChris Mason 	ret = btrfs_run_delayed_refs(trans, root, 0);
1630e4a2bcacSJosef Bacik 	if (ret) {
1631e4a2bcacSJosef Bacik 		btrfs_end_transaction(trans, root);
1632e4a2bcacSJosef Bacik 		return ret;
1633e4a2bcacSJosef Bacik 	}
163456bec294SChris Mason 
16354a9d8bdeSMiao Xie 	spin_lock(&root->fs_info->trans_lock);
16364a9d8bdeSMiao Xie 	if (cur_trans->state >= TRANS_STATE_COMMIT_START) {
16374a9d8bdeSMiao Xie 		spin_unlock(&root->fs_info->trans_lock);
163813c5a93eSJosef Bacik 		atomic_inc(&cur_trans->use_count);
163949b25e05SJeff Mahoney 		ret = btrfs_end_transaction(trans, root);
1640ccd467d6SChris Mason 
1641b9c8300cSLi Zefan 		wait_for_commit(root, cur_trans);
164215ee9bc7SJosef Bacik 
164379154b1bSChris Mason 		put_transaction(cur_trans);
164415ee9bc7SJosef Bacik 
164549b25e05SJeff Mahoney 		return ret;
164679154b1bSChris Mason 	}
16474313b399SChris Mason 
16484a9d8bdeSMiao Xie 	cur_trans->state = TRANS_STATE_COMMIT_START;
1649bb9c12c9SSage Weil 	wake_up(&root->fs_info->transaction_blocked_wait);
1650bb9c12c9SSage Weil 
1651ccd467d6SChris Mason 	if (cur_trans->list.prev != &root->fs_info->trans_list) {
1652ccd467d6SChris Mason 		prev_trans = list_entry(cur_trans->list.prev,
1653ccd467d6SChris Mason 					struct btrfs_transaction, list);
16544a9d8bdeSMiao Xie 		if (prev_trans->state != TRANS_STATE_COMPLETED) {
165513c5a93eSJosef Bacik 			atomic_inc(&prev_trans->use_count);
1656a4abeea4SJosef Bacik 			spin_unlock(&root->fs_info->trans_lock);
1657ccd467d6SChris Mason 
1658ccd467d6SChris Mason 			wait_for_commit(root, prev_trans);
1659ccd467d6SChris Mason 
166015ee9bc7SJosef Bacik 			put_transaction(prev_trans);
1661a4abeea4SJosef Bacik 		} else {
1662a4abeea4SJosef Bacik 			spin_unlock(&root->fs_info->trans_lock);
1663ccd467d6SChris Mason 		}
1664a4abeea4SJosef Bacik 	} else {
1665a4abeea4SJosef Bacik 		spin_unlock(&root->fs_info->trans_lock);
1666ccd467d6SChris Mason 	}
166715ee9bc7SJosef Bacik 
16680860adfdSMiao Xie 	extwriter_counter_dec(cur_trans, trans->type);
16690860adfdSMiao Xie 
167082436617SMiao Xie 	ret = btrfs_start_delalloc_flush(root->fs_info);
167182436617SMiao Xie 	if (ret)
167282436617SMiao Xie 		goto cleanup_transaction;
167382436617SMiao Xie 
1674ca469637SMiao Xie 	ret = btrfs_flush_all_pending_stuffs(trans, root);
167549b25e05SJeff Mahoney 	if (ret)
167649b25e05SJeff Mahoney 		goto cleanup_transaction;
167716cdcec7SMiao Xie 
1678581227d0SMiao Xie 	wait_event(cur_trans->writer_wait,
1679581227d0SMiao Xie 		   extwriter_counter_read(cur_trans) == 0);
1680ed3b3d31SChris Mason 
1681581227d0SMiao Xie 	/* some pending stuffs might be added after the previous flush. */
1682ca469637SMiao Xie 	ret = btrfs_flush_all_pending_stuffs(trans, root);
1683ca469637SMiao Xie 	if (ret)
1684ca469637SMiao Xie 		goto cleanup_transaction;
1685ca469637SMiao Xie 
168682436617SMiao Xie 	btrfs_wait_delalloc_flush(root->fs_info);
1687ed0ca140SJosef Bacik 	/*
1688ed0ca140SJosef Bacik 	 * Ok now we need to make sure to block out any other joins while we
1689ed0ca140SJosef Bacik 	 * commit the transaction.  We could have started a join before setting
16904a9d8bdeSMiao Xie 	 * COMMIT_DOING so make sure to wait for num_writers to == 1 again.
1691ed0ca140SJosef Bacik 	 */
1692a4abeea4SJosef Bacik 	spin_lock(&root->fs_info->trans_lock);
16934a9d8bdeSMiao Xie 	cur_trans->state = TRANS_STATE_COMMIT_DOING;
1694a4abeea4SJosef Bacik 	spin_unlock(&root->fs_info->trans_lock);
1695ed0ca140SJosef Bacik 	wait_event(cur_trans->writer_wait,
1696ed0ca140SJosef Bacik 		   atomic_read(&cur_trans->num_writers) == 1);
169715ee9bc7SJosef Bacik 
16982cba30f1SMiao Xie 	/* ->aborted might be set after the previous check, so check it */
16992cba30f1SMiao Xie 	if (unlikely(ACCESS_ONCE(cur_trans->aborted))) {
17002cba30f1SMiao Xie 		ret = cur_trans->aborted;
17012cba30f1SMiao Xie 		goto cleanup_transaction;
17022cba30f1SMiao Xie 	}
17037585717fSChris Mason 	/*
17047585717fSChris Mason 	 * the reloc mutex makes sure that we stop
17057585717fSChris Mason 	 * the balancing code from coming in and moving
17067585717fSChris Mason 	 * extents around in the middle of the commit
17077585717fSChris Mason 	 */
17087585717fSChris Mason 	mutex_lock(&root->fs_info->reloc_mutex);
17097585717fSChris Mason 
171042874b3dSMiao Xie 	/*
171142874b3dSMiao Xie 	 * We needn't worry about the delayed items because we will
171242874b3dSMiao Xie 	 * deal with them in create_pending_snapshot(), which is the
171342874b3dSMiao Xie 	 * core function of the snapshot creation.
171442874b3dSMiao Xie 	 */
171542874b3dSMiao Xie 	ret = create_pending_snapshots(trans, root->fs_info);
171649b25e05SJeff Mahoney 	if (ret) {
171749b25e05SJeff Mahoney 		mutex_unlock(&root->fs_info->reloc_mutex);
171849b25e05SJeff Mahoney 		goto cleanup_transaction;
171949b25e05SJeff Mahoney 	}
17203063d29fSChris Mason 
172142874b3dSMiao Xie 	/*
172242874b3dSMiao Xie 	 * We insert the dir indexes of the snapshots and update the inode
172342874b3dSMiao Xie 	 * of the snapshots' parents after the snapshot creation, so there
172442874b3dSMiao Xie 	 * are some delayed items which are not dealt with. Now deal with
172542874b3dSMiao Xie 	 * them.
172642874b3dSMiao Xie 	 *
172742874b3dSMiao Xie 	 * We needn't worry that this operation will corrupt the snapshots,
172842874b3dSMiao Xie 	 * because all the tree which are snapshoted will be forced to COW
172942874b3dSMiao Xie 	 * the nodes and leaves.
173042874b3dSMiao Xie 	 */
173142874b3dSMiao Xie 	ret = btrfs_run_delayed_items(trans, root);
173249b25e05SJeff Mahoney 	if (ret) {
173349b25e05SJeff Mahoney 		mutex_unlock(&root->fs_info->reloc_mutex);
173449b25e05SJeff Mahoney 		goto cleanup_transaction;
173549b25e05SJeff Mahoney 	}
173616cdcec7SMiao Xie 
173756bec294SChris Mason 	ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
173849b25e05SJeff Mahoney 	if (ret) {
173949b25e05SJeff Mahoney 		mutex_unlock(&root->fs_info->reloc_mutex);
174049b25e05SJeff Mahoney 		goto cleanup_transaction;
174149b25e05SJeff Mahoney 	}
174256bec294SChris Mason 
1743e999376fSChris Mason 	/*
1744e999376fSChris Mason 	 * make sure none of the code above managed to slip in a
1745e999376fSChris Mason 	 * delayed item
1746e999376fSChris Mason 	 */
1747e999376fSChris Mason 	btrfs_assert_delayed_root_empty(root);
1748e999376fSChris Mason 
17492c90e5d6SChris Mason 	WARN_ON(cur_trans != trans->transaction);
1750dc17ff8fSChris Mason 
1751a2de733cSArne Jansen 	btrfs_scrub_pause(root);
1752e02119d5SChris Mason 	/* btrfs_commit_tree_roots is responsible for getting the
1753e02119d5SChris Mason 	 * various roots consistent with each other.  Every pointer
1754e02119d5SChris Mason 	 * in the tree of tree roots has to point to the most up to date
1755e02119d5SChris Mason 	 * root for every subvolume and other tree.  So, we have to keep
1756e02119d5SChris Mason 	 * the tree logging code from jumping in and changing any
1757e02119d5SChris Mason 	 * of the trees.
1758e02119d5SChris Mason 	 *
1759e02119d5SChris Mason 	 * At this point in the commit, there can't be any tree-log
1760e02119d5SChris Mason 	 * writers, but a little lower down we drop the trans mutex
1761e02119d5SChris Mason 	 * and let new people in.  By holding the tree_log_mutex
1762e02119d5SChris Mason 	 * from now until after the super is written, we avoid races
1763e02119d5SChris Mason 	 * with the tree-log code.
1764e02119d5SChris Mason 	 */
1765e02119d5SChris Mason 	mutex_lock(&root->fs_info->tree_log_mutex);
17661a40e23bSZheng Yan 
17675d4f98a2SYan Zheng 	ret = commit_fs_roots(trans, root);
176849b25e05SJeff Mahoney 	if (ret) {
176949b25e05SJeff Mahoney 		mutex_unlock(&root->fs_info->tree_log_mutex);
1770871383beSDavid Sterba 		mutex_unlock(&root->fs_info->reloc_mutex);
177149b25e05SJeff Mahoney 		goto cleanup_transaction;
177249b25e05SJeff Mahoney 	}
177354aa1f4dSChris Mason 
17745d4f98a2SYan Zheng 	/* commit_fs_roots gets rid of all the tree log roots, it is now
1775e02119d5SChris Mason 	 * safe to free the root of tree log roots
1776e02119d5SChris Mason 	 */
1777e02119d5SChris Mason 	btrfs_free_log_root_tree(trans, root->fs_info);
1778e02119d5SChris Mason 
17795d4f98a2SYan Zheng 	ret = commit_cowonly_roots(trans, root);
178049b25e05SJeff Mahoney 	if (ret) {
178149b25e05SJeff Mahoney 		mutex_unlock(&root->fs_info->tree_log_mutex);
1782871383beSDavid Sterba 		mutex_unlock(&root->fs_info->reloc_mutex);
178349b25e05SJeff Mahoney 		goto cleanup_transaction;
178449b25e05SJeff Mahoney 	}
178554aa1f4dSChris Mason 
17862cba30f1SMiao Xie 	/*
17872cba30f1SMiao Xie 	 * The tasks which save the space cache and inode cache may also
17882cba30f1SMiao Xie 	 * update ->aborted, check it.
17892cba30f1SMiao Xie 	 */
17902cba30f1SMiao Xie 	if (unlikely(ACCESS_ONCE(cur_trans->aborted))) {
17912cba30f1SMiao Xie 		ret = cur_trans->aborted;
17922cba30f1SMiao Xie 		mutex_unlock(&root->fs_info->tree_log_mutex);
17932cba30f1SMiao Xie 		mutex_unlock(&root->fs_info->reloc_mutex);
17942cba30f1SMiao Xie 		goto cleanup_transaction;
17952cba30f1SMiao Xie 	}
17962cba30f1SMiao Xie 
179711833d66SYan Zheng 	btrfs_prepare_extent_commit(trans, root);
179811833d66SYan Zheng 
179978fae27eSChris Mason 	cur_trans = root->fs_info->running_transaction;
18005f39d397SChris Mason 
18015d4f98a2SYan Zheng 	btrfs_set_root_node(&root->fs_info->tree_root->root_item,
18025d4f98a2SYan Zheng 			    root->fs_info->tree_root->node);
1803817d52f8SJosef Bacik 	switch_commit_root(root->fs_info->tree_root);
18045d4f98a2SYan Zheng 
18055d4f98a2SYan Zheng 	btrfs_set_root_node(&root->fs_info->chunk_root->root_item,
18065d4f98a2SYan Zheng 			    root->fs_info->chunk_root->node);
1807817d52f8SJosef Bacik 	switch_commit_root(root->fs_info->chunk_root);
18085d4f98a2SYan Zheng 
1809edf39272SJan Schmidt 	assert_qgroups_uptodate(trans);
18105d4f98a2SYan Zheng 	update_super_roots(root);
1811e02119d5SChris Mason 
1812e02119d5SChris Mason 	if (!root->fs_info->log_root_recovering) {
18136c41761fSDavid Sterba 		btrfs_set_super_log_root(root->fs_info->super_copy, 0);
18146c41761fSDavid Sterba 		btrfs_set_super_log_root_level(root->fs_info->super_copy, 0);
1815e02119d5SChris Mason 	}
1816e02119d5SChris Mason 
18176c41761fSDavid Sterba 	memcpy(root->fs_info->super_for_commit, root->fs_info->super_copy,
18186c41761fSDavid Sterba 	       sizeof(*root->fs_info->super_copy));
1819ccd467d6SChris Mason 
1820a4abeea4SJosef Bacik 	spin_lock(&root->fs_info->trans_lock);
18214a9d8bdeSMiao Xie 	cur_trans->state = TRANS_STATE_UNBLOCKED;
1822a4abeea4SJosef Bacik 	root->fs_info->running_transaction = NULL;
1823a4abeea4SJosef Bacik 	spin_unlock(&root->fs_info->trans_lock);
18247585717fSChris Mason 	mutex_unlock(&root->fs_info->reloc_mutex);
1825b7ec40d7SChris Mason 
1826f9295749SChris Mason 	wake_up(&root->fs_info->transaction_wait);
1827e6dcd2dcSChris Mason 
182879154b1bSChris Mason 	ret = btrfs_write_and_wait_transaction(trans, root);
182949b25e05SJeff Mahoney 	if (ret) {
183049b25e05SJeff Mahoney 		btrfs_error(root->fs_info, ret,
183108748810SDavid Sterba 			    "Error while writing out transaction");
183249b25e05SJeff Mahoney 		mutex_unlock(&root->fs_info->tree_log_mutex);
183349b25e05SJeff Mahoney 		goto cleanup_transaction;
183449b25e05SJeff Mahoney 	}
183549b25e05SJeff Mahoney 
183649b25e05SJeff Mahoney 	ret = write_ctree_super(trans, root, 0);
183749b25e05SJeff Mahoney 	if (ret) {
183849b25e05SJeff Mahoney 		mutex_unlock(&root->fs_info->tree_log_mutex);
183949b25e05SJeff Mahoney 		goto cleanup_transaction;
184049b25e05SJeff Mahoney 	}
18414313b399SChris Mason 
1842e02119d5SChris Mason 	/*
1843e02119d5SChris Mason 	 * the super is written, we can safely allow the tree-loggers
1844e02119d5SChris Mason 	 * to go about their business
1845e02119d5SChris Mason 	 */
1846e02119d5SChris Mason 	mutex_unlock(&root->fs_info->tree_log_mutex);
1847e02119d5SChris Mason 
184811833d66SYan Zheng 	btrfs_finish_extent_commit(trans, root);
18494313b399SChris Mason 
185015ee9bc7SJosef Bacik 	root->fs_info->last_trans_committed = cur_trans->transid;
18514a9d8bdeSMiao Xie 	/*
18524a9d8bdeSMiao Xie 	 * We needn't acquire the lock here because there is no other task
18534a9d8bdeSMiao Xie 	 * which can change it.
18544a9d8bdeSMiao Xie 	 */
18554a9d8bdeSMiao Xie 	cur_trans->state = TRANS_STATE_COMPLETED;
18562c90e5d6SChris Mason 	wake_up(&cur_trans->commit_wait);
18573de4586cSChris Mason 
1858a4abeea4SJosef Bacik 	spin_lock(&root->fs_info->trans_lock);
185913c5a93eSJosef Bacik 	list_del_init(&cur_trans->list);
1860a4abeea4SJosef Bacik 	spin_unlock(&root->fs_info->trans_lock);
1861a4abeea4SJosef Bacik 
186279154b1bSChris Mason 	put_transaction(cur_trans);
186378fae27eSChris Mason 	put_transaction(cur_trans);
186458176a96SJosef Bacik 
18650860adfdSMiao Xie 	if (trans->type & __TRANS_FREEZABLE)
1866b2b5ef5cSJan Kara 		sb_end_intwrite(root->fs_info->sb);
1867b2b5ef5cSJan Kara 
18681abe9b8aSliubo 	trace_btrfs_transaction_commit(root);
18691abe9b8aSliubo 
1870a2de733cSArne Jansen 	btrfs_scrub_continue(root);
1871a2de733cSArne Jansen 
18729ed74f2dSJosef Bacik 	if (current->journal_info == trans)
18739ed74f2dSJosef Bacik 		current->journal_info = NULL;
18749ed74f2dSJosef Bacik 
18752c90e5d6SChris Mason 	kmem_cache_free(btrfs_trans_handle_cachep, trans);
187624bbcf04SYan, Zheng 
187724bbcf04SYan, Zheng 	if (current != root->fs_info->transaction_kthread)
187824bbcf04SYan, Zheng 		btrfs_run_delayed_iputs(root);
187924bbcf04SYan, Zheng 
188079154b1bSChris Mason 	return ret;
188149b25e05SJeff Mahoney 
188249b25e05SJeff Mahoney cleanup_transaction:
18830e721106SJosef Bacik 	btrfs_trans_release_metadata(trans, root);
18840e721106SJosef Bacik 	trans->block_rsv = NULL;
1885272d26d0SMiao Xie 	if (trans->qgroup_reserved) {
1886272d26d0SMiao Xie 		btrfs_qgroup_free(root, trans->qgroup_reserved);
1887272d26d0SMiao Xie 		trans->qgroup_reserved = 0;
1888272d26d0SMiao Xie 	}
1889c2cf52ebSSimon Kirby 	btrfs_warn(root->fs_info, "Skipping commit of aborted transaction.");
189049b25e05SJeff Mahoney 	if (current->journal_info == trans)
189149b25e05SJeff Mahoney 		current->journal_info = NULL;
18927b8b92afSJosef Bacik 	cleanup_transaction(trans, root, ret);
189349b25e05SJeff Mahoney 
189449b25e05SJeff Mahoney 	return ret;
189579154b1bSChris Mason }
189679154b1bSChris Mason 
1897d352ac68SChris Mason /*
18989d1a2a3aSDavid Sterba  * return < 0 if error
18999d1a2a3aSDavid Sterba  * 0 if there are no more dead_roots at the time of call
19009d1a2a3aSDavid Sterba  * 1 there are more to be processed, call me again
19019d1a2a3aSDavid Sterba  *
19029d1a2a3aSDavid Sterba  * The return value indicates there are certainly more snapshots to delete, but
19039d1a2a3aSDavid Sterba  * if there comes a new one during processing, it may return 0. We don't mind,
19049d1a2a3aSDavid Sterba  * because btrfs_commit_super will poke cleaner thread and it will process it a
19059d1a2a3aSDavid Sterba  * few seconds later.
1906d352ac68SChris Mason  */
19079d1a2a3aSDavid Sterba int btrfs_clean_one_deleted_snapshot(struct btrfs_root *root)
1908e9d0b13bSChris Mason {
19099d1a2a3aSDavid Sterba 	int ret;
19105d4f98a2SYan Zheng 	struct btrfs_fs_info *fs_info = root->fs_info;
1911e9d0b13bSChris Mason 
1912a4abeea4SJosef Bacik 	spin_lock(&fs_info->trans_lock);
19139d1a2a3aSDavid Sterba 	if (list_empty(&fs_info->dead_roots)) {
19149d1a2a3aSDavid Sterba 		spin_unlock(&fs_info->trans_lock);
19159d1a2a3aSDavid Sterba 		return 0;
19169d1a2a3aSDavid Sterba 	}
19179d1a2a3aSDavid Sterba 	root = list_first_entry(&fs_info->dead_roots,
19189d1a2a3aSDavid Sterba 			struct btrfs_root, root_list);
19199d1a2a3aSDavid Sterba 	list_del(&root->root_list);
1920a4abeea4SJosef Bacik 	spin_unlock(&fs_info->trans_lock);
19215d4f98a2SYan Zheng 
19229d1a2a3aSDavid Sterba 	pr_debug("btrfs: cleaner removing %llu\n",
19239d1a2a3aSDavid Sterba 			(unsigned long long)root->objectid);
192476dda93cSYan, Zheng 
192516cdcec7SMiao Xie 	btrfs_kill_all_delayed_nodes(root);
192616cdcec7SMiao Xie 
192776dda93cSYan, Zheng 	if (btrfs_header_backref_rev(root->node) <
192876dda93cSYan, Zheng 			BTRFS_MIXED_BACKREF_REV)
19292c536799SJeff Mahoney 		ret = btrfs_drop_snapshot(root, NULL, 0, 0);
193076dda93cSYan, Zheng 	else
19312c536799SJeff Mahoney 		ret = btrfs_drop_snapshot(root, NULL, 1, 0);
19329d1a2a3aSDavid Sterba 	/*
19339d1a2a3aSDavid Sterba 	 * If we encounter a transaction abort during snapshot cleaning, we
19349d1a2a3aSDavid Sterba 	 * don't want to crash here
19359d1a2a3aSDavid Sterba 	 */
19369d1a2a3aSDavid Sterba 	BUG_ON(ret < 0 && ret != -EAGAIN && ret != -EROFS);
19379d1a2a3aSDavid Sterba 	return 1;
1938e9d0b13bSChris Mason }
1939