xref: /openbmc/linux/fs/btrfs/transaction.c (revision bed92eae)
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>
2579154b1bSChris Mason #include "ctree.h"
2679154b1bSChris Mason #include "disk-io.h"
2779154b1bSChris Mason #include "transaction.h"
28925baeddSChris Mason #include "locking.h"
29e02119d5SChris Mason #include "tree-log.h"
30581bb050SLi Zefan #include "inode-map.h"
31733f4fbbSStefan Behrens #include "volumes.h"
3279154b1bSChris Mason 
330f7d52f4SChris Mason #define BTRFS_ROOT_TRANS_TAG 0
340f7d52f4SChris Mason 
3549b25e05SJeff Mahoney void put_transaction(struct btrfs_transaction *transaction)
3679154b1bSChris Mason {
3713c5a93eSJosef Bacik 	WARN_ON(atomic_read(&transaction->use_count) == 0);
3813c5a93eSJosef Bacik 	if (atomic_dec_and_test(&transaction->use_count)) {
39a4abeea4SJosef Bacik 		BUG_ON(!list_empty(&transaction->list));
4000f04b88SArne Jansen 		WARN_ON(transaction->delayed_refs.root.rb_node);
412c90e5d6SChris Mason 		memset(transaction, 0, sizeof(*transaction));
422c90e5d6SChris Mason 		kmem_cache_free(btrfs_transaction_cachep, transaction);
4379154b1bSChris Mason 	}
4478fae27eSChris Mason }
4579154b1bSChris Mason 
46817d52f8SJosef Bacik static noinline void switch_commit_root(struct btrfs_root *root)
47817d52f8SJosef Bacik {
48817d52f8SJosef Bacik 	free_extent_buffer(root->commit_root);
49817d52f8SJosef Bacik 	root->commit_root = btrfs_root_node(root);
50817d52f8SJosef Bacik }
51817d52f8SJosef Bacik 
52d352ac68SChris Mason /*
53d352ac68SChris Mason  * either allocate a new transaction or hop into the existing one
54d352ac68SChris Mason  */
55a4abeea4SJosef Bacik static noinline int join_transaction(struct btrfs_root *root, int nofail)
5679154b1bSChris Mason {
5779154b1bSChris Mason 	struct btrfs_transaction *cur_trans;
5819ae4e81SJan Schmidt 	struct btrfs_fs_info *fs_info = root->fs_info;
59a4abeea4SJosef Bacik 
6019ae4e81SJan Schmidt 	spin_lock(&fs_info->trans_lock);
61d43317dcSChris Mason loop:
6249b25e05SJeff Mahoney 	/* The file system has been taken offline. No new transactions. */
6319ae4e81SJan Schmidt 	if (fs_info->fs_state & BTRFS_SUPER_FLAG_ERROR) {
6419ae4e81SJan Schmidt 		spin_unlock(&fs_info->trans_lock);
6549b25e05SJeff Mahoney 		return -EROFS;
6649b25e05SJeff Mahoney 	}
6749b25e05SJeff Mahoney 
6819ae4e81SJan Schmidt 	if (fs_info->trans_no_join) {
69a4abeea4SJosef Bacik 		if (!nofail) {
7019ae4e81SJan Schmidt 			spin_unlock(&fs_info->trans_lock);
71a4abeea4SJosef Bacik 			return -EBUSY;
72a4abeea4SJosef Bacik 		}
73a4abeea4SJosef Bacik 	}
74a4abeea4SJosef Bacik 
7519ae4e81SJan Schmidt 	cur_trans = fs_info->running_transaction;
76a4abeea4SJosef Bacik 	if (cur_trans) {
77871383beSDavid Sterba 		if (cur_trans->aborted) {
7819ae4e81SJan Schmidt 			spin_unlock(&fs_info->trans_lock);
7949b25e05SJeff Mahoney 			return cur_trans->aborted;
80871383beSDavid Sterba 		}
81a4abeea4SJosef Bacik 		atomic_inc(&cur_trans->use_count);
82a4abeea4SJosef Bacik 		atomic_inc(&cur_trans->num_writers);
83a4abeea4SJosef Bacik 		cur_trans->num_joined++;
8419ae4e81SJan Schmidt 		spin_unlock(&fs_info->trans_lock);
85a4abeea4SJosef Bacik 		return 0;
86a4abeea4SJosef Bacik 	}
8719ae4e81SJan Schmidt 	spin_unlock(&fs_info->trans_lock);
88a4abeea4SJosef Bacik 
89a4abeea4SJosef Bacik 	cur_trans = kmem_cache_alloc(btrfs_transaction_cachep, GFP_NOFS);
90db5b493aSTsutomu Itoh 	if (!cur_trans)
91db5b493aSTsutomu Itoh 		return -ENOMEM;
92d43317dcSChris Mason 
9319ae4e81SJan Schmidt 	spin_lock(&fs_info->trans_lock);
9419ae4e81SJan Schmidt 	if (fs_info->running_transaction) {
95d43317dcSChris Mason 		/*
96d43317dcSChris Mason 		 * someone started a transaction after we unlocked.  Make sure
97d43317dcSChris Mason 		 * to redo the trans_no_join checks above
98d43317dcSChris Mason 		 */
99a4abeea4SJosef Bacik 		kmem_cache_free(btrfs_transaction_cachep, cur_trans);
10019ae4e81SJan Schmidt 		cur_trans = fs_info->running_transaction;
101d43317dcSChris Mason 		goto loop;
1027b8b92afSJosef Bacik 	} else if (root->fs_info->fs_state & BTRFS_SUPER_FLAG_ERROR) {
1037b8b92afSJosef Bacik 		spin_unlock(&root->fs_info->trans_lock);
1047b8b92afSJosef Bacik 		kmem_cache_free(btrfs_transaction_cachep, cur_trans);
1057b8b92afSJosef Bacik 		return -EROFS;
106a4abeea4SJosef Bacik 	}
107d43317dcSChris Mason 
10813c5a93eSJosef Bacik 	atomic_set(&cur_trans->num_writers, 1);
10915ee9bc7SJosef Bacik 	cur_trans->num_joined = 0;
11079154b1bSChris Mason 	init_waitqueue_head(&cur_trans->writer_wait);
11179154b1bSChris Mason 	init_waitqueue_head(&cur_trans->commit_wait);
11279154b1bSChris Mason 	cur_trans->in_commit = 0;
113f9295749SChris Mason 	cur_trans->blocked = 0;
114a4abeea4SJosef Bacik 	/*
115a4abeea4SJosef Bacik 	 * One for this trans handle, one so it will live on until we
116a4abeea4SJosef Bacik 	 * commit the transaction.
117a4abeea4SJosef Bacik 	 */
118a4abeea4SJosef Bacik 	atomic_set(&cur_trans->use_count, 2);
11979154b1bSChris Mason 	cur_trans->commit_done = 0;
12008607c1bSChris Mason 	cur_trans->start_time = get_seconds();
12156bec294SChris Mason 
1226bef4d31SEric Paris 	cur_trans->delayed_refs.root = RB_ROOT;
12356bec294SChris Mason 	cur_trans->delayed_refs.num_entries = 0;
124c3e69d58SChris Mason 	cur_trans->delayed_refs.num_heads_ready = 0;
125c3e69d58SChris Mason 	cur_trans->delayed_refs.num_heads = 0;
12656bec294SChris Mason 	cur_trans->delayed_refs.flushing = 0;
127c3e69d58SChris Mason 	cur_trans->delayed_refs.run_delayed_start = 0;
12820b297d6SJan Schmidt 
12920b297d6SJan Schmidt 	/*
13020b297d6SJan Schmidt 	 * although the tree mod log is per file system and not per transaction,
13120b297d6SJan Schmidt 	 * the log must never go across transaction boundaries.
13220b297d6SJan Schmidt 	 */
13320b297d6SJan Schmidt 	smp_mb();
13420b297d6SJan Schmidt 	if (!list_empty(&fs_info->tree_mod_seq_list)) {
13520b297d6SJan Schmidt 		printk(KERN_ERR "btrfs: tree_mod_seq_list not empty when "
13620b297d6SJan Schmidt 			"creating a fresh transaction\n");
13720b297d6SJan Schmidt 		WARN_ON(1);
13820b297d6SJan Schmidt 	}
13920b297d6SJan Schmidt 	if (!RB_EMPTY_ROOT(&fs_info->tree_mod_log)) {
14020b297d6SJan Schmidt 		printk(KERN_ERR "btrfs: tree_mod_log rb tree not empty when "
14120b297d6SJan Schmidt 			"creating a fresh transaction\n");
14220b297d6SJan Schmidt 		WARN_ON(1);
14320b297d6SJan Schmidt 	}
14420b297d6SJan Schmidt 	atomic_set(&fs_info->tree_mod_seq, 0);
14520b297d6SJan Schmidt 
146a4abeea4SJosef Bacik 	spin_lock_init(&cur_trans->commit_lock);
14756bec294SChris Mason 	spin_lock_init(&cur_trans->delayed_refs.lock);
14856bec294SChris Mason 
1493063d29fSChris Mason 	INIT_LIST_HEAD(&cur_trans->pending_snapshots);
15019ae4e81SJan Schmidt 	list_add_tail(&cur_trans->list, &fs_info->trans_list);
151d1310b2eSChris Mason 	extent_io_tree_init(&cur_trans->dirty_pages,
15219ae4e81SJan Schmidt 			     fs_info->btree_inode->i_mapping);
15319ae4e81SJan Schmidt 	fs_info->generation++;
15419ae4e81SJan Schmidt 	cur_trans->transid = fs_info->generation;
15519ae4e81SJan Schmidt 	fs_info->running_transaction = cur_trans;
15649b25e05SJeff Mahoney 	cur_trans->aborted = 0;
15719ae4e81SJan Schmidt 	spin_unlock(&fs_info->trans_lock);
15815ee9bc7SJosef Bacik 
15979154b1bSChris Mason 	return 0;
16079154b1bSChris Mason }
16179154b1bSChris Mason 
162d352ac68SChris Mason /*
163d397712bSChris Mason  * this does all the record keeping required to make sure that a reference
164d397712bSChris Mason  * counted root is properly recorded in a given transaction.  This is required
165d397712bSChris Mason  * to make sure the old root from before we joined the transaction is deleted
166d397712bSChris Mason  * when the transaction commits
167d352ac68SChris Mason  */
1687585717fSChris Mason static int record_root_in_trans(struct btrfs_trans_handle *trans,
1695d4f98a2SYan Zheng 			       struct btrfs_root *root)
1706702ed49SChris Mason {
1715d4f98a2SYan Zheng 	if (root->ref_cows && root->last_trans < trans->transid) {
1726702ed49SChris Mason 		WARN_ON(root == root->fs_info->extent_root);
1735d4f98a2SYan Zheng 		WARN_ON(root->commit_root != root->node);
1745d4f98a2SYan Zheng 
1757585717fSChris Mason 		/*
1767585717fSChris Mason 		 * see below for in_trans_setup usage rules
1777585717fSChris Mason 		 * we have the reloc mutex held now, so there
1787585717fSChris Mason 		 * is only one writer in this function
1797585717fSChris Mason 		 */
1807585717fSChris Mason 		root->in_trans_setup = 1;
1817585717fSChris Mason 
1827585717fSChris Mason 		/* make sure readers find in_trans_setup before
1837585717fSChris Mason 		 * they find our root->last_trans update
1847585717fSChris Mason 		 */
1857585717fSChris Mason 		smp_wmb();
1867585717fSChris Mason 
187a4abeea4SJosef Bacik 		spin_lock(&root->fs_info->fs_roots_radix_lock);
188a4abeea4SJosef Bacik 		if (root->last_trans == trans->transid) {
189a4abeea4SJosef Bacik 			spin_unlock(&root->fs_info->fs_roots_radix_lock);
190a4abeea4SJosef Bacik 			return 0;
191a4abeea4SJosef Bacik 		}
1926702ed49SChris Mason 		radix_tree_tag_set(&root->fs_info->fs_roots_radix,
1936702ed49SChris Mason 			   (unsigned long)root->root_key.objectid,
1946702ed49SChris Mason 			   BTRFS_ROOT_TRANS_TAG);
195a4abeea4SJosef Bacik 		spin_unlock(&root->fs_info->fs_roots_radix_lock);
1967585717fSChris Mason 		root->last_trans = trans->transid;
1977585717fSChris Mason 
1987585717fSChris Mason 		/* this is pretty tricky.  We don't want to
1997585717fSChris Mason 		 * take the relocation lock in btrfs_record_root_in_trans
2007585717fSChris Mason 		 * unless we're really doing the first setup for this root in
2017585717fSChris Mason 		 * this transaction.
2027585717fSChris Mason 		 *
2037585717fSChris Mason 		 * Normally we'd use root->last_trans as a flag to decide
2047585717fSChris Mason 		 * if we want to take the expensive mutex.
2057585717fSChris Mason 		 *
2067585717fSChris Mason 		 * But, we have to set root->last_trans before we
2077585717fSChris Mason 		 * init the relocation root, otherwise, we trip over warnings
2087585717fSChris Mason 		 * in ctree.c.  The solution used here is to flag ourselves
2097585717fSChris Mason 		 * with root->in_trans_setup.  When this is 1, we're still
2107585717fSChris Mason 		 * fixing up the reloc trees and everyone must wait.
2117585717fSChris Mason 		 *
2127585717fSChris Mason 		 * When this is zero, they can trust root->last_trans and fly
2137585717fSChris Mason 		 * through btrfs_record_root_in_trans without having to take the
2147585717fSChris Mason 		 * lock.  smp_wmb() makes sure that all the writes above are
2157585717fSChris Mason 		 * done before we pop in the zero below
2167585717fSChris Mason 		 */
2175d4f98a2SYan Zheng 		btrfs_init_reloc_root(trans, root);
2187585717fSChris Mason 		smp_wmb();
2197585717fSChris Mason 		root->in_trans_setup = 0;
2206702ed49SChris Mason 	}
2215d4f98a2SYan Zheng 	return 0;
2226702ed49SChris Mason }
2235d4f98a2SYan Zheng 
2247585717fSChris Mason 
2257585717fSChris Mason int btrfs_record_root_in_trans(struct btrfs_trans_handle *trans,
2267585717fSChris Mason 			       struct btrfs_root *root)
2277585717fSChris Mason {
2287585717fSChris Mason 	if (!root->ref_cows)
2297585717fSChris Mason 		return 0;
2307585717fSChris Mason 
2317585717fSChris Mason 	/*
2327585717fSChris Mason 	 * see record_root_in_trans for comments about in_trans_setup usage
2337585717fSChris Mason 	 * and barriers
2347585717fSChris Mason 	 */
2357585717fSChris Mason 	smp_rmb();
2367585717fSChris Mason 	if (root->last_trans == trans->transid &&
2377585717fSChris Mason 	    !root->in_trans_setup)
2387585717fSChris Mason 		return 0;
2397585717fSChris Mason 
2407585717fSChris Mason 	mutex_lock(&root->fs_info->reloc_mutex);
2417585717fSChris Mason 	record_root_in_trans(trans, root);
2427585717fSChris Mason 	mutex_unlock(&root->fs_info->reloc_mutex);
2437585717fSChris Mason 
2447585717fSChris Mason 	return 0;
2457585717fSChris Mason }
2467585717fSChris Mason 
247d352ac68SChris Mason /* wait for commit against the current transaction to become unblocked
248d352ac68SChris Mason  * when this is done, it is safe to start a new transaction, but the current
249d352ac68SChris Mason  * transaction might not be fully on disk.
250d352ac68SChris Mason  */
25137d1aeeeSChris Mason static void wait_current_trans(struct btrfs_root *root)
25279154b1bSChris Mason {
253f9295749SChris Mason 	struct btrfs_transaction *cur_trans;
25479154b1bSChris Mason 
255a4abeea4SJosef Bacik 	spin_lock(&root->fs_info->trans_lock);
256f9295749SChris Mason 	cur_trans = root->fs_info->running_transaction;
25737d1aeeeSChris Mason 	if (cur_trans && cur_trans->blocked) {
25813c5a93eSJosef Bacik 		atomic_inc(&cur_trans->use_count);
259a4abeea4SJosef Bacik 		spin_unlock(&root->fs_info->trans_lock);
26072d63ed6SLi Zefan 
26172d63ed6SLi Zefan 		wait_event(root->fs_info->transaction_wait,
26272d63ed6SLi Zefan 			   !cur_trans->blocked);
263f9295749SChris Mason 		put_transaction(cur_trans);
264a4abeea4SJosef Bacik 	} else {
265a4abeea4SJosef Bacik 		spin_unlock(&root->fs_info->trans_lock);
266f9295749SChris Mason 	}
26737d1aeeeSChris Mason }
26837d1aeeeSChris Mason 
269249ac1e5SJosef Bacik enum btrfs_trans_type {
270249ac1e5SJosef Bacik 	TRANS_START,
271249ac1e5SJosef Bacik 	TRANS_JOIN,
272249ac1e5SJosef Bacik 	TRANS_USERSPACE,
2730af3d00bSJosef Bacik 	TRANS_JOIN_NOLOCK,
274249ac1e5SJosef Bacik };
275249ac1e5SJosef Bacik 
276a22285a6SYan, Zheng static int may_wait_transaction(struct btrfs_root *root, int type)
27737d1aeeeSChris Mason {
278a4abeea4SJosef Bacik 	if (root->fs_info->log_root_recovering)
279a4abeea4SJosef Bacik 		return 0;
280a4abeea4SJosef Bacik 
281a4abeea4SJosef Bacik 	if (type == TRANS_USERSPACE)
282a22285a6SYan, Zheng 		return 1;
283a4abeea4SJosef Bacik 
284a4abeea4SJosef Bacik 	if (type == TRANS_START &&
285a4abeea4SJosef Bacik 	    !atomic_read(&root->fs_info->open_ioctl_trans))
286a4abeea4SJosef Bacik 		return 1;
287a4abeea4SJosef Bacik 
288a22285a6SYan, Zheng 	return 0;
289a22285a6SYan, Zheng }
290a22285a6SYan, Zheng 
291a22285a6SYan, Zheng static struct btrfs_trans_handle *start_transaction(struct btrfs_root *root,
292a22285a6SYan, Zheng 						    u64 num_items, int type)
293a22285a6SYan, Zheng {
294a22285a6SYan, Zheng 	struct btrfs_trans_handle *h;
295a22285a6SYan, Zheng 	struct btrfs_transaction *cur_trans;
296b5009945SJosef Bacik 	u64 num_bytes = 0;
297a22285a6SYan, Zheng 	int ret;
298acce952bSliubo 
299acce952bSliubo 	if (root->fs_info->fs_state & BTRFS_SUPER_FLAG_ERROR)
300acce952bSliubo 		return ERR_PTR(-EROFS);
3012a1eb461SJosef Bacik 
3022a1eb461SJosef Bacik 	if (current->journal_info) {
3032a1eb461SJosef Bacik 		WARN_ON(type != TRANS_JOIN && type != TRANS_JOIN_NOLOCK);
3042a1eb461SJosef Bacik 		h = current->journal_info;
3052a1eb461SJosef Bacik 		h->use_count++;
3062a1eb461SJosef Bacik 		h->orig_rsv = h->block_rsv;
3072a1eb461SJosef Bacik 		h->block_rsv = NULL;
3082a1eb461SJosef Bacik 		goto got_it;
3092a1eb461SJosef Bacik 	}
310b5009945SJosef Bacik 
311b5009945SJosef Bacik 	/*
312b5009945SJosef Bacik 	 * Do the reservation before we join the transaction so we can do all
313b5009945SJosef Bacik 	 * the appropriate flushing if need be.
314b5009945SJosef Bacik 	 */
315b5009945SJosef Bacik 	if (num_items > 0 && root != root->fs_info->chunk_root) {
316b5009945SJosef Bacik 		num_bytes = btrfs_calc_trans_metadata_size(root, num_items);
3174a92b1b8SJosef Bacik 		ret = btrfs_block_rsv_add(root,
318b5009945SJosef Bacik 					  &root->fs_info->trans_block_rsv,
319b5009945SJosef Bacik 					  num_bytes);
320b5009945SJosef Bacik 		if (ret)
321b5009945SJosef Bacik 			return ERR_PTR(ret);
322b5009945SJosef Bacik 	}
323a22285a6SYan, Zheng again:
324a22285a6SYan, Zheng 	h = kmem_cache_alloc(btrfs_trans_handle_cachep, GFP_NOFS);
325a22285a6SYan, Zheng 	if (!h)
326a22285a6SYan, Zheng 		return ERR_PTR(-ENOMEM);
327a22285a6SYan, Zheng 
328a22285a6SYan, Zheng 	if (may_wait_transaction(root, type))
32937d1aeeeSChris Mason 		wait_current_trans(root);
330a22285a6SYan, Zheng 
331a4abeea4SJosef Bacik 	do {
332a4abeea4SJosef Bacik 		ret = join_transaction(root, type == TRANS_JOIN_NOLOCK);
333a4abeea4SJosef Bacik 		if (ret == -EBUSY)
334a4abeea4SJosef Bacik 			wait_current_trans(root);
335a4abeea4SJosef Bacik 	} while (ret == -EBUSY);
336a4abeea4SJosef Bacik 
337db5b493aSTsutomu Itoh 	if (ret < 0) {
3386e8df2aeSYoshinori Sano 		kmem_cache_free(btrfs_trans_handle_cachep, h);
339db5b493aSTsutomu Itoh 		return ERR_PTR(ret);
340db5b493aSTsutomu Itoh 	}
3410f7d52f4SChris Mason 
342a22285a6SYan, Zheng 	cur_trans = root->fs_info->running_transaction;
343a22285a6SYan, Zheng 
344a22285a6SYan, Zheng 	h->transid = cur_trans->transid;
345a22285a6SYan, Zheng 	h->transaction = cur_trans;
34679154b1bSChris Mason 	h->blocks_used = 0;
347a22285a6SYan, Zheng 	h->bytes_reserved = 0;
348d13603efSArne Jansen 	h->root = root;
34956bec294SChris Mason 	h->delayed_ref_updates = 0;
3502a1eb461SJosef Bacik 	h->use_count = 1;
351f0486c68SYan, Zheng 	h->block_rsv = NULL;
3522a1eb461SJosef Bacik 	h->orig_rsv = NULL;
35349b25e05SJeff Mahoney 	h->aborted = 0;
354bed92eaeSArne Jansen 	h->delayed_ref_elem.seq = 0;
355bed92eaeSArne Jansen 	INIT_LIST_HEAD(&h->qgroup_ref_list);
356b7ec40d7SChris Mason 
357a22285a6SYan, Zheng 	smp_mb();
358a22285a6SYan, Zheng 	if (cur_trans->blocked && may_wait_transaction(root, type)) {
359a22285a6SYan, Zheng 		btrfs_commit_transaction(h, root);
360a22285a6SYan, Zheng 		goto again;
361a22285a6SYan, Zheng 	}
3629ed74f2dSJosef Bacik 
363b5009945SJosef Bacik 	if (num_bytes) {
3648c2a3ca2SJosef Bacik 		trace_btrfs_space_reservation(root->fs_info, "transaction",
3652bcc0328SLiu Bo 					      h->transid, num_bytes, 1);
366b5009945SJosef Bacik 		h->block_rsv = &root->fs_info->trans_block_rsv;
367b5009945SJosef Bacik 		h->bytes_reserved = num_bytes;
368a22285a6SYan, Zheng 	}
369a22285a6SYan, Zheng 
3702a1eb461SJosef Bacik got_it:
371a4abeea4SJosef Bacik 	btrfs_record_root_in_trans(h, root);
372a22285a6SYan, Zheng 
373a22285a6SYan, Zheng 	if (!current->journal_info && type != TRANS_USERSPACE)
374a22285a6SYan, Zheng 		current->journal_info = h;
37579154b1bSChris Mason 	return h;
37679154b1bSChris Mason }
37779154b1bSChris Mason 
378f9295749SChris Mason struct btrfs_trans_handle *btrfs_start_transaction(struct btrfs_root *root,
379a22285a6SYan, Zheng 						   int num_items)
380f9295749SChris Mason {
381a22285a6SYan, Zheng 	return start_transaction(root, num_items, TRANS_START);
382f9295749SChris Mason }
3837a7eaa40SJosef Bacik struct btrfs_trans_handle *btrfs_join_transaction(struct btrfs_root *root)
384f9295749SChris Mason {
385a22285a6SYan, Zheng 	return start_transaction(root, 0, TRANS_JOIN);
386f9295749SChris Mason }
387f9295749SChris Mason 
3887a7eaa40SJosef Bacik struct btrfs_trans_handle *btrfs_join_transaction_nolock(struct btrfs_root *root)
3890af3d00bSJosef Bacik {
3900af3d00bSJosef Bacik 	return start_transaction(root, 0, TRANS_JOIN_NOLOCK);
3910af3d00bSJosef Bacik }
3920af3d00bSJosef Bacik 
3937a7eaa40SJosef Bacik struct btrfs_trans_handle *btrfs_start_ioctl_transaction(struct btrfs_root *root)
3949ca9ee09SSage Weil {
3957a7eaa40SJosef Bacik 	return start_transaction(root, 0, TRANS_USERSPACE);
3969ca9ee09SSage Weil }
3979ca9ee09SSage Weil 
398d352ac68SChris Mason /* wait for a transaction commit to be fully complete */
399b9c8300cSLi Zefan static noinline void wait_for_commit(struct btrfs_root *root,
40089ce8a63SChris Mason 				    struct btrfs_transaction *commit)
40189ce8a63SChris Mason {
40272d63ed6SLi Zefan 	wait_event(commit->commit_wait, commit->commit_done);
40389ce8a63SChris Mason }
40489ce8a63SChris Mason 
40546204592SSage Weil int btrfs_wait_for_commit(struct btrfs_root *root, u64 transid)
40646204592SSage Weil {
40746204592SSage Weil 	struct btrfs_transaction *cur_trans = NULL, *t;
40846204592SSage Weil 	int ret;
40946204592SSage Weil 
41046204592SSage Weil 	ret = 0;
41146204592SSage Weil 	if (transid) {
41246204592SSage Weil 		if (transid <= root->fs_info->last_trans_committed)
413a4abeea4SJosef Bacik 			goto out;
41446204592SSage Weil 
41546204592SSage Weil 		/* find specified transaction */
416a4abeea4SJosef Bacik 		spin_lock(&root->fs_info->trans_lock);
41746204592SSage Weil 		list_for_each_entry(t, &root->fs_info->trans_list, list) {
41846204592SSage Weil 			if (t->transid == transid) {
41946204592SSage Weil 				cur_trans = t;
420a4abeea4SJosef Bacik 				atomic_inc(&cur_trans->use_count);
42146204592SSage Weil 				break;
42246204592SSage Weil 			}
42346204592SSage Weil 			if (t->transid > transid)
42446204592SSage Weil 				break;
42546204592SSage Weil 		}
426a4abeea4SJosef Bacik 		spin_unlock(&root->fs_info->trans_lock);
42746204592SSage Weil 		ret = -EINVAL;
42846204592SSage Weil 		if (!cur_trans)
429a4abeea4SJosef Bacik 			goto out;  /* bad transid */
43046204592SSage Weil 	} else {
43146204592SSage Weil 		/* find newest transaction that is committing | committed */
432a4abeea4SJosef Bacik 		spin_lock(&root->fs_info->trans_lock);
43346204592SSage Weil 		list_for_each_entry_reverse(t, &root->fs_info->trans_list,
43446204592SSage Weil 					    list) {
43546204592SSage Weil 			if (t->in_commit) {
43646204592SSage Weil 				if (t->commit_done)
4373473f3c0SJosef Bacik 					break;
43846204592SSage Weil 				cur_trans = t;
439a4abeea4SJosef Bacik 				atomic_inc(&cur_trans->use_count);
44046204592SSage Weil 				break;
44146204592SSage Weil 			}
44246204592SSage Weil 		}
443a4abeea4SJosef Bacik 		spin_unlock(&root->fs_info->trans_lock);
44446204592SSage Weil 		if (!cur_trans)
445a4abeea4SJosef Bacik 			goto out;  /* nothing committing|committed */
44646204592SSage Weil 	}
44746204592SSage Weil 
44846204592SSage Weil 	wait_for_commit(root, cur_trans);
44946204592SSage Weil 
45046204592SSage Weil 	put_transaction(cur_trans);
45146204592SSage Weil 	ret = 0;
452a4abeea4SJosef Bacik out:
45346204592SSage Weil 	return ret;
45446204592SSage Weil }
45546204592SSage Weil 
45637d1aeeeSChris Mason void btrfs_throttle(struct btrfs_root *root)
45737d1aeeeSChris Mason {
458a4abeea4SJosef Bacik 	if (!atomic_read(&root->fs_info->open_ioctl_trans))
45937d1aeeeSChris Mason 		wait_current_trans(root);
46037d1aeeeSChris Mason }
46137d1aeeeSChris Mason 
4628929ecfaSYan, Zheng static int should_end_transaction(struct btrfs_trans_handle *trans,
4638929ecfaSYan, Zheng 				  struct btrfs_root *root)
4648929ecfaSYan, Zheng {
4658929ecfaSYan, Zheng 	int ret;
46636ba022aSJosef Bacik 
46736ba022aSJosef Bacik 	ret = btrfs_block_rsv_check(root, &root->fs_info->global_block_rsv, 5);
4688929ecfaSYan, Zheng 	return ret ? 1 : 0;
4698929ecfaSYan, Zheng }
4708929ecfaSYan, Zheng 
4718929ecfaSYan, Zheng int btrfs_should_end_transaction(struct btrfs_trans_handle *trans,
4728929ecfaSYan, Zheng 				 struct btrfs_root *root)
4738929ecfaSYan, Zheng {
4748929ecfaSYan, Zheng 	struct btrfs_transaction *cur_trans = trans->transaction;
4759c8d86dbSJosef Bacik 	struct btrfs_block_rsv *rsv = trans->block_rsv;
4768929ecfaSYan, Zheng 	int updates;
47749b25e05SJeff Mahoney 	int err;
4788929ecfaSYan, Zheng 
479a4abeea4SJosef Bacik 	smp_mb();
4808929ecfaSYan, Zheng 	if (cur_trans->blocked || cur_trans->delayed_refs.flushing)
4818929ecfaSYan, Zheng 		return 1;
4828929ecfaSYan, Zheng 
4839c8d86dbSJosef Bacik 	/*
4849c8d86dbSJosef Bacik 	 * We need to do this in case we're deleting csums so the global block
4859c8d86dbSJosef Bacik 	 * rsv get's used instead of the csum block rsv.
4869c8d86dbSJosef Bacik 	 */
4879c8d86dbSJosef Bacik 	trans->block_rsv = NULL;
4889c8d86dbSJosef Bacik 
4898929ecfaSYan, Zheng 	updates = trans->delayed_ref_updates;
4908929ecfaSYan, Zheng 	trans->delayed_ref_updates = 0;
49149b25e05SJeff Mahoney 	if (updates) {
49249b25e05SJeff Mahoney 		err = btrfs_run_delayed_refs(trans, root, updates);
49349b25e05SJeff Mahoney 		if (err) /* Error code will also eval true */
49449b25e05SJeff Mahoney 			return err;
49549b25e05SJeff Mahoney 	}
4968929ecfaSYan, Zheng 
4979c8d86dbSJosef Bacik 	trans->block_rsv = rsv;
4989c8d86dbSJosef Bacik 
4998929ecfaSYan, Zheng 	return should_end_transaction(trans, root);
5008929ecfaSYan, Zheng }
5018929ecfaSYan, Zheng 
50289ce8a63SChris Mason static int __btrfs_end_transaction(struct btrfs_trans_handle *trans,
5030af3d00bSJosef Bacik 			  struct btrfs_root *root, int throttle, int lock)
50479154b1bSChris Mason {
5058929ecfaSYan, Zheng 	struct btrfs_transaction *cur_trans = trans->transaction;
506ab78c84dSChris Mason 	struct btrfs_fs_info *info = root->fs_info;
507c3e69d58SChris Mason 	int count = 0;
5084edc2ca3SDave Jones 	int err = 0;
509d6e4a428SChris Mason 
5102a1eb461SJosef Bacik 	if (--trans->use_count) {
5112a1eb461SJosef Bacik 		trans->block_rsv = trans->orig_rsv;
5122a1eb461SJosef Bacik 		return 0;
5132a1eb461SJosef Bacik 	}
5142a1eb461SJosef Bacik 
515b24e03dbSJosef Bacik 	btrfs_trans_release_metadata(trans, root);
5164c13d758SJosef Bacik 	trans->block_rsv = NULL;
517d13603efSArne Jansen 	/*
518d13603efSArne Jansen 	 * the same root has to be passed to start_transaction and
519d13603efSArne Jansen 	 * end_transaction. Subvolume quota depends on this.
520d13603efSArne Jansen 	 */
521d13603efSArne Jansen 	WARN_ON(trans->root != root);
522203bf287SChris Mason 	while (count < 2) {
523c3e69d58SChris Mason 		unsigned long cur = trans->delayed_ref_updates;
524c3e69d58SChris Mason 		trans->delayed_ref_updates = 0;
525c3e69d58SChris Mason 		if (cur &&
526c3e69d58SChris Mason 		    trans->transaction->delayed_refs.num_heads_ready > 64) {
527c3e69d58SChris Mason 			trans->delayed_ref_updates = 0;
528c3e69d58SChris Mason 			btrfs_run_delayed_refs(trans, root, cur);
529c3e69d58SChris Mason 		} else {
530c3e69d58SChris Mason 			break;
531c3e69d58SChris Mason 		}
532c3e69d58SChris Mason 		count++;
53356bec294SChris Mason 	}
53456bec294SChris Mason 
535a4abeea4SJosef Bacik 	if (lock && !atomic_read(&root->fs_info->open_ioctl_trans) &&
536a4abeea4SJosef Bacik 	    should_end_transaction(trans, root)) {
5378929ecfaSYan, Zheng 		trans->transaction->blocked = 1;
538a4abeea4SJosef Bacik 		smp_wmb();
539a4abeea4SJosef Bacik 	}
5408929ecfaSYan, Zheng 
5410af3d00bSJosef Bacik 	if (lock && cur_trans->blocked && !cur_trans->in_commit) {
54281317fdeSJosef Bacik 		if (throttle) {
54381317fdeSJosef Bacik 			/*
54481317fdeSJosef Bacik 			 * We may race with somebody else here so end up having
54581317fdeSJosef Bacik 			 * to call end_transaction on ourselves again, so inc
54681317fdeSJosef Bacik 			 * our use_count.
54781317fdeSJosef Bacik 			 */
54881317fdeSJosef Bacik 			trans->use_count++;
5498929ecfaSYan, Zheng 			return btrfs_commit_transaction(trans, root);
55081317fdeSJosef Bacik 		} else {
5518929ecfaSYan, Zheng 			wake_up_process(info->transaction_kthread);
5528929ecfaSYan, Zheng 		}
55381317fdeSJosef Bacik 	}
5548929ecfaSYan, Zheng 
5558929ecfaSYan, Zheng 	WARN_ON(cur_trans != info->running_transaction);
55613c5a93eSJosef Bacik 	WARN_ON(atomic_read(&cur_trans->num_writers) < 1);
55713c5a93eSJosef Bacik 	atomic_dec(&cur_trans->num_writers);
55889ce8a63SChris Mason 
55999d16cbcSSage Weil 	smp_mb();
56079154b1bSChris Mason 	if (waitqueue_active(&cur_trans->writer_wait))
56179154b1bSChris Mason 		wake_up(&cur_trans->writer_wait);
56279154b1bSChris Mason 	put_transaction(cur_trans);
5639ed74f2dSJosef Bacik 
5649ed74f2dSJosef Bacik 	if (current->journal_info == trans)
5659ed74f2dSJosef Bacik 		current->journal_info = NULL;
566ab78c84dSChris Mason 
56724bbcf04SYan, Zheng 	if (throttle)
56824bbcf04SYan, Zheng 		btrfs_run_delayed_iputs(root);
56924bbcf04SYan, Zheng 
57049b25e05SJeff Mahoney 	if (trans->aborted ||
57149b25e05SJeff Mahoney 	    root->fs_info->fs_state & BTRFS_SUPER_FLAG_ERROR) {
5724edc2ca3SDave Jones 		err = -EIO;
57349b25e05SJeff Mahoney 	}
57449b25e05SJeff Mahoney 
5754edc2ca3SDave Jones 	memset(trans, 0, sizeof(*trans));
5764edc2ca3SDave Jones 	kmem_cache_free(btrfs_trans_handle_cachep, trans);
5774edc2ca3SDave Jones 	return err;
57879154b1bSChris Mason }
57979154b1bSChris Mason 
58089ce8a63SChris Mason int btrfs_end_transaction(struct btrfs_trans_handle *trans,
58189ce8a63SChris Mason 			  struct btrfs_root *root)
58289ce8a63SChris Mason {
58316cdcec7SMiao Xie 	int ret;
58416cdcec7SMiao Xie 
58516cdcec7SMiao Xie 	ret = __btrfs_end_transaction(trans, root, 0, 1);
58616cdcec7SMiao Xie 	if (ret)
58716cdcec7SMiao Xie 		return ret;
58816cdcec7SMiao Xie 	return 0;
58989ce8a63SChris Mason }
59089ce8a63SChris Mason 
59189ce8a63SChris Mason int btrfs_end_transaction_throttle(struct btrfs_trans_handle *trans,
59289ce8a63SChris Mason 				   struct btrfs_root *root)
59389ce8a63SChris Mason {
59416cdcec7SMiao Xie 	int ret;
59516cdcec7SMiao Xie 
59616cdcec7SMiao Xie 	ret = __btrfs_end_transaction(trans, root, 1, 1);
59716cdcec7SMiao Xie 	if (ret)
59816cdcec7SMiao Xie 		return ret;
59916cdcec7SMiao Xie 	return 0;
6000af3d00bSJosef Bacik }
6010af3d00bSJosef Bacik 
6020af3d00bSJosef Bacik int btrfs_end_transaction_nolock(struct btrfs_trans_handle *trans,
6030af3d00bSJosef Bacik 				 struct btrfs_root *root)
6040af3d00bSJosef Bacik {
60516cdcec7SMiao Xie 	int ret;
60616cdcec7SMiao Xie 
60716cdcec7SMiao Xie 	ret = __btrfs_end_transaction(trans, root, 0, 0);
60816cdcec7SMiao Xie 	if (ret)
60916cdcec7SMiao Xie 		return ret;
61016cdcec7SMiao Xie 	return 0;
61116cdcec7SMiao Xie }
61216cdcec7SMiao Xie 
61316cdcec7SMiao Xie int btrfs_end_transaction_dmeta(struct btrfs_trans_handle *trans,
61416cdcec7SMiao Xie 				struct btrfs_root *root)
61516cdcec7SMiao Xie {
61616cdcec7SMiao Xie 	return __btrfs_end_transaction(trans, root, 1, 1);
61789ce8a63SChris Mason }
61889ce8a63SChris Mason 
619d352ac68SChris Mason /*
620d352ac68SChris Mason  * when btree blocks are allocated, they have some corresponding bits set for
621d352ac68SChris Mason  * them in one of two extent_io trees.  This is used to make sure all of
622690587d1SChris Mason  * those extents are sent to disk but does not wait on them
623d352ac68SChris Mason  */
624690587d1SChris Mason int btrfs_write_marked_extents(struct btrfs_root *root,
6258cef4e16SYan, Zheng 			       struct extent_io_tree *dirty_pages, int mark)
62679154b1bSChris Mason {
627777e6bd7SChris Mason 	int err = 0;
6287c4452b9SChris Mason 	int werr = 0;
6291728366eSJosef Bacik 	struct address_space *mapping = root->fs_info->btree_inode->i_mapping;
630777e6bd7SChris Mason 	u64 start = 0;
6315f39d397SChris Mason 	u64 end;
6327c4452b9SChris Mason 
6331728366eSJosef Bacik 	while (!find_first_extent_bit(dirty_pages, start, &start, &end,
6341728366eSJosef Bacik 				      mark)) {
6351728366eSJosef Bacik 		convert_extent_bit(dirty_pages, start, end, EXTENT_NEED_WAIT, mark,
6361728366eSJosef Bacik 				   GFP_NOFS);
6371728366eSJosef Bacik 		err = filemap_fdatawrite_range(mapping, start, end);
6387c4452b9SChris Mason 		if (err)
6397c4452b9SChris Mason 			werr = err;
6401728366eSJosef Bacik 		cond_resched();
6411728366eSJosef Bacik 		start = end + 1;
6427c4452b9SChris Mason 	}
643690587d1SChris Mason 	if (err)
644690587d1SChris Mason 		werr = err;
645690587d1SChris Mason 	return werr;
646690587d1SChris Mason }
647690587d1SChris Mason 
648690587d1SChris Mason /*
649690587d1SChris Mason  * when btree blocks are allocated, they have some corresponding bits set for
650690587d1SChris Mason  * them in one of two extent_io trees.  This is used to make sure all of
651690587d1SChris Mason  * those extents are on disk for transaction or log commit.  We wait
652690587d1SChris Mason  * on all the pages and clear them from the dirty pages state tree
653690587d1SChris Mason  */
654690587d1SChris Mason int btrfs_wait_marked_extents(struct btrfs_root *root,
6558cef4e16SYan, Zheng 			      struct extent_io_tree *dirty_pages, int mark)
656690587d1SChris Mason {
657690587d1SChris Mason 	int err = 0;
658690587d1SChris Mason 	int werr = 0;
6591728366eSJosef Bacik 	struct address_space *mapping = root->fs_info->btree_inode->i_mapping;
660690587d1SChris Mason 	u64 start = 0;
661690587d1SChris Mason 	u64 end;
662690587d1SChris Mason 
6631728366eSJosef Bacik 	while (!find_first_extent_bit(dirty_pages, start, &start, &end,
6641728366eSJosef Bacik 				      EXTENT_NEED_WAIT)) {
6651728366eSJosef Bacik 		clear_extent_bits(dirty_pages, start, end, EXTENT_NEED_WAIT, GFP_NOFS);
6661728366eSJosef Bacik 		err = filemap_fdatawait_range(mapping, start, end);
667777e6bd7SChris Mason 		if (err)
668777e6bd7SChris Mason 			werr = err;
669777e6bd7SChris Mason 		cond_resched();
6701728366eSJosef Bacik 		start = end + 1;
671777e6bd7SChris Mason 	}
6727c4452b9SChris Mason 	if (err)
6737c4452b9SChris Mason 		werr = err;
6747c4452b9SChris Mason 	return werr;
67579154b1bSChris Mason }
67679154b1bSChris Mason 
677690587d1SChris Mason /*
678690587d1SChris Mason  * when btree blocks are allocated, they have some corresponding bits set for
679690587d1SChris Mason  * them in one of two extent_io trees.  This is used to make sure all of
680690587d1SChris Mason  * those extents are on disk for transaction or log commit
681690587d1SChris Mason  */
682690587d1SChris Mason int btrfs_write_and_wait_marked_extents(struct btrfs_root *root,
6838cef4e16SYan, Zheng 				struct extent_io_tree *dirty_pages, int mark)
684690587d1SChris Mason {
685690587d1SChris Mason 	int ret;
686690587d1SChris Mason 	int ret2;
687690587d1SChris Mason 
6888cef4e16SYan, Zheng 	ret = btrfs_write_marked_extents(root, dirty_pages, mark);
6898cef4e16SYan, Zheng 	ret2 = btrfs_wait_marked_extents(root, dirty_pages, mark);
690bf0da8c1SChris Mason 
691bf0da8c1SChris Mason 	if (ret)
692bf0da8c1SChris Mason 		return ret;
693bf0da8c1SChris Mason 	if (ret2)
694bf0da8c1SChris Mason 		return ret2;
695bf0da8c1SChris Mason 	return 0;
696690587d1SChris Mason }
697690587d1SChris Mason 
698d0c803c4SChris Mason int btrfs_write_and_wait_transaction(struct btrfs_trans_handle *trans,
699d0c803c4SChris Mason 				     struct btrfs_root *root)
700d0c803c4SChris Mason {
701d0c803c4SChris Mason 	if (!trans || !trans->transaction) {
702d0c803c4SChris Mason 		struct inode *btree_inode;
703d0c803c4SChris Mason 		btree_inode = root->fs_info->btree_inode;
704d0c803c4SChris Mason 		return filemap_write_and_wait(btree_inode->i_mapping);
705d0c803c4SChris Mason 	}
706d0c803c4SChris Mason 	return btrfs_write_and_wait_marked_extents(root,
7078cef4e16SYan, Zheng 					   &trans->transaction->dirty_pages,
7088cef4e16SYan, Zheng 					   EXTENT_DIRTY);
709d0c803c4SChris Mason }
710d0c803c4SChris Mason 
711d352ac68SChris Mason /*
712d352ac68SChris Mason  * this is used to update the root pointer in the tree of tree roots.
713d352ac68SChris Mason  *
714d352ac68SChris Mason  * But, in the case of the extent allocation tree, updating the root
715d352ac68SChris Mason  * pointer may allocate blocks which may change the root of the extent
716d352ac68SChris Mason  * allocation tree.
717d352ac68SChris Mason  *
718d352ac68SChris Mason  * So, this loops and repeats and makes sure the cowonly root didn't
719d352ac68SChris Mason  * change while the root pointer was being updated in the metadata.
720d352ac68SChris Mason  */
7210b86a832SChris Mason static int update_cowonly_root(struct btrfs_trans_handle *trans,
72279154b1bSChris Mason 			       struct btrfs_root *root)
72379154b1bSChris Mason {
72479154b1bSChris Mason 	int ret;
7250b86a832SChris Mason 	u64 old_root_bytenr;
72686b9f2ecSYan, Zheng 	u64 old_root_used;
7270b86a832SChris Mason 	struct btrfs_root *tree_root = root->fs_info->tree_root;
72879154b1bSChris Mason 
72986b9f2ecSYan, Zheng 	old_root_used = btrfs_root_used(&root->root_item);
7300b86a832SChris Mason 	btrfs_write_dirty_block_groups(trans, root);
73156bec294SChris Mason 
73279154b1bSChris Mason 	while (1) {
7330b86a832SChris Mason 		old_root_bytenr = btrfs_root_bytenr(&root->root_item);
73486b9f2ecSYan, Zheng 		if (old_root_bytenr == root->node->start &&
73586b9f2ecSYan, Zheng 		    old_root_used == btrfs_root_used(&root->root_item))
73679154b1bSChris Mason 			break;
73787ef2bb4SChris Mason 
7385d4f98a2SYan Zheng 		btrfs_set_root_node(&root->root_item, root->node);
73979154b1bSChris Mason 		ret = btrfs_update_root(trans, tree_root,
7400b86a832SChris Mason 					&root->root_key,
7410b86a832SChris Mason 					&root->root_item);
74249b25e05SJeff Mahoney 		if (ret)
74349b25e05SJeff Mahoney 			return ret;
74456bec294SChris Mason 
74586b9f2ecSYan, Zheng 		old_root_used = btrfs_root_used(&root->root_item);
7464a8c9a62SYan Zheng 		ret = btrfs_write_dirty_block_groups(trans, root);
74749b25e05SJeff Mahoney 		if (ret)
74849b25e05SJeff Mahoney 			return ret;
7490b86a832SChris Mason 	}
750276e680dSYan Zheng 
751276e680dSYan Zheng 	if (root != root->fs_info->extent_root)
752817d52f8SJosef Bacik 		switch_commit_root(root);
753276e680dSYan Zheng 
7540b86a832SChris Mason 	return 0;
7550b86a832SChris Mason }
7560b86a832SChris Mason 
757d352ac68SChris Mason /*
758d352ac68SChris Mason  * update all the cowonly tree roots on disk
75949b25e05SJeff Mahoney  *
76049b25e05SJeff Mahoney  * The error handling in this function may not be obvious. Any of the
76149b25e05SJeff Mahoney  * failures will cause the file system to go offline. We still need
76249b25e05SJeff Mahoney  * to clean up the delayed refs.
763d352ac68SChris Mason  */
7645d4f98a2SYan Zheng static noinline int commit_cowonly_roots(struct btrfs_trans_handle *trans,
7650b86a832SChris Mason 					 struct btrfs_root *root)
7660b86a832SChris Mason {
7670b86a832SChris Mason 	struct btrfs_fs_info *fs_info = root->fs_info;
7680b86a832SChris Mason 	struct list_head *next;
76984234f3aSYan Zheng 	struct extent_buffer *eb;
77056bec294SChris Mason 	int ret;
77184234f3aSYan Zheng 
77256bec294SChris Mason 	ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
77349b25e05SJeff Mahoney 	if (ret)
77449b25e05SJeff Mahoney 		return ret;
77587ef2bb4SChris Mason 
77684234f3aSYan Zheng 	eb = btrfs_lock_root_node(fs_info->tree_root);
77749b25e05SJeff Mahoney 	ret = btrfs_cow_block(trans, fs_info->tree_root, eb, NULL,
77849b25e05SJeff Mahoney 			      0, &eb);
77984234f3aSYan Zheng 	btrfs_tree_unlock(eb);
78084234f3aSYan Zheng 	free_extent_buffer(eb);
7810b86a832SChris Mason 
78249b25e05SJeff Mahoney 	if (ret)
78349b25e05SJeff Mahoney 		return ret;
78449b25e05SJeff Mahoney 
78556bec294SChris Mason 	ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
78649b25e05SJeff Mahoney 	if (ret)
78749b25e05SJeff Mahoney 		return ret;
78887ef2bb4SChris Mason 
789733f4fbbSStefan Behrens 	ret = btrfs_run_dev_stats(trans, root->fs_info);
790733f4fbbSStefan Behrens 	BUG_ON(ret);
791733f4fbbSStefan Behrens 
7920b86a832SChris Mason 	while (!list_empty(&fs_info->dirty_cowonly_roots)) {
7930b86a832SChris Mason 		next = fs_info->dirty_cowonly_roots.next;
7940b86a832SChris Mason 		list_del_init(next);
7950b86a832SChris Mason 		root = list_entry(next, struct btrfs_root, dirty_list);
79687ef2bb4SChris Mason 
79749b25e05SJeff Mahoney 		ret = update_cowonly_root(trans, root);
79849b25e05SJeff Mahoney 		if (ret)
79949b25e05SJeff Mahoney 			return ret;
80079154b1bSChris Mason 	}
801276e680dSYan Zheng 
802276e680dSYan Zheng 	down_write(&fs_info->extent_commit_sem);
803276e680dSYan Zheng 	switch_commit_root(fs_info->extent_root);
804276e680dSYan Zheng 	up_write(&fs_info->extent_commit_sem);
805276e680dSYan Zheng 
80679154b1bSChris Mason 	return 0;
80779154b1bSChris Mason }
80879154b1bSChris Mason 
809d352ac68SChris Mason /*
810d352ac68SChris Mason  * dead roots are old snapshots that need to be deleted.  This allocates
811d352ac68SChris Mason  * a dirty root struct and adds it into the list of dead roots that need to
812d352ac68SChris Mason  * be deleted
813d352ac68SChris Mason  */
8145d4f98a2SYan Zheng int btrfs_add_dead_root(struct btrfs_root *root)
8155eda7b5eSChris Mason {
816a4abeea4SJosef Bacik 	spin_lock(&root->fs_info->trans_lock);
8175d4f98a2SYan Zheng 	list_add(&root->root_list, &root->fs_info->dead_roots);
818a4abeea4SJosef Bacik 	spin_unlock(&root->fs_info->trans_lock);
8195eda7b5eSChris Mason 	return 0;
8205eda7b5eSChris Mason }
8215eda7b5eSChris Mason 
822d352ac68SChris Mason /*
8235d4f98a2SYan Zheng  * update all the cowonly tree roots on disk
824d352ac68SChris Mason  */
8255d4f98a2SYan Zheng static noinline int commit_fs_roots(struct btrfs_trans_handle *trans,
8265d4f98a2SYan Zheng 				    struct btrfs_root *root)
8270f7d52f4SChris Mason {
8280f7d52f4SChris Mason 	struct btrfs_root *gang[8];
8295d4f98a2SYan Zheng 	struct btrfs_fs_info *fs_info = root->fs_info;
8300f7d52f4SChris Mason 	int i;
8310f7d52f4SChris Mason 	int ret;
83254aa1f4dSChris Mason 	int err = 0;
83354aa1f4dSChris Mason 
834a4abeea4SJosef Bacik 	spin_lock(&fs_info->fs_roots_radix_lock);
8350f7d52f4SChris Mason 	while (1) {
8365d4f98a2SYan Zheng 		ret = radix_tree_gang_lookup_tag(&fs_info->fs_roots_radix,
8375d4f98a2SYan Zheng 						 (void **)gang, 0,
8380f7d52f4SChris Mason 						 ARRAY_SIZE(gang),
8390f7d52f4SChris Mason 						 BTRFS_ROOT_TRANS_TAG);
8400f7d52f4SChris Mason 		if (ret == 0)
8410f7d52f4SChris Mason 			break;
8420f7d52f4SChris Mason 		for (i = 0; i < ret; i++) {
8430f7d52f4SChris Mason 			root = gang[i];
8445d4f98a2SYan Zheng 			radix_tree_tag_clear(&fs_info->fs_roots_radix,
8452619ba1fSChris Mason 					(unsigned long)root->root_key.objectid,
8460f7d52f4SChris Mason 					BTRFS_ROOT_TRANS_TAG);
847a4abeea4SJosef Bacik 			spin_unlock(&fs_info->fs_roots_radix_lock);
84831153d81SYan Zheng 
849e02119d5SChris Mason 			btrfs_free_log(trans, root);
8505d4f98a2SYan Zheng 			btrfs_update_reloc_root(trans, root);
851d68fc57bSYan, Zheng 			btrfs_orphan_commit_root(trans, root);
852e02119d5SChris Mason 
85382d5902dSLi Zefan 			btrfs_save_ino_cache(root, trans);
85482d5902dSLi Zefan 
855f1ebcc74SLiu Bo 			/* see comments in should_cow_block() */
856f1ebcc74SLiu Bo 			root->force_cow = 0;
857f1ebcc74SLiu Bo 			smp_wmb();
858f1ebcc74SLiu Bo 
859978d910dSYan Zheng 			if (root->commit_root != root->node) {
860581bb050SLi Zefan 				mutex_lock(&root->fs_commit_mutex);
861817d52f8SJosef Bacik 				switch_commit_root(root);
862581bb050SLi Zefan 				btrfs_unpin_free_ino(root);
863581bb050SLi Zefan 				mutex_unlock(&root->fs_commit_mutex);
864581bb050SLi Zefan 
865978d910dSYan Zheng 				btrfs_set_root_node(&root->root_item,
866978d910dSYan Zheng 						    root->node);
867978d910dSYan Zheng 			}
86831153d81SYan Zheng 
8695d4f98a2SYan Zheng 			err = btrfs_update_root(trans, fs_info->tree_root,
8700f7d52f4SChris Mason 						&root->root_key,
8710f7d52f4SChris Mason 						&root->root_item);
872a4abeea4SJosef Bacik 			spin_lock(&fs_info->fs_roots_radix_lock);
87354aa1f4dSChris Mason 			if (err)
87454aa1f4dSChris Mason 				break;
8750f7d52f4SChris Mason 		}
8769f3a7427SChris Mason 	}
877a4abeea4SJosef Bacik 	spin_unlock(&fs_info->fs_roots_radix_lock);
87854aa1f4dSChris Mason 	return err;
8790f7d52f4SChris Mason }
8800f7d52f4SChris Mason 
881d352ac68SChris Mason /*
882d352ac68SChris Mason  * defrag a given btree.  If cacheonly == 1, this won't read from the disk,
883d352ac68SChris Mason  * otherwise every leaf in the btree is read and defragged.
884d352ac68SChris Mason  */
885e9d0b13bSChris Mason int btrfs_defrag_root(struct btrfs_root *root, int cacheonly)
886e9d0b13bSChris Mason {
887e9d0b13bSChris Mason 	struct btrfs_fs_info *info = root->fs_info;
888e9d0b13bSChris Mason 	struct btrfs_trans_handle *trans;
8898929ecfaSYan, Zheng 	int ret;
890d3c2fdcfSChris Mason 	unsigned long nr;
891e9d0b13bSChris Mason 
8928929ecfaSYan, Zheng 	if (xchg(&root->defrag_running, 1))
893e9d0b13bSChris Mason 		return 0;
8948929ecfaSYan, Zheng 
8956b80053dSChris Mason 	while (1) {
8968929ecfaSYan, Zheng 		trans = btrfs_start_transaction(root, 0);
8978929ecfaSYan, Zheng 		if (IS_ERR(trans))
8988929ecfaSYan, Zheng 			return PTR_ERR(trans);
8998929ecfaSYan, Zheng 
900e9d0b13bSChris Mason 		ret = btrfs_defrag_leaves(trans, root, cacheonly);
9018929ecfaSYan, Zheng 
902d3c2fdcfSChris Mason 		nr = trans->blocks_used;
903e9d0b13bSChris Mason 		btrfs_end_transaction(trans, root);
904d3c2fdcfSChris Mason 		btrfs_btree_balance_dirty(info->tree_root, nr);
905e9d0b13bSChris Mason 		cond_resched();
906e9d0b13bSChris Mason 
9077841cb28SDavid Sterba 		if (btrfs_fs_closing(root->fs_info) || ret != -EAGAIN)
908e9d0b13bSChris Mason 			break;
909e9d0b13bSChris Mason 	}
910e9d0b13bSChris Mason 	root->defrag_running = 0;
9118929ecfaSYan, Zheng 	return ret;
912e9d0b13bSChris Mason }
913e9d0b13bSChris Mason 
914d352ac68SChris Mason /*
915d352ac68SChris Mason  * new snapshots need to be created at a very specific time in the
916d352ac68SChris Mason  * transaction commit.  This does the actual creation
917d352ac68SChris Mason  */
91880b6794dSChris Mason static noinline int create_pending_snapshot(struct btrfs_trans_handle *trans,
9193063d29fSChris Mason 				   struct btrfs_fs_info *fs_info,
9203063d29fSChris Mason 				   struct btrfs_pending_snapshot *pending)
9213063d29fSChris Mason {
9223063d29fSChris Mason 	struct btrfs_key key;
92380b6794dSChris Mason 	struct btrfs_root_item *new_root_item;
9243063d29fSChris Mason 	struct btrfs_root *tree_root = fs_info->tree_root;
9253063d29fSChris Mason 	struct btrfs_root *root = pending->root;
9266bdb72deSSage Weil 	struct btrfs_root *parent_root;
92798c9942aSLiu Bo 	struct btrfs_block_rsv *rsv;
9286bdb72deSSage Weil 	struct inode *parent_inode;
9296a912213SJosef Bacik 	struct dentry *parent;
930a22285a6SYan, Zheng 	struct dentry *dentry;
9313063d29fSChris Mason 	struct extent_buffer *tmp;
932925baeddSChris Mason 	struct extent_buffer *old;
9333063d29fSChris Mason 	int ret;
934d68fc57bSYan, Zheng 	u64 to_reserve = 0;
9356bdb72deSSage Weil 	u64 index = 0;
936a22285a6SYan, Zheng 	u64 objectid;
937b83cc969SLi Zefan 	u64 root_flags;
9383063d29fSChris Mason 
93998c9942aSLiu Bo 	rsv = trans->block_rsv;
94098c9942aSLiu Bo 
94180b6794dSChris Mason 	new_root_item = kmalloc(sizeof(*new_root_item), GFP_NOFS);
94280b6794dSChris Mason 	if (!new_root_item) {
94349b25e05SJeff Mahoney 		ret = pending->error = -ENOMEM;
94480b6794dSChris Mason 		goto fail;
94580b6794dSChris Mason 	}
946a22285a6SYan, Zheng 
947581bb050SLi Zefan 	ret = btrfs_find_free_objectid(tree_root, &objectid);
948a22285a6SYan, Zheng 	if (ret) {
949a22285a6SYan, Zheng 		pending->error = ret;
9503063d29fSChris Mason 		goto fail;
951a22285a6SYan, Zheng 	}
9523063d29fSChris Mason 
9533fd0a558SYan, Zheng 	btrfs_reloc_pre_snapshot(trans, pending, &to_reserve);
954d68fc57bSYan, Zheng 
955d68fc57bSYan, Zheng 	if (to_reserve > 0) {
95662f30c54SMiao Xie 		ret = btrfs_block_rsv_add_noflush(root, &pending->block_rsv,
9578bb8ab2eSJosef Bacik 						  to_reserve);
958d68fc57bSYan, Zheng 		if (ret) {
959d68fc57bSYan, Zheng 			pending->error = ret;
960d68fc57bSYan, Zheng 			goto fail;
961d68fc57bSYan, Zheng 		}
962d68fc57bSYan, Zheng 	}
963d68fc57bSYan, Zheng 
9643063d29fSChris Mason 	key.objectid = objectid;
965a22285a6SYan, Zheng 	key.offset = (u64)-1;
966a22285a6SYan, Zheng 	key.type = BTRFS_ROOT_ITEM_KEY;
9673063d29fSChris Mason 
968a22285a6SYan, Zheng 	trans->block_rsv = &pending->block_rsv;
9696bdb72deSSage Weil 
970a22285a6SYan, Zheng 	dentry = pending->dentry;
9716a912213SJosef Bacik 	parent = dget_parent(dentry);
9726a912213SJosef Bacik 	parent_inode = parent->d_inode;
973a22285a6SYan, Zheng 	parent_root = BTRFS_I(parent_inode)->root;
9747585717fSChris Mason 	record_root_in_trans(trans, parent_root);
975a22285a6SYan, Zheng 
9766bdb72deSSage Weil 	/*
9776bdb72deSSage Weil 	 * insert the directory item
9786bdb72deSSage Weil 	 */
9796bdb72deSSage Weil 	ret = btrfs_set_inode_index(parent_inode, &index);
98049b25e05SJeff Mahoney 	BUG_ON(ret); /* -ENOMEM */
9816bdb72deSSage Weil 	ret = btrfs_insert_dir_item(trans, parent_root,
982a22285a6SYan, Zheng 				dentry->d_name.name, dentry->d_name.len,
98316cdcec7SMiao Xie 				parent_inode, &key,
984a22285a6SYan, Zheng 				BTRFS_FT_DIR, index);
98579787eaaSJeff Mahoney 	if (ret == -EEXIST) {
986fe66a05aSChris Mason 		pending->error = -EEXIST;
987fe66a05aSChris Mason 		dput(parent);
988fe66a05aSChris Mason 		goto fail;
98979787eaaSJeff Mahoney 	} else if (ret) {
99079787eaaSJeff Mahoney 		goto abort_trans_dput;
99179787eaaSJeff Mahoney 	}
9926bdb72deSSage Weil 
993a22285a6SYan, Zheng 	btrfs_i_size_write(parent_inode, parent_inode->i_size +
994a22285a6SYan, Zheng 					 dentry->d_name.len * 2);
9956bdb72deSSage Weil 	ret = btrfs_update_inode(trans, parent_root, parent_inode);
99649b25e05SJeff Mahoney 	if (ret)
99779787eaaSJeff Mahoney 		goto abort_trans_dput;
9986bdb72deSSage Weil 
999e999376fSChris Mason 	/*
1000e999376fSChris Mason 	 * pull in the delayed directory update
1001e999376fSChris Mason 	 * and the delayed inode item
1002e999376fSChris Mason 	 * otherwise we corrupt the FS during
1003e999376fSChris Mason 	 * snapshot
1004e999376fSChris Mason 	 */
1005e999376fSChris Mason 	ret = btrfs_run_delayed_items(trans, root);
100679787eaaSJeff Mahoney 	if (ret) { /* Transaction aborted */
100779787eaaSJeff Mahoney 		dput(parent);
100849b25e05SJeff Mahoney 		goto fail;
100979787eaaSJeff Mahoney 	}
1010e999376fSChris Mason 
10117585717fSChris Mason 	record_root_in_trans(trans, root);
10126bdb72deSSage Weil 	btrfs_set_root_last_snapshot(&root->root_item, trans->transid);
10136bdb72deSSage Weil 	memcpy(new_root_item, &root->root_item, sizeof(*new_root_item));
101408fe4db1SLi Zefan 	btrfs_check_and_init_root_item(new_root_item);
10156bdb72deSSage Weil 
1016b83cc969SLi Zefan 	root_flags = btrfs_root_flags(new_root_item);
1017b83cc969SLi Zefan 	if (pending->readonly)
1018b83cc969SLi Zefan 		root_flags |= BTRFS_ROOT_SUBVOL_RDONLY;
1019b83cc969SLi Zefan 	else
1020b83cc969SLi Zefan 		root_flags &= ~BTRFS_ROOT_SUBVOL_RDONLY;
1021b83cc969SLi Zefan 	btrfs_set_root_flags(new_root_item, root_flags);
1022b83cc969SLi Zefan 
1023925baeddSChris Mason 	old = btrfs_lock_root_node(root);
102449b25e05SJeff Mahoney 	ret = btrfs_cow_block(trans, root, old, NULL, 0, &old);
102579787eaaSJeff Mahoney 	if (ret) {
102679787eaaSJeff Mahoney 		btrfs_tree_unlock(old);
102779787eaaSJeff Mahoney 		free_extent_buffer(old);
102879787eaaSJeff Mahoney 		goto abort_trans_dput;
102979787eaaSJeff Mahoney 	}
103049b25e05SJeff Mahoney 
10315d4f98a2SYan Zheng 	btrfs_set_lock_blocking(old);
10323063d29fSChris Mason 
103349b25e05SJeff Mahoney 	ret = btrfs_copy_root(trans, root, old, &tmp, objectid);
103479787eaaSJeff Mahoney 	/* clean up in any case */
1035925baeddSChris Mason 	btrfs_tree_unlock(old);
1036925baeddSChris Mason 	free_extent_buffer(old);
103779787eaaSJeff Mahoney 	if (ret)
103879787eaaSJeff Mahoney 		goto abort_trans_dput;
10393063d29fSChris Mason 
1040f1ebcc74SLiu Bo 	/* see comments in should_cow_block() */
1041f1ebcc74SLiu Bo 	root->force_cow = 1;
1042f1ebcc74SLiu Bo 	smp_wmb();
1043f1ebcc74SLiu Bo 
10445d4f98a2SYan Zheng 	btrfs_set_root_node(new_root_item, tmp);
1045a22285a6SYan, Zheng 	/* record when the snapshot was created in key.offset */
1046a22285a6SYan, Zheng 	key.offset = trans->transid;
1047a22285a6SYan, Zheng 	ret = btrfs_insert_root(trans, tree_root, &key, new_root_item);
1048925baeddSChris Mason 	btrfs_tree_unlock(tmp);
10493063d29fSChris Mason 	free_extent_buffer(tmp);
105049b25e05SJeff Mahoney 	if (ret)
105179787eaaSJeff Mahoney 		goto abort_trans_dput;
10520660b5afSChris Mason 
1053a22285a6SYan, Zheng 	/*
1054a22285a6SYan, Zheng 	 * insert root back/forward references
1055a22285a6SYan, Zheng 	 */
1056a22285a6SYan, Zheng 	ret = btrfs_add_root_ref(trans, tree_root, objectid,
1057a22285a6SYan, Zheng 				 parent_root->root_key.objectid,
105833345d01SLi Zefan 				 btrfs_ino(parent_inode), index,
1059a22285a6SYan, Zheng 				 dentry->d_name.name, dentry->d_name.len);
106079787eaaSJeff Mahoney 	dput(parent);
106149b25e05SJeff Mahoney 	if (ret)
106249b25e05SJeff Mahoney 		goto fail;
1063a22285a6SYan, Zheng 
1064a22285a6SYan, Zheng 	key.offset = (u64)-1;
1065a22285a6SYan, Zheng 	pending->snap = btrfs_read_fs_root_no_name(root->fs_info, &key);
106679787eaaSJeff Mahoney 	if (IS_ERR(pending->snap)) {
106779787eaaSJeff Mahoney 		ret = PTR_ERR(pending->snap);
106849b25e05SJeff Mahoney 		goto abort_trans;
106979787eaaSJeff Mahoney 	}
1070d68fc57bSYan, Zheng 
107149b25e05SJeff Mahoney 	ret = btrfs_reloc_post_snapshot(trans, pending);
107249b25e05SJeff Mahoney 	if (ret)
107349b25e05SJeff Mahoney 		goto abort_trans;
107449b25e05SJeff Mahoney 	ret = 0;
10753063d29fSChris Mason fail:
10766bdb72deSSage Weil 	kfree(new_root_item);
107798c9942aSLiu Bo 	trans->block_rsv = rsv;
1078a22285a6SYan, Zheng 	btrfs_block_rsv_release(root, &pending->block_rsv, (u64)-1);
107949b25e05SJeff Mahoney 	return ret;
108049b25e05SJeff Mahoney 
108179787eaaSJeff Mahoney abort_trans_dput:
108279787eaaSJeff Mahoney 	dput(parent);
108349b25e05SJeff Mahoney abort_trans:
108449b25e05SJeff Mahoney 	btrfs_abort_transaction(trans, root, ret);
108549b25e05SJeff Mahoney 	goto fail;
10863063d29fSChris Mason }
10873063d29fSChris Mason 
1088d352ac68SChris Mason /*
1089d352ac68SChris Mason  * create all the snapshots we've scheduled for creation
1090d352ac68SChris Mason  */
109180b6794dSChris Mason static noinline int create_pending_snapshots(struct btrfs_trans_handle *trans,
10923063d29fSChris Mason 					     struct btrfs_fs_info *fs_info)
10933063d29fSChris Mason {
10943063d29fSChris Mason 	struct btrfs_pending_snapshot *pending;
10953063d29fSChris Mason 	struct list_head *head = &trans->transaction->pending_snapshots;
10963de4586cSChris Mason 
1097fe66a05aSChris Mason 	list_for_each_entry(pending, head, list)
1098fe66a05aSChris Mason 		create_pending_snapshot(trans, fs_info, pending);
10993de4586cSChris Mason 	return 0;
11003de4586cSChris Mason }
11013de4586cSChris Mason 
11025d4f98a2SYan Zheng static void update_super_roots(struct btrfs_root *root)
11035d4f98a2SYan Zheng {
11045d4f98a2SYan Zheng 	struct btrfs_root_item *root_item;
11055d4f98a2SYan Zheng 	struct btrfs_super_block *super;
11065d4f98a2SYan Zheng 
11076c41761fSDavid Sterba 	super = root->fs_info->super_copy;
11085d4f98a2SYan Zheng 
11095d4f98a2SYan Zheng 	root_item = &root->fs_info->chunk_root->root_item;
11105d4f98a2SYan Zheng 	super->chunk_root = root_item->bytenr;
11115d4f98a2SYan Zheng 	super->chunk_root_generation = root_item->generation;
11125d4f98a2SYan Zheng 	super->chunk_root_level = root_item->level;
11135d4f98a2SYan Zheng 
11145d4f98a2SYan Zheng 	root_item = &root->fs_info->tree_root->root_item;
11155d4f98a2SYan Zheng 	super->root = root_item->bytenr;
11165d4f98a2SYan Zheng 	super->generation = root_item->generation;
11175d4f98a2SYan Zheng 	super->root_level = root_item->level;
111873bc1876SJosef Bacik 	if (btrfs_test_opt(root, SPACE_CACHE))
11190af3d00bSJosef Bacik 		super->cache_generation = root_item->generation;
11205d4f98a2SYan Zheng }
11215d4f98a2SYan Zheng 
1122f36f3042SChris Mason int btrfs_transaction_in_commit(struct btrfs_fs_info *info)
1123f36f3042SChris Mason {
1124f36f3042SChris Mason 	int ret = 0;
1125a4abeea4SJosef Bacik 	spin_lock(&info->trans_lock);
1126f36f3042SChris Mason 	if (info->running_transaction)
1127f36f3042SChris Mason 		ret = info->running_transaction->in_commit;
1128a4abeea4SJosef Bacik 	spin_unlock(&info->trans_lock);
1129f36f3042SChris Mason 	return ret;
1130f36f3042SChris Mason }
1131f36f3042SChris Mason 
11328929ecfaSYan, Zheng int btrfs_transaction_blocked(struct btrfs_fs_info *info)
11338929ecfaSYan, Zheng {
11348929ecfaSYan, Zheng 	int ret = 0;
1135a4abeea4SJosef Bacik 	spin_lock(&info->trans_lock);
11368929ecfaSYan, Zheng 	if (info->running_transaction)
11378929ecfaSYan, Zheng 		ret = info->running_transaction->blocked;
1138a4abeea4SJosef Bacik 	spin_unlock(&info->trans_lock);
11398929ecfaSYan, Zheng 	return ret;
11408929ecfaSYan, Zheng }
11418929ecfaSYan, Zheng 
1142bb9c12c9SSage Weil /*
1143bb9c12c9SSage Weil  * wait for the current transaction commit to start and block subsequent
1144bb9c12c9SSage Weil  * transaction joins
1145bb9c12c9SSage Weil  */
1146bb9c12c9SSage Weil static void wait_current_trans_commit_start(struct btrfs_root *root,
1147bb9c12c9SSage Weil 					    struct btrfs_transaction *trans)
1148bb9c12c9SSage Weil {
114972d63ed6SLi Zefan 	wait_event(root->fs_info->transaction_blocked_wait, trans->in_commit);
1150bb9c12c9SSage Weil }
1151bb9c12c9SSage Weil 
1152bb9c12c9SSage Weil /*
1153bb9c12c9SSage Weil  * wait for the current transaction to start and then become unblocked.
1154bb9c12c9SSage Weil  * caller holds ref.
1155bb9c12c9SSage Weil  */
1156bb9c12c9SSage Weil static void wait_current_trans_commit_start_and_unblock(struct btrfs_root *root,
1157bb9c12c9SSage Weil 					 struct btrfs_transaction *trans)
1158bb9c12c9SSage Weil {
115972d63ed6SLi Zefan 	wait_event(root->fs_info->transaction_wait,
116072d63ed6SLi Zefan 		   trans->commit_done || (trans->in_commit && !trans->blocked));
1161bb9c12c9SSage Weil }
1162bb9c12c9SSage Weil 
1163bb9c12c9SSage Weil /*
1164bb9c12c9SSage Weil  * commit transactions asynchronously. once btrfs_commit_transaction_async
1165bb9c12c9SSage Weil  * returns, any subsequent transaction will not be allowed to join.
1166bb9c12c9SSage Weil  */
1167bb9c12c9SSage Weil struct btrfs_async_commit {
1168bb9c12c9SSage Weil 	struct btrfs_trans_handle *newtrans;
1169bb9c12c9SSage Weil 	struct btrfs_root *root;
1170bb9c12c9SSage Weil 	struct delayed_work work;
1171bb9c12c9SSage Weil };
1172bb9c12c9SSage Weil 
1173bb9c12c9SSage Weil static void do_async_commit(struct work_struct *work)
1174bb9c12c9SSage Weil {
1175bb9c12c9SSage Weil 	struct btrfs_async_commit *ac =
1176bb9c12c9SSage Weil 		container_of(work, struct btrfs_async_commit, work.work);
1177bb9c12c9SSage Weil 
1178bb9c12c9SSage Weil 	btrfs_commit_transaction(ac->newtrans, ac->root);
1179bb9c12c9SSage Weil 	kfree(ac);
1180bb9c12c9SSage Weil }
1181bb9c12c9SSage Weil 
1182bb9c12c9SSage Weil int btrfs_commit_transaction_async(struct btrfs_trans_handle *trans,
1183bb9c12c9SSage Weil 				   struct btrfs_root *root,
1184bb9c12c9SSage Weil 				   int wait_for_unblock)
1185bb9c12c9SSage Weil {
1186bb9c12c9SSage Weil 	struct btrfs_async_commit *ac;
1187bb9c12c9SSage Weil 	struct btrfs_transaction *cur_trans;
1188bb9c12c9SSage Weil 
1189bb9c12c9SSage Weil 	ac = kmalloc(sizeof(*ac), GFP_NOFS);
1190db5b493aSTsutomu Itoh 	if (!ac)
1191db5b493aSTsutomu Itoh 		return -ENOMEM;
1192bb9c12c9SSage Weil 
1193bb9c12c9SSage Weil 	INIT_DELAYED_WORK(&ac->work, do_async_commit);
1194bb9c12c9SSage Weil 	ac->root = root;
11957a7eaa40SJosef Bacik 	ac->newtrans = btrfs_join_transaction(root);
11963612b495STsutomu Itoh 	if (IS_ERR(ac->newtrans)) {
11973612b495STsutomu Itoh 		int err = PTR_ERR(ac->newtrans);
11983612b495STsutomu Itoh 		kfree(ac);
11993612b495STsutomu Itoh 		return err;
12003612b495STsutomu Itoh 	}
1201bb9c12c9SSage Weil 
1202bb9c12c9SSage Weil 	/* take transaction reference */
1203bb9c12c9SSage Weil 	cur_trans = trans->transaction;
120413c5a93eSJosef Bacik 	atomic_inc(&cur_trans->use_count);
1205bb9c12c9SSage Weil 
1206bb9c12c9SSage Weil 	btrfs_end_transaction(trans, root);
1207bb9c12c9SSage Weil 	schedule_delayed_work(&ac->work, 0);
1208bb9c12c9SSage Weil 
1209bb9c12c9SSage Weil 	/* wait for transaction to start and unblock */
1210bb9c12c9SSage Weil 	if (wait_for_unblock)
1211bb9c12c9SSage Weil 		wait_current_trans_commit_start_and_unblock(root, cur_trans);
1212bb9c12c9SSage Weil 	else
1213bb9c12c9SSage Weil 		wait_current_trans_commit_start(root, cur_trans);
1214bb9c12c9SSage Weil 
121538e88054SSage Weil 	if (current->journal_info == trans)
121638e88054SSage Weil 		current->journal_info = NULL;
121738e88054SSage Weil 
121838e88054SSage Weil 	put_transaction(cur_trans);
1219bb9c12c9SSage Weil 	return 0;
1220bb9c12c9SSage Weil }
1221bb9c12c9SSage Weil 
122249b25e05SJeff Mahoney 
122349b25e05SJeff Mahoney static void cleanup_transaction(struct btrfs_trans_handle *trans,
12247b8b92afSJosef Bacik 				struct btrfs_root *root, int err)
122549b25e05SJeff Mahoney {
122649b25e05SJeff Mahoney 	struct btrfs_transaction *cur_trans = trans->transaction;
122749b25e05SJeff Mahoney 
122849b25e05SJeff Mahoney 	WARN_ON(trans->use_count > 1);
122949b25e05SJeff Mahoney 
12307b8b92afSJosef Bacik 	btrfs_abort_transaction(trans, root, err);
12317b8b92afSJosef Bacik 
123249b25e05SJeff Mahoney 	spin_lock(&root->fs_info->trans_lock);
123349b25e05SJeff Mahoney 	list_del_init(&cur_trans->list);
1234d7096fc3SJosef Bacik 	if (cur_trans == root->fs_info->running_transaction) {
1235d7096fc3SJosef Bacik 		root->fs_info->running_transaction = NULL;
1236d7096fc3SJosef Bacik 		root->fs_info->trans_no_join = 0;
1237d7096fc3SJosef Bacik 	}
123849b25e05SJeff Mahoney 	spin_unlock(&root->fs_info->trans_lock);
123949b25e05SJeff Mahoney 
124049b25e05SJeff Mahoney 	btrfs_cleanup_one_transaction(trans->transaction, root);
124149b25e05SJeff Mahoney 
124249b25e05SJeff Mahoney 	put_transaction(cur_trans);
124349b25e05SJeff Mahoney 	put_transaction(cur_trans);
124449b25e05SJeff Mahoney 
124549b25e05SJeff Mahoney 	trace_btrfs_transaction_commit(root);
124649b25e05SJeff Mahoney 
124749b25e05SJeff Mahoney 	btrfs_scrub_continue(root);
124849b25e05SJeff Mahoney 
124949b25e05SJeff Mahoney 	if (current->journal_info == trans)
125049b25e05SJeff Mahoney 		current->journal_info = NULL;
125149b25e05SJeff Mahoney 
125249b25e05SJeff Mahoney 	kmem_cache_free(btrfs_trans_handle_cachep, trans);
125349b25e05SJeff Mahoney }
125449b25e05SJeff Mahoney 
1255bb9c12c9SSage Weil /*
1256bb9c12c9SSage Weil  * btrfs_transaction state sequence:
1257bb9c12c9SSage Weil  *    in_commit = 0, blocked = 0  (initial)
1258bb9c12c9SSage Weil  *    in_commit = 1, blocked = 1
1259bb9c12c9SSage Weil  *    blocked = 0
1260bb9c12c9SSage Weil  *    commit_done = 1
1261bb9c12c9SSage Weil  */
126279154b1bSChris Mason int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
126379154b1bSChris Mason 			     struct btrfs_root *root)
126479154b1bSChris Mason {
126515ee9bc7SJosef Bacik 	unsigned long joined = 0;
126649b25e05SJeff Mahoney 	struct btrfs_transaction *cur_trans = trans->transaction;
12678fd17795SChris Mason 	struct btrfs_transaction *prev_trans = NULL;
126879154b1bSChris Mason 	DEFINE_WAIT(wait);
126949b25e05SJeff Mahoney 	int ret = -EIO;
127089573b9cSChris Mason 	int should_grow = 0;
127189573b9cSChris Mason 	unsigned long now = get_seconds();
1272dccae999SSage Weil 	int flush_on_commit = btrfs_test_opt(root, FLUSHONCOMMIT);
127379154b1bSChris Mason 
12745a3f23d5SChris Mason 	btrfs_run_ordered_operations(root, 0);
12755a3f23d5SChris Mason 
1276b24e03dbSJosef Bacik 	btrfs_trans_release_metadata(trans, root);
12779c8d86dbSJosef Bacik 	trans->block_rsv = NULL;
12789c8d86dbSJosef Bacik 
127949b25e05SJeff Mahoney 	if (cur_trans->aborted)
128049b25e05SJeff Mahoney 		goto cleanup_transaction;
128149b25e05SJeff Mahoney 
128256bec294SChris Mason 	/* make a pass through all the delayed refs we have so far
128356bec294SChris Mason 	 * any runnings procs may add more while we are here
128456bec294SChris Mason 	 */
128556bec294SChris Mason 	ret = btrfs_run_delayed_refs(trans, root, 0);
128649b25e05SJeff Mahoney 	if (ret)
128749b25e05SJeff Mahoney 		goto cleanup_transaction;
128856bec294SChris Mason 
1289b7ec40d7SChris Mason 	cur_trans = trans->transaction;
129049b25e05SJeff Mahoney 
129156bec294SChris Mason 	/*
129256bec294SChris Mason 	 * set the flushing flag so procs in this transaction have to
129356bec294SChris Mason 	 * start sending their work down.
129456bec294SChris Mason 	 */
1295b7ec40d7SChris Mason 	cur_trans->delayed_refs.flushing = 1;
129656bec294SChris Mason 
1297c3e69d58SChris Mason 	ret = btrfs_run_delayed_refs(trans, root, 0);
129849b25e05SJeff Mahoney 	if (ret)
129949b25e05SJeff Mahoney 		goto cleanup_transaction;
130056bec294SChris Mason 
1301a4abeea4SJosef Bacik 	spin_lock(&cur_trans->commit_lock);
1302b7ec40d7SChris Mason 	if (cur_trans->in_commit) {
1303a4abeea4SJosef Bacik 		spin_unlock(&cur_trans->commit_lock);
130413c5a93eSJosef Bacik 		atomic_inc(&cur_trans->use_count);
130549b25e05SJeff Mahoney 		ret = btrfs_end_transaction(trans, root);
1306ccd467d6SChris Mason 
1307b9c8300cSLi Zefan 		wait_for_commit(root, cur_trans);
130815ee9bc7SJosef Bacik 
130979154b1bSChris Mason 		put_transaction(cur_trans);
131015ee9bc7SJosef Bacik 
131149b25e05SJeff Mahoney 		return ret;
131279154b1bSChris Mason 	}
13134313b399SChris Mason 
13142c90e5d6SChris Mason 	trans->transaction->in_commit = 1;
1315f9295749SChris Mason 	trans->transaction->blocked = 1;
1316a4abeea4SJosef Bacik 	spin_unlock(&cur_trans->commit_lock);
1317bb9c12c9SSage Weil 	wake_up(&root->fs_info->transaction_blocked_wait);
1318bb9c12c9SSage Weil 
1319a4abeea4SJosef Bacik 	spin_lock(&root->fs_info->trans_lock);
1320ccd467d6SChris Mason 	if (cur_trans->list.prev != &root->fs_info->trans_list) {
1321ccd467d6SChris Mason 		prev_trans = list_entry(cur_trans->list.prev,
1322ccd467d6SChris Mason 					struct btrfs_transaction, list);
1323ccd467d6SChris Mason 		if (!prev_trans->commit_done) {
132413c5a93eSJosef Bacik 			atomic_inc(&prev_trans->use_count);
1325a4abeea4SJosef Bacik 			spin_unlock(&root->fs_info->trans_lock);
1326ccd467d6SChris Mason 
1327ccd467d6SChris Mason 			wait_for_commit(root, prev_trans);
1328ccd467d6SChris Mason 
132915ee9bc7SJosef Bacik 			put_transaction(prev_trans);
1330a4abeea4SJosef Bacik 		} else {
1331a4abeea4SJosef Bacik 			spin_unlock(&root->fs_info->trans_lock);
1332ccd467d6SChris Mason 		}
1333a4abeea4SJosef Bacik 	} else {
1334a4abeea4SJosef Bacik 		spin_unlock(&root->fs_info->trans_lock);
1335ccd467d6SChris Mason 	}
133615ee9bc7SJosef Bacik 
133789573b9cSChris Mason 	if (now < cur_trans->start_time || now - cur_trans->start_time < 1)
133889573b9cSChris Mason 		should_grow = 1;
133989573b9cSChris Mason 
134015ee9bc7SJosef Bacik 	do {
13417ea394f1SYan Zheng 		int snap_pending = 0;
1342a4abeea4SJosef Bacik 
134315ee9bc7SJosef Bacik 		joined = cur_trans->num_joined;
13447ea394f1SYan Zheng 		if (!list_empty(&trans->transaction->pending_snapshots))
13457ea394f1SYan Zheng 			snap_pending = 1;
13467ea394f1SYan Zheng 
13472c90e5d6SChris Mason 		WARN_ON(cur_trans != trans->transaction);
134815ee9bc7SJosef Bacik 
13490bdb1db2SSage Weil 		if (flush_on_commit || snap_pending) {
135024bbcf04SYan, Zheng 			btrfs_start_delalloc_inodes(root, 1);
1351143bede5SJeff Mahoney 			btrfs_wait_ordered_extents(root, 0, 1);
13527ea394f1SYan Zheng 		}
13537ea394f1SYan Zheng 
135416cdcec7SMiao Xie 		ret = btrfs_run_delayed_items(trans, root);
135549b25e05SJeff Mahoney 		if (ret)
135649b25e05SJeff Mahoney 			goto cleanup_transaction;
135716cdcec7SMiao Xie 
13585a3f23d5SChris Mason 		/*
13595a3f23d5SChris Mason 		 * rename don't use btrfs_join_transaction, so, once we
13605a3f23d5SChris Mason 		 * set the transaction to blocked above, we aren't going
13615a3f23d5SChris Mason 		 * to get any new ordered operations.  We can safely run
13625a3f23d5SChris Mason 		 * it here and no for sure that nothing new will be added
13635a3f23d5SChris Mason 		 * to the list
13645a3f23d5SChris Mason 		 */
13655a3f23d5SChris Mason 		btrfs_run_ordered_operations(root, 1);
13665a3f23d5SChris Mason 
1367ed3b3d31SChris Mason 		prepare_to_wait(&cur_trans->writer_wait, &wait,
1368ed3b3d31SChris Mason 				TASK_UNINTERRUPTIBLE);
1369ed3b3d31SChris Mason 
137013c5a93eSJosef Bacik 		if (atomic_read(&cur_trans->num_writers) > 1)
137199d16cbcSSage Weil 			schedule_timeout(MAX_SCHEDULE_TIMEOUT);
137299d16cbcSSage Weil 		else if (should_grow)
137399d16cbcSSage Weil 			schedule_timeout(1);
137415ee9bc7SJosef Bacik 
137515ee9bc7SJosef Bacik 		finish_wait(&cur_trans->writer_wait, &wait);
1376ed0ca140SJosef Bacik 	} while (atomic_read(&cur_trans->num_writers) > 1 ||
1377ed0ca140SJosef Bacik 		 (should_grow && cur_trans->num_joined != joined));
1378ed0ca140SJosef Bacik 
1379ed0ca140SJosef Bacik 	/*
1380ed0ca140SJosef Bacik 	 * Ok now we need to make sure to block out any other joins while we
1381ed0ca140SJosef Bacik 	 * commit the transaction.  We could have started a join before setting
1382ed0ca140SJosef Bacik 	 * no_join so make sure to wait for num_writers to == 1 again.
1383ed0ca140SJosef Bacik 	 */
1384a4abeea4SJosef Bacik 	spin_lock(&root->fs_info->trans_lock);
1385a4abeea4SJosef Bacik 	root->fs_info->trans_no_join = 1;
1386a4abeea4SJosef Bacik 	spin_unlock(&root->fs_info->trans_lock);
1387ed0ca140SJosef Bacik 	wait_event(cur_trans->writer_wait,
1388ed0ca140SJosef Bacik 		   atomic_read(&cur_trans->num_writers) == 1);
138915ee9bc7SJosef Bacik 
13907585717fSChris Mason 	/*
13917585717fSChris Mason 	 * the reloc mutex makes sure that we stop
13927585717fSChris Mason 	 * the balancing code from coming in and moving
13937585717fSChris Mason 	 * extents around in the middle of the commit
13947585717fSChris Mason 	 */
13957585717fSChris Mason 	mutex_lock(&root->fs_info->reloc_mutex);
13967585717fSChris Mason 
1397e999376fSChris Mason 	ret = btrfs_run_delayed_items(trans, root);
139849b25e05SJeff Mahoney 	if (ret) {
139949b25e05SJeff Mahoney 		mutex_unlock(&root->fs_info->reloc_mutex);
140049b25e05SJeff Mahoney 		goto cleanup_transaction;
140149b25e05SJeff Mahoney 	}
14023063d29fSChris Mason 
1403e999376fSChris Mason 	ret = create_pending_snapshots(trans, root->fs_info);
140449b25e05SJeff Mahoney 	if (ret) {
140549b25e05SJeff Mahoney 		mutex_unlock(&root->fs_info->reloc_mutex);
140649b25e05SJeff Mahoney 		goto cleanup_transaction;
140749b25e05SJeff Mahoney 	}
140816cdcec7SMiao Xie 
140956bec294SChris Mason 	ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
141049b25e05SJeff Mahoney 	if (ret) {
141149b25e05SJeff Mahoney 		mutex_unlock(&root->fs_info->reloc_mutex);
141249b25e05SJeff Mahoney 		goto cleanup_transaction;
141349b25e05SJeff Mahoney 	}
141456bec294SChris Mason 
1415e999376fSChris Mason 	/*
1416e999376fSChris Mason 	 * make sure none of the code above managed to slip in a
1417e999376fSChris Mason 	 * delayed item
1418e999376fSChris Mason 	 */
1419e999376fSChris Mason 	btrfs_assert_delayed_root_empty(root);
1420e999376fSChris Mason 
14212c90e5d6SChris Mason 	WARN_ON(cur_trans != trans->transaction);
1422dc17ff8fSChris Mason 
1423a2de733cSArne Jansen 	btrfs_scrub_pause(root);
1424e02119d5SChris Mason 	/* btrfs_commit_tree_roots is responsible for getting the
1425e02119d5SChris Mason 	 * various roots consistent with each other.  Every pointer
1426e02119d5SChris Mason 	 * in the tree of tree roots has to point to the most up to date
1427e02119d5SChris Mason 	 * root for every subvolume and other tree.  So, we have to keep
1428e02119d5SChris Mason 	 * the tree logging code from jumping in and changing any
1429e02119d5SChris Mason 	 * of the trees.
1430e02119d5SChris Mason 	 *
1431e02119d5SChris Mason 	 * At this point in the commit, there can't be any tree-log
1432e02119d5SChris Mason 	 * writers, but a little lower down we drop the trans mutex
1433e02119d5SChris Mason 	 * and let new people in.  By holding the tree_log_mutex
1434e02119d5SChris Mason 	 * from now until after the super is written, we avoid races
1435e02119d5SChris Mason 	 * with the tree-log code.
1436e02119d5SChris Mason 	 */
1437e02119d5SChris Mason 	mutex_lock(&root->fs_info->tree_log_mutex);
14381a40e23bSZheng Yan 
14395d4f98a2SYan Zheng 	ret = commit_fs_roots(trans, root);
144049b25e05SJeff Mahoney 	if (ret) {
144149b25e05SJeff Mahoney 		mutex_unlock(&root->fs_info->tree_log_mutex);
1442871383beSDavid Sterba 		mutex_unlock(&root->fs_info->reloc_mutex);
144349b25e05SJeff Mahoney 		goto cleanup_transaction;
144449b25e05SJeff Mahoney 	}
144554aa1f4dSChris Mason 
14465d4f98a2SYan Zheng 	/* commit_fs_roots gets rid of all the tree log roots, it is now
1447e02119d5SChris Mason 	 * safe to free the root of tree log roots
1448e02119d5SChris Mason 	 */
1449e02119d5SChris Mason 	btrfs_free_log_root_tree(trans, root->fs_info);
1450e02119d5SChris Mason 
14515d4f98a2SYan Zheng 	ret = commit_cowonly_roots(trans, root);
145249b25e05SJeff Mahoney 	if (ret) {
145349b25e05SJeff Mahoney 		mutex_unlock(&root->fs_info->tree_log_mutex);
1454871383beSDavid Sterba 		mutex_unlock(&root->fs_info->reloc_mutex);
145549b25e05SJeff Mahoney 		goto cleanup_transaction;
145649b25e05SJeff Mahoney 	}
145754aa1f4dSChris Mason 
145811833d66SYan Zheng 	btrfs_prepare_extent_commit(trans, root);
145911833d66SYan Zheng 
146078fae27eSChris Mason 	cur_trans = root->fs_info->running_transaction;
14615f39d397SChris Mason 
14625d4f98a2SYan Zheng 	btrfs_set_root_node(&root->fs_info->tree_root->root_item,
14635d4f98a2SYan Zheng 			    root->fs_info->tree_root->node);
1464817d52f8SJosef Bacik 	switch_commit_root(root->fs_info->tree_root);
14655d4f98a2SYan Zheng 
14665d4f98a2SYan Zheng 	btrfs_set_root_node(&root->fs_info->chunk_root->root_item,
14675d4f98a2SYan Zheng 			    root->fs_info->chunk_root->node);
1468817d52f8SJosef Bacik 	switch_commit_root(root->fs_info->chunk_root);
14695d4f98a2SYan Zheng 
14705d4f98a2SYan Zheng 	update_super_roots(root);
1471e02119d5SChris Mason 
1472e02119d5SChris Mason 	if (!root->fs_info->log_root_recovering) {
14736c41761fSDavid Sterba 		btrfs_set_super_log_root(root->fs_info->super_copy, 0);
14746c41761fSDavid Sterba 		btrfs_set_super_log_root_level(root->fs_info->super_copy, 0);
1475e02119d5SChris Mason 	}
1476e02119d5SChris Mason 
14776c41761fSDavid Sterba 	memcpy(root->fs_info->super_for_commit, root->fs_info->super_copy,
14786c41761fSDavid Sterba 	       sizeof(*root->fs_info->super_copy));
1479ccd467d6SChris Mason 
1480f9295749SChris Mason 	trans->transaction->blocked = 0;
1481a4abeea4SJosef Bacik 	spin_lock(&root->fs_info->trans_lock);
1482a4abeea4SJosef Bacik 	root->fs_info->running_transaction = NULL;
1483a4abeea4SJosef Bacik 	root->fs_info->trans_no_join = 0;
1484a4abeea4SJosef Bacik 	spin_unlock(&root->fs_info->trans_lock);
14857585717fSChris Mason 	mutex_unlock(&root->fs_info->reloc_mutex);
1486b7ec40d7SChris Mason 
1487f9295749SChris Mason 	wake_up(&root->fs_info->transaction_wait);
1488e6dcd2dcSChris Mason 
148979154b1bSChris Mason 	ret = btrfs_write_and_wait_transaction(trans, root);
149049b25e05SJeff Mahoney 	if (ret) {
149149b25e05SJeff Mahoney 		btrfs_error(root->fs_info, ret,
149249b25e05SJeff Mahoney 			    "Error while writing out transaction.");
149349b25e05SJeff Mahoney 		mutex_unlock(&root->fs_info->tree_log_mutex);
149449b25e05SJeff Mahoney 		goto cleanup_transaction;
149549b25e05SJeff Mahoney 	}
149649b25e05SJeff Mahoney 
149749b25e05SJeff Mahoney 	ret = write_ctree_super(trans, root, 0);
149849b25e05SJeff Mahoney 	if (ret) {
149949b25e05SJeff Mahoney 		mutex_unlock(&root->fs_info->tree_log_mutex);
150049b25e05SJeff Mahoney 		goto cleanup_transaction;
150149b25e05SJeff Mahoney 	}
15024313b399SChris Mason 
1503e02119d5SChris Mason 	/*
1504e02119d5SChris Mason 	 * the super is written, we can safely allow the tree-loggers
1505e02119d5SChris Mason 	 * to go about their business
1506e02119d5SChris Mason 	 */
1507e02119d5SChris Mason 	mutex_unlock(&root->fs_info->tree_log_mutex);
1508e02119d5SChris Mason 
150911833d66SYan Zheng 	btrfs_finish_extent_commit(trans, root);
15104313b399SChris Mason 
15112c90e5d6SChris Mason 	cur_trans->commit_done = 1;
1512b7ec40d7SChris Mason 
151315ee9bc7SJosef Bacik 	root->fs_info->last_trans_committed = cur_trans->transid;
1514817d52f8SJosef Bacik 
15152c90e5d6SChris Mason 	wake_up(&cur_trans->commit_wait);
15163de4586cSChris Mason 
1517a4abeea4SJosef Bacik 	spin_lock(&root->fs_info->trans_lock);
151813c5a93eSJosef Bacik 	list_del_init(&cur_trans->list);
1519a4abeea4SJosef Bacik 	spin_unlock(&root->fs_info->trans_lock);
1520a4abeea4SJosef Bacik 
152179154b1bSChris Mason 	put_transaction(cur_trans);
152278fae27eSChris Mason 	put_transaction(cur_trans);
152358176a96SJosef Bacik 
15241abe9b8aSliubo 	trace_btrfs_transaction_commit(root);
15251abe9b8aSliubo 
1526a2de733cSArne Jansen 	btrfs_scrub_continue(root);
1527a2de733cSArne Jansen 
15289ed74f2dSJosef Bacik 	if (current->journal_info == trans)
15299ed74f2dSJosef Bacik 		current->journal_info = NULL;
15309ed74f2dSJosef Bacik 
15312c90e5d6SChris Mason 	kmem_cache_free(btrfs_trans_handle_cachep, trans);
153224bbcf04SYan, Zheng 
153324bbcf04SYan, Zheng 	if (current != root->fs_info->transaction_kthread)
153424bbcf04SYan, Zheng 		btrfs_run_delayed_iputs(root);
153524bbcf04SYan, Zheng 
153679154b1bSChris Mason 	return ret;
153749b25e05SJeff Mahoney 
153849b25e05SJeff Mahoney cleanup_transaction:
153949b25e05SJeff Mahoney 	btrfs_printk(root->fs_info, "Skipping commit of aborted transaction.\n");
154049b25e05SJeff Mahoney //	WARN_ON(1);
154149b25e05SJeff Mahoney 	if (current->journal_info == trans)
154249b25e05SJeff Mahoney 		current->journal_info = NULL;
15437b8b92afSJosef Bacik 	cleanup_transaction(trans, root, ret);
154449b25e05SJeff Mahoney 
154549b25e05SJeff Mahoney 	return ret;
154679154b1bSChris Mason }
154779154b1bSChris Mason 
1548d352ac68SChris Mason /*
1549d352ac68SChris Mason  * interface function to delete all the snapshots we have scheduled for deletion
1550d352ac68SChris Mason  */
1551e9d0b13bSChris Mason int btrfs_clean_old_snapshots(struct btrfs_root *root)
1552e9d0b13bSChris Mason {
15535d4f98a2SYan Zheng 	LIST_HEAD(list);
15545d4f98a2SYan Zheng 	struct btrfs_fs_info *fs_info = root->fs_info;
1555e9d0b13bSChris Mason 
1556a4abeea4SJosef Bacik 	spin_lock(&fs_info->trans_lock);
15575d4f98a2SYan Zheng 	list_splice_init(&fs_info->dead_roots, &list);
1558a4abeea4SJosef Bacik 	spin_unlock(&fs_info->trans_lock);
15595d4f98a2SYan Zheng 
15605d4f98a2SYan Zheng 	while (!list_empty(&list)) {
15612c536799SJeff Mahoney 		int ret;
15622c536799SJeff Mahoney 
15635d4f98a2SYan Zheng 		root = list_entry(list.next, struct btrfs_root, root_list);
156476dda93cSYan, Zheng 		list_del(&root->root_list);
156576dda93cSYan, Zheng 
156616cdcec7SMiao Xie 		btrfs_kill_all_delayed_nodes(root);
156716cdcec7SMiao Xie 
156876dda93cSYan, Zheng 		if (btrfs_header_backref_rev(root->node) <
156976dda93cSYan, Zheng 		    BTRFS_MIXED_BACKREF_REV)
15702c536799SJeff Mahoney 			ret = btrfs_drop_snapshot(root, NULL, 0, 0);
157176dda93cSYan, Zheng 		else
15722c536799SJeff Mahoney 			ret =btrfs_drop_snapshot(root, NULL, 1, 0);
15732c536799SJeff Mahoney 		BUG_ON(ret < 0);
1574e9d0b13bSChris Mason 	}
1575e9d0b13bSChris Mason 	return 0;
1576e9d0b13bSChris Mason }
1577