xref: /openbmc/linux/fs/btrfs/transaction.c (revision bb9c12c9)
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"
3079154b1bSChris Mason 
310f7d52f4SChris Mason #define BTRFS_ROOT_TRANS_TAG 0
320f7d52f4SChris Mason 
3380b6794dSChris Mason static noinline void put_transaction(struct btrfs_transaction *transaction)
3479154b1bSChris Mason {
352c90e5d6SChris Mason 	WARN_ON(transaction->use_count == 0);
3679154b1bSChris Mason 	transaction->use_count--;
3778fae27eSChris Mason 	if (transaction->use_count == 0) {
388fd17795SChris Mason 		list_del_init(&transaction->list);
392c90e5d6SChris Mason 		memset(transaction, 0, sizeof(*transaction));
402c90e5d6SChris Mason 		kmem_cache_free(btrfs_transaction_cachep, transaction);
4179154b1bSChris Mason 	}
4278fae27eSChris Mason }
4379154b1bSChris Mason 
44817d52f8SJosef Bacik static noinline void switch_commit_root(struct btrfs_root *root)
45817d52f8SJosef Bacik {
46817d52f8SJosef Bacik 	free_extent_buffer(root->commit_root);
47817d52f8SJosef Bacik 	root->commit_root = btrfs_root_node(root);
48817d52f8SJosef Bacik }
49817d52f8SJosef Bacik 
50d352ac68SChris Mason /*
51d352ac68SChris Mason  * either allocate a new transaction or hop into the existing one
52d352ac68SChris Mason  */
5380b6794dSChris Mason static noinline int join_transaction(struct btrfs_root *root)
5479154b1bSChris Mason {
5579154b1bSChris Mason 	struct btrfs_transaction *cur_trans;
5679154b1bSChris Mason 	cur_trans = root->fs_info->running_transaction;
5779154b1bSChris Mason 	if (!cur_trans) {
582c90e5d6SChris Mason 		cur_trans = kmem_cache_alloc(btrfs_transaction_cachep,
592c90e5d6SChris Mason 					     GFP_NOFS);
6079154b1bSChris Mason 		BUG_ON(!cur_trans);
610f7d52f4SChris Mason 		root->fs_info->generation++;
6215ee9bc7SJosef Bacik 		cur_trans->num_writers = 1;
6315ee9bc7SJosef Bacik 		cur_trans->num_joined = 0;
640f7d52f4SChris Mason 		cur_trans->transid = root->fs_info->generation;
6579154b1bSChris Mason 		init_waitqueue_head(&cur_trans->writer_wait);
6679154b1bSChris Mason 		init_waitqueue_head(&cur_trans->commit_wait);
6779154b1bSChris Mason 		cur_trans->in_commit = 0;
68f9295749SChris Mason 		cur_trans->blocked = 0;
69d5719762SChris Mason 		cur_trans->use_count = 1;
7079154b1bSChris Mason 		cur_trans->commit_done = 0;
7108607c1bSChris Mason 		cur_trans->start_time = get_seconds();
7256bec294SChris Mason 
736bef4d31SEric Paris 		cur_trans->delayed_refs.root = RB_ROOT;
7456bec294SChris Mason 		cur_trans->delayed_refs.num_entries = 0;
75c3e69d58SChris Mason 		cur_trans->delayed_refs.num_heads_ready = 0;
76c3e69d58SChris Mason 		cur_trans->delayed_refs.num_heads = 0;
7756bec294SChris Mason 		cur_trans->delayed_refs.flushing = 0;
78c3e69d58SChris Mason 		cur_trans->delayed_refs.run_delayed_start = 0;
7956bec294SChris Mason 		spin_lock_init(&cur_trans->delayed_refs.lock);
8056bec294SChris Mason 
813063d29fSChris Mason 		INIT_LIST_HEAD(&cur_trans->pending_snapshots);
828fd17795SChris Mason 		list_add_tail(&cur_trans->list, &root->fs_info->trans_list);
83d1310b2eSChris Mason 		extent_io_tree_init(&cur_trans->dirty_pages,
845f39d397SChris Mason 				     root->fs_info->btree_inode->i_mapping,
855f39d397SChris Mason 				     GFP_NOFS);
8648ec2cf8SChris Mason 		spin_lock(&root->fs_info->new_trans_lock);
8748ec2cf8SChris Mason 		root->fs_info->running_transaction = cur_trans;
8848ec2cf8SChris Mason 		spin_unlock(&root->fs_info->new_trans_lock);
8915ee9bc7SJosef Bacik 	} else {
9079154b1bSChris Mason 		cur_trans->num_writers++;
9115ee9bc7SJosef Bacik 		cur_trans->num_joined++;
9215ee9bc7SJosef Bacik 	}
9315ee9bc7SJosef Bacik 
9479154b1bSChris Mason 	return 0;
9579154b1bSChris Mason }
9679154b1bSChris Mason 
97d352ac68SChris Mason /*
98d397712bSChris Mason  * this does all the record keeping required to make sure that a reference
99d397712bSChris Mason  * counted root is properly recorded in a given transaction.  This is required
100d397712bSChris Mason  * to make sure the old root from before we joined the transaction is deleted
101d397712bSChris Mason  * when the transaction commits
102d352ac68SChris Mason  */
1035d4f98a2SYan Zheng static noinline int record_root_in_trans(struct btrfs_trans_handle *trans,
1045d4f98a2SYan Zheng 					 struct btrfs_root *root)
1056702ed49SChris Mason {
1065d4f98a2SYan Zheng 	if (root->ref_cows && root->last_trans < trans->transid) {
1076702ed49SChris Mason 		WARN_ON(root == root->fs_info->extent_root);
1085d4f98a2SYan Zheng 		WARN_ON(root->commit_root != root->node);
1095d4f98a2SYan Zheng 
1106702ed49SChris Mason 		radix_tree_tag_set(&root->fs_info->fs_roots_radix,
1116702ed49SChris Mason 			   (unsigned long)root->root_key.objectid,
1126702ed49SChris Mason 			   BTRFS_ROOT_TRANS_TAG);
1135d4f98a2SYan Zheng 		root->last_trans = trans->transid;
1145d4f98a2SYan Zheng 		btrfs_init_reloc_root(trans, root);
1156702ed49SChris Mason 	}
1165d4f98a2SYan Zheng 	return 0;
1176702ed49SChris Mason }
1185d4f98a2SYan Zheng 
1195d4f98a2SYan Zheng int btrfs_record_root_in_trans(struct btrfs_trans_handle *trans,
1205d4f98a2SYan Zheng 			       struct btrfs_root *root)
1215d4f98a2SYan Zheng {
1225d4f98a2SYan Zheng 	if (!root->ref_cows)
1235d4f98a2SYan Zheng 		return 0;
1245d4f98a2SYan Zheng 
1255d4f98a2SYan Zheng 	mutex_lock(&root->fs_info->trans_mutex);
1265d4f98a2SYan Zheng 	if (root->last_trans == trans->transid) {
1275d4f98a2SYan Zheng 		mutex_unlock(&root->fs_info->trans_mutex);
1285d4f98a2SYan Zheng 		return 0;
1295d4f98a2SYan Zheng 	}
1305d4f98a2SYan Zheng 
1315d4f98a2SYan Zheng 	record_root_in_trans(trans, root);
1325d4f98a2SYan Zheng 	mutex_unlock(&root->fs_info->trans_mutex);
1336702ed49SChris Mason 	return 0;
1346702ed49SChris Mason }
1356702ed49SChris Mason 
136d352ac68SChris Mason /* wait for commit against the current transaction to become unblocked
137d352ac68SChris Mason  * when this is done, it is safe to start a new transaction, but the current
138d352ac68SChris Mason  * transaction might not be fully on disk.
139d352ac68SChris Mason  */
14037d1aeeeSChris Mason static void wait_current_trans(struct btrfs_root *root)
14179154b1bSChris Mason {
142f9295749SChris Mason 	struct btrfs_transaction *cur_trans;
14379154b1bSChris Mason 
144f9295749SChris Mason 	cur_trans = root->fs_info->running_transaction;
14537d1aeeeSChris Mason 	if (cur_trans && cur_trans->blocked) {
146f9295749SChris Mason 		DEFINE_WAIT(wait);
147f9295749SChris Mason 		cur_trans->use_count++;
148f9295749SChris Mason 		while (1) {
149f9295749SChris Mason 			prepare_to_wait(&root->fs_info->transaction_wait, &wait,
150f9295749SChris Mason 					TASK_UNINTERRUPTIBLE);
151471fa17dSZhao Lei 			if (!cur_trans->blocked)
152471fa17dSZhao Lei 				break;
153f9295749SChris Mason 			mutex_unlock(&root->fs_info->trans_mutex);
154f9295749SChris Mason 			schedule();
155f9295749SChris Mason 			mutex_lock(&root->fs_info->trans_mutex);
156f9295749SChris Mason 		}
157471fa17dSZhao Lei 		finish_wait(&root->fs_info->transaction_wait, &wait);
158f9295749SChris Mason 		put_transaction(cur_trans);
159f9295749SChris Mason 	}
16037d1aeeeSChris Mason }
16137d1aeeeSChris Mason 
162249ac1e5SJosef Bacik enum btrfs_trans_type {
163249ac1e5SJosef Bacik 	TRANS_START,
164249ac1e5SJosef Bacik 	TRANS_JOIN,
165249ac1e5SJosef Bacik 	TRANS_USERSPACE,
1660af3d00bSJosef Bacik 	TRANS_JOIN_NOLOCK,
167249ac1e5SJosef Bacik };
168249ac1e5SJosef Bacik 
169a22285a6SYan, Zheng static int may_wait_transaction(struct btrfs_root *root, int type)
17037d1aeeeSChris Mason {
1714bef0848SChris Mason 	if (!root->fs_info->log_root_recovering &&
172249ac1e5SJosef Bacik 	    ((type == TRANS_START && !root->fs_info->open_ioctl_trans) ||
173249ac1e5SJosef Bacik 	     type == TRANS_USERSPACE))
174a22285a6SYan, Zheng 		return 1;
175a22285a6SYan, Zheng 	return 0;
176a22285a6SYan, Zheng }
177a22285a6SYan, Zheng 
178a22285a6SYan, Zheng static struct btrfs_trans_handle *start_transaction(struct btrfs_root *root,
179a22285a6SYan, Zheng 						    u64 num_items, int type)
180a22285a6SYan, Zheng {
181a22285a6SYan, Zheng 	struct btrfs_trans_handle *h;
182a22285a6SYan, Zheng 	struct btrfs_transaction *cur_trans;
183a22285a6SYan, Zheng 	int ret;
184a22285a6SYan, Zheng again:
185a22285a6SYan, Zheng 	h = kmem_cache_alloc(btrfs_trans_handle_cachep, GFP_NOFS);
186a22285a6SYan, Zheng 	if (!h)
187a22285a6SYan, Zheng 		return ERR_PTR(-ENOMEM);
188a22285a6SYan, Zheng 
1890af3d00bSJosef Bacik 	if (type != TRANS_JOIN_NOLOCK)
190a22285a6SYan, Zheng 		mutex_lock(&root->fs_info->trans_mutex);
191a22285a6SYan, Zheng 	if (may_wait_transaction(root, type))
19237d1aeeeSChris Mason 		wait_current_trans(root);
193a22285a6SYan, Zheng 
19479154b1bSChris Mason 	ret = join_transaction(root);
19579154b1bSChris Mason 	BUG_ON(ret);
1960f7d52f4SChris Mason 
197a22285a6SYan, Zheng 	cur_trans = root->fs_info->running_transaction;
198a22285a6SYan, Zheng 	cur_trans->use_count++;
1990af3d00bSJosef Bacik 	if (type != TRANS_JOIN_NOLOCK)
200a22285a6SYan, Zheng 		mutex_unlock(&root->fs_info->trans_mutex);
201a22285a6SYan, Zheng 
202a22285a6SYan, Zheng 	h->transid = cur_trans->transid;
203a22285a6SYan, Zheng 	h->transaction = cur_trans;
20479154b1bSChris Mason 	h->blocks_used = 0;
205d2fb3437SYan Zheng 	h->block_group = 0;
206a22285a6SYan, Zheng 	h->bytes_reserved = 0;
20756bec294SChris Mason 	h->delayed_ref_updates = 0;
208f0486c68SYan, Zheng 	h->block_rsv = NULL;
209b7ec40d7SChris Mason 
210a22285a6SYan, Zheng 	smp_mb();
211a22285a6SYan, Zheng 	if (cur_trans->blocked && may_wait_transaction(root, type)) {
212a22285a6SYan, Zheng 		btrfs_commit_transaction(h, root);
213a22285a6SYan, Zheng 		goto again;
214a22285a6SYan, Zheng 	}
2159ed74f2dSJosef Bacik 
216a22285a6SYan, Zheng 	if (num_items > 0) {
2178bb8ab2eSJosef Bacik 		ret = btrfs_trans_reserve_metadata(h, root, num_items);
218a22285a6SYan, Zheng 		if (ret == -EAGAIN) {
219a22285a6SYan, Zheng 			btrfs_commit_transaction(h, root);
220a22285a6SYan, Zheng 			goto again;
221a22285a6SYan, Zheng 		}
222a22285a6SYan, Zheng 		if (ret < 0) {
223a22285a6SYan, Zheng 			btrfs_end_transaction(h, root);
224a22285a6SYan, Zheng 			return ERR_PTR(ret);
225a22285a6SYan, Zheng 		}
226a22285a6SYan, Zheng 	}
227a22285a6SYan, Zheng 
2280af3d00bSJosef Bacik 	if (type != TRANS_JOIN_NOLOCK)
229a22285a6SYan, Zheng 		mutex_lock(&root->fs_info->trans_mutex);
2305d4f98a2SYan Zheng 	record_root_in_trans(h, root);
2310af3d00bSJosef Bacik 	if (type != TRANS_JOIN_NOLOCK)
23279154b1bSChris Mason 		mutex_unlock(&root->fs_info->trans_mutex);
233a22285a6SYan, Zheng 
234a22285a6SYan, Zheng 	if (!current->journal_info && type != TRANS_USERSPACE)
235a22285a6SYan, Zheng 		current->journal_info = h;
23679154b1bSChris Mason 	return h;
23779154b1bSChris Mason }
23879154b1bSChris Mason 
239f9295749SChris Mason struct btrfs_trans_handle *btrfs_start_transaction(struct btrfs_root *root,
240a22285a6SYan, Zheng 						   int num_items)
241f9295749SChris Mason {
242a22285a6SYan, Zheng 	return start_transaction(root, num_items, TRANS_START);
243f9295749SChris Mason }
244f9295749SChris Mason struct btrfs_trans_handle *btrfs_join_transaction(struct btrfs_root *root,
245f9295749SChris Mason 						   int num_blocks)
246f9295749SChris Mason {
247a22285a6SYan, Zheng 	return start_transaction(root, 0, TRANS_JOIN);
248f9295749SChris Mason }
249f9295749SChris Mason 
2500af3d00bSJosef Bacik struct btrfs_trans_handle *btrfs_join_transaction_nolock(struct btrfs_root *root,
2510af3d00bSJosef Bacik 							  int num_blocks)
2520af3d00bSJosef Bacik {
2530af3d00bSJosef Bacik 	return start_transaction(root, 0, TRANS_JOIN_NOLOCK);
2540af3d00bSJosef Bacik }
2550af3d00bSJosef Bacik 
2569ca9ee09SSage Weil struct btrfs_trans_handle *btrfs_start_ioctl_transaction(struct btrfs_root *r,
2579ca9ee09SSage Weil 							 int num_blocks)
2589ca9ee09SSage Weil {
259a22285a6SYan, Zheng 	return start_transaction(r, 0, TRANS_USERSPACE);
2609ca9ee09SSage Weil }
2619ca9ee09SSage Weil 
262d352ac68SChris Mason /* wait for a transaction commit to be fully complete */
26389ce8a63SChris Mason static noinline int wait_for_commit(struct btrfs_root *root,
26489ce8a63SChris Mason 				    struct btrfs_transaction *commit)
26589ce8a63SChris Mason {
26689ce8a63SChris Mason 	DEFINE_WAIT(wait);
26789ce8a63SChris Mason 	mutex_lock(&root->fs_info->trans_mutex);
26889ce8a63SChris Mason 	while (!commit->commit_done) {
26989ce8a63SChris Mason 		prepare_to_wait(&commit->commit_wait, &wait,
27089ce8a63SChris Mason 				TASK_UNINTERRUPTIBLE);
27189ce8a63SChris Mason 		if (commit->commit_done)
27289ce8a63SChris Mason 			break;
27389ce8a63SChris Mason 		mutex_unlock(&root->fs_info->trans_mutex);
27489ce8a63SChris Mason 		schedule();
27589ce8a63SChris Mason 		mutex_lock(&root->fs_info->trans_mutex);
27689ce8a63SChris Mason 	}
27789ce8a63SChris Mason 	mutex_unlock(&root->fs_info->trans_mutex);
27889ce8a63SChris Mason 	finish_wait(&commit->commit_wait, &wait);
27989ce8a63SChris Mason 	return 0;
28089ce8a63SChris Mason }
28189ce8a63SChris Mason 
2825d4f98a2SYan Zheng #if 0
283d352ac68SChris Mason /*
284d397712bSChris Mason  * rate limit against the drop_snapshot code.  This helps to slow down new
285d397712bSChris Mason  * operations if the drop_snapshot code isn't able to keep up.
286d352ac68SChris Mason  */
28737d1aeeeSChris Mason static void throttle_on_drops(struct btrfs_root *root)
288ab78c84dSChris Mason {
289ab78c84dSChris Mason 	struct btrfs_fs_info *info = root->fs_info;
2902dd3e67bSChris Mason 	int harder_count = 0;
291ab78c84dSChris Mason 
2922dd3e67bSChris Mason harder:
293ab78c84dSChris Mason 	if (atomic_read(&info->throttles)) {
294ab78c84dSChris Mason 		DEFINE_WAIT(wait);
295ab78c84dSChris Mason 		int thr;
296ab78c84dSChris Mason 		thr = atomic_read(&info->throttle_gen);
297ab78c84dSChris Mason 
298ab78c84dSChris Mason 		do {
299ab78c84dSChris Mason 			prepare_to_wait(&info->transaction_throttle,
300ab78c84dSChris Mason 					&wait, TASK_UNINTERRUPTIBLE);
301ab78c84dSChris Mason 			if (!atomic_read(&info->throttles)) {
302ab78c84dSChris Mason 				finish_wait(&info->transaction_throttle, &wait);
303ab78c84dSChris Mason 				break;
304ab78c84dSChris Mason 			}
305ab78c84dSChris Mason 			schedule();
306ab78c84dSChris Mason 			finish_wait(&info->transaction_throttle, &wait);
307ab78c84dSChris Mason 		} while (thr == atomic_read(&info->throttle_gen));
3082dd3e67bSChris Mason 		harder_count++;
3092dd3e67bSChris Mason 
3102dd3e67bSChris Mason 		if (root->fs_info->total_ref_cache_size > 1 * 1024 * 1024 &&
3112dd3e67bSChris Mason 		    harder_count < 2)
3122dd3e67bSChris Mason 			goto harder;
3132dd3e67bSChris Mason 
3142dd3e67bSChris Mason 		if (root->fs_info->total_ref_cache_size > 5 * 1024 * 1024 &&
3152dd3e67bSChris Mason 		    harder_count < 10)
3162dd3e67bSChris Mason 			goto harder;
3172dd3e67bSChris Mason 
3182dd3e67bSChris Mason 		if (root->fs_info->total_ref_cache_size > 10 * 1024 * 1024 &&
3192dd3e67bSChris Mason 		    harder_count < 20)
3202dd3e67bSChris Mason 			goto harder;
321ab78c84dSChris Mason 	}
322ab78c84dSChris Mason }
3235d4f98a2SYan Zheng #endif
324ab78c84dSChris Mason 
32537d1aeeeSChris Mason void btrfs_throttle(struct btrfs_root *root)
32637d1aeeeSChris Mason {
32737d1aeeeSChris Mason 	mutex_lock(&root->fs_info->trans_mutex);
3289ca9ee09SSage Weil 	if (!root->fs_info->open_ioctl_trans)
32937d1aeeeSChris Mason 		wait_current_trans(root);
33037d1aeeeSChris Mason 	mutex_unlock(&root->fs_info->trans_mutex);
33137d1aeeeSChris Mason }
33237d1aeeeSChris Mason 
3338929ecfaSYan, Zheng static int should_end_transaction(struct btrfs_trans_handle *trans,
3348929ecfaSYan, Zheng 				  struct btrfs_root *root)
3358929ecfaSYan, Zheng {
3368929ecfaSYan, Zheng 	int ret;
3378929ecfaSYan, Zheng 	ret = btrfs_block_rsv_check(trans, root,
3388929ecfaSYan, Zheng 				    &root->fs_info->global_block_rsv, 0, 5);
3398929ecfaSYan, Zheng 	return ret ? 1 : 0;
3408929ecfaSYan, Zheng }
3418929ecfaSYan, Zheng 
3428929ecfaSYan, Zheng int btrfs_should_end_transaction(struct btrfs_trans_handle *trans,
3438929ecfaSYan, Zheng 				 struct btrfs_root *root)
3448929ecfaSYan, Zheng {
3458929ecfaSYan, Zheng 	struct btrfs_transaction *cur_trans = trans->transaction;
3468929ecfaSYan, Zheng 	int updates;
3478929ecfaSYan, Zheng 
3488929ecfaSYan, Zheng 	if (cur_trans->blocked || cur_trans->delayed_refs.flushing)
3498929ecfaSYan, Zheng 		return 1;
3508929ecfaSYan, Zheng 
3518929ecfaSYan, Zheng 	updates = trans->delayed_ref_updates;
3528929ecfaSYan, Zheng 	trans->delayed_ref_updates = 0;
3538929ecfaSYan, Zheng 	if (updates)
3548929ecfaSYan, Zheng 		btrfs_run_delayed_refs(trans, root, updates);
3558929ecfaSYan, Zheng 
3568929ecfaSYan, Zheng 	return should_end_transaction(trans, root);
3578929ecfaSYan, Zheng }
3588929ecfaSYan, Zheng 
35989ce8a63SChris Mason static int __btrfs_end_transaction(struct btrfs_trans_handle *trans,
3600af3d00bSJosef Bacik 			  struct btrfs_root *root, int throttle, int lock)
36179154b1bSChris Mason {
3628929ecfaSYan, Zheng 	struct btrfs_transaction *cur_trans = trans->transaction;
363ab78c84dSChris Mason 	struct btrfs_fs_info *info = root->fs_info;
364c3e69d58SChris Mason 	int count = 0;
365d6e4a428SChris Mason 
366c3e69d58SChris Mason 	while (count < 4) {
367c3e69d58SChris Mason 		unsigned long cur = trans->delayed_ref_updates;
368c3e69d58SChris Mason 		trans->delayed_ref_updates = 0;
369c3e69d58SChris Mason 		if (cur &&
370c3e69d58SChris Mason 		    trans->transaction->delayed_refs.num_heads_ready > 64) {
371c3e69d58SChris Mason 			trans->delayed_ref_updates = 0;
372b7ec40d7SChris Mason 
373b7ec40d7SChris Mason 			/*
374b7ec40d7SChris Mason 			 * do a full flush if the transaction is trying
375b7ec40d7SChris Mason 			 * to close
376b7ec40d7SChris Mason 			 */
377b7ec40d7SChris Mason 			if (trans->transaction->delayed_refs.flushing)
378b7ec40d7SChris Mason 				cur = 0;
379c3e69d58SChris Mason 			btrfs_run_delayed_refs(trans, root, cur);
380c3e69d58SChris Mason 		} else {
381c3e69d58SChris Mason 			break;
382c3e69d58SChris Mason 		}
383c3e69d58SChris Mason 		count++;
38456bec294SChris Mason 	}
38556bec294SChris Mason 
386a22285a6SYan, Zheng 	btrfs_trans_release_metadata(trans, root);
387a22285a6SYan, Zheng 
3880af3d00bSJosef Bacik 	if (lock && !root->fs_info->open_ioctl_trans &&
3898929ecfaSYan, Zheng 	    should_end_transaction(trans, root))
3908929ecfaSYan, Zheng 		trans->transaction->blocked = 1;
3918929ecfaSYan, Zheng 
3920af3d00bSJosef Bacik 	if (lock && cur_trans->blocked && !cur_trans->in_commit) {
3938929ecfaSYan, Zheng 		if (throttle)
3948929ecfaSYan, Zheng 			return btrfs_commit_transaction(trans, root);
3958929ecfaSYan, Zheng 		else
3968929ecfaSYan, Zheng 			wake_up_process(info->transaction_kthread);
3978929ecfaSYan, Zheng 	}
3988929ecfaSYan, Zheng 
3990af3d00bSJosef Bacik 	if (lock)
400ab78c84dSChris Mason 		mutex_lock(&info->trans_mutex);
4018929ecfaSYan, Zheng 	WARN_ON(cur_trans != info->running_transaction);
402d5719762SChris Mason 	WARN_ON(cur_trans->num_writers < 1);
403ccd467d6SChris Mason 	cur_trans->num_writers--;
40489ce8a63SChris Mason 
40599d16cbcSSage Weil 	smp_mb();
40679154b1bSChris Mason 	if (waitqueue_active(&cur_trans->writer_wait))
40779154b1bSChris Mason 		wake_up(&cur_trans->writer_wait);
40879154b1bSChris Mason 	put_transaction(cur_trans);
4090af3d00bSJosef Bacik 	if (lock)
410ab78c84dSChris Mason 		mutex_unlock(&info->trans_mutex);
4119ed74f2dSJosef Bacik 
4129ed74f2dSJosef Bacik 	if (current->journal_info == trans)
4139ed74f2dSJosef Bacik 		current->journal_info = NULL;
414d6025579SChris Mason 	memset(trans, 0, sizeof(*trans));
4152c90e5d6SChris Mason 	kmem_cache_free(btrfs_trans_handle_cachep, trans);
416ab78c84dSChris Mason 
41724bbcf04SYan, Zheng 	if (throttle)
41824bbcf04SYan, Zheng 		btrfs_run_delayed_iputs(root);
41924bbcf04SYan, Zheng 
42079154b1bSChris Mason 	return 0;
42179154b1bSChris Mason }
42279154b1bSChris Mason 
42389ce8a63SChris Mason int btrfs_end_transaction(struct btrfs_trans_handle *trans,
42489ce8a63SChris Mason 			  struct btrfs_root *root)
42589ce8a63SChris Mason {
4260af3d00bSJosef Bacik 	return __btrfs_end_transaction(trans, root, 0, 1);
42789ce8a63SChris Mason }
42889ce8a63SChris Mason 
42989ce8a63SChris Mason int btrfs_end_transaction_throttle(struct btrfs_trans_handle *trans,
43089ce8a63SChris Mason 				   struct btrfs_root *root)
43189ce8a63SChris Mason {
4320af3d00bSJosef Bacik 	return __btrfs_end_transaction(trans, root, 1, 1);
4330af3d00bSJosef Bacik }
4340af3d00bSJosef Bacik 
4350af3d00bSJosef Bacik int btrfs_end_transaction_nolock(struct btrfs_trans_handle *trans,
4360af3d00bSJosef Bacik 				 struct btrfs_root *root)
4370af3d00bSJosef Bacik {
4380af3d00bSJosef Bacik 	return __btrfs_end_transaction(trans, root, 0, 0);
43989ce8a63SChris Mason }
44089ce8a63SChris Mason 
441d352ac68SChris Mason /*
442d352ac68SChris Mason  * when btree blocks are allocated, they have some corresponding bits set for
443d352ac68SChris Mason  * them in one of two extent_io trees.  This is used to make sure all of
444690587d1SChris Mason  * those extents are sent to disk but does not wait on them
445d352ac68SChris Mason  */
446690587d1SChris Mason int btrfs_write_marked_extents(struct btrfs_root *root,
4478cef4e16SYan, Zheng 			       struct extent_io_tree *dirty_pages, int mark)
44879154b1bSChris Mason {
4497c4452b9SChris Mason 	int ret;
450777e6bd7SChris Mason 	int err = 0;
4517c4452b9SChris Mason 	int werr = 0;
4527c4452b9SChris Mason 	struct page *page;
4537c4452b9SChris Mason 	struct inode *btree_inode = root->fs_info->btree_inode;
454777e6bd7SChris Mason 	u64 start = 0;
4555f39d397SChris Mason 	u64 end;
4565f39d397SChris Mason 	unsigned long index;
4577c4452b9SChris Mason 
4587c4452b9SChris Mason 	while (1) {
459777e6bd7SChris Mason 		ret = find_first_extent_bit(dirty_pages, start, &start, &end,
4608cef4e16SYan, Zheng 					    mark);
4615f39d397SChris Mason 		if (ret)
4627c4452b9SChris Mason 			break;
4635f39d397SChris Mason 		while (start <= end) {
464777e6bd7SChris Mason 			cond_resched();
465777e6bd7SChris Mason 
4665f39d397SChris Mason 			index = start >> PAGE_CACHE_SHIFT;
46735ebb934SChris Mason 			start = (u64)(index + 1) << PAGE_CACHE_SHIFT;
4684bef0848SChris Mason 			page = find_get_page(btree_inode->i_mapping, index);
4697c4452b9SChris Mason 			if (!page)
4707c4452b9SChris Mason 				continue;
4714bef0848SChris Mason 
4724bef0848SChris Mason 			btree_lock_page_hook(page);
4734bef0848SChris Mason 			if (!page->mapping) {
4744bef0848SChris Mason 				unlock_page(page);
4754bef0848SChris Mason 				page_cache_release(page);
4764bef0848SChris Mason 				continue;
4774bef0848SChris Mason 			}
4784bef0848SChris Mason 
4796702ed49SChris Mason 			if (PageWriteback(page)) {
4806702ed49SChris Mason 				if (PageDirty(page))
4816702ed49SChris Mason 					wait_on_page_writeback(page);
4826702ed49SChris Mason 				else {
4836702ed49SChris Mason 					unlock_page(page);
4846702ed49SChris Mason 					page_cache_release(page);
4856702ed49SChris Mason 					continue;
4866702ed49SChris Mason 				}
4876702ed49SChris Mason 			}
4887c4452b9SChris Mason 			err = write_one_page(page, 0);
4897c4452b9SChris Mason 			if (err)
4907c4452b9SChris Mason 				werr = err;
4917c4452b9SChris Mason 			page_cache_release(page);
4927c4452b9SChris Mason 		}
4937c4452b9SChris Mason 	}
494690587d1SChris Mason 	if (err)
495690587d1SChris Mason 		werr = err;
496690587d1SChris Mason 	return werr;
497690587d1SChris Mason }
498690587d1SChris Mason 
499690587d1SChris Mason /*
500690587d1SChris Mason  * when btree blocks are allocated, they have some corresponding bits set for
501690587d1SChris Mason  * them in one of two extent_io trees.  This is used to make sure all of
502690587d1SChris Mason  * those extents are on disk for transaction or log commit.  We wait
503690587d1SChris Mason  * on all the pages and clear them from the dirty pages state tree
504690587d1SChris Mason  */
505690587d1SChris Mason int btrfs_wait_marked_extents(struct btrfs_root *root,
5068cef4e16SYan, Zheng 			      struct extent_io_tree *dirty_pages, int mark)
507690587d1SChris Mason {
508690587d1SChris Mason 	int ret;
509690587d1SChris Mason 	int err = 0;
510690587d1SChris Mason 	int werr = 0;
511690587d1SChris Mason 	struct page *page;
512690587d1SChris Mason 	struct inode *btree_inode = root->fs_info->btree_inode;
513690587d1SChris Mason 	u64 start = 0;
514690587d1SChris Mason 	u64 end;
515690587d1SChris Mason 	unsigned long index;
516690587d1SChris Mason 
517777e6bd7SChris Mason 	while (1) {
5188cef4e16SYan, Zheng 		ret = find_first_extent_bit(dirty_pages, start, &start, &end,
5198cef4e16SYan, Zheng 					    mark);
520777e6bd7SChris Mason 		if (ret)
521777e6bd7SChris Mason 			break;
522777e6bd7SChris Mason 
5238cef4e16SYan, Zheng 		clear_extent_bits(dirty_pages, start, end, mark, GFP_NOFS);
524777e6bd7SChris Mason 		while (start <= end) {
525777e6bd7SChris Mason 			index = start >> PAGE_CACHE_SHIFT;
526777e6bd7SChris Mason 			start = (u64)(index + 1) << PAGE_CACHE_SHIFT;
527777e6bd7SChris Mason 			page = find_get_page(btree_inode->i_mapping, index);
528777e6bd7SChris Mason 			if (!page)
529777e6bd7SChris Mason 				continue;
530777e6bd7SChris Mason 			if (PageDirty(page)) {
5314bef0848SChris Mason 				btree_lock_page_hook(page);
5324bef0848SChris Mason 				wait_on_page_writeback(page);
533777e6bd7SChris Mason 				err = write_one_page(page, 0);
534777e6bd7SChris Mason 				if (err)
535777e6bd7SChris Mason 					werr = err;
536777e6bd7SChris Mason 			}
537777e6bd7SChris Mason 			wait_on_page_writeback(page);
538777e6bd7SChris Mason 			page_cache_release(page);
539777e6bd7SChris Mason 			cond_resched();
540777e6bd7SChris Mason 		}
541777e6bd7SChris Mason 	}
5427c4452b9SChris Mason 	if (err)
5437c4452b9SChris Mason 		werr = err;
5447c4452b9SChris Mason 	return werr;
54579154b1bSChris Mason }
54679154b1bSChris Mason 
547690587d1SChris Mason /*
548690587d1SChris Mason  * when btree blocks are allocated, they have some corresponding bits set for
549690587d1SChris Mason  * them in one of two extent_io trees.  This is used to make sure all of
550690587d1SChris Mason  * those extents are on disk for transaction or log commit
551690587d1SChris Mason  */
552690587d1SChris Mason int btrfs_write_and_wait_marked_extents(struct btrfs_root *root,
5538cef4e16SYan, Zheng 				struct extent_io_tree *dirty_pages, int mark)
554690587d1SChris Mason {
555690587d1SChris Mason 	int ret;
556690587d1SChris Mason 	int ret2;
557690587d1SChris Mason 
5588cef4e16SYan, Zheng 	ret = btrfs_write_marked_extents(root, dirty_pages, mark);
5598cef4e16SYan, Zheng 	ret2 = btrfs_wait_marked_extents(root, dirty_pages, mark);
560690587d1SChris Mason 	return ret || ret2;
561690587d1SChris Mason }
562690587d1SChris Mason 
563d0c803c4SChris Mason int btrfs_write_and_wait_transaction(struct btrfs_trans_handle *trans,
564d0c803c4SChris Mason 				     struct btrfs_root *root)
565d0c803c4SChris Mason {
566d0c803c4SChris Mason 	if (!trans || !trans->transaction) {
567d0c803c4SChris Mason 		struct inode *btree_inode;
568d0c803c4SChris Mason 		btree_inode = root->fs_info->btree_inode;
569d0c803c4SChris Mason 		return filemap_write_and_wait(btree_inode->i_mapping);
570d0c803c4SChris Mason 	}
571d0c803c4SChris Mason 	return btrfs_write_and_wait_marked_extents(root,
5728cef4e16SYan, Zheng 					   &trans->transaction->dirty_pages,
5738cef4e16SYan, Zheng 					   EXTENT_DIRTY);
574d0c803c4SChris Mason }
575d0c803c4SChris Mason 
576d352ac68SChris Mason /*
577d352ac68SChris Mason  * this is used to update the root pointer in the tree of tree roots.
578d352ac68SChris Mason  *
579d352ac68SChris Mason  * But, in the case of the extent allocation tree, updating the root
580d352ac68SChris Mason  * pointer may allocate blocks which may change the root of the extent
581d352ac68SChris Mason  * allocation tree.
582d352ac68SChris Mason  *
583d352ac68SChris Mason  * So, this loops and repeats and makes sure the cowonly root didn't
584d352ac68SChris Mason  * change while the root pointer was being updated in the metadata.
585d352ac68SChris Mason  */
5860b86a832SChris Mason static int update_cowonly_root(struct btrfs_trans_handle *trans,
58779154b1bSChris Mason 			       struct btrfs_root *root)
58879154b1bSChris Mason {
58979154b1bSChris Mason 	int ret;
5900b86a832SChris Mason 	u64 old_root_bytenr;
59186b9f2ecSYan, Zheng 	u64 old_root_used;
5920b86a832SChris Mason 	struct btrfs_root *tree_root = root->fs_info->tree_root;
59379154b1bSChris Mason 
59486b9f2ecSYan, Zheng 	old_root_used = btrfs_root_used(&root->root_item);
5950b86a832SChris Mason 	btrfs_write_dirty_block_groups(trans, root);
59656bec294SChris Mason 
59779154b1bSChris Mason 	while (1) {
5980b86a832SChris Mason 		old_root_bytenr = btrfs_root_bytenr(&root->root_item);
59986b9f2ecSYan, Zheng 		if (old_root_bytenr == root->node->start &&
60086b9f2ecSYan, Zheng 		    old_root_used == btrfs_root_used(&root->root_item))
60179154b1bSChris Mason 			break;
60287ef2bb4SChris Mason 
6035d4f98a2SYan Zheng 		btrfs_set_root_node(&root->root_item, root->node);
60479154b1bSChris Mason 		ret = btrfs_update_root(trans, tree_root,
6050b86a832SChris Mason 					&root->root_key,
6060b86a832SChris Mason 					&root->root_item);
60779154b1bSChris Mason 		BUG_ON(ret);
60856bec294SChris Mason 
60986b9f2ecSYan, Zheng 		old_root_used = btrfs_root_used(&root->root_item);
6104a8c9a62SYan Zheng 		ret = btrfs_write_dirty_block_groups(trans, root);
61156bec294SChris Mason 		BUG_ON(ret);
6120b86a832SChris Mason 	}
613276e680dSYan Zheng 
614276e680dSYan Zheng 	if (root != root->fs_info->extent_root)
615817d52f8SJosef Bacik 		switch_commit_root(root);
616276e680dSYan Zheng 
6170b86a832SChris Mason 	return 0;
6180b86a832SChris Mason }
6190b86a832SChris Mason 
620d352ac68SChris Mason /*
621d352ac68SChris Mason  * update all the cowonly tree roots on disk
622d352ac68SChris Mason  */
6235d4f98a2SYan Zheng static noinline int commit_cowonly_roots(struct btrfs_trans_handle *trans,
6240b86a832SChris Mason 					 struct btrfs_root *root)
6250b86a832SChris Mason {
6260b86a832SChris Mason 	struct btrfs_fs_info *fs_info = root->fs_info;
6270b86a832SChris Mason 	struct list_head *next;
62884234f3aSYan Zheng 	struct extent_buffer *eb;
62956bec294SChris Mason 	int ret;
63084234f3aSYan Zheng 
63156bec294SChris Mason 	ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
63256bec294SChris Mason 	BUG_ON(ret);
63387ef2bb4SChris Mason 
63484234f3aSYan Zheng 	eb = btrfs_lock_root_node(fs_info->tree_root);
6359fa8cfe7SChris Mason 	btrfs_cow_block(trans, fs_info->tree_root, eb, NULL, 0, &eb);
63684234f3aSYan Zheng 	btrfs_tree_unlock(eb);
63784234f3aSYan Zheng 	free_extent_buffer(eb);
6380b86a832SChris Mason 
63956bec294SChris Mason 	ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
64056bec294SChris Mason 	BUG_ON(ret);
64187ef2bb4SChris Mason 
6420b86a832SChris Mason 	while (!list_empty(&fs_info->dirty_cowonly_roots)) {
6430b86a832SChris Mason 		next = fs_info->dirty_cowonly_roots.next;
6440b86a832SChris Mason 		list_del_init(next);
6450b86a832SChris Mason 		root = list_entry(next, struct btrfs_root, dirty_list);
64687ef2bb4SChris Mason 
6470b86a832SChris Mason 		update_cowonly_root(trans, root);
64879154b1bSChris Mason 	}
649276e680dSYan Zheng 
650276e680dSYan Zheng 	down_write(&fs_info->extent_commit_sem);
651276e680dSYan Zheng 	switch_commit_root(fs_info->extent_root);
652276e680dSYan Zheng 	up_write(&fs_info->extent_commit_sem);
653276e680dSYan Zheng 
65479154b1bSChris Mason 	return 0;
65579154b1bSChris Mason }
65679154b1bSChris Mason 
657d352ac68SChris Mason /*
658d352ac68SChris Mason  * dead roots are old snapshots that need to be deleted.  This allocates
659d352ac68SChris Mason  * a dirty root struct and adds it into the list of dead roots that need to
660d352ac68SChris Mason  * be deleted
661d352ac68SChris Mason  */
6625d4f98a2SYan Zheng int btrfs_add_dead_root(struct btrfs_root *root)
6635eda7b5eSChris Mason {
664b48652c1SYan Zheng 	mutex_lock(&root->fs_info->trans_mutex);
6655d4f98a2SYan Zheng 	list_add(&root->root_list, &root->fs_info->dead_roots);
666b48652c1SYan Zheng 	mutex_unlock(&root->fs_info->trans_mutex);
6675eda7b5eSChris Mason 	return 0;
6685eda7b5eSChris Mason }
6695eda7b5eSChris Mason 
670d352ac68SChris Mason /*
6715d4f98a2SYan Zheng  * update all the cowonly tree roots on disk
672d352ac68SChris Mason  */
6735d4f98a2SYan Zheng static noinline int commit_fs_roots(struct btrfs_trans_handle *trans,
6745d4f98a2SYan Zheng 				    struct btrfs_root *root)
6750f7d52f4SChris Mason {
6760f7d52f4SChris Mason 	struct btrfs_root *gang[8];
6775d4f98a2SYan Zheng 	struct btrfs_fs_info *fs_info = root->fs_info;
6780f7d52f4SChris Mason 	int i;
6790f7d52f4SChris Mason 	int ret;
68054aa1f4dSChris Mason 	int err = 0;
68154aa1f4dSChris Mason 
6820f7d52f4SChris Mason 	while (1) {
6835d4f98a2SYan Zheng 		ret = radix_tree_gang_lookup_tag(&fs_info->fs_roots_radix,
6845d4f98a2SYan Zheng 						 (void **)gang, 0,
6850f7d52f4SChris Mason 						 ARRAY_SIZE(gang),
6860f7d52f4SChris Mason 						 BTRFS_ROOT_TRANS_TAG);
6870f7d52f4SChris Mason 		if (ret == 0)
6880f7d52f4SChris Mason 			break;
6890f7d52f4SChris Mason 		for (i = 0; i < ret; i++) {
6900f7d52f4SChris Mason 			root = gang[i];
6915d4f98a2SYan Zheng 			radix_tree_tag_clear(&fs_info->fs_roots_radix,
6922619ba1fSChris Mason 					(unsigned long)root->root_key.objectid,
6930f7d52f4SChris Mason 					BTRFS_ROOT_TRANS_TAG);
69431153d81SYan Zheng 
695e02119d5SChris Mason 			btrfs_free_log(trans, root);
6965d4f98a2SYan Zheng 			btrfs_update_reloc_root(trans, root);
697d68fc57bSYan, Zheng 			btrfs_orphan_commit_root(trans, root);
698e02119d5SChris Mason 
699978d910dSYan Zheng 			if (root->commit_root != root->node) {
700817d52f8SJosef Bacik 				switch_commit_root(root);
701978d910dSYan Zheng 				btrfs_set_root_node(&root->root_item,
702978d910dSYan Zheng 						    root->node);
703978d910dSYan Zheng 			}
70431153d81SYan Zheng 
7055d4f98a2SYan Zheng 			err = btrfs_update_root(trans, fs_info->tree_root,
7060f7d52f4SChris Mason 						&root->root_key,
7070f7d52f4SChris Mason 						&root->root_item);
70854aa1f4dSChris Mason 			if (err)
70954aa1f4dSChris Mason 				break;
7100f7d52f4SChris Mason 		}
7119f3a7427SChris Mason 	}
71254aa1f4dSChris Mason 	return err;
7130f7d52f4SChris Mason }
7140f7d52f4SChris Mason 
715d352ac68SChris Mason /*
716d352ac68SChris Mason  * defrag a given btree.  If cacheonly == 1, this won't read from the disk,
717d352ac68SChris Mason  * otherwise every leaf in the btree is read and defragged.
718d352ac68SChris Mason  */
719e9d0b13bSChris Mason int btrfs_defrag_root(struct btrfs_root *root, int cacheonly)
720e9d0b13bSChris Mason {
721e9d0b13bSChris Mason 	struct btrfs_fs_info *info = root->fs_info;
722e9d0b13bSChris Mason 	struct btrfs_trans_handle *trans;
7238929ecfaSYan, Zheng 	int ret;
724d3c2fdcfSChris Mason 	unsigned long nr;
725e9d0b13bSChris Mason 
7268929ecfaSYan, Zheng 	if (xchg(&root->defrag_running, 1))
727e9d0b13bSChris Mason 		return 0;
7288929ecfaSYan, Zheng 
7296b80053dSChris Mason 	while (1) {
7308929ecfaSYan, Zheng 		trans = btrfs_start_transaction(root, 0);
7318929ecfaSYan, Zheng 		if (IS_ERR(trans))
7328929ecfaSYan, Zheng 			return PTR_ERR(trans);
7338929ecfaSYan, Zheng 
734e9d0b13bSChris Mason 		ret = btrfs_defrag_leaves(trans, root, cacheonly);
7358929ecfaSYan, Zheng 
736d3c2fdcfSChris Mason 		nr = trans->blocks_used;
737e9d0b13bSChris Mason 		btrfs_end_transaction(trans, root);
738d3c2fdcfSChris Mason 		btrfs_btree_balance_dirty(info->tree_root, nr);
739e9d0b13bSChris Mason 		cond_resched();
740e9d0b13bSChris Mason 
7413f157a2fSChris Mason 		if (root->fs_info->closing || ret != -EAGAIN)
742e9d0b13bSChris Mason 			break;
743e9d0b13bSChris Mason 	}
744e9d0b13bSChris Mason 	root->defrag_running = 0;
7458929ecfaSYan, Zheng 	return ret;
746e9d0b13bSChris Mason }
747e9d0b13bSChris Mason 
7482c47e605SYan Zheng #if 0
749d352ac68SChris Mason /*
750b7ec40d7SChris Mason  * when dropping snapshots, we generate a ton of delayed refs, and it makes
751b7ec40d7SChris Mason  * sense not to join the transaction while it is trying to flush the current
752b7ec40d7SChris Mason  * queue of delayed refs out.
753b7ec40d7SChris Mason  *
754b7ec40d7SChris Mason  * This is used by the drop snapshot code only
755b7ec40d7SChris Mason  */
756b7ec40d7SChris Mason static noinline int wait_transaction_pre_flush(struct btrfs_fs_info *info)
757b7ec40d7SChris Mason {
758b7ec40d7SChris Mason 	DEFINE_WAIT(wait);
759b7ec40d7SChris Mason 
760b7ec40d7SChris Mason 	mutex_lock(&info->trans_mutex);
761b7ec40d7SChris Mason 	while (info->running_transaction &&
762b7ec40d7SChris Mason 	       info->running_transaction->delayed_refs.flushing) {
763b7ec40d7SChris Mason 		prepare_to_wait(&info->transaction_wait, &wait,
764b7ec40d7SChris Mason 				TASK_UNINTERRUPTIBLE);
765b7ec40d7SChris Mason 		mutex_unlock(&info->trans_mutex);
76659bc5c75SChris Mason 
767b7ec40d7SChris Mason 		schedule();
76859bc5c75SChris Mason 
769b7ec40d7SChris Mason 		mutex_lock(&info->trans_mutex);
770b7ec40d7SChris Mason 		finish_wait(&info->transaction_wait, &wait);
771b7ec40d7SChris Mason 	}
772b7ec40d7SChris Mason 	mutex_unlock(&info->trans_mutex);
773b7ec40d7SChris Mason 	return 0;
774b7ec40d7SChris Mason }
775b7ec40d7SChris Mason 
776b7ec40d7SChris Mason /*
777d352ac68SChris Mason  * Given a list of roots that need to be deleted, call btrfs_drop_snapshot on
778d352ac68SChris Mason  * all of them
779d352ac68SChris Mason  */
7805d4f98a2SYan Zheng int btrfs_drop_dead_root(struct btrfs_root *root)
7810f7d52f4SChris Mason {
7820f7d52f4SChris Mason 	struct btrfs_trans_handle *trans;
7835d4f98a2SYan Zheng 	struct btrfs_root *tree_root = root->fs_info->tree_root;
784d3c2fdcfSChris Mason 	unsigned long nr;
7855d4f98a2SYan Zheng 	int ret;
78658176a96SJosef Bacik 
7879f3a7427SChris Mason 	while (1) {
788b7ec40d7SChris Mason 		/*
789b7ec40d7SChris Mason 		 * we don't want to jump in and create a bunch of
790b7ec40d7SChris Mason 		 * delayed refs if the transaction is starting to close
791b7ec40d7SChris Mason 		 */
792b7ec40d7SChris Mason 		wait_transaction_pre_flush(tree_root->fs_info);
7930f7d52f4SChris Mason 		trans = btrfs_start_transaction(tree_root, 1);
794b7ec40d7SChris Mason 
795b7ec40d7SChris Mason 		/*
796b7ec40d7SChris Mason 		 * we've joined a transaction, make sure it isn't
797b7ec40d7SChris Mason 		 * closing right now
798b7ec40d7SChris Mason 		 */
799b7ec40d7SChris Mason 		if (trans->transaction->delayed_refs.flushing) {
800b7ec40d7SChris Mason 			btrfs_end_transaction(trans, tree_root);
801b7ec40d7SChris Mason 			continue;
802b7ec40d7SChris Mason 		}
803b7ec40d7SChris Mason 
8045d4f98a2SYan Zheng 		ret = btrfs_drop_snapshot(trans, root);
805d397712bSChris Mason 		if (ret != -EAGAIN)
8069f3a7427SChris Mason 			break;
80758176a96SJosef Bacik 
8085d4f98a2SYan Zheng 		ret = btrfs_update_root(trans, tree_root,
8095d4f98a2SYan Zheng 					&root->root_key,
8105d4f98a2SYan Zheng 					&root->root_item);
8115d4f98a2SYan Zheng 		if (ret)
81254aa1f4dSChris Mason 			break;
813bcc63abbSYan 
814d3c2fdcfSChris Mason 		nr = trans->blocks_used;
8150f7d52f4SChris Mason 		ret = btrfs_end_transaction(trans, tree_root);
8160f7d52f4SChris Mason 		BUG_ON(ret);
8175eda7b5eSChris Mason 
818d3c2fdcfSChris Mason 		btrfs_btree_balance_dirty(tree_root, nr);
8194dc11904SChris Mason 		cond_resched();
8200f7d52f4SChris Mason 	}
8215d4f98a2SYan Zheng 	BUG_ON(ret);
8225d4f98a2SYan Zheng 
8235d4f98a2SYan Zheng 	ret = btrfs_del_root(trans, tree_root, &root->root_key);
8245d4f98a2SYan Zheng 	BUG_ON(ret);
8255d4f98a2SYan Zheng 
8265d4f98a2SYan Zheng 	nr = trans->blocks_used;
8275d4f98a2SYan Zheng 	ret = btrfs_end_transaction(trans, tree_root);
8285d4f98a2SYan Zheng 	BUG_ON(ret);
8295d4f98a2SYan Zheng 
8305d4f98a2SYan Zheng 	free_extent_buffer(root->node);
8315d4f98a2SYan Zheng 	free_extent_buffer(root->commit_root);
8325d4f98a2SYan Zheng 	kfree(root);
8335d4f98a2SYan Zheng 
8345d4f98a2SYan Zheng 	btrfs_btree_balance_dirty(tree_root, nr);
83554aa1f4dSChris Mason 	return ret;
8360f7d52f4SChris Mason }
8372c47e605SYan Zheng #endif
8380f7d52f4SChris Mason 
839d352ac68SChris Mason /*
840d352ac68SChris Mason  * new snapshots need to be created at a very specific time in the
841d352ac68SChris Mason  * transaction commit.  This does the actual creation
842d352ac68SChris Mason  */
84380b6794dSChris Mason static noinline int create_pending_snapshot(struct btrfs_trans_handle *trans,
8443063d29fSChris Mason 				   struct btrfs_fs_info *fs_info,
8453063d29fSChris Mason 				   struct btrfs_pending_snapshot *pending)
8463063d29fSChris Mason {
8473063d29fSChris Mason 	struct btrfs_key key;
84880b6794dSChris Mason 	struct btrfs_root_item *new_root_item;
8493063d29fSChris Mason 	struct btrfs_root *tree_root = fs_info->tree_root;
8503063d29fSChris Mason 	struct btrfs_root *root = pending->root;
8516bdb72deSSage Weil 	struct btrfs_root *parent_root;
8526bdb72deSSage Weil 	struct inode *parent_inode;
853a22285a6SYan, Zheng 	struct dentry *dentry;
8543063d29fSChris Mason 	struct extent_buffer *tmp;
855925baeddSChris Mason 	struct extent_buffer *old;
8563063d29fSChris Mason 	int ret;
857d68fc57bSYan, Zheng 	u64 to_reserve = 0;
8586bdb72deSSage Weil 	u64 index = 0;
859a22285a6SYan, Zheng 	u64 objectid;
8603063d29fSChris Mason 
86180b6794dSChris Mason 	new_root_item = kmalloc(sizeof(*new_root_item), GFP_NOFS);
86280b6794dSChris Mason 	if (!new_root_item) {
863a22285a6SYan, Zheng 		pending->error = -ENOMEM;
86480b6794dSChris Mason 		goto fail;
86580b6794dSChris Mason 	}
866a22285a6SYan, Zheng 
8673063d29fSChris Mason 	ret = btrfs_find_free_objectid(trans, tree_root, 0, &objectid);
868a22285a6SYan, Zheng 	if (ret) {
869a22285a6SYan, Zheng 		pending->error = ret;
8703063d29fSChris Mason 		goto fail;
871a22285a6SYan, Zheng 	}
8723063d29fSChris Mason 
8733fd0a558SYan, Zheng 	btrfs_reloc_pre_snapshot(trans, pending, &to_reserve);
874d68fc57bSYan, Zheng 	btrfs_orphan_pre_snapshot(trans, pending, &to_reserve);
875d68fc57bSYan, Zheng 
876d68fc57bSYan, Zheng 	if (to_reserve > 0) {
877d68fc57bSYan, Zheng 		ret = btrfs_block_rsv_add(trans, root, &pending->block_rsv,
8788bb8ab2eSJosef Bacik 					  to_reserve);
879d68fc57bSYan, Zheng 		if (ret) {
880d68fc57bSYan, Zheng 			pending->error = ret;
881d68fc57bSYan, Zheng 			goto fail;
882d68fc57bSYan, Zheng 		}
883d68fc57bSYan, Zheng 	}
884d68fc57bSYan, Zheng 
8853063d29fSChris Mason 	key.objectid = objectid;
886a22285a6SYan, Zheng 	key.offset = (u64)-1;
887a22285a6SYan, Zheng 	key.type = BTRFS_ROOT_ITEM_KEY;
8883063d29fSChris Mason 
889a22285a6SYan, Zheng 	trans->block_rsv = &pending->block_rsv;
8906bdb72deSSage Weil 
891a22285a6SYan, Zheng 	dentry = pending->dentry;
892a22285a6SYan, Zheng 	parent_inode = dentry->d_parent->d_inode;
893a22285a6SYan, Zheng 	parent_root = BTRFS_I(parent_inode)->root;
8946bdb72deSSage Weil 	record_root_in_trans(trans, parent_root);
895a22285a6SYan, Zheng 
8966bdb72deSSage Weil 	/*
8976bdb72deSSage Weil 	 * insert the directory item
8986bdb72deSSage Weil 	 */
8996bdb72deSSage Weil 	ret = btrfs_set_inode_index(parent_inode, &index);
9006bdb72deSSage Weil 	BUG_ON(ret);
9016bdb72deSSage Weil 	ret = btrfs_insert_dir_item(trans, parent_root,
902a22285a6SYan, Zheng 				dentry->d_name.name, dentry->d_name.len,
903a22285a6SYan, Zheng 				parent_inode->i_ino, &key,
904a22285a6SYan, Zheng 				BTRFS_FT_DIR, index);
9056bdb72deSSage Weil 	BUG_ON(ret);
9066bdb72deSSage Weil 
907a22285a6SYan, Zheng 	btrfs_i_size_write(parent_inode, parent_inode->i_size +
908a22285a6SYan, Zheng 					 dentry->d_name.len * 2);
9096bdb72deSSage Weil 	ret = btrfs_update_inode(trans, parent_root, parent_inode);
9106bdb72deSSage Weil 	BUG_ON(ret);
9116bdb72deSSage Weil 
9126bdb72deSSage Weil 	record_root_in_trans(trans, root);
9136bdb72deSSage Weil 	btrfs_set_root_last_snapshot(&root->root_item, trans->transid);
9146bdb72deSSage Weil 	memcpy(new_root_item, &root->root_item, sizeof(*new_root_item));
9156bdb72deSSage Weil 
916925baeddSChris Mason 	old = btrfs_lock_root_node(root);
9179fa8cfe7SChris Mason 	btrfs_cow_block(trans, root, old, NULL, 0, &old);
9185d4f98a2SYan Zheng 	btrfs_set_lock_blocking(old);
9193063d29fSChris Mason 
920925baeddSChris Mason 	btrfs_copy_root(trans, root, old, &tmp, objectid);
921925baeddSChris Mason 	btrfs_tree_unlock(old);
922925baeddSChris Mason 	free_extent_buffer(old);
9233063d29fSChris Mason 
9245d4f98a2SYan Zheng 	btrfs_set_root_node(new_root_item, tmp);
925a22285a6SYan, Zheng 	/* record when the snapshot was created in key.offset */
926a22285a6SYan, Zheng 	key.offset = trans->transid;
927a22285a6SYan, Zheng 	ret = btrfs_insert_root(trans, tree_root, &key, new_root_item);
928925baeddSChris Mason 	btrfs_tree_unlock(tmp);
9293063d29fSChris Mason 	free_extent_buffer(tmp);
9300660b5afSChris Mason 	BUG_ON(ret);
9310660b5afSChris Mason 
932a22285a6SYan, Zheng 	/*
933a22285a6SYan, Zheng 	 * insert root back/forward references
934a22285a6SYan, Zheng 	 */
935a22285a6SYan, Zheng 	ret = btrfs_add_root_ref(trans, tree_root, objectid,
936a22285a6SYan, Zheng 				 parent_root->root_key.objectid,
937a22285a6SYan, Zheng 				 parent_inode->i_ino, index,
938a22285a6SYan, Zheng 				 dentry->d_name.name, dentry->d_name.len);
939a22285a6SYan, Zheng 	BUG_ON(ret);
940a22285a6SYan, Zheng 
941a22285a6SYan, Zheng 	key.offset = (u64)-1;
942a22285a6SYan, Zheng 	pending->snap = btrfs_read_fs_root_no_name(root->fs_info, &key);
943a22285a6SYan, Zheng 	BUG_ON(IS_ERR(pending->snap));
944d68fc57bSYan, Zheng 
9453fd0a558SYan, Zheng 	btrfs_reloc_post_snapshot(trans, pending);
946d68fc57bSYan, Zheng 	btrfs_orphan_post_snapshot(trans, pending);
9473063d29fSChris Mason fail:
9486bdb72deSSage Weil 	kfree(new_root_item);
949a22285a6SYan, Zheng 	btrfs_block_rsv_release(root, &pending->block_rsv, (u64)-1);
950a22285a6SYan, Zheng 	return 0;
9513063d29fSChris Mason }
9523063d29fSChris Mason 
953d352ac68SChris Mason /*
954d352ac68SChris Mason  * create all the snapshots we've scheduled for creation
955d352ac68SChris Mason  */
95680b6794dSChris Mason static noinline int create_pending_snapshots(struct btrfs_trans_handle *trans,
9573063d29fSChris Mason 					     struct btrfs_fs_info *fs_info)
9583063d29fSChris Mason {
9593063d29fSChris Mason 	struct btrfs_pending_snapshot *pending;
9603063d29fSChris Mason 	struct list_head *head = &trans->transaction->pending_snapshots;
9613de4586cSChris Mason 	int ret;
9623de4586cSChris Mason 
963c6e30871SQinghuang Feng 	list_for_each_entry(pending, head, list) {
9643de4586cSChris Mason 		ret = create_pending_snapshot(trans, fs_info, pending);
9653de4586cSChris Mason 		BUG_ON(ret);
9663de4586cSChris Mason 	}
9673de4586cSChris Mason 	return 0;
9683de4586cSChris Mason }
9693de4586cSChris Mason 
9705d4f98a2SYan Zheng static void update_super_roots(struct btrfs_root *root)
9715d4f98a2SYan Zheng {
9725d4f98a2SYan Zheng 	struct btrfs_root_item *root_item;
9735d4f98a2SYan Zheng 	struct btrfs_super_block *super;
9745d4f98a2SYan Zheng 
9755d4f98a2SYan Zheng 	super = &root->fs_info->super_copy;
9765d4f98a2SYan Zheng 
9775d4f98a2SYan Zheng 	root_item = &root->fs_info->chunk_root->root_item;
9785d4f98a2SYan Zheng 	super->chunk_root = root_item->bytenr;
9795d4f98a2SYan Zheng 	super->chunk_root_generation = root_item->generation;
9805d4f98a2SYan Zheng 	super->chunk_root_level = root_item->level;
9815d4f98a2SYan Zheng 
9825d4f98a2SYan Zheng 	root_item = &root->fs_info->tree_root->root_item;
9835d4f98a2SYan Zheng 	super->root = root_item->bytenr;
9845d4f98a2SYan Zheng 	super->generation = root_item->generation;
9855d4f98a2SYan Zheng 	super->root_level = root_item->level;
9860af3d00bSJosef Bacik 	if (super->cache_generation != 0 || btrfs_test_opt(root, SPACE_CACHE))
9870af3d00bSJosef Bacik 		super->cache_generation = root_item->generation;
9885d4f98a2SYan Zheng }
9895d4f98a2SYan Zheng 
990f36f3042SChris Mason int btrfs_transaction_in_commit(struct btrfs_fs_info *info)
991f36f3042SChris Mason {
992f36f3042SChris Mason 	int ret = 0;
993f36f3042SChris Mason 	spin_lock(&info->new_trans_lock);
994f36f3042SChris Mason 	if (info->running_transaction)
995f36f3042SChris Mason 		ret = info->running_transaction->in_commit;
996f36f3042SChris Mason 	spin_unlock(&info->new_trans_lock);
997f36f3042SChris Mason 	return ret;
998f36f3042SChris Mason }
999f36f3042SChris Mason 
10008929ecfaSYan, Zheng int btrfs_transaction_blocked(struct btrfs_fs_info *info)
10018929ecfaSYan, Zheng {
10028929ecfaSYan, Zheng 	int ret = 0;
10038929ecfaSYan, Zheng 	spin_lock(&info->new_trans_lock);
10048929ecfaSYan, Zheng 	if (info->running_transaction)
10058929ecfaSYan, Zheng 		ret = info->running_transaction->blocked;
10068929ecfaSYan, Zheng 	spin_unlock(&info->new_trans_lock);
10078929ecfaSYan, Zheng 	return ret;
10088929ecfaSYan, Zheng }
10098929ecfaSYan, Zheng 
1010bb9c12c9SSage Weil /*
1011bb9c12c9SSage Weil  * wait for the current transaction commit to start and block subsequent
1012bb9c12c9SSage Weil  * transaction joins
1013bb9c12c9SSage Weil  */
1014bb9c12c9SSage Weil static void wait_current_trans_commit_start(struct btrfs_root *root,
1015bb9c12c9SSage Weil 					    struct btrfs_transaction *trans)
1016bb9c12c9SSage Weil {
1017bb9c12c9SSage Weil 	DEFINE_WAIT(wait);
1018bb9c12c9SSage Weil 
1019bb9c12c9SSage Weil 	if (trans->in_commit)
1020bb9c12c9SSage Weil 		return;
1021bb9c12c9SSage Weil 
1022bb9c12c9SSage Weil 	while (1) {
1023bb9c12c9SSage Weil 		prepare_to_wait(&root->fs_info->transaction_blocked_wait, &wait,
1024bb9c12c9SSage Weil 				TASK_UNINTERRUPTIBLE);
1025bb9c12c9SSage Weil 		if (trans->in_commit) {
1026bb9c12c9SSage Weil 			finish_wait(&root->fs_info->transaction_blocked_wait,
1027bb9c12c9SSage Weil 				    &wait);
1028bb9c12c9SSage Weil 			break;
1029bb9c12c9SSage Weil 		}
1030bb9c12c9SSage Weil 		mutex_unlock(&root->fs_info->trans_mutex);
1031bb9c12c9SSage Weil 		schedule();
1032bb9c12c9SSage Weil 		mutex_lock(&root->fs_info->trans_mutex);
1033bb9c12c9SSage Weil 		finish_wait(&root->fs_info->transaction_blocked_wait, &wait);
1034bb9c12c9SSage Weil 	}
1035bb9c12c9SSage Weil }
1036bb9c12c9SSage Weil 
1037bb9c12c9SSage Weil /*
1038bb9c12c9SSage Weil  * wait for the current transaction to start and then become unblocked.
1039bb9c12c9SSage Weil  * caller holds ref.
1040bb9c12c9SSage Weil  */
1041bb9c12c9SSage Weil static void wait_current_trans_commit_start_and_unblock(struct btrfs_root *root,
1042bb9c12c9SSage Weil 					 struct btrfs_transaction *trans)
1043bb9c12c9SSage Weil {
1044bb9c12c9SSage Weil 	DEFINE_WAIT(wait);
1045bb9c12c9SSage Weil 
1046bb9c12c9SSage Weil 	if (trans->commit_done || (trans->in_commit && !trans->blocked))
1047bb9c12c9SSage Weil 		return;
1048bb9c12c9SSage Weil 
1049bb9c12c9SSage Weil 	while (1) {
1050bb9c12c9SSage Weil 		prepare_to_wait(&root->fs_info->transaction_wait, &wait,
1051bb9c12c9SSage Weil 				TASK_UNINTERRUPTIBLE);
1052bb9c12c9SSage Weil 		if (trans->commit_done ||
1053bb9c12c9SSage Weil 		    (trans->in_commit && !trans->blocked)) {
1054bb9c12c9SSage Weil 			finish_wait(&root->fs_info->transaction_wait,
1055bb9c12c9SSage Weil 				    &wait);
1056bb9c12c9SSage Weil 			break;
1057bb9c12c9SSage Weil 		}
1058bb9c12c9SSage Weil 		mutex_unlock(&root->fs_info->trans_mutex);
1059bb9c12c9SSage Weil 		schedule();
1060bb9c12c9SSage Weil 		mutex_lock(&root->fs_info->trans_mutex);
1061bb9c12c9SSage Weil 		finish_wait(&root->fs_info->transaction_wait,
1062bb9c12c9SSage Weil 			    &wait);
1063bb9c12c9SSage Weil 	}
1064bb9c12c9SSage Weil }
1065bb9c12c9SSage Weil 
1066bb9c12c9SSage Weil /*
1067bb9c12c9SSage Weil  * commit transactions asynchronously. once btrfs_commit_transaction_async
1068bb9c12c9SSage Weil  * returns, any subsequent transaction will not be allowed to join.
1069bb9c12c9SSage Weil  */
1070bb9c12c9SSage Weil struct btrfs_async_commit {
1071bb9c12c9SSage Weil 	struct btrfs_trans_handle *newtrans;
1072bb9c12c9SSage Weil 	struct btrfs_root *root;
1073bb9c12c9SSage Weil 	struct delayed_work work;
1074bb9c12c9SSage Weil };
1075bb9c12c9SSage Weil 
1076bb9c12c9SSage Weil static void do_async_commit(struct work_struct *work)
1077bb9c12c9SSage Weil {
1078bb9c12c9SSage Weil 	struct btrfs_async_commit *ac =
1079bb9c12c9SSage Weil 		container_of(work, struct btrfs_async_commit, work.work);
1080bb9c12c9SSage Weil 
1081bb9c12c9SSage Weil 	btrfs_commit_transaction(ac->newtrans, ac->root);
1082bb9c12c9SSage Weil 	kfree(ac);
1083bb9c12c9SSage Weil }
1084bb9c12c9SSage Weil 
1085bb9c12c9SSage Weil int btrfs_commit_transaction_async(struct btrfs_trans_handle *trans,
1086bb9c12c9SSage Weil 				   struct btrfs_root *root,
1087bb9c12c9SSage Weil 				   int wait_for_unblock)
1088bb9c12c9SSage Weil {
1089bb9c12c9SSage Weil 	struct btrfs_async_commit *ac;
1090bb9c12c9SSage Weil 	struct btrfs_transaction *cur_trans;
1091bb9c12c9SSage Weil 
1092bb9c12c9SSage Weil 	ac = kmalloc(sizeof(*ac), GFP_NOFS);
1093bb9c12c9SSage Weil 	BUG_ON(!ac);
1094bb9c12c9SSage Weil 
1095bb9c12c9SSage Weil 	INIT_DELAYED_WORK(&ac->work, do_async_commit);
1096bb9c12c9SSage Weil 	ac->root = root;
1097bb9c12c9SSage Weil 	ac->newtrans = btrfs_join_transaction(root, 0);
1098bb9c12c9SSage Weil 
1099bb9c12c9SSage Weil 	/* take transaction reference */
1100bb9c12c9SSage Weil 	mutex_lock(&root->fs_info->trans_mutex);
1101bb9c12c9SSage Weil 	cur_trans = trans->transaction;
1102bb9c12c9SSage Weil 	cur_trans->use_count++;
1103bb9c12c9SSage Weil 	mutex_unlock(&root->fs_info->trans_mutex);
1104bb9c12c9SSage Weil 
1105bb9c12c9SSage Weil 	btrfs_end_transaction(trans, root);
1106bb9c12c9SSage Weil 	schedule_delayed_work(&ac->work, 0);
1107bb9c12c9SSage Weil 
1108bb9c12c9SSage Weil 	/* wait for transaction to start and unblock */
1109bb9c12c9SSage Weil 	mutex_lock(&root->fs_info->trans_mutex);
1110bb9c12c9SSage Weil 	if (wait_for_unblock)
1111bb9c12c9SSage Weil 		wait_current_trans_commit_start_and_unblock(root, cur_trans);
1112bb9c12c9SSage Weil 	else
1113bb9c12c9SSage Weil 		wait_current_trans_commit_start(root, cur_trans);
1114bb9c12c9SSage Weil 	put_transaction(cur_trans);
1115bb9c12c9SSage Weil 	mutex_unlock(&root->fs_info->trans_mutex);
1116bb9c12c9SSage Weil 
1117bb9c12c9SSage Weil 	return 0;
1118bb9c12c9SSage Weil }
1119bb9c12c9SSage Weil 
1120bb9c12c9SSage Weil /*
1121bb9c12c9SSage Weil  * btrfs_transaction state sequence:
1122bb9c12c9SSage Weil  *    in_commit = 0, blocked = 0  (initial)
1123bb9c12c9SSage Weil  *    in_commit = 1, blocked = 1
1124bb9c12c9SSage Weil  *    blocked = 0
1125bb9c12c9SSage Weil  *    commit_done = 1
1126bb9c12c9SSage Weil  */
112779154b1bSChris Mason int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
112879154b1bSChris Mason 			     struct btrfs_root *root)
112979154b1bSChris Mason {
113015ee9bc7SJosef Bacik 	unsigned long joined = 0;
113179154b1bSChris Mason 	struct btrfs_transaction *cur_trans;
11328fd17795SChris Mason 	struct btrfs_transaction *prev_trans = NULL;
113379154b1bSChris Mason 	DEFINE_WAIT(wait);
113415ee9bc7SJosef Bacik 	int ret;
113589573b9cSChris Mason 	int should_grow = 0;
113689573b9cSChris Mason 	unsigned long now = get_seconds();
1137dccae999SSage Weil 	int flush_on_commit = btrfs_test_opt(root, FLUSHONCOMMIT);
113879154b1bSChris Mason 
11395a3f23d5SChris Mason 	btrfs_run_ordered_operations(root, 0);
11405a3f23d5SChris Mason 
114156bec294SChris Mason 	/* make a pass through all the delayed refs we have so far
114256bec294SChris Mason 	 * any runnings procs may add more while we are here
114356bec294SChris Mason 	 */
114456bec294SChris Mason 	ret = btrfs_run_delayed_refs(trans, root, 0);
114556bec294SChris Mason 	BUG_ON(ret);
114656bec294SChris Mason 
1147a22285a6SYan, Zheng 	btrfs_trans_release_metadata(trans, root);
1148a22285a6SYan, Zheng 
1149b7ec40d7SChris Mason 	cur_trans = trans->transaction;
115056bec294SChris Mason 	/*
115156bec294SChris Mason 	 * set the flushing flag so procs in this transaction have to
115256bec294SChris Mason 	 * start sending their work down.
115356bec294SChris Mason 	 */
1154b7ec40d7SChris Mason 	cur_trans->delayed_refs.flushing = 1;
115556bec294SChris Mason 
1156c3e69d58SChris Mason 	ret = btrfs_run_delayed_refs(trans, root, 0);
115756bec294SChris Mason 	BUG_ON(ret);
115856bec294SChris Mason 
115979154b1bSChris Mason 	mutex_lock(&root->fs_info->trans_mutex);
1160b7ec40d7SChris Mason 	if (cur_trans->in_commit) {
1161b7ec40d7SChris Mason 		cur_trans->use_count++;
1162ccd467d6SChris Mason 		mutex_unlock(&root->fs_info->trans_mutex);
116379154b1bSChris Mason 		btrfs_end_transaction(trans, root);
1164ccd467d6SChris Mason 
116579154b1bSChris Mason 		ret = wait_for_commit(root, cur_trans);
116679154b1bSChris Mason 		BUG_ON(ret);
116715ee9bc7SJosef Bacik 
116815ee9bc7SJosef Bacik 		mutex_lock(&root->fs_info->trans_mutex);
116979154b1bSChris Mason 		put_transaction(cur_trans);
117015ee9bc7SJosef Bacik 		mutex_unlock(&root->fs_info->trans_mutex);
117115ee9bc7SJosef Bacik 
117279154b1bSChris Mason 		return 0;
117379154b1bSChris Mason 	}
11744313b399SChris Mason 
11752c90e5d6SChris Mason 	trans->transaction->in_commit = 1;
1176f9295749SChris Mason 	trans->transaction->blocked = 1;
1177bb9c12c9SSage Weil 	wake_up(&root->fs_info->transaction_blocked_wait);
1178bb9c12c9SSage Weil 
1179ccd467d6SChris Mason 	if (cur_trans->list.prev != &root->fs_info->trans_list) {
1180ccd467d6SChris Mason 		prev_trans = list_entry(cur_trans->list.prev,
1181ccd467d6SChris Mason 					struct btrfs_transaction, list);
1182ccd467d6SChris Mason 		if (!prev_trans->commit_done) {
1183ccd467d6SChris Mason 			prev_trans->use_count++;
1184ccd467d6SChris Mason 			mutex_unlock(&root->fs_info->trans_mutex);
1185ccd467d6SChris Mason 
1186ccd467d6SChris Mason 			wait_for_commit(root, prev_trans);
1187ccd467d6SChris Mason 
1188ccd467d6SChris Mason 			mutex_lock(&root->fs_info->trans_mutex);
118915ee9bc7SJosef Bacik 			put_transaction(prev_trans);
1190ccd467d6SChris Mason 		}
1191ccd467d6SChris Mason 	}
119215ee9bc7SJosef Bacik 
119389573b9cSChris Mason 	if (now < cur_trans->start_time || now - cur_trans->start_time < 1)
119489573b9cSChris Mason 		should_grow = 1;
119589573b9cSChris Mason 
119615ee9bc7SJosef Bacik 	do {
11977ea394f1SYan Zheng 		int snap_pending = 0;
119815ee9bc7SJosef Bacik 		joined = cur_trans->num_joined;
11997ea394f1SYan Zheng 		if (!list_empty(&trans->transaction->pending_snapshots))
12007ea394f1SYan Zheng 			snap_pending = 1;
12017ea394f1SYan Zheng 
12022c90e5d6SChris Mason 		WARN_ON(cur_trans != trans->transaction);
120379154b1bSChris Mason 		mutex_unlock(&root->fs_info->trans_mutex);
120415ee9bc7SJosef Bacik 
12050bdb1db2SSage Weil 		if (flush_on_commit || snap_pending) {
120624bbcf04SYan, Zheng 			btrfs_start_delalloc_inodes(root, 1);
120724bbcf04SYan, Zheng 			ret = btrfs_wait_ordered_extents(root, 0, 1);
1208ebecd3d9SSage Weil 			BUG_ON(ret);
12097ea394f1SYan Zheng 		}
12107ea394f1SYan Zheng 
12115a3f23d5SChris Mason 		/*
12125a3f23d5SChris Mason 		 * rename don't use btrfs_join_transaction, so, once we
12135a3f23d5SChris Mason 		 * set the transaction to blocked above, we aren't going
12145a3f23d5SChris Mason 		 * to get any new ordered operations.  We can safely run
12155a3f23d5SChris Mason 		 * it here and no for sure that nothing new will be added
12165a3f23d5SChris Mason 		 * to the list
12175a3f23d5SChris Mason 		 */
12185a3f23d5SChris Mason 		btrfs_run_ordered_operations(root, 1);
12195a3f23d5SChris Mason 
1220ed3b3d31SChris Mason 		prepare_to_wait(&cur_trans->writer_wait, &wait,
1221ed3b3d31SChris Mason 				TASK_UNINTERRUPTIBLE);
1222ed3b3d31SChris Mason 
122389573b9cSChris Mason 		smp_mb();
122499d16cbcSSage Weil 		if (cur_trans->num_writers > 1)
122599d16cbcSSage Weil 			schedule_timeout(MAX_SCHEDULE_TIMEOUT);
122699d16cbcSSage Weil 		else if (should_grow)
122799d16cbcSSage Weil 			schedule_timeout(1);
122815ee9bc7SJosef Bacik 
122979154b1bSChris Mason 		mutex_lock(&root->fs_info->trans_mutex);
123015ee9bc7SJosef Bacik 		finish_wait(&cur_trans->writer_wait, &wait);
123115ee9bc7SJosef Bacik 	} while (cur_trans->num_writers > 1 ||
123289573b9cSChris Mason 		 (should_grow && cur_trans->num_joined != joined));
123315ee9bc7SJosef Bacik 
12343063d29fSChris Mason 	ret = create_pending_snapshots(trans, root->fs_info);
12353063d29fSChris Mason 	BUG_ON(ret);
12363063d29fSChris Mason 
123756bec294SChris Mason 	ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
123856bec294SChris Mason 	BUG_ON(ret);
123956bec294SChris Mason 
12402c90e5d6SChris Mason 	WARN_ON(cur_trans != trans->transaction);
1241dc17ff8fSChris Mason 
1242e02119d5SChris Mason 	/* btrfs_commit_tree_roots is responsible for getting the
1243e02119d5SChris Mason 	 * various roots consistent with each other.  Every pointer
1244e02119d5SChris Mason 	 * in the tree of tree roots has to point to the most up to date
1245e02119d5SChris Mason 	 * root for every subvolume and other tree.  So, we have to keep
1246e02119d5SChris Mason 	 * the tree logging code from jumping in and changing any
1247e02119d5SChris Mason 	 * of the trees.
1248e02119d5SChris Mason 	 *
1249e02119d5SChris Mason 	 * At this point in the commit, there can't be any tree-log
1250e02119d5SChris Mason 	 * writers, but a little lower down we drop the trans mutex
1251e02119d5SChris Mason 	 * and let new people in.  By holding the tree_log_mutex
1252e02119d5SChris Mason 	 * from now until after the super is written, we avoid races
1253e02119d5SChris Mason 	 * with the tree-log code.
1254e02119d5SChris Mason 	 */
1255e02119d5SChris Mason 	mutex_lock(&root->fs_info->tree_log_mutex);
12561a40e23bSZheng Yan 
12575d4f98a2SYan Zheng 	ret = commit_fs_roots(trans, root);
125854aa1f4dSChris Mason 	BUG_ON(ret);
125954aa1f4dSChris Mason 
12605d4f98a2SYan Zheng 	/* commit_fs_roots gets rid of all the tree log roots, it is now
1261e02119d5SChris Mason 	 * safe to free the root of tree log roots
1262e02119d5SChris Mason 	 */
1263e02119d5SChris Mason 	btrfs_free_log_root_tree(trans, root->fs_info);
1264e02119d5SChris Mason 
12655d4f98a2SYan Zheng 	ret = commit_cowonly_roots(trans, root);
126679154b1bSChris Mason 	BUG_ON(ret);
126754aa1f4dSChris Mason 
126811833d66SYan Zheng 	btrfs_prepare_extent_commit(trans, root);
126911833d66SYan Zheng 
127078fae27eSChris Mason 	cur_trans = root->fs_info->running_transaction;
1271cee36a03SChris Mason 	spin_lock(&root->fs_info->new_trans_lock);
127278fae27eSChris Mason 	root->fs_info->running_transaction = NULL;
1273cee36a03SChris Mason 	spin_unlock(&root->fs_info->new_trans_lock);
12745f39d397SChris Mason 
12755d4f98a2SYan Zheng 	btrfs_set_root_node(&root->fs_info->tree_root->root_item,
12765d4f98a2SYan Zheng 			    root->fs_info->tree_root->node);
1277817d52f8SJosef Bacik 	switch_commit_root(root->fs_info->tree_root);
12785d4f98a2SYan Zheng 
12795d4f98a2SYan Zheng 	btrfs_set_root_node(&root->fs_info->chunk_root->root_item,
12805d4f98a2SYan Zheng 			    root->fs_info->chunk_root->node);
1281817d52f8SJosef Bacik 	switch_commit_root(root->fs_info->chunk_root);
12825d4f98a2SYan Zheng 
12835d4f98a2SYan Zheng 	update_super_roots(root);
1284e02119d5SChris Mason 
1285e02119d5SChris Mason 	if (!root->fs_info->log_root_recovering) {
1286e02119d5SChris Mason 		btrfs_set_super_log_root(&root->fs_info->super_copy, 0);
1287e02119d5SChris Mason 		btrfs_set_super_log_root_level(&root->fs_info->super_copy, 0);
1288e02119d5SChris Mason 	}
1289e02119d5SChris Mason 
1290a061fc8dSChris Mason 	memcpy(&root->fs_info->super_for_commit, &root->fs_info->super_copy,
12914b52dff6SChris Mason 	       sizeof(root->fs_info->super_copy));
1292ccd467d6SChris Mason 
1293f9295749SChris Mason 	trans->transaction->blocked = 0;
1294b7ec40d7SChris Mason 
1295f9295749SChris Mason 	wake_up(&root->fs_info->transaction_wait);
1296e6dcd2dcSChris Mason 
129778fae27eSChris Mason 	mutex_unlock(&root->fs_info->trans_mutex);
129879154b1bSChris Mason 	ret = btrfs_write_and_wait_transaction(trans, root);
129979154b1bSChris Mason 	BUG_ON(ret);
1300a512bbf8SYan Zheng 	write_ctree_super(trans, root, 0);
13014313b399SChris Mason 
1302e02119d5SChris Mason 	/*
1303e02119d5SChris Mason 	 * the super is written, we can safely allow the tree-loggers
1304e02119d5SChris Mason 	 * to go about their business
1305e02119d5SChris Mason 	 */
1306e02119d5SChris Mason 	mutex_unlock(&root->fs_info->tree_log_mutex);
1307e02119d5SChris Mason 
130811833d66SYan Zheng 	btrfs_finish_extent_commit(trans, root);
13094313b399SChris Mason 
13101a40e23bSZheng Yan 	mutex_lock(&root->fs_info->trans_mutex);
13111a40e23bSZheng Yan 
13122c90e5d6SChris Mason 	cur_trans->commit_done = 1;
1313b7ec40d7SChris Mason 
131415ee9bc7SJosef Bacik 	root->fs_info->last_trans_committed = cur_trans->transid;
1315817d52f8SJosef Bacik 
13162c90e5d6SChris Mason 	wake_up(&cur_trans->commit_wait);
13173de4586cSChris Mason 
131879154b1bSChris Mason 	put_transaction(cur_trans);
131978fae27eSChris Mason 	put_transaction(cur_trans);
132058176a96SJosef Bacik 
132178fae27eSChris Mason 	mutex_unlock(&root->fs_info->trans_mutex);
13223de4586cSChris Mason 
13239ed74f2dSJosef Bacik 	if (current->journal_info == trans)
13249ed74f2dSJosef Bacik 		current->journal_info = NULL;
13259ed74f2dSJosef Bacik 
13262c90e5d6SChris Mason 	kmem_cache_free(btrfs_trans_handle_cachep, trans);
132724bbcf04SYan, Zheng 
132824bbcf04SYan, Zheng 	if (current != root->fs_info->transaction_kthread)
132924bbcf04SYan, Zheng 		btrfs_run_delayed_iputs(root);
133024bbcf04SYan, Zheng 
133179154b1bSChris Mason 	return ret;
133279154b1bSChris Mason }
133379154b1bSChris Mason 
1334d352ac68SChris Mason /*
1335d352ac68SChris Mason  * interface function to delete all the snapshots we have scheduled for deletion
1336d352ac68SChris Mason  */
1337e9d0b13bSChris Mason int btrfs_clean_old_snapshots(struct btrfs_root *root)
1338e9d0b13bSChris Mason {
13395d4f98a2SYan Zheng 	LIST_HEAD(list);
13405d4f98a2SYan Zheng 	struct btrfs_fs_info *fs_info = root->fs_info;
1341e9d0b13bSChris Mason 
13425d4f98a2SYan Zheng 	mutex_lock(&fs_info->trans_mutex);
13435d4f98a2SYan Zheng 	list_splice_init(&fs_info->dead_roots, &list);
13445d4f98a2SYan Zheng 	mutex_unlock(&fs_info->trans_mutex);
13455d4f98a2SYan Zheng 
13465d4f98a2SYan Zheng 	while (!list_empty(&list)) {
13475d4f98a2SYan Zheng 		root = list_entry(list.next, struct btrfs_root, root_list);
134876dda93cSYan, Zheng 		list_del(&root->root_list);
134976dda93cSYan, Zheng 
135076dda93cSYan, Zheng 		if (btrfs_header_backref_rev(root->node) <
135176dda93cSYan, Zheng 		    BTRFS_MIXED_BACKREF_REV)
13523fd0a558SYan, Zheng 			btrfs_drop_snapshot(root, NULL, 0);
135376dda93cSYan, Zheng 		else
13543fd0a558SYan, Zheng 			btrfs_drop_snapshot(root, NULL, 1);
1355e9d0b13bSChris Mason 	}
1356e9d0b13bSChris Mason 	return 0;
1357e9d0b13bSChris Mason }
1358