xref: /openbmc/linux/fs/btrfs/transaction.c (revision 581227d0)
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 
3748a3b636SEric Sandeen static void put_transaction(struct btrfs_transaction *transaction)
3879154b1bSChris Mason {
3913c5a93eSJosef Bacik 	WARN_ON(atomic_read(&transaction->use_count) == 0);
4013c5a93eSJosef Bacik 	if (atomic_dec_and_test(&transaction->use_count)) {
41a4abeea4SJosef Bacik 		BUG_ON(!list_empty(&transaction->list));
4200f04b88SArne Jansen 		WARN_ON(transaction->delayed_refs.root.rb_node);
432c90e5d6SChris Mason 		kmem_cache_free(btrfs_transaction_cachep, transaction);
4479154b1bSChris Mason 	}
4578fae27eSChris Mason }
4679154b1bSChris Mason 
47817d52f8SJosef Bacik static noinline void switch_commit_root(struct btrfs_root *root)
48817d52f8SJosef Bacik {
49817d52f8SJosef Bacik 	free_extent_buffer(root->commit_root);
50817d52f8SJosef Bacik 	root->commit_root = btrfs_root_node(root);
51817d52f8SJosef Bacik }
52817d52f8SJosef Bacik 
53178260b2SMiao Xie static inline int can_join_transaction(struct btrfs_transaction *trans,
540860adfdSMiao Xie 				       unsigned int type)
55178260b2SMiao Xie {
56178260b2SMiao Xie 	return !(trans->in_commit &&
570860adfdSMiao Xie 		 (type & TRANS_EXTWRITERS));
580860adfdSMiao Xie }
590860adfdSMiao Xie 
600860adfdSMiao Xie static inline void extwriter_counter_inc(struct btrfs_transaction *trans,
610860adfdSMiao Xie 					 unsigned int type)
620860adfdSMiao Xie {
630860adfdSMiao Xie 	if (type & TRANS_EXTWRITERS)
640860adfdSMiao Xie 		atomic_inc(&trans->num_extwriters);
650860adfdSMiao Xie }
660860adfdSMiao Xie 
670860adfdSMiao Xie static inline void extwriter_counter_dec(struct btrfs_transaction *trans,
680860adfdSMiao Xie 					 unsigned int type)
690860adfdSMiao Xie {
700860adfdSMiao Xie 	if (type & TRANS_EXTWRITERS)
710860adfdSMiao Xie 		atomic_dec(&trans->num_extwriters);
720860adfdSMiao Xie }
730860adfdSMiao Xie 
740860adfdSMiao Xie static inline void extwriter_counter_init(struct btrfs_transaction *trans,
750860adfdSMiao Xie 					  unsigned int type)
760860adfdSMiao Xie {
770860adfdSMiao Xie 	atomic_set(&trans->num_extwriters, ((type & TRANS_EXTWRITERS) ? 1 : 0));
780860adfdSMiao Xie }
790860adfdSMiao Xie 
800860adfdSMiao Xie static inline int extwriter_counter_read(struct btrfs_transaction *trans)
810860adfdSMiao Xie {
820860adfdSMiao Xie 	return atomic_read(&trans->num_extwriters);
83178260b2SMiao Xie }
84178260b2SMiao Xie 
85d352ac68SChris Mason /*
86d352ac68SChris Mason  * either allocate a new transaction or hop into the existing one
87d352ac68SChris Mason  */
880860adfdSMiao Xie static noinline int join_transaction(struct btrfs_root *root, unsigned int type)
8979154b1bSChris Mason {
9079154b1bSChris Mason 	struct btrfs_transaction *cur_trans;
9119ae4e81SJan Schmidt 	struct btrfs_fs_info *fs_info = root->fs_info;
92a4abeea4SJosef Bacik 
9319ae4e81SJan Schmidt 	spin_lock(&fs_info->trans_lock);
94d43317dcSChris Mason loop:
9549b25e05SJeff Mahoney 	/* The file system has been taken offline. No new transactions. */
9687533c47SMiao Xie 	if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) {
9719ae4e81SJan Schmidt 		spin_unlock(&fs_info->trans_lock);
9849b25e05SJeff Mahoney 		return -EROFS;
9949b25e05SJeff Mahoney 	}
10049b25e05SJeff Mahoney 
10119ae4e81SJan Schmidt 	if (fs_info->trans_no_join) {
102354aa0fbSMiao Xie 		/*
103354aa0fbSMiao Xie 		 * If we are JOIN_NOLOCK we're already committing a current
104354aa0fbSMiao Xie 		 * transaction, we just need a handle to deal with something
105354aa0fbSMiao Xie 		 * when committing the transaction, such as inode cache and
106354aa0fbSMiao Xie 		 * space cache. It is a special case.
107354aa0fbSMiao Xie 		 */
108354aa0fbSMiao Xie 		if (type != TRANS_JOIN_NOLOCK) {
10919ae4e81SJan Schmidt 			spin_unlock(&fs_info->trans_lock);
110a4abeea4SJosef Bacik 			return -EBUSY;
111a4abeea4SJosef Bacik 		}
112a4abeea4SJosef Bacik 	}
113a4abeea4SJosef Bacik 
11419ae4e81SJan Schmidt 	cur_trans = fs_info->running_transaction;
115a4abeea4SJosef Bacik 	if (cur_trans) {
116871383beSDavid Sterba 		if (cur_trans->aborted) {
11719ae4e81SJan Schmidt 			spin_unlock(&fs_info->trans_lock);
11849b25e05SJeff Mahoney 			return cur_trans->aborted;
119871383beSDavid Sterba 		}
120178260b2SMiao Xie 		if (!can_join_transaction(cur_trans, type)) {
121178260b2SMiao Xie 			spin_unlock(&fs_info->trans_lock);
122178260b2SMiao Xie 			return -EBUSY;
123178260b2SMiao Xie 		}
124a4abeea4SJosef Bacik 		atomic_inc(&cur_trans->use_count);
125a4abeea4SJosef Bacik 		atomic_inc(&cur_trans->num_writers);
1260860adfdSMiao Xie 		extwriter_counter_inc(cur_trans, type);
12719ae4e81SJan Schmidt 		spin_unlock(&fs_info->trans_lock);
128a4abeea4SJosef Bacik 		return 0;
129a4abeea4SJosef Bacik 	}
13019ae4e81SJan Schmidt 	spin_unlock(&fs_info->trans_lock);
131a4abeea4SJosef Bacik 
132354aa0fbSMiao Xie 	/*
133354aa0fbSMiao Xie 	 * If we are ATTACH, we just want to catch the current transaction,
134354aa0fbSMiao Xie 	 * and commit it. If there is no transaction, just return ENOENT.
135354aa0fbSMiao Xie 	 */
136354aa0fbSMiao Xie 	if (type == TRANS_ATTACH)
137354aa0fbSMiao Xie 		return -ENOENT;
138354aa0fbSMiao Xie 
139a4abeea4SJosef Bacik 	cur_trans = kmem_cache_alloc(btrfs_transaction_cachep, GFP_NOFS);
140db5b493aSTsutomu Itoh 	if (!cur_trans)
141db5b493aSTsutomu Itoh 		return -ENOMEM;
142d43317dcSChris Mason 
14319ae4e81SJan Schmidt 	spin_lock(&fs_info->trans_lock);
14419ae4e81SJan Schmidt 	if (fs_info->running_transaction) {
145d43317dcSChris Mason 		/*
146d43317dcSChris Mason 		 * someone started a transaction after we unlocked.  Make sure
147d43317dcSChris Mason 		 * to redo the trans_no_join checks above
148d43317dcSChris Mason 		 */
149a4abeea4SJosef Bacik 		kmem_cache_free(btrfs_transaction_cachep, cur_trans);
150d43317dcSChris Mason 		goto loop;
15187533c47SMiao Xie 	} else if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) {
152e4b50e14SDan Carpenter 		spin_unlock(&fs_info->trans_lock);
1537b8b92afSJosef Bacik 		kmem_cache_free(btrfs_transaction_cachep, cur_trans);
1547b8b92afSJosef Bacik 		return -EROFS;
155a4abeea4SJosef Bacik 	}
156d43317dcSChris Mason 
15713c5a93eSJosef Bacik 	atomic_set(&cur_trans->num_writers, 1);
1580860adfdSMiao Xie 	extwriter_counter_init(cur_trans, type);
15979154b1bSChris Mason 	init_waitqueue_head(&cur_trans->writer_wait);
16079154b1bSChris Mason 	init_waitqueue_head(&cur_trans->commit_wait);
16179154b1bSChris Mason 	cur_trans->in_commit = 0;
162f9295749SChris Mason 	cur_trans->blocked = 0;
163a4abeea4SJosef Bacik 	/*
164a4abeea4SJosef Bacik 	 * One for this trans handle, one so it will live on until we
165a4abeea4SJosef Bacik 	 * commit the transaction.
166a4abeea4SJosef Bacik 	 */
167a4abeea4SJosef Bacik 	atomic_set(&cur_trans->use_count, 2);
16879154b1bSChris Mason 	cur_trans->commit_done = 0;
16908607c1bSChris Mason 	cur_trans->start_time = get_seconds();
17056bec294SChris Mason 
1716bef4d31SEric Paris 	cur_trans->delayed_refs.root = RB_ROOT;
17256bec294SChris Mason 	cur_trans->delayed_refs.num_entries = 0;
173c3e69d58SChris Mason 	cur_trans->delayed_refs.num_heads_ready = 0;
174c3e69d58SChris Mason 	cur_trans->delayed_refs.num_heads = 0;
17556bec294SChris Mason 	cur_trans->delayed_refs.flushing = 0;
176c3e69d58SChris Mason 	cur_trans->delayed_refs.run_delayed_start = 0;
17720b297d6SJan Schmidt 
17820b297d6SJan Schmidt 	/*
17920b297d6SJan Schmidt 	 * although the tree mod log is per file system and not per transaction,
18020b297d6SJan Schmidt 	 * the log must never go across transaction boundaries.
18120b297d6SJan Schmidt 	 */
18220b297d6SJan Schmidt 	smp_mb();
18331b1a2bdSJulia Lawall 	if (!list_empty(&fs_info->tree_mod_seq_list))
18431b1a2bdSJulia Lawall 		WARN(1, KERN_ERR "btrfs: tree_mod_seq_list not empty when "
18520b297d6SJan Schmidt 			"creating a fresh transaction\n");
18631b1a2bdSJulia Lawall 	if (!RB_EMPTY_ROOT(&fs_info->tree_mod_log))
18731b1a2bdSJulia Lawall 		WARN(1, KERN_ERR "btrfs: tree_mod_log rb tree not empty when "
18820b297d6SJan Schmidt 			"creating a fresh transaction\n");
189fc36ed7eSJan Schmidt 	atomic64_set(&fs_info->tree_mod_seq, 0);
19020b297d6SJan Schmidt 
191a4abeea4SJosef Bacik 	spin_lock_init(&cur_trans->commit_lock);
19256bec294SChris Mason 	spin_lock_init(&cur_trans->delayed_refs.lock);
193bb721703SChris Mason 	atomic_set(&cur_trans->delayed_refs.procs_running_refs, 0);
194bb721703SChris Mason 	atomic_set(&cur_trans->delayed_refs.ref_seq, 0);
195bb721703SChris Mason 	init_waitqueue_head(&cur_trans->delayed_refs.wait);
19656bec294SChris Mason 
1973063d29fSChris Mason 	INIT_LIST_HEAD(&cur_trans->pending_snapshots);
198569e0f35SJosef Bacik 	INIT_LIST_HEAD(&cur_trans->ordered_operations);
19919ae4e81SJan Schmidt 	list_add_tail(&cur_trans->list, &fs_info->trans_list);
200d1310b2eSChris Mason 	extent_io_tree_init(&cur_trans->dirty_pages,
20119ae4e81SJan Schmidt 			     fs_info->btree_inode->i_mapping);
20219ae4e81SJan Schmidt 	fs_info->generation++;
20319ae4e81SJan Schmidt 	cur_trans->transid = fs_info->generation;
20419ae4e81SJan Schmidt 	fs_info->running_transaction = cur_trans;
20549b25e05SJeff Mahoney 	cur_trans->aborted = 0;
20619ae4e81SJan Schmidt 	spin_unlock(&fs_info->trans_lock);
20715ee9bc7SJosef Bacik 
20879154b1bSChris Mason 	return 0;
20979154b1bSChris Mason }
21079154b1bSChris Mason 
211d352ac68SChris Mason /*
212d397712bSChris Mason  * this does all the record keeping required to make sure that a reference
213d397712bSChris Mason  * counted root is properly recorded in a given transaction.  This is required
214d397712bSChris Mason  * to make sure the old root from before we joined the transaction is deleted
215d397712bSChris Mason  * when the transaction commits
216d352ac68SChris Mason  */
2177585717fSChris Mason static int record_root_in_trans(struct btrfs_trans_handle *trans,
2185d4f98a2SYan Zheng 			       struct btrfs_root *root)
2196702ed49SChris Mason {
2205d4f98a2SYan Zheng 	if (root->ref_cows && root->last_trans < trans->transid) {
2216702ed49SChris Mason 		WARN_ON(root == root->fs_info->extent_root);
2225d4f98a2SYan Zheng 		WARN_ON(root->commit_root != root->node);
2235d4f98a2SYan Zheng 
2247585717fSChris Mason 		/*
2257585717fSChris Mason 		 * see below for in_trans_setup usage rules
2267585717fSChris Mason 		 * we have the reloc mutex held now, so there
2277585717fSChris Mason 		 * is only one writer in this function
2287585717fSChris Mason 		 */
2297585717fSChris Mason 		root->in_trans_setup = 1;
2307585717fSChris Mason 
2317585717fSChris Mason 		/* make sure readers find in_trans_setup before
2327585717fSChris Mason 		 * they find our root->last_trans update
2337585717fSChris Mason 		 */
2347585717fSChris Mason 		smp_wmb();
2357585717fSChris Mason 
236a4abeea4SJosef Bacik 		spin_lock(&root->fs_info->fs_roots_radix_lock);
237a4abeea4SJosef Bacik 		if (root->last_trans == trans->transid) {
238a4abeea4SJosef Bacik 			spin_unlock(&root->fs_info->fs_roots_radix_lock);
239a4abeea4SJosef Bacik 			return 0;
240a4abeea4SJosef Bacik 		}
2416702ed49SChris Mason 		radix_tree_tag_set(&root->fs_info->fs_roots_radix,
2426702ed49SChris Mason 			   (unsigned long)root->root_key.objectid,
2436702ed49SChris Mason 			   BTRFS_ROOT_TRANS_TAG);
244a4abeea4SJosef Bacik 		spin_unlock(&root->fs_info->fs_roots_radix_lock);
2457585717fSChris Mason 		root->last_trans = trans->transid;
2467585717fSChris Mason 
2477585717fSChris Mason 		/* this is pretty tricky.  We don't want to
2487585717fSChris Mason 		 * take the relocation lock in btrfs_record_root_in_trans
2497585717fSChris Mason 		 * unless we're really doing the first setup for this root in
2507585717fSChris Mason 		 * this transaction.
2517585717fSChris Mason 		 *
2527585717fSChris Mason 		 * Normally we'd use root->last_trans as a flag to decide
2537585717fSChris Mason 		 * if we want to take the expensive mutex.
2547585717fSChris Mason 		 *
2557585717fSChris Mason 		 * But, we have to set root->last_trans before we
2567585717fSChris Mason 		 * init the relocation root, otherwise, we trip over warnings
2577585717fSChris Mason 		 * in ctree.c.  The solution used here is to flag ourselves
2587585717fSChris Mason 		 * with root->in_trans_setup.  When this is 1, we're still
2597585717fSChris Mason 		 * fixing up the reloc trees and everyone must wait.
2607585717fSChris Mason 		 *
2617585717fSChris Mason 		 * When this is zero, they can trust root->last_trans and fly
2627585717fSChris Mason 		 * through btrfs_record_root_in_trans without having to take the
2637585717fSChris Mason 		 * lock.  smp_wmb() makes sure that all the writes above are
2647585717fSChris Mason 		 * done before we pop in the zero below
2657585717fSChris Mason 		 */
2665d4f98a2SYan Zheng 		btrfs_init_reloc_root(trans, root);
2677585717fSChris Mason 		smp_wmb();
2687585717fSChris Mason 		root->in_trans_setup = 0;
2696702ed49SChris Mason 	}
2705d4f98a2SYan Zheng 	return 0;
2716702ed49SChris Mason }
2725d4f98a2SYan Zheng 
2737585717fSChris Mason 
2747585717fSChris Mason int btrfs_record_root_in_trans(struct btrfs_trans_handle *trans,
2757585717fSChris Mason 			       struct btrfs_root *root)
2767585717fSChris Mason {
2777585717fSChris Mason 	if (!root->ref_cows)
2787585717fSChris Mason 		return 0;
2797585717fSChris Mason 
2807585717fSChris Mason 	/*
2817585717fSChris Mason 	 * see record_root_in_trans for comments about in_trans_setup usage
2827585717fSChris Mason 	 * and barriers
2837585717fSChris Mason 	 */
2847585717fSChris Mason 	smp_rmb();
2857585717fSChris Mason 	if (root->last_trans == trans->transid &&
2867585717fSChris Mason 	    !root->in_trans_setup)
2877585717fSChris Mason 		return 0;
2887585717fSChris Mason 
2897585717fSChris Mason 	mutex_lock(&root->fs_info->reloc_mutex);
2907585717fSChris Mason 	record_root_in_trans(trans, root);
2917585717fSChris Mason 	mutex_unlock(&root->fs_info->reloc_mutex);
2927585717fSChris Mason 
2937585717fSChris Mason 	return 0;
2947585717fSChris Mason }
2957585717fSChris Mason 
296d352ac68SChris Mason /* wait for commit against the current transaction to become unblocked
297d352ac68SChris Mason  * when this is done, it is safe to start a new transaction, but the current
298d352ac68SChris Mason  * transaction might not be fully on disk.
299d352ac68SChris Mason  */
30037d1aeeeSChris Mason static void wait_current_trans(struct btrfs_root *root)
30179154b1bSChris Mason {
302f9295749SChris Mason 	struct btrfs_transaction *cur_trans;
30379154b1bSChris Mason 
304a4abeea4SJosef Bacik 	spin_lock(&root->fs_info->trans_lock);
305f9295749SChris Mason 	cur_trans = root->fs_info->running_transaction;
30637d1aeeeSChris Mason 	if (cur_trans && cur_trans->blocked) {
30713c5a93eSJosef Bacik 		atomic_inc(&cur_trans->use_count);
308a4abeea4SJosef Bacik 		spin_unlock(&root->fs_info->trans_lock);
30972d63ed6SLi Zefan 
31072d63ed6SLi Zefan 		wait_event(root->fs_info->transaction_wait,
31172d63ed6SLi Zefan 			   !cur_trans->blocked);
312f9295749SChris Mason 		put_transaction(cur_trans);
313a4abeea4SJosef Bacik 	} else {
314a4abeea4SJosef Bacik 		spin_unlock(&root->fs_info->trans_lock);
315f9295749SChris Mason 	}
31637d1aeeeSChris Mason }
31737d1aeeeSChris Mason 
318a22285a6SYan, Zheng static int may_wait_transaction(struct btrfs_root *root, int type)
31937d1aeeeSChris Mason {
320a4abeea4SJosef Bacik 	if (root->fs_info->log_root_recovering)
321a4abeea4SJosef Bacik 		return 0;
322a4abeea4SJosef Bacik 
323a4abeea4SJosef Bacik 	if (type == TRANS_USERSPACE)
324a22285a6SYan, Zheng 		return 1;
325a4abeea4SJosef Bacik 
326a4abeea4SJosef Bacik 	if (type == TRANS_START &&
327a4abeea4SJosef Bacik 	    !atomic_read(&root->fs_info->open_ioctl_trans))
328a4abeea4SJosef Bacik 		return 1;
329a4abeea4SJosef Bacik 
330a22285a6SYan, Zheng 	return 0;
331a22285a6SYan, Zheng }
332a22285a6SYan, Zheng 
33308e007d2SMiao Xie static struct btrfs_trans_handle *
3340860adfdSMiao Xie start_transaction(struct btrfs_root *root, u64 num_items, unsigned int type,
33508e007d2SMiao Xie 		  enum btrfs_reserve_flush_enum flush)
336a22285a6SYan, Zheng {
337a22285a6SYan, Zheng 	struct btrfs_trans_handle *h;
338a22285a6SYan, Zheng 	struct btrfs_transaction *cur_trans;
339b5009945SJosef Bacik 	u64 num_bytes = 0;
340a22285a6SYan, Zheng 	int ret;
341c5567237SArne Jansen 	u64 qgroup_reserved = 0;
342acce952bSliubo 
34387533c47SMiao Xie 	if (test_bit(BTRFS_FS_STATE_ERROR, &root->fs_info->fs_state))
344acce952bSliubo 		return ERR_PTR(-EROFS);
3452a1eb461SJosef Bacik 
3462a1eb461SJosef Bacik 	if (current->journal_info) {
3470860adfdSMiao Xie 		WARN_ON(type & TRANS_EXTWRITERS);
3482a1eb461SJosef Bacik 		h = current->journal_info;
3492a1eb461SJosef Bacik 		h->use_count++;
350b7d5b0a8SMiao Xie 		WARN_ON(h->use_count > 2);
3512a1eb461SJosef Bacik 		h->orig_rsv = h->block_rsv;
3522a1eb461SJosef Bacik 		h->block_rsv = NULL;
3532a1eb461SJosef Bacik 		goto got_it;
3542a1eb461SJosef Bacik 	}
355b5009945SJosef Bacik 
356b5009945SJosef Bacik 	/*
357b5009945SJosef Bacik 	 * Do the reservation before we join the transaction so we can do all
358b5009945SJosef Bacik 	 * the appropriate flushing if need be.
359b5009945SJosef Bacik 	 */
360b5009945SJosef Bacik 	if (num_items > 0 && root != root->fs_info->chunk_root) {
361c5567237SArne Jansen 		if (root->fs_info->quota_enabled &&
362c5567237SArne Jansen 		    is_fstree(root->root_key.objectid)) {
363c5567237SArne Jansen 			qgroup_reserved = num_items * root->leafsize;
364c5567237SArne Jansen 			ret = btrfs_qgroup_reserve(root, qgroup_reserved);
365c5567237SArne Jansen 			if (ret)
366c5567237SArne Jansen 				return ERR_PTR(ret);
367c5567237SArne Jansen 		}
368c5567237SArne Jansen 
369b5009945SJosef Bacik 		num_bytes = btrfs_calc_trans_metadata_size(root, num_items);
3704a92b1b8SJosef Bacik 		ret = btrfs_block_rsv_add(root,
371b5009945SJosef Bacik 					  &root->fs_info->trans_block_rsv,
37208e007d2SMiao Xie 					  num_bytes, flush);
373b5009945SJosef Bacik 		if (ret)
374843fcf35SMiao Xie 			goto reserve_fail;
375b5009945SJosef Bacik 	}
376a22285a6SYan, Zheng again:
377a22285a6SYan, Zheng 	h = kmem_cache_alloc(btrfs_trans_handle_cachep, GFP_NOFS);
378843fcf35SMiao Xie 	if (!h) {
379843fcf35SMiao Xie 		ret = -ENOMEM;
380843fcf35SMiao Xie 		goto alloc_fail;
381843fcf35SMiao Xie 	}
382a22285a6SYan, Zheng 
38398114659SJosef Bacik 	/*
38498114659SJosef Bacik 	 * If we are JOIN_NOLOCK we're already committing a transaction and
38598114659SJosef Bacik 	 * waiting on this guy, so we don't need to do the sb_start_intwrite
38698114659SJosef Bacik 	 * because we're already holding a ref.  We need this because we could
38798114659SJosef Bacik 	 * have raced in and did an fsync() on a file which can kick a commit
38898114659SJosef Bacik 	 * and then we deadlock with somebody doing a freeze.
389354aa0fbSMiao Xie 	 *
390354aa0fbSMiao Xie 	 * If we are ATTACH, it means we just want to catch the current
391354aa0fbSMiao Xie 	 * transaction and commit it, so we needn't do sb_start_intwrite().
39298114659SJosef Bacik 	 */
3930860adfdSMiao Xie 	if (type & __TRANS_FREEZABLE)
394b2b5ef5cSJan Kara 		sb_start_intwrite(root->fs_info->sb);
395b2b5ef5cSJan Kara 
396a22285a6SYan, Zheng 	if (may_wait_transaction(root, type))
39737d1aeeeSChris Mason 		wait_current_trans(root);
398a22285a6SYan, Zheng 
399a4abeea4SJosef Bacik 	do {
400354aa0fbSMiao Xie 		ret = join_transaction(root, type);
401178260b2SMiao Xie 		if (ret == -EBUSY) {
402a4abeea4SJosef Bacik 			wait_current_trans(root);
403178260b2SMiao Xie 			if (unlikely(type == TRANS_ATTACH))
404178260b2SMiao Xie 				ret = -ENOENT;
405178260b2SMiao Xie 		}
406a4abeea4SJosef Bacik 	} while (ret == -EBUSY);
407a4abeea4SJosef Bacik 
408db5b493aSTsutomu Itoh 	if (ret < 0) {
409354aa0fbSMiao Xie 		/* We must get the transaction if we are JOIN_NOLOCK. */
410354aa0fbSMiao Xie 		BUG_ON(type == TRANS_JOIN_NOLOCK);
411843fcf35SMiao Xie 		goto join_fail;
412db5b493aSTsutomu Itoh 	}
4130f7d52f4SChris Mason 
414a22285a6SYan, Zheng 	cur_trans = root->fs_info->running_transaction;
415a22285a6SYan, Zheng 
416a22285a6SYan, Zheng 	h->transid = cur_trans->transid;
417a22285a6SYan, Zheng 	h->transaction = cur_trans;
41879154b1bSChris Mason 	h->blocks_used = 0;
419a22285a6SYan, Zheng 	h->bytes_reserved = 0;
420d13603efSArne Jansen 	h->root = root;
42156bec294SChris Mason 	h->delayed_ref_updates = 0;
4222a1eb461SJosef Bacik 	h->use_count = 1;
4230e721106SJosef Bacik 	h->adding_csums = 0;
424f0486c68SYan, Zheng 	h->block_rsv = NULL;
4252a1eb461SJosef Bacik 	h->orig_rsv = NULL;
42649b25e05SJeff Mahoney 	h->aborted = 0;
4274b824906SMiao Xie 	h->qgroup_reserved = 0;
428bed92eaeSArne Jansen 	h->delayed_ref_elem.seq = 0;
429a698d075SMiao Xie 	h->type = type;
430c6b305a8SJosef Bacik 	h->allocating_chunk = false;
431bed92eaeSArne Jansen 	INIT_LIST_HEAD(&h->qgroup_ref_list);
432ea658badSJosef Bacik 	INIT_LIST_HEAD(&h->new_bgs);
433b7ec40d7SChris Mason 
434a22285a6SYan, Zheng 	smp_mb();
435a22285a6SYan, Zheng 	if (cur_trans->blocked && may_wait_transaction(root, type)) {
436a22285a6SYan, Zheng 		btrfs_commit_transaction(h, root);
437a22285a6SYan, Zheng 		goto again;
438a22285a6SYan, Zheng 	}
4399ed74f2dSJosef Bacik 
440b5009945SJosef Bacik 	if (num_bytes) {
4418c2a3ca2SJosef Bacik 		trace_btrfs_space_reservation(root->fs_info, "transaction",
4422bcc0328SLiu Bo 					      h->transid, num_bytes, 1);
443b5009945SJosef Bacik 		h->block_rsv = &root->fs_info->trans_block_rsv;
444b5009945SJosef Bacik 		h->bytes_reserved = num_bytes;
445a22285a6SYan, Zheng 	}
4464b824906SMiao Xie 	h->qgroup_reserved = qgroup_reserved;
447a22285a6SYan, Zheng 
4482a1eb461SJosef Bacik got_it:
449a4abeea4SJosef Bacik 	btrfs_record_root_in_trans(h, root);
450a22285a6SYan, Zheng 
451a22285a6SYan, Zheng 	if (!current->journal_info && type != TRANS_USERSPACE)
452a22285a6SYan, Zheng 		current->journal_info = h;
45379154b1bSChris Mason 	return h;
454843fcf35SMiao Xie 
455843fcf35SMiao Xie join_fail:
4560860adfdSMiao Xie 	if (type & __TRANS_FREEZABLE)
457843fcf35SMiao Xie 		sb_end_intwrite(root->fs_info->sb);
458843fcf35SMiao Xie 	kmem_cache_free(btrfs_trans_handle_cachep, h);
459843fcf35SMiao Xie alloc_fail:
460843fcf35SMiao Xie 	if (num_bytes)
461843fcf35SMiao Xie 		btrfs_block_rsv_release(root, &root->fs_info->trans_block_rsv,
462843fcf35SMiao Xie 					num_bytes);
463843fcf35SMiao Xie reserve_fail:
464843fcf35SMiao Xie 	if (qgroup_reserved)
465843fcf35SMiao Xie 		btrfs_qgroup_free(root, qgroup_reserved);
466843fcf35SMiao Xie 	return ERR_PTR(ret);
46779154b1bSChris Mason }
46879154b1bSChris Mason 
469f9295749SChris Mason struct btrfs_trans_handle *btrfs_start_transaction(struct btrfs_root *root,
470a22285a6SYan, Zheng 						   int num_items)
471f9295749SChris Mason {
47208e007d2SMiao Xie 	return start_transaction(root, num_items, TRANS_START,
47308e007d2SMiao Xie 				 BTRFS_RESERVE_FLUSH_ALL);
474f9295749SChris Mason }
4758407aa46SMiao Xie 
47608e007d2SMiao Xie struct btrfs_trans_handle *btrfs_start_transaction_lflush(
4778407aa46SMiao Xie 					struct btrfs_root *root, int num_items)
4788407aa46SMiao Xie {
47908e007d2SMiao Xie 	return start_transaction(root, num_items, TRANS_START,
48008e007d2SMiao Xie 				 BTRFS_RESERVE_FLUSH_LIMIT);
4818407aa46SMiao Xie }
4828407aa46SMiao Xie 
4837a7eaa40SJosef Bacik struct btrfs_trans_handle *btrfs_join_transaction(struct btrfs_root *root)
484f9295749SChris Mason {
4858407aa46SMiao Xie 	return start_transaction(root, 0, TRANS_JOIN, 0);
486f9295749SChris Mason }
487f9295749SChris Mason 
4887a7eaa40SJosef Bacik struct btrfs_trans_handle *btrfs_join_transaction_nolock(struct btrfs_root *root)
4890af3d00bSJosef Bacik {
4908407aa46SMiao Xie 	return start_transaction(root, 0, TRANS_JOIN_NOLOCK, 0);
4910af3d00bSJosef Bacik }
4920af3d00bSJosef Bacik 
4937a7eaa40SJosef Bacik struct btrfs_trans_handle *btrfs_start_ioctl_transaction(struct btrfs_root *root)
4949ca9ee09SSage Weil {
4958407aa46SMiao Xie 	return start_transaction(root, 0, TRANS_USERSPACE, 0);
4969ca9ee09SSage Weil }
4979ca9ee09SSage Weil 
498d4edf39bSMiao Xie /*
499d4edf39bSMiao Xie  * btrfs_attach_transaction() - catch the running transaction
500d4edf39bSMiao Xie  *
501d4edf39bSMiao Xie  * It is used when we want to commit the current the transaction, but
502d4edf39bSMiao Xie  * don't want to start a new one.
503d4edf39bSMiao Xie  *
504d4edf39bSMiao Xie  * Note: If this function return -ENOENT, it just means there is no
505d4edf39bSMiao Xie  * running transaction. But it is possible that the inactive transaction
506d4edf39bSMiao Xie  * is still in the memory, not fully on disk. If you hope there is no
507d4edf39bSMiao Xie  * inactive transaction in the fs when -ENOENT is returned, you should
508d4edf39bSMiao Xie  * invoke
509d4edf39bSMiao Xie  *     btrfs_attach_transaction_barrier()
510d4edf39bSMiao Xie  */
511354aa0fbSMiao Xie struct btrfs_trans_handle *btrfs_attach_transaction(struct btrfs_root *root)
51260376ce4SJosef Bacik {
513354aa0fbSMiao Xie 	return start_transaction(root, 0, TRANS_ATTACH, 0);
51460376ce4SJosef Bacik }
51560376ce4SJosef Bacik 
516d4edf39bSMiao Xie /*
517d4edf39bSMiao Xie  * btrfs_attach_transaction() - catch the running transaction
518d4edf39bSMiao Xie  *
519d4edf39bSMiao Xie  * It is similar to the above function, the differentia is this one
520d4edf39bSMiao Xie  * will wait for all the inactive transactions until they fully
521d4edf39bSMiao Xie  * complete.
522d4edf39bSMiao Xie  */
523d4edf39bSMiao Xie struct btrfs_trans_handle *
524d4edf39bSMiao Xie btrfs_attach_transaction_barrier(struct btrfs_root *root)
525d4edf39bSMiao Xie {
526d4edf39bSMiao Xie 	struct btrfs_trans_handle *trans;
527d4edf39bSMiao Xie 
528d4edf39bSMiao Xie 	trans = start_transaction(root, 0, TRANS_ATTACH, 0);
529d4edf39bSMiao Xie 	if (IS_ERR(trans) && PTR_ERR(trans) == -ENOENT)
530d4edf39bSMiao Xie 		btrfs_wait_for_commit(root, 0);
531d4edf39bSMiao Xie 
532d4edf39bSMiao Xie 	return trans;
533d4edf39bSMiao Xie }
534d4edf39bSMiao Xie 
535d352ac68SChris Mason /* wait for a transaction commit to be fully complete */
536b9c8300cSLi Zefan static noinline void wait_for_commit(struct btrfs_root *root,
53789ce8a63SChris Mason 				    struct btrfs_transaction *commit)
53889ce8a63SChris Mason {
53972d63ed6SLi Zefan 	wait_event(commit->commit_wait, commit->commit_done);
54089ce8a63SChris Mason }
54189ce8a63SChris Mason 
54246204592SSage Weil int btrfs_wait_for_commit(struct btrfs_root *root, u64 transid)
54346204592SSage Weil {
54446204592SSage Weil 	struct btrfs_transaction *cur_trans = NULL, *t;
5458cd2807fSMiao Xie 	int ret = 0;
54646204592SSage Weil 
54746204592SSage Weil 	if (transid) {
54846204592SSage Weil 		if (transid <= root->fs_info->last_trans_committed)
549a4abeea4SJosef Bacik 			goto out;
55046204592SSage Weil 
5518cd2807fSMiao Xie 		ret = -EINVAL;
55246204592SSage Weil 		/* find specified transaction */
553a4abeea4SJosef Bacik 		spin_lock(&root->fs_info->trans_lock);
55446204592SSage Weil 		list_for_each_entry(t, &root->fs_info->trans_list, list) {
55546204592SSage Weil 			if (t->transid == transid) {
55646204592SSage Weil 				cur_trans = t;
557a4abeea4SJosef Bacik 				atomic_inc(&cur_trans->use_count);
5588cd2807fSMiao Xie 				ret = 0;
55946204592SSage Weil 				break;
56046204592SSage Weil 			}
5618cd2807fSMiao Xie 			if (t->transid > transid) {
5628cd2807fSMiao Xie 				ret = 0;
56346204592SSage Weil 				break;
56446204592SSage Weil 			}
5658cd2807fSMiao Xie 		}
566a4abeea4SJosef Bacik 		spin_unlock(&root->fs_info->trans_lock);
5678cd2807fSMiao Xie 		/* The specified transaction doesn't exist */
56846204592SSage Weil 		if (!cur_trans)
5698cd2807fSMiao Xie 			goto out;
57046204592SSage Weil 	} else {
57146204592SSage Weil 		/* find newest transaction that is committing | committed */
572a4abeea4SJosef Bacik 		spin_lock(&root->fs_info->trans_lock);
57346204592SSage Weil 		list_for_each_entry_reverse(t, &root->fs_info->trans_list,
57446204592SSage Weil 					    list) {
57546204592SSage Weil 			if (t->in_commit) {
57646204592SSage Weil 				if (t->commit_done)
5773473f3c0SJosef Bacik 					break;
57846204592SSage Weil 				cur_trans = t;
579a4abeea4SJosef Bacik 				atomic_inc(&cur_trans->use_count);
58046204592SSage Weil 				break;
58146204592SSage Weil 			}
58246204592SSage Weil 		}
583a4abeea4SJosef Bacik 		spin_unlock(&root->fs_info->trans_lock);
58446204592SSage Weil 		if (!cur_trans)
585a4abeea4SJosef Bacik 			goto out;  /* nothing committing|committed */
58646204592SSage Weil 	}
58746204592SSage Weil 
58846204592SSage Weil 	wait_for_commit(root, cur_trans);
58946204592SSage Weil 	put_transaction(cur_trans);
590a4abeea4SJosef Bacik out:
59146204592SSage Weil 	return ret;
59246204592SSage Weil }
59346204592SSage Weil 
59437d1aeeeSChris Mason void btrfs_throttle(struct btrfs_root *root)
59537d1aeeeSChris Mason {
596a4abeea4SJosef Bacik 	if (!atomic_read(&root->fs_info->open_ioctl_trans))
59737d1aeeeSChris Mason 		wait_current_trans(root);
59837d1aeeeSChris Mason }
59937d1aeeeSChris Mason 
6008929ecfaSYan, Zheng static int should_end_transaction(struct btrfs_trans_handle *trans,
6018929ecfaSYan, Zheng 				  struct btrfs_root *root)
6028929ecfaSYan, Zheng {
6038929ecfaSYan, Zheng 	int ret;
60436ba022aSJosef Bacik 
60536ba022aSJosef Bacik 	ret = btrfs_block_rsv_check(root, &root->fs_info->global_block_rsv, 5);
6068929ecfaSYan, Zheng 	return ret ? 1 : 0;
6078929ecfaSYan, Zheng }
6088929ecfaSYan, Zheng 
6098929ecfaSYan, Zheng int btrfs_should_end_transaction(struct btrfs_trans_handle *trans,
6108929ecfaSYan, Zheng 				 struct btrfs_root *root)
6118929ecfaSYan, Zheng {
6128929ecfaSYan, Zheng 	struct btrfs_transaction *cur_trans = trans->transaction;
6138929ecfaSYan, Zheng 	int updates;
61449b25e05SJeff Mahoney 	int err;
6158929ecfaSYan, Zheng 
616a4abeea4SJosef Bacik 	smp_mb();
6178929ecfaSYan, Zheng 	if (cur_trans->blocked || cur_trans->delayed_refs.flushing)
6188929ecfaSYan, Zheng 		return 1;
6198929ecfaSYan, Zheng 
6208929ecfaSYan, Zheng 	updates = trans->delayed_ref_updates;
6218929ecfaSYan, Zheng 	trans->delayed_ref_updates = 0;
62249b25e05SJeff Mahoney 	if (updates) {
62349b25e05SJeff Mahoney 		err = btrfs_run_delayed_refs(trans, root, updates);
62449b25e05SJeff Mahoney 		if (err) /* Error code will also eval true */
62549b25e05SJeff Mahoney 			return err;
62649b25e05SJeff Mahoney 	}
6278929ecfaSYan, Zheng 
6288929ecfaSYan, Zheng 	return should_end_transaction(trans, root);
6298929ecfaSYan, Zheng }
6308929ecfaSYan, Zheng 
63189ce8a63SChris Mason static int __btrfs_end_transaction(struct btrfs_trans_handle *trans,
632a698d075SMiao Xie 			  struct btrfs_root *root, int throttle)
63379154b1bSChris Mason {
6348929ecfaSYan, Zheng 	struct btrfs_transaction *cur_trans = trans->transaction;
635ab78c84dSChris Mason 	struct btrfs_fs_info *info = root->fs_info;
636c3e69d58SChris Mason 	int count = 0;
637a698d075SMiao Xie 	int lock = (trans->type != TRANS_JOIN_NOLOCK);
6384edc2ca3SDave Jones 	int err = 0;
639d6e4a428SChris Mason 
6402a1eb461SJosef Bacik 	if (--trans->use_count) {
6412a1eb461SJosef Bacik 		trans->block_rsv = trans->orig_rsv;
6422a1eb461SJosef Bacik 		return 0;
6432a1eb461SJosef Bacik 	}
6442a1eb461SJosef Bacik 
645edf39272SJan Schmidt 	/*
646edf39272SJan Schmidt 	 * do the qgroup accounting as early as possible
647edf39272SJan Schmidt 	 */
648edf39272SJan Schmidt 	err = btrfs_delayed_refs_qgroup_accounting(trans, info);
649edf39272SJan Schmidt 
650b24e03dbSJosef Bacik 	btrfs_trans_release_metadata(trans, root);
6514c13d758SJosef Bacik 	trans->block_rsv = NULL;
652c5567237SArne Jansen 
653c5567237SArne Jansen 	if (trans->qgroup_reserved) {
6547c2ec3f0SLiu Bo 		/*
6557c2ec3f0SLiu Bo 		 * the same root has to be passed here between start_transaction
6567c2ec3f0SLiu Bo 		 * and end_transaction. Subvolume quota depends on this.
6577c2ec3f0SLiu Bo 		 */
6587c2ec3f0SLiu Bo 		btrfs_qgroup_free(trans->root, trans->qgroup_reserved);
659c5567237SArne Jansen 		trans->qgroup_reserved = 0;
660c5567237SArne Jansen 	}
661c5567237SArne Jansen 
662ea658badSJosef Bacik 	if (!list_empty(&trans->new_bgs))
663ea658badSJosef Bacik 		btrfs_create_pending_block_groups(trans, root);
664ea658badSJosef Bacik 
665bb721703SChris Mason 	while (count < 1) {
666c3e69d58SChris Mason 		unsigned long cur = trans->delayed_ref_updates;
667c3e69d58SChris Mason 		trans->delayed_ref_updates = 0;
668c3e69d58SChris Mason 		if (cur &&
669c3e69d58SChris Mason 		    trans->transaction->delayed_refs.num_heads_ready > 64) {
670c3e69d58SChris Mason 			trans->delayed_ref_updates = 0;
671c3e69d58SChris Mason 			btrfs_run_delayed_refs(trans, root, cur);
672c3e69d58SChris Mason 		} else {
673c3e69d58SChris Mason 			break;
674c3e69d58SChris Mason 		}
675c3e69d58SChris Mason 		count++;
67656bec294SChris Mason 	}
677bb721703SChris Mason 
6780e721106SJosef Bacik 	btrfs_trans_release_metadata(trans, root);
6790e721106SJosef Bacik 	trans->block_rsv = NULL;
68056bec294SChris Mason 
681ea658badSJosef Bacik 	if (!list_empty(&trans->new_bgs))
682ea658badSJosef Bacik 		btrfs_create_pending_block_groups(trans, root);
683ea658badSJosef Bacik 
684a4abeea4SJosef Bacik 	if (lock && !atomic_read(&root->fs_info->open_ioctl_trans) &&
685a4abeea4SJosef Bacik 	    should_end_transaction(trans, root)) {
6868929ecfaSYan, Zheng 		trans->transaction->blocked = 1;
687a4abeea4SJosef Bacik 		smp_wmb();
688a4abeea4SJosef Bacik 	}
6898929ecfaSYan, Zheng 
6900af3d00bSJosef Bacik 	if (lock && cur_trans->blocked && !cur_trans->in_commit) {
69181317fdeSJosef Bacik 		if (throttle) {
69281317fdeSJosef Bacik 			/*
69381317fdeSJosef Bacik 			 * We may race with somebody else here so end up having
69481317fdeSJosef Bacik 			 * to call end_transaction on ourselves again, so inc
69581317fdeSJosef Bacik 			 * our use_count.
69681317fdeSJosef Bacik 			 */
69781317fdeSJosef Bacik 			trans->use_count++;
6988929ecfaSYan, Zheng 			return btrfs_commit_transaction(trans, root);
69981317fdeSJosef Bacik 		} else {
7008929ecfaSYan, Zheng 			wake_up_process(info->transaction_kthread);
7018929ecfaSYan, Zheng 		}
70281317fdeSJosef Bacik 	}
7038929ecfaSYan, Zheng 
7040860adfdSMiao Xie 	if (trans->type & __TRANS_FREEZABLE)
7056df7881aSJosef Bacik 		sb_end_intwrite(root->fs_info->sb);
7066df7881aSJosef Bacik 
7078929ecfaSYan, Zheng 	WARN_ON(cur_trans != info->running_transaction);
70813c5a93eSJosef Bacik 	WARN_ON(atomic_read(&cur_trans->num_writers) < 1);
70913c5a93eSJosef Bacik 	atomic_dec(&cur_trans->num_writers);
7100860adfdSMiao Xie 	extwriter_counter_dec(cur_trans, trans->type);
71189ce8a63SChris Mason 
71299d16cbcSSage Weil 	smp_mb();
71379154b1bSChris Mason 	if (waitqueue_active(&cur_trans->writer_wait))
71479154b1bSChris Mason 		wake_up(&cur_trans->writer_wait);
71579154b1bSChris Mason 	put_transaction(cur_trans);
7169ed74f2dSJosef Bacik 
7179ed74f2dSJosef Bacik 	if (current->journal_info == trans)
7189ed74f2dSJosef Bacik 		current->journal_info = NULL;
719ab78c84dSChris Mason 
72024bbcf04SYan, Zheng 	if (throttle)
72124bbcf04SYan, Zheng 		btrfs_run_delayed_iputs(root);
72224bbcf04SYan, Zheng 
72349b25e05SJeff Mahoney 	if (trans->aborted ||
72487533c47SMiao Xie 	    test_bit(BTRFS_FS_STATE_ERROR, &root->fs_info->fs_state))
7254edc2ca3SDave Jones 		err = -EIO;
726edf39272SJan Schmidt 	assert_qgroups_uptodate(trans);
72749b25e05SJeff Mahoney 
7284edc2ca3SDave Jones 	kmem_cache_free(btrfs_trans_handle_cachep, trans);
7294edc2ca3SDave Jones 	return err;
73079154b1bSChris Mason }
73179154b1bSChris Mason 
73289ce8a63SChris Mason int btrfs_end_transaction(struct btrfs_trans_handle *trans,
73389ce8a63SChris Mason 			  struct btrfs_root *root)
73489ce8a63SChris Mason {
73598ad43beSWang Shilong 	return __btrfs_end_transaction(trans, root, 0);
73689ce8a63SChris Mason }
73789ce8a63SChris Mason 
73889ce8a63SChris Mason int btrfs_end_transaction_throttle(struct btrfs_trans_handle *trans,
73989ce8a63SChris Mason 				   struct btrfs_root *root)
74089ce8a63SChris Mason {
74198ad43beSWang Shilong 	return __btrfs_end_transaction(trans, root, 1);
74216cdcec7SMiao Xie }
74316cdcec7SMiao Xie 
74416cdcec7SMiao Xie int btrfs_end_transaction_dmeta(struct btrfs_trans_handle *trans,
74516cdcec7SMiao Xie 				struct btrfs_root *root)
74616cdcec7SMiao Xie {
747a698d075SMiao Xie 	return __btrfs_end_transaction(trans, root, 1);
74889ce8a63SChris Mason }
74989ce8a63SChris Mason 
750d352ac68SChris Mason /*
751d352ac68SChris Mason  * when btree blocks are allocated, they have some corresponding bits set for
752d352ac68SChris Mason  * them in one of two extent_io trees.  This is used to make sure all of
753690587d1SChris Mason  * those extents are sent to disk but does not wait on them
754d352ac68SChris Mason  */
755690587d1SChris Mason int btrfs_write_marked_extents(struct btrfs_root *root,
7568cef4e16SYan, Zheng 			       struct extent_io_tree *dirty_pages, int mark)
75779154b1bSChris Mason {
758777e6bd7SChris Mason 	int err = 0;
7597c4452b9SChris Mason 	int werr = 0;
7601728366eSJosef Bacik 	struct address_space *mapping = root->fs_info->btree_inode->i_mapping;
761e6138876SJosef Bacik 	struct extent_state *cached_state = NULL;
762777e6bd7SChris Mason 	u64 start = 0;
7635f39d397SChris Mason 	u64 end;
76453b381b3SDavid Woodhouse 	struct blk_plug plug;
7657c4452b9SChris Mason 
76653b381b3SDavid Woodhouse 	blk_start_plug(&plug);
7671728366eSJosef Bacik 	while (!find_first_extent_bit(dirty_pages, start, &start, &end,
768e6138876SJosef Bacik 				      mark, &cached_state)) {
769e6138876SJosef Bacik 		convert_extent_bit(dirty_pages, start, end, EXTENT_NEED_WAIT,
770e6138876SJosef Bacik 				   mark, &cached_state, GFP_NOFS);
771e6138876SJosef Bacik 		cached_state = NULL;
7721728366eSJosef Bacik 		err = filemap_fdatawrite_range(mapping, start, end);
7737c4452b9SChris Mason 		if (err)
7747c4452b9SChris Mason 			werr = err;
7751728366eSJosef Bacik 		cond_resched();
7761728366eSJosef Bacik 		start = end + 1;
7777c4452b9SChris Mason 	}
778690587d1SChris Mason 	if (err)
779690587d1SChris Mason 		werr = err;
78053b381b3SDavid Woodhouse 	blk_finish_plug(&plug);
781690587d1SChris Mason 	return werr;
782690587d1SChris Mason }
783690587d1SChris Mason 
784690587d1SChris Mason /*
785690587d1SChris Mason  * when btree blocks are allocated, they have some corresponding bits set for
786690587d1SChris Mason  * them in one of two extent_io trees.  This is used to make sure all of
787690587d1SChris Mason  * those extents are on disk for transaction or log commit.  We wait
788690587d1SChris Mason  * on all the pages and clear them from the dirty pages state tree
789690587d1SChris Mason  */
790690587d1SChris Mason int btrfs_wait_marked_extents(struct btrfs_root *root,
7918cef4e16SYan, Zheng 			      struct extent_io_tree *dirty_pages, int mark)
792690587d1SChris Mason {
793690587d1SChris Mason 	int err = 0;
794690587d1SChris Mason 	int werr = 0;
7951728366eSJosef Bacik 	struct address_space *mapping = root->fs_info->btree_inode->i_mapping;
796e6138876SJosef Bacik 	struct extent_state *cached_state = NULL;
797690587d1SChris Mason 	u64 start = 0;
798690587d1SChris Mason 	u64 end;
799690587d1SChris Mason 
8001728366eSJosef Bacik 	while (!find_first_extent_bit(dirty_pages, start, &start, &end,
801e6138876SJosef Bacik 				      EXTENT_NEED_WAIT, &cached_state)) {
802e6138876SJosef Bacik 		clear_extent_bit(dirty_pages, start, end, EXTENT_NEED_WAIT,
803e6138876SJosef Bacik 				 0, 0, &cached_state, GFP_NOFS);
8041728366eSJosef Bacik 		err = filemap_fdatawait_range(mapping, start, end);
805777e6bd7SChris Mason 		if (err)
806777e6bd7SChris Mason 			werr = err;
807777e6bd7SChris Mason 		cond_resched();
8081728366eSJosef Bacik 		start = end + 1;
809777e6bd7SChris Mason 	}
8107c4452b9SChris Mason 	if (err)
8117c4452b9SChris Mason 		werr = err;
8127c4452b9SChris Mason 	return werr;
81379154b1bSChris Mason }
81479154b1bSChris Mason 
815690587d1SChris Mason /*
816690587d1SChris Mason  * when btree blocks are allocated, they have some corresponding bits set for
817690587d1SChris Mason  * them in one of two extent_io trees.  This is used to make sure all of
818690587d1SChris Mason  * those extents are on disk for transaction or log commit
819690587d1SChris Mason  */
820690587d1SChris Mason int btrfs_write_and_wait_marked_extents(struct btrfs_root *root,
8218cef4e16SYan, Zheng 				struct extent_io_tree *dirty_pages, int mark)
822690587d1SChris Mason {
823690587d1SChris Mason 	int ret;
824690587d1SChris Mason 	int ret2;
825690587d1SChris Mason 
8268cef4e16SYan, Zheng 	ret = btrfs_write_marked_extents(root, dirty_pages, mark);
8278cef4e16SYan, Zheng 	ret2 = btrfs_wait_marked_extents(root, dirty_pages, mark);
828bf0da8c1SChris Mason 
829bf0da8c1SChris Mason 	if (ret)
830bf0da8c1SChris Mason 		return ret;
831bf0da8c1SChris Mason 	if (ret2)
832bf0da8c1SChris Mason 		return ret2;
833bf0da8c1SChris Mason 	return 0;
834690587d1SChris Mason }
835690587d1SChris Mason 
836d0c803c4SChris Mason int btrfs_write_and_wait_transaction(struct btrfs_trans_handle *trans,
837d0c803c4SChris Mason 				     struct btrfs_root *root)
838d0c803c4SChris Mason {
839d0c803c4SChris Mason 	if (!trans || !trans->transaction) {
840d0c803c4SChris Mason 		struct inode *btree_inode;
841d0c803c4SChris Mason 		btree_inode = root->fs_info->btree_inode;
842d0c803c4SChris Mason 		return filemap_write_and_wait(btree_inode->i_mapping);
843d0c803c4SChris Mason 	}
844d0c803c4SChris Mason 	return btrfs_write_and_wait_marked_extents(root,
8458cef4e16SYan, Zheng 					   &trans->transaction->dirty_pages,
8468cef4e16SYan, Zheng 					   EXTENT_DIRTY);
847d0c803c4SChris Mason }
848d0c803c4SChris Mason 
849d352ac68SChris Mason /*
850d352ac68SChris Mason  * this is used to update the root pointer in the tree of tree roots.
851d352ac68SChris Mason  *
852d352ac68SChris Mason  * But, in the case of the extent allocation tree, updating the root
853d352ac68SChris Mason  * pointer may allocate blocks which may change the root of the extent
854d352ac68SChris Mason  * allocation tree.
855d352ac68SChris Mason  *
856d352ac68SChris Mason  * So, this loops and repeats and makes sure the cowonly root didn't
857d352ac68SChris Mason  * change while the root pointer was being updated in the metadata.
858d352ac68SChris Mason  */
8590b86a832SChris Mason static int update_cowonly_root(struct btrfs_trans_handle *trans,
86079154b1bSChris Mason 			       struct btrfs_root *root)
86179154b1bSChris Mason {
86279154b1bSChris Mason 	int ret;
8630b86a832SChris Mason 	u64 old_root_bytenr;
86486b9f2ecSYan, Zheng 	u64 old_root_used;
8650b86a832SChris Mason 	struct btrfs_root *tree_root = root->fs_info->tree_root;
86679154b1bSChris Mason 
86786b9f2ecSYan, Zheng 	old_root_used = btrfs_root_used(&root->root_item);
8680b86a832SChris Mason 	btrfs_write_dirty_block_groups(trans, root);
86956bec294SChris Mason 
87079154b1bSChris Mason 	while (1) {
8710b86a832SChris Mason 		old_root_bytenr = btrfs_root_bytenr(&root->root_item);
87286b9f2ecSYan, Zheng 		if (old_root_bytenr == root->node->start &&
87386b9f2ecSYan, Zheng 		    old_root_used == btrfs_root_used(&root->root_item))
87479154b1bSChris Mason 			break;
87587ef2bb4SChris Mason 
8765d4f98a2SYan Zheng 		btrfs_set_root_node(&root->root_item, root->node);
87779154b1bSChris Mason 		ret = btrfs_update_root(trans, tree_root,
8780b86a832SChris Mason 					&root->root_key,
8790b86a832SChris Mason 					&root->root_item);
88049b25e05SJeff Mahoney 		if (ret)
88149b25e05SJeff Mahoney 			return ret;
88256bec294SChris Mason 
88386b9f2ecSYan, Zheng 		old_root_used = btrfs_root_used(&root->root_item);
8844a8c9a62SYan Zheng 		ret = btrfs_write_dirty_block_groups(trans, root);
88549b25e05SJeff Mahoney 		if (ret)
88649b25e05SJeff Mahoney 			return ret;
8870b86a832SChris Mason 	}
888276e680dSYan Zheng 
889276e680dSYan Zheng 	if (root != root->fs_info->extent_root)
890817d52f8SJosef Bacik 		switch_commit_root(root);
891276e680dSYan Zheng 
8920b86a832SChris Mason 	return 0;
8930b86a832SChris Mason }
8940b86a832SChris Mason 
895d352ac68SChris Mason /*
896d352ac68SChris Mason  * update all the cowonly tree roots on disk
89749b25e05SJeff Mahoney  *
89849b25e05SJeff Mahoney  * The error handling in this function may not be obvious. Any of the
89949b25e05SJeff Mahoney  * failures will cause the file system to go offline. We still need
90049b25e05SJeff Mahoney  * to clean up the delayed refs.
901d352ac68SChris Mason  */
9025d4f98a2SYan Zheng static noinline int commit_cowonly_roots(struct btrfs_trans_handle *trans,
9030b86a832SChris Mason 					 struct btrfs_root *root)
9040b86a832SChris Mason {
9050b86a832SChris Mason 	struct btrfs_fs_info *fs_info = root->fs_info;
9060b86a832SChris Mason 	struct list_head *next;
90784234f3aSYan Zheng 	struct extent_buffer *eb;
90856bec294SChris Mason 	int ret;
90984234f3aSYan Zheng 
91056bec294SChris Mason 	ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
91149b25e05SJeff Mahoney 	if (ret)
91249b25e05SJeff Mahoney 		return ret;
91387ef2bb4SChris Mason 
91484234f3aSYan Zheng 	eb = btrfs_lock_root_node(fs_info->tree_root);
91549b25e05SJeff Mahoney 	ret = btrfs_cow_block(trans, fs_info->tree_root, eb, NULL,
91649b25e05SJeff Mahoney 			      0, &eb);
91784234f3aSYan Zheng 	btrfs_tree_unlock(eb);
91884234f3aSYan Zheng 	free_extent_buffer(eb);
9190b86a832SChris Mason 
92049b25e05SJeff Mahoney 	if (ret)
92149b25e05SJeff Mahoney 		return ret;
92249b25e05SJeff Mahoney 
92356bec294SChris Mason 	ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
92449b25e05SJeff Mahoney 	if (ret)
92549b25e05SJeff Mahoney 		return ret;
92687ef2bb4SChris Mason 
927733f4fbbSStefan Behrens 	ret = btrfs_run_dev_stats(trans, root->fs_info);
9288dabb742SStefan Behrens 	WARN_ON(ret);
9298dabb742SStefan Behrens 	ret = btrfs_run_dev_replace(trans, root->fs_info);
9308dabb742SStefan Behrens 	WARN_ON(ret);
931733f4fbbSStefan Behrens 
932546adb0dSJan Schmidt 	ret = btrfs_run_qgroups(trans, root->fs_info);
933546adb0dSJan Schmidt 	BUG_ON(ret);
934546adb0dSJan Schmidt 
935546adb0dSJan Schmidt 	/* run_qgroups might have added some more refs */
936546adb0dSJan Schmidt 	ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
937546adb0dSJan Schmidt 	BUG_ON(ret);
938546adb0dSJan Schmidt 
9390b86a832SChris Mason 	while (!list_empty(&fs_info->dirty_cowonly_roots)) {
9400b86a832SChris Mason 		next = fs_info->dirty_cowonly_roots.next;
9410b86a832SChris Mason 		list_del_init(next);
9420b86a832SChris Mason 		root = list_entry(next, struct btrfs_root, dirty_list);
94387ef2bb4SChris Mason 
94449b25e05SJeff Mahoney 		ret = update_cowonly_root(trans, root);
94549b25e05SJeff Mahoney 		if (ret)
94649b25e05SJeff Mahoney 			return ret;
94779154b1bSChris Mason 	}
948276e680dSYan Zheng 
949276e680dSYan Zheng 	down_write(&fs_info->extent_commit_sem);
950276e680dSYan Zheng 	switch_commit_root(fs_info->extent_root);
951276e680dSYan Zheng 	up_write(&fs_info->extent_commit_sem);
952276e680dSYan Zheng 
9538dabb742SStefan Behrens 	btrfs_after_dev_replace_commit(fs_info);
9548dabb742SStefan Behrens 
95579154b1bSChris Mason 	return 0;
95679154b1bSChris Mason }
95779154b1bSChris Mason 
958d352ac68SChris Mason /*
959d352ac68SChris Mason  * dead roots are old snapshots that need to be deleted.  This allocates
960d352ac68SChris Mason  * a dirty root struct and adds it into the list of dead roots that need to
961d352ac68SChris Mason  * be deleted
962d352ac68SChris Mason  */
9635d4f98a2SYan Zheng int btrfs_add_dead_root(struct btrfs_root *root)
9645eda7b5eSChris Mason {
965a4abeea4SJosef Bacik 	spin_lock(&root->fs_info->trans_lock);
9669d1a2a3aSDavid Sterba 	list_add_tail(&root->root_list, &root->fs_info->dead_roots);
967a4abeea4SJosef Bacik 	spin_unlock(&root->fs_info->trans_lock);
9685eda7b5eSChris Mason 	return 0;
9695eda7b5eSChris Mason }
9705eda7b5eSChris Mason 
971d352ac68SChris Mason /*
9725d4f98a2SYan Zheng  * update all the cowonly tree roots on disk
973d352ac68SChris Mason  */
9745d4f98a2SYan Zheng static noinline int commit_fs_roots(struct btrfs_trans_handle *trans,
9755d4f98a2SYan Zheng 				    struct btrfs_root *root)
9760f7d52f4SChris Mason {
9770f7d52f4SChris Mason 	struct btrfs_root *gang[8];
9785d4f98a2SYan Zheng 	struct btrfs_fs_info *fs_info = root->fs_info;
9790f7d52f4SChris Mason 	int i;
9800f7d52f4SChris Mason 	int ret;
98154aa1f4dSChris Mason 	int err = 0;
98254aa1f4dSChris Mason 
983a4abeea4SJosef Bacik 	spin_lock(&fs_info->fs_roots_radix_lock);
9840f7d52f4SChris Mason 	while (1) {
9855d4f98a2SYan Zheng 		ret = radix_tree_gang_lookup_tag(&fs_info->fs_roots_radix,
9865d4f98a2SYan Zheng 						 (void **)gang, 0,
9870f7d52f4SChris Mason 						 ARRAY_SIZE(gang),
9880f7d52f4SChris Mason 						 BTRFS_ROOT_TRANS_TAG);
9890f7d52f4SChris Mason 		if (ret == 0)
9900f7d52f4SChris Mason 			break;
9910f7d52f4SChris Mason 		for (i = 0; i < ret; i++) {
9920f7d52f4SChris Mason 			root = gang[i];
9935d4f98a2SYan Zheng 			radix_tree_tag_clear(&fs_info->fs_roots_radix,
9942619ba1fSChris Mason 					(unsigned long)root->root_key.objectid,
9950f7d52f4SChris Mason 					BTRFS_ROOT_TRANS_TAG);
996a4abeea4SJosef Bacik 			spin_unlock(&fs_info->fs_roots_radix_lock);
99731153d81SYan Zheng 
998e02119d5SChris Mason 			btrfs_free_log(trans, root);
9995d4f98a2SYan Zheng 			btrfs_update_reloc_root(trans, root);
1000d68fc57bSYan, Zheng 			btrfs_orphan_commit_root(trans, root);
1001e02119d5SChris Mason 
100282d5902dSLi Zefan 			btrfs_save_ino_cache(root, trans);
100382d5902dSLi Zefan 
1004f1ebcc74SLiu Bo 			/* see comments in should_cow_block() */
1005f1ebcc74SLiu Bo 			root->force_cow = 0;
1006f1ebcc74SLiu Bo 			smp_wmb();
1007f1ebcc74SLiu Bo 
1008978d910dSYan Zheng 			if (root->commit_root != root->node) {
1009581bb050SLi Zefan 				mutex_lock(&root->fs_commit_mutex);
1010817d52f8SJosef Bacik 				switch_commit_root(root);
1011581bb050SLi Zefan 				btrfs_unpin_free_ino(root);
1012581bb050SLi Zefan 				mutex_unlock(&root->fs_commit_mutex);
1013581bb050SLi Zefan 
1014978d910dSYan Zheng 				btrfs_set_root_node(&root->root_item,
1015978d910dSYan Zheng 						    root->node);
1016978d910dSYan Zheng 			}
101731153d81SYan Zheng 
10185d4f98a2SYan Zheng 			err = btrfs_update_root(trans, fs_info->tree_root,
10190f7d52f4SChris Mason 						&root->root_key,
10200f7d52f4SChris Mason 						&root->root_item);
1021a4abeea4SJosef Bacik 			spin_lock(&fs_info->fs_roots_radix_lock);
102254aa1f4dSChris Mason 			if (err)
102354aa1f4dSChris Mason 				break;
10240f7d52f4SChris Mason 		}
10259f3a7427SChris Mason 	}
1026a4abeea4SJosef Bacik 	spin_unlock(&fs_info->fs_roots_radix_lock);
102754aa1f4dSChris Mason 	return err;
10280f7d52f4SChris Mason }
10290f7d52f4SChris Mason 
1030d352ac68SChris Mason /*
1031de78b51aSEric Sandeen  * defrag a given btree.
1032de78b51aSEric Sandeen  * Every leaf in the btree is read and defragged.
1033d352ac68SChris Mason  */
1034de78b51aSEric Sandeen int btrfs_defrag_root(struct btrfs_root *root)
1035e9d0b13bSChris Mason {
1036e9d0b13bSChris Mason 	struct btrfs_fs_info *info = root->fs_info;
1037e9d0b13bSChris Mason 	struct btrfs_trans_handle *trans;
10388929ecfaSYan, Zheng 	int ret;
1039e9d0b13bSChris Mason 
10408929ecfaSYan, Zheng 	if (xchg(&root->defrag_running, 1))
1041e9d0b13bSChris Mason 		return 0;
10428929ecfaSYan, Zheng 
10436b80053dSChris Mason 	while (1) {
10448929ecfaSYan, Zheng 		trans = btrfs_start_transaction(root, 0);
10458929ecfaSYan, Zheng 		if (IS_ERR(trans))
10468929ecfaSYan, Zheng 			return PTR_ERR(trans);
10478929ecfaSYan, Zheng 
1048de78b51aSEric Sandeen 		ret = btrfs_defrag_leaves(trans, root);
10498929ecfaSYan, Zheng 
1050e9d0b13bSChris Mason 		btrfs_end_transaction(trans, root);
1051b53d3f5dSLiu Bo 		btrfs_btree_balance_dirty(info->tree_root);
1052e9d0b13bSChris Mason 		cond_resched();
1053e9d0b13bSChris Mason 
10547841cb28SDavid Sterba 		if (btrfs_fs_closing(root->fs_info) || ret != -EAGAIN)
1055e9d0b13bSChris Mason 			break;
1056210549ebSDavid Sterba 
1057210549ebSDavid Sterba 		if (btrfs_defrag_cancelled(root->fs_info)) {
1058210549ebSDavid Sterba 			printk(KERN_DEBUG "btrfs: defrag_root cancelled\n");
1059210549ebSDavid Sterba 			ret = -EAGAIN;
1060210549ebSDavid Sterba 			break;
1061210549ebSDavid Sterba 		}
1062e9d0b13bSChris Mason 	}
1063e9d0b13bSChris Mason 	root->defrag_running = 0;
10648929ecfaSYan, Zheng 	return ret;
1065e9d0b13bSChris Mason }
1066e9d0b13bSChris Mason 
1067d352ac68SChris Mason /*
1068d352ac68SChris Mason  * new snapshots need to be created at a very specific time in the
1069aec8030aSMiao Xie  * transaction commit.  This does the actual creation.
1070aec8030aSMiao Xie  *
1071aec8030aSMiao Xie  * Note:
1072aec8030aSMiao Xie  * If the error which may affect the commitment of the current transaction
1073aec8030aSMiao Xie  * happens, we should return the error number. If the error which just affect
1074aec8030aSMiao Xie  * the creation of the pending snapshots, just return 0.
1075d352ac68SChris Mason  */
107680b6794dSChris Mason static noinline int create_pending_snapshot(struct btrfs_trans_handle *trans,
10773063d29fSChris Mason 				   struct btrfs_fs_info *fs_info,
10783063d29fSChris Mason 				   struct btrfs_pending_snapshot *pending)
10793063d29fSChris Mason {
10803063d29fSChris Mason 	struct btrfs_key key;
108180b6794dSChris Mason 	struct btrfs_root_item *new_root_item;
10823063d29fSChris Mason 	struct btrfs_root *tree_root = fs_info->tree_root;
10833063d29fSChris Mason 	struct btrfs_root *root = pending->root;
10846bdb72deSSage Weil 	struct btrfs_root *parent_root;
108598c9942aSLiu Bo 	struct btrfs_block_rsv *rsv;
10866bdb72deSSage Weil 	struct inode *parent_inode;
108742874b3dSMiao Xie 	struct btrfs_path *path;
108842874b3dSMiao Xie 	struct btrfs_dir_item *dir_item;
1089a22285a6SYan, Zheng 	struct dentry *dentry;
10903063d29fSChris Mason 	struct extent_buffer *tmp;
1091925baeddSChris Mason 	struct extent_buffer *old;
10928ea05e3aSAlexander Block 	struct timespec cur_time = CURRENT_TIME;
1093aec8030aSMiao Xie 	int ret = 0;
1094d68fc57bSYan, Zheng 	u64 to_reserve = 0;
10956bdb72deSSage Weil 	u64 index = 0;
1096a22285a6SYan, Zheng 	u64 objectid;
1097b83cc969SLi Zefan 	u64 root_flags;
10988ea05e3aSAlexander Block 	uuid_le new_uuid;
10993063d29fSChris Mason 
110042874b3dSMiao Xie 	path = btrfs_alloc_path();
110142874b3dSMiao Xie 	if (!path) {
1102aec8030aSMiao Xie 		pending->error = -ENOMEM;
1103aec8030aSMiao Xie 		return 0;
110442874b3dSMiao Xie 	}
110542874b3dSMiao Xie 
110680b6794dSChris Mason 	new_root_item = kmalloc(sizeof(*new_root_item), GFP_NOFS);
110780b6794dSChris Mason 	if (!new_root_item) {
1108aec8030aSMiao Xie 		pending->error = -ENOMEM;
11096fa9700eSMiao Xie 		goto root_item_alloc_fail;
111080b6794dSChris Mason 	}
1111a22285a6SYan, Zheng 
1112aec8030aSMiao Xie 	pending->error = btrfs_find_free_objectid(tree_root, &objectid);
1113aec8030aSMiao Xie 	if (pending->error)
11146fa9700eSMiao Xie 		goto no_free_objectid;
11153063d29fSChris Mason 
11163fd0a558SYan, Zheng 	btrfs_reloc_pre_snapshot(trans, pending, &to_reserve);
1117d68fc57bSYan, Zheng 
1118d68fc57bSYan, Zheng 	if (to_reserve > 0) {
1119aec8030aSMiao Xie 		pending->error = btrfs_block_rsv_add(root,
1120aec8030aSMiao Xie 						     &pending->block_rsv,
112108e007d2SMiao Xie 						     to_reserve,
112208e007d2SMiao Xie 						     BTRFS_RESERVE_NO_FLUSH);
1123aec8030aSMiao Xie 		if (pending->error)
11246fa9700eSMiao Xie 			goto no_free_objectid;
1125d68fc57bSYan, Zheng 	}
1126d68fc57bSYan, Zheng 
1127aec8030aSMiao Xie 	pending->error = btrfs_qgroup_inherit(trans, fs_info,
1128aec8030aSMiao Xie 					      root->root_key.objectid,
11296f72c7e2SArne Jansen 					      objectid, pending->inherit);
1130aec8030aSMiao Xie 	if (pending->error)
11316fa9700eSMiao Xie 		goto no_free_objectid;
11326f72c7e2SArne Jansen 
11333063d29fSChris Mason 	key.objectid = objectid;
1134a22285a6SYan, Zheng 	key.offset = (u64)-1;
1135a22285a6SYan, Zheng 	key.type = BTRFS_ROOT_ITEM_KEY;
11363063d29fSChris Mason 
11376fa9700eSMiao Xie 	rsv = trans->block_rsv;
1138a22285a6SYan, Zheng 	trans->block_rsv = &pending->block_rsv;
11392382c5ccSLiu Bo 	trans->bytes_reserved = trans->block_rsv->reserved;
11406bdb72deSSage Weil 
1141a22285a6SYan, Zheng 	dentry = pending->dentry;
1142e9662f70SMiao Xie 	parent_inode = pending->dir;
1143a22285a6SYan, Zheng 	parent_root = BTRFS_I(parent_inode)->root;
11447585717fSChris Mason 	record_root_in_trans(trans, parent_root);
1145a22285a6SYan, Zheng 
11466bdb72deSSage Weil 	/*
11476bdb72deSSage Weil 	 * insert the directory item
11486bdb72deSSage Weil 	 */
11496bdb72deSSage Weil 	ret = btrfs_set_inode_index(parent_inode, &index);
115049b25e05SJeff Mahoney 	BUG_ON(ret); /* -ENOMEM */
115142874b3dSMiao Xie 
115242874b3dSMiao Xie 	/* check if there is a file/dir which has the same name. */
115342874b3dSMiao Xie 	dir_item = btrfs_lookup_dir_item(NULL, parent_root, path,
115442874b3dSMiao Xie 					 btrfs_ino(parent_inode),
115542874b3dSMiao Xie 					 dentry->d_name.name,
115642874b3dSMiao Xie 					 dentry->d_name.len, 0);
115742874b3dSMiao Xie 	if (dir_item != NULL && !IS_ERR(dir_item)) {
1158fe66a05aSChris Mason 		pending->error = -EEXIST;
1159aec8030aSMiao Xie 		goto dir_item_existed;
116042874b3dSMiao Xie 	} else if (IS_ERR(dir_item)) {
116142874b3dSMiao Xie 		ret = PTR_ERR(dir_item);
11628732d44fSMiao Xie 		btrfs_abort_transaction(trans, root, ret);
11638732d44fSMiao Xie 		goto fail;
116479787eaaSJeff Mahoney 	}
116542874b3dSMiao Xie 	btrfs_release_path(path);
11666bdb72deSSage Weil 
1167e999376fSChris Mason 	/*
1168e999376fSChris Mason 	 * pull in the delayed directory update
1169e999376fSChris Mason 	 * and the delayed inode item
1170e999376fSChris Mason 	 * otherwise we corrupt the FS during
1171e999376fSChris Mason 	 * snapshot
1172e999376fSChris Mason 	 */
1173e999376fSChris Mason 	ret = btrfs_run_delayed_items(trans, root);
11748732d44fSMiao Xie 	if (ret) {	/* Transaction aborted */
11758732d44fSMiao Xie 		btrfs_abort_transaction(trans, root, ret);
11768732d44fSMiao Xie 		goto fail;
11778732d44fSMiao Xie 	}
1178e999376fSChris Mason 
11797585717fSChris Mason 	record_root_in_trans(trans, root);
11806bdb72deSSage Weil 	btrfs_set_root_last_snapshot(&root->root_item, trans->transid);
11816bdb72deSSage Weil 	memcpy(new_root_item, &root->root_item, sizeof(*new_root_item));
118208fe4db1SLi Zefan 	btrfs_check_and_init_root_item(new_root_item);
11836bdb72deSSage Weil 
1184b83cc969SLi Zefan 	root_flags = btrfs_root_flags(new_root_item);
1185b83cc969SLi Zefan 	if (pending->readonly)
1186b83cc969SLi Zefan 		root_flags |= BTRFS_ROOT_SUBVOL_RDONLY;
1187b83cc969SLi Zefan 	else
1188b83cc969SLi Zefan 		root_flags &= ~BTRFS_ROOT_SUBVOL_RDONLY;
1189b83cc969SLi Zefan 	btrfs_set_root_flags(new_root_item, root_flags);
1190b83cc969SLi Zefan 
11918ea05e3aSAlexander Block 	btrfs_set_root_generation_v2(new_root_item,
11928ea05e3aSAlexander Block 			trans->transid);
11938ea05e3aSAlexander Block 	uuid_le_gen(&new_uuid);
11948ea05e3aSAlexander Block 	memcpy(new_root_item->uuid, new_uuid.b, BTRFS_UUID_SIZE);
11958ea05e3aSAlexander Block 	memcpy(new_root_item->parent_uuid, root->root_item.uuid,
11968ea05e3aSAlexander Block 			BTRFS_UUID_SIZE);
119770023da2SStefan Behrens 	if (!(root_flags & BTRFS_ROOT_SUBVOL_RDONLY)) {
119870023da2SStefan Behrens 		memset(new_root_item->received_uuid, 0,
119970023da2SStefan Behrens 		       sizeof(new_root_item->received_uuid));
12008ea05e3aSAlexander Block 		memset(&new_root_item->stime, 0, sizeof(new_root_item->stime));
12018ea05e3aSAlexander Block 		memset(&new_root_item->rtime, 0, sizeof(new_root_item->rtime));
12028ea05e3aSAlexander Block 		btrfs_set_root_stransid(new_root_item, 0);
12038ea05e3aSAlexander Block 		btrfs_set_root_rtransid(new_root_item, 0);
120470023da2SStefan Behrens 	}
120570023da2SStefan Behrens 	new_root_item->otime.sec = cpu_to_le64(cur_time.tv_sec);
120670023da2SStefan Behrens 	new_root_item->otime.nsec = cpu_to_le32(cur_time.tv_nsec);
120770023da2SStefan Behrens 	btrfs_set_root_otransid(new_root_item, trans->transid);
12088ea05e3aSAlexander Block 
1209925baeddSChris Mason 	old = btrfs_lock_root_node(root);
121049b25e05SJeff Mahoney 	ret = btrfs_cow_block(trans, root, old, NULL, 0, &old);
121179787eaaSJeff Mahoney 	if (ret) {
121279787eaaSJeff Mahoney 		btrfs_tree_unlock(old);
121379787eaaSJeff Mahoney 		free_extent_buffer(old);
12148732d44fSMiao Xie 		btrfs_abort_transaction(trans, root, ret);
12158732d44fSMiao Xie 		goto fail;
121679787eaaSJeff Mahoney 	}
121749b25e05SJeff Mahoney 
12185d4f98a2SYan Zheng 	btrfs_set_lock_blocking(old);
12193063d29fSChris Mason 
122049b25e05SJeff Mahoney 	ret = btrfs_copy_root(trans, root, old, &tmp, objectid);
122179787eaaSJeff Mahoney 	/* clean up in any case */
1222925baeddSChris Mason 	btrfs_tree_unlock(old);
1223925baeddSChris Mason 	free_extent_buffer(old);
12248732d44fSMiao Xie 	if (ret) {
12258732d44fSMiao Xie 		btrfs_abort_transaction(trans, root, ret);
12268732d44fSMiao Xie 		goto fail;
12278732d44fSMiao Xie 	}
12283063d29fSChris Mason 
1229f1ebcc74SLiu Bo 	/* see comments in should_cow_block() */
1230f1ebcc74SLiu Bo 	root->force_cow = 1;
1231f1ebcc74SLiu Bo 	smp_wmb();
1232f1ebcc74SLiu Bo 
12335d4f98a2SYan Zheng 	btrfs_set_root_node(new_root_item, tmp);
1234a22285a6SYan, Zheng 	/* record when the snapshot was created in key.offset */
1235a22285a6SYan, Zheng 	key.offset = trans->transid;
1236a22285a6SYan, Zheng 	ret = btrfs_insert_root(trans, tree_root, &key, new_root_item);
1237925baeddSChris Mason 	btrfs_tree_unlock(tmp);
12383063d29fSChris Mason 	free_extent_buffer(tmp);
12398732d44fSMiao Xie 	if (ret) {
12408732d44fSMiao Xie 		btrfs_abort_transaction(trans, root, ret);
12418732d44fSMiao Xie 		goto fail;
12428732d44fSMiao Xie 	}
12430660b5afSChris Mason 
1244a22285a6SYan, Zheng 	/*
1245a22285a6SYan, Zheng 	 * insert root back/forward references
1246a22285a6SYan, Zheng 	 */
1247a22285a6SYan, Zheng 	ret = btrfs_add_root_ref(trans, tree_root, objectid,
1248a22285a6SYan, Zheng 				 parent_root->root_key.objectid,
124933345d01SLi Zefan 				 btrfs_ino(parent_inode), index,
1250a22285a6SYan, Zheng 				 dentry->d_name.name, dentry->d_name.len);
12518732d44fSMiao Xie 	if (ret) {
12528732d44fSMiao Xie 		btrfs_abort_transaction(trans, root, ret);
12538732d44fSMiao Xie 		goto fail;
12548732d44fSMiao Xie 	}
1255a22285a6SYan, Zheng 
1256a22285a6SYan, Zheng 	key.offset = (u64)-1;
1257a22285a6SYan, Zheng 	pending->snap = btrfs_read_fs_root_no_name(root->fs_info, &key);
125879787eaaSJeff Mahoney 	if (IS_ERR(pending->snap)) {
125979787eaaSJeff Mahoney 		ret = PTR_ERR(pending->snap);
12608732d44fSMiao Xie 		btrfs_abort_transaction(trans, root, ret);
12618732d44fSMiao Xie 		goto fail;
126279787eaaSJeff Mahoney 	}
1263d68fc57bSYan, Zheng 
126449b25e05SJeff Mahoney 	ret = btrfs_reloc_post_snapshot(trans, pending);
12658732d44fSMiao Xie 	if (ret) {
12668732d44fSMiao Xie 		btrfs_abort_transaction(trans, root, ret);
12678732d44fSMiao Xie 		goto fail;
12688732d44fSMiao Xie 	}
1269361048f5SMiao Xie 
1270361048f5SMiao Xie 	ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
12718732d44fSMiao Xie 	if (ret) {
12728732d44fSMiao Xie 		btrfs_abort_transaction(trans, root, ret);
12738732d44fSMiao Xie 		goto fail;
12748732d44fSMiao Xie 	}
127542874b3dSMiao Xie 
127642874b3dSMiao Xie 	ret = btrfs_insert_dir_item(trans, parent_root,
127742874b3dSMiao Xie 				    dentry->d_name.name, dentry->d_name.len,
127842874b3dSMiao Xie 				    parent_inode, &key,
127942874b3dSMiao Xie 				    BTRFS_FT_DIR, index);
128042874b3dSMiao Xie 	/* We have check then name at the beginning, so it is impossible. */
12819c52057cSChris Mason 	BUG_ON(ret == -EEXIST || ret == -EOVERFLOW);
12828732d44fSMiao Xie 	if (ret) {
12838732d44fSMiao Xie 		btrfs_abort_transaction(trans, root, ret);
12848732d44fSMiao Xie 		goto fail;
12858732d44fSMiao Xie 	}
128642874b3dSMiao Xie 
128742874b3dSMiao Xie 	btrfs_i_size_write(parent_inode, parent_inode->i_size +
128842874b3dSMiao Xie 					 dentry->d_name.len * 2);
128942874b3dSMiao Xie 	parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
1290be6aef60SJosef Bacik 	ret = btrfs_update_inode_fallback(trans, parent_root, parent_inode);
129142874b3dSMiao Xie 	if (ret)
12928732d44fSMiao Xie 		btrfs_abort_transaction(trans, root, ret);
12933063d29fSChris Mason fail:
1294aec8030aSMiao Xie 	pending->error = ret;
1295aec8030aSMiao Xie dir_item_existed:
129698c9942aSLiu Bo 	trans->block_rsv = rsv;
12972382c5ccSLiu Bo 	trans->bytes_reserved = 0;
12986fa9700eSMiao Xie no_free_objectid:
12996fa9700eSMiao Xie 	kfree(new_root_item);
13006fa9700eSMiao Xie root_item_alloc_fail:
130142874b3dSMiao Xie 	btrfs_free_path(path);
130249b25e05SJeff Mahoney 	return ret;
13033063d29fSChris Mason }
13043063d29fSChris Mason 
1305d352ac68SChris Mason /*
1306d352ac68SChris Mason  * create all the snapshots we've scheduled for creation
1307d352ac68SChris Mason  */
130880b6794dSChris Mason static noinline int create_pending_snapshots(struct btrfs_trans_handle *trans,
13093063d29fSChris Mason 					     struct btrfs_fs_info *fs_info)
13103063d29fSChris Mason {
1311aec8030aSMiao Xie 	struct btrfs_pending_snapshot *pending, *next;
13123063d29fSChris Mason 	struct list_head *head = &trans->transaction->pending_snapshots;
1313aec8030aSMiao Xie 	int ret = 0;
13143de4586cSChris Mason 
1315aec8030aSMiao Xie 	list_for_each_entry_safe(pending, next, head, list) {
1316aec8030aSMiao Xie 		list_del(&pending->list);
1317aec8030aSMiao Xie 		ret = create_pending_snapshot(trans, fs_info, pending);
1318aec8030aSMiao Xie 		if (ret)
1319aec8030aSMiao Xie 			break;
1320aec8030aSMiao Xie 	}
1321aec8030aSMiao Xie 	return ret;
13223de4586cSChris Mason }
13233de4586cSChris Mason 
13245d4f98a2SYan Zheng static void update_super_roots(struct btrfs_root *root)
13255d4f98a2SYan Zheng {
13265d4f98a2SYan Zheng 	struct btrfs_root_item *root_item;
13275d4f98a2SYan Zheng 	struct btrfs_super_block *super;
13285d4f98a2SYan Zheng 
13296c41761fSDavid Sterba 	super = root->fs_info->super_copy;
13305d4f98a2SYan Zheng 
13315d4f98a2SYan Zheng 	root_item = &root->fs_info->chunk_root->root_item;
13325d4f98a2SYan Zheng 	super->chunk_root = root_item->bytenr;
13335d4f98a2SYan Zheng 	super->chunk_root_generation = root_item->generation;
13345d4f98a2SYan Zheng 	super->chunk_root_level = root_item->level;
13355d4f98a2SYan Zheng 
13365d4f98a2SYan Zheng 	root_item = &root->fs_info->tree_root->root_item;
13375d4f98a2SYan Zheng 	super->root = root_item->bytenr;
13385d4f98a2SYan Zheng 	super->generation = root_item->generation;
13395d4f98a2SYan Zheng 	super->root_level = root_item->level;
134073bc1876SJosef Bacik 	if (btrfs_test_opt(root, SPACE_CACHE))
13410af3d00bSJosef Bacik 		super->cache_generation = root_item->generation;
13425d4f98a2SYan Zheng }
13435d4f98a2SYan Zheng 
1344f36f3042SChris Mason int btrfs_transaction_in_commit(struct btrfs_fs_info *info)
1345f36f3042SChris Mason {
1346f36f3042SChris Mason 	int ret = 0;
1347a4abeea4SJosef Bacik 	spin_lock(&info->trans_lock);
1348f36f3042SChris Mason 	if (info->running_transaction)
1349f36f3042SChris Mason 		ret = info->running_transaction->in_commit;
1350a4abeea4SJosef Bacik 	spin_unlock(&info->trans_lock);
1351f36f3042SChris Mason 	return ret;
1352f36f3042SChris Mason }
1353f36f3042SChris Mason 
13548929ecfaSYan, Zheng int btrfs_transaction_blocked(struct btrfs_fs_info *info)
13558929ecfaSYan, Zheng {
13568929ecfaSYan, Zheng 	int ret = 0;
1357a4abeea4SJosef Bacik 	spin_lock(&info->trans_lock);
13588929ecfaSYan, Zheng 	if (info->running_transaction)
13598929ecfaSYan, Zheng 		ret = info->running_transaction->blocked;
1360a4abeea4SJosef Bacik 	spin_unlock(&info->trans_lock);
13618929ecfaSYan, Zheng 	return ret;
13628929ecfaSYan, Zheng }
13638929ecfaSYan, Zheng 
1364bb9c12c9SSage Weil /*
1365bb9c12c9SSage Weil  * wait for the current transaction commit to start and block subsequent
1366bb9c12c9SSage Weil  * transaction joins
1367bb9c12c9SSage Weil  */
1368bb9c12c9SSage Weil static void wait_current_trans_commit_start(struct btrfs_root *root,
1369bb9c12c9SSage Weil 					    struct btrfs_transaction *trans)
1370bb9c12c9SSage Weil {
137172d63ed6SLi Zefan 	wait_event(root->fs_info->transaction_blocked_wait, trans->in_commit);
1372bb9c12c9SSage Weil }
1373bb9c12c9SSage Weil 
1374bb9c12c9SSage Weil /*
1375bb9c12c9SSage Weil  * wait for the current transaction to start and then become unblocked.
1376bb9c12c9SSage Weil  * caller holds ref.
1377bb9c12c9SSage Weil  */
1378bb9c12c9SSage Weil static void wait_current_trans_commit_start_and_unblock(struct btrfs_root *root,
1379bb9c12c9SSage Weil 					 struct btrfs_transaction *trans)
1380bb9c12c9SSage Weil {
138172d63ed6SLi Zefan 	wait_event(root->fs_info->transaction_wait,
138272d63ed6SLi Zefan 		   trans->commit_done || (trans->in_commit && !trans->blocked));
1383bb9c12c9SSage Weil }
1384bb9c12c9SSage Weil 
1385bb9c12c9SSage Weil /*
1386bb9c12c9SSage Weil  * commit transactions asynchronously. once btrfs_commit_transaction_async
1387bb9c12c9SSage Weil  * returns, any subsequent transaction will not be allowed to join.
1388bb9c12c9SSage Weil  */
1389bb9c12c9SSage Weil struct btrfs_async_commit {
1390bb9c12c9SSage Weil 	struct btrfs_trans_handle *newtrans;
1391bb9c12c9SSage Weil 	struct btrfs_root *root;
13927892b5afSMiao Xie 	struct work_struct work;
1393bb9c12c9SSage Weil };
1394bb9c12c9SSage Weil 
1395bb9c12c9SSage Weil static void do_async_commit(struct work_struct *work)
1396bb9c12c9SSage Weil {
1397bb9c12c9SSage Weil 	struct btrfs_async_commit *ac =
13987892b5afSMiao Xie 		container_of(work, struct btrfs_async_commit, work);
1399bb9c12c9SSage Weil 
14006fc4e354SSage Weil 	/*
14016fc4e354SSage Weil 	 * We've got freeze protection passed with the transaction.
14026fc4e354SSage Weil 	 * Tell lockdep about it.
14036fc4e354SSage Weil 	 */
1404ff7c1d33SMiao Xie 	if (ac->newtrans->type < TRANS_JOIN_NOLOCK)
14056fc4e354SSage Weil 		rwsem_acquire_read(
14066fc4e354SSage Weil 		     &ac->root->fs_info->sb->s_writers.lock_map[SB_FREEZE_FS-1],
14076fc4e354SSage Weil 		     0, 1, _THIS_IP_);
14086fc4e354SSage Weil 
1409e209db7aSSage Weil 	current->journal_info = ac->newtrans;
1410e209db7aSSage Weil 
1411bb9c12c9SSage Weil 	btrfs_commit_transaction(ac->newtrans, ac->root);
1412bb9c12c9SSage Weil 	kfree(ac);
1413bb9c12c9SSage Weil }
1414bb9c12c9SSage Weil 
1415bb9c12c9SSage Weil int btrfs_commit_transaction_async(struct btrfs_trans_handle *trans,
1416bb9c12c9SSage Weil 				   struct btrfs_root *root,
1417bb9c12c9SSage Weil 				   int wait_for_unblock)
1418bb9c12c9SSage Weil {
1419bb9c12c9SSage Weil 	struct btrfs_async_commit *ac;
1420bb9c12c9SSage Weil 	struct btrfs_transaction *cur_trans;
1421bb9c12c9SSage Weil 
1422bb9c12c9SSage Weil 	ac = kmalloc(sizeof(*ac), GFP_NOFS);
1423db5b493aSTsutomu Itoh 	if (!ac)
1424db5b493aSTsutomu Itoh 		return -ENOMEM;
1425bb9c12c9SSage Weil 
14267892b5afSMiao Xie 	INIT_WORK(&ac->work, do_async_commit);
1427bb9c12c9SSage Weil 	ac->root = root;
14287a7eaa40SJosef Bacik 	ac->newtrans = btrfs_join_transaction(root);
14293612b495STsutomu Itoh 	if (IS_ERR(ac->newtrans)) {
14303612b495STsutomu Itoh 		int err = PTR_ERR(ac->newtrans);
14313612b495STsutomu Itoh 		kfree(ac);
14323612b495STsutomu Itoh 		return err;
14333612b495STsutomu Itoh 	}
1434bb9c12c9SSage Weil 
1435bb9c12c9SSage Weil 	/* take transaction reference */
1436bb9c12c9SSage Weil 	cur_trans = trans->transaction;
143713c5a93eSJosef Bacik 	atomic_inc(&cur_trans->use_count);
1438bb9c12c9SSage Weil 
1439bb9c12c9SSage Weil 	btrfs_end_transaction(trans, root);
14406fc4e354SSage Weil 
14416fc4e354SSage Weil 	/*
14426fc4e354SSage Weil 	 * Tell lockdep we've released the freeze rwsem, since the
14436fc4e354SSage Weil 	 * async commit thread will be the one to unlock it.
14446fc4e354SSage Weil 	 */
1445ff7c1d33SMiao Xie 	if (trans->type < TRANS_JOIN_NOLOCK)
1446ff7c1d33SMiao Xie 		rwsem_release(
1447ff7c1d33SMiao Xie 			&root->fs_info->sb->s_writers.lock_map[SB_FREEZE_FS-1],
14486fc4e354SSage Weil 			1, _THIS_IP_);
14496fc4e354SSage Weil 
14507892b5afSMiao Xie 	schedule_work(&ac->work);
1451bb9c12c9SSage Weil 
1452bb9c12c9SSage Weil 	/* wait for transaction to start and unblock */
1453bb9c12c9SSage Weil 	if (wait_for_unblock)
1454bb9c12c9SSage Weil 		wait_current_trans_commit_start_and_unblock(root, cur_trans);
1455bb9c12c9SSage Weil 	else
1456bb9c12c9SSage Weil 		wait_current_trans_commit_start(root, cur_trans);
1457bb9c12c9SSage Weil 
145838e88054SSage Weil 	if (current->journal_info == trans)
145938e88054SSage Weil 		current->journal_info = NULL;
146038e88054SSage Weil 
146138e88054SSage Weil 	put_transaction(cur_trans);
1462bb9c12c9SSage Weil 	return 0;
1463bb9c12c9SSage Weil }
1464bb9c12c9SSage Weil 
146549b25e05SJeff Mahoney 
146649b25e05SJeff Mahoney static void cleanup_transaction(struct btrfs_trans_handle *trans,
14677b8b92afSJosef Bacik 				struct btrfs_root *root, int err)
146849b25e05SJeff Mahoney {
146949b25e05SJeff Mahoney 	struct btrfs_transaction *cur_trans = trans->transaction;
1470f094ac32SLiu Bo 	DEFINE_WAIT(wait);
147149b25e05SJeff Mahoney 
147249b25e05SJeff Mahoney 	WARN_ON(trans->use_count > 1);
147349b25e05SJeff Mahoney 
14747b8b92afSJosef Bacik 	btrfs_abort_transaction(trans, root, err);
14757b8b92afSJosef Bacik 
147649b25e05SJeff Mahoney 	spin_lock(&root->fs_info->trans_lock);
147766b6135bSLiu Bo 
147825d8c284SMiao Xie 	/*
147925d8c284SMiao Xie 	 * If the transaction is removed from the list, it means this
148025d8c284SMiao Xie 	 * transaction has been committed successfully, so it is impossible
148125d8c284SMiao Xie 	 * to call the cleanup function.
148225d8c284SMiao Xie 	 */
148325d8c284SMiao Xie 	BUG_ON(list_empty(&cur_trans->list));
148466b6135bSLiu Bo 
148549b25e05SJeff Mahoney 	list_del_init(&cur_trans->list);
1486d7096fc3SJosef Bacik 	if (cur_trans == root->fs_info->running_transaction) {
1487f094ac32SLiu Bo 		root->fs_info->trans_no_join = 1;
1488f094ac32SLiu Bo 		spin_unlock(&root->fs_info->trans_lock);
1489f094ac32SLiu Bo 		wait_event(cur_trans->writer_wait,
1490f094ac32SLiu Bo 			   atomic_read(&cur_trans->num_writers) == 1);
1491f094ac32SLiu Bo 
1492f094ac32SLiu Bo 		spin_lock(&root->fs_info->trans_lock);
1493d7096fc3SJosef Bacik 		root->fs_info->running_transaction = NULL;
1494d7096fc3SJosef Bacik 	}
149549b25e05SJeff Mahoney 	spin_unlock(&root->fs_info->trans_lock);
149649b25e05SJeff Mahoney 
149749b25e05SJeff Mahoney 	btrfs_cleanup_one_transaction(trans->transaction, root);
149849b25e05SJeff Mahoney 
149949b25e05SJeff Mahoney 	put_transaction(cur_trans);
150049b25e05SJeff Mahoney 	put_transaction(cur_trans);
150149b25e05SJeff Mahoney 
150249b25e05SJeff Mahoney 	trace_btrfs_transaction_commit(root);
150349b25e05SJeff Mahoney 
150449b25e05SJeff Mahoney 	btrfs_scrub_continue(root);
150549b25e05SJeff Mahoney 
150649b25e05SJeff Mahoney 	if (current->journal_info == trans)
150749b25e05SJeff Mahoney 		current->journal_info = NULL;
150849b25e05SJeff Mahoney 
150949b25e05SJeff Mahoney 	kmem_cache_free(btrfs_trans_handle_cachep, trans);
1510cf79ffb5SJosef Bacik 
1511cf79ffb5SJosef Bacik 	spin_lock(&root->fs_info->trans_lock);
1512cf79ffb5SJosef Bacik 	root->fs_info->trans_no_join = 0;
1513cf79ffb5SJosef Bacik 	spin_unlock(&root->fs_info->trans_lock);
151449b25e05SJeff Mahoney }
151549b25e05SJeff Mahoney 
1516ca469637SMiao Xie static int btrfs_flush_all_pending_stuffs(struct btrfs_trans_handle *trans,
1517ca469637SMiao Xie 					  struct btrfs_root *root)
1518ca469637SMiao Xie {
1519ca469637SMiao Xie 	int ret;
1520ca469637SMiao Xie 
1521ca469637SMiao Xie 	ret = btrfs_run_delayed_items(trans, root);
1522ca469637SMiao Xie 	if (ret)
1523ca469637SMiao Xie 		return ret;
1524ca469637SMiao Xie 
1525ca469637SMiao Xie 	/*
1526ca469637SMiao Xie 	 * running the delayed items may have added new refs. account
1527ca469637SMiao Xie 	 * them now so that they hinder processing of more delayed refs
1528ca469637SMiao Xie 	 * as little as possible.
1529ca469637SMiao Xie 	 */
1530ca469637SMiao Xie 	btrfs_delayed_refs_qgroup_accounting(trans, root->fs_info);
1531ca469637SMiao Xie 
1532ca469637SMiao Xie 	/*
1533ca469637SMiao Xie 	 * rename don't use btrfs_join_transaction, so, once we
1534ca469637SMiao Xie 	 * set the transaction to blocked above, we aren't going
1535ca469637SMiao Xie 	 * to get any new ordered operations.  We can safely run
1536ca469637SMiao Xie 	 * it here and no for sure that nothing new will be added
1537ca469637SMiao Xie 	 * to the list
1538ca469637SMiao Xie 	 */
1539569e0f35SJosef Bacik 	ret = btrfs_run_ordered_operations(trans, root, 1);
1540ca469637SMiao Xie 
1541eebc6084SMiao Xie 	return ret;
1542ca469637SMiao Xie }
1543ca469637SMiao Xie 
154482436617SMiao Xie static inline int btrfs_start_delalloc_flush(struct btrfs_fs_info *fs_info)
154582436617SMiao Xie {
154682436617SMiao Xie 	if (btrfs_test_opt(fs_info->tree_root, FLUSHONCOMMIT))
154782436617SMiao Xie 		return btrfs_start_all_delalloc_inodes(fs_info, 1);
154882436617SMiao Xie 	return 0;
154982436617SMiao Xie }
155082436617SMiao Xie 
155182436617SMiao Xie static inline void btrfs_wait_delalloc_flush(struct btrfs_fs_info *fs_info)
155282436617SMiao Xie {
155382436617SMiao Xie 	if (btrfs_test_opt(fs_info->tree_root, FLUSHONCOMMIT))
155482436617SMiao Xie 		btrfs_wait_all_ordered_extents(fs_info, 1);
155582436617SMiao Xie }
155682436617SMiao Xie 
1557bb9c12c9SSage Weil /*
1558bb9c12c9SSage Weil  * btrfs_transaction state sequence:
1559bb9c12c9SSage Weil  *    in_commit = 0, blocked = 0  (initial)
1560bb9c12c9SSage Weil  *    in_commit = 1, blocked = 1
1561bb9c12c9SSage Weil  *    blocked = 0
1562bb9c12c9SSage Weil  *    commit_done = 1
1563bb9c12c9SSage Weil  */
156479154b1bSChris Mason int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
156579154b1bSChris Mason 			     struct btrfs_root *root)
156679154b1bSChris Mason {
156749b25e05SJeff Mahoney 	struct btrfs_transaction *cur_trans = trans->transaction;
15688fd17795SChris Mason 	struct btrfs_transaction *prev_trans = NULL;
156925287e0aSMiao Xie 	int ret;
157079154b1bSChris Mason 
1571569e0f35SJosef Bacik 	ret = btrfs_run_ordered_operations(trans, root, 0);
157225287e0aSMiao Xie 	if (ret) {
157325287e0aSMiao Xie 		btrfs_abort_transaction(trans, root, ret);
1574e4a2bcacSJosef Bacik 		btrfs_end_transaction(trans, root);
1575e4a2bcacSJosef Bacik 		return ret;
157625287e0aSMiao Xie 	}
157725287e0aSMiao Xie 
15788d25a086SMiao Xie 	/* Stop the commit early if ->aborted is set */
15798d25a086SMiao Xie 	if (unlikely(ACCESS_ONCE(cur_trans->aborted))) {
158025287e0aSMiao Xie 		ret = cur_trans->aborted;
1581e4a2bcacSJosef Bacik 		btrfs_end_transaction(trans, root);
1582e4a2bcacSJosef Bacik 		return ret;
158325287e0aSMiao Xie 	}
158449b25e05SJeff Mahoney 
158556bec294SChris Mason 	/* make a pass through all the delayed refs we have so far
158656bec294SChris Mason 	 * any runnings procs may add more while we are here
158756bec294SChris Mason 	 */
158856bec294SChris Mason 	ret = btrfs_run_delayed_refs(trans, root, 0);
1589e4a2bcacSJosef Bacik 	if (ret) {
1590e4a2bcacSJosef Bacik 		btrfs_end_transaction(trans, root);
1591e4a2bcacSJosef Bacik 		return ret;
1592e4a2bcacSJosef Bacik 	}
159356bec294SChris Mason 
15940e721106SJosef Bacik 	btrfs_trans_release_metadata(trans, root);
15950e721106SJosef Bacik 	trans->block_rsv = NULL;
1596272d26d0SMiao Xie 	if (trans->qgroup_reserved) {
1597272d26d0SMiao Xie 		btrfs_qgroup_free(root, trans->qgroup_reserved);
1598272d26d0SMiao Xie 		trans->qgroup_reserved = 0;
1599272d26d0SMiao Xie 	}
16000e721106SJosef Bacik 
1601b7ec40d7SChris Mason 	cur_trans = trans->transaction;
160249b25e05SJeff Mahoney 
160356bec294SChris Mason 	/*
160456bec294SChris Mason 	 * set the flushing flag so procs in this transaction have to
160556bec294SChris Mason 	 * start sending their work down.
160656bec294SChris Mason 	 */
1607b7ec40d7SChris Mason 	cur_trans->delayed_refs.flushing = 1;
160856bec294SChris Mason 
1609ea658badSJosef Bacik 	if (!list_empty(&trans->new_bgs))
1610ea658badSJosef Bacik 		btrfs_create_pending_block_groups(trans, root);
1611ea658badSJosef Bacik 
1612c3e69d58SChris Mason 	ret = btrfs_run_delayed_refs(trans, root, 0);
1613e4a2bcacSJosef Bacik 	if (ret) {
1614e4a2bcacSJosef Bacik 		btrfs_end_transaction(trans, root);
1615e4a2bcacSJosef Bacik 		return ret;
1616e4a2bcacSJosef Bacik 	}
161756bec294SChris Mason 
1618a4abeea4SJosef Bacik 	spin_lock(&cur_trans->commit_lock);
1619b7ec40d7SChris Mason 	if (cur_trans->in_commit) {
1620a4abeea4SJosef Bacik 		spin_unlock(&cur_trans->commit_lock);
162113c5a93eSJosef Bacik 		atomic_inc(&cur_trans->use_count);
162249b25e05SJeff Mahoney 		ret = btrfs_end_transaction(trans, root);
1623ccd467d6SChris Mason 
1624b9c8300cSLi Zefan 		wait_for_commit(root, cur_trans);
162515ee9bc7SJosef Bacik 
162679154b1bSChris Mason 		put_transaction(cur_trans);
162715ee9bc7SJosef Bacik 
162849b25e05SJeff Mahoney 		return ret;
162979154b1bSChris Mason 	}
16304313b399SChris Mason 
16312c90e5d6SChris Mason 	trans->transaction->in_commit = 1;
1632f9295749SChris Mason 	trans->transaction->blocked = 1;
1633a4abeea4SJosef Bacik 	spin_unlock(&cur_trans->commit_lock);
1634bb9c12c9SSage Weil 	wake_up(&root->fs_info->transaction_blocked_wait);
1635bb9c12c9SSage Weil 
1636a4abeea4SJosef Bacik 	spin_lock(&root->fs_info->trans_lock);
1637ccd467d6SChris Mason 	if (cur_trans->list.prev != &root->fs_info->trans_list) {
1638ccd467d6SChris Mason 		prev_trans = list_entry(cur_trans->list.prev,
1639ccd467d6SChris Mason 					struct btrfs_transaction, list);
1640ccd467d6SChris Mason 		if (!prev_trans->commit_done) {
164113c5a93eSJosef Bacik 			atomic_inc(&prev_trans->use_count);
1642a4abeea4SJosef Bacik 			spin_unlock(&root->fs_info->trans_lock);
1643ccd467d6SChris Mason 
1644ccd467d6SChris Mason 			wait_for_commit(root, prev_trans);
1645ccd467d6SChris Mason 
164615ee9bc7SJosef Bacik 			put_transaction(prev_trans);
1647a4abeea4SJosef Bacik 		} else {
1648a4abeea4SJosef Bacik 			spin_unlock(&root->fs_info->trans_lock);
1649ccd467d6SChris Mason 		}
1650a4abeea4SJosef Bacik 	} else {
1651a4abeea4SJosef Bacik 		spin_unlock(&root->fs_info->trans_lock);
1652ccd467d6SChris Mason 	}
165315ee9bc7SJosef Bacik 
16540860adfdSMiao Xie 	extwriter_counter_dec(cur_trans, trans->type);
16550860adfdSMiao Xie 
165682436617SMiao Xie 	ret = btrfs_start_delalloc_flush(root->fs_info);
165782436617SMiao Xie 	if (ret)
165882436617SMiao Xie 		goto cleanup_transaction;
165982436617SMiao Xie 
1660ca469637SMiao Xie 	ret = btrfs_flush_all_pending_stuffs(trans, root);
166149b25e05SJeff Mahoney 	if (ret)
166249b25e05SJeff Mahoney 		goto cleanup_transaction;
166316cdcec7SMiao Xie 
1664581227d0SMiao Xie 	wait_event(cur_trans->writer_wait,
1665581227d0SMiao Xie 		   extwriter_counter_read(cur_trans) == 0);
1666ed3b3d31SChris Mason 
1667581227d0SMiao Xie 	/* some pending stuffs might be added after the previous flush. */
1668ca469637SMiao Xie 	ret = btrfs_flush_all_pending_stuffs(trans, root);
1669ca469637SMiao Xie 	if (ret)
1670ca469637SMiao Xie 		goto cleanup_transaction;
1671ca469637SMiao Xie 
167282436617SMiao Xie 	btrfs_wait_delalloc_flush(root->fs_info);
1673ed0ca140SJosef Bacik 	/*
1674ed0ca140SJosef Bacik 	 * Ok now we need to make sure to block out any other joins while we
1675ed0ca140SJosef Bacik 	 * commit the transaction.  We could have started a join before setting
1676ed0ca140SJosef Bacik 	 * no_join so make sure to wait for num_writers to == 1 again.
1677ed0ca140SJosef Bacik 	 */
1678a4abeea4SJosef Bacik 	spin_lock(&root->fs_info->trans_lock);
1679a4abeea4SJosef Bacik 	root->fs_info->trans_no_join = 1;
1680a4abeea4SJosef Bacik 	spin_unlock(&root->fs_info->trans_lock);
1681ed0ca140SJosef Bacik 	wait_event(cur_trans->writer_wait,
1682ed0ca140SJosef Bacik 		   atomic_read(&cur_trans->num_writers) == 1);
168315ee9bc7SJosef Bacik 
16842cba30f1SMiao Xie 	/* ->aborted might be set after the previous check, so check it */
16852cba30f1SMiao Xie 	if (unlikely(ACCESS_ONCE(cur_trans->aborted))) {
16862cba30f1SMiao Xie 		ret = cur_trans->aborted;
16872cba30f1SMiao Xie 		goto cleanup_transaction;
16882cba30f1SMiao Xie 	}
16897585717fSChris Mason 	/*
16907585717fSChris Mason 	 * the reloc mutex makes sure that we stop
16917585717fSChris Mason 	 * the balancing code from coming in and moving
16927585717fSChris Mason 	 * extents around in the middle of the commit
16937585717fSChris Mason 	 */
16947585717fSChris Mason 	mutex_lock(&root->fs_info->reloc_mutex);
16957585717fSChris Mason 
169642874b3dSMiao Xie 	/*
169742874b3dSMiao Xie 	 * We needn't worry about the delayed items because we will
169842874b3dSMiao Xie 	 * deal with them in create_pending_snapshot(), which is the
169942874b3dSMiao Xie 	 * core function of the snapshot creation.
170042874b3dSMiao Xie 	 */
170142874b3dSMiao Xie 	ret = create_pending_snapshots(trans, root->fs_info);
170249b25e05SJeff Mahoney 	if (ret) {
170349b25e05SJeff Mahoney 		mutex_unlock(&root->fs_info->reloc_mutex);
170449b25e05SJeff Mahoney 		goto cleanup_transaction;
170549b25e05SJeff Mahoney 	}
17063063d29fSChris Mason 
170742874b3dSMiao Xie 	/*
170842874b3dSMiao Xie 	 * We insert the dir indexes of the snapshots and update the inode
170942874b3dSMiao Xie 	 * of the snapshots' parents after the snapshot creation, so there
171042874b3dSMiao Xie 	 * are some delayed items which are not dealt with. Now deal with
171142874b3dSMiao Xie 	 * them.
171242874b3dSMiao Xie 	 *
171342874b3dSMiao Xie 	 * We needn't worry that this operation will corrupt the snapshots,
171442874b3dSMiao Xie 	 * because all the tree which are snapshoted will be forced to COW
171542874b3dSMiao Xie 	 * the nodes and leaves.
171642874b3dSMiao Xie 	 */
171742874b3dSMiao Xie 	ret = btrfs_run_delayed_items(trans, root);
171849b25e05SJeff Mahoney 	if (ret) {
171949b25e05SJeff Mahoney 		mutex_unlock(&root->fs_info->reloc_mutex);
172049b25e05SJeff Mahoney 		goto cleanup_transaction;
172149b25e05SJeff Mahoney 	}
172216cdcec7SMiao Xie 
172356bec294SChris Mason 	ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
172449b25e05SJeff Mahoney 	if (ret) {
172549b25e05SJeff Mahoney 		mutex_unlock(&root->fs_info->reloc_mutex);
172649b25e05SJeff Mahoney 		goto cleanup_transaction;
172749b25e05SJeff Mahoney 	}
172856bec294SChris Mason 
1729e999376fSChris Mason 	/*
1730e999376fSChris Mason 	 * make sure none of the code above managed to slip in a
1731e999376fSChris Mason 	 * delayed item
1732e999376fSChris Mason 	 */
1733e999376fSChris Mason 	btrfs_assert_delayed_root_empty(root);
1734e999376fSChris Mason 
17352c90e5d6SChris Mason 	WARN_ON(cur_trans != trans->transaction);
1736dc17ff8fSChris Mason 
1737a2de733cSArne Jansen 	btrfs_scrub_pause(root);
1738e02119d5SChris Mason 	/* btrfs_commit_tree_roots is responsible for getting the
1739e02119d5SChris Mason 	 * various roots consistent with each other.  Every pointer
1740e02119d5SChris Mason 	 * in the tree of tree roots has to point to the most up to date
1741e02119d5SChris Mason 	 * root for every subvolume and other tree.  So, we have to keep
1742e02119d5SChris Mason 	 * the tree logging code from jumping in and changing any
1743e02119d5SChris Mason 	 * of the trees.
1744e02119d5SChris Mason 	 *
1745e02119d5SChris Mason 	 * At this point in the commit, there can't be any tree-log
1746e02119d5SChris Mason 	 * writers, but a little lower down we drop the trans mutex
1747e02119d5SChris Mason 	 * and let new people in.  By holding the tree_log_mutex
1748e02119d5SChris Mason 	 * from now until after the super is written, we avoid races
1749e02119d5SChris Mason 	 * with the tree-log code.
1750e02119d5SChris Mason 	 */
1751e02119d5SChris Mason 	mutex_lock(&root->fs_info->tree_log_mutex);
17521a40e23bSZheng Yan 
17535d4f98a2SYan Zheng 	ret = commit_fs_roots(trans, root);
175449b25e05SJeff Mahoney 	if (ret) {
175549b25e05SJeff Mahoney 		mutex_unlock(&root->fs_info->tree_log_mutex);
1756871383beSDavid Sterba 		mutex_unlock(&root->fs_info->reloc_mutex);
175749b25e05SJeff Mahoney 		goto cleanup_transaction;
175849b25e05SJeff Mahoney 	}
175954aa1f4dSChris Mason 
17605d4f98a2SYan Zheng 	/* commit_fs_roots gets rid of all the tree log roots, it is now
1761e02119d5SChris Mason 	 * safe to free the root of tree log roots
1762e02119d5SChris Mason 	 */
1763e02119d5SChris Mason 	btrfs_free_log_root_tree(trans, root->fs_info);
1764e02119d5SChris Mason 
17655d4f98a2SYan Zheng 	ret = commit_cowonly_roots(trans, root);
176649b25e05SJeff Mahoney 	if (ret) {
176749b25e05SJeff Mahoney 		mutex_unlock(&root->fs_info->tree_log_mutex);
1768871383beSDavid Sterba 		mutex_unlock(&root->fs_info->reloc_mutex);
176949b25e05SJeff Mahoney 		goto cleanup_transaction;
177049b25e05SJeff Mahoney 	}
177154aa1f4dSChris Mason 
17722cba30f1SMiao Xie 	/*
17732cba30f1SMiao Xie 	 * The tasks which save the space cache and inode cache may also
17742cba30f1SMiao Xie 	 * update ->aborted, check it.
17752cba30f1SMiao Xie 	 */
17762cba30f1SMiao Xie 	if (unlikely(ACCESS_ONCE(cur_trans->aborted))) {
17772cba30f1SMiao Xie 		ret = cur_trans->aborted;
17782cba30f1SMiao Xie 		mutex_unlock(&root->fs_info->tree_log_mutex);
17792cba30f1SMiao Xie 		mutex_unlock(&root->fs_info->reloc_mutex);
17802cba30f1SMiao Xie 		goto cleanup_transaction;
17812cba30f1SMiao Xie 	}
17822cba30f1SMiao Xie 
178311833d66SYan Zheng 	btrfs_prepare_extent_commit(trans, root);
178411833d66SYan Zheng 
178578fae27eSChris Mason 	cur_trans = root->fs_info->running_transaction;
17865f39d397SChris Mason 
17875d4f98a2SYan Zheng 	btrfs_set_root_node(&root->fs_info->tree_root->root_item,
17885d4f98a2SYan Zheng 			    root->fs_info->tree_root->node);
1789817d52f8SJosef Bacik 	switch_commit_root(root->fs_info->tree_root);
17905d4f98a2SYan Zheng 
17915d4f98a2SYan Zheng 	btrfs_set_root_node(&root->fs_info->chunk_root->root_item,
17925d4f98a2SYan Zheng 			    root->fs_info->chunk_root->node);
1793817d52f8SJosef Bacik 	switch_commit_root(root->fs_info->chunk_root);
17945d4f98a2SYan Zheng 
1795edf39272SJan Schmidt 	assert_qgroups_uptodate(trans);
17965d4f98a2SYan Zheng 	update_super_roots(root);
1797e02119d5SChris Mason 
1798e02119d5SChris Mason 	if (!root->fs_info->log_root_recovering) {
17996c41761fSDavid Sterba 		btrfs_set_super_log_root(root->fs_info->super_copy, 0);
18006c41761fSDavid Sterba 		btrfs_set_super_log_root_level(root->fs_info->super_copy, 0);
1801e02119d5SChris Mason 	}
1802e02119d5SChris Mason 
18036c41761fSDavid Sterba 	memcpy(root->fs_info->super_for_commit, root->fs_info->super_copy,
18046c41761fSDavid Sterba 	       sizeof(*root->fs_info->super_copy));
1805ccd467d6SChris Mason 
1806f9295749SChris Mason 	trans->transaction->blocked = 0;
1807a4abeea4SJosef Bacik 	spin_lock(&root->fs_info->trans_lock);
1808a4abeea4SJosef Bacik 	root->fs_info->running_transaction = NULL;
1809a4abeea4SJosef Bacik 	root->fs_info->trans_no_join = 0;
1810a4abeea4SJosef Bacik 	spin_unlock(&root->fs_info->trans_lock);
18117585717fSChris Mason 	mutex_unlock(&root->fs_info->reloc_mutex);
1812b7ec40d7SChris Mason 
1813f9295749SChris Mason 	wake_up(&root->fs_info->transaction_wait);
1814e6dcd2dcSChris Mason 
181579154b1bSChris Mason 	ret = btrfs_write_and_wait_transaction(trans, root);
181649b25e05SJeff Mahoney 	if (ret) {
181749b25e05SJeff Mahoney 		btrfs_error(root->fs_info, ret,
181808748810SDavid Sterba 			    "Error while writing out transaction");
181949b25e05SJeff Mahoney 		mutex_unlock(&root->fs_info->tree_log_mutex);
182049b25e05SJeff Mahoney 		goto cleanup_transaction;
182149b25e05SJeff Mahoney 	}
182249b25e05SJeff Mahoney 
182349b25e05SJeff Mahoney 	ret = write_ctree_super(trans, root, 0);
182449b25e05SJeff Mahoney 	if (ret) {
182549b25e05SJeff Mahoney 		mutex_unlock(&root->fs_info->tree_log_mutex);
182649b25e05SJeff Mahoney 		goto cleanup_transaction;
182749b25e05SJeff Mahoney 	}
18284313b399SChris Mason 
1829e02119d5SChris Mason 	/*
1830e02119d5SChris Mason 	 * the super is written, we can safely allow the tree-loggers
1831e02119d5SChris Mason 	 * to go about their business
1832e02119d5SChris Mason 	 */
1833e02119d5SChris Mason 	mutex_unlock(&root->fs_info->tree_log_mutex);
1834e02119d5SChris Mason 
183511833d66SYan Zheng 	btrfs_finish_extent_commit(trans, root);
18364313b399SChris Mason 
18372c90e5d6SChris Mason 	cur_trans->commit_done = 1;
1838b7ec40d7SChris Mason 
183915ee9bc7SJosef Bacik 	root->fs_info->last_trans_committed = cur_trans->transid;
1840817d52f8SJosef Bacik 
18412c90e5d6SChris Mason 	wake_up(&cur_trans->commit_wait);
18423de4586cSChris Mason 
1843a4abeea4SJosef Bacik 	spin_lock(&root->fs_info->trans_lock);
184413c5a93eSJosef Bacik 	list_del_init(&cur_trans->list);
1845a4abeea4SJosef Bacik 	spin_unlock(&root->fs_info->trans_lock);
1846a4abeea4SJosef Bacik 
184779154b1bSChris Mason 	put_transaction(cur_trans);
184878fae27eSChris Mason 	put_transaction(cur_trans);
184958176a96SJosef Bacik 
18500860adfdSMiao Xie 	if (trans->type & __TRANS_FREEZABLE)
1851b2b5ef5cSJan Kara 		sb_end_intwrite(root->fs_info->sb);
1852b2b5ef5cSJan Kara 
18531abe9b8aSliubo 	trace_btrfs_transaction_commit(root);
18541abe9b8aSliubo 
1855a2de733cSArne Jansen 	btrfs_scrub_continue(root);
1856a2de733cSArne Jansen 
18579ed74f2dSJosef Bacik 	if (current->journal_info == trans)
18589ed74f2dSJosef Bacik 		current->journal_info = NULL;
18599ed74f2dSJosef Bacik 
18602c90e5d6SChris Mason 	kmem_cache_free(btrfs_trans_handle_cachep, trans);
186124bbcf04SYan, Zheng 
186224bbcf04SYan, Zheng 	if (current != root->fs_info->transaction_kthread)
186324bbcf04SYan, Zheng 		btrfs_run_delayed_iputs(root);
186424bbcf04SYan, Zheng 
186579154b1bSChris Mason 	return ret;
186649b25e05SJeff Mahoney 
186749b25e05SJeff Mahoney cleanup_transaction:
18680e721106SJosef Bacik 	btrfs_trans_release_metadata(trans, root);
18690e721106SJosef Bacik 	trans->block_rsv = NULL;
1870272d26d0SMiao Xie 	if (trans->qgroup_reserved) {
1871272d26d0SMiao Xie 		btrfs_qgroup_free(root, trans->qgroup_reserved);
1872272d26d0SMiao Xie 		trans->qgroup_reserved = 0;
1873272d26d0SMiao Xie 	}
1874c2cf52ebSSimon Kirby 	btrfs_warn(root->fs_info, "Skipping commit of aborted transaction.");
187549b25e05SJeff Mahoney 	if (current->journal_info == trans)
187649b25e05SJeff Mahoney 		current->journal_info = NULL;
18777b8b92afSJosef Bacik 	cleanup_transaction(trans, root, ret);
187849b25e05SJeff Mahoney 
187949b25e05SJeff Mahoney 	return ret;
188079154b1bSChris Mason }
188179154b1bSChris Mason 
1882d352ac68SChris Mason /*
18839d1a2a3aSDavid Sterba  * return < 0 if error
18849d1a2a3aSDavid Sterba  * 0 if there are no more dead_roots at the time of call
18859d1a2a3aSDavid Sterba  * 1 there are more to be processed, call me again
18869d1a2a3aSDavid Sterba  *
18879d1a2a3aSDavid Sterba  * The return value indicates there are certainly more snapshots to delete, but
18889d1a2a3aSDavid Sterba  * if there comes a new one during processing, it may return 0. We don't mind,
18899d1a2a3aSDavid Sterba  * because btrfs_commit_super will poke cleaner thread and it will process it a
18909d1a2a3aSDavid Sterba  * few seconds later.
1891d352ac68SChris Mason  */
18929d1a2a3aSDavid Sterba int btrfs_clean_one_deleted_snapshot(struct btrfs_root *root)
1893e9d0b13bSChris Mason {
18949d1a2a3aSDavid Sterba 	int ret;
18955d4f98a2SYan Zheng 	struct btrfs_fs_info *fs_info = root->fs_info;
1896e9d0b13bSChris Mason 
1897a4abeea4SJosef Bacik 	spin_lock(&fs_info->trans_lock);
18989d1a2a3aSDavid Sterba 	if (list_empty(&fs_info->dead_roots)) {
18999d1a2a3aSDavid Sterba 		spin_unlock(&fs_info->trans_lock);
19009d1a2a3aSDavid Sterba 		return 0;
19019d1a2a3aSDavid Sterba 	}
19029d1a2a3aSDavid Sterba 	root = list_first_entry(&fs_info->dead_roots,
19039d1a2a3aSDavid Sterba 			struct btrfs_root, root_list);
19049d1a2a3aSDavid Sterba 	list_del(&root->root_list);
1905a4abeea4SJosef Bacik 	spin_unlock(&fs_info->trans_lock);
19065d4f98a2SYan Zheng 
19079d1a2a3aSDavid Sterba 	pr_debug("btrfs: cleaner removing %llu\n",
19089d1a2a3aSDavid Sterba 			(unsigned long long)root->objectid);
190976dda93cSYan, Zheng 
191016cdcec7SMiao Xie 	btrfs_kill_all_delayed_nodes(root);
191116cdcec7SMiao Xie 
191276dda93cSYan, Zheng 	if (btrfs_header_backref_rev(root->node) <
191376dda93cSYan, Zheng 			BTRFS_MIXED_BACKREF_REV)
19142c536799SJeff Mahoney 		ret = btrfs_drop_snapshot(root, NULL, 0, 0);
191576dda93cSYan, Zheng 	else
19162c536799SJeff Mahoney 		ret = btrfs_drop_snapshot(root, NULL, 1, 0);
19179d1a2a3aSDavid Sterba 	/*
19189d1a2a3aSDavid Sterba 	 * If we encounter a transaction abort during snapshot cleaning, we
19199d1a2a3aSDavid Sterba 	 * don't want to crash here
19209d1a2a3aSDavid Sterba 	 */
19219d1a2a3aSDavid Sterba 	BUG_ON(ret < 0 && ret != -EAGAIN && ret != -EROFS);
19229d1a2a3aSDavid Sterba 	return 1;
1923e9d0b13bSChris Mason }
1924