xref: /openbmc/linux/fs/btrfs/transaction.c (revision 60376ce4)
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,
27560376ce4SJosef Bacik 	TRANS_JOIN_FREEZE,
276249ac1e5SJosef Bacik };
277249ac1e5SJosef Bacik 
278a22285a6SYan, Zheng static int may_wait_transaction(struct btrfs_root *root, int type)
27937d1aeeeSChris Mason {
280a4abeea4SJosef Bacik 	if (root->fs_info->log_root_recovering)
281a4abeea4SJosef Bacik 		return 0;
282a4abeea4SJosef Bacik 
283a4abeea4SJosef Bacik 	if (type == TRANS_USERSPACE)
284a22285a6SYan, Zheng 		return 1;
285a4abeea4SJosef Bacik 
286a4abeea4SJosef Bacik 	if (type == TRANS_START &&
287a4abeea4SJosef Bacik 	    !atomic_read(&root->fs_info->open_ioctl_trans))
288a4abeea4SJosef Bacik 		return 1;
289a4abeea4SJosef Bacik 
290a22285a6SYan, Zheng 	return 0;
291a22285a6SYan, Zheng }
292a22285a6SYan, Zheng 
293a22285a6SYan, Zheng static struct btrfs_trans_handle *start_transaction(struct btrfs_root *root,
2948407aa46SMiao Xie 						    u64 num_items, int type,
2958407aa46SMiao Xie 						    int noflush)
296a22285a6SYan, Zheng {
297a22285a6SYan, Zheng 	struct btrfs_trans_handle *h;
298a22285a6SYan, Zheng 	struct btrfs_transaction *cur_trans;
299b5009945SJosef Bacik 	u64 num_bytes = 0;
300a22285a6SYan, Zheng 	int ret;
301c5567237SArne Jansen 	u64 qgroup_reserved = 0;
302acce952bSliubo 
303acce952bSliubo 	if (root->fs_info->fs_state & BTRFS_SUPER_FLAG_ERROR)
304acce952bSliubo 		return ERR_PTR(-EROFS);
3052a1eb461SJosef Bacik 
3062a1eb461SJosef Bacik 	if (current->journal_info) {
3072a1eb461SJosef Bacik 		WARN_ON(type != TRANS_JOIN && type != TRANS_JOIN_NOLOCK);
3082a1eb461SJosef Bacik 		h = current->journal_info;
3092a1eb461SJosef Bacik 		h->use_count++;
3102a1eb461SJosef Bacik 		h->orig_rsv = h->block_rsv;
3112a1eb461SJosef Bacik 		h->block_rsv = NULL;
3122a1eb461SJosef Bacik 		goto got_it;
3132a1eb461SJosef Bacik 	}
314b5009945SJosef Bacik 
315b5009945SJosef Bacik 	/*
316b5009945SJosef Bacik 	 * Do the reservation before we join the transaction so we can do all
317b5009945SJosef Bacik 	 * the appropriate flushing if need be.
318b5009945SJosef Bacik 	 */
319b5009945SJosef Bacik 	if (num_items > 0 && root != root->fs_info->chunk_root) {
320c5567237SArne Jansen 		if (root->fs_info->quota_enabled &&
321c5567237SArne Jansen 		    is_fstree(root->root_key.objectid)) {
322c5567237SArne Jansen 			qgroup_reserved = num_items * root->leafsize;
323c5567237SArne Jansen 			ret = btrfs_qgroup_reserve(root, qgroup_reserved);
324c5567237SArne Jansen 			if (ret)
325c5567237SArne Jansen 				return ERR_PTR(ret);
326c5567237SArne Jansen 		}
327c5567237SArne Jansen 
328b5009945SJosef Bacik 		num_bytes = btrfs_calc_trans_metadata_size(root, num_items);
3298407aa46SMiao Xie 		if (noflush)
3308407aa46SMiao Xie 			ret = btrfs_block_rsv_add_noflush(root,
3318407aa46SMiao Xie 						&root->fs_info->trans_block_rsv,
3328407aa46SMiao Xie 						num_bytes);
3338407aa46SMiao Xie 		else
3344a92b1b8SJosef Bacik 			ret = btrfs_block_rsv_add(root,
335b5009945SJosef Bacik 						&root->fs_info->trans_block_rsv,
336b5009945SJosef Bacik 						num_bytes);
337b5009945SJosef Bacik 		if (ret)
338b5009945SJosef Bacik 			return ERR_PTR(ret);
339b5009945SJosef Bacik 	}
340a22285a6SYan, Zheng again:
341a22285a6SYan, Zheng 	h = kmem_cache_alloc(btrfs_trans_handle_cachep, GFP_NOFS);
342a22285a6SYan, Zheng 	if (!h)
343a22285a6SYan, Zheng 		return ERR_PTR(-ENOMEM);
344a22285a6SYan, Zheng 
34560376ce4SJosef Bacik 	if (!__sb_start_write(root->fs_info->sb, SB_FREEZE_FS, false)) {
34660376ce4SJosef Bacik 		if (type == TRANS_JOIN_FREEZE)
34760376ce4SJosef Bacik 			return ERR_PTR(-EPERM);
348b2b5ef5cSJan Kara 		sb_start_intwrite(root->fs_info->sb);
34960376ce4SJosef Bacik 	}
350b2b5ef5cSJan Kara 
351a22285a6SYan, Zheng 	if (may_wait_transaction(root, type))
35237d1aeeeSChris Mason 		wait_current_trans(root);
353a22285a6SYan, Zheng 
354a4abeea4SJosef Bacik 	do {
355a4abeea4SJosef Bacik 		ret = join_transaction(root, type == TRANS_JOIN_NOLOCK);
356a4abeea4SJosef Bacik 		if (ret == -EBUSY)
357a4abeea4SJosef Bacik 			wait_current_trans(root);
358a4abeea4SJosef Bacik 	} while (ret == -EBUSY);
359a4abeea4SJosef Bacik 
360db5b493aSTsutomu Itoh 	if (ret < 0) {
361b2b5ef5cSJan Kara 		sb_end_intwrite(root->fs_info->sb);
3626e8df2aeSYoshinori Sano 		kmem_cache_free(btrfs_trans_handle_cachep, h);
363db5b493aSTsutomu Itoh 		return ERR_PTR(ret);
364db5b493aSTsutomu Itoh 	}
3650f7d52f4SChris Mason 
366a22285a6SYan, Zheng 	cur_trans = root->fs_info->running_transaction;
367a22285a6SYan, Zheng 
368a22285a6SYan, Zheng 	h->transid = cur_trans->transid;
369a22285a6SYan, Zheng 	h->transaction = cur_trans;
37079154b1bSChris Mason 	h->blocks_used = 0;
371a22285a6SYan, Zheng 	h->bytes_reserved = 0;
372d13603efSArne Jansen 	h->root = root;
37356bec294SChris Mason 	h->delayed_ref_updates = 0;
3742a1eb461SJosef Bacik 	h->use_count = 1;
3750e721106SJosef Bacik 	h->adding_csums = 0;
376f0486c68SYan, Zheng 	h->block_rsv = NULL;
3772a1eb461SJosef Bacik 	h->orig_rsv = NULL;
37849b25e05SJeff Mahoney 	h->aborted = 0;
379c5567237SArne Jansen 	h->qgroup_reserved = qgroup_reserved;
380bed92eaeSArne Jansen 	h->delayed_ref_elem.seq = 0;
381bed92eaeSArne Jansen 	INIT_LIST_HEAD(&h->qgroup_ref_list);
382ea658badSJosef Bacik 	INIT_LIST_HEAD(&h->new_bgs);
383b7ec40d7SChris Mason 
384a22285a6SYan, Zheng 	smp_mb();
385a22285a6SYan, Zheng 	if (cur_trans->blocked && may_wait_transaction(root, type)) {
386a22285a6SYan, Zheng 		btrfs_commit_transaction(h, root);
387a22285a6SYan, Zheng 		goto again;
388a22285a6SYan, Zheng 	}
3899ed74f2dSJosef Bacik 
390b5009945SJosef Bacik 	if (num_bytes) {
3918c2a3ca2SJosef Bacik 		trace_btrfs_space_reservation(root->fs_info, "transaction",
3922bcc0328SLiu Bo 					      h->transid, num_bytes, 1);
393b5009945SJosef Bacik 		h->block_rsv = &root->fs_info->trans_block_rsv;
394b5009945SJosef Bacik 		h->bytes_reserved = num_bytes;
395a22285a6SYan, Zheng 	}
396a22285a6SYan, Zheng 
3972a1eb461SJosef Bacik got_it:
398a4abeea4SJosef Bacik 	btrfs_record_root_in_trans(h, root);
399a22285a6SYan, Zheng 
400a22285a6SYan, Zheng 	if (!current->journal_info && type != TRANS_USERSPACE)
401a22285a6SYan, Zheng 		current->journal_info = h;
40279154b1bSChris Mason 	return h;
40379154b1bSChris Mason }
40479154b1bSChris Mason 
405f9295749SChris Mason struct btrfs_trans_handle *btrfs_start_transaction(struct btrfs_root *root,
406a22285a6SYan, Zheng 						   int num_items)
407f9295749SChris Mason {
4088407aa46SMiao Xie 	return start_transaction(root, num_items, TRANS_START, 0);
409f9295749SChris Mason }
4108407aa46SMiao Xie 
4118407aa46SMiao Xie struct btrfs_trans_handle *btrfs_start_transaction_noflush(
4128407aa46SMiao Xie 					struct btrfs_root *root, int num_items)
4138407aa46SMiao Xie {
4148407aa46SMiao Xie 	return start_transaction(root, num_items, TRANS_START, 1);
4158407aa46SMiao Xie }
4168407aa46SMiao Xie 
4177a7eaa40SJosef Bacik struct btrfs_trans_handle *btrfs_join_transaction(struct btrfs_root *root)
418f9295749SChris Mason {
4198407aa46SMiao Xie 	return start_transaction(root, 0, TRANS_JOIN, 0);
420f9295749SChris Mason }
421f9295749SChris Mason 
4227a7eaa40SJosef Bacik struct btrfs_trans_handle *btrfs_join_transaction_nolock(struct btrfs_root *root)
4230af3d00bSJosef Bacik {
4248407aa46SMiao Xie 	return start_transaction(root, 0, TRANS_JOIN_NOLOCK, 0);
4250af3d00bSJosef Bacik }
4260af3d00bSJosef Bacik 
4277a7eaa40SJosef Bacik struct btrfs_trans_handle *btrfs_start_ioctl_transaction(struct btrfs_root *root)
4289ca9ee09SSage Weil {
4298407aa46SMiao Xie 	return start_transaction(root, 0, TRANS_USERSPACE, 0);
4309ca9ee09SSage Weil }
4319ca9ee09SSage Weil 
43260376ce4SJosef Bacik struct btrfs_trans_handle *btrfs_join_transaction_freeze(struct btrfs_root *root)
43360376ce4SJosef Bacik {
43460376ce4SJosef Bacik 	return start_transaction(root, 0, TRANS_JOIN_FREEZE, 0);
43560376ce4SJosef Bacik }
43660376ce4SJosef Bacik 
437d352ac68SChris Mason /* wait for a transaction commit to be fully complete */
438b9c8300cSLi Zefan static noinline void wait_for_commit(struct btrfs_root *root,
43989ce8a63SChris Mason 				    struct btrfs_transaction *commit)
44089ce8a63SChris Mason {
44172d63ed6SLi Zefan 	wait_event(commit->commit_wait, commit->commit_done);
44289ce8a63SChris Mason }
44389ce8a63SChris Mason 
44446204592SSage Weil int btrfs_wait_for_commit(struct btrfs_root *root, u64 transid)
44546204592SSage Weil {
44646204592SSage Weil 	struct btrfs_transaction *cur_trans = NULL, *t;
44746204592SSage Weil 	int ret;
44846204592SSage Weil 
44946204592SSage Weil 	ret = 0;
45046204592SSage Weil 	if (transid) {
45146204592SSage Weil 		if (transid <= root->fs_info->last_trans_committed)
452a4abeea4SJosef Bacik 			goto out;
45346204592SSage Weil 
45446204592SSage Weil 		/* find specified transaction */
455a4abeea4SJosef Bacik 		spin_lock(&root->fs_info->trans_lock);
45646204592SSage Weil 		list_for_each_entry(t, &root->fs_info->trans_list, list) {
45746204592SSage Weil 			if (t->transid == transid) {
45846204592SSage Weil 				cur_trans = t;
459a4abeea4SJosef Bacik 				atomic_inc(&cur_trans->use_count);
46046204592SSage Weil 				break;
46146204592SSage Weil 			}
46246204592SSage Weil 			if (t->transid > transid)
46346204592SSage Weil 				break;
46446204592SSage Weil 		}
465a4abeea4SJosef Bacik 		spin_unlock(&root->fs_info->trans_lock);
46646204592SSage Weil 		ret = -EINVAL;
46746204592SSage Weil 		if (!cur_trans)
468a4abeea4SJosef Bacik 			goto out;  /* bad transid */
46946204592SSage Weil 	} else {
47046204592SSage Weil 		/* find newest transaction that is committing | committed */
471a4abeea4SJosef Bacik 		spin_lock(&root->fs_info->trans_lock);
47246204592SSage Weil 		list_for_each_entry_reverse(t, &root->fs_info->trans_list,
47346204592SSage Weil 					    list) {
47446204592SSage Weil 			if (t->in_commit) {
47546204592SSage Weil 				if (t->commit_done)
4763473f3c0SJosef Bacik 					break;
47746204592SSage Weil 				cur_trans = t;
478a4abeea4SJosef Bacik 				atomic_inc(&cur_trans->use_count);
47946204592SSage Weil 				break;
48046204592SSage Weil 			}
48146204592SSage Weil 		}
482a4abeea4SJosef Bacik 		spin_unlock(&root->fs_info->trans_lock);
48346204592SSage Weil 		if (!cur_trans)
484a4abeea4SJosef Bacik 			goto out;  /* nothing committing|committed */
48546204592SSage Weil 	}
48646204592SSage Weil 
48746204592SSage Weil 	wait_for_commit(root, cur_trans);
48846204592SSage Weil 
48946204592SSage Weil 	put_transaction(cur_trans);
49046204592SSage Weil 	ret = 0;
491a4abeea4SJosef Bacik out:
49246204592SSage Weil 	return ret;
49346204592SSage Weil }
49446204592SSage Weil 
49537d1aeeeSChris Mason void btrfs_throttle(struct btrfs_root *root)
49637d1aeeeSChris Mason {
497a4abeea4SJosef Bacik 	if (!atomic_read(&root->fs_info->open_ioctl_trans))
49837d1aeeeSChris Mason 		wait_current_trans(root);
49937d1aeeeSChris Mason }
50037d1aeeeSChris Mason 
5018929ecfaSYan, Zheng static int should_end_transaction(struct btrfs_trans_handle *trans,
5028929ecfaSYan, Zheng 				  struct btrfs_root *root)
5038929ecfaSYan, Zheng {
5048929ecfaSYan, Zheng 	int ret;
50536ba022aSJosef Bacik 
50636ba022aSJosef Bacik 	ret = btrfs_block_rsv_check(root, &root->fs_info->global_block_rsv, 5);
5078929ecfaSYan, Zheng 	return ret ? 1 : 0;
5088929ecfaSYan, Zheng }
5098929ecfaSYan, Zheng 
5108929ecfaSYan, Zheng int btrfs_should_end_transaction(struct btrfs_trans_handle *trans,
5118929ecfaSYan, Zheng 				 struct btrfs_root *root)
5128929ecfaSYan, Zheng {
5138929ecfaSYan, Zheng 	struct btrfs_transaction *cur_trans = trans->transaction;
5148929ecfaSYan, Zheng 	int updates;
51549b25e05SJeff Mahoney 	int err;
5168929ecfaSYan, Zheng 
517a4abeea4SJosef Bacik 	smp_mb();
5188929ecfaSYan, Zheng 	if (cur_trans->blocked || cur_trans->delayed_refs.flushing)
5198929ecfaSYan, Zheng 		return 1;
5208929ecfaSYan, Zheng 
5218929ecfaSYan, Zheng 	updates = trans->delayed_ref_updates;
5228929ecfaSYan, Zheng 	trans->delayed_ref_updates = 0;
52349b25e05SJeff Mahoney 	if (updates) {
52449b25e05SJeff Mahoney 		err = btrfs_run_delayed_refs(trans, root, updates);
52549b25e05SJeff Mahoney 		if (err) /* Error code will also eval true */
52649b25e05SJeff Mahoney 			return err;
52749b25e05SJeff Mahoney 	}
5288929ecfaSYan, Zheng 
5298929ecfaSYan, Zheng 	return should_end_transaction(trans, root);
5308929ecfaSYan, Zheng }
5318929ecfaSYan, Zheng 
53289ce8a63SChris Mason static int __btrfs_end_transaction(struct btrfs_trans_handle *trans,
5330af3d00bSJosef Bacik 			  struct btrfs_root *root, int throttle, int lock)
53479154b1bSChris Mason {
5358929ecfaSYan, Zheng 	struct btrfs_transaction *cur_trans = trans->transaction;
536ab78c84dSChris Mason 	struct btrfs_fs_info *info = root->fs_info;
537c3e69d58SChris Mason 	int count = 0;
5384edc2ca3SDave Jones 	int err = 0;
539d6e4a428SChris Mason 
5402a1eb461SJosef Bacik 	if (--trans->use_count) {
5412a1eb461SJosef Bacik 		trans->block_rsv = trans->orig_rsv;
5422a1eb461SJosef Bacik 		return 0;
5432a1eb461SJosef Bacik 	}
5442a1eb461SJosef Bacik 
545edf39272SJan Schmidt 	/*
546edf39272SJan Schmidt 	 * do the qgroup accounting as early as possible
547edf39272SJan Schmidt 	 */
548edf39272SJan Schmidt 	err = btrfs_delayed_refs_qgroup_accounting(trans, info);
549edf39272SJan Schmidt 
550b24e03dbSJosef Bacik 	btrfs_trans_release_metadata(trans, root);
5514c13d758SJosef Bacik 	trans->block_rsv = NULL;
552d13603efSArne Jansen 	/*
553d13603efSArne Jansen 	 * the same root has to be passed to start_transaction and
554d13603efSArne Jansen 	 * end_transaction. Subvolume quota depends on this.
555d13603efSArne Jansen 	 */
556d13603efSArne Jansen 	WARN_ON(trans->root != root);
557c5567237SArne Jansen 
558c5567237SArne Jansen 	if (trans->qgroup_reserved) {
559c5567237SArne Jansen 		btrfs_qgroup_free(root, trans->qgroup_reserved);
560c5567237SArne Jansen 		trans->qgroup_reserved = 0;
561c5567237SArne Jansen 	}
562c5567237SArne Jansen 
563ea658badSJosef Bacik 	if (!list_empty(&trans->new_bgs))
564ea658badSJosef Bacik 		btrfs_create_pending_block_groups(trans, root);
565ea658badSJosef Bacik 
566203bf287SChris Mason 	while (count < 2) {
567c3e69d58SChris Mason 		unsigned long cur = trans->delayed_ref_updates;
568c3e69d58SChris Mason 		trans->delayed_ref_updates = 0;
569c3e69d58SChris Mason 		if (cur &&
570c3e69d58SChris Mason 		    trans->transaction->delayed_refs.num_heads_ready > 64) {
571c3e69d58SChris Mason 			trans->delayed_ref_updates = 0;
572c3e69d58SChris Mason 			btrfs_run_delayed_refs(trans, root, cur);
573c3e69d58SChris Mason 		} else {
574c3e69d58SChris Mason 			break;
575c3e69d58SChris Mason 		}
576c3e69d58SChris Mason 		count++;
57756bec294SChris Mason 	}
5780e721106SJosef Bacik 	btrfs_trans_release_metadata(trans, root);
5790e721106SJosef Bacik 	trans->block_rsv = NULL;
58056bec294SChris Mason 
581ea658badSJosef Bacik 	if (!list_empty(&trans->new_bgs))
582ea658badSJosef Bacik 		btrfs_create_pending_block_groups(trans, root);
583ea658badSJosef Bacik 
584a4abeea4SJosef Bacik 	if (lock && !atomic_read(&root->fs_info->open_ioctl_trans) &&
585a4abeea4SJosef Bacik 	    should_end_transaction(trans, root)) {
5868929ecfaSYan, Zheng 		trans->transaction->blocked = 1;
587a4abeea4SJosef Bacik 		smp_wmb();
588a4abeea4SJosef Bacik 	}
5898929ecfaSYan, Zheng 
5900af3d00bSJosef Bacik 	if (lock && cur_trans->blocked && !cur_trans->in_commit) {
59181317fdeSJosef Bacik 		if (throttle) {
59281317fdeSJosef Bacik 			/*
59381317fdeSJosef Bacik 			 * We may race with somebody else here so end up having
59481317fdeSJosef Bacik 			 * to call end_transaction on ourselves again, so inc
59581317fdeSJosef Bacik 			 * our use_count.
59681317fdeSJosef Bacik 			 */
59781317fdeSJosef Bacik 			trans->use_count++;
5988929ecfaSYan, Zheng 			return btrfs_commit_transaction(trans, root);
59981317fdeSJosef Bacik 		} else {
6008929ecfaSYan, Zheng 			wake_up_process(info->transaction_kthread);
6018929ecfaSYan, Zheng 		}
60281317fdeSJosef Bacik 	}
6038929ecfaSYan, Zheng 
6046df7881aSJosef Bacik 	sb_end_intwrite(root->fs_info->sb);
6056df7881aSJosef Bacik 
6068929ecfaSYan, Zheng 	WARN_ON(cur_trans != info->running_transaction);
60713c5a93eSJosef Bacik 	WARN_ON(atomic_read(&cur_trans->num_writers) < 1);
60813c5a93eSJosef Bacik 	atomic_dec(&cur_trans->num_writers);
60989ce8a63SChris Mason 
61099d16cbcSSage Weil 	smp_mb();
61179154b1bSChris Mason 	if (waitqueue_active(&cur_trans->writer_wait))
61279154b1bSChris Mason 		wake_up(&cur_trans->writer_wait);
61379154b1bSChris Mason 	put_transaction(cur_trans);
6149ed74f2dSJosef Bacik 
6159ed74f2dSJosef Bacik 	if (current->journal_info == trans)
6169ed74f2dSJosef Bacik 		current->journal_info = NULL;
617ab78c84dSChris Mason 
61824bbcf04SYan, Zheng 	if (throttle)
61924bbcf04SYan, Zheng 		btrfs_run_delayed_iputs(root);
62024bbcf04SYan, Zheng 
62149b25e05SJeff Mahoney 	if (trans->aborted ||
62249b25e05SJeff Mahoney 	    root->fs_info->fs_state & BTRFS_SUPER_FLAG_ERROR) {
6234edc2ca3SDave Jones 		err = -EIO;
62449b25e05SJeff Mahoney 	}
625edf39272SJan Schmidt 	assert_qgroups_uptodate(trans);
62649b25e05SJeff Mahoney 
6274edc2ca3SDave Jones 	memset(trans, 0, sizeof(*trans));
6284edc2ca3SDave Jones 	kmem_cache_free(btrfs_trans_handle_cachep, trans);
6294edc2ca3SDave Jones 	return err;
63079154b1bSChris Mason }
63179154b1bSChris Mason 
63289ce8a63SChris Mason int btrfs_end_transaction(struct btrfs_trans_handle *trans,
63389ce8a63SChris Mason 			  struct btrfs_root *root)
63489ce8a63SChris Mason {
63516cdcec7SMiao Xie 	int ret;
63616cdcec7SMiao Xie 
63716cdcec7SMiao Xie 	ret = __btrfs_end_transaction(trans, root, 0, 1);
63816cdcec7SMiao Xie 	if (ret)
63916cdcec7SMiao Xie 		return ret;
64016cdcec7SMiao Xie 	return 0;
64189ce8a63SChris Mason }
64289ce8a63SChris Mason 
64389ce8a63SChris Mason int btrfs_end_transaction_throttle(struct btrfs_trans_handle *trans,
64489ce8a63SChris Mason 				   struct btrfs_root *root)
64589ce8a63SChris Mason {
64616cdcec7SMiao Xie 	int ret;
64716cdcec7SMiao Xie 
64816cdcec7SMiao Xie 	ret = __btrfs_end_transaction(trans, root, 1, 1);
64916cdcec7SMiao Xie 	if (ret)
65016cdcec7SMiao Xie 		return ret;
65116cdcec7SMiao Xie 	return 0;
6520af3d00bSJosef Bacik }
6530af3d00bSJosef Bacik 
6540af3d00bSJosef Bacik int btrfs_end_transaction_nolock(struct btrfs_trans_handle *trans,
6550af3d00bSJosef Bacik 				 struct btrfs_root *root)
6560af3d00bSJosef Bacik {
65716cdcec7SMiao Xie 	int ret;
65816cdcec7SMiao Xie 
65916cdcec7SMiao Xie 	ret = __btrfs_end_transaction(trans, root, 0, 0);
66016cdcec7SMiao Xie 	if (ret)
66116cdcec7SMiao Xie 		return ret;
66216cdcec7SMiao Xie 	return 0;
66316cdcec7SMiao Xie }
66416cdcec7SMiao Xie 
66516cdcec7SMiao Xie int btrfs_end_transaction_dmeta(struct btrfs_trans_handle *trans,
66616cdcec7SMiao Xie 				struct btrfs_root *root)
66716cdcec7SMiao Xie {
66816cdcec7SMiao Xie 	return __btrfs_end_transaction(trans, root, 1, 1);
66989ce8a63SChris Mason }
67089ce8a63SChris Mason 
671d352ac68SChris Mason /*
672d352ac68SChris Mason  * when btree blocks are allocated, they have some corresponding bits set for
673d352ac68SChris Mason  * them in one of two extent_io trees.  This is used to make sure all of
674690587d1SChris Mason  * those extents are sent to disk but does not wait on them
675d352ac68SChris Mason  */
676690587d1SChris Mason int btrfs_write_marked_extents(struct btrfs_root *root,
6778cef4e16SYan, Zheng 			       struct extent_io_tree *dirty_pages, int mark)
67879154b1bSChris Mason {
679777e6bd7SChris Mason 	int err = 0;
6807c4452b9SChris Mason 	int werr = 0;
6811728366eSJosef Bacik 	struct address_space *mapping = root->fs_info->btree_inode->i_mapping;
682777e6bd7SChris Mason 	u64 start = 0;
6835f39d397SChris Mason 	u64 end;
6847c4452b9SChris Mason 
6851728366eSJosef Bacik 	while (!find_first_extent_bit(dirty_pages, start, &start, &end,
6861728366eSJosef Bacik 				      mark)) {
6871728366eSJosef Bacik 		convert_extent_bit(dirty_pages, start, end, EXTENT_NEED_WAIT, mark,
6881728366eSJosef Bacik 				   GFP_NOFS);
6891728366eSJosef Bacik 		err = filemap_fdatawrite_range(mapping, start, end);
6907c4452b9SChris Mason 		if (err)
6917c4452b9SChris Mason 			werr = err;
6921728366eSJosef Bacik 		cond_resched();
6931728366eSJosef Bacik 		start = end + 1;
6947c4452b9SChris Mason 	}
695690587d1SChris Mason 	if (err)
696690587d1SChris Mason 		werr = err;
697690587d1SChris Mason 	return werr;
698690587d1SChris Mason }
699690587d1SChris Mason 
700690587d1SChris Mason /*
701690587d1SChris Mason  * when btree blocks are allocated, they have some corresponding bits set for
702690587d1SChris Mason  * them in one of two extent_io trees.  This is used to make sure all of
703690587d1SChris Mason  * those extents are on disk for transaction or log commit.  We wait
704690587d1SChris Mason  * on all the pages and clear them from the dirty pages state tree
705690587d1SChris Mason  */
706690587d1SChris Mason int btrfs_wait_marked_extents(struct btrfs_root *root,
7078cef4e16SYan, Zheng 			      struct extent_io_tree *dirty_pages, int mark)
708690587d1SChris Mason {
709690587d1SChris Mason 	int err = 0;
710690587d1SChris Mason 	int werr = 0;
7111728366eSJosef Bacik 	struct address_space *mapping = root->fs_info->btree_inode->i_mapping;
712690587d1SChris Mason 	u64 start = 0;
713690587d1SChris Mason 	u64 end;
714690587d1SChris Mason 
7151728366eSJosef Bacik 	while (!find_first_extent_bit(dirty_pages, start, &start, &end,
7161728366eSJosef Bacik 				      EXTENT_NEED_WAIT)) {
7171728366eSJosef Bacik 		clear_extent_bits(dirty_pages, start, end, EXTENT_NEED_WAIT, GFP_NOFS);
7181728366eSJosef Bacik 		err = filemap_fdatawait_range(mapping, start, end);
719777e6bd7SChris Mason 		if (err)
720777e6bd7SChris Mason 			werr = err;
721777e6bd7SChris Mason 		cond_resched();
7221728366eSJosef Bacik 		start = end + 1;
723777e6bd7SChris Mason 	}
7247c4452b9SChris Mason 	if (err)
7257c4452b9SChris Mason 		werr = err;
7267c4452b9SChris Mason 	return werr;
72779154b1bSChris Mason }
72879154b1bSChris Mason 
729690587d1SChris Mason /*
730690587d1SChris Mason  * when btree blocks are allocated, they have some corresponding bits set for
731690587d1SChris Mason  * them in one of two extent_io trees.  This is used to make sure all of
732690587d1SChris Mason  * those extents are on disk for transaction or log commit
733690587d1SChris Mason  */
734690587d1SChris Mason int btrfs_write_and_wait_marked_extents(struct btrfs_root *root,
7358cef4e16SYan, Zheng 				struct extent_io_tree *dirty_pages, int mark)
736690587d1SChris Mason {
737690587d1SChris Mason 	int ret;
738690587d1SChris Mason 	int ret2;
739690587d1SChris Mason 
7408cef4e16SYan, Zheng 	ret = btrfs_write_marked_extents(root, dirty_pages, mark);
7418cef4e16SYan, Zheng 	ret2 = btrfs_wait_marked_extents(root, dirty_pages, mark);
742bf0da8c1SChris Mason 
743bf0da8c1SChris Mason 	if (ret)
744bf0da8c1SChris Mason 		return ret;
745bf0da8c1SChris Mason 	if (ret2)
746bf0da8c1SChris Mason 		return ret2;
747bf0da8c1SChris Mason 	return 0;
748690587d1SChris Mason }
749690587d1SChris Mason 
750d0c803c4SChris Mason int btrfs_write_and_wait_transaction(struct btrfs_trans_handle *trans,
751d0c803c4SChris Mason 				     struct btrfs_root *root)
752d0c803c4SChris Mason {
753d0c803c4SChris Mason 	if (!trans || !trans->transaction) {
754d0c803c4SChris Mason 		struct inode *btree_inode;
755d0c803c4SChris Mason 		btree_inode = root->fs_info->btree_inode;
756d0c803c4SChris Mason 		return filemap_write_and_wait(btree_inode->i_mapping);
757d0c803c4SChris Mason 	}
758d0c803c4SChris Mason 	return btrfs_write_and_wait_marked_extents(root,
7598cef4e16SYan, Zheng 					   &trans->transaction->dirty_pages,
7608cef4e16SYan, Zheng 					   EXTENT_DIRTY);
761d0c803c4SChris Mason }
762d0c803c4SChris Mason 
763d352ac68SChris Mason /*
764d352ac68SChris Mason  * this is used to update the root pointer in the tree of tree roots.
765d352ac68SChris Mason  *
766d352ac68SChris Mason  * But, in the case of the extent allocation tree, updating the root
767d352ac68SChris Mason  * pointer may allocate blocks which may change the root of the extent
768d352ac68SChris Mason  * allocation tree.
769d352ac68SChris Mason  *
770d352ac68SChris Mason  * So, this loops and repeats and makes sure the cowonly root didn't
771d352ac68SChris Mason  * change while the root pointer was being updated in the metadata.
772d352ac68SChris Mason  */
7730b86a832SChris Mason static int update_cowonly_root(struct btrfs_trans_handle *trans,
77479154b1bSChris Mason 			       struct btrfs_root *root)
77579154b1bSChris Mason {
77679154b1bSChris Mason 	int ret;
7770b86a832SChris Mason 	u64 old_root_bytenr;
77886b9f2ecSYan, Zheng 	u64 old_root_used;
7790b86a832SChris Mason 	struct btrfs_root *tree_root = root->fs_info->tree_root;
78079154b1bSChris Mason 
78186b9f2ecSYan, Zheng 	old_root_used = btrfs_root_used(&root->root_item);
7820b86a832SChris Mason 	btrfs_write_dirty_block_groups(trans, root);
78356bec294SChris Mason 
78479154b1bSChris Mason 	while (1) {
7850b86a832SChris Mason 		old_root_bytenr = btrfs_root_bytenr(&root->root_item);
78686b9f2ecSYan, Zheng 		if (old_root_bytenr == root->node->start &&
78786b9f2ecSYan, Zheng 		    old_root_used == btrfs_root_used(&root->root_item))
78879154b1bSChris Mason 			break;
78987ef2bb4SChris Mason 
7905d4f98a2SYan Zheng 		btrfs_set_root_node(&root->root_item, root->node);
79179154b1bSChris Mason 		ret = btrfs_update_root(trans, tree_root,
7920b86a832SChris Mason 					&root->root_key,
7930b86a832SChris Mason 					&root->root_item);
79449b25e05SJeff Mahoney 		if (ret)
79549b25e05SJeff Mahoney 			return ret;
79656bec294SChris Mason 
79786b9f2ecSYan, Zheng 		old_root_used = btrfs_root_used(&root->root_item);
7984a8c9a62SYan Zheng 		ret = btrfs_write_dirty_block_groups(trans, root);
79949b25e05SJeff Mahoney 		if (ret)
80049b25e05SJeff Mahoney 			return ret;
8010b86a832SChris Mason 	}
802276e680dSYan Zheng 
803276e680dSYan Zheng 	if (root != root->fs_info->extent_root)
804817d52f8SJosef Bacik 		switch_commit_root(root);
805276e680dSYan Zheng 
8060b86a832SChris Mason 	return 0;
8070b86a832SChris Mason }
8080b86a832SChris Mason 
809d352ac68SChris Mason /*
810d352ac68SChris Mason  * update all the cowonly tree roots on disk
81149b25e05SJeff Mahoney  *
81249b25e05SJeff Mahoney  * The error handling in this function may not be obvious. Any of the
81349b25e05SJeff Mahoney  * failures will cause the file system to go offline. We still need
81449b25e05SJeff Mahoney  * to clean up the delayed refs.
815d352ac68SChris Mason  */
8165d4f98a2SYan Zheng static noinline int commit_cowonly_roots(struct btrfs_trans_handle *trans,
8170b86a832SChris Mason 					 struct btrfs_root *root)
8180b86a832SChris Mason {
8190b86a832SChris Mason 	struct btrfs_fs_info *fs_info = root->fs_info;
8200b86a832SChris Mason 	struct list_head *next;
82184234f3aSYan Zheng 	struct extent_buffer *eb;
82256bec294SChris Mason 	int ret;
82384234f3aSYan Zheng 
82456bec294SChris Mason 	ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
82549b25e05SJeff Mahoney 	if (ret)
82649b25e05SJeff Mahoney 		return ret;
82787ef2bb4SChris Mason 
82884234f3aSYan Zheng 	eb = btrfs_lock_root_node(fs_info->tree_root);
82949b25e05SJeff Mahoney 	ret = btrfs_cow_block(trans, fs_info->tree_root, eb, NULL,
83049b25e05SJeff Mahoney 			      0, &eb);
83184234f3aSYan Zheng 	btrfs_tree_unlock(eb);
83284234f3aSYan Zheng 	free_extent_buffer(eb);
8330b86a832SChris Mason 
83449b25e05SJeff Mahoney 	if (ret)
83549b25e05SJeff Mahoney 		return ret;
83649b25e05SJeff Mahoney 
83756bec294SChris Mason 	ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
83849b25e05SJeff Mahoney 	if (ret)
83949b25e05SJeff Mahoney 		return ret;
84087ef2bb4SChris Mason 
841733f4fbbSStefan Behrens 	ret = btrfs_run_dev_stats(trans, root->fs_info);
842733f4fbbSStefan Behrens 	BUG_ON(ret);
843733f4fbbSStefan Behrens 
844546adb0dSJan Schmidt 	ret = btrfs_run_qgroups(trans, root->fs_info);
845546adb0dSJan Schmidt 	BUG_ON(ret);
846546adb0dSJan Schmidt 
847546adb0dSJan Schmidt 	/* run_qgroups might have added some more refs */
848546adb0dSJan Schmidt 	ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
849546adb0dSJan Schmidt 	BUG_ON(ret);
850546adb0dSJan Schmidt 
8510b86a832SChris Mason 	while (!list_empty(&fs_info->dirty_cowonly_roots)) {
8520b86a832SChris Mason 		next = fs_info->dirty_cowonly_roots.next;
8530b86a832SChris Mason 		list_del_init(next);
8540b86a832SChris Mason 		root = list_entry(next, struct btrfs_root, dirty_list);
85587ef2bb4SChris Mason 
85649b25e05SJeff Mahoney 		ret = update_cowonly_root(trans, root);
85749b25e05SJeff Mahoney 		if (ret)
85849b25e05SJeff Mahoney 			return ret;
85979154b1bSChris Mason 	}
860276e680dSYan Zheng 
861276e680dSYan Zheng 	down_write(&fs_info->extent_commit_sem);
862276e680dSYan Zheng 	switch_commit_root(fs_info->extent_root);
863276e680dSYan Zheng 	up_write(&fs_info->extent_commit_sem);
864276e680dSYan Zheng 
86579154b1bSChris Mason 	return 0;
86679154b1bSChris Mason }
86779154b1bSChris Mason 
868d352ac68SChris Mason /*
869d352ac68SChris Mason  * dead roots are old snapshots that need to be deleted.  This allocates
870d352ac68SChris Mason  * a dirty root struct and adds it into the list of dead roots that need to
871d352ac68SChris Mason  * be deleted
872d352ac68SChris Mason  */
8735d4f98a2SYan Zheng int btrfs_add_dead_root(struct btrfs_root *root)
8745eda7b5eSChris Mason {
875a4abeea4SJosef Bacik 	spin_lock(&root->fs_info->trans_lock);
8765d4f98a2SYan Zheng 	list_add(&root->root_list, &root->fs_info->dead_roots);
877a4abeea4SJosef Bacik 	spin_unlock(&root->fs_info->trans_lock);
8785eda7b5eSChris Mason 	return 0;
8795eda7b5eSChris Mason }
8805eda7b5eSChris Mason 
881d352ac68SChris Mason /*
8825d4f98a2SYan Zheng  * update all the cowonly tree roots on disk
883d352ac68SChris Mason  */
8845d4f98a2SYan Zheng static noinline int commit_fs_roots(struct btrfs_trans_handle *trans,
8855d4f98a2SYan Zheng 				    struct btrfs_root *root)
8860f7d52f4SChris Mason {
8870f7d52f4SChris Mason 	struct btrfs_root *gang[8];
8885d4f98a2SYan Zheng 	struct btrfs_fs_info *fs_info = root->fs_info;
8890f7d52f4SChris Mason 	int i;
8900f7d52f4SChris Mason 	int ret;
89154aa1f4dSChris Mason 	int err = 0;
89254aa1f4dSChris Mason 
893a4abeea4SJosef Bacik 	spin_lock(&fs_info->fs_roots_radix_lock);
8940f7d52f4SChris Mason 	while (1) {
8955d4f98a2SYan Zheng 		ret = radix_tree_gang_lookup_tag(&fs_info->fs_roots_radix,
8965d4f98a2SYan Zheng 						 (void **)gang, 0,
8970f7d52f4SChris Mason 						 ARRAY_SIZE(gang),
8980f7d52f4SChris Mason 						 BTRFS_ROOT_TRANS_TAG);
8990f7d52f4SChris Mason 		if (ret == 0)
9000f7d52f4SChris Mason 			break;
9010f7d52f4SChris Mason 		for (i = 0; i < ret; i++) {
9020f7d52f4SChris Mason 			root = gang[i];
9035d4f98a2SYan Zheng 			radix_tree_tag_clear(&fs_info->fs_roots_radix,
9042619ba1fSChris Mason 					(unsigned long)root->root_key.objectid,
9050f7d52f4SChris Mason 					BTRFS_ROOT_TRANS_TAG);
906a4abeea4SJosef Bacik 			spin_unlock(&fs_info->fs_roots_radix_lock);
90731153d81SYan Zheng 
908e02119d5SChris Mason 			btrfs_free_log(trans, root);
9095d4f98a2SYan Zheng 			btrfs_update_reloc_root(trans, root);
910d68fc57bSYan, Zheng 			btrfs_orphan_commit_root(trans, root);
911e02119d5SChris Mason 
91282d5902dSLi Zefan 			btrfs_save_ino_cache(root, trans);
91382d5902dSLi Zefan 
914f1ebcc74SLiu Bo 			/* see comments in should_cow_block() */
915f1ebcc74SLiu Bo 			root->force_cow = 0;
916f1ebcc74SLiu Bo 			smp_wmb();
917f1ebcc74SLiu Bo 
918978d910dSYan Zheng 			if (root->commit_root != root->node) {
919581bb050SLi Zefan 				mutex_lock(&root->fs_commit_mutex);
920817d52f8SJosef Bacik 				switch_commit_root(root);
921581bb050SLi Zefan 				btrfs_unpin_free_ino(root);
922581bb050SLi Zefan 				mutex_unlock(&root->fs_commit_mutex);
923581bb050SLi Zefan 
924978d910dSYan Zheng 				btrfs_set_root_node(&root->root_item,
925978d910dSYan Zheng 						    root->node);
926978d910dSYan Zheng 			}
92731153d81SYan Zheng 
9285d4f98a2SYan Zheng 			err = btrfs_update_root(trans, fs_info->tree_root,
9290f7d52f4SChris Mason 						&root->root_key,
9300f7d52f4SChris Mason 						&root->root_item);
931a4abeea4SJosef Bacik 			spin_lock(&fs_info->fs_roots_radix_lock);
93254aa1f4dSChris Mason 			if (err)
93354aa1f4dSChris Mason 				break;
9340f7d52f4SChris Mason 		}
9359f3a7427SChris Mason 	}
936a4abeea4SJosef Bacik 	spin_unlock(&fs_info->fs_roots_radix_lock);
93754aa1f4dSChris Mason 	return err;
9380f7d52f4SChris Mason }
9390f7d52f4SChris Mason 
940d352ac68SChris Mason /*
941d352ac68SChris Mason  * defrag a given btree.  If cacheonly == 1, this won't read from the disk,
942d352ac68SChris Mason  * otherwise every leaf in the btree is read and defragged.
943d352ac68SChris Mason  */
944e9d0b13bSChris Mason int btrfs_defrag_root(struct btrfs_root *root, int cacheonly)
945e9d0b13bSChris Mason {
946e9d0b13bSChris Mason 	struct btrfs_fs_info *info = root->fs_info;
947e9d0b13bSChris Mason 	struct btrfs_trans_handle *trans;
9488929ecfaSYan, Zheng 	int ret;
949d3c2fdcfSChris Mason 	unsigned long nr;
950e9d0b13bSChris Mason 
9518929ecfaSYan, Zheng 	if (xchg(&root->defrag_running, 1))
952e9d0b13bSChris Mason 		return 0;
9538929ecfaSYan, Zheng 
9546b80053dSChris Mason 	while (1) {
9558929ecfaSYan, Zheng 		trans = btrfs_start_transaction(root, 0);
9568929ecfaSYan, Zheng 		if (IS_ERR(trans))
9578929ecfaSYan, Zheng 			return PTR_ERR(trans);
9588929ecfaSYan, Zheng 
959e9d0b13bSChris Mason 		ret = btrfs_defrag_leaves(trans, root, cacheonly);
9608929ecfaSYan, Zheng 
961d3c2fdcfSChris Mason 		nr = trans->blocks_used;
962e9d0b13bSChris Mason 		btrfs_end_transaction(trans, root);
963d3c2fdcfSChris Mason 		btrfs_btree_balance_dirty(info->tree_root, nr);
964e9d0b13bSChris Mason 		cond_resched();
965e9d0b13bSChris Mason 
9667841cb28SDavid Sterba 		if (btrfs_fs_closing(root->fs_info) || ret != -EAGAIN)
967e9d0b13bSChris Mason 			break;
968e9d0b13bSChris Mason 	}
969e9d0b13bSChris Mason 	root->defrag_running = 0;
9708929ecfaSYan, Zheng 	return ret;
971e9d0b13bSChris Mason }
972e9d0b13bSChris Mason 
973d352ac68SChris Mason /*
974d352ac68SChris Mason  * new snapshots need to be created at a very specific time in the
975d352ac68SChris Mason  * transaction commit.  This does the actual creation
976d352ac68SChris Mason  */
97780b6794dSChris Mason static noinline int create_pending_snapshot(struct btrfs_trans_handle *trans,
9783063d29fSChris Mason 				   struct btrfs_fs_info *fs_info,
9793063d29fSChris Mason 				   struct btrfs_pending_snapshot *pending)
9803063d29fSChris Mason {
9813063d29fSChris Mason 	struct btrfs_key key;
98280b6794dSChris Mason 	struct btrfs_root_item *new_root_item;
9833063d29fSChris Mason 	struct btrfs_root *tree_root = fs_info->tree_root;
9843063d29fSChris Mason 	struct btrfs_root *root = pending->root;
9856bdb72deSSage Weil 	struct btrfs_root *parent_root;
98698c9942aSLiu Bo 	struct btrfs_block_rsv *rsv;
9876bdb72deSSage Weil 	struct inode *parent_inode;
98842874b3dSMiao Xie 	struct btrfs_path *path;
98942874b3dSMiao Xie 	struct btrfs_dir_item *dir_item;
9906a912213SJosef Bacik 	struct dentry *parent;
991a22285a6SYan, Zheng 	struct dentry *dentry;
9923063d29fSChris Mason 	struct extent_buffer *tmp;
993925baeddSChris Mason 	struct extent_buffer *old;
9948ea05e3aSAlexander Block 	struct timespec cur_time = CURRENT_TIME;
9953063d29fSChris Mason 	int ret;
996d68fc57bSYan, Zheng 	u64 to_reserve = 0;
9976bdb72deSSage Weil 	u64 index = 0;
998a22285a6SYan, Zheng 	u64 objectid;
999b83cc969SLi Zefan 	u64 root_flags;
10008ea05e3aSAlexander Block 	uuid_le new_uuid;
10013063d29fSChris Mason 
100242874b3dSMiao Xie 	path = btrfs_alloc_path();
100342874b3dSMiao Xie 	if (!path) {
100442874b3dSMiao Xie 		ret = pending->error = -ENOMEM;
100542874b3dSMiao Xie 		goto path_alloc_fail;
100642874b3dSMiao Xie 	}
100742874b3dSMiao Xie 
100880b6794dSChris Mason 	new_root_item = kmalloc(sizeof(*new_root_item), GFP_NOFS);
100980b6794dSChris Mason 	if (!new_root_item) {
101049b25e05SJeff Mahoney 		ret = pending->error = -ENOMEM;
10116fa9700eSMiao Xie 		goto root_item_alloc_fail;
101280b6794dSChris Mason 	}
1013a22285a6SYan, Zheng 
1014581bb050SLi Zefan 	ret = btrfs_find_free_objectid(tree_root, &objectid);
1015a22285a6SYan, Zheng 	if (ret) {
1016a22285a6SYan, Zheng 		pending->error = ret;
10176fa9700eSMiao Xie 		goto no_free_objectid;
1018a22285a6SYan, Zheng 	}
10193063d29fSChris Mason 
10203fd0a558SYan, Zheng 	btrfs_reloc_pre_snapshot(trans, pending, &to_reserve);
1021d68fc57bSYan, Zheng 
1022d68fc57bSYan, Zheng 	if (to_reserve > 0) {
102362f30c54SMiao Xie 		ret = btrfs_block_rsv_add_noflush(root, &pending->block_rsv,
10248bb8ab2eSJosef Bacik 						  to_reserve);
1025d68fc57bSYan, Zheng 		if (ret) {
1026d68fc57bSYan, Zheng 			pending->error = ret;
10276fa9700eSMiao Xie 			goto no_free_objectid;
1028d68fc57bSYan, Zheng 		}
1029d68fc57bSYan, Zheng 	}
1030d68fc57bSYan, Zheng 
10316f72c7e2SArne Jansen 	ret = btrfs_qgroup_inherit(trans, fs_info, root->root_key.objectid,
10326f72c7e2SArne Jansen 				   objectid, pending->inherit);
10336f72c7e2SArne Jansen 	if (ret) {
10346f72c7e2SArne Jansen 		pending->error = ret;
10356fa9700eSMiao Xie 		goto no_free_objectid;
10366f72c7e2SArne Jansen 	}
10376f72c7e2SArne Jansen 
10383063d29fSChris Mason 	key.objectid = objectid;
1039a22285a6SYan, Zheng 	key.offset = (u64)-1;
1040a22285a6SYan, Zheng 	key.type = BTRFS_ROOT_ITEM_KEY;
10413063d29fSChris Mason 
10426fa9700eSMiao Xie 	rsv = trans->block_rsv;
1043a22285a6SYan, Zheng 	trans->block_rsv = &pending->block_rsv;
10446bdb72deSSage Weil 
1045a22285a6SYan, Zheng 	dentry = pending->dentry;
10466a912213SJosef Bacik 	parent = dget_parent(dentry);
10476a912213SJosef Bacik 	parent_inode = parent->d_inode;
1048a22285a6SYan, Zheng 	parent_root = BTRFS_I(parent_inode)->root;
10497585717fSChris Mason 	record_root_in_trans(trans, parent_root);
1050a22285a6SYan, Zheng 
10516bdb72deSSage Weil 	/*
10526bdb72deSSage Weil 	 * insert the directory item
10536bdb72deSSage Weil 	 */
10546bdb72deSSage Weil 	ret = btrfs_set_inode_index(parent_inode, &index);
105549b25e05SJeff Mahoney 	BUG_ON(ret); /* -ENOMEM */
105642874b3dSMiao Xie 
105742874b3dSMiao Xie 	/* check if there is a file/dir which has the same name. */
105842874b3dSMiao Xie 	dir_item = btrfs_lookup_dir_item(NULL, parent_root, path,
105942874b3dSMiao Xie 					 btrfs_ino(parent_inode),
106042874b3dSMiao Xie 					 dentry->d_name.name,
106142874b3dSMiao Xie 					 dentry->d_name.len, 0);
106242874b3dSMiao Xie 	if (dir_item != NULL && !IS_ERR(dir_item)) {
1063fe66a05aSChris Mason 		pending->error = -EEXIST;
1064fe66a05aSChris Mason 		goto fail;
106542874b3dSMiao Xie 	} else if (IS_ERR(dir_item)) {
106642874b3dSMiao Xie 		ret = PTR_ERR(dir_item);
10676fa9700eSMiao Xie 		goto abort_trans;
106879787eaaSJeff Mahoney 	}
106942874b3dSMiao Xie 	btrfs_release_path(path);
10706bdb72deSSage Weil 
1071e999376fSChris Mason 	/*
1072e999376fSChris Mason 	 * pull in the delayed directory update
1073e999376fSChris Mason 	 * and the delayed inode item
1074e999376fSChris Mason 	 * otherwise we corrupt the FS during
1075e999376fSChris Mason 	 * snapshot
1076e999376fSChris Mason 	 */
1077e999376fSChris Mason 	ret = btrfs_run_delayed_items(trans, root);
10786fa9700eSMiao Xie 	if (ret)	/* Transaction aborted */
10796fa9700eSMiao Xie 		goto abort_trans;
1080e999376fSChris Mason 
10817585717fSChris Mason 	record_root_in_trans(trans, root);
10826bdb72deSSage Weil 	btrfs_set_root_last_snapshot(&root->root_item, trans->transid);
10836bdb72deSSage Weil 	memcpy(new_root_item, &root->root_item, sizeof(*new_root_item));
108408fe4db1SLi Zefan 	btrfs_check_and_init_root_item(new_root_item);
10856bdb72deSSage Weil 
1086b83cc969SLi Zefan 	root_flags = btrfs_root_flags(new_root_item);
1087b83cc969SLi Zefan 	if (pending->readonly)
1088b83cc969SLi Zefan 		root_flags |= BTRFS_ROOT_SUBVOL_RDONLY;
1089b83cc969SLi Zefan 	else
1090b83cc969SLi Zefan 		root_flags &= ~BTRFS_ROOT_SUBVOL_RDONLY;
1091b83cc969SLi Zefan 	btrfs_set_root_flags(new_root_item, root_flags);
1092b83cc969SLi Zefan 
10938ea05e3aSAlexander Block 	btrfs_set_root_generation_v2(new_root_item,
10948ea05e3aSAlexander Block 			trans->transid);
10958ea05e3aSAlexander Block 	uuid_le_gen(&new_uuid);
10968ea05e3aSAlexander Block 	memcpy(new_root_item->uuid, new_uuid.b, BTRFS_UUID_SIZE);
10978ea05e3aSAlexander Block 	memcpy(new_root_item->parent_uuid, root->root_item.uuid,
10988ea05e3aSAlexander Block 			BTRFS_UUID_SIZE);
10998ea05e3aSAlexander Block 	new_root_item->otime.sec = cpu_to_le64(cur_time.tv_sec);
1100dadd1105SDan Carpenter 	new_root_item->otime.nsec = cpu_to_le32(cur_time.tv_nsec);
11018ea05e3aSAlexander Block 	btrfs_set_root_otransid(new_root_item, trans->transid);
11028ea05e3aSAlexander Block 	memset(&new_root_item->stime, 0, sizeof(new_root_item->stime));
11038ea05e3aSAlexander Block 	memset(&new_root_item->rtime, 0, sizeof(new_root_item->rtime));
11048ea05e3aSAlexander Block 	btrfs_set_root_stransid(new_root_item, 0);
11058ea05e3aSAlexander Block 	btrfs_set_root_rtransid(new_root_item, 0);
11068ea05e3aSAlexander Block 
1107925baeddSChris Mason 	old = btrfs_lock_root_node(root);
110849b25e05SJeff Mahoney 	ret = btrfs_cow_block(trans, root, old, NULL, 0, &old);
110979787eaaSJeff Mahoney 	if (ret) {
111079787eaaSJeff Mahoney 		btrfs_tree_unlock(old);
111179787eaaSJeff Mahoney 		free_extent_buffer(old);
11126fa9700eSMiao Xie 		goto abort_trans;
111379787eaaSJeff Mahoney 	}
111449b25e05SJeff Mahoney 
11155d4f98a2SYan Zheng 	btrfs_set_lock_blocking(old);
11163063d29fSChris Mason 
111749b25e05SJeff Mahoney 	ret = btrfs_copy_root(trans, root, old, &tmp, objectid);
111879787eaaSJeff Mahoney 	/* clean up in any case */
1119925baeddSChris Mason 	btrfs_tree_unlock(old);
1120925baeddSChris Mason 	free_extent_buffer(old);
112179787eaaSJeff Mahoney 	if (ret)
11226fa9700eSMiao Xie 		goto abort_trans;
11233063d29fSChris Mason 
1124f1ebcc74SLiu Bo 	/* see comments in should_cow_block() */
1125f1ebcc74SLiu Bo 	root->force_cow = 1;
1126f1ebcc74SLiu Bo 	smp_wmb();
1127f1ebcc74SLiu Bo 
11285d4f98a2SYan Zheng 	btrfs_set_root_node(new_root_item, tmp);
1129a22285a6SYan, Zheng 	/* record when the snapshot was created in key.offset */
1130a22285a6SYan, Zheng 	key.offset = trans->transid;
1131a22285a6SYan, Zheng 	ret = btrfs_insert_root(trans, tree_root, &key, new_root_item);
1132925baeddSChris Mason 	btrfs_tree_unlock(tmp);
11333063d29fSChris Mason 	free_extent_buffer(tmp);
113449b25e05SJeff Mahoney 	if (ret)
11356fa9700eSMiao Xie 		goto abort_trans;
11360660b5afSChris Mason 
1137a22285a6SYan, Zheng 	/*
1138a22285a6SYan, Zheng 	 * insert root back/forward references
1139a22285a6SYan, Zheng 	 */
1140a22285a6SYan, Zheng 	ret = btrfs_add_root_ref(trans, tree_root, objectid,
1141a22285a6SYan, Zheng 				 parent_root->root_key.objectid,
114233345d01SLi Zefan 				 btrfs_ino(parent_inode), index,
1143a22285a6SYan, Zheng 				 dentry->d_name.name, dentry->d_name.len);
114449b25e05SJeff Mahoney 	if (ret)
11456fa9700eSMiao Xie 		goto abort_trans;
1146a22285a6SYan, Zheng 
1147a22285a6SYan, Zheng 	key.offset = (u64)-1;
1148a22285a6SYan, Zheng 	pending->snap = btrfs_read_fs_root_no_name(root->fs_info, &key);
114979787eaaSJeff Mahoney 	if (IS_ERR(pending->snap)) {
115079787eaaSJeff Mahoney 		ret = PTR_ERR(pending->snap);
115149b25e05SJeff Mahoney 		goto abort_trans;
115279787eaaSJeff Mahoney 	}
1153d68fc57bSYan, Zheng 
115449b25e05SJeff Mahoney 	ret = btrfs_reloc_post_snapshot(trans, pending);
115549b25e05SJeff Mahoney 	if (ret)
115649b25e05SJeff Mahoney 		goto abort_trans;
1157361048f5SMiao Xie 
1158361048f5SMiao Xie 	ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
1159361048f5SMiao Xie 	if (ret)
1160361048f5SMiao Xie 		goto abort_trans;
116142874b3dSMiao Xie 
116242874b3dSMiao Xie 	ret = btrfs_insert_dir_item(trans, parent_root,
116342874b3dSMiao Xie 				    dentry->d_name.name, dentry->d_name.len,
116442874b3dSMiao Xie 				    parent_inode, &key,
116542874b3dSMiao Xie 				    BTRFS_FT_DIR, index);
116642874b3dSMiao Xie 	/* We have check then name at the beginning, so it is impossible. */
116742874b3dSMiao Xie 	BUG_ON(ret == -EEXIST);
116842874b3dSMiao Xie 	if (ret)
116942874b3dSMiao Xie 		goto abort_trans;
117042874b3dSMiao Xie 
117142874b3dSMiao Xie 	btrfs_i_size_write(parent_inode, parent_inode->i_size +
117242874b3dSMiao Xie 					 dentry->d_name.len * 2);
117342874b3dSMiao Xie 	parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
117442874b3dSMiao Xie 	ret = btrfs_update_inode(trans, parent_root, parent_inode);
117542874b3dSMiao Xie 	if (ret)
117642874b3dSMiao Xie 		goto abort_trans;
11773063d29fSChris Mason fail:
11786fa9700eSMiao Xie 	dput(parent);
117998c9942aSLiu Bo 	trans->block_rsv = rsv;
11806fa9700eSMiao Xie no_free_objectid:
11816fa9700eSMiao Xie 	kfree(new_root_item);
11826fa9700eSMiao Xie root_item_alloc_fail:
118342874b3dSMiao Xie 	btrfs_free_path(path);
118442874b3dSMiao Xie path_alloc_fail:
1185a22285a6SYan, Zheng 	btrfs_block_rsv_release(root, &pending->block_rsv, (u64)-1);
118649b25e05SJeff Mahoney 	return ret;
118749b25e05SJeff Mahoney 
118849b25e05SJeff Mahoney abort_trans:
118949b25e05SJeff Mahoney 	btrfs_abort_transaction(trans, root, ret);
119049b25e05SJeff Mahoney 	goto fail;
11913063d29fSChris Mason }
11923063d29fSChris Mason 
1193d352ac68SChris Mason /*
1194d352ac68SChris Mason  * create all the snapshots we've scheduled for creation
1195d352ac68SChris Mason  */
119680b6794dSChris Mason static noinline int create_pending_snapshots(struct btrfs_trans_handle *trans,
11973063d29fSChris Mason 					     struct btrfs_fs_info *fs_info)
11983063d29fSChris Mason {
11993063d29fSChris Mason 	struct btrfs_pending_snapshot *pending;
12003063d29fSChris Mason 	struct list_head *head = &trans->transaction->pending_snapshots;
12013de4586cSChris Mason 
1202fe66a05aSChris Mason 	list_for_each_entry(pending, head, list)
1203fe66a05aSChris Mason 		create_pending_snapshot(trans, fs_info, pending);
12043de4586cSChris Mason 	return 0;
12053de4586cSChris Mason }
12063de4586cSChris Mason 
12075d4f98a2SYan Zheng static void update_super_roots(struct btrfs_root *root)
12085d4f98a2SYan Zheng {
12095d4f98a2SYan Zheng 	struct btrfs_root_item *root_item;
12105d4f98a2SYan Zheng 	struct btrfs_super_block *super;
12115d4f98a2SYan Zheng 
12126c41761fSDavid Sterba 	super = root->fs_info->super_copy;
12135d4f98a2SYan Zheng 
12145d4f98a2SYan Zheng 	root_item = &root->fs_info->chunk_root->root_item;
12155d4f98a2SYan Zheng 	super->chunk_root = root_item->bytenr;
12165d4f98a2SYan Zheng 	super->chunk_root_generation = root_item->generation;
12175d4f98a2SYan Zheng 	super->chunk_root_level = root_item->level;
12185d4f98a2SYan Zheng 
12195d4f98a2SYan Zheng 	root_item = &root->fs_info->tree_root->root_item;
12205d4f98a2SYan Zheng 	super->root = root_item->bytenr;
12215d4f98a2SYan Zheng 	super->generation = root_item->generation;
12225d4f98a2SYan Zheng 	super->root_level = root_item->level;
122373bc1876SJosef Bacik 	if (btrfs_test_opt(root, SPACE_CACHE))
12240af3d00bSJosef Bacik 		super->cache_generation = root_item->generation;
12255d4f98a2SYan Zheng }
12265d4f98a2SYan Zheng 
1227f36f3042SChris Mason int btrfs_transaction_in_commit(struct btrfs_fs_info *info)
1228f36f3042SChris Mason {
1229f36f3042SChris Mason 	int ret = 0;
1230a4abeea4SJosef Bacik 	spin_lock(&info->trans_lock);
1231f36f3042SChris Mason 	if (info->running_transaction)
1232f36f3042SChris Mason 		ret = info->running_transaction->in_commit;
1233a4abeea4SJosef Bacik 	spin_unlock(&info->trans_lock);
1234f36f3042SChris Mason 	return ret;
1235f36f3042SChris Mason }
1236f36f3042SChris Mason 
12378929ecfaSYan, Zheng int btrfs_transaction_blocked(struct btrfs_fs_info *info)
12388929ecfaSYan, Zheng {
12398929ecfaSYan, Zheng 	int ret = 0;
1240a4abeea4SJosef Bacik 	spin_lock(&info->trans_lock);
12418929ecfaSYan, Zheng 	if (info->running_transaction)
12428929ecfaSYan, Zheng 		ret = info->running_transaction->blocked;
1243a4abeea4SJosef Bacik 	spin_unlock(&info->trans_lock);
12448929ecfaSYan, Zheng 	return ret;
12458929ecfaSYan, Zheng }
12468929ecfaSYan, Zheng 
1247bb9c12c9SSage Weil /*
1248bb9c12c9SSage Weil  * wait for the current transaction commit to start and block subsequent
1249bb9c12c9SSage Weil  * transaction joins
1250bb9c12c9SSage Weil  */
1251bb9c12c9SSage Weil static void wait_current_trans_commit_start(struct btrfs_root *root,
1252bb9c12c9SSage Weil 					    struct btrfs_transaction *trans)
1253bb9c12c9SSage Weil {
125472d63ed6SLi Zefan 	wait_event(root->fs_info->transaction_blocked_wait, trans->in_commit);
1255bb9c12c9SSage Weil }
1256bb9c12c9SSage Weil 
1257bb9c12c9SSage Weil /*
1258bb9c12c9SSage Weil  * wait for the current transaction to start and then become unblocked.
1259bb9c12c9SSage Weil  * caller holds ref.
1260bb9c12c9SSage Weil  */
1261bb9c12c9SSage Weil static void wait_current_trans_commit_start_and_unblock(struct btrfs_root *root,
1262bb9c12c9SSage Weil 					 struct btrfs_transaction *trans)
1263bb9c12c9SSage Weil {
126472d63ed6SLi Zefan 	wait_event(root->fs_info->transaction_wait,
126572d63ed6SLi Zefan 		   trans->commit_done || (trans->in_commit && !trans->blocked));
1266bb9c12c9SSage Weil }
1267bb9c12c9SSage Weil 
1268bb9c12c9SSage Weil /*
1269bb9c12c9SSage Weil  * commit transactions asynchronously. once btrfs_commit_transaction_async
1270bb9c12c9SSage Weil  * returns, any subsequent transaction will not be allowed to join.
1271bb9c12c9SSage Weil  */
1272bb9c12c9SSage Weil struct btrfs_async_commit {
1273bb9c12c9SSage Weil 	struct btrfs_trans_handle *newtrans;
1274bb9c12c9SSage Weil 	struct btrfs_root *root;
1275bb9c12c9SSage Weil 	struct delayed_work work;
1276bb9c12c9SSage Weil };
1277bb9c12c9SSage Weil 
1278bb9c12c9SSage Weil static void do_async_commit(struct work_struct *work)
1279bb9c12c9SSage Weil {
1280bb9c12c9SSage Weil 	struct btrfs_async_commit *ac =
1281bb9c12c9SSage Weil 		container_of(work, struct btrfs_async_commit, work.work);
1282bb9c12c9SSage Weil 
12836fc4e354SSage Weil 	/*
12846fc4e354SSage Weil 	 * We've got freeze protection passed with the transaction.
12856fc4e354SSage Weil 	 * Tell lockdep about it.
12866fc4e354SSage Weil 	 */
12876fc4e354SSage Weil 	rwsem_acquire_read(
12886fc4e354SSage Weil 		&ac->root->fs_info->sb->s_writers.lock_map[SB_FREEZE_FS-1],
12896fc4e354SSage Weil 		0, 1, _THIS_IP_);
12906fc4e354SSage Weil 
1291e209db7aSSage Weil 	current->journal_info = ac->newtrans;
1292e209db7aSSage Weil 
1293bb9c12c9SSage Weil 	btrfs_commit_transaction(ac->newtrans, ac->root);
1294bb9c12c9SSage Weil 	kfree(ac);
1295bb9c12c9SSage Weil }
1296bb9c12c9SSage Weil 
1297bb9c12c9SSage Weil int btrfs_commit_transaction_async(struct btrfs_trans_handle *trans,
1298bb9c12c9SSage Weil 				   struct btrfs_root *root,
1299bb9c12c9SSage Weil 				   int wait_for_unblock)
1300bb9c12c9SSage Weil {
1301bb9c12c9SSage Weil 	struct btrfs_async_commit *ac;
1302bb9c12c9SSage Weil 	struct btrfs_transaction *cur_trans;
1303bb9c12c9SSage Weil 
1304bb9c12c9SSage Weil 	ac = kmalloc(sizeof(*ac), GFP_NOFS);
1305db5b493aSTsutomu Itoh 	if (!ac)
1306db5b493aSTsutomu Itoh 		return -ENOMEM;
1307bb9c12c9SSage Weil 
1308bb9c12c9SSage Weil 	INIT_DELAYED_WORK(&ac->work, do_async_commit);
1309bb9c12c9SSage Weil 	ac->root = root;
13107a7eaa40SJosef Bacik 	ac->newtrans = btrfs_join_transaction(root);
13113612b495STsutomu Itoh 	if (IS_ERR(ac->newtrans)) {
13123612b495STsutomu Itoh 		int err = PTR_ERR(ac->newtrans);
13133612b495STsutomu Itoh 		kfree(ac);
13143612b495STsutomu Itoh 		return err;
13153612b495STsutomu Itoh 	}
1316bb9c12c9SSage Weil 
1317bb9c12c9SSage Weil 	/* take transaction reference */
1318bb9c12c9SSage Weil 	cur_trans = trans->transaction;
131913c5a93eSJosef Bacik 	atomic_inc(&cur_trans->use_count);
1320bb9c12c9SSage Weil 
1321bb9c12c9SSage Weil 	btrfs_end_transaction(trans, root);
13226fc4e354SSage Weil 
13236fc4e354SSage Weil 	/*
13246fc4e354SSage Weil 	 * Tell lockdep we've released the freeze rwsem, since the
13256fc4e354SSage Weil 	 * async commit thread will be the one to unlock it.
13266fc4e354SSage Weil 	 */
13276fc4e354SSage Weil 	rwsem_release(&root->fs_info->sb->s_writers.lock_map[SB_FREEZE_FS-1],
13286fc4e354SSage Weil 		      1, _THIS_IP_);
13296fc4e354SSage Weil 
1330bb9c12c9SSage Weil 	schedule_delayed_work(&ac->work, 0);
1331bb9c12c9SSage Weil 
1332bb9c12c9SSage Weil 	/* wait for transaction to start and unblock */
1333bb9c12c9SSage Weil 	if (wait_for_unblock)
1334bb9c12c9SSage Weil 		wait_current_trans_commit_start_and_unblock(root, cur_trans);
1335bb9c12c9SSage Weil 	else
1336bb9c12c9SSage Weil 		wait_current_trans_commit_start(root, cur_trans);
1337bb9c12c9SSage Weil 
133838e88054SSage Weil 	if (current->journal_info == trans)
133938e88054SSage Weil 		current->journal_info = NULL;
134038e88054SSage Weil 
134138e88054SSage Weil 	put_transaction(cur_trans);
1342bb9c12c9SSage Weil 	return 0;
1343bb9c12c9SSage Weil }
1344bb9c12c9SSage Weil 
134549b25e05SJeff Mahoney 
134649b25e05SJeff Mahoney static void cleanup_transaction(struct btrfs_trans_handle *trans,
13477b8b92afSJosef Bacik 				struct btrfs_root *root, int err)
134849b25e05SJeff Mahoney {
134949b25e05SJeff Mahoney 	struct btrfs_transaction *cur_trans = trans->transaction;
135049b25e05SJeff Mahoney 
135149b25e05SJeff Mahoney 	WARN_ON(trans->use_count > 1);
135249b25e05SJeff Mahoney 
13537b8b92afSJosef Bacik 	btrfs_abort_transaction(trans, root, err);
13547b8b92afSJosef Bacik 
135549b25e05SJeff Mahoney 	spin_lock(&root->fs_info->trans_lock);
135649b25e05SJeff Mahoney 	list_del_init(&cur_trans->list);
1357d7096fc3SJosef Bacik 	if (cur_trans == root->fs_info->running_transaction) {
1358d7096fc3SJosef Bacik 		root->fs_info->running_transaction = NULL;
1359d7096fc3SJosef Bacik 		root->fs_info->trans_no_join = 0;
1360d7096fc3SJosef Bacik 	}
136149b25e05SJeff Mahoney 	spin_unlock(&root->fs_info->trans_lock);
136249b25e05SJeff Mahoney 
136349b25e05SJeff Mahoney 	btrfs_cleanup_one_transaction(trans->transaction, root);
136449b25e05SJeff Mahoney 
136549b25e05SJeff Mahoney 	put_transaction(cur_trans);
136649b25e05SJeff Mahoney 	put_transaction(cur_trans);
136749b25e05SJeff Mahoney 
136849b25e05SJeff Mahoney 	trace_btrfs_transaction_commit(root);
136949b25e05SJeff Mahoney 
137049b25e05SJeff Mahoney 	btrfs_scrub_continue(root);
137149b25e05SJeff Mahoney 
137249b25e05SJeff Mahoney 	if (current->journal_info == trans)
137349b25e05SJeff Mahoney 		current->journal_info = NULL;
137449b25e05SJeff Mahoney 
137549b25e05SJeff Mahoney 	kmem_cache_free(btrfs_trans_handle_cachep, trans);
137649b25e05SJeff Mahoney }
137749b25e05SJeff Mahoney 
1378bb9c12c9SSage Weil /*
1379bb9c12c9SSage Weil  * btrfs_transaction state sequence:
1380bb9c12c9SSage Weil  *    in_commit = 0, blocked = 0  (initial)
1381bb9c12c9SSage Weil  *    in_commit = 1, blocked = 1
1382bb9c12c9SSage Weil  *    blocked = 0
1383bb9c12c9SSage Weil  *    commit_done = 1
1384bb9c12c9SSage Weil  */
138579154b1bSChris Mason int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
138679154b1bSChris Mason 			     struct btrfs_root *root)
138779154b1bSChris Mason {
138815ee9bc7SJosef Bacik 	unsigned long joined = 0;
138949b25e05SJeff Mahoney 	struct btrfs_transaction *cur_trans = trans->transaction;
13908fd17795SChris Mason 	struct btrfs_transaction *prev_trans = NULL;
139179154b1bSChris Mason 	DEFINE_WAIT(wait);
139249b25e05SJeff Mahoney 	int ret = -EIO;
139389573b9cSChris Mason 	int should_grow = 0;
139489573b9cSChris Mason 	unsigned long now = get_seconds();
1395dccae999SSage Weil 	int flush_on_commit = btrfs_test_opt(root, FLUSHONCOMMIT);
139679154b1bSChris Mason 
13975a3f23d5SChris Mason 	btrfs_run_ordered_operations(root, 0);
13985a3f23d5SChris Mason 
139949b25e05SJeff Mahoney 	if (cur_trans->aborted)
140049b25e05SJeff Mahoney 		goto cleanup_transaction;
140149b25e05SJeff Mahoney 
140256bec294SChris Mason 	/* make a pass through all the delayed refs we have so far
140356bec294SChris Mason 	 * any runnings procs may add more while we are here
140456bec294SChris Mason 	 */
140556bec294SChris Mason 	ret = btrfs_run_delayed_refs(trans, root, 0);
140649b25e05SJeff Mahoney 	if (ret)
140749b25e05SJeff Mahoney 		goto cleanup_transaction;
140856bec294SChris Mason 
14090e721106SJosef Bacik 	btrfs_trans_release_metadata(trans, root);
14100e721106SJosef Bacik 	trans->block_rsv = NULL;
14110e721106SJosef Bacik 
1412b7ec40d7SChris Mason 	cur_trans = trans->transaction;
141349b25e05SJeff Mahoney 
141456bec294SChris Mason 	/*
141556bec294SChris Mason 	 * set the flushing flag so procs in this transaction have to
141656bec294SChris Mason 	 * start sending their work down.
141756bec294SChris Mason 	 */
1418b7ec40d7SChris Mason 	cur_trans->delayed_refs.flushing = 1;
141956bec294SChris Mason 
1420ea658badSJosef Bacik 	if (!list_empty(&trans->new_bgs))
1421ea658badSJosef Bacik 		btrfs_create_pending_block_groups(trans, root);
1422ea658badSJosef Bacik 
1423c3e69d58SChris Mason 	ret = btrfs_run_delayed_refs(trans, root, 0);
142449b25e05SJeff Mahoney 	if (ret)
142549b25e05SJeff Mahoney 		goto cleanup_transaction;
142656bec294SChris Mason 
1427a4abeea4SJosef Bacik 	spin_lock(&cur_trans->commit_lock);
1428b7ec40d7SChris Mason 	if (cur_trans->in_commit) {
1429a4abeea4SJosef Bacik 		spin_unlock(&cur_trans->commit_lock);
143013c5a93eSJosef Bacik 		atomic_inc(&cur_trans->use_count);
143149b25e05SJeff Mahoney 		ret = btrfs_end_transaction(trans, root);
1432ccd467d6SChris Mason 
1433b9c8300cSLi Zefan 		wait_for_commit(root, cur_trans);
143415ee9bc7SJosef Bacik 
143579154b1bSChris Mason 		put_transaction(cur_trans);
143615ee9bc7SJosef Bacik 
143749b25e05SJeff Mahoney 		return ret;
143879154b1bSChris Mason 	}
14394313b399SChris Mason 
14402c90e5d6SChris Mason 	trans->transaction->in_commit = 1;
1441f9295749SChris Mason 	trans->transaction->blocked = 1;
1442a4abeea4SJosef Bacik 	spin_unlock(&cur_trans->commit_lock);
1443bb9c12c9SSage Weil 	wake_up(&root->fs_info->transaction_blocked_wait);
1444bb9c12c9SSage Weil 
1445a4abeea4SJosef Bacik 	spin_lock(&root->fs_info->trans_lock);
1446ccd467d6SChris Mason 	if (cur_trans->list.prev != &root->fs_info->trans_list) {
1447ccd467d6SChris Mason 		prev_trans = list_entry(cur_trans->list.prev,
1448ccd467d6SChris Mason 					struct btrfs_transaction, list);
1449ccd467d6SChris Mason 		if (!prev_trans->commit_done) {
145013c5a93eSJosef Bacik 			atomic_inc(&prev_trans->use_count);
1451a4abeea4SJosef Bacik 			spin_unlock(&root->fs_info->trans_lock);
1452ccd467d6SChris Mason 
1453ccd467d6SChris Mason 			wait_for_commit(root, prev_trans);
1454ccd467d6SChris Mason 
145515ee9bc7SJosef Bacik 			put_transaction(prev_trans);
1456a4abeea4SJosef Bacik 		} else {
1457a4abeea4SJosef Bacik 			spin_unlock(&root->fs_info->trans_lock);
1458ccd467d6SChris Mason 		}
1459a4abeea4SJosef Bacik 	} else {
1460a4abeea4SJosef Bacik 		spin_unlock(&root->fs_info->trans_lock);
1461ccd467d6SChris Mason 	}
146215ee9bc7SJosef Bacik 
1463e39e64acSChris Mason 	if (!btrfs_test_opt(root, SSD) &&
1464e39e64acSChris Mason 	    (now < cur_trans->start_time || now - cur_trans->start_time < 1))
146589573b9cSChris Mason 		should_grow = 1;
146689573b9cSChris Mason 
146715ee9bc7SJosef Bacik 	do {
14687ea394f1SYan Zheng 		int snap_pending = 0;
1469a4abeea4SJosef Bacik 
147015ee9bc7SJosef Bacik 		joined = cur_trans->num_joined;
14717ea394f1SYan Zheng 		if (!list_empty(&trans->transaction->pending_snapshots))
14727ea394f1SYan Zheng 			snap_pending = 1;
14737ea394f1SYan Zheng 
14742c90e5d6SChris Mason 		WARN_ON(cur_trans != trans->transaction);
147515ee9bc7SJosef Bacik 
14760bdb1db2SSage Weil 		if (flush_on_commit || snap_pending) {
147724bbcf04SYan, Zheng 			btrfs_start_delalloc_inodes(root, 1);
1478143bede5SJeff Mahoney 			btrfs_wait_ordered_extents(root, 0, 1);
14797ea394f1SYan Zheng 		}
14807ea394f1SYan Zheng 
148116cdcec7SMiao Xie 		ret = btrfs_run_delayed_items(trans, root);
148249b25e05SJeff Mahoney 		if (ret)
148349b25e05SJeff Mahoney 			goto cleanup_transaction;
148416cdcec7SMiao Xie 
14855a3f23d5SChris Mason 		/*
1486edf39272SJan Schmidt 		 * running the delayed items may have added new refs. account
1487edf39272SJan Schmidt 		 * them now so that they hinder processing of more delayed refs
1488edf39272SJan Schmidt 		 * as little as possible.
1489edf39272SJan Schmidt 		 */
1490edf39272SJan Schmidt 		btrfs_delayed_refs_qgroup_accounting(trans, root->fs_info);
1491edf39272SJan Schmidt 
1492edf39272SJan Schmidt 		/*
14935a3f23d5SChris Mason 		 * rename don't use btrfs_join_transaction, so, once we
14945a3f23d5SChris Mason 		 * set the transaction to blocked above, we aren't going
14955a3f23d5SChris Mason 		 * to get any new ordered operations.  We can safely run
14965a3f23d5SChris Mason 		 * it here and no for sure that nothing new will be added
14975a3f23d5SChris Mason 		 * to the list
14985a3f23d5SChris Mason 		 */
14995a3f23d5SChris Mason 		btrfs_run_ordered_operations(root, 1);
15005a3f23d5SChris Mason 
1501ed3b3d31SChris Mason 		prepare_to_wait(&cur_trans->writer_wait, &wait,
1502ed3b3d31SChris Mason 				TASK_UNINTERRUPTIBLE);
1503ed3b3d31SChris Mason 
150413c5a93eSJosef Bacik 		if (atomic_read(&cur_trans->num_writers) > 1)
150599d16cbcSSage Weil 			schedule_timeout(MAX_SCHEDULE_TIMEOUT);
150699d16cbcSSage Weil 		else if (should_grow)
150799d16cbcSSage Weil 			schedule_timeout(1);
150815ee9bc7SJosef Bacik 
150915ee9bc7SJosef Bacik 		finish_wait(&cur_trans->writer_wait, &wait);
1510ed0ca140SJosef Bacik 	} while (atomic_read(&cur_trans->num_writers) > 1 ||
1511ed0ca140SJosef Bacik 		 (should_grow && cur_trans->num_joined != joined));
1512ed0ca140SJosef Bacik 
1513ed0ca140SJosef Bacik 	/*
1514ed0ca140SJosef Bacik 	 * Ok now we need to make sure to block out any other joins while we
1515ed0ca140SJosef Bacik 	 * commit the transaction.  We could have started a join before setting
1516ed0ca140SJosef Bacik 	 * no_join so make sure to wait for num_writers to == 1 again.
1517ed0ca140SJosef Bacik 	 */
1518a4abeea4SJosef Bacik 	spin_lock(&root->fs_info->trans_lock);
1519a4abeea4SJosef Bacik 	root->fs_info->trans_no_join = 1;
1520a4abeea4SJosef Bacik 	spin_unlock(&root->fs_info->trans_lock);
1521ed0ca140SJosef Bacik 	wait_event(cur_trans->writer_wait,
1522ed0ca140SJosef Bacik 		   atomic_read(&cur_trans->num_writers) == 1);
152315ee9bc7SJosef Bacik 
15247585717fSChris Mason 	/*
15257585717fSChris Mason 	 * the reloc mutex makes sure that we stop
15267585717fSChris Mason 	 * the balancing code from coming in and moving
15277585717fSChris Mason 	 * extents around in the middle of the commit
15287585717fSChris Mason 	 */
15297585717fSChris Mason 	mutex_lock(&root->fs_info->reloc_mutex);
15307585717fSChris Mason 
153142874b3dSMiao Xie 	/*
153242874b3dSMiao Xie 	 * We needn't worry about the delayed items because we will
153342874b3dSMiao Xie 	 * deal with them in create_pending_snapshot(), which is the
153442874b3dSMiao Xie 	 * core function of the snapshot creation.
153542874b3dSMiao Xie 	 */
153642874b3dSMiao Xie 	ret = create_pending_snapshots(trans, root->fs_info);
153749b25e05SJeff Mahoney 	if (ret) {
153849b25e05SJeff Mahoney 		mutex_unlock(&root->fs_info->reloc_mutex);
153949b25e05SJeff Mahoney 		goto cleanup_transaction;
154049b25e05SJeff Mahoney 	}
15413063d29fSChris Mason 
154242874b3dSMiao Xie 	/*
154342874b3dSMiao Xie 	 * We insert the dir indexes of the snapshots and update the inode
154442874b3dSMiao Xie 	 * of the snapshots' parents after the snapshot creation, so there
154542874b3dSMiao Xie 	 * are some delayed items which are not dealt with. Now deal with
154642874b3dSMiao Xie 	 * them.
154742874b3dSMiao Xie 	 *
154842874b3dSMiao Xie 	 * We needn't worry that this operation will corrupt the snapshots,
154942874b3dSMiao Xie 	 * because all the tree which are snapshoted will be forced to COW
155042874b3dSMiao Xie 	 * the nodes and leaves.
155142874b3dSMiao Xie 	 */
155242874b3dSMiao Xie 	ret = btrfs_run_delayed_items(trans, root);
155349b25e05SJeff Mahoney 	if (ret) {
155449b25e05SJeff Mahoney 		mutex_unlock(&root->fs_info->reloc_mutex);
155549b25e05SJeff Mahoney 		goto cleanup_transaction;
155649b25e05SJeff Mahoney 	}
155716cdcec7SMiao Xie 
155856bec294SChris Mason 	ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
155949b25e05SJeff Mahoney 	if (ret) {
156049b25e05SJeff Mahoney 		mutex_unlock(&root->fs_info->reloc_mutex);
156149b25e05SJeff Mahoney 		goto cleanup_transaction;
156249b25e05SJeff Mahoney 	}
156356bec294SChris Mason 
1564e999376fSChris Mason 	/*
1565e999376fSChris Mason 	 * make sure none of the code above managed to slip in a
1566e999376fSChris Mason 	 * delayed item
1567e999376fSChris Mason 	 */
1568e999376fSChris Mason 	btrfs_assert_delayed_root_empty(root);
1569e999376fSChris Mason 
15702c90e5d6SChris Mason 	WARN_ON(cur_trans != trans->transaction);
1571dc17ff8fSChris Mason 
1572a2de733cSArne Jansen 	btrfs_scrub_pause(root);
1573e02119d5SChris Mason 	/* btrfs_commit_tree_roots is responsible for getting the
1574e02119d5SChris Mason 	 * various roots consistent with each other.  Every pointer
1575e02119d5SChris Mason 	 * in the tree of tree roots has to point to the most up to date
1576e02119d5SChris Mason 	 * root for every subvolume and other tree.  So, we have to keep
1577e02119d5SChris Mason 	 * the tree logging code from jumping in and changing any
1578e02119d5SChris Mason 	 * of the trees.
1579e02119d5SChris Mason 	 *
1580e02119d5SChris Mason 	 * At this point in the commit, there can't be any tree-log
1581e02119d5SChris Mason 	 * writers, but a little lower down we drop the trans mutex
1582e02119d5SChris Mason 	 * and let new people in.  By holding the tree_log_mutex
1583e02119d5SChris Mason 	 * from now until after the super is written, we avoid races
1584e02119d5SChris Mason 	 * with the tree-log code.
1585e02119d5SChris Mason 	 */
1586e02119d5SChris Mason 	mutex_lock(&root->fs_info->tree_log_mutex);
15871a40e23bSZheng Yan 
15885d4f98a2SYan Zheng 	ret = commit_fs_roots(trans, root);
158949b25e05SJeff Mahoney 	if (ret) {
159049b25e05SJeff Mahoney 		mutex_unlock(&root->fs_info->tree_log_mutex);
1591871383beSDavid Sterba 		mutex_unlock(&root->fs_info->reloc_mutex);
159249b25e05SJeff Mahoney 		goto cleanup_transaction;
159349b25e05SJeff Mahoney 	}
159454aa1f4dSChris Mason 
15955d4f98a2SYan Zheng 	/* commit_fs_roots gets rid of all the tree log roots, it is now
1596e02119d5SChris Mason 	 * safe to free the root of tree log roots
1597e02119d5SChris Mason 	 */
1598e02119d5SChris Mason 	btrfs_free_log_root_tree(trans, root->fs_info);
1599e02119d5SChris Mason 
16005d4f98a2SYan Zheng 	ret = commit_cowonly_roots(trans, root);
160149b25e05SJeff Mahoney 	if (ret) {
160249b25e05SJeff Mahoney 		mutex_unlock(&root->fs_info->tree_log_mutex);
1603871383beSDavid Sterba 		mutex_unlock(&root->fs_info->reloc_mutex);
160449b25e05SJeff Mahoney 		goto cleanup_transaction;
160549b25e05SJeff Mahoney 	}
160654aa1f4dSChris Mason 
160711833d66SYan Zheng 	btrfs_prepare_extent_commit(trans, root);
160811833d66SYan Zheng 
160978fae27eSChris Mason 	cur_trans = root->fs_info->running_transaction;
16105f39d397SChris Mason 
16115d4f98a2SYan Zheng 	btrfs_set_root_node(&root->fs_info->tree_root->root_item,
16125d4f98a2SYan Zheng 			    root->fs_info->tree_root->node);
1613817d52f8SJosef Bacik 	switch_commit_root(root->fs_info->tree_root);
16145d4f98a2SYan Zheng 
16155d4f98a2SYan Zheng 	btrfs_set_root_node(&root->fs_info->chunk_root->root_item,
16165d4f98a2SYan Zheng 			    root->fs_info->chunk_root->node);
1617817d52f8SJosef Bacik 	switch_commit_root(root->fs_info->chunk_root);
16185d4f98a2SYan Zheng 
1619edf39272SJan Schmidt 	assert_qgroups_uptodate(trans);
16205d4f98a2SYan Zheng 	update_super_roots(root);
1621e02119d5SChris Mason 
1622e02119d5SChris Mason 	if (!root->fs_info->log_root_recovering) {
16236c41761fSDavid Sterba 		btrfs_set_super_log_root(root->fs_info->super_copy, 0);
16246c41761fSDavid Sterba 		btrfs_set_super_log_root_level(root->fs_info->super_copy, 0);
1625e02119d5SChris Mason 	}
1626e02119d5SChris Mason 
16276c41761fSDavid Sterba 	memcpy(root->fs_info->super_for_commit, root->fs_info->super_copy,
16286c41761fSDavid Sterba 	       sizeof(*root->fs_info->super_copy));
1629ccd467d6SChris Mason 
1630f9295749SChris Mason 	trans->transaction->blocked = 0;
1631a4abeea4SJosef Bacik 	spin_lock(&root->fs_info->trans_lock);
1632a4abeea4SJosef Bacik 	root->fs_info->running_transaction = NULL;
1633a4abeea4SJosef Bacik 	root->fs_info->trans_no_join = 0;
1634a4abeea4SJosef Bacik 	spin_unlock(&root->fs_info->trans_lock);
16357585717fSChris Mason 	mutex_unlock(&root->fs_info->reloc_mutex);
1636b7ec40d7SChris Mason 
1637f9295749SChris Mason 	wake_up(&root->fs_info->transaction_wait);
1638e6dcd2dcSChris Mason 
163979154b1bSChris Mason 	ret = btrfs_write_and_wait_transaction(trans, root);
164049b25e05SJeff Mahoney 	if (ret) {
164149b25e05SJeff Mahoney 		btrfs_error(root->fs_info, ret,
164249b25e05SJeff Mahoney 			    "Error while writing out transaction.");
164349b25e05SJeff Mahoney 		mutex_unlock(&root->fs_info->tree_log_mutex);
164449b25e05SJeff Mahoney 		goto cleanup_transaction;
164549b25e05SJeff Mahoney 	}
164649b25e05SJeff Mahoney 
164749b25e05SJeff Mahoney 	ret = write_ctree_super(trans, root, 0);
164849b25e05SJeff Mahoney 	if (ret) {
164949b25e05SJeff Mahoney 		mutex_unlock(&root->fs_info->tree_log_mutex);
165049b25e05SJeff Mahoney 		goto cleanup_transaction;
165149b25e05SJeff Mahoney 	}
16524313b399SChris Mason 
1653e02119d5SChris Mason 	/*
1654e02119d5SChris Mason 	 * the super is written, we can safely allow the tree-loggers
1655e02119d5SChris Mason 	 * to go about their business
1656e02119d5SChris Mason 	 */
1657e02119d5SChris Mason 	mutex_unlock(&root->fs_info->tree_log_mutex);
1658e02119d5SChris Mason 
165911833d66SYan Zheng 	btrfs_finish_extent_commit(trans, root);
16604313b399SChris Mason 
16612c90e5d6SChris Mason 	cur_trans->commit_done = 1;
1662b7ec40d7SChris Mason 
166315ee9bc7SJosef Bacik 	root->fs_info->last_trans_committed = cur_trans->transid;
1664817d52f8SJosef Bacik 
16652c90e5d6SChris Mason 	wake_up(&cur_trans->commit_wait);
16663de4586cSChris Mason 
1667a4abeea4SJosef Bacik 	spin_lock(&root->fs_info->trans_lock);
166813c5a93eSJosef Bacik 	list_del_init(&cur_trans->list);
1669a4abeea4SJosef Bacik 	spin_unlock(&root->fs_info->trans_lock);
1670a4abeea4SJosef Bacik 
167179154b1bSChris Mason 	put_transaction(cur_trans);
167278fae27eSChris Mason 	put_transaction(cur_trans);
167358176a96SJosef Bacik 
1674b2b5ef5cSJan Kara 	sb_end_intwrite(root->fs_info->sb);
1675b2b5ef5cSJan Kara 
16761abe9b8aSliubo 	trace_btrfs_transaction_commit(root);
16771abe9b8aSliubo 
1678a2de733cSArne Jansen 	btrfs_scrub_continue(root);
1679a2de733cSArne Jansen 
16809ed74f2dSJosef Bacik 	if (current->journal_info == trans)
16819ed74f2dSJosef Bacik 		current->journal_info = NULL;
16829ed74f2dSJosef Bacik 
16832c90e5d6SChris Mason 	kmem_cache_free(btrfs_trans_handle_cachep, trans);
168424bbcf04SYan, Zheng 
168524bbcf04SYan, Zheng 	if (current != root->fs_info->transaction_kthread)
168624bbcf04SYan, Zheng 		btrfs_run_delayed_iputs(root);
168724bbcf04SYan, Zheng 
168879154b1bSChris Mason 	return ret;
168949b25e05SJeff Mahoney 
169049b25e05SJeff Mahoney cleanup_transaction:
16910e721106SJosef Bacik 	btrfs_trans_release_metadata(trans, root);
16920e721106SJosef Bacik 	trans->block_rsv = NULL;
169349b25e05SJeff Mahoney 	btrfs_printk(root->fs_info, "Skipping commit of aborted transaction.\n");
169449b25e05SJeff Mahoney //	WARN_ON(1);
169549b25e05SJeff Mahoney 	if (current->journal_info == trans)
169649b25e05SJeff Mahoney 		current->journal_info = NULL;
16977b8b92afSJosef Bacik 	cleanup_transaction(trans, root, ret);
169849b25e05SJeff Mahoney 
169949b25e05SJeff Mahoney 	return ret;
170079154b1bSChris Mason }
170179154b1bSChris Mason 
1702d352ac68SChris Mason /*
1703d352ac68SChris Mason  * interface function to delete all the snapshots we have scheduled for deletion
1704d352ac68SChris Mason  */
1705e9d0b13bSChris Mason int btrfs_clean_old_snapshots(struct btrfs_root *root)
1706e9d0b13bSChris Mason {
17075d4f98a2SYan Zheng 	LIST_HEAD(list);
17085d4f98a2SYan Zheng 	struct btrfs_fs_info *fs_info = root->fs_info;
1709e9d0b13bSChris Mason 
1710a4abeea4SJosef Bacik 	spin_lock(&fs_info->trans_lock);
17115d4f98a2SYan Zheng 	list_splice_init(&fs_info->dead_roots, &list);
1712a4abeea4SJosef Bacik 	spin_unlock(&fs_info->trans_lock);
17135d4f98a2SYan Zheng 
17145d4f98a2SYan Zheng 	while (!list_empty(&list)) {
17152c536799SJeff Mahoney 		int ret;
17162c536799SJeff Mahoney 
17175d4f98a2SYan Zheng 		root = list_entry(list.next, struct btrfs_root, root_list);
171876dda93cSYan, Zheng 		list_del(&root->root_list);
171976dda93cSYan, Zheng 
172016cdcec7SMiao Xie 		btrfs_kill_all_delayed_nodes(root);
172116cdcec7SMiao Xie 
172276dda93cSYan, Zheng 		if (btrfs_header_backref_rev(root->node) <
172376dda93cSYan, Zheng 		    BTRFS_MIXED_BACKREF_REV)
17242c536799SJeff Mahoney 			ret = btrfs_drop_snapshot(root, NULL, 0, 0);
172576dda93cSYan, Zheng 		else
17262c536799SJeff Mahoney 			ret =btrfs_drop_snapshot(root, NULL, 1, 0);
17272c536799SJeff Mahoney 		BUG_ON(ret < 0);
1728e9d0b13bSChris Mason 	}
1729e9d0b13bSChris Mason 	return 0;
1730e9d0b13bSChris Mason }
1731