xref: /openbmc/linux/fs/btrfs/transaction.c (revision 581bb050)
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"
3179154b1bSChris Mason 
320f7d52f4SChris Mason #define BTRFS_ROOT_TRANS_TAG 0
330f7d52f4SChris Mason 
3480b6794dSChris Mason static noinline void put_transaction(struct btrfs_transaction *transaction)
3579154b1bSChris Mason {
3613c5a93eSJosef Bacik 	WARN_ON(atomic_read(&transaction->use_count) == 0);
3713c5a93eSJosef Bacik 	if (atomic_dec_and_test(&transaction->use_count)) {
382c90e5d6SChris Mason 		memset(transaction, 0, sizeof(*transaction));
392c90e5d6SChris Mason 		kmem_cache_free(btrfs_transaction_cachep, transaction);
4079154b1bSChris Mason 	}
4178fae27eSChris Mason }
4279154b1bSChris Mason 
43817d52f8SJosef Bacik static noinline void switch_commit_root(struct btrfs_root *root)
44817d52f8SJosef Bacik {
45817d52f8SJosef Bacik 	free_extent_buffer(root->commit_root);
46817d52f8SJosef Bacik 	root->commit_root = btrfs_root_node(root);
47817d52f8SJosef Bacik }
48817d52f8SJosef Bacik 
49d352ac68SChris Mason /*
50d352ac68SChris Mason  * either allocate a new transaction or hop into the existing one
51d352ac68SChris Mason  */
5280b6794dSChris Mason static noinline int join_transaction(struct btrfs_root *root)
5379154b1bSChris Mason {
5479154b1bSChris Mason 	struct btrfs_transaction *cur_trans;
5579154b1bSChris Mason 	cur_trans = root->fs_info->running_transaction;
5679154b1bSChris Mason 	if (!cur_trans) {
572c90e5d6SChris Mason 		cur_trans = kmem_cache_alloc(btrfs_transaction_cachep,
582c90e5d6SChris Mason 					     GFP_NOFS);
59db5b493aSTsutomu Itoh 		if (!cur_trans)
60db5b493aSTsutomu Itoh 			return -ENOMEM;
610f7d52f4SChris Mason 		root->fs_info->generation++;
6213c5a93eSJosef Bacik 		atomic_set(&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;
6913c5a93eSJosef Bacik 		atomic_set(&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 {
9013c5a93eSJosef Bacik 		atomic_inc(&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);
14713c5a93eSJosef Bacik 		atomic_inc(&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;
18306d5a589SJosef Bacik 	int retries = 0;
184a22285a6SYan, Zheng 	int ret;
185acce952bSliubo 
186acce952bSliubo 	if (root->fs_info->fs_state & BTRFS_SUPER_FLAG_ERROR)
187acce952bSliubo 		return ERR_PTR(-EROFS);
188a22285a6SYan, Zheng again:
189a22285a6SYan, Zheng 	h = kmem_cache_alloc(btrfs_trans_handle_cachep, GFP_NOFS);
190a22285a6SYan, Zheng 	if (!h)
191a22285a6SYan, Zheng 		return ERR_PTR(-ENOMEM);
192a22285a6SYan, Zheng 
1930af3d00bSJosef Bacik 	if (type != TRANS_JOIN_NOLOCK)
194a22285a6SYan, Zheng 		mutex_lock(&root->fs_info->trans_mutex);
195a22285a6SYan, Zheng 	if (may_wait_transaction(root, type))
19637d1aeeeSChris Mason 		wait_current_trans(root);
197a22285a6SYan, Zheng 
19879154b1bSChris Mason 	ret = join_transaction(root);
199db5b493aSTsutomu Itoh 	if (ret < 0) {
2006e8df2aeSYoshinori Sano 		kmem_cache_free(btrfs_trans_handle_cachep, h);
201db5b493aSTsutomu Itoh 		if (type != TRANS_JOIN_NOLOCK)
202db5b493aSTsutomu Itoh 			mutex_unlock(&root->fs_info->trans_mutex);
203db5b493aSTsutomu Itoh 		return ERR_PTR(ret);
204db5b493aSTsutomu Itoh 	}
2050f7d52f4SChris Mason 
206a22285a6SYan, Zheng 	cur_trans = root->fs_info->running_transaction;
20713c5a93eSJosef Bacik 	atomic_inc(&cur_trans->use_count);
2080af3d00bSJosef Bacik 	if (type != TRANS_JOIN_NOLOCK)
209a22285a6SYan, Zheng 		mutex_unlock(&root->fs_info->trans_mutex);
210a22285a6SYan, Zheng 
211a22285a6SYan, Zheng 	h->transid = cur_trans->transid;
212a22285a6SYan, Zheng 	h->transaction = cur_trans;
21379154b1bSChris Mason 	h->blocks_used = 0;
214d2fb3437SYan Zheng 	h->block_group = 0;
215a22285a6SYan, Zheng 	h->bytes_reserved = 0;
21656bec294SChris Mason 	h->delayed_ref_updates = 0;
217f0486c68SYan, Zheng 	h->block_rsv = NULL;
218b7ec40d7SChris Mason 
219a22285a6SYan, Zheng 	smp_mb();
220a22285a6SYan, Zheng 	if (cur_trans->blocked && may_wait_transaction(root, type)) {
221a22285a6SYan, Zheng 		btrfs_commit_transaction(h, root);
222a22285a6SYan, Zheng 		goto again;
223a22285a6SYan, Zheng 	}
2249ed74f2dSJosef Bacik 
225a22285a6SYan, Zheng 	if (num_items > 0) {
2268bb8ab2eSJosef Bacik 		ret = btrfs_trans_reserve_metadata(h, root, num_items);
22706d5a589SJosef Bacik 		if (ret == -EAGAIN && !retries) {
22806d5a589SJosef Bacik 			retries++;
229a22285a6SYan, Zheng 			btrfs_commit_transaction(h, root);
230a22285a6SYan, Zheng 			goto again;
23106d5a589SJosef Bacik 		} else if (ret == -EAGAIN) {
23206d5a589SJosef Bacik 			/*
23306d5a589SJosef Bacik 			 * We have already retried and got EAGAIN, so really we
23406d5a589SJosef Bacik 			 * don't have space, so set ret to -ENOSPC.
23506d5a589SJosef Bacik 			 */
23606d5a589SJosef Bacik 			ret = -ENOSPC;
237a22285a6SYan, Zheng 		}
23806d5a589SJosef Bacik 
239a22285a6SYan, Zheng 		if (ret < 0) {
240a22285a6SYan, Zheng 			btrfs_end_transaction(h, root);
241a22285a6SYan, Zheng 			return ERR_PTR(ret);
242a22285a6SYan, Zheng 		}
243a22285a6SYan, Zheng 	}
244a22285a6SYan, Zheng 
2450af3d00bSJosef Bacik 	if (type != TRANS_JOIN_NOLOCK)
246a22285a6SYan, Zheng 		mutex_lock(&root->fs_info->trans_mutex);
2475d4f98a2SYan Zheng 	record_root_in_trans(h, root);
2480af3d00bSJosef Bacik 	if (type != TRANS_JOIN_NOLOCK)
24979154b1bSChris Mason 		mutex_unlock(&root->fs_info->trans_mutex);
250a22285a6SYan, Zheng 
251a22285a6SYan, Zheng 	if (!current->journal_info && type != TRANS_USERSPACE)
252a22285a6SYan, Zheng 		current->journal_info = h;
25379154b1bSChris Mason 	return h;
25479154b1bSChris Mason }
25579154b1bSChris Mason 
256f9295749SChris Mason struct btrfs_trans_handle *btrfs_start_transaction(struct btrfs_root *root,
257a22285a6SYan, Zheng 						   int num_items)
258f9295749SChris Mason {
259a22285a6SYan, Zheng 	return start_transaction(root, num_items, TRANS_START);
260f9295749SChris Mason }
261f9295749SChris Mason struct btrfs_trans_handle *btrfs_join_transaction(struct btrfs_root *root,
262f9295749SChris Mason 						   int num_blocks)
263f9295749SChris Mason {
264a22285a6SYan, Zheng 	return start_transaction(root, 0, TRANS_JOIN);
265f9295749SChris Mason }
266f9295749SChris Mason 
2670af3d00bSJosef Bacik struct btrfs_trans_handle *btrfs_join_transaction_nolock(struct btrfs_root *root,
2680af3d00bSJosef Bacik 							  int num_blocks)
2690af3d00bSJosef Bacik {
2700af3d00bSJosef Bacik 	return start_transaction(root, 0, TRANS_JOIN_NOLOCK);
2710af3d00bSJosef Bacik }
2720af3d00bSJosef Bacik 
2739ca9ee09SSage Weil struct btrfs_trans_handle *btrfs_start_ioctl_transaction(struct btrfs_root *r,
2749ca9ee09SSage Weil 							 int num_blocks)
2759ca9ee09SSage Weil {
276a22285a6SYan, Zheng 	return start_transaction(r, 0, TRANS_USERSPACE);
2779ca9ee09SSage Weil }
2789ca9ee09SSage Weil 
279d352ac68SChris Mason /* wait for a transaction commit to be fully complete */
28089ce8a63SChris Mason static noinline int wait_for_commit(struct btrfs_root *root,
28189ce8a63SChris Mason 				    struct btrfs_transaction *commit)
28289ce8a63SChris Mason {
28389ce8a63SChris Mason 	DEFINE_WAIT(wait);
28489ce8a63SChris Mason 	mutex_lock(&root->fs_info->trans_mutex);
28589ce8a63SChris Mason 	while (!commit->commit_done) {
28689ce8a63SChris Mason 		prepare_to_wait(&commit->commit_wait, &wait,
28789ce8a63SChris Mason 				TASK_UNINTERRUPTIBLE);
28889ce8a63SChris Mason 		if (commit->commit_done)
28989ce8a63SChris Mason 			break;
29089ce8a63SChris Mason 		mutex_unlock(&root->fs_info->trans_mutex);
29189ce8a63SChris Mason 		schedule();
29289ce8a63SChris Mason 		mutex_lock(&root->fs_info->trans_mutex);
29389ce8a63SChris Mason 	}
29489ce8a63SChris Mason 	mutex_unlock(&root->fs_info->trans_mutex);
29589ce8a63SChris Mason 	finish_wait(&commit->commit_wait, &wait);
29689ce8a63SChris Mason 	return 0;
29789ce8a63SChris Mason }
29889ce8a63SChris Mason 
29946204592SSage Weil int btrfs_wait_for_commit(struct btrfs_root *root, u64 transid)
30046204592SSage Weil {
30146204592SSage Weil 	struct btrfs_transaction *cur_trans = NULL, *t;
30246204592SSage Weil 	int ret;
30346204592SSage Weil 
30446204592SSage Weil 	mutex_lock(&root->fs_info->trans_mutex);
30546204592SSage Weil 
30646204592SSage Weil 	ret = 0;
30746204592SSage Weil 	if (transid) {
30846204592SSage Weil 		if (transid <= root->fs_info->last_trans_committed)
30946204592SSage Weil 			goto out_unlock;
31046204592SSage Weil 
31146204592SSage Weil 		/* find specified transaction */
31246204592SSage Weil 		list_for_each_entry(t, &root->fs_info->trans_list, list) {
31346204592SSage Weil 			if (t->transid == transid) {
31446204592SSage Weil 				cur_trans = t;
31546204592SSage Weil 				break;
31646204592SSage Weil 			}
31746204592SSage Weil 			if (t->transid > transid)
31846204592SSage Weil 				break;
31946204592SSage Weil 		}
32046204592SSage Weil 		ret = -EINVAL;
32146204592SSage Weil 		if (!cur_trans)
32246204592SSage Weil 			goto out_unlock;  /* bad transid */
32346204592SSage Weil 	} else {
32446204592SSage Weil 		/* find newest transaction that is committing | committed */
32546204592SSage Weil 		list_for_each_entry_reverse(t, &root->fs_info->trans_list,
32646204592SSage Weil 					    list) {
32746204592SSage Weil 			if (t->in_commit) {
32846204592SSage Weil 				if (t->commit_done)
32946204592SSage Weil 					goto out_unlock;
33046204592SSage Weil 				cur_trans = t;
33146204592SSage Weil 				break;
33246204592SSage Weil 			}
33346204592SSage Weil 		}
33446204592SSage Weil 		if (!cur_trans)
33546204592SSage Weil 			goto out_unlock;  /* nothing committing|committed */
33646204592SSage Weil 	}
33746204592SSage Weil 
33813c5a93eSJosef Bacik 	atomic_inc(&cur_trans->use_count);
33946204592SSage Weil 	mutex_unlock(&root->fs_info->trans_mutex);
34046204592SSage Weil 
34146204592SSage Weil 	wait_for_commit(root, cur_trans);
34246204592SSage Weil 
34346204592SSage Weil 	mutex_lock(&root->fs_info->trans_mutex);
34446204592SSage Weil 	put_transaction(cur_trans);
34546204592SSage Weil 	ret = 0;
34646204592SSage Weil out_unlock:
34746204592SSage Weil 	mutex_unlock(&root->fs_info->trans_mutex);
34846204592SSage Weil 	return ret;
34946204592SSage Weil }
35046204592SSage Weil 
3515d4f98a2SYan Zheng #if 0
352d352ac68SChris Mason /*
353d397712bSChris Mason  * rate limit against the drop_snapshot code.  This helps to slow down new
354d397712bSChris Mason  * operations if the drop_snapshot code isn't able to keep up.
355d352ac68SChris Mason  */
35637d1aeeeSChris Mason static void throttle_on_drops(struct btrfs_root *root)
357ab78c84dSChris Mason {
358ab78c84dSChris Mason 	struct btrfs_fs_info *info = root->fs_info;
3592dd3e67bSChris Mason 	int harder_count = 0;
360ab78c84dSChris Mason 
3612dd3e67bSChris Mason harder:
362ab78c84dSChris Mason 	if (atomic_read(&info->throttles)) {
363ab78c84dSChris Mason 		DEFINE_WAIT(wait);
364ab78c84dSChris Mason 		int thr;
365ab78c84dSChris Mason 		thr = atomic_read(&info->throttle_gen);
366ab78c84dSChris Mason 
367ab78c84dSChris Mason 		do {
368ab78c84dSChris Mason 			prepare_to_wait(&info->transaction_throttle,
369ab78c84dSChris Mason 					&wait, TASK_UNINTERRUPTIBLE);
370ab78c84dSChris Mason 			if (!atomic_read(&info->throttles)) {
371ab78c84dSChris Mason 				finish_wait(&info->transaction_throttle, &wait);
372ab78c84dSChris Mason 				break;
373ab78c84dSChris Mason 			}
374ab78c84dSChris Mason 			schedule();
375ab78c84dSChris Mason 			finish_wait(&info->transaction_throttle, &wait);
376ab78c84dSChris Mason 		} while (thr == atomic_read(&info->throttle_gen));
3772dd3e67bSChris Mason 		harder_count++;
3782dd3e67bSChris Mason 
3792dd3e67bSChris Mason 		if (root->fs_info->total_ref_cache_size > 1 * 1024 * 1024 &&
3802dd3e67bSChris Mason 		    harder_count < 2)
3812dd3e67bSChris Mason 			goto harder;
3822dd3e67bSChris Mason 
3832dd3e67bSChris Mason 		if (root->fs_info->total_ref_cache_size > 5 * 1024 * 1024 &&
3842dd3e67bSChris Mason 		    harder_count < 10)
3852dd3e67bSChris Mason 			goto harder;
3862dd3e67bSChris Mason 
3872dd3e67bSChris Mason 		if (root->fs_info->total_ref_cache_size > 10 * 1024 * 1024 &&
3882dd3e67bSChris Mason 		    harder_count < 20)
3892dd3e67bSChris Mason 			goto harder;
390ab78c84dSChris Mason 	}
391ab78c84dSChris Mason }
3925d4f98a2SYan Zheng #endif
393ab78c84dSChris Mason 
39437d1aeeeSChris Mason void btrfs_throttle(struct btrfs_root *root)
39537d1aeeeSChris Mason {
39637d1aeeeSChris Mason 	mutex_lock(&root->fs_info->trans_mutex);
3979ca9ee09SSage Weil 	if (!root->fs_info->open_ioctl_trans)
39837d1aeeeSChris Mason 		wait_current_trans(root);
39937d1aeeeSChris Mason 	mutex_unlock(&root->fs_info->trans_mutex);
40037d1aeeeSChris Mason }
40137d1aeeeSChris Mason 
4028929ecfaSYan, Zheng static int should_end_transaction(struct btrfs_trans_handle *trans,
4038929ecfaSYan, Zheng 				  struct btrfs_root *root)
4048929ecfaSYan, Zheng {
4058929ecfaSYan, Zheng 	int ret;
4068929ecfaSYan, Zheng 	ret = btrfs_block_rsv_check(trans, root,
4078929ecfaSYan, Zheng 				    &root->fs_info->global_block_rsv, 0, 5);
4088929ecfaSYan, Zheng 	return ret ? 1 : 0;
4098929ecfaSYan, Zheng }
4108929ecfaSYan, Zheng 
4118929ecfaSYan, Zheng int btrfs_should_end_transaction(struct btrfs_trans_handle *trans,
4128929ecfaSYan, Zheng 				 struct btrfs_root *root)
4138929ecfaSYan, Zheng {
4148929ecfaSYan, Zheng 	struct btrfs_transaction *cur_trans = trans->transaction;
4158929ecfaSYan, Zheng 	int updates;
4168929ecfaSYan, Zheng 
4178929ecfaSYan, Zheng 	if (cur_trans->blocked || cur_trans->delayed_refs.flushing)
4188929ecfaSYan, Zheng 		return 1;
4198929ecfaSYan, Zheng 
4208929ecfaSYan, Zheng 	updates = trans->delayed_ref_updates;
4218929ecfaSYan, Zheng 	trans->delayed_ref_updates = 0;
4228929ecfaSYan, Zheng 	if (updates)
4238929ecfaSYan, Zheng 		btrfs_run_delayed_refs(trans, root, updates);
4248929ecfaSYan, Zheng 
4258929ecfaSYan, Zheng 	return should_end_transaction(trans, root);
4268929ecfaSYan, Zheng }
4278929ecfaSYan, Zheng 
42889ce8a63SChris Mason static int __btrfs_end_transaction(struct btrfs_trans_handle *trans,
4290af3d00bSJosef Bacik 			  struct btrfs_root *root, int throttle, int lock)
43079154b1bSChris Mason {
4318929ecfaSYan, Zheng 	struct btrfs_transaction *cur_trans = trans->transaction;
432ab78c84dSChris Mason 	struct btrfs_fs_info *info = root->fs_info;
433c3e69d58SChris Mason 	int count = 0;
434d6e4a428SChris Mason 
435c3e69d58SChris Mason 	while (count < 4) {
436c3e69d58SChris Mason 		unsigned long cur = trans->delayed_ref_updates;
437c3e69d58SChris Mason 		trans->delayed_ref_updates = 0;
438c3e69d58SChris Mason 		if (cur &&
439c3e69d58SChris Mason 		    trans->transaction->delayed_refs.num_heads_ready > 64) {
440c3e69d58SChris Mason 			trans->delayed_ref_updates = 0;
441b7ec40d7SChris Mason 
442b7ec40d7SChris Mason 			/*
443b7ec40d7SChris Mason 			 * do a full flush if the transaction is trying
444b7ec40d7SChris Mason 			 * to close
445b7ec40d7SChris Mason 			 */
446b7ec40d7SChris Mason 			if (trans->transaction->delayed_refs.flushing)
447b7ec40d7SChris Mason 				cur = 0;
448c3e69d58SChris Mason 			btrfs_run_delayed_refs(trans, root, cur);
449c3e69d58SChris Mason 		} else {
450c3e69d58SChris Mason 			break;
451c3e69d58SChris Mason 		}
452c3e69d58SChris Mason 		count++;
45356bec294SChris Mason 	}
45456bec294SChris Mason 
455a22285a6SYan, Zheng 	btrfs_trans_release_metadata(trans, root);
456a22285a6SYan, Zheng 
4570af3d00bSJosef Bacik 	if (lock && !root->fs_info->open_ioctl_trans &&
4588929ecfaSYan, Zheng 	    should_end_transaction(trans, root))
4598929ecfaSYan, Zheng 		trans->transaction->blocked = 1;
4608929ecfaSYan, Zheng 
4610af3d00bSJosef Bacik 	if (lock && cur_trans->blocked && !cur_trans->in_commit) {
4628929ecfaSYan, Zheng 		if (throttle)
4638929ecfaSYan, Zheng 			return btrfs_commit_transaction(trans, root);
4648929ecfaSYan, Zheng 		else
4658929ecfaSYan, Zheng 			wake_up_process(info->transaction_kthread);
4668929ecfaSYan, Zheng 	}
4678929ecfaSYan, Zheng 
4688929ecfaSYan, Zheng 	WARN_ON(cur_trans != info->running_transaction);
46913c5a93eSJosef Bacik 	WARN_ON(atomic_read(&cur_trans->num_writers) < 1);
47013c5a93eSJosef Bacik 	atomic_dec(&cur_trans->num_writers);
47189ce8a63SChris Mason 
47299d16cbcSSage Weil 	smp_mb();
47379154b1bSChris Mason 	if (waitqueue_active(&cur_trans->writer_wait))
47479154b1bSChris Mason 		wake_up(&cur_trans->writer_wait);
47579154b1bSChris Mason 	put_transaction(cur_trans);
4769ed74f2dSJosef Bacik 
4779ed74f2dSJosef Bacik 	if (current->journal_info == trans)
4789ed74f2dSJosef Bacik 		current->journal_info = NULL;
479d6025579SChris Mason 	memset(trans, 0, sizeof(*trans));
4802c90e5d6SChris Mason 	kmem_cache_free(btrfs_trans_handle_cachep, trans);
481ab78c84dSChris Mason 
48224bbcf04SYan, Zheng 	if (throttle)
48324bbcf04SYan, Zheng 		btrfs_run_delayed_iputs(root);
48424bbcf04SYan, Zheng 
48579154b1bSChris Mason 	return 0;
48679154b1bSChris Mason }
48779154b1bSChris Mason 
48889ce8a63SChris Mason int btrfs_end_transaction(struct btrfs_trans_handle *trans,
48989ce8a63SChris Mason 			  struct btrfs_root *root)
49089ce8a63SChris Mason {
4910af3d00bSJosef Bacik 	return __btrfs_end_transaction(trans, root, 0, 1);
49289ce8a63SChris Mason }
49389ce8a63SChris Mason 
49489ce8a63SChris Mason int btrfs_end_transaction_throttle(struct btrfs_trans_handle *trans,
49589ce8a63SChris Mason 				   struct btrfs_root *root)
49689ce8a63SChris Mason {
4970af3d00bSJosef Bacik 	return __btrfs_end_transaction(trans, root, 1, 1);
4980af3d00bSJosef Bacik }
4990af3d00bSJosef Bacik 
5000af3d00bSJosef Bacik int btrfs_end_transaction_nolock(struct btrfs_trans_handle *trans,
5010af3d00bSJosef Bacik 				 struct btrfs_root *root)
5020af3d00bSJosef Bacik {
5030af3d00bSJosef Bacik 	return __btrfs_end_transaction(trans, root, 0, 0);
50489ce8a63SChris Mason }
50589ce8a63SChris Mason 
506d352ac68SChris Mason /*
507d352ac68SChris Mason  * when btree blocks are allocated, they have some corresponding bits set for
508d352ac68SChris Mason  * them in one of two extent_io trees.  This is used to make sure all of
509690587d1SChris Mason  * those extents are sent to disk but does not wait on them
510d352ac68SChris Mason  */
511690587d1SChris Mason int btrfs_write_marked_extents(struct btrfs_root *root,
5128cef4e16SYan, Zheng 			       struct extent_io_tree *dirty_pages, int mark)
51379154b1bSChris Mason {
5147c4452b9SChris Mason 	int ret;
515777e6bd7SChris Mason 	int err = 0;
5167c4452b9SChris Mason 	int werr = 0;
5177c4452b9SChris Mason 	struct page *page;
5187c4452b9SChris Mason 	struct inode *btree_inode = root->fs_info->btree_inode;
519777e6bd7SChris Mason 	u64 start = 0;
5205f39d397SChris Mason 	u64 end;
5215f39d397SChris Mason 	unsigned long index;
5227c4452b9SChris Mason 
5237c4452b9SChris Mason 	while (1) {
524777e6bd7SChris Mason 		ret = find_first_extent_bit(dirty_pages, start, &start, &end,
5258cef4e16SYan, Zheng 					    mark);
5265f39d397SChris Mason 		if (ret)
5277c4452b9SChris Mason 			break;
5285f39d397SChris Mason 		while (start <= end) {
529777e6bd7SChris Mason 			cond_resched();
530777e6bd7SChris Mason 
5315f39d397SChris Mason 			index = start >> PAGE_CACHE_SHIFT;
53235ebb934SChris Mason 			start = (u64)(index + 1) << PAGE_CACHE_SHIFT;
5334bef0848SChris Mason 			page = find_get_page(btree_inode->i_mapping, index);
5347c4452b9SChris Mason 			if (!page)
5357c4452b9SChris Mason 				continue;
5364bef0848SChris Mason 
5374bef0848SChris Mason 			btree_lock_page_hook(page);
5384bef0848SChris Mason 			if (!page->mapping) {
5394bef0848SChris Mason 				unlock_page(page);
5404bef0848SChris Mason 				page_cache_release(page);
5414bef0848SChris Mason 				continue;
5424bef0848SChris Mason 			}
5434bef0848SChris Mason 
5446702ed49SChris Mason 			if (PageWriteback(page)) {
5456702ed49SChris Mason 				if (PageDirty(page))
5466702ed49SChris Mason 					wait_on_page_writeback(page);
5476702ed49SChris Mason 				else {
5486702ed49SChris Mason 					unlock_page(page);
5496702ed49SChris Mason 					page_cache_release(page);
5506702ed49SChris Mason 					continue;
5516702ed49SChris Mason 				}
5526702ed49SChris Mason 			}
5537c4452b9SChris Mason 			err = write_one_page(page, 0);
5547c4452b9SChris Mason 			if (err)
5557c4452b9SChris Mason 				werr = err;
5567c4452b9SChris Mason 			page_cache_release(page);
5577c4452b9SChris Mason 		}
5587c4452b9SChris Mason 	}
559690587d1SChris Mason 	if (err)
560690587d1SChris Mason 		werr = err;
561690587d1SChris Mason 	return werr;
562690587d1SChris Mason }
563690587d1SChris Mason 
564690587d1SChris Mason /*
565690587d1SChris Mason  * when btree blocks are allocated, they have some corresponding bits set for
566690587d1SChris Mason  * them in one of two extent_io trees.  This is used to make sure all of
567690587d1SChris Mason  * those extents are on disk for transaction or log commit.  We wait
568690587d1SChris Mason  * on all the pages and clear them from the dirty pages state tree
569690587d1SChris Mason  */
570690587d1SChris Mason int btrfs_wait_marked_extents(struct btrfs_root *root,
5718cef4e16SYan, Zheng 			      struct extent_io_tree *dirty_pages, int mark)
572690587d1SChris Mason {
573690587d1SChris Mason 	int ret;
574690587d1SChris Mason 	int err = 0;
575690587d1SChris Mason 	int werr = 0;
576690587d1SChris Mason 	struct page *page;
577690587d1SChris Mason 	struct inode *btree_inode = root->fs_info->btree_inode;
578690587d1SChris Mason 	u64 start = 0;
579690587d1SChris Mason 	u64 end;
580690587d1SChris Mason 	unsigned long index;
581690587d1SChris Mason 
582777e6bd7SChris Mason 	while (1) {
5838cef4e16SYan, Zheng 		ret = find_first_extent_bit(dirty_pages, start, &start, &end,
5848cef4e16SYan, Zheng 					    mark);
585777e6bd7SChris Mason 		if (ret)
586777e6bd7SChris Mason 			break;
587777e6bd7SChris Mason 
5888cef4e16SYan, Zheng 		clear_extent_bits(dirty_pages, start, end, mark, GFP_NOFS);
589777e6bd7SChris Mason 		while (start <= end) {
590777e6bd7SChris Mason 			index = start >> PAGE_CACHE_SHIFT;
591777e6bd7SChris Mason 			start = (u64)(index + 1) << PAGE_CACHE_SHIFT;
592777e6bd7SChris Mason 			page = find_get_page(btree_inode->i_mapping, index);
593777e6bd7SChris Mason 			if (!page)
594777e6bd7SChris Mason 				continue;
595777e6bd7SChris Mason 			if (PageDirty(page)) {
5964bef0848SChris Mason 				btree_lock_page_hook(page);
5974bef0848SChris Mason 				wait_on_page_writeback(page);
598777e6bd7SChris Mason 				err = write_one_page(page, 0);
599777e6bd7SChris Mason 				if (err)
600777e6bd7SChris Mason 					werr = err;
601777e6bd7SChris Mason 			}
602777e6bd7SChris Mason 			wait_on_page_writeback(page);
603777e6bd7SChris Mason 			page_cache_release(page);
604777e6bd7SChris Mason 			cond_resched();
605777e6bd7SChris Mason 		}
606777e6bd7SChris Mason 	}
6077c4452b9SChris Mason 	if (err)
6087c4452b9SChris Mason 		werr = err;
6097c4452b9SChris Mason 	return werr;
61079154b1bSChris Mason }
61179154b1bSChris Mason 
612690587d1SChris Mason /*
613690587d1SChris Mason  * when btree blocks are allocated, they have some corresponding bits set for
614690587d1SChris Mason  * them in one of two extent_io trees.  This is used to make sure all of
615690587d1SChris Mason  * those extents are on disk for transaction or log commit
616690587d1SChris Mason  */
617690587d1SChris Mason int btrfs_write_and_wait_marked_extents(struct btrfs_root *root,
6188cef4e16SYan, Zheng 				struct extent_io_tree *dirty_pages, int mark)
619690587d1SChris Mason {
620690587d1SChris Mason 	int ret;
621690587d1SChris Mason 	int ret2;
622690587d1SChris Mason 
6238cef4e16SYan, Zheng 	ret = btrfs_write_marked_extents(root, dirty_pages, mark);
6248cef4e16SYan, Zheng 	ret2 = btrfs_wait_marked_extents(root, dirty_pages, mark);
625690587d1SChris Mason 	return ret || ret2;
626690587d1SChris Mason }
627690587d1SChris Mason 
628d0c803c4SChris Mason int btrfs_write_and_wait_transaction(struct btrfs_trans_handle *trans,
629d0c803c4SChris Mason 				     struct btrfs_root *root)
630d0c803c4SChris Mason {
631d0c803c4SChris Mason 	if (!trans || !trans->transaction) {
632d0c803c4SChris Mason 		struct inode *btree_inode;
633d0c803c4SChris Mason 		btree_inode = root->fs_info->btree_inode;
634d0c803c4SChris Mason 		return filemap_write_and_wait(btree_inode->i_mapping);
635d0c803c4SChris Mason 	}
636d0c803c4SChris Mason 	return btrfs_write_and_wait_marked_extents(root,
6378cef4e16SYan, Zheng 					   &trans->transaction->dirty_pages,
6388cef4e16SYan, Zheng 					   EXTENT_DIRTY);
639d0c803c4SChris Mason }
640d0c803c4SChris Mason 
641d352ac68SChris Mason /*
642d352ac68SChris Mason  * this is used to update the root pointer in the tree of tree roots.
643d352ac68SChris Mason  *
644d352ac68SChris Mason  * But, in the case of the extent allocation tree, updating the root
645d352ac68SChris Mason  * pointer may allocate blocks which may change the root of the extent
646d352ac68SChris Mason  * allocation tree.
647d352ac68SChris Mason  *
648d352ac68SChris Mason  * So, this loops and repeats and makes sure the cowonly root didn't
649d352ac68SChris Mason  * change while the root pointer was being updated in the metadata.
650d352ac68SChris Mason  */
6510b86a832SChris Mason static int update_cowonly_root(struct btrfs_trans_handle *trans,
65279154b1bSChris Mason 			       struct btrfs_root *root)
65379154b1bSChris Mason {
65479154b1bSChris Mason 	int ret;
6550b86a832SChris Mason 	u64 old_root_bytenr;
65686b9f2ecSYan, Zheng 	u64 old_root_used;
6570b86a832SChris Mason 	struct btrfs_root *tree_root = root->fs_info->tree_root;
65879154b1bSChris Mason 
65986b9f2ecSYan, Zheng 	old_root_used = btrfs_root_used(&root->root_item);
6600b86a832SChris Mason 	btrfs_write_dirty_block_groups(trans, root);
66156bec294SChris Mason 
66279154b1bSChris Mason 	while (1) {
6630b86a832SChris Mason 		old_root_bytenr = btrfs_root_bytenr(&root->root_item);
66486b9f2ecSYan, Zheng 		if (old_root_bytenr == root->node->start &&
66586b9f2ecSYan, Zheng 		    old_root_used == btrfs_root_used(&root->root_item))
66679154b1bSChris Mason 			break;
66787ef2bb4SChris Mason 
6685d4f98a2SYan Zheng 		btrfs_set_root_node(&root->root_item, root->node);
66979154b1bSChris Mason 		ret = btrfs_update_root(trans, tree_root,
6700b86a832SChris Mason 					&root->root_key,
6710b86a832SChris Mason 					&root->root_item);
67279154b1bSChris Mason 		BUG_ON(ret);
67356bec294SChris Mason 
67486b9f2ecSYan, Zheng 		old_root_used = btrfs_root_used(&root->root_item);
6754a8c9a62SYan Zheng 		ret = btrfs_write_dirty_block_groups(trans, root);
67656bec294SChris Mason 		BUG_ON(ret);
6770b86a832SChris Mason 	}
678276e680dSYan Zheng 
679276e680dSYan Zheng 	if (root != root->fs_info->extent_root)
680817d52f8SJosef Bacik 		switch_commit_root(root);
681276e680dSYan Zheng 
6820b86a832SChris Mason 	return 0;
6830b86a832SChris Mason }
6840b86a832SChris Mason 
685d352ac68SChris Mason /*
686d352ac68SChris Mason  * update all the cowonly tree roots on disk
687d352ac68SChris Mason  */
6885d4f98a2SYan Zheng static noinline int commit_cowonly_roots(struct btrfs_trans_handle *trans,
6890b86a832SChris Mason 					 struct btrfs_root *root)
6900b86a832SChris Mason {
6910b86a832SChris Mason 	struct btrfs_fs_info *fs_info = root->fs_info;
6920b86a832SChris Mason 	struct list_head *next;
69384234f3aSYan Zheng 	struct extent_buffer *eb;
69456bec294SChris Mason 	int ret;
69584234f3aSYan Zheng 
69656bec294SChris Mason 	ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
69756bec294SChris Mason 	BUG_ON(ret);
69887ef2bb4SChris Mason 
69984234f3aSYan Zheng 	eb = btrfs_lock_root_node(fs_info->tree_root);
7009fa8cfe7SChris Mason 	btrfs_cow_block(trans, fs_info->tree_root, eb, NULL, 0, &eb);
70184234f3aSYan Zheng 	btrfs_tree_unlock(eb);
70284234f3aSYan Zheng 	free_extent_buffer(eb);
7030b86a832SChris Mason 
70456bec294SChris Mason 	ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
70556bec294SChris Mason 	BUG_ON(ret);
70687ef2bb4SChris Mason 
7070b86a832SChris Mason 	while (!list_empty(&fs_info->dirty_cowonly_roots)) {
7080b86a832SChris Mason 		next = fs_info->dirty_cowonly_roots.next;
7090b86a832SChris Mason 		list_del_init(next);
7100b86a832SChris Mason 		root = list_entry(next, struct btrfs_root, dirty_list);
71187ef2bb4SChris Mason 
7120b86a832SChris Mason 		update_cowonly_root(trans, root);
71379154b1bSChris Mason 	}
714276e680dSYan Zheng 
715276e680dSYan Zheng 	down_write(&fs_info->extent_commit_sem);
716276e680dSYan Zheng 	switch_commit_root(fs_info->extent_root);
717276e680dSYan Zheng 	up_write(&fs_info->extent_commit_sem);
718276e680dSYan Zheng 
71979154b1bSChris Mason 	return 0;
72079154b1bSChris Mason }
72179154b1bSChris Mason 
722d352ac68SChris Mason /*
723d352ac68SChris Mason  * dead roots are old snapshots that need to be deleted.  This allocates
724d352ac68SChris Mason  * a dirty root struct and adds it into the list of dead roots that need to
725d352ac68SChris Mason  * be deleted
726d352ac68SChris Mason  */
7275d4f98a2SYan Zheng int btrfs_add_dead_root(struct btrfs_root *root)
7285eda7b5eSChris Mason {
729b48652c1SYan Zheng 	mutex_lock(&root->fs_info->trans_mutex);
7305d4f98a2SYan Zheng 	list_add(&root->root_list, &root->fs_info->dead_roots);
731b48652c1SYan Zheng 	mutex_unlock(&root->fs_info->trans_mutex);
7325eda7b5eSChris Mason 	return 0;
7335eda7b5eSChris Mason }
7345eda7b5eSChris Mason 
735d352ac68SChris Mason /*
7365d4f98a2SYan Zheng  * update all the cowonly tree roots on disk
737d352ac68SChris Mason  */
7385d4f98a2SYan Zheng static noinline int commit_fs_roots(struct btrfs_trans_handle *trans,
7395d4f98a2SYan Zheng 				    struct btrfs_root *root)
7400f7d52f4SChris Mason {
7410f7d52f4SChris Mason 	struct btrfs_root *gang[8];
7425d4f98a2SYan Zheng 	struct btrfs_fs_info *fs_info = root->fs_info;
7430f7d52f4SChris Mason 	int i;
7440f7d52f4SChris Mason 	int ret;
74554aa1f4dSChris Mason 	int err = 0;
74654aa1f4dSChris Mason 
7470f7d52f4SChris Mason 	while (1) {
7485d4f98a2SYan Zheng 		ret = radix_tree_gang_lookup_tag(&fs_info->fs_roots_radix,
7495d4f98a2SYan Zheng 						 (void **)gang, 0,
7500f7d52f4SChris Mason 						 ARRAY_SIZE(gang),
7510f7d52f4SChris Mason 						 BTRFS_ROOT_TRANS_TAG);
7520f7d52f4SChris Mason 		if (ret == 0)
7530f7d52f4SChris Mason 			break;
7540f7d52f4SChris Mason 		for (i = 0; i < ret; i++) {
7550f7d52f4SChris Mason 			root = gang[i];
7565d4f98a2SYan Zheng 			radix_tree_tag_clear(&fs_info->fs_roots_radix,
7572619ba1fSChris Mason 					(unsigned long)root->root_key.objectid,
7580f7d52f4SChris Mason 					BTRFS_ROOT_TRANS_TAG);
75931153d81SYan Zheng 
760e02119d5SChris Mason 			btrfs_free_log(trans, root);
7615d4f98a2SYan Zheng 			btrfs_update_reloc_root(trans, root);
762d68fc57bSYan, Zheng 			btrfs_orphan_commit_root(trans, root);
763e02119d5SChris Mason 
764978d910dSYan Zheng 			if (root->commit_root != root->node) {
765581bb050SLi Zefan 				mutex_lock(&root->fs_commit_mutex);
766817d52f8SJosef Bacik 				switch_commit_root(root);
767581bb050SLi Zefan 				btrfs_unpin_free_ino(root);
768581bb050SLi Zefan 				mutex_unlock(&root->fs_commit_mutex);
769581bb050SLi Zefan 
770978d910dSYan Zheng 				btrfs_set_root_node(&root->root_item,
771978d910dSYan Zheng 						    root->node);
772978d910dSYan Zheng 			}
77331153d81SYan Zheng 
7745d4f98a2SYan Zheng 			err = btrfs_update_root(trans, fs_info->tree_root,
7750f7d52f4SChris Mason 						&root->root_key,
7760f7d52f4SChris Mason 						&root->root_item);
77754aa1f4dSChris Mason 			if (err)
77854aa1f4dSChris Mason 				break;
7790f7d52f4SChris Mason 		}
7809f3a7427SChris Mason 	}
78154aa1f4dSChris Mason 	return err;
7820f7d52f4SChris Mason }
7830f7d52f4SChris Mason 
784d352ac68SChris Mason /*
785d352ac68SChris Mason  * defrag a given btree.  If cacheonly == 1, this won't read from the disk,
786d352ac68SChris Mason  * otherwise every leaf in the btree is read and defragged.
787d352ac68SChris Mason  */
788e9d0b13bSChris Mason int btrfs_defrag_root(struct btrfs_root *root, int cacheonly)
789e9d0b13bSChris Mason {
790e9d0b13bSChris Mason 	struct btrfs_fs_info *info = root->fs_info;
791e9d0b13bSChris Mason 	struct btrfs_trans_handle *trans;
7928929ecfaSYan, Zheng 	int ret;
793d3c2fdcfSChris Mason 	unsigned long nr;
794e9d0b13bSChris Mason 
7958929ecfaSYan, Zheng 	if (xchg(&root->defrag_running, 1))
796e9d0b13bSChris Mason 		return 0;
7978929ecfaSYan, Zheng 
7986b80053dSChris Mason 	while (1) {
7998929ecfaSYan, Zheng 		trans = btrfs_start_transaction(root, 0);
8008929ecfaSYan, Zheng 		if (IS_ERR(trans))
8018929ecfaSYan, Zheng 			return PTR_ERR(trans);
8028929ecfaSYan, Zheng 
803e9d0b13bSChris Mason 		ret = btrfs_defrag_leaves(trans, root, cacheonly);
8048929ecfaSYan, Zheng 
805d3c2fdcfSChris Mason 		nr = trans->blocks_used;
806e9d0b13bSChris Mason 		btrfs_end_transaction(trans, root);
807d3c2fdcfSChris Mason 		btrfs_btree_balance_dirty(info->tree_root, nr);
808e9d0b13bSChris Mason 		cond_resched();
809e9d0b13bSChris Mason 
8103f157a2fSChris Mason 		if (root->fs_info->closing || ret != -EAGAIN)
811e9d0b13bSChris Mason 			break;
812e9d0b13bSChris Mason 	}
813e9d0b13bSChris Mason 	root->defrag_running = 0;
8148929ecfaSYan, Zheng 	return ret;
815e9d0b13bSChris Mason }
816e9d0b13bSChris Mason 
8172c47e605SYan Zheng #if 0
818d352ac68SChris Mason /*
819b7ec40d7SChris Mason  * when dropping snapshots, we generate a ton of delayed refs, and it makes
820b7ec40d7SChris Mason  * sense not to join the transaction while it is trying to flush the current
821b7ec40d7SChris Mason  * queue of delayed refs out.
822b7ec40d7SChris Mason  *
823b7ec40d7SChris Mason  * This is used by the drop snapshot code only
824b7ec40d7SChris Mason  */
825b7ec40d7SChris Mason static noinline int wait_transaction_pre_flush(struct btrfs_fs_info *info)
826b7ec40d7SChris Mason {
827b7ec40d7SChris Mason 	DEFINE_WAIT(wait);
828b7ec40d7SChris Mason 
829b7ec40d7SChris Mason 	mutex_lock(&info->trans_mutex);
830b7ec40d7SChris Mason 	while (info->running_transaction &&
831b7ec40d7SChris Mason 	       info->running_transaction->delayed_refs.flushing) {
832b7ec40d7SChris Mason 		prepare_to_wait(&info->transaction_wait, &wait,
833b7ec40d7SChris Mason 				TASK_UNINTERRUPTIBLE);
834b7ec40d7SChris Mason 		mutex_unlock(&info->trans_mutex);
83559bc5c75SChris Mason 
836b7ec40d7SChris Mason 		schedule();
83759bc5c75SChris Mason 
838b7ec40d7SChris Mason 		mutex_lock(&info->trans_mutex);
839b7ec40d7SChris Mason 		finish_wait(&info->transaction_wait, &wait);
840b7ec40d7SChris Mason 	}
841b7ec40d7SChris Mason 	mutex_unlock(&info->trans_mutex);
842b7ec40d7SChris Mason 	return 0;
843b7ec40d7SChris Mason }
844b7ec40d7SChris Mason 
845b7ec40d7SChris Mason /*
846d352ac68SChris Mason  * Given a list of roots that need to be deleted, call btrfs_drop_snapshot on
847d352ac68SChris Mason  * all of them
848d352ac68SChris Mason  */
8495d4f98a2SYan Zheng int btrfs_drop_dead_root(struct btrfs_root *root)
8500f7d52f4SChris Mason {
8510f7d52f4SChris Mason 	struct btrfs_trans_handle *trans;
8525d4f98a2SYan Zheng 	struct btrfs_root *tree_root = root->fs_info->tree_root;
853d3c2fdcfSChris Mason 	unsigned long nr;
8545d4f98a2SYan Zheng 	int ret;
85558176a96SJosef Bacik 
8569f3a7427SChris Mason 	while (1) {
857b7ec40d7SChris Mason 		/*
858b7ec40d7SChris Mason 		 * we don't want to jump in and create a bunch of
859b7ec40d7SChris Mason 		 * delayed refs if the transaction is starting to close
860b7ec40d7SChris Mason 		 */
861b7ec40d7SChris Mason 		wait_transaction_pre_flush(tree_root->fs_info);
8620f7d52f4SChris Mason 		trans = btrfs_start_transaction(tree_root, 1);
863b7ec40d7SChris Mason 
864b7ec40d7SChris Mason 		/*
865b7ec40d7SChris Mason 		 * we've joined a transaction, make sure it isn't
866b7ec40d7SChris Mason 		 * closing right now
867b7ec40d7SChris Mason 		 */
868b7ec40d7SChris Mason 		if (trans->transaction->delayed_refs.flushing) {
869b7ec40d7SChris Mason 			btrfs_end_transaction(trans, tree_root);
870b7ec40d7SChris Mason 			continue;
871b7ec40d7SChris Mason 		}
872b7ec40d7SChris Mason 
8735d4f98a2SYan Zheng 		ret = btrfs_drop_snapshot(trans, root);
874d397712bSChris Mason 		if (ret != -EAGAIN)
8759f3a7427SChris Mason 			break;
87658176a96SJosef Bacik 
8775d4f98a2SYan Zheng 		ret = btrfs_update_root(trans, tree_root,
8785d4f98a2SYan Zheng 					&root->root_key,
8795d4f98a2SYan Zheng 					&root->root_item);
8805d4f98a2SYan Zheng 		if (ret)
88154aa1f4dSChris Mason 			break;
882bcc63abbSYan 
883d3c2fdcfSChris Mason 		nr = trans->blocks_used;
8840f7d52f4SChris Mason 		ret = btrfs_end_transaction(trans, tree_root);
8850f7d52f4SChris Mason 		BUG_ON(ret);
8865eda7b5eSChris Mason 
887d3c2fdcfSChris Mason 		btrfs_btree_balance_dirty(tree_root, nr);
8884dc11904SChris Mason 		cond_resched();
8890f7d52f4SChris Mason 	}
8905d4f98a2SYan Zheng 	BUG_ON(ret);
8915d4f98a2SYan Zheng 
8925d4f98a2SYan Zheng 	ret = btrfs_del_root(trans, tree_root, &root->root_key);
8935d4f98a2SYan Zheng 	BUG_ON(ret);
8945d4f98a2SYan Zheng 
8955d4f98a2SYan Zheng 	nr = trans->blocks_used;
8965d4f98a2SYan Zheng 	ret = btrfs_end_transaction(trans, tree_root);
8975d4f98a2SYan Zheng 	BUG_ON(ret);
8985d4f98a2SYan Zheng 
8995d4f98a2SYan Zheng 	free_extent_buffer(root->node);
9005d4f98a2SYan Zheng 	free_extent_buffer(root->commit_root);
9015d4f98a2SYan Zheng 	kfree(root);
9025d4f98a2SYan Zheng 
9035d4f98a2SYan Zheng 	btrfs_btree_balance_dirty(tree_root, nr);
90454aa1f4dSChris Mason 	return ret;
9050f7d52f4SChris Mason }
9062c47e605SYan Zheng #endif
9070f7d52f4SChris Mason 
908d352ac68SChris Mason /*
909d352ac68SChris Mason  * new snapshots need to be created at a very specific time in the
910d352ac68SChris Mason  * transaction commit.  This does the actual creation
911d352ac68SChris Mason  */
91280b6794dSChris Mason static noinline int create_pending_snapshot(struct btrfs_trans_handle *trans,
9133063d29fSChris Mason 				   struct btrfs_fs_info *fs_info,
9143063d29fSChris Mason 				   struct btrfs_pending_snapshot *pending)
9153063d29fSChris Mason {
9163063d29fSChris Mason 	struct btrfs_key key;
91780b6794dSChris Mason 	struct btrfs_root_item *new_root_item;
9183063d29fSChris Mason 	struct btrfs_root *tree_root = fs_info->tree_root;
9193063d29fSChris Mason 	struct btrfs_root *root = pending->root;
9206bdb72deSSage Weil 	struct btrfs_root *parent_root;
9216bdb72deSSage Weil 	struct inode *parent_inode;
9226a912213SJosef Bacik 	struct dentry *parent;
923a22285a6SYan, Zheng 	struct dentry *dentry;
9243063d29fSChris Mason 	struct extent_buffer *tmp;
925925baeddSChris Mason 	struct extent_buffer *old;
9263063d29fSChris Mason 	int ret;
927d68fc57bSYan, Zheng 	u64 to_reserve = 0;
9286bdb72deSSage Weil 	u64 index = 0;
929a22285a6SYan, Zheng 	u64 objectid;
930b83cc969SLi Zefan 	u64 root_flags;
9313063d29fSChris Mason 
93280b6794dSChris Mason 	new_root_item = kmalloc(sizeof(*new_root_item), GFP_NOFS);
93380b6794dSChris Mason 	if (!new_root_item) {
934a22285a6SYan, Zheng 		pending->error = -ENOMEM;
93580b6794dSChris Mason 		goto fail;
93680b6794dSChris Mason 	}
937a22285a6SYan, Zheng 
938581bb050SLi Zefan 	ret = btrfs_find_free_objectid(tree_root, &objectid);
939a22285a6SYan, Zheng 	if (ret) {
940a22285a6SYan, Zheng 		pending->error = ret;
9413063d29fSChris Mason 		goto fail;
942a22285a6SYan, Zheng 	}
9433063d29fSChris Mason 
9443fd0a558SYan, Zheng 	btrfs_reloc_pre_snapshot(trans, pending, &to_reserve);
945d68fc57bSYan, Zheng 	btrfs_orphan_pre_snapshot(trans, pending, &to_reserve);
946d68fc57bSYan, Zheng 
947d68fc57bSYan, Zheng 	if (to_reserve > 0) {
948d68fc57bSYan, Zheng 		ret = btrfs_block_rsv_add(trans, root, &pending->block_rsv,
9498bb8ab2eSJosef Bacik 					  to_reserve);
950d68fc57bSYan, Zheng 		if (ret) {
951d68fc57bSYan, Zheng 			pending->error = ret;
952d68fc57bSYan, Zheng 			goto fail;
953d68fc57bSYan, Zheng 		}
954d68fc57bSYan, Zheng 	}
955d68fc57bSYan, Zheng 
9563063d29fSChris Mason 	key.objectid = objectid;
957a22285a6SYan, Zheng 	key.offset = (u64)-1;
958a22285a6SYan, Zheng 	key.type = BTRFS_ROOT_ITEM_KEY;
9593063d29fSChris Mason 
960a22285a6SYan, Zheng 	trans->block_rsv = &pending->block_rsv;
9616bdb72deSSage Weil 
962a22285a6SYan, Zheng 	dentry = pending->dentry;
9636a912213SJosef Bacik 	parent = dget_parent(dentry);
9646a912213SJosef Bacik 	parent_inode = parent->d_inode;
965a22285a6SYan, Zheng 	parent_root = BTRFS_I(parent_inode)->root;
9666bdb72deSSage Weil 	record_root_in_trans(trans, parent_root);
967a22285a6SYan, Zheng 
9686bdb72deSSage Weil 	/*
9696bdb72deSSage Weil 	 * insert the directory item
9706bdb72deSSage Weil 	 */
9716bdb72deSSage Weil 	ret = btrfs_set_inode_index(parent_inode, &index);
9726bdb72deSSage Weil 	BUG_ON(ret);
9736bdb72deSSage Weil 	ret = btrfs_insert_dir_item(trans, parent_root,
974a22285a6SYan, Zheng 				dentry->d_name.name, dentry->d_name.len,
975a22285a6SYan, Zheng 				parent_inode->i_ino, &key,
976a22285a6SYan, Zheng 				BTRFS_FT_DIR, index);
9776bdb72deSSage Weil 	BUG_ON(ret);
9786bdb72deSSage Weil 
979a22285a6SYan, Zheng 	btrfs_i_size_write(parent_inode, parent_inode->i_size +
980a22285a6SYan, Zheng 					 dentry->d_name.len * 2);
9816bdb72deSSage Weil 	ret = btrfs_update_inode(trans, parent_root, parent_inode);
9826bdb72deSSage Weil 	BUG_ON(ret);
9836bdb72deSSage Weil 
9846bdb72deSSage Weil 	record_root_in_trans(trans, root);
9856bdb72deSSage Weil 	btrfs_set_root_last_snapshot(&root->root_item, trans->transid);
9866bdb72deSSage Weil 	memcpy(new_root_item, &root->root_item, sizeof(*new_root_item));
98708fe4db1SLi Zefan 	btrfs_check_and_init_root_item(new_root_item);
9886bdb72deSSage Weil 
989b83cc969SLi Zefan 	root_flags = btrfs_root_flags(new_root_item);
990b83cc969SLi Zefan 	if (pending->readonly)
991b83cc969SLi Zefan 		root_flags |= BTRFS_ROOT_SUBVOL_RDONLY;
992b83cc969SLi Zefan 	else
993b83cc969SLi Zefan 		root_flags &= ~BTRFS_ROOT_SUBVOL_RDONLY;
994b83cc969SLi Zefan 	btrfs_set_root_flags(new_root_item, root_flags);
995b83cc969SLi Zefan 
996925baeddSChris Mason 	old = btrfs_lock_root_node(root);
9979fa8cfe7SChris Mason 	btrfs_cow_block(trans, root, old, NULL, 0, &old);
9985d4f98a2SYan Zheng 	btrfs_set_lock_blocking(old);
9993063d29fSChris Mason 
1000925baeddSChris Mason 	btrfs_copy_root(trans, root, old, &tmp, objectid);
1001925baeddSChris Mason 	btrfs_tree_unlock(old);
1002925baeddSChris Mason 	free_extent_buffer(old);
10033063d29fSChris Mason 
10045d4f98a2SYan Zheng 	btrfs_set_root_node(new_root_item, tmp);
1005a22285a6SYan, Zheng 	/* record when the snapshot was created in key.offset */
1006a22285a6SYan, Zheng 	key.offset = trans->transid;
1007a22285a6SYan, Zheng 	ret = btrfs_insert_root(trans, tree_root, &key, new_root_item);
1008925baeddSChris Mason 	btrfs_tree_unlock(tmp);
10093063d29fSChris Mason 	free_extent_buffer(tmp);
10100660b5afSChris Mason 	BUG_ON(ret);
10110660b5afSChris Mason 
1012a22285a6SYan, Zheng 	/*
1013a22285a6SYan, Zheng 	 * insert root back/forward references
1014a22285a6SYan, Zheng 	 */
1015a22285a6SYan, Zheng 	ret = btrfs_add_root_ref(trans, tree_root, objectid,
1016a22285a6SYan, Zheng 				 parent_root->root_key.objectid,
1017a22285a6SYan, Zheng 				 parent_inode->i_ino, index,
1018a22285a6SYan, Zheng 				 dentry->d_name.name, dentry->d_name.len);
1019a22285a6SYan, Zheng 	BUG_ON(ret);
10206a912213SJosef Bacik 	dput(parent);
1021a22285a6SYan, Zheng 
1022a22285a6SYan, Zheng 	key.offset = (u64)-1;
1023a22285a6SYan, Zheng 	pending->snap = btrfs_read_fs_root_no_name(root->fs_info, &key);
1024a22285a6SYan, Zheng 	BUG_ON(IS_ERR(pending->snap));
1025d68fc57bSYan, Zheng 
10263fd0a558SYan, Zheng 	btrfs_reloc_post_snapshot(trans, pending);
1027d68fc57bSYan, Zheng 	btrfs_orphan_post_snapshot(trans, pending);
10283063d29fSChris Mason fail:
10296bdb72deSSage Weil 	kfree(new_root_item);
1030a22285a6SYan, Zheng 	btrfs_block_rsv_release(root, &pending->block_rsv, (u64)-1);
1031a22285a6SYan, Zheng 	return 0;
10323063d29fSChris Mason }
10333063d29fSChris Mason 
1034d352ac68SChris Mason /*
1035d352ac68SChris Mason  * create all the snapshots we've scheduled for creation
1036d352ac68SChris Mason  */
103780b6794dSChris Mason static noinline int create_pending_snapshots(struct btrfs_trans_handle *trans,
10383063d29fSChris Mason 					     struct btrfs_fs_info *fs_info)
10393063d29fSChris Mason {
10403063d29fSChris Mason 	struct btrfs_pending_snapshot *pending;
10413063d29fSChris Mason 	struct list_head *head = &trans->transaction->pending_snapshots;
10423de4586cSChris Mason 	int ret;
10433de4586cSChris Mason 
1044c6e30871SQinghuang Feng 	list_for_each_entry(pending, head, list) {
10453de4586cSChris Mason 		ret = create_pending_snapshot(trans, fs_info, pending);
10463de4586cSChris Mason 		BUG_ON(ret);
10473de4586cSChris Mason 	}
10483de4586cSChris Mason 	return 0;
10493de4586cSChris Mason }
10503de4586cSChris Mason 
10515d4f98a2SYan Zheng static void update_super_roots(struct btrfs_root *root)
10525d4f98a2SYan Zheng {
10535d4f98a2SYan Zheng 	struct btrfs_root_item *root_item;
10545d4f98a2SYan Zheng 	struct btrfs_super_block *super;
10555d4f98a2SYan Zheng 
10565d4f98a2SYan Zheng 	super = &root->fs_info->super_copy;
10575d4f98a2SYan Zheng 
10585d4f98a2SYan Zheng 	root_item = &root->fs_info->chunk_root->root_item;
10595d4f98a2SYan Zheng 	super->chunk_root = root_item->bytenr;
10605d4f98a2SYan Zheng 	super->chunk_root_generation = root_item->generation;
10615d4f98a2SYan Zheng 	super->chunk_root_level = root_item->level;
10625d4f98a2SYan Zheng 
10635d4f98a2SYan Zheng 	root_item = &root->fs_info->tree_root->root_item;
10645d4f98a2SYan Zheng 	super->root = root_item->bytenr;
10655d4f98a2SYan Zheng 	super->generation = root_item->generation;
10665d4f98a2SYan Zheng 	super->root_level = root_item->level;
10670af3d00bSJosef Bacik 	if (super->cache_generation != 0 || btrfs_test_opt(root, SPACE_CACHE))
10680af3d00bSJosef Bacik 		super->cache_generation = root_item->generation;
10695d4f98a2SYan Zheng }
10705d4f98a2SYan Zheng 
1071f36f3042SChris Mason int btrfs_transaction_in_commit(struct btrfs_fs_info *info)
1072f36f3042SChris Mason {
1073f36f3042SChris Mason 	int ret = 0;
1074f36f3042SChris Mason 	spin_lock(&info->new_trans_lock);
1075f36f3042SChris Mason 	if (info->running_transaction)
1076f36f3042SChris Mason 		ret = info->running_transaction->in_commit;
1077f36f3042SChris Mason 	spin_unlock(&info->new_trans_lock);
1078f36f3042SChris Mason 	return ret;
1079f36f3042SChris Mason }
1080f36f3042SChris Mason 
10818929ecfaSYan, Zheng int btrfs_transaction_blocked(struct btrfs_fs_info *info)
10828929ecfaSYan, Zheng {
10838929ecfaSYan, Zheng 	int ret = 0;
10848929ecfaSYan, Zheng 	spin_lock(&info->new_trans_lock);
10858929ecfaSYan, Zheng 	if (info->running_transaction)
10868929ecfaSYan, Zheng 		ret = info->running_transaction->blocked;
10878929ecfaSYan, Zheng 	spin_unlock(&info->new_trans_lock);
10888929ecfaSYan, Zheng 	return ret;
10898929ecfaSYan, Zheng }
10908929ecfaSYan, Zheng 
1091bb9c12c9SSage Weil /*
1092bb9c12c9SSage Weil  * wait for the current transaction commit to start and block subsequent
1093bb9c12c9SSage Weil  * transaction joins
1094bb9c12c9SSage Weil  */
1095bb9c12c9SSage Weil static void wait_current_trans_commit_start(struct btrfs_root *root,
1096bb9c12c9SSage Weil 					    struct btrfs_transaction *trans)
1097bb9c12c9SSage Weil {
1098bb9c12c9SSage Weil 	DEFINE_WAIT(wait);
1099bb9c12c9SSage Weil 
1100bb9c12c9SSage Weil 	if (trans->in_commit)
1101bb9c12c9SSage Weil 		return;
1102bb9c12c9SSage Weil 
1103bb9c12c9SSage Weil 	while (1) {
1104bb9c12c9SSage Weil 		prepare_to_wait(&root->fs_info->transaction_blocked_wait, &wait,
1105bb9c12c9SSage Weil 				TASK_UNINTERRUPTIBLE);
1106bb9c12c9SSage Weil 		if (trans->in_commit) {
1107bb9c12c9SSage Weil 			finish_wait(&root->fs_info->transaction_blocked_wait,
1108bb9c12c9SSage Weil 				    &wait);
1109bb9c12c9SSage Weil 			break;
1110bb9c12c9SSage Weil 		}
1111bb9c12c9SSage Weil 		mutex_unlock(&root->fs_info->trans_mutex);
1112bb9c12c9SSage Weil 		schedule();
1113bb9c12c9SSage Weil 		mutex_lock(&root->fs_info->trans_mutex);
1114bb9c12c9SSage Weil 		finish_wait(&root->fs_info->transaction_blocked_wait, &wait);
1115bb9c12c9SSage Weil 	}
1116bb9c12c9SSage Weil }
1117bb9c12c9SSage Weil 
1118bb9c12c9SSage Weil /*
1119bb9c12c9SSage Weil  * wait for the current transaction to start and then become unblocked.
1120bb9c12c9SSage Weil  * caller holds ref.
1121bb9c12c9SSage Weil  */
1122bb9c12c9SSage Weil static void wait_current_trans_commit_start_and_unblock(struct btrfs_root *root,
1123bb9c12c9SSage Weil 					 struct btrfs_transaction *trans)
1124bb9c12c9SSage Weil {
1125bb9c12c9SSage Weil 	DEFINE_WAIT(wait);
1126bb9c12c9SSage Weil 
1127bb9c12c9SSage Weil 	if (trans->commit_done || (trans->in_commit && !trans->blocked))
1128bb9c12c9SSage Weil 		return;
1129bb9c12c9SSage Weil 
1130bb9c12c9SSage Weil 	while (1) {
1131bb9c12c9SSage Weil 		prepare_to_wait(&root->fs_info->transaction_wait, &wait,
1132bb9c12c9SSage Weil 				TASK_UNINTERRUPTIBLE);
1133bb9c12c9SSage Weil 		if (trans->commit_done ||
1134bb9c12c9SSage Weil 		    (trans->in_commit && !trans->blocked)) {
1135bb9c12c9SSage Weil 			finish_wait(&root->fs_info->transaction_wait,
1136bb9c12c9SSage Weil 				    &wait);
1137bb9c12c9SSage Weil 			break;
1138bb9c12c9SSage Weil 		}
1139bb9c12c9SSage Weil 		mutex_unlock(&root->fs_info->trans_mutex);
1140bb9c12c9SSage Weil 		schedule();
1141bb9c12c9SSage Weil 		mutex_lock(&root->fs_info->trans_mutex);
1142bb9c12c9SSage Weil 		finish_wait(&root->fs_info->transaction_wait,
1143bb9c12c9SSage Weil 			    &wait);
1144bb9c12c9SSage Weil 	}
1145bb9c12c9SSage Weil }
1146bb9c12c9SSage Weil 
1147bb9c12c9SSage Weil /*
1148bb9c12c9SSage Weil  * commit transactions asynchronously. once btrfs_commit_transaction_async
1149bb9c12c9SSage Weil  * returns, any subsequent transaction will not be allowed to join.
1150bb9c12c9SSage Weil  */
1151bb9c12c9SSage Weil struct btrfs_async_commit {
1152bb9c12c9SSage Weil 	struct btrfs_trans_handle *newtrans;
1153bb9c12c9SSage Weil 	struct btrfs_root *root;
1154bb9c12c9SSage Weil 	struct delayed_work work;
1155bb9c12c9SSage Weil };
1156bb9c12c9SSage Weil 
1157bb9c12c9SSage Weil static void do_async_commit(struct work_struct *work)
1158bb9c12c9SSage Weil {
1159bb9c12c9SSage Weil 	struct btrfs_async_commit *ac =
1160bb9c12c9SSage Weil 		container_of(work, struct btrfs_async_commit, work.work);
1161bb9c12c9SSage Weil 
1162bb9c12c9SSage Weil 	btrfs_commit_transaction(ac->newtrans, ac->root);
1163bb9c12c9SSage Weil 	kfree(ac);
1164bb9c12c9SSage Weil }
1165bb9c12c9SSage Weil 
1166bb9c12c9SSage Weil int btrfs_commit_transaction_async(struct btrfs_trans_handle *trans,
1167bb9c12c9SSage Weil 				   struct btrfs_root *root,
1168bb9c12c9SSage Weil 				   int wait_for_unblock)
1169bb9c12c9SSage Weil {
1170bb9c12c9SSage Weil 	struct btrfs_async_commit *ac;
1171bb9c12c9SSage Weil 	struct btrfs_transaction *cur_trans;
1172bb9c12c9SSage Weil 
1173bb9c12c9SSage Weil 	ac = kmalloc(sizeof(*ac), GFP_NOFS);
1174db5b493aSTsutomu Itoh 	if (!ac)
1175db5b493aSTsutomu Itoh 		return -ENOMEM;
1176bb9c12c9SSage Weil 
1177bb9c12c9SSage Weil 	INIT_DELAYED_WORK(&ac->work, do_async_commit);
1178bb9c12c9SSage Weil 	ac->root = root;
1179bb9c12c9SSage Weil 	ac->newtrans = btrfs_join_transaction(root, 0);
11803612b495STsutomu Itoh 	if (IS_ERR(ac->newtrans)) {
11813612b495STsutomu Itoh 		int err = PTR_ERR(ac->newtrans);
11823612b495STsutomu Itoh 		kfree(ac);
11833612b495STsutomu Itoh 		return err;
11843612b495STsutomu Itoh 	}
1185bb9c12c9SSage Weil 
1186bb9c12c9SSage Weil 	/* take transaction reference */
1187bb9c12c9SSage Weil 	mutex_lock(&root->fs_info->trans_mutex);
1188bb9c12c9SSage Weil 	cur_trans = trans->transaction;
118913c5a93eSJosef Bacik 	atomic_inc(&cur_trans->use_count);
1190bb9c12c9SSage Weil 	mutex_unlock(&root->fs_info->trans_mutex);
1191bb9c12c9SSage Weil 
1192bb9c12c9SSage Weil 	btrfs_end_transaction(trans, root);
1193bb9c12c9SSage Weil 	schedule_delayed_work(&ac->work, 0);
1194bb9c12c9SSage Weil 
1195bb9c12c9SSage Weil 	/* wait for transaction to start and unblock */
1196bb9c12c9SSage Weil 	mutex_lock(&root->fs_info->trans_mutex);
1197bb9c12c9SSage Weil 	if (wait_for_unblock)
1198bb9c12c9SSage Weil 		wait_current_trans_commit_start_and_unblock(root, cur_trans);
1199bb9c12c9SSage Weil 	else
1200bb9c12c9SSage Weil 		wait_current_trans_commit_start(root, cur_trans);
1201bb9c12c9SSage Weil 	put_transaction(cur_trans);
1202bb9c12c9SSage Weil 	mutex_unlock(&root->fs_info->trans_mutex);
1203bb9c12c9SSage Weil 
1204bb9c12c9SSage Weil 	return 0;
1205bb9c12c9SSage Weil }
1206bb9c12c9SSage Weil 
1207bb9c12c9SSage Weil /*
1208bb9c12c9SSage Weil  * btrfs_transaction state sequence:
1209bb9c12c9SSage Weil  *    in_commit = 0, blocked = 0  (initial)
1210bb9c12c9SSage Weil  *    in_commit = 1, blocked = 1
1211bb9c12c9SSage Weil  *    blocked = 0
1212bb9c12c9SSage Weil  *    commit_done = 1
1213bb9c12c9SSage Weil  */
121479154b1bSChris Mason int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
121579154b1bSChris Mason 			     struct btrfs_root *root)
121679154b1bSChris Mason {
121715ee9bc7SJosef Bacik 	unsigned long joined = 0;
121879154b1bSChris Mason 	struct btrfs_transaction *cur_trans;
12198fd17795SChris Mason 	struct btrfs_transaction *prev_trans = NULL;
122079154b1bSChris Mason 	DEFINE_WAIT(wait);
122115ee9bc7SJosef Bacik 	int ret;
122289573b9cSChris Mason 	int should_grow = 0;
122389573b9cSChris Mason 	unsigned long now = get_seconds();
1224dccae999SSage Weil 	int flush_on_commit = btrfs_test_opt(root, FLUSHONCOMMIT);
122579154b1bSChris Mason 
12265a3f23d5SChris Mason 	btrfs_run_ordered_operations(root, 0);
12275a3f23d5SChris Mason 
122856bec294SChris Mason 	/* make a pass through all the delayed refs we have so far
122956bec294SChris Mason 	 * any runnings procs may add more while we are here
123056bec294SChris Mason 	 */
123156bec294SChris Mason 	ret = btrfs_run_delayed_refs(trans, root, 0);
123256bec294SChris Mason 	BUG_ON(ret);
123356bec294SChris Mason 
1234a22285a6SYan, Zheng 	btrfs_trans_release_metadata(trans, root);
1235a22285a6SYan, Zheng 
1236b7ec40d7SChris Mason 	cur_trans = trans->transaction;
123756bec294SChris Mason 	/*
123856bec294SChris Mason 	 * set the flushing flag so procs in this transaction have to
123956bec294SChris Mason 	 * start sending their work down.
124056bec294SChris Mason 	 */
1241b7ec40d7SChris Mason 	cur_trans->delayed_refs.flushing = 1;
124256bec294SChris Mason 
1243c3e69d58SChris Mason 	ret = btrfs_run_delayed_refs(trans, root, 0);
124456bec294SChris Mason 	BUG_ON(ret);
124556bec294SChris Mason 
124679154b1bSChris Mason 	mutex_lock(&root->fs_info->trans_mutex);
1247b7ec40d7SChris Mason 	if (cur_trans->in_commit) {
124813c5a93eSJosef Bacik 		atomic_inc(&cur_trans->use_count);
1249ccd467d6SChris Mason 		mutex_unlock(&root->fs_info->trans_mutex);
125079154b1bSChris Mason 		btrfs_end_transaction(trans, root);
1251ccd467d6SChris Mason 
125279154b1bSChris Mason 		ret = wait_for_commit(root, cur_trans);
125379154b1bSChris Mason 		BUG_ON(ret);
125415ee9bc7SJosef Bacik 
125515ee9bc7SJosef Bacik 		mutex_lock(&root->fs_info->trans_mutex);
125679154b1bSChris Mason 		put_transaction(cur_trans);
125715ee9bc7SJosef Bacik 		mutex_unlock(&root->fs_info->trans_mutex);
125815ee9bc7SJosef Bacik 
125979154b1bSChris Mason 		return 0;
126079154b1bSChris Mason 	}
12614313b399SChris Mason 
12622c90e5d6SChris Mason 	trans->transaction->in_commit = 1;
1263f9295749SChris Mason 	trans->transaction->blocked = 1;
1264bb9c12c9SSage Weil 	wake_up(&root->fs_info->transaction_blocked_wait);
1265bb9c12c9SSage Weil 
1266ccd467d6SChris Mason 	if (cur_trans->list.prev != &root->fs_info->trans_list) {
1267ccd467d6SChris Mason 		prev_trans = list_entry(cur_trans->list.prev,
1268ccd467d6SChris Mason 					struct btrfs_transaction, list);
1269ccd467d6SChris Mason 		if (!prev_trans->commit_done) {
127013c5a93eSJosef Bacik 			atomic_inc(&prev_trans->use_count);
1271ccd467d6SChris Mason 			mutex_unlock(&root->fs_info->trans_mutex);
1272ccd467d6SChris Mason 
1273ccd467d6SChris Mason 			wait_for_commit(root, prev_trans);
1274ccd467d6SChris Mason 
1275ccd467d6SChris Mason 			mutex_lock(&root->fs_info->trans_mutex);
127615ee9bc7SJosef Bacik 			put_transaction(prev_trans);
1277ccd467d6SChris Mason 		}
1278ccd467d6SChris Mason 	}
127915ee9bc7SJosef Bacik 
128089573b9cSChris Mason 	if (now < cur_trans->start_time || now - cur_trans->start_time < 1)
128189573b9cSChris Mason 		should_grow = 1;
128289573b9cSChris Mason 
128315ee9bc7SJosef Bacik 	do {
12847ea394f1SYan Zheng 		int snap_pending = 0;
128515ee9bc7SJosef Bacik 		joined = cur_trans->num_joined;
12867ea394f1SYan Zheng 		if (!list_empty(&trans->transaction->pending_snapshots))
12877ea394f1SYan Zheng 			snap_pending = 1;
12887ea394f1SYan Zheng 
12892c90e5d6SChris Mason 		WARN_ON(cur_trans != trans->transaction);
129079154b1bSChris Mason 		mutex_unlock(&root->fs_info->trans_mutex);
129115ee9bc7SJosef Bacik 
12920bdb1db2SSage Weil 		if (flush_on_commit || snap_pending) {
129324bbcf04SYan, Zheng 			btrfs_start_delalloc_inodes(root, 1);
129424bbcf04SYan, Zheng 			ret = btrfs_wait_ordered_extents(root, 0, 1);
1295ebecd3d9SSage Weil 			BUG_ON(ret);
12967ea394f1SYan Zheng 		}
12977ea394f1SYan Zheng 
12985a3f23d5SChris Mason 		/*
12995a3f23d5SChris Mason 		 * rename don't use btrfs_join_transaction, so, once we
13005a3f23d5SChris Mason 		 * set the transaction to blocked above, we aren't going
13015a3f23d5SChris Mason 		 * to get any new ordered operations.  We can safely run
13025a3f23d5SChris Mason 		 * it here and no for sure that nothing new will be added
13035a3f23d5SChris Mason 		 * to the list
13045a3f23d5SChris Mason 		 */
13055a3f23d5SChris Mason 		btrfs_run_ordered_operations(root, 1);
13065a3f23d5SChris Mason 
1307ed3b3d31SChris Mason 		prepare_to_wait(&cur_trans->writer_wait, &wait,
1308ed3b3d31SChris Mason 				TASK_UNINTERRUPTIBLE);
1309ed3b3d31SChris Mason 
131089573b9cSChris Mason 		smp_mb();
131113c5a93eSJosef Bacik 		if (atomic_read(&cur_trans->num_writers) > 1)
131299d16cbcSSage Weil 			schedule_timeout(MAX_SCHEDULE_TIMEOUT);
131399d16cbcSSage Weil 		else if (should_grow)
131499d16cbcSSage Weil 			schedule_timeout(1);
131515ee9bc7SJosef Bacik 
131679154b1bSChris Mason 		mutex_lock(&root->fs_info->trans_mutex);
131715ee9bc7SJosef Bacik 		finish_wait(&cur_trans->writer_wait, &wait);
131813c5a93eSJosef Bacik 	} while (atomic_read(&cur_trans->num_writers) > 1 ||
131989573b9cSChris Mason 		 (should_grow && cur_trans->num_joined != joined));
132015ee9bc7SJosef Bacik 
13213063d29fSChris Mason 	ret = create_pending_snapshots(trans, root->fs_info);
13223063d29fSChris Mason 	BUG_ON(ret);
13233063d29fSChris Mason 
132456bec294SChris Mason 	ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
132556bec294SChris Mason 	BUG_ON(ret);
132656bec294SChris Mason 
13272c90e5d6SChris Mason 	WARN_ON(cur_trans != trans->transaction);
1328dc17ff8fSChris Mason 
1329e02119d5SChris Mason 	/* btrfs_commit_tree_roots is responsible for getting the
1330e02119d5SChris Mason 	 * various roots consistent with each other.  Every pointer
1331e02119d5SChris Mason 	 * in the tree of tree roots has to point to the most up to date
1332e02119d5SChris Mason 	 * root for every subvolume and other tree.  So, we have to keep
1333e02119d5SChris Mason 	 * the tree logging code from jumping in and changing any
1334e02119d5SChris Mason 	 * of the trees.
1335e02119d5SChris Mason 	 *
1336e02119d5SChris Mason 	 * At this point in the commit, there can't be any tree-log
1337e02119d5SChris Mason 	 * writers, but a little lower down we drop the trans mutex
1338e02119d5SChris Mason 	 * and let new people in.  By holding the tree_log_mutex
1339e02119d5SChris Mason 	 * from now until after the super is written, we avoid races
1340e02119d5SChris Mason 	 * with the tree-log code.
1341e02119d5SChris Mason 	 */
1342e02119d5SChris Mason 	mutex_lock(&root->fs_info->tree_log_mutex);
13431a40e23bSZheng Yan 
13445d4f98a2SYan Zheng 	ret = commit_fs_roots(trans, root);
134554aa1f4dSChris Mason 	BUG_ON(ret);
134654aa1f4dSChris Mason 
13475d4f98a2SYan Zheng 	/* commit_fs_roots gets rid of all the tree log roots, it is now
1348e02119d5SChris Mason 	 * safe to free the root of tree log roots
1349e02119d5SChris Mason 	 */
1350e02119d5SChris Mason 	btrfs_free_log_root_tree(trans, root->fs_info);
1351e02119d5SChris Mason 
13525d4f98a2SYan Zheng 	ret = commit_cowonly_roots(trans, root);
135379154b1bSChris Mason 	BUG_ON(ret);
135454aa1f4dSChris Mason 
135511833d66SYan Zheng 	btrfs_prepare_extent_commit(trans, root);
135611833d66SYan Zheng 
135778fae27eSChris Mason 	cur_trans = root->fs_info->running_transaction;
1358cee36a03SChris Mason 	spin_lock(&root->fs_info->new_trans_lock);
135978fae27eSChris Mason 	root->fs_info->running_transaction = NULL;
1360cee36a03SChris Mason 	spin_unlock(&root->fs_info->new_trans_lock);
13615f39d397SChris Mason 
13625d4f98a2SYan Zheng 	btrfs_set_root_node(&root->fs_info->tree_root->root_item,
13635d4f98a2SYan Zheng 			    root->fs_info->tree_root->node);
1364817d52f8SJosef Bacik 	switch_commit_root(root->fs_info->tree_root);
13655d4f98a2SYan Zheng 
13665d4f98a2SYan Zheng 	btrfs_set_root_node(&root->fs_info->chunk_root->root_item,
13675d4f98a2SYan Zheng 			    root->fs_info->chunk_root->node);
1368817d52f8SJosef Bacik 	switch_commit_root(root->fs_info->chunk_root);
13695d4f98a2SYan Zheng 
13705d4f98a2SYan Zheng 	update_super_roots(root);
1371e02119d5SChris Mason 
1372e02119d5SChris Mason 	if (!root->fs_info->log_root_recovering) {
1373e02119d5SChris Mason 		btrfs_set_super_log_root(&root->fs_info->super_copy, 0);
1374e02119d5SChris Mason 		btrfs_set_super_log_root_level(&root->fs_info->super_copy, 0);
1375e02119d5SChris Mason 	}
1376e02119d5SChris Mason 
1377a061fc8dSChris Mason 	memcpy(&root->fs_info->super_for_commit, &root->fs_info->super_copy,
13784b52dff6SChris Mason 	       sizeof(root->fs_info->super_copy));
1379ccd467d6SChris Mason 
1380f9295749SChris Mason 	trans->transaction->blocked = 0;
1381b7ec40d7SChris Mason 
1382f9295749SChris Mason 	wake_up(&root->fs_info->transaction_wait);
1383e6dcd2dcSChris Mason 
138478fae27eSChris Mason 	mutex_unlock(&root->fs_info->trans_mutex);
138579154b1bSChris Mason 	ret = btrfs_write_and_wait_transaction(trans, root);
138679154b1bSChris Mason 	BUG_ON(ret);
1387a512bbf8SYan Zheng 	write_ctree_super(trans, root, 0);
13884313b399SChris Mason 
1389e02119d5SChris Mason 	/*
1390e02119d5SChris Mason 	 * the super is written, we can safely allow the tree-loggers
1391e02119d5SChris Mason 	 * to go about their business
1392e02119d5SChris Mason 	 */
1393e02119d5SChris Mason 	mutex_unlock(&root->fs_info->tree_log_mutex);
1394e02119d5SChris Mason 
139511833d66SYan Zheng 	btrfs_finish_extent_commit(trans, root);
13964313b399SChris Mason 
13971a40e23bSZheng Yan 	mutex_lock(&root->fs_info->trans_mutex);
13981a40e23bSZheng Yan 
13992c90e5d6SChris Mason 	cur_trans->commit_done = 1;
1400b7ec40d7SChris Mason 
140115ee9bc7SJosef Bacik 	root->fs_info->last_trans_committed = cur_trans->transid;
1402817d52f8SJosef Bacik 
14032c90e5d6SChris Mason 	wake_up(&cur_trans->commit_wait);
14043de4586cSChris Mason 
140513c5a93eSJosef Bacik 	list_del_init(&cur_trans->list);
140679154b1bSChris Mason 	put_transaction(cur_trans);
140778fae27eSChris Mason 	put_transaction(cur_trans);
140858176a96SJosef Bacik 
14091abe9b8aSliubo 	trace_btrfs_transaction_commit(root);
14101abe9b8aSliubo 
141178fae27eSChris Mason 	mutex_unlock(&root->fs_info->trans_mutex);
14123de4586cSChris Mason 
14139ed74f2dSJosef Bacik 	if (current->journal_info == trans)
14149ed74f2dSJosef Bacik 		current->journal_info = NULL;
14159ed74f2dSJosef Bacik 
14162c90e5d6SChris Mason 	kmem_cache_free(btrfs_trans_handle_cachep, trans);
141724bbcf04SYan, Zheng 
141824bbcf04SYan, Zheng 	if (current != root->fs_info->transaction_kthread)
141924bbcf04SYan, Zheng 		btrfs_run_delayed_iputs(root);
142024bbcf04SYan, Zheng 
142179154b1bSChris Mason 	return ret;
142279154b1bSChris Mason }
142379154b1bSChris Mason 
1424d352ac68SChris Mason /*
1425d352ac68SChris Mason  * interface function to delete all the snapshots we have scheduled for deletion
1426d352ac68SChris Mason  */
1427e9d0b13bSChris Mason int btrfs_clean_old_snapshots(struct btrfs_root *root)
1428e9d0b13bSChris Mason {
14295d4f98a2SYan Zheng 	LIST_HEAD(list);
14305d4f98a2SYan Zheng 	struct btrfs_fs_info *fs_info = root->fs_info;
1431e9d0b13bSChris Mason 
14325d4f98a2SYan Zheng 	mutex_lock(&fs_info->trans_mutex);
14335d4f98a2SYan Zheng 	list_splice_init(&fs_info->dead_roots, &list);
14345d4f98a2SYan Zheng 	mutex_unlock(&fs_info->trans_mutex);
14355d4f98a2SYan Zheng 
14365d4f98a2SYan Zheng 	while (!list_empty(&list)) {
14375d4f98a2SYan Zheng 		root = list_entry(list.next, struct btrfs_root, root_list);
143876dda93cSYan, Zheng 		list_del(&root->root_list);
143976dda93cSYan, Zheng 
144076dda93cSYan, Zheng 		if (btrfs_header_backref_rev(root->node) <
144176dda93cSYan, Zheng 		    BTRFS_MIXED_BACKREF_REV)
14423fd0a558SYan, Zheng 			btrfs_drop_snapshot(root, NULL, 0);
144376dda93cSYan, Zheng 		else
14443fd0a558SYan, Zheng 			btrfs_drop_snapshot(root, NULL, 1);
1445e9d0b13bSChris Mason 	}
1446e9d0b13bSChris Mason 	return 0;
1447e9d0b13bSChris Mason }
1448