xref: /openbmc/linux/fs/btrfs/transaction.c (revision 42874b3d)
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"
3379154b1bSChris Mason 
340f7d52f4SChris Mason #define BTRFS_ROOT_TRANS_TAG 0
350f7d52f4SChris Mason 
3649b25e05SJeff Mahoney void put_transaction(struct btrfs_transaction *transaction)
3779154b1bSChris Mason {
3813c5a93eSJosef Bacik 	WARN_ON(atomic_read(&transaction->use_count) == 0);
3913c5a93eSJosef Bacik 	if (atomic_dec_and_test(&transaction->use_count)) {
40a4abeea4SJosef Bacik 		BUG_ON(!list_empty(&transaction->list));
4100f04b88SArne Jansen 		WARN_ON(transaction->delayed_refs.root.rb_node);
422c90e5d6SChris Mason 		memset(transaction, 0, sizeof(*transaction));
432c90e5d6SChris Mason 		kmem_cache_free(btrfs_transaction_cachep, transaction);
4479154b1bSChris Mason 	}
4578fae27eSChris Mason }
4679154b1bSChris Mason 
47817d52f8SJosef Bacik static noinline void switch_commit_root(struct btrfs_root *root)
48817d52f8SJosef Bacik {
49817d52f8SJosef Bacik 	free_extent_buffer(root->commit_root);
50817d52f8SJosef Bacik 	root->commit_root = btrfs_root_node(root);
51817d52f8SJosef Bacik }
52817d52f8SJosef Bacik 
53d352ac68SChris Mason /*
54d352ac68SChris Mason  * either allocate a new transaction or hop into the existing one
55d352ac68SChris Mason  */
56a4abeea4SJosef Bacik static noinline int join_transaction(struct btrfs_root *root, int nofail)
5779154b1bSChris Mason {
5879154b1bSChris Mason 	struct btrfs_transaction *cur_trans;
5919ae4e81SJan Schmidt 	struct btrfs_fs_info *fs_info = root->fs_info;
60a4abeea4SJosef Bacik 
6119ae4e81SJan Schmidt 	spin_lock(&fs_info->trans_lock);
62d43317dcSChris Mason loop:
6349b25e05SJeff Mahoney 	/* The file system has been taken offline. No new transactions. */
6419ae4e81SJan Schmidt 	if (fs_info->fs_state & BTRFS_SUPER_FLAG_ERROR) {
6519ae4e81SJan Schmidt 		spin_unlock(&fs_info->trans_lock);
6649b25e05SJeff Mahoney 		return -EROFS;
6749b25e05SJeff Mahoney 	}
6849b25e05SJeff Mahoney 
6919ae4e81SJan Schmidt 	if (fs_info->trans_no_join) {
70a4abeea4SJosef Bacik 		if (!nofail) {
7119ae4e81SJan Schmidt 			spin_unlock(&fs_info->trans_lock);
72a4abeea4SJosef Bacik 			return -EBUSY;
73a4abeea4SJosef Bacik 		}
74a4abeea4SJosef Bacik 	}
75a4abeea4SJosef Bacik 
7619ae4e81SJan Schmidt 	cur_trans = fs_info->running_transaction;
77a4abeea4SJosef Bacik 	if (cur_trans) {
78871383beSDavid Sterba 		if (cur_trans->aborted) {
7919ae4e81SJan Schmidt 			spin_unlock(&fs_info->trans_lock);
8049b25e05SJeff Mahoney 			return cur_trans->aborted;
81871383beSDavid Sterba 		}
82a4abeea4SJosef Bacik 		atomic_inc(&cur_trans->use_count);
83a4abeea4SJosef Bacik 		atomic_inc(&cur_trans->num_writers);
84a4abeea4SJosef Bacik 		cur_trans->num_joined++;
8519ae4e81SJan Schmidt 		spin_unlock(&fs_info->trans_lock);
86a4abeea4SJosef Bacik 		return 0;
87a4abeea4SJosef Bacik 	}
8819ae4e81SJan Schmidt 	spin_unlock(&fs_info->trans_lock);
89a4abeea4SJosef Bacik 
90a4abeea4SJosef Bacik 	cur_trans = kmem_cache_alloc(btrfs_transaction_cachep, GFP_NOFS);
91db5b493aSTsutomu Itoh 	if (!cur_trans)
92db5b493aSTsutomu Itoh 		return -ENOMEM;
93d43317dcSChris Mason 
9419ae4e81SJan Schmidt 	spin_lock(&fs_info->trans_lock);
9519ae4e81SJan Schmidt 	if (fs_info->running_transaction) {
96d43317dcSChris Mason 		/*
97d43317dcSChris Mason 		 * someone started a transaction after we unlocked.  Make sure
98d43317dcSChris Mason 		 * to redo the trans_no_join checks above
99d43317dcSChris Mason 		 */
100a4abeea4SJosef Bacik 		kmem_cache_free(btrfs_transaction_cachep, cur_trans);
10119ae4e81SJan Schmidt 		cur_trans = fs_info->running_transaction;
102d43317dcSChris Mason 		goto loop;
103e4b50e14SDan Carpenter 	} else if (fs_info->fs_state & BTRFS_SUPER_FLAG_ERROR) {
104e4b50e14SDan Carpenter 		spin_unlock(&fs_info->trans_lock);
1057b8b92afSJosef Bacik 		kmem_cache_free(btrfs_transaction_cachep, cur_trans);
1067b8b92afSJosef Bacik 		return -EROFS;
107a4abeea4SJosef Bacik 	}
108d43317dcSChris Mason 
10913c5a93eSJosef Bacik 	atomic_set(&cur_trans->num_writers, 1);
11015ee9bc7SJosef Bacik 	cur_trans->num_joined = 0;
11179154b1bSChris Mason 	init_waitqueue_head(&cur_trans->writer_wait);
11279154b1bSChris Mason 	init_waitqueue_head(&cur_trans->commit_wait);
11379154b1bSChris Mason 	cur_trans->in_commit = 0;
114f9295749SChris Mason 	cur_trans->blocked = 0;
115a4abeea4SJosef Bacik 	/*
116a4abeea4SJosef Bacik 	 * One for this trans handle, one so it will live on until we
117a4abeea4SJosef Bacik 	 * commit the transaction.
118a4abeea4SJosef Bacik 	 */
119a4abeea4SJosef Bacik 	atomic_set(&cur_trans->use_count, 2);
12079154b1bSChris Mason 	cur_trans->commit_done = 0;
12108607c1bSChris Mason 	cur_trans->start_time = get_seconds();
12256bec294SChris Mason 
1236bef4d31SEric Paris 	cur_trans->delayed_refs.root = RB_ROOT;
12456bec294SChris Mason 	cur_trans->delayed_refs.num_entries = 0;
125c3e69d58SChris Mason 	cur_trans->delayed_refs.num_heads_ready = 0;
126c3e69d58SChris Mason 	cur_trans->delayed_refs.num_heads = 0;
12756bec294SChris Mason 	cur_trans->delayed_refs.flushing = 0;
128c3e69d58SChris Mason 	cur_trans->delayed_refs.run_delayed_start = 0;
12920b297d6SJan Schmidt 
13020b297d6SJan Schmidt 	/*
13120b297d6SJan Schmidt 	 * although the tree mod log is per file system and not per transaction,
13220b297d6SJan Schmidt 	 * the log must never go across transaction boundaries.
13320b297d6SJan Schmidt 	 */
13420b297d6SJan Schmidt 	smp_mb();
13520b297d6SJan Schmidt 	if (!list_empty(&fs_info->tree_mod_seq_list)) {
13620b297d6SJan Schmidt 		printk(KERN_ERR "btrfs: tree_mod_seq_list not empty when "
13720b297d6SJan Schmidt 			"creating a fresh transaction\n");
13820b297d6SJan Schmidt 		WARN_ON(1);
13920b297d6SJan Schmidt 	}
14020b297d6SJan Schmidt 	if (!RB_EMPTY_ROOT(&fs_info->tree_mod_log)) {
14120b297d6SJan Schmidt 		printk(KERN_ERR "btrfs: tree_mod_log rb tree not empty when "
14220b297d6SJan Schmidt 			"creating a fresh transaction\n");
14320b297d6SJan Schmidt 		WARN_ON(1);
14420b297d6SJan Schmidt 	}
14520b297d6SJan Schmidt 	atomic_set(&fs_info->tree_mod_seq, 0);
14620b297d6SJan Schmidt 
147a4abeea4SJosef Bacik 	spin_lock_init(&cur_trans->commit_lock);
14856bec294SChris Mason 	spin_lock_init(&cur_trans->delayed_refs.lock);
14956bec294SChris Mason 
1503063d29fSChris Mason 	INIT_LIST_HEAD(&cur_trans->pending_snapshots);
15119ae4e81SJan Schmidt 	list_add_tail(&cur_trans->list, &fs_info->trans_list);
152d1310b2eSChris Mason 	extent_io_tree_init(&cur_trans->dirty_pages,
15319ae4e81SJan Schmidt 			     fs_info->btree_inode->i_mapping);
15419ae4e81SJan Schmidt 	fs_info->generation++;
15519ae4e81SJan Schmidt 	cur_trans->transid = fs_info->generation;
15619ae4e81SJan Schmidt 	fs_info->running_transaction = cur_trans;
15749b25e05SJeff Mahoney 	cur_trans->aborted = 0;
15819ae4e81SJan Schmidt 	spin_unlock(&fs_info->trans_lock);
15915ee9bc7SJosef Bacik 
16079154b1bSChris Mason 	return 0;
16179154b1bSChris Mason }
16279154b1bSChris Mason 
163d352ac68SChris Mason /*
164d397712bSChris Mason  * this does all the record keeping required to make sure that a reference
165d397712bSChris Mason  * counted root is properly recorded in a given transaction.  This is required
166d397712bSChris Mason  * to make sure the old root from before we joined the transaction is deleted
167d397712bSChris Mason  * when the transaction commits
168d352ac68SChris Mason  */
1697585717fSChris Mason static int record_root_in_trans(struct btrfs_trans_handle *trans,
1705d4f98a2SYan Zheng 			       struct btrfs_root *root)
1716702ed49SChris Mason {
1725d4f98a2SYan Zheng 	if (root->ref_cows && root->last_trans < trans->transid) {
1736702ed49SChris Mason 		WARN_ON(root == root->fs_info->extent_root);
1745d4f98a2SYan Zheng 		WARN_ON(root->commit_root != root->node);
1755d4f98a2SYan Zheng 
1767585717fSChris Mason 		/*
1777585717fSChris Mason 		 * see below for in_trans_setup usage rules
1787585717fSChris Mason 		 * we have the reloc mutex held now, so there
1797585717fSChris Mason 		 * is only one writer in this function
1807585717fSChris Mason 		 */
1817585717fSChris Mason 		root->in_trans_setup = 1;
1827585717fSChris Mason 
1837585717fSChris Mason 		/* make sure readers find in_trans_setup before
1847585717fSChris Mason 		 * they find our root->last_trans update
1857585717fSChris Mason 		 */
1867585717fSChris Mason 		smp_wmb();
1877585717fSChris Mason 
188a4abeea4SJosef Bacik 		spin_lock(&root->fs_info->fs_roots_radix_lock);
189a4abeea4SJosef Bacik 		if (root->last_trans == trans->transid) {
190a4abeea4SJosef Bacik 			spin_unlock(&root->fs_info->fs_roots_radix_lock);
191a4abeea4SJosef Bacik 			return 0;
192a4abeea4SJosef Bacik 		}
1936702ed49SChris Mason 		radix_tree_tag_set(&root->fs_info->fs_roots_radix,
1946702ed49SChris Mason 			   (unsigned long)root->root_key.objectid,
1956702ed49SChris Mason 			   BTRFS_ROOT_TRANS_TAG);
196a4abeea4SJosef Bacik 		spin_unlock(&root->fs_info->fs_roots_radix_lock);
1977585717fSChris Mason 		root->last_trans = trans->transid;
1987585717fSChris Mason 
1997585717fSChris Mason 		/* this is pretty tricky.  We don't want to
2007585717fSChris Mason 		 * take the relocation lock in btrfs_record_root_in_trans
2017585717fSChris Mason 		 * unless we're really doing the first setup for this root in
2027585717fSChris Mason 		 * this transaction.
2037585717fSChris Mason 		 *
2047585717fSChris Mason 		 * Normally we'd use root->last_trans as a flag to decide
2057585717fSChris Mason 		 * if we want to take the expensive mutex.
2067585717fSChris Mason 		 *
2077585717fSChris Mason 		 * But, we have to set root->last_trans before we
2087585717fSChris Mason 		 * init the relocation root, otherwise, we trip over warnings
2097585717fSChris Mason 		 * in ctree.c.  The solution used here is to flag ourselves
2107585717fSChris Mason 		 * with root->in_trans_setup.  When this is 1, we're still
2117585717fSChris Mason 		 * fixing up the reloc trees and everyone must wait.
2127585717fSChris Mason 		 *
2137585717fSChris Mason 		 * When this is zero, they can trust root->last_trans and fly
2147585717fSChris Mason 		 * through btrfs_record_root_in_trans without having to take the
2157585717fSChris Mason 		 * lock.  smp_wmb() makes sure that all the writes above are
2167585717fSChris Mason 		 * done before we pop in the zero below
2177585717fSChris Mason 		 */
2185d4f98a2SYan Zheng 		btrfs_init_reloc_root(trans, root);
2197585717fSChris Mason 		smp_wmb();
2207585717fSChris Mason 		root->in_trans_setup = 0;
2216702ed49SChris Mason 	}
2225d4f98a2SYan Zheng 	return 0;
2236702ed49SChris Mason }
2245d4f98a2SYan Zheng 
2257585717fSChris Mason 
2267585717fSChris Mason int btrfs_record_root_in_trans(struct btrfs_trans_handle *trans,
2277585717fSChris Mason 			       struct btrfs_root *root)
2287585717fSChris Mason {
2297585717fSChris Mason 	if (!root->ref_cows)
2307585717fSChris Mason 		return 0;
2317585717fSChris Mason 
2327585717fSChris Mason 	/*
2337585717fSChris Mason 	 * see record_root_in_trans for comments about in_trans_setup usage
2347585717fSChris Mason 	 * and barriers
2357585717fSChris Mason 	 */
2367585717fSChris Mason 	smp_rmb();
2377585717fSChris Mason 	if (root->last_trans == trans->transid &&
2387585717fSChris Mason 	    !root->in_trans_setup)
2397585717fSChris Mason 		return 0;
2407585717fSChris Mason 
2417585717fSChris Mason 	mutex_lock(&root->fs_info->reloc_mutex);
2427585717fSChris Mason 	record_root_in_trans(trans, root);
2437585717fSChris Mason 	mutex_unlock(&root->fs_info->reloc_mutex);
2447585717fSChris Mason 
2457585717fSChris Mason 	return 0;
2467585717fSChris Mason }
2477585717fSChris Mason 
248d352ac68SChris Mason /* wait for commit against the current transaction to become unblocked
249d352ac68SChris Mason  * when this is done, it is safe to start a new transaction, but the current
250d352ac68SChris Mason  * transaction might not be fully on disk.
251d352ac68SChris Mason  */
25237d1aeeeSChris Mason static void wait_current_trans(struct btrfs_root *root)
25379154b1bSChris Mason {
254f9295749SChris Mason 	struct btrfs_transaction *cur_trans;
25579154b1bSChris Mason 
256a4abeea4SJosef Bacik 	spin_lock(&root->fs_info->trans_lock);
257f9295749SChris Mason 	cur_trans = root->fs_info->running_transaction;
25837d1aeeeSChris Mason 	if (cur_trans && cur_trans->blocked) {
25913c5a93eSJosef Bacik 		atomic_inc(&cur_trans->use_count);
260a4abeea4SJosef Bacik 		spin_unlock(&root->fs_info->trans_lock);
26172d63ed6SLi Zefan 
26272d63ed6SLi Zefan 		wait_event(root->fs_info->transaction_wait,
26372d63ed6SLi Zefan 			   !cur_trans->blocked);
264f9295749SChris Mason 		put_transaction(cur_trans);
265a4abeea4SJosef Bacik 	} else {
266a4abeea4SJosef Bacik 		spin_unlock(&root->fs_info->trans_lock);
267f9295749SChris Mason 	}
26837d1aeeeSChris Mason }
26937d1aeeeSChris Mason 
270249ac1e5SJosef Bacik enum btrfs_trans_type {
271249ac1e5SJosef Bacik 	TRANS_START,
272249ac1e5SJosef Bacik 	TRANS_JOIN,
273249ac1e5SJosef Bacik 	TRANS_USERSPACE,
2740af3d00bSJosef Bacik 	TRANS_JOIN_NOLOCK,
275249ac1e5SJosef Bacik };
276249ac1e5SJosef Bacik 
277a22285a6SYan, Zheng static int may_wait_transaction(struct btrfs_root *root, int type)
27837d1aeeeSChris Mason {
279a4abeea4SJosef Bacik 	if (root->fs_info->log_root_recovering)
280a4abeea4SJosef Bacik 		return 0;
281a4abeea4SJosef Bacik 
282a4abeea4SJosef Bacik 	if (type == TRANS_USERSPACE)
283a22285a6SYan, Zheng 		return 1;
284a4abeea4SJosef Bacik 
285a4abeea4SJosef Bacik 	if (type == TRANS_START &&
286a4abeea4SJosef Bacik 	    !atomic_read(&root->fs_info->open_ioctl_trans))
287a4abeea4SJosef Bacik 		return 1;
288a4abeea4SJosef Bacik 
289a22285a6SYan, Zheng 	return 0;
290a22285a6SYan, Zheng }
291a22285a6SYan, Zheng 
292a22285a6SYan, Zheng static struct btrfs_trans_handle *start_transaction(struct btrfs_root *root,
293a22285a6SYan, Zheng 						    u64 num_items, int type)
294a22285a6SYan, Zheng {
295a22285a6SYan, Zheng 	struct btrfs_trans_handle *h;
296a22285a6SYan, Zheng 	struct btrfs_transaction *cur_trans;
297b5009945SJosef Bacik 	u64 num_bytes = 0;
298a22285a6SYan, Zheng 	int ret;
299c5567237SArne Jansen 	u64 qgroup_reserved = 0;
300acce952bSliubo 
301acce952bSliubo 	if (root->fs_info->fs_state & BTRFS_SUPER_FLAG_ERROR)
302acce952bSliubo 		return ERR_PTR(-EROFS);
3032a1eb461SJosef Bacik 
3042a1eb461SJosef Bacik 	if (current->journal_info) {
3052a1eb461SJosef Bacik 		WARN_ON(type != TRANS_JOIN && type != TRANS_JOIN_NOLOCK);
3062a1eb461SJosef Bacik 		h = current->journal_info;
3072a1eb461SJosef Bacik 		h->use_count++;
3082a1eb461SJosef Bacik 		h->orig_rsv = h->block_rsv;
3092a1eb461SJosef Bacik 		h->block_rsv = NULL;
3102a1eb461SJosef Bacik 		goto got_it;
3112a1eb461SJosef Bacik 	}
312b5009945SJosef Bacik 
313b5009945SJosef Bacik 	/*
314b5009945SJosef Bacik 	 * Do the reservation before we join the transaction so we can do all
315b5009945SJosef Bacik 	 * the appropriate flushing if need be.
316b5009945SJosef Bacik 	 */
317b5009945SJosef Bacik 	if (num_items > 0 && root != root->fs_info->chunk_root) {
318c5567237SArne Jansen 		if (root->fs_info->quota_enabled &&
319c5567237SArne Jansen 		    is_fstree(root->root_key.objectid)) {
320c5567237SArne Jansen 			qgroup_reserved = num_items * root->leafsize;
321c5567237SArne Jansen 			ret = btrfs_qgroup_reserve(root, qgroup_reserved);
322c5567237SArne Jansen 			if (ret)
323c5567237SArne Jansen 				return ERR_PTR(ret);
324c5567237SArne Jansen 		}
325c5567237SArne Jansen 
326b5009945SJosef Bacik 		num_bytes = btrfs_calc_trans_metadata_size(root, num_items);
3274a92b1b8SJosef Bacik 		ret = btrfs_block_rsv_add(root,
328b5009945SJosef Bacik 					  &root->fs_info->trans_block_rsv,
329b5009945SJosef Bacik 					  num_bytes);
330b5009945SJosef Bacik 		if (ret)
331b5009945SJosef Bacik 			return ERR_PTR(ret);
332b5009945SJosef Bacik 	}
333a22285a6SYan, Zheng again:
334a22285a6SYan, Zheng 	h = kmem_cache_alloc(btrfs_trans_handle_cachep, GFP_NOFS);
335a22285a6SYan, Zheng 	if (!h)
336a22285a6SYan, Zheng 		return ERR_PTR(-ENOMEM);
337a22285a6SYan, Zheng 
338b2b5ef5cSJan Kara 	sb_start_intwrite(root->fs_info->sb);
339b2b5ef5cSJan Kara 
340a22285a6SYan, Zheng 	if (may_wait_transaction(root, type))
34137d1aeeeSChris Mason 		wait_current_trans(root);
342a22285a6SYan, Zheng 
343a4abeea4SJosef Bacik 	do {
344a4abeea4SJosef Bacik 		ret = join_transaction(root, type == TRANS_JOIN_NOLOCK);
345a4abeea4SJosef Bacik 		if (ret == -EBUSY)
346a4abeea4SJosef Bacik 			wait_current_trans(root);
347a4abeea4SJosef Bacik 	} while (ret == -EBUSY);
348a4abeea4SJosef Bacik 
349db5b493aSTsutomu Itoh 	if (ret < 0) {
350b2b5ef5cSJan Kara 		sb_end_intwrite(root->fs_info->sb);
3516e8df2aeSYoshinori Sano 		kmem_cache_free(btrfs_trans_handle_cachep, h);
352db5b493aSTsutomu Itoh 		return ERR_PTR(ret);
353db5b493aSTsutomu Itoh 	}
3540f7d52f4SChris Mason 
355a22285a6SYan, Zheng 	cur_trans = root->fs_info->running_transaction;
356a22285a6SYan, Zheng 
357a22285a6SYan, Zheng 	h->transid = cur_trans->transid;
358a22285a6SYan, Zheng 	h->transaction = cur_trans;
35979154b1bSChris Mason 	h->blocks_used = 0;
360a22285a6SYan, Zheng 	h->bytes_reserved = 0;
361d13603efSArne Jansen 	h->root = root;
36256bec294SChris Mason 	h->delayed_ref_updates = 0;
3632a1eb461SJosef Bacik 	h->use_count = 1;
3640e721106SJosef Bacik 	h->adding_csums = 0;
365f0486c68SYan, Zheng 	h->block_rsv = NULL;
3662a1eb461SJosef Bacik 	h->orig_rsv = NULL;
36749b25e05SJeff Mahoney 	h->aborted = 0;
368c5567237SArne Jansen 	h->qgroup_reserved = qgroup_reserved;
369bed92eaeSArne Jansen 	h->delayed_ref_elem.seq = 0;
370bed92eaeSArne Jansen 	INIT_LIST_HEAD(&h->qgroup_ref_list);
371b7ec40d7SChris Mason 
372a22285a6SYan, Zheng 	smp_mb();
373a22285a6SYan, Zheng 	if (cur_trans->blocked && may_wait_transaction(root, type)) {
374a22285a6SYan, Zheng 		btrfs_commit_transaction(h, root);
375a22285a6SYan, Zheng 		goto again;
376a22285a6SYan, Zheng 	}
3779ed74f2dSJosef Bacik 
378b5009945SJosef Bacik 	if (num_bytes) {
3798c2a3ca2SJosef Bacik 		trace_btrfs_space_reservation(root->fs_info, "transaction",
3802bcc0328SLiu Bo 					      h->transid, num_bytes, 1);
381b5009945SJosef Bacik 		h->block_rsv = &root->fs_info->trans_block_rsv;
382b5009945SJosef Bacik 		h->bytes_reserved = num_bytes;
383a22285a6SYan, Zheng 	}
384a22285a6SYan, Zheng 
3852a1eb461SJosef Bacik got_it:
386a4abeea4SJosef Bacik 	btrfs_record_root_in_trans(h, root);
387a22285a6SYan, Zheng 
388a22285a6SYan, Zheng 	if (!current->journal_info && type != TRANS_USERSPACE)
389a22285a6SYan, Zheng 		current->journal_info = h;
39079154b1bSChris Mason 	return h;
39179154b1bSChris Mason }
39279154b1bSChris Mason 
393f9295749SChris Mason struct btrfs_trans_handle *btrfs_start_transaction(struct btrfs_root *root,
394a22285a6SYan, Zheng 						   int num_items)
395f9295749SChris Mason {
396a22285a6SYan, Zheng 	return start_transaction(root, num_items, TRANS_START);
397f9295749SChris Mason }
3987a7eaa40SJosef Bacik struct btrfs_trans_handle *btrfs_join_transaction(struct btrfs_root *root)
399f9295749SChris Mason {
400a22285a6SYan, Zheng 	return start_transaction(root, 0, TRANS_JOIN);
401f9295749SChris Mason }
402f9295749SChris Mason 
4037a7eaa40SJosef Bacik struct btrfs_trans_handle *btrfs_join_transaction_nolock(struct btrfs_root *root)
4040af3d00bSJosef Bacik {
4050af3d00bSJosef Bacik 	return start_transaction(root, 0, TRANS_JOIN_NOLOCK);
4060af3d00bSJosef Bacik }
4070af3d00bSJosef Bacik 
4087a7eaa40SJosef Bacik struct btrfs_trans_handle *btrfs_start_ioctl_transaction(struct btrfs_root *root)
4099ca9ee09SSage Weil {
4107a7eaa40SJosef Bacik 	return start_transaction(root, 0, TRANS_USERSPACE);
4119ca9ee09SSage Weil }
4129ca9ee09SSage Weil 
413d352ac68SChris Mason /* wait for a transaction commit to be fully complete */
414b9c8300cSLi Zefan static noinline void wait_for_commit(struct btrfs_root *root,
41589ce8a63SChris Mason 				    struct btrfs_transaction *commit)
41689ce8a63SChris Mason {
41772d63ed6SLi Zefan 	wait_event(commit->commit_wait, commit->commit_done);
41889ce8a63SChris Mason }
41989ce8a63SChris Mason 
42046204592SSage Weil int btrfs_wait_for_commit(struct btrfs_root *root, u64 transid)
42146204592SSage Weil {
42246204592SSage Weil 	struct btrfs_transaction *cur_trans = NULL, *t;
42346204592SSage Weil 	int ret;
42446204592SSage Weil 
42546204592SSage Weil 	ret = 0;
42646204592SSage Weil 	if (transid) {
42746204592SSage Weil 		if (transid <= root->fs_info->last_trans_committed)
428a4abeea4SJosef Bacik 			goto out;
42946204592SSage Weil 
43046204592SSage Weil 		/* find specified transaction */
431a4abeea4SJosef Bacik 		spin_lock(&root->fs_info->trans_lock);
43246204592SSage Weil 		list_for_each_entry(t, &root->fs_info->trans_list, list) {
43346204592SSage Weil 			if (t->transid == transid) {
43446204592SSage Weil 				cur_trans = t;
435a4abeea4SJosef Bacik 				atomic_inc(&cur_trans->use_count);
43646204592SSage Weil 				break;
43746204592SSage Weil 			}
43846204592SSage Weil 			if (t->transid > transid)
43946204592SSage Weil 				break;
44046204592SSage Weil 		}
441a4abeea4SJosef Bacik 		spin_unlock(&root->fs_info->trans_lock);
44246204592SSage Weil 		ret = -EINVAL;
44346204592SSage Weil 		if (!cur_trans)
444a4abeea4SJosef Bacik 			goto out;  /* bad transid */
44546204592SSage Weil 	} else {
44646204592SSage Weil 		/* find newest transaction that is committing | committed */
447a4abeea4SJosef Bacik 		spin_lock(&root->fs_info->trans_lock);
44846204592SSage Weil 		list_for_each_entry_reverse(t, &root->fs_info->trans_list,
44946204592SSage Weil 					    list) {
45046204592SSage Weil 			if (t->in_commit) {
45146204592SSage Weil 				if (t->commit_done)
4523473f3c0SJosef Bacik 					break;
45346204592SSage Weil 				cur_trans = t;
454a4abeea4SJosef Bacik 				atomic_inc(&cur_trans->use_count);
45546204592SSage Weil 				break;
45646204592SSage Weil 			}
45746204592SSage Weil 		}
458a4abeea4SJosef Bacik 		spin_unlock(&root->fs_info->trans_lock);
45946204592SSage Weil 		if (!cur_trans)
460a4abeea4SJosef Bacik 			goto out;  /* nothing committing|committed */
46146204592SSage Weil 	}
46246204592SSage Weil 
46346204592SSage Weil 	wait_for_commit(root, cur_trans);
46446204592SSage Weil 
46546204592SSage Weil 	put_transaction(cur_trans);
46646204592SSage Weil 	ret = 0;
467a4abeea4SJosef Bacik out:
46846204592SSage Weil 	return ret;
46946204592SSage Weil }
47046204592SSage Weil 
47137d1aeeeSChris Mason void btrfs_throttle(struct btrfs_root *root)
47237d1aeeeSChris Mason {
473a4abeea4SJosef Bacik 	if (!atomic_read(&root->fs_info->open_ioctl_trans))
47437d1aeeeSChris Mason 		wait_current_trans(root);
47537d1aeeeSChris Mason }
47637d1aeeeSChris Mason 
4778929ecfaSYan, Zheng static int should_end_transaction(struct btrfs_trans_handle *trans,
4788929ecfaSYan, Zheng 				  struct btrfs_root *root)
4798929ecfaSYan, Zheng {
4808929ecfaSYan, Zheng 	int ret;
48136ba022aSJosef Bacik 
48236ba022aSJosef Bacik 	ret = btrfs_block_rsv_check(root, &root->fs_info->global_block_rsv, 5);
4838929ecfaSYan, Zheng 	return ret ? 1 : 0;
4848929ecfaSYan, Zheng }
4858929ecfaSYan, Zheng 
4868929ecfaSYan, Zheng int btrfs_should_end_transaction(struct btrfs_trans_handle *trans,
4878929ecfaSYan, Zheng 				 struct btrfs_root *root)
4888929ecfaSYan, Zheng {
4898929ecfaSYan, Zheng 	struct btrfs_transaction *cur_trans = trans->transaction;
4908929ecfaSYan, Zheng 	int updates;
49149b25e05SJeff Mahoney 	int err;
4928929ecfaSYan, Zheng 
493a4abeea4SJosef Bacik 	smp_mb();
4948929ecfaSYan, Zheng 	if (cur_trans->blocked || cur_trans->delayed_refs.flushing)
4958929ecfaSYan, Zheng 		return 1;
4968929ecfaSYan, Zheng 
4978929ecfaSYan, Zheng 	updates = trans->delayed_ref_updates;
4988929ecfaSYan, Zheng 	trans->delayed_ref_updates = 0;
49949b25e05SJeff Mahoney 	if (updates) {
50049b25e05SJeff Mahoney 		err = btrfs_run_delayed_refs(trans, root, updates);
50149b25e05SJeff Mahoney 		if (err) /* Error code will also eval true */
50249b25e05SJeff Mahoney 			return err;
50349b25e05SJeff Mahoney 	}
5048929ecfaSYan, Zheng 
5058929ecfaSYan, Zheng 	return should_end_transaction(trans, root);
5068929ecfaSYan, Zheng }
5078929ecfaSYan, Zheng 
50889ce8a63SChris Mason static int __btrfs_end_transaction(struct btrfs_trans_handle *trans,
5090af3d00bSJosef Bacik 			  struct btrfs_root *root, int throttle, int lock)
51079154b1bSChris Mason {
5118929ecfaSYan, Zheng 	struct btrfs_transaction *cur_trans = trans->transaction;
512ab78c84dSChris Mason 	struct btrfs_fs_info *info = root->fs_info;
513c3e69d58SChris Mason 	int count = 0;
5144edc2ca3SDave Jones 	int err = 0;
515d6e4a428SChris Mason 
5162a1eb461SJosef Bacik 	if (--trans->use_count) {
5172a1eb461SJosef Bacik 		trans->block_rsv = trans->orig_rsv;
5182a1eb461SJosef Bacik 		return 0;
5192a1eb461SJosef Bacik 	}
5202a1eb461SJosef Bacik 
521edf39272SJan Schmidt 	/*
522edf39272SJan Schmidt 	 * do the qgroup accounting as early as possible
523edf39272SJan Schmidt 	 */
524edf39272SJan Schmidt 	err = btrfs_delayed_refs_qgroup_accounting(trans, info);
525edf39272SJan Schmidt 
526b24e03dbSJosef Bacik 	btrfs_trans_release_metadata(trans, root);
5274c13d758SJosef Bacik 	trans->block_rsv = NULL;
528d13603efSArne Jansen 	/*
529d13603efSArne Jansen 	 * the same root has to be passed to start_transaction and
530d13603efSArne Jansen 	 * end_transaction. Subvolume quota depends on this.
531d13603efSArne Jansen 	 */
532d13603efSArne Jansen 	WARN_ON(trans->root != root);
533c5567237SArne Jansen 
534c5567237SArne Jansen 	if (trans->qgroup_reserved) {
535c5567237SArne Jansen 		btrfs_qgroup_free(root, trans->qgroup_reserved);
536c5567237SArne Jansen 		trans->qgroup_reserved = 0;
537c5567237SArne Jansen 	}
538c5567237SArne Jansen 
539203bf287SChris Mason 	while (count < 2) {
540c3e69d58SChris Mason 		unsigned long cur = trans->delayed_ref_updates;
541c3e69d58SChris Mason 		trans->delayed_ref_updates = 0;
542c3e69d58SChris Mason 		if (cur &&
543c3e69d58SChris Mason 		    trans->transaction->delayed_refs.num_heads_ready > 64) {
544c3e69d58SChris Mason 			trans->delayed_ref_updates = 0;
545c3e69d58SChris Mason 			btrfs_run_delayed_refs(trans, root, cur);
546c3e69d58SChris Mason 		} else {
547c3e69d58SChris Mason 			break;
548c3e69d58SChris Mason 		}
549c3e69d58SChris Mason 		count++;
55056bec294SChris Mason 	}
5510e721106SJosef Bacik 	btrfs_trans_release_metadata(trans, root);
5520e721106SJosef Bacik 	trans->block_rsv = NULL;
55356bec294SChris Mason 
554b2b5ef5cSJan Kara 	sb_end_intwrite(root->fs_info->sb);
555b2b5ef5cSJan Kara 
556a4abeea4SJosef Bacik 	if (lock && !atomic_read(&root->fs_info->open_ioctl_trans) &&
557a4abeea4SJosef Bacik 	    should_end_transaction(trans, root)) {
5588929ecfaSYan, Zheng 		trans->transaction->blocked = 1;
559a4abeea4SJosef Bacik 		smp_wmb();
560a4abeea4SJosef Bacik 	}
5618929ecfaSYan, Zheng 
5620af3d00bSJosef Bacik 	if (lock && cur_trans->blocked && !cur_trans->in_commit) {
56381317fdeSJosef Bacik 		if (throttle) {
56481317fdeSJosef Bacik 			/*
56581317fdeSJosef Bacik 			 * We may race with somebody else here so end up having
56681317fdeSJosef Bacik 			 * to call end_transaction on ourselves again, so inc
56781317fdeSJosef Bacik 			 * our use_count.
56881317fdeSJosef Bacik 			 */
56981317fdeSJosef Bacik 			trans->use_count++;
5708929ecfaSYan, Zheng 			return btrfs_commit_transaction(trans, root);
57181317fdeSJosef Bacik 		} else {
5728929ecfaSYan, Zheng 			wake_up_process(info->transaction_kthread);
5738929ecfaSYan, Zheng 		}
57481317fdeSJosef Bacik 	}
5758929ecfaSYan, Zheng 
5768929ecfaSYan, Zheng 	WARN_ON(cur_trans != info->running_transaction);
57713c5a93eSJosef Bacik 	WARN_ON(atomic_read(&cur_trans->num_writers) < 1);
57813c5a93eSJosef Bacik 	atomic_dec(&cur_trans->num_writers);
57989ce8a63SChris Mason 
58099d16cbcSSage Weil 	smp_mb();
58179154b1bSChris Mason 	if (waitqueue_active(&cur_trans->writer_wait))
58279154b1bSChris Mason 		wake_up(&cur_trans->writer_wait);
58379154b1bSChris Mason 	put_transaction(cur_trans);
5849ed74f2dSJosef Bacik 
5859ed74f2dSJosef Bacik 	if (current->journal_info == trans)
5869ed74f2dSJosef Bacik 		current->journal_info = NULL;
587ab78c84dSChris Mason 
58824bbcf04SYan, Zheng 	if (throttle)
58924bbcf04SYan, Zheng 		btrfs_run_delayed_iputs(root);
59024bbcf04SYan, Zheng 
59149b25e05SJeff Mahoney 	if (trans->aborted ||
59249b25e05SJeff Mahoney 	    root->fs_info->fs_state & BTRFS_SUPER_FLAG_ERROR) {
5934edc2ca3SDave Jones 		err = -EIO;
59449b25e05SJeff Mahoney 	}
595edf39272SJan Schmidt 	assert_qgroups_uptodate(trans);
59649b25e05SJeff Mahoney 
5974edc2ca3SDave Jones 	memset(trans, 0, sizeof(*trans));
5984edc2ca3SDave Jones 	kmem_cache_free(btrfs_trans_handle_cachep, trans);
5994edc2ca3SDave Jones 	return err;
60079154b1bSChris Mason }
60179154b1bSChris Mason 
60289ce8a63SChris Mason int btrfs_end_transaction(struct btrfs_trans_handle *trans,
60389ce8a63SChris Mason 			  struct btrfs_root *root)
60489ce8a63SChris Mason {
60516cdcec7SMiao Xie 	int ret;
60616cdcec7SMiao Xie 
60716cdcec7SMiao Xie 	ret = __btrfs_end_transaction(trans, root, 0, 1);
60816cdcec7SMiao Xie 	if (ret)
60916cdcec7SMiao Xie 		return ret;
61016cdcec7SMiao Xie 	return 0;
61189ce8a63SChris Mason }
61289ce8a63SChris Mason 
61389ce8a63SChris Mason int btrfs_end_transaction_throttle(struct btrfs_trans_handle *trans,
61489ce8a63SChris Mason 				   struct btrfs_root *root)
61589ce8a63SChris Mason {
61616cdcec7SMiao Xie 	int ret;
61716cdcec7SMiao Xie 
61816cdcec7SMiao Xie 	ret = __btrfs_end_transaction(trans, root, 1, 1);
61916cdcec7SMiao Xie 	if (ret)
62016cdcec7SMiao Xie 		return ret;
62116cdcec7SMiao Xie 	return 0;
6220af3d00bSJosef Bacik }
6230af3d00bSJosef Bacik 
6240af3d00bSJosef Bacik int btrfs_end_transaction_nolock(struct btrfs_trans_handle *trans,
6250af3d00bSJosef Bacik 				 struct btrfs_root *root)
6260af3d00bSJosef Bacik {
62716cdcec7SMiao Xie 	int ret;
62816cdcec7SMiao Xie 
62916cdcec7SMiao Xie 	ret = __btrfs_end_transaction(trans, root, 0, 0);
63016cdcec7SMiao Xie 	if (ret)
63116cdcec7SMiao Xie 		return ret;
63216cdcec7SMiao Xie 	return 0;
63316cdcec7SMiao Xie }
63416cdcec7SMiao Xie 
63516cdcec7SMiao Xie int btrfs_end_transaction_dmeta(struct btrfs_trans_handle *trans,
63616cdcec7SMiao Xie 				struct btrfs_root *root)
63716cdcec7SMiao Xie {
63816cdcec7SMiao Xie 	return __btrfs_end_transaction(trans, root, 1, 1);
63989ce8a63SChris Mason }
64089ce8a63SChris Mason 
641d352ac68SChris Mason /*
642d352ac68SChris Mason  * when btree blocks are allocated, they have some corresponding bits set for
643d352ac68SChris Mason  * them in one of two extent_io trees.  This is used to make sure all of
644690587d1SChris Mason  * those extents are sent to disk but does not wait on them
645d352ac68SChris Mason  */
646690587d1SChris Mason int btrfs_write_marked_extents(struct btrfs_root *root,
6478cef4e16SYan, Zheng 			       struct extent_io_tree *dirty_pages, int mark)
64879154b1bSChris Mason {
649777e6bd7SChris Mason 	int err = 0;
6507c4452b9SChris Mason 	int werr = 0;
6511728366eSJosef Bacik 	struct address_space *mapping = root->fs_info->btree_inode->i_mapping;
652777e6bd7SChris Mason 	u64 start = 0;
6535f39d397SChris Mason 	u64 end;
6547c4452b9SChris Mason 
6551728366eSJosef Bacik 	while (!find_first_extent_bit(dirty_pages, start, &start, &end,
6561728366eSJosef Bacik 				      mark)) {
6571728366eSJosef Bacik 		convert_extent_bit(dirty_pages, start, end, EXTENT_NEED_WAIT, mark,
6581728366eSJosef Bacik 				   GFP_NOFS);
6591728366eSJosef Bacik 		err = filemap_fdatawrite_range(mapping, start, end);
6607c4452b9SChris Mason 		if (err)
6617c4452b9SChris Mason 			werr = err;
6621728366eSJosef Bacik 		cond_resched();
6631728366eSJosef Bacik 		start = end + 1;
6647c4452b9SChris Mason 	}
665690587d1SChris Mason 	if (err)
666690587d1SChris Mason 		werr = err;
667690587d1SChris Mason 	return werr;
668690587d1SChris Mason }
669690587d1SChris Mason 
670690587d1SChris Mason /*
671690587d1SChris Mason  * when btree blocks are allocated, they have some corresponding bits set for
672690587d1SChris Mason  * them in one of two extent_io trees.  This is used to make sure all of
673690587d1SChris Mason  * those extents are on disk for transaction or log commit.  We wait
674690587d1SChris Mason  * on all the pages and clear them from the dirty pages state tree
675690587d1SChris Mason  */
676690587d1SChris Mason int btrfs_wait_marked_extents(struct btrfs_root *root,
6778cef4e16SYan, Zheng 			      struct extent_io_tree *dirty_pages, int mark)
678690587d1SChris Mason {
679690587d1SChris Mason 	int err = 0;
680690587d1SChris Mason 	int werr = 0;
6811728366eSJosef Bacik 	struct address_space *mapping = root->fs_info->btree_inode->i_mapping;
682690587d1SChris Mason 	u64 start = 0;
683690587d1SChris Mason 	u64 end;
684690587d1SChris Mason 
6851728366eSJosef Bacik 	while (!find_first_extent_bit(dirty_pages, start, &start, &end,
6861728366eSJosef Bacik 				      EXTENT_NEED_WAIT)) {
6871728366eSJosef Bacik 		clear_extent_bits(dirty_pages, start, end, EXTENT_NEED_WAIT, GFP_NOFS);
6881728366eSJosef Bacik 		err = filemap_fdatawait_range(mapping, start, end);
689777e6bd7SChris Mason 		if (err)
690777e6bd7SChris Mason 			werr = err;
691777e6bd7SChris Mason 		cond_resched();
6921728366eSJosef Bacik 		start = end + 1;
693777e6bd7SChris Mason 	}
6947c4452b9SChris Mason 	if (err)
6957c4452b9SChris Mason 		werr = err;
6967c4452b9SChris Mason 	return werr;
69779154b1bSChris Mason }
69879154b1bSChris Mason 
699690587d1SChris Mason /*
700690587d1SChris Mason  * when btree blocks are allocated, they have some corresponding bits set for
701690587d1SChris Mason  * them in one of two extent_io trees.  This is used to make sure all of
702690587d1SChris Mason  * those extents are on disk for transaction or log commit
703690587d1SChris Mason  */
704690587d1SChris Mason int btrfs_write_and_wait_marked_extents(struct btrfs_root *root,
7058cef4e16SYan, Zheng 				struct extent_io_tree *dirty_pages, int mark)
706690587d1SChris Mason {
707690587d1SChris Mason 	int ret;
708690587d1SChris Mason 	int ret2;
709690587d1SChris Mason 
7108cef4e16SYan, Zheng 	ret = btrfs_write_marked_extents(root, dirty_pages, mark);
7118cef4e16SYan, Zheng 	ret2 = btrfs_wait_marked_extents(root, dirty_pages, mark);
712bf0da8c1SChris Mason 
713bf0da8c1SChris Mason 	if (ret)
714bf0da8c1SChris Mason 		return ret;
715bf0da8c1SChris Mason 	if (ret2)
716bf0da8c1SChris Mason 		return ret2;
717bf0da8c1SChris Mason 	return 0;
718690587d1SChris Mason }
719690587d1SChris Mason 
720d0c803c4SChris Mason int btrfs_write_and_wait_transaction(struct btrfs_trans_handle *trans,
721d0c803c4SChris Mason 				     struct btrfs_root *root)
722d0c803c4SChris Mason {
723d0c803c4SChris Mason 	if (!trans || !trans->transaction) {
724d0c803c4SChris Mason 		struct inode *btree_inode;
725d0c803c4SChris Mason 		btree_inode = root->fs_info->btree_inode;
726d0c803c4SChris Mason 		return filemap_write_and_wait(btree_inode->i_mapping);
727d0c803c4SChris Mason 	}
728d0c803c4SChris Mason 	return btrfs_write_and_wait_marked_extents(root,
7298cef4e16SYan, Zheng 					   &trans->transaction->dirty_pages,
7308cef4e16SYan, Zheng 					   EXTENT_DIRTY);
731d0c803c4SChris Mason }
732d0c803c4SChris Mason 
733d352ac68SChris Mason /*
734d352ac68SChris Mason  * this is used to update the root pointer in the tree of tree roots.
735d352ac68SChris Mason  *
736d352ac68SChris Mason  * But, in the case of the extent allocation tree, updating the root
737d352ac68SChris Mason  * pointer may allocate blocks which may change the root of the extent
738d352ac68SChris Mason  * allocation tree.
739d352ac68SChris Mason  *
740d352ac68SChris Mason  * So, this loops and repeats and makes sure the cowonly root didn't
741d352ac68SChris Mason  * change while the root pointer was being updated in the metadata.
742d352ac68SChris Mason  */
7430b86a832SChris Mason static int update_cowonly_root(struct btrfs_trans_handle *trans,
74479154b1bSChris Mason 			       struct btrfs_root *root)
74579154b1bSChris Mason {
74679154b1bSChris Mason 	int ret;
7470b86a832SChris Mason 	u64 old_root_bytenr;
74886b9f2ecSYan, Zheng 	u64 old_root_used;
7490b86a832SChris Mason 	struct btrfs_root *tree_root = root->fs_info->tree_root;
75079154b1bSChris Mason 
75186b9f2ecSYan, Zheng 	old_root_used = btrfs_root_used(&root->root_item);
7520b86a832SChris Mason 	btrfs_write_dirty_block_groups(trans, root);
75356bec294SChris Mason 
75479154b1bSChris Mason 	while (1) {
7550b86a832SChris Mason 		old_root_bytenr = btrfs_root_bytenr(&root->root_item);
75686b9f2ecSYan, Zheng 		if (old_root_bytenr == root->node->start &&
75786b9f2ecSYan, Zheng 		    old_root_used == btrfs_root_used(&root->root_item))
75879154b1bSChris Mason 			break;
75987ef2bb4SChris Mason 
7605d4f98a2SYan Zheng 		btrfs_set_root_node(&root->root_item, root->node);
76179154b1bSChris Mason 		ret = btrfs_update_root(trans, tree_root,
7620b86a832SChris Mason 					&root->root_key,
7630b86a832SChris Mason 					&root->root_item);
76449b25e05SJeff Mahoney 		if (ret)
76549b25e05SJeff Mahoney 			return ret;
76656bec294SChris Mason 
76786b9f2ecSYan, Zheng 		old_root_used = btrfs_root_used(&root->root_item);
7684a8c9a62SYan Zheng 		ret = btrfs_write_dirty_block_groups(trans, root);
76949b25e05SJeff Mahoney 		if (ret)
77049b25e05SJeff Mahoney 			return ret;
7710b86a832SChris Mason 	}
772276e680dSYan Zheng 
773276e680dSYan Zheng 	if (root != root->fs_info->extent_root)
774817d52f8SJosef Bacik 		switch_commit_root(root);
775276e680dSYan Zheng 
7760b86a832SChris Mason 	return 0;
7770b86a832SChris Mason }
7780b86a832SChris Mason 
779d352ac68SChris Mason /*
780d352ac68SChris Mason  * update all the cowonly tree roots on disk
78149b25e05SJeff Mahoney  *
78249b25e05SJeff Mahoney  * The error handling in this function may not be obvious. Any of the
78349b25e05SJeff Mahoney  * failures will cause the file system to go offline. We still need
78449b25e05SJeff Mahoney  * to clean up the delayed refs.
785d352ac68SChris Mason  */
7865d4f98a2SYan Zheng static noinline int commit_cowonly_roots(struct btrfs_trans_handle *trans,
7870b86a832SChris Mason 					 struct btrfs_root *root)
7880b86a832SChris Mason {
7890b86a832SChris Mason 	struct btrfs_fs_info *fs_info = root->fs_info;
7900b86a832SChris Mason 	struct list_head *next;
79184234f3aSYan Zheng 	struct extent_buffer *eb;
79256bec294SChris Mason 	int ret;
79384234f3aSYan Zheng 
79456bec294SChris Mason 	ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
79549b25e05SJeff Mahoney 	if (ret)
79649b25e05SJeff Mahoney 		return ret;
79787ef2bb4SChris Mason 
79884234f3aSYan Zheng 	eb = btrfs_lock_root_node(fs_info->tree_root);
79949b25e05SJeff Mahoney 	ret = btrfs_cow_block(trans, fs_info->tree_root, eb, NULL,
80049b25e05SJeff Mahoney 			      0, &eb);
80184234f3aSYan Zheng 	btrfs_tree_unlock(eb);
80284234f3aSYan Zheng 	free_extent_buffer(eb);
8030b86a832SChris Mason 
80449b25e05SJeff Mahoney 	if (ret)
80549b25e05SJeff Mahoney 		return ret;
80649b25e05SJeff Mahoney 
80756bec294SChris Mason 	ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
80849b25e05SJeff Mahoney 	if (ret)
80949b25e05SJeff Mahoney 		return ret;
81087ef2bb4SChris Mason 
811733f4fbbSStefan Behrens 	ret = btrfs_run_dev_stats(trans, root->fs_info);
812733f4fbbSStefan Behrens 	BUG_ON(ret);
813733f4fbbSStefan Behrens 
814546adb0dSJan Schmidt 	ret = btrfs_run_qgroups(trans, root->fs_info);
815546adb0dSJan Schmidt 	BUG_ON(ret);
816546adb0dSJan Schmidt 
817546adb0dSJan Schmidt 	/* run_qgroups might have added some more refs */
818546adb0dSJan Schmidt 	ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
819546adb0dSJan Schmidt 	BUG_ON(ret);
820546adb0dSJan Schmidt 
8210b86a832SChris Mason 	while (!list_empty(&fs_info->dirty_cowonly_roots)) {
8220b86a832SChris Mason 		next = fs_info->dirty_cowonly_roots.next;
8230b86a832SChris Mason 		list_del_init(next);
8240b86a832SChris Mason 		root = list_entry(next, struct btrfs_root, dirty_list);
82587ef2bb4SChris Mason 
82649b25e05SJeff Mahoney 		ret = update_cowonly_root(trans, root);
82749b25e05SJeff Mahoney 		if (ret)
82849b25e05SJeff Mahoney 			return ret;
82979154b1bSChris Mason 	}
830276e680dSYan Zheng 
831276e680dSYan Zheng 	down_write(&fs_info->extent_commit_sem);
832276e680dSYan Zheng 	switch_commit_root(fs_info->extent_root);
833276e680dSYan Zheng 	up_write(&fs_info->extent_commit_sem);
834276e680dSYan Zheng 
83579154b1bSChris Mason 	return 0;
83679154b1bSChris Mason }
83779154b1bSChris Mason 
838d352ac68SChris Mason /*
839d352ac68SChris Mason  * dead roots are old snapshots that need to be deleted.  This allocates
840d352ac68SChris Mason  * a dirty root struct and adds it into the list of dead roots that need to
841d352ac68SChris Mason  * be deleted
842d352ac68SChris Mason  */
8435d4f98a2SYan Zheng int btrfs_add_dead_root(struct btrfs_root *root)
8445eda7b5eSChris Mason {
845a4abeea4SJosef Bacik 	spin_lock(&root->fs_info->trans_lock);
8465d4f98a2SYan Zheng 	list_add(&root->root_list, &root->fs_info->dead_roots);
847a4abeea4SJosef Bacik 	spin_unlock(&root->fs_info->trans_lock);
8485eda7b5eSChris Mason 	return 0;
8495eda7b5eSChris Mason }
8505eda7b5eSChris Mason 
851d352ac68SChris Mason /*
8525d4f98a2SYan Zheng  * update all the cowonly tree roots on disk
853d352ac68SChris Mason  */
8545d4f98a2SYan Zheng static noinline int commit_fs_roots(struct btrfs_trans_handle *trans,
8555d4f98a2SYan Zheng 				    struct btrfs_root *root)
8560f7d52f4SChris Mason {
8570f7d52f4SChris Mason 	struct btrfs_root *gang[8];
8585d4f98a2SYan Zheng 	struct btrfs_fs_info *fs_info = root->fs_info;
8590f7d52f4SChris Mason 	int i;
8600f7d52f4SChris Mason 	int ret;
86154aa1f4dSChris Mason 	int err = 0;
86254aa1f4dSChris Mason 
863a4abeea4SJosef Bacik 	spin_lock(&fs_info->fs_roots_radix_lock);
8640f7d52f4SChris Mason 	while (1) {
8655d4f98a2SYan Zheng 		ret = radix_tree_gang_lookup_tag(&fs_info->fs_roots_radix,
8665d4f98a2SYan Zheng 						 (void **)gang, 0,
8670f7d52f4SChris Mason 						 ARRAY_SIZE(gang),
8680f7d52f4SChris Mason 						 BTRFS_ROOT_TRANS_TAG);
8690f7d52f4SChris Mason 		if (ret == 0)
8700f7d52f4SChris Mason 			break;
8710f7d52f4SChris Mason 		for (i = 0; i < ret; i++) {
8720f7d52f4SChris Mason 			root = gang[i];
8735d4f98a2SYan Zheng 			radix_tree_tag_clear(&fs_info->fs_roots_radix,
8742619ba1fSChris Mason 					(unsigned long)root->root_key.objectid,
8750f7d52f4SChris Mason 					BTRFS_ROOT_TRANS_TAG);
876a4abeea4SJosef Bacik 			spin_unlock(&fs_info->fs_roots_radix_lock);
87731153d81SYan Zheng 
878e02119d5SChris Mason 			btrfs_free_log(trans, root);
8795d4f98a2SYan Zheng 			btrfs_update_reloc_root(trans, root);
880d68fc57bSYan, Zheng 			btrfs_orphan_commit_root(trans, root);
881e02119d5SChris Mason 
88282d5902dSLi Zefan 			btrfs_save_ino_cache(root, trans);
88382d5902dSLi Zefan 
884f1ebcc74SLiu Bo 			/* see comments in should_cow_block() */
885f1ebcc74SLiu Bo 			root->force_cow = 0;
886f1ebcc74SLiu Bo 			smp_wmb();
887f1ebcc74SLiu Bo 
888978d910dSYan Zheng 			if (root->commit_root != root->node) {
889581bb050SLi Zefan 				mutex_lock(&root->fs_commit_mutex);
890817d52f8SJosef Bacik 				switch_commit_root(root);
891581bb050SLi Zefan 				btrfs_unpin_free_ino(root);
892581bb050SLi Zefan 				mutex_unlock(&root->fs_commit_mutex);
893581bb050SLi Zefan 
894978d910dSYan Zheng 				btrfs_set_root_node(&root->root_item,
895978d910dSYan Zheng 						    root->node);
896978d910dSYan Zheng 			}
89731153d81SYan Zheng 
8985d4f98a2SYan Zheng 			err = btrfs_update_root(trans, fs_info->tree_root,
8990f7d52f4SChris Mason 						&root->root_key,
9000f7d52f4SChris Mason 						&root->root_item);
901a4abeea4SJosef Bacik 			spin_lock(&fs_info->fs_roots_radix_lock);
90254aa1f4dSChris Mason 			if (err)
90354aa1f4dSChris Mason 				break;
9040f7d52f4SChris Mason 		}
9059f3a7427SChris Mason 	}
906a4abeea4SJosef Bacik 	spin_unlock(&fs_info->fs_roots_radix_lock);
90754aa1f4dSChris Mason 	return err;
9080f7d52f4SChris Mason }
9090f7d52f4SChris Mason 
910d352ac68SChris Mason /*
911d352ac68SChris Mason  * defrag a given btree.  If cacheonly == 1, this won't read from the disk,
912d352ac68SChris Mason  * otherwise every leaf in the btree is read and defragged.
913d352ac68SChris Mason  */
914e9d0b13bSChris Mason int btrfs_defrag_root(struct btrfs_root *root, int cacheonly)
915e9d0b13bSChris Mason {
916e9d0b13bSChris Mason 	struct btrfs_fs_info *info = root->fs_info;
917e9d0b13bSChris Mason 	struct btrfs_trans_handle *trans;
9188929ecfaSYan, Zheng 	int ret;
919d3c2fdcfSChris Mason 	unsigned long nr;
920e9d0b13bSChris Mason 
9218929ecfaSYan, Zheng 	if (xchg(&root->defrag_running, 1))
922e9d0b13bSChris Mason 		return 0;
9238929ecfaSYan, Zheng 
9246b80053dSChris Mason 	while (1) {
9258929ecfaSYan, Zheng 		trans = btrfs_start_transaction(root, 0);
9268929ecfaSYan, Zheng 		if (IS_ERR(trans))
9278929ecfaSYan, Zheng 			return PTR_ERR(trans);
9288929ecfaSYan, Zheng 
929e9d0b13bSChris Mason 		ret = btrfs_defrag_leaves(trans, root, cacheonly);
9308929ecfaSYan, Zheng 
931d3c2fdcfSChris Mason 		nr = trans->blocks_used;
932e9d0b13bSChris Mason 		btrfs_end_transaction(trans, root);
933d3c2fdcfSChris Mason 		btrfs_btree_balance_dirty(info->tree_root, nr);
934e9d0b13bSChris Mason 		cond_resched();
935e9d0b13bSChris Mason 
9367841cb28SDavid Sterba 		if (btrfs_fs_closing(root->fs_info) || ret != -EAGAIN)
937e9d0b13bSChris Mason 			break;
938e9d0b13bSChris Mason 	}
939e9d0b13bSChris Mason 	root->defrag_running = 0;
9408929ecfaSYan, Zheng 	return ret;
941e9d0b13bSChris Mason }
942e9d0b13bSChris Mason 
943d352ac68SChris Mason /*
944d352ac68SChris Mason  * new snapshots need to be created at a very specific time in the
945d352ac68SChris Mason  * transaction commit.  This does the actual creation
946d352ac68SChris Mason  */
94780b6794dSChris Mason static noinline int create_pending_snapshot(struct btrfs_trans_handle *trans,
9483063d29fSChris Mason 				   struct btrfs_fs_info *fs_info,
9493063d29fSChris Mason 				   struct btrfs_pending_snapshot *pending)
9503063d29fSChris Mason {
9513063d29fSChris Mason 	struct btrfs_key key;
95280b6794dSChris Mason 	struct btrfs_root_item *new_root_item;
9533063d29fSChris Mason 	struct btrfs_root *tree_root = fs_info->tree_root;
9543063d29fSChris Mason 	struct btrfs_root *root = pending->root;
9556bdb72deSSage Weil 	struct btrfs_root *parent_root;
95698c9942aSLiu Bo 	struct btrfs_block_rsv *rsv;
9576bdb72deSSage Weil 	struct inode *parent_inode;
95842874b3dSMiao Xie 	struct btrfs_path *path;
95942874b3dSMiao Xie 	struct btrfs_dir_item *dir_item;
9606a912213SJosef Bacik 	struct dentry *parent;
961a22285a6SYan, Zheng 	struct dentry *dentry;
9623063d29fSChris Mason 	struct extent_buffer *tmp;
963925baeddSChris Mason 	struct extent_buffer *old;
9648ea05e3aSAlexander Block 	struct timespec cur_time = CURRENT_TIME;
9653063d29fSChris Mason 	int ret;
966d68fc57bSYan, Zheng 	u64 to_reserve = 0;
9676bdb72deSSage Weil 	u64 index = 0;
968a22285a6SYan, Zheng 	u64 objectid;
969b83cc969SLi Zefan 	u64 root_flags;
9708ea05e3aSAlexander Block 	uuid_le new_uuid;
9713063d29fSChris Mason 
97242874b3dSMiao Xie 	path = btrfs_alloc_path();
97342874b3dSMiao Xie 	if (!path) {
97442874b3dSMiao Xie 		ret = pending->error = -ENOMEM;
97542874b3dSMiao Xie 		goto path_alloc_fail;
97642874b3dSMiao Xie 	}
97742874b3dSMiao Xie 
97880b6794dSChris Mason 	new_root_item = kmalloc(sizeof(*new_root_item), GFP_NOFS);
97980b6794dSChris Mason 	if (!new_root_item) {
98049b25e05SJeff Mahoney 		ret = pending->error = -ENOMEM;
9816fa9700eSMiao Xie 		goto root_item_alloc_fail;
98280b6794dSChris Mason 	}
983a22285a6SYan, Zheng 
984581bb050SLi Zefan 	ret = btrfs_find_free_objectid(tree_root, &objectid);
985a22285a6SYan, Zheng 	if (ret) {
986a22285a6SYan, Zheng 		pending->error = ret;
9876fa9700eSMiao Xie 		goto no_free_objectid;
988a22285a6SYan, Zheng 	}
9893063d29fSChris Mason 
9903fd0a558SYan, Zheng 	btrfs_reloc_pre_snapshot(trans, pending, &to_reserve);
991d68fc57bSYan, Zheng 
992d68fc57bSYan, Zheng 	if (to_reserve > 0) {
99362f30c54SMiao Xie 		ret = btrfs_block_rsv_add_noflush(root, &pending->block_rsv,
9948bb8ab2eSJosef Bacik 						  to_reserve);
995d68fc57bSYan, Zheng 		if (ret) {
996d68fc57bSYan, Zheng 			pending->error = ret;
9976fa9700eSMiao Xie 			goto no_free_objectid;
998d68fc57bSYan, Zheng 		}
999d68fc57bSYan, Zheng 	}
1000d68fc57bSYan, Zheng 
10016f72c7e2SArne Jansen 	ret = btrfs_qgroup_inherit(trans, fs_info, root->root_key.objectid,
10026f72c7e2SArne Jansen 				   objectid, pending->inherit);
10036f72c7e2SArne Jansen 	if (ret) {
10046f72c7e2SArne Jansen 		pending->error = ret;
10056fa9700eSMiao Xie 		goto no_free_objectid;
10066f72c7e2SArne Jansen 	}
10076f72c7e2SArne Jansen 
10083063d29fSChris Mason 	key.objectid = objectid;
1009a22285a6SYan, Zheng 	key.offset = (u64)-1;
1010a22285a6SYan, Zheng 	key.type = BTRFS_ROOT_ITEM_KEY;
10113063d29fSChris Mason 
10126fa9700eSMiao Xie 	rsv = trans->block_rsv;
1013a22285a6SYan, Zheng 	trans->block_rsv = &pending->block_rsv;
10146bdb72deSSage Weil 
1015a22285a6SYan, Zheng 	dentry = pending->dentry;
10166a912213SJosef Bacik 	parent = dget_parent(dentry);
10176a912213SJosef Bacik 	parent_inode = parent->d_inode;
1018a22285a6SYan, Zheng 	parent_root = BTRFS_I(parent_inode)->root;
10197585717fSChris Mason 	record_root_in_trans(trans, parent_root);
1020a22285a6SYan, Zheng 
10216bdb72deSSage Weil 	/*
10226bdb72deSSage Weil 	 * insert the directory item
10236bdb72deSSage Weil 	 */
10246bdb72deSSage Weil 	ret = btrfs_set_inode_index(parent_inode, &index);
102549b25e05SJeff Mahoney 	BUG_ON(ret); /* -ENOMEM */
102642874b3dSMiao Xie 
102742874b3dSMiao Xie 	/* check if there is a file/dir which has the same name. */
102842874b3dSMiao Xie 	dir_item = btrfs_lookup_dir_item(NULL, parent_root, path,
102942874b3dSMiao Xie 					 btrfs_ino(parent_inode),
103042874b3dSMiao Xie 					 dentry->d_name.name,
103142874b3dSMiao Xie 					 dentry->d_name.len, 0);
103242874b3dSMiao Xie 	if (dir_item != NULL && !IS_ERR(dir_item)) {
1033fe66a05aSChris Mason 		pending->error = -EEXIST;
1034fe66a05aSChris Mason 		goto fail;
103542874b3dSMiao Xie 	} else if (IS_ERR(dir_item)) {
103642874b3dSMiao Xie 		ret = PTR_ERR(dir_item);
10376fa9700eSMiao Xie 		goto abort_trans;
103879787eaaSJeff Mahoney 	}
103942874b3dSMiao Xie 	btrfs_release_path(path);
10406bdb72deSSage Weil 
1041e999376fSChris Mason 	/*
1042e999376fSChris Mason 	 * pull in the delayed directory update
1043e999376fSChris Mason 	 * and the delayed inode item
1044e999376fSChris Mason 	 * otherwise we corrupt the FS during
1045e999376fSChris Mason 	 * snapshot
1046e999376fSChris Mason 	 */
1047e999376fSChris Mason 	ret = btrfs_run_delayed_items(trans, root);
10486fa9700eSMiao Xie 	if (ret)	/* Transaction aborted */
10496fa9700eSMiao Xie 		goto abort_trans;
1050e999376fSChris Mason 
10517585717fSChris Mason 	record_root_in_trans(trans, root);
10526bdb72deSSage Weil 	btrfs_set_root_last_snapshot(&root->root_item, trans->transid);
10536bdb72deSSage Weil 	memcpy(new_root_item, &root->root_item, sizeof(*new_root_item));
105408fe4db1SLi Zefan 	btrfs_check_and_init_root_item(new_root_item);
10556bdb72deSSage Weil 
1056b83cc969SLi Zefan 	root_flags = btrfs_root_flags(new_root_item);
1057b83cc969SLi Zefan 	if (pending->readonly)
1058b83cc969SLi Zefan 		root_flags |= BTRFS_ROOT_SUBVOL_RDONLY;
1059b83cc969SLi Zefan 	else
1060b83cc969SLi Zefan 		root_flags &= ~BTRFS_ROOT_SUBVOL_RDONLY;
1061b83cc969SLi Zefan 	btrfs_set_root_flags(new_root_item, root_flags);
1062b83cc969SLi Zefan 
10638ea05e3aSAlexander Block 	btrfs_set_root_generation_v2(new_root_item,
10648ea05e3aSAlexander Block 			trans->transid);
10658ea05e3aSAlexander Block 	uuid_le_gen(&new_uuid);
10668ea05e3aSAlexander Block 	memcpy(new_root_item->uuid, new_uuid.b, BTRFS_UUID_SIZE);
10678ea05e3aSAlexander Block 	memcpy(new_root_item->parent_uuid, root->root_item.uuid,
10688ea05e3aSAlexander Block 			BTRFS_UUID_SIZE);
10698ea05e3aSAlexander Block 	new_root_item->otime.sec = cpu_to_le64(cur_time.tv_sec);
1070dadd1105SDan Carpenter 	new_root_item->otime.nsec = cpu_to_le32(cur_time.tv_nsec);
10718ea05e3aSAlexander Block 	btrfs_set_root_otransid(new_root_item, trans->transid);
10728ea05e3aSAlexander Block 	memset(&new_root_item->stime, 0, sizeof(new_root_item->stime));
10738ea05e3aSAlexander Block 	memset(&new_root_item->rtime, 0, sizeof(new_root_item->rtime));
10748ea05e3aSAlexander Block 	btrfs_set_root_stransid(new_root_item, 0);
10758ea05e3aSAlexander Block 	btrfs_set_root_rtransid(new_root_item, 0);
10768ea05e3aSAlexander Block 
1077925baeddSChris Mason 	old = btrfs_lock_root_node(root);
107849b25e05SJeff Mahoney 	ret = btrfs_cow_block(trans, root, old, NULL, 0, &old);
107979787eaaSJeff Mahoney 	if (ret) {
108079787eaaSJeff Mahoney 		btrfs_tree_unlock(old);
108179787eaaSJeff Mahoney 		free_extent_buffer(old);
10826fa9700eSMiao Xie 		goto abort_trans;
108379787eaaSJeff Mahoney 	}
108449b25e05SJeff Mahoney 
10855d4f98a2SYan Zheng 	btrfs_set_lock_blocking(old);
10863063d29fSChris Mason 
108749b25e05SJeff Mahoney 	ret = btrfs_copy_root(trans, root, old, &tmp, objectid);
108879787eaaSJeff Mahoney 	/* clean up in any case */
1089925baeddSChris Mason 	btrfs_tree_unlock(old);
1090925baeddSChris Mason 	free_extent_buffer(old);
109179787eaaSJeff Mahoney 	if (ret)
10926fa9700eSMiao Xie 		goto abort_trans;
10933063d29fSChris Mason 
1094f1ebcc74SLiu Bo 	/* see comments in should_cow_block() */
1095f1ebcc74SLiu Bo 	root->force_cow = 1;
1096f1ebcc74SLiu Bo 	smp_wmb();
1097f1ebcc74SLiu Bo 
10985d4f98a2SYan Zheng 	btrfs_set_root_node(new_root_item, tmp);
1099a22285a6SYan, Zheng 	/* record when the snapshot was created in key.offset */
1100a22285a6SYan, Zheng 	key.offset = trans->transid;
1101a22285a6SYan, Zheng 	ret = btrfs_insert_root(trans, tree_root, &key, new_root_item);
1102925baeddSChris Mason 	btrfs_tree_unlock(tmp);
11033063d29fSChris Mason 	free_extent_buffer(tmp);
110449b25e05SJeff Mahoney 	if (ret)
11056fa9700eSMiao Xie 		goto abort_trans;
11060660b5afSChris Mason 
1107a22285a6SYan, Zheng 	/*
1108a22285a6SYan, Zheng 	 * insert root back/forward references
1109a22285a6SYan, Zheng 	 */
1110a22285a6SYan, Zheng 	ret = btrfs_add_root_ref(trans, tree_root, objectid,
1111a22285a6SYan, Zheng 				 parent_root->root_key.objectid,
111233345d01SLi Zefan 				 btrfs_ino(parent_inode), index,
1113a22285a6SYan, Zheng 				 dentry->d_name.name, dentry->d_name.len);
111449b25e05SJeff Mahoney 	if (ret)
11156fa9700eSMiao Xie 		goto abort_trans;
1116a22285a6SYan, Zheng 
1117a22285a6SYan, Zheng 	key.offset = (u64)-1;
1118a22285a6SYan, Zheng 	pending->snap = btrfs_read_fs_root_no_name(root->fs_info, &key);
111979787eaaSJeff Mahoney 	if (IS_ERR(pending->snap)) {
112079787eaaSJeff Mahoney 		ret = PTR_ERR(pending->snap);
112149b25e05SJeff Mahoney 		goto abort_trans;
112279787eaaSJeff Mahoney 	}
1123d68fc57bSYan, Zheng 
112449b25e05SJeff Mahoney 	ret = btrfs_reloc_post_snapshot(trans, pending);
112549b25e05SJeff Mahoney 	if (ret)
112649b25e05SJeff Mahoney 		goto abort_trans;
1127361048f5SMiao Xie 
1128361048f5SMiao Xie 	ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
1129361048f5SMiao Xie 	if (ret)
1130361048f5SMiao Xie 		goto abort_trans;
113142874b3dSMiao Xie 
113242874b3dSMiao Xie 	ret = btrfs_insert_dir_item(trans, parent_root,
113342874b3dSMiao Xie 				    dentry->d_name.name, dentry->d_name.len,
113442874b3dSMiao Xie 				    parent_inode, &key,
113542874b3dSMiao Xie 				    BTRFS_FT_DIR, index);
113642874b3dSMiao Xie 	/* We have check then name at the beginning, so it is impossible. */
113742874b3dSMiao Xie 	BUG_ON(ret == -EEXIST);
113842874b3dSMiao Xie 	if (ret)
113942874b3dSMiao Xie 		goto abort_trans;
114042874b3dSMiao Xie 
114142874b3dSMiao Xie 	btrfs_i_size_write(parent_inode, parent_inode->i_size +
114242874b3dSMiao Xie 					 dentry->d_name.len * 2);
114342874b3dSMiao Xie 	parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
114442874b3dSMiao Xie 	ret = btrfs_update_inode(trans, parent_root, parent_inode);
114542874b3dSMiao Xie 	if (ret)
114642874b3dSMiao Xie 		goto abort_trans;
11473063d29fSChris Mason fail:
11486fa9700eSMiao Xie 	dput(parent);
114998c9942aSLiu Bo 	trans->block_rsv = rsv;
11506fa9700eSMiao Xie no_free_objectid:
11516fa9700eSMiao Xie 	kfree(new_root_item);
11526fa9700eSMiao Xie root_item_alloc_fail:
115342874b3dSMiao Xie 	btrfs_free_path(path);
115442874b3dSMiao Xie path_alloc_fail:
1155a22285a6SYan, Zheng 	btrfs_block_rsv_release(root, &pending->block_rsv, (u64)-1);
115649b25e05SJeff Mahoney 	return ret;
115749b25e05SJeff Mahoney 
115849b25e05SJeff Mahoney abort_trans:
115949b25e05SJeff Mahoney 	btrfs_abort_transaction(trans, root, ret);
116049b25e05SJeff Mahoney 	goto fail;
11613063d29fSChris Mason }
11623063d29fSChris Mason 
1163d352ac68SChris Mason /*
1164d352ac68SChris Mason  * create all the snapshots we've scheduled for creation
1165d352ac68SChris Mason  */
116680b6794dSChris Mason static noinline int create_pending_snapshots(struct btrfs_trans_handle *trans,
11673063d29fSChris Mason 					     struct btrfs_fs_info *fs_info)
11683063d29fSChris Mason {
11693063d29fSChris Mason 	struct btrfs_pending_snapshot *pending;
11703063d29fSChris Mason 	struct list_head *head = &trans->transaction->pending_snapshots;
11713de4586cSChris Mason 
1172fe66a05aSChris Mason 	list_for_each_entry(pending, head, list)
1173fe66a05aSChris Mason 		create_pending_snapshot(trans, fs_info, pending);
11743de4586cSChris Mason 	return 0;
11753de4586cSChris Mason }
11763de4586cSChris Mason 
11775d4f98a2SYan Zheng static void update_super_roots(struct btrfs_root *root)
11785d4f98a2SYan Zheng {
11795d4f98a2SYan Zheng 	struct btrfs_root_item *root_item;
11805d4f98a2SYan Zheng 	struct btrfs_super_block *super;
11815d4f98a2SYan Zheng 
11826c41761fSDavid Sterba 	super = root->fs_info->super_copy;
11835d4f98a2SYan Zheng 
11845d4f98a2SYan Zheng 	root_item = &root->fs_info->chunk_root->root_item;
11855d4f98a2SYan Zheng 	super->chunk_root = root_item->bytenr;
11865d4f98a2SYan Zheng 	super->chunk_root_generation = root_item->generation;
11875d4f98a2SYan Zheng 	super->chunk_root_level = root_item->level;
11885d4f98a2SYan Zheng 
11895d4f98a2SYan Zheng 	root_item = &root->fs_info->tree_root->root_item;
11905d4f98a2SYan Zheng 	super->root = root_item->bytenr;
11915d4f98a2SYan Zheng 	super->generation = root_item->generation;
11925d4f98a2SYan Zheng 	super->root_level = root_item->level;
119373bc1876SJosef Bacik 	if (btrfs_test_opt(root, SPACE_CACHE))
11940af3d00bSJosef Bacik 		super->cache_generation = root_item->generation;
11955d4f98a2SYan Zheng }
11965d4f98a2SYan Zheng 
1197f36f3042SChris Mason int btrfs_transaction_in_commit(struct btrfs_fs_info *info)
1198f36f3042SChris Mason {
1199f36f3042SChris Mason 	int ret = 0;
1200a4abeea4SJosef Bacik 	spin_lock(&info->trans_lock);
1201f36f3042SChris Mason 	if (info->running_transaction)
1202f36f3042SChris Mason 		ret = info->running_transaction->in_commit;
1203a4abeea4SJosef Bacik 	spin_unlock(&info->trans_lock);
1204f36f3042SChris Mason 	return ret;
1205f36f3042SChris Mason }
1206f36f3042SChris Mason 
12078929ecfaSYan, Zheng int btrfs_transaction_blocked(struct btrfs_fs_info *info)
12088929ecfaSYan, Zheng {
12098929ecfaSYan, Zheng 	int ret = 0;
1210a4abeea4SJosef Bacik 	spin_lock(&info->trans_lock);
12118929ecfaSYan, Zheng 	if (info->running_transaction)
12128929ecfaSYan, Zheng 		ret = info->running_transaction->blocked;
1213a4abeea4SJosef Bacik 	spin_unlock(&info->trans_lock);
12148929ecfaSYan, Zheng 	return ret;
12158929ecfaSYan, Zheng }
12168929ecfaSYan, Zheng 
1217bb9c12c9SSage Weil /*
1218bb9c12c9SSage Weil  * wait for the current transaction commit to start and block subsequent
1219bb9c12c9SSage Weil  * transaction joins
1220bb9c12c9SSage Weil  */
1221bb9c12c9SSage Weil static void wait_current_trans_commit_start(struct btrfs_root *root,
1222bb9c12c9SSage Weil 					    struct btrfs_transaction *trans)
1223bb9c12c9SSage Weil {
122472d63ed6SLi Zefan 	wait_event(root->fs_info->transaction_blocked_wait, trans->in_commit);
1225bb9c12c9SSage Weil }
1226bb9c12c9SSage Weil 
1227bb9c12c9SSage Weil /*
1228bb9c12c9SSage Weil  * wait for the current transaction to start and then become unblocked.
1229bb9c12c9SSage Weil  * caller holds ref.
1230bb9c12c9SSage Weil  */
1231bb9c12c9SSage Weil static void wait_current_trans_commit_start_and_unblock(struct btrfs_root *root,
1232bb9c12c9SSage Weil 					 struct btrfs_transaction *trans)
1233bb9c12c9SSage Weil {
123472d63ed6SLi Zefan 	wait_event(root->fs_info->transaction_wait,
123572d63ed6SLi Zefan 		   trans->commit_done || (trans->in_commit && !trans->blocked));
1236bb9c12c9SSage Weil }
1237bb9c12c9SSage Weil 
1238bb9c12c9SSage Weil /*
1239bb9c12c9SSage Weil  * commit transactions asynchronously. once btrfs_commit_transaction_async
1240bb9c12c9SSage Weil  * returns, any subsequent transaction will not be allowed to join.
1241bb9c12c9SSage Weil  */
1242bb9c12c9SSage Weil struct btrfs_async_commit {
1243bb9c12c9SSage Weil 	struct btrfs_trans_handle *newtrans;
1244bb9c12c9SSage Weil 	struct btrfs_root *root;
1245bb9c12c9SSage Weil 	struct delayed_work work;
1246bb9c12c9SSage Weil };
1247bb9c12c9SSage Weil 
1248bb9c12c9SSage Weil static void do_async_commit(struct work_struct *work)
1249bb9c12c9SSage Weil {
1250bb9c12c9SSage Weil 	struct btrfs_async_commit *ac =
1251bb9c12c9SSage Weil 		container_of(work, struct btrfs_async_commit, work.work);
1252bb9c12c9SSage Weil 
12536fc4e354SSage Weil 	/*
12546fc4e354SSage Weil 	 * We've got freeze protection passed with the transaction.
12556fc4e354SSage Weil 	 * Tell lockdep about it.
12566fc4e354SSage Weil 	 */
12576fc4e354SSage Weil 	rwsem_acquire_read(
12586fc4e354SSage Weil 		&ac->root->fs_info->sb->s_writers.lock_map[SB_FREEZE_FS-1],
12596fc4e354SSage Weil 		0, 1, _THIS_IP_);
12606fc4e354SSage Weil 
1261e209db7aSSage Weil 	current->journal_info = ac->newtrans;
1262e209db7aSSage Weil 
1263bb9c12c9SSage Weil 	btrfs_commit_transaction(ac->newtrans, ac->root);
1264bb9c12c9SSage Weil 	kfree(ac);
1265bb9c12c9SSage Weil }
1266bb9c12c9SSage Weil 
1267bb9c12c9SSage Weil int btrfs_commit_transaction_async(struct btrfs_trans_handle *trans,
1268bb9c12c9SSage Weil 				   struct btrfs_root *root,
1269bb9c12c9SSage Weil 				   int wait_for_unblock)
1270bb9c12c9SSage Weil {
1271bb9c12c9SSage Weil 	struct btrfs_async_commit *ac;
1272bb9c12c9SSage Weil 	struct btrfs_transaction *cur_trans;
1273bb9c12c9SSage Weil 
1274bb9c12c9SSage Weil 	ac = kmalloc(sizeof(*ac), GFP_NOFS);
1275db5b493aSTsutomu Itoh 	if (!ac)
1276db5b493aSTsutomu Itoh 		return -ENOMEM;
1277bb9c12c9SSage Weil 
1278bb9c12c9SSage Weil 	INIT_DELAYED_WORK(&ac->work, do_async_commit);
1279bb9c12c9SSage Weil 	ac->root = root;
12807a7eaa40SJosef Bacik 	ac->newtrans = btrfs_join_transaction(root);
12813612b495STsutomu Itoh 	if (IS_ERR(ac->newtrans)) {
12823612b495STsutomu Itoh 		int err = PTR_ERR(ac->newtrans);
12833612b495STsutomu Itoh 		kfree(ac);
12843612b495STsutomu Itoh 		return err;
12853612b495STsutomu Itoh 	}
1286bb9c12c9SSage Weil 
1287bb9c12c9SSage Weil 	/* take transaction reference */
1288bb9c12c9SSage Weil 	cur_trans = trans->transaction;
128913c5a93eSJosef Bacik 	atomic_inc(&cur_trans->use_count);
1290bb9c12c9SSage Weil 
1291bb9c12c9SSage Weil 	btrfs_end_transaction(trans, root);
12926fc4e354SSage Weil 
12936fc4e354SSage Weil 	/*
12946fc4e354SSage Weil 	 * Tell lockdep we've released the freeze rwsem, since the
12956fc4e354SSage Weil 	 * async commit thread will be the one to unlock it.
12966fc4e354SSage Weil 	 */
12976fc4e354SSage Weil 	rwsem_release(&root->fs_info->sb->s_writers.lock_map[SB_FREEZE_FS-1],
12986fc4e354SSage Weil 		      1, _THIS_IP_);
12996fc4e354SSage Weil 
1300bb9c12c9SSage Weil 	schedule_delayed_work(&ac->work, 0);
1301bb9c12c9SSage Weil 
1302bb9c12c9SSage Weil 	/* wait for transaction to start and unblock */
1303bb9c12c9SSage Weil 	if (wait_for_unblock)
1304bb9c12c9SSage Weil 		wait_current_trans_commit_start_and_unblock(root, cur_trans);
1305bb9c12c9SSage Weil 	else
1306bb9c12c9SSage Weil 		wait_current_trans_commit_start(root, cur_trans);
1307bb9c12c9SSage Weil 
130838e88054SSage Weil 	if (current->journal_info == trans)
130938e88054SSage Weil 		current->journal_info = NULL;
131038e88054SSage Weil 
131138e88054SSage Weil 	put_transaction(cur_trans);
1312bb9c12c9SSage Weil 	return 0;
1313bb9c12c9SSage Weil }
1314bb9c12c9SSage Weil 
131549b25e05SJeff Mahoney 
131649b25e05SJeff Mahoney static void cleanup_transaction(struct btrfs_trans_handle *trans,
13177b8b92afSJosef Bacik 				struct btrfs_root *root, int err)
131849b25e05SJeff Mahoney {
131949b25e05SJeff Mahoney 	struct btrfs_transaction *cur_trans = trans->transaction;
132049b25e05SJeff Mahoney 
132149b25e05SJeff Mahoney 	WARN_ON(trans->use_count > 1);
132249b25e05SJeff Mahoney 
13237b8b92afSJosef Bacik 	btrfs_abort_transaction(trans, root, err);
13247b8b92afSJosef Bacik 
132549b25e05SJeff Mahoney 	spin_lock(&root->fs_info->trans_lock);
132649b25e05SJeff Mahoney 	list_del_init(&cur_trans->list);
1327d7096fc3SJosef Bacik 	if (cur_trans == root->fs_info->running_transaction) {
1328d7096fc3SJosef Bacik 		root->fs_info->running_transaction = NULL;
1329d7096fc3SJosef Bacik 		root->fs_info->trans_no_join = 0;
1330d7096fc3SJosef Bacik 	}
133149b25e05SJeff Mahoney 	spin_unlock(&root->fs_info->trans_lock);
133249b25e05SJeff Mahoney 
133349b25e05SJeff Mahoney 	btrfs_cleanup_one_transaction(trans->transaction, root);
133449b25e05SJeff Mahoney 
133549b25e05SJeff Mahoney 	put_transaction(cur_trans);
133649b25e05SJeff Mahoney 	put_transaction(cur_trans);
133749b25e05SJeff Mahoney 
133849b25e05SJeff Mahoney 	trace_btrfs_transaction_commit(root);
133949b25e05SJeff Mahoney 
134049b25e05SJeff Mahoney 	btrfs_scrub_continue(root);
134149b25e05SJeff Mahoney 
134249b25e05SJeff Mahoney 	if (current->journal_info == trans)
134349b25e05SJeff Mahoney 		current->journal_info = NULL;
134449b25e05SJeff Mahoney 
134549b25e05SJeff Mahoney 	kmem_cache_free(btrfs_trans_handle_cachep, trans);
134649b25e05SJeff Mahoney }
134749b25e05SJeff Mahoney 
1348bb9c12c9SSage Weil /*
1349bb9c12c9SSage Weil  * btrfs_transaction state sequence:
1350bb9c12c9SSage Weil  *    in_commit = 0, blocked = 0  (initial)
1351bb9c12c9SSage Weil  *    in_commit = 1, blocked = 1
1352bb9c12c9SSage Weil  *    blocked = 0
1353bb9c12c9SSage Weil  *    commit_done = 1
1354bb9c12c9SSage Weil  */
135579154b1bSChris Mason int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
135679154b1bSChris Mason 			     struct btrfs_root *root)
135779154b1bSChris Mason {
135815ee9bc7SJosef Bacik 	unsigned long joined = 0;
135949b25e05SJeff Mahoney 	struct btrfs_transaction *cur_trans = trans->transaction;
13608fd17795SChris Mason 	struct btrfs_transaction *prev_trans = NULL;
136179154b1bSChris Mason 	DEFINE_WAIT(wait);
136249b25e05SJeff Mahoney 	int ret = -EIO;
136389573b9cSChris Mason 	int should_grow = 0;
136489573b9cSChris Mason 	unsigned long now = get_seconds();
1365dccae999SSage Weil 	int flush_on_commit = btrfs_test_opt(root, FLUSHONCOMMIT);
136679154b1bSChris Mason 
13675a3f23d5SChris Mason 	btrfs_run_ordered_operations(root, 0);
13685a3f23d5SChris Mason 
136949b25e05SJeff Mahoney 	if (cur_trans->aborted)
137049b25e05SJeff Mahoney 		goto cleanup_transaction;
137149b25e05SJeff Mahoney 
137256bec294SChris Mason 	/* make a pass through all the delayed refs we have so far
137356bec294SChris Mason 	 * any runnings procs may add more while we are here
137456bec294SChris Mason 	 */
137556bec294SChris Mason 	ret = btrfs_run_delayed_refs(trans, root, 0);
137649b25e05SJeff Mahoney 	if (ret)
137749b25e05SJeff Mahoney 		goto cleanup_transaction;
137856bec294SChris Mason 
13790e721106SJosef Bacik 	btrfs_trans_release_metadata(trans, root);
13800e721106SJosef Bacik 	trans->block_rsv = NULL;
13810e721106SJosef Bacik 
1382b7ec40d7SChris Mason 	cur_trans = trans->transaction;
138349b25e05SJeff Mahoney 
138456bec294SChris Mason 	/*
138556bec294SChris Mason 	 * set the flushing flag so procs in this transaction have to
138656bec294SChris Mason 	 * start sending their work down.
138756bec294SChris Mason 	 */
1388b7ec40d7SChris Mason 	cur_trans->delayed_refs.flushing = 1;
138956bec294SChris Mason 
1390c3e69d58SChris Mason 	ret = btrfs_run_delayed_refs(trans, root, 0);
139149b25e05SJeff Mahoney 	if (ret)
139249b25e05SJeff Mahoney 		goto cleanup_transaction;
139356bec294SChris Mason 
1394a4abeea4SJosef Bacik 	spin_lock(&cur_trans->commit_lock);
1395b7ec40d7SChris Mason 	if (cur_trans->in_commit) {
1396a4abeea4SJosef Bacik 		spin_unlock(&cur_trans->commit_lock);
139713c5a93eSJosef Bacik 		atomic_inc(&cur_trans->use_count);
139849b25e05SJeff Mahoney 		ret = btrfs_end_transaction(trans, root);
1399ccd467d6SChris Mason 
1400b9c8300cSLi Zefan 		wait_for_commit(root, cur_trans);
140115ee9bc7SJosef Bacik 
140279154b1bSChris Mason 		put_transaction(cur_trans);
140315ee9bc7SJosef Bacik 
140449b25e05SJeff Mahoney 		return ret;
140579154b1bSChris Mason 	}
14064313b399SChris Mason 
14072c90e5d6SChris Mason 	trans->transaction->in_commit = 1;
1408f9295749SChris Mason 	trans->transaction->blocked = 1;
1409a4abeea4SJosef Bacik 	spin_unlock(&cur_trans->commit_lock);
1410bb9c12c9SSage Weil 	wake_up(&root->fs_info->transaction_blocked_wait);
1411bb9c12c9SSage Weil 
1412a4abeea4SJosef Bacik 	spin_lock(&root->fs_info->trans_lock);
1413ccd467d6SChris Mason 	if (cur_trans->list.prev != &root->fs_info->trans_list) {
1414ccd467d6SChris Mason 		prev_trans = list_entry(cur_trans->list.prev,
1415ccd467d6SChris Mason 					struct btrfs_transaction, list);
1416ccd467d6SChris Mason 		if (!prev_trans->commit_done) {
141713c5a93eSJosef Bacik 			atomic_inc(&prev_trans->use_count);
1418a4abeea4SJosef Bacik 			spin_unlock(&root->fs_info->trans_lock);
1419ccd467d6SChris Mason 
1420ccd467d6SChris Mason 			wait_for_commit(root, prev_trans);
1421ccd467d6SChris Mason 
142215ee9bc7SJosef Bacik 			put_transaction(prev_trans);
1423a4abeea4SJosef Bacik 		} else {
1424a4abeea4SJosef Bacik 			spin_unlock(&root->fs_info->trans_lock);
1425ccd467d6SChris Mason 		}
1426a4abeea4SJosef Bacik 	} else {
1427a4abeea4SJosef Bacik 		spin_unlock(&root->fs_info->trans_lock);
1428ccd467d6SChris Mason 	}
142915ee9bc7SJosef Bacik 
1430e39e64acSChris Mason 	if (!btrfs_test_opt(root, SSD) &&
1431e39e64acSChris Mason 	    (now < cur_trans->start_time || now - cur_trans->start_time < 1))
143289573b9cSChris Mason 		should_grow = 1;
143389573b9cSChris Mason 
143415ee9bc7SJosef Bacik 	do {
14357ea394f1SYan Zheng 		int snap_pending = 0;
1436a4abeea4SJosef Bacik 
143715ee9bc7SJosef Bacik 		joined = cur_trans->num_joined;
14387ea394f1SYan Zheng 		if (!list_empty(&trans->transaction->pending_snapshots))
14397ea394f1SYan Zheng 			snap_pending = 1;
14407ea394f1SYan Zheng 
14412c90e5d6SChris Mason 		WARN_ON(cur_trans != trans->transaction);
144215ee9bc7SJosef Bacik 
14430bdb1db2SSage Weil 		if (flush_on_commit || snap_pending) {
144424bbcf04SYan, Zheng 			btrfs_start_delalloc_inodes(root, 1);
1445143bede5SJeff Mahoney 			btrfs_wait_ordered_extents(root, 0, 1);
14467ea394f1SYan Zheng 		}
14477ea394f1SYan Zheng 
144816cdcec7SMiao Xie 		ret = btrfs_run_delayed_items(trans, root);
144949b25e05SJeff Mahoney 		if (ret)
145049b25e05SJeff Mahoney 			goto cleanup_transaction;
145116cdcec7SMiao Xie 
14525a3f23d5SChris Mason 		/*
1453edf39272SJan Schmidt 		 * running the delayed items may have added new refs. account
1454edf39272SJan Schmidt 		 * them now so that they hinder processing of more delayed refs
1455edf39272SJan Schmidt 		 * as little as possible.
1456edf39272SJan Schmidt 		 */
1457edf39272SJan Schmidt 		btrfs_delayed_refs_qgroup_accounting(trans, root->fs_info);
1458edf39272SJan Schmidt 
1459edf39272SJan Schmidt 		/*
14605a3f23d5SChris Mason 		 * rename don't use btrfs_join_transaction, so, once we
14615a3f23d5SChris Mason 		 * set the transaction to blocked above, we aren't going
14625a3f23d5SChris Mason 		 * to get any new ordered operations.  We can safely run
14635a3f23d5SChris Mason 		 * it here and no for sure that nothing new will be added
14645a3f23d5SChris Mason 		 * to the list
14655a3f23d5SChris Mason 		 */
14665a3f23d5SChris Mason 		btrfs_run_ordered_operations(root, 1);
14675a3f23d5SChris Mason 
1468ed3b3d31SChris Mason 		prepare_to_wait(&cur_trans->writer_wait, &wait,
1469ed3b3d31SChris Mason 				TASK_UNINTERRUPTIBLE);
1470ed3b3d31SChris Mason 
147113c5a93eSJosef Bacik 		if (atomic_read(&cur_trans->num_writers) > 1)
147299d16cbcSSage Weil 			schedule_timeout(MAX_SCHEDULE_TIMEOUT);
147399d16cbcSSage Weil 		else if (should_grow)
147499d16cbcSSage Weil 			schedule_timeout(1);
147515ee9bc7SJosef Bacik 
147615ee9bc7SJosef Bacik 		finish_wait(&cur_trans->writer_wait, &wait);
1477ed0ca140SJosef Bacik 	} while (atomic_read(&cur_trans->num_writers) > 1 ||
1478ed0ca140SJosef Bacik 		 (should_grow && cur_trans->num_joined != joined));
1479ed0ca140SJosef Bacik 
1480ed0ca140SJosef Bacik 	/*
1481ed0ca140SJosef Bacik 	 * Ok now we need to make sure to block out any other joins while we
1482ed0ca140SJosef Bacik 	 * commit the transaction.  We could have started a join before setting
1483ed0ca140SJosef Bacik 	 * no_join so make sure to wait for num_writers to == 1 again.
1484ed0ca140SJosef Bacik 	 */
1485a4abeea4SJosef Bacik 	spin_lock(&root->fs_info->trans_lock);
1486a4abeea4SJosef Bacik 	root->fs_info->trans_no_join = 1;
1487a4abeea4SJosef Bacik 	spin_unlock(&root->fs_info->trans_lock);
1488ed0ca140SJosef Bacik 	wait_event(cur_trans->writer_wait,
1489ed0ca140SJosef Bacik 		   atomic_read(&cur_trans->num_writers) == 1);
149015ee9bc7SJosef Bacik 
14917585717fSChris Mason 	/*
14927585717fSChris Mason 	 * the reloc mutex makes sure that we stop
14937585717fSChris Mason 	 * the balancing code from coming in and moving
14947585717fSChris Mason 	 * extents around in the middle of the commit
14957585717fSChris Mason 	 */
14967585717fSChris Mason 	mutex_lock(&root->fs_info->reloc_mutex);
14977585717fSChris Mason 
149842874b3dSMiao Xie 	/*
149942874b3dSMiao Xie 	 * We needn't worry about the delayed items because we will
150042874b3dSMiao Xie 	 * deal with them in create_pending_snapshot(), which is the
150142874b3dSMiao Xie 	 * core function of the snapshot creation.
150242874b3dSMiao Xie 	 */
150342874b3dSMiao Xie 	ret = create_pending_snapshots(trans, root->fs_info);
150449b25e05SJeff Mahoney 	if (ret) {
150549b25e05SJeff Mahoney 		mutex_unlock(&root->fs_info->reloc_mutex);
150649b25e05SJeff Mahoney 		goto cleanup_transaction;
150749b25e05SJeff Mahoney 	}
15083063d29fSChris Mason 
150942874b3dSMiao Xie 	/*
151042874b3dSMiao Xie 	 * We insert the dir indexes of the snapshots and update the inode
151142874b3dSMiao Xie 	 * of the snapshots' parents after the snapshot creation, so there
151242874b3dSMiao Xie 	 * are some delayed items which are not dealt with. Now deal with
151342874b3dSMiao Xie 	 * them.
151442874b3dSMiao Xie 	 *
151542874b3dSMiao Xie 	 * We needn't worry that this operation will corrupt the snapshots,
151642874b3dSMiao Xie 	 * because all the tree which are snapshoted will be forced to COW
151742874b3dSMiao Xie 	 * the nodes and leaves.
151842874b3dSMiao Xie 	 */
151942874b3dSMiao Xie 	ret = btrfs_run_delayed_items(trans, root);
152049b25e05SJeff Mahoney 	if (ret) {
152149b25e05SJeff Mahoney 		mutex_unlock(&root->fs_info->reloc_mutex);
152249b25e05SJeff Mahoney 		goto cleanup_transaction;
152349b25e05SJeff Mahoney 	}
152416cdcec7SMiao Xie 
152556bec294SChris Mason 	ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
152649b25e05SJeff Mahoney 	if (ret) {
152749b25e05SJeff Mahoney 		mutex_unlock(&root->fs_info->reloc_mutex);
152849b25e05SJeff Mahoney 		goto cleanup_transaction;
152949b25e05SJeff Mahoney 	}
153056bec294SChris Mason 
1531e999376fSChris Mason 	/*
1532e999376fSChris Mason 	 * make sure none of the code above managed to slip in a
1533e999376fSChris Mason 	 * delayed item
1534e999376fSChris Mason 	 */
1535e999376fSChris Mason 	btrfs_assert_delayed_root_empty(root);
1536e999376fSChris Mason 
15372c90e5d6SChris Mason 	WARN_ON(cur_trans != trans->transaction);
1538dc17ff8fSChris Mason 
1539a2de733cSArne Jansen 	btrfs_scrub_pause(root);
1540e02119d5SChris Mason 	/* btrfs_commit_tree_roots is responsible for getting the
1541e02119d5SChris Mason 	 * various roots consistent with each other.  Every pointer
1542e02119d5SChris Mason 	 * in the tree of tree roots has to point to the most up to date
1543e02119d5SChris Mason 	 * root for every subvolume and other tree.  So, we have to keep
1544e02119d5SChris Mason 	 * the tree logging code from jumping in and changing any
1545e02119d5SChris Mason 	 * of the trees.
1546e02119d5SChris Mason 	 *
1547e02119d5SChris Mason 	 * At this point in the commit, there can't be any tree-log
1548e02119d5SChris Mason 	 * writers, but a little lower down we drop the trans mutex
1549e02119d5SChris Mason 	 * and let new people in.  By holding the tree_log_mutex
1550e02119d5SChris Mason 	 * from now until after the super is written, we avoid races
1551e02119d5SChris Mason 	 * with the tree-log code.
1552e02119d5SChris Mason 	 */
1553e02119d5SChris Mason 	mutex_lock(&root->fs_info->tree_log_mutex);
15541a40e23bSZheng Yan 
15555d4f98a2SYan Zheng 	ret = commit_fs_roots(trans, root);
155649b25e05SJeff Mahoney 	if (ret) {
155749b25e05SJeff Mahoney 		mutex_unlock(&root->fs_info->tree_log_mutex);
1558871383beSDavid Sterba 		mutex_unlock(&root->fs_info->reloc_mutex);
155949b25e05SJeff Mahoney 		goto cleanup_transaction;
156049b25e05SJeff Mahoney 	}
156154aa1f4dSChris Mason 
15625d4f98a2SYan Zheng 	/* commit_fs_roots gets rid of all the tree log roots, it is now
1563e02119d5SChris Mason 	 * safe to free the root of tree log roots
1564e02119d5SChris Mason 	 */
1565e02119d5SChris Mason 	btrfs_free_log_root_tree(trans, root->fs_info);
1566e02119d5SChris Mason 
15675d4f98a2SYan Zheng 	ret = commit_cowonly_roots(trans, root);
156849b25e05SJeff Mahoney 	if (ret) {
156949b25e05SJeff Mahoney 		mutex_unlock(&root->fs_info->tree_log_mutex);
1570871383beSDavid Sterba 		mutex_unlock(&root->fs_info->reloc_mutex);
157149b25e05SJeff Mahoney 		goto cleanup_transaction;
157249b25e05SJeff Mahoney 	}
157354aa1f4dSChris Mason 
157411833d66SYan Zheng 	btrfs_prepare_extent_commit(trans, root);
157511833d66SYan Zheng 
157678fae27eSChris Mason 	cur_trans = root->fs_info->running_transaction;
15775f39d397SChris Mason 
15785d4f98a2SYan Zheng 	btrfs_set_root_node(&root->fs_info->tree_root->root_item,
15795d4f98a2SYan Zheng 			    root->fs_info->tree_root->node);
1580817d52f8SJosef Bacik 	switch_commit_root(root->fs_info->tree_root);
15815d4f98a2SYan Zheng 
15825d4f98a2SYan Zheng 	btrfs_set_root_node(&root->fs_info->chunk_root->root_item,
15835d4f98a2SYan Zheng 			    root->fs_info->chunk_root->node);
1584817d52f8SJosef Bacik 	switch_commit_root(root->fs_info->chunk_root);
15855d4f98a2SYan Zheng 
1586edf39272SJan Schmidt 	assert_qgroups_uptodate(trans);
15875d4f98a2SYan Zheng 	update_super_roots(root);
1588e02119d5SChris Mason 
1589e02119d5SChris Mason 	if (!root->fs_info->log_root_recovering) {
15906c41761fSDavid Sterba 		btrfs_set_super_log_root(root->fs_info->super_copy, 0);
15916c41761fSDavid Sterba 		btrfs_set_super_log_root_level(root->fs_info->super_copy, 0);
1592e02119d5SChris Mason 	}
1593e02119d5SChris Mason 
15946c41761fSDavid Sterba 	memcpy(root->fs_info->super_for_commit, root->fs_info->super_copy,
15956c41761fSDavid Sterba 	       sizeof(*root->fs_info->super_copy));
1596ccd467d6SChris Mason 
1597f9295749SChris Mason 	trans->transaction->blocked = 0;
1598a4abeea4SJosef Bacik 	spin_lock(&root->fs_info->trans_lock);
1599a4abeea4SJosef Bacik 	root->fs_info->running_transaction = NULL;
1600a4abeea4SJosef Bacik 	root->fs_info->trans_no_join = 0;
1601a4abeea4SJosef Bacik 	spin_unlock(&root->fs_info->trans_lock);
16027585717fSChris Mason 	mutex_unlock(&root->fs_info->reloc_mutex);
1603b7ec40d7SChris Mason 
1604f9295749SChris Mason 	wake_up(&root->fs_info->transaction_wait);
1605e6dcd2dcSChris Mason 
160679154b1bSChris Mason 	ret = btrfs_write_and_wait_transaction(trans, root);
160749b25e05SJeff Mahoney 	if (ret) {
160849b25e05SJeff Mahoney 		btrfs_error(root->fs_info, ret,
160949b25e05SJeff Mahoney 			    "Error while writing out transaction.");
161049b25e05SJeff Mahoney 		mutex_unlock(&root->fs_info->tree_log_mutex);
161149b25e05SJeff Mahoney 		goto cleanup_transaction;
161249b25e05SJeff Mahoney 	}
161349b25e05SJeff Mahoney 
161449b25e05SJeff Mahoney 	ret = write_ctree_super(trans, root, 0);
161549b25e05SJeff Mahoney 	if (ret) {
161649b25e05SJeff Mahoney 		mutex_unlock(&root->fs_info->tree_log_mutex);
161749b25e05SJeff Mahoney 		goto cleanup_transaction;
161849b25e05SJeff Mahoney 	}
16194313b399SChris Mason 
1620e02119d5SChris Mason 	/*
1621e02119d5SChris Mason 	 * the super is written, we can safely allow the tree-loggers
1622e02119d5SChris Mason 	 * to go about their business
1623e02119d5SChris Mason 	 */
1624e02119d5SChris Mason 	mutex_unlock(&root->fs_info->tree_log_mutex);
1625e02119d5SChris Mason 
162611833d66SYan Zheng 	btrfs_finish_extent_commit(trans, root);
16274313b399SChris Mason 
16282c90e5d6SChris Mason 	cur_trans->commit_done = 1;
1629b7ec40d7SChris Mason 
163015ee9bc7SJosef Bacik 	root->fs_info->last_trans_committed = cur_trans->transid;
1631817d52f8SJosef Bacik 
16322c90e5d6SChris Mason 	wake_up(&cur_trans->commit_wait);
16333de4586cSChris Mason 
1634a4abeea4SJosef Bacik 	spin_lock(&root->fs_info->trans_lock);
163513c5a93eSJosef Bacik 	list_del_init(&cur_trans->list);
1636a4abeea4SJosef Bacik 	spin_unlock(&root->fs_info->trans_lock);
1637a4abeea4SJosef Bacik 
163879154b1bSChris Mason 	put_transaction(cur_trans);
163978fae27eSChris Mason 	put_transaction(cur_trans);
164058176a96SJosef Bacik 
1641b2b5ef5cSJan Kara 	sb_end_intwrite(root->fs_info->sb);
1642b2b5ef5cSJan Kara 
16431abe9b8aSliubo 	trace_btrfs_transaction_commit(root);
16441abe9b8aSliubo 
1645a2de733cSArne Jansen 	btrfs_scrub_continue(root);
1646a2de733cSArne Jansen 
16479ed74f2dSJosef Bacik 	if (current->journal_info == trans)
16489ed74f2dSJosef Bacik 		current->journal_info = NULL;
16499ed74f2dSJosef Bacik 
16502c90e5d6SChris Mason 	kmem_cache_free(btrfs_trans_handle_cachep, trans);
165124bbcf04SYan, Zheng 
165224bbcf04SYan, Zheng 	if (current != root->fs_info->transaction_kthread)
165324bbcf04SYan, Zheng 		btrfs_run_delayed_iputs(root);
165424bbcf04SYan, Zheng 
165579154b1bSChris Mason 	return ret;
165649b25e05SJeff Mahoney 
165749b25e05SJeff Mahoney cleanup_transaction:
16580e721106SJosef Bacik 	btrfs_trans_release_metadata(trans, root);
16590e721106SJosef Bacik 	trans->block_rsv = NULL;
166049b25e05SJeff Mahoney 	btrfs_printk(root->fs_info, "Skipping commit of aborted transaction.\n");
166149b25e05SJeff Mahoney //	WARN_ON(1);
166249b25e05SJeff Mahoney 	if (current->journal_info == trans)
166349b25e05SJeff Mahoney 		current->journal_info = NULL;
16647b8b92afSJosef Bacik 	cleanup_transaction(trans, root, ret);
166549b25e05SJeff Mahoney 
166649b25e05SJeff Mahoney 	return ret;
166779154b1bSChris Mason }
166879154b1bSChris Mason 
1669d352ac68SChris Mason /*
1670d352ac68SChris Mason  * interface function to delete all the snapshots we have scheduled for deletion
1671d352ac68SChris Mason  */
1672e9d0b13bSChris Mason int btrfs_clean_old_snapshots(struct btrfs_root *root)
1673e9d0b13bSChris Mason {
16745d4f98a2SYan Zheng 	LIST_HEAD(list);
16755d4f98a2SYan Zheng 	struct btrfs_fs_info *fs_info = root->fs_info;
1676e9d0b13bSChris Mason 
1677a4abeea4SJosef Bacik 	spin_lock(&fs_info->trans_lock);
16785d4f98a2SYan Zheng 	list_splice_init(&fs_info->dead_roots, &list);
1679a4abeea4SJosef Bacik 	spin_unlock(&fs_info->trans_lock);
16805d4f98a2SYan Zheng 
16815d4f98a2SYan Zheng 	while (!list_empty(&list)) {
16822c536799SJeff Mahoney 		int ret;
16832c536799SJeff Mahoney 
16845d4f98a2SYan Zheng 		root = list_entry(list.next, struct btrfs_root, root_list);
168576dda93cSYan, Zheng 		list_del(&root->root_list);
168676dda93cSYan, Zheng 
168716cdcec7SMiao Xie 		btrfs_kill_all_delayed_nodes(root);
168816cdcec7SMiao Xie 
168976dda93cSYan, Zheng 		if (btrfs_header_backref_rev(root->node) <
169076dda93cSYan, Zheng 		    BTRFS_MIXED_BACKREF_REV)
16912c536799SJeff Mahoney 			ret = btrfs_drop_snapshot(root, NULL, 0, 0);
169276dda93cSYan, Zheng 		else
16932c536799SJeff Mahoney 			ret =btrfs_drop_snapshot(root, NULL, 1, 0);
16942c536799SJeff Mahoney 		BUG_ON(ret < 0);
1695e9d0b13bSChris Mason 	}
1696e9d0b13bSChris Mason 	return 0;
1697e9d0b13bSChris Mason }
1698