xref: /openbmc/linux/fs/btrfs/transaction.c (revision a4abeea4)
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 {
3513c5a93eSJosef Bacik 	WARN_ON(atomic_read(&transaction->use_count) == 0);
3613c5a93eSJosef Bacik 	if (atomic_dec_and_test(&transaction->use_count)) {
37a4abeea4SJosef Bacik 		BUG_ON(!list_empty(&transaction->list));
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  */
52a4abeea4SJosef Bacik static noinline int join_transaction(struct btrfs_root *root, int nofail)
5379154b1bSChris Mason {
5479154b1bSChris Mason 	struct btrfs_transaction *cur_trans;
55a4abeea4SJosef Bacik 
56a4abeea4SJosef Bacik 	spin_lock(&root->fs_info->trans_lock);
57a4abeea4SJosef Bacik 	if (root->fs_info->trans_no_join) {
58a4abeea4SJosef Bacik 		if (!nofail) {
59a4abeea4SJosef Bacik 			spin_unlock(&root->fs_info->trans_lock);
60a4abeea4SJosef Bacik 			return -EBUSY;
61a4abeea4SJosef Bacik 		}
62a4abeea4SJosef Bacik 	}
63a4abeea4SJosef Bacik 
6479154b1bSChris Mason 	cur_trans = root->fs_info->running_transaction;
65a4abeea4SJosef Bacik 	if (cur_trans) {
66a4abeea4SJosef Bacik 		atomic_inc(&cur_trans->use_count);
67a4abeea4SJosef Bacik 		atomic_inc(&cur_trans->num_writers);
68a4abeea4SJosef Bacik 		cur_trans->num_joined++;
69a4abeea4SJosef Bacik 		spin_unlock(&root->fs_info->trans_lock);
70a4abeea4SJosef Bacik 		return 0;
71a4abeea4SJosef Bacik 	}
72a4abeea4SJosef Bacik 	spin_unlock(&root->fs_info->trans_lock);
73a4abeea4SJosef Bacik 
74a4abeea4SJosef Bacik 	cur_trans = kmem_cache_alloc(btrfs_transaction_cachep, GFP_NOFS);
75db5b493aSTsutomu Itoh 	if (!cur_trans)
76db5b493aSTsutomu Itoh 		return -ENOMEM;
77a4abeea4SJosef Bacik 	spin_lock(&root->fs_info->trans_lock);
78a4abeea4SJosef Bacik 	if (root->fs_info->running_transaction) {
79a4abeea4SJosef Bacik 		kmem_cache_free(btrfs_transaction_cachep, cur_trans);
80a4abeea4SJosef Bacik 		cur_trans = root->fs_info->running_transaction;
81a4abeea4SJosef Bacik 		atomic_inc(&cur_trans->use_count);
82a4abeea4SJosef Bacik 		atomic_inc(&cur_trans->num_writers);
83a4abeea4SJosef Bacik 		cur_trans->num_joined++;
84a4abeea4SJosef Bacik 		spin_unlock(&root->fs_info->trans_lock);
85a4abeea4SJosef Bacik 		return 0;
86a4abeea4SJosef Bacik 	}
8713c5a93eSJosef Bacik 	atomic_set(&cur_trans->num_writers, 1);
8815ee9bc7SJosef Bacik 	cur_trans->num_joined = 0;
8979154b1bSChris Mason 	init_waitqueue_head(&cur_trans->writer_wait);
9079154b1bSChris Mason 	init_waitqueue_head(&cur_trans->commit_wait);
9179154b1bSChris Mason 	cur_trans->in_commit = 0;
92f9295749SChris Mason 	cur_trans->blocked = 0;
93a4abeea4SJosef Bacik 	/*
94a4abeea4SJosef Bacik 	 * One for this trans handle, one so it will live on until we
95a4abeea4SJosef Bacik 	 * commit the transaction.
96a4abeea4SJosef Bacik 	 */
97a4abeea4SJosef Bacik 	atomic_set(&cur_trans->use_count, 2);
9879154b1bSChris Mason 	cur_trans->commit_done = 0;
9908607c1bSChris Mason 	cur_trans->start_time = get_seconds();
10056bec294SChris Mason 
1016bef4d31SEric Paris 	cur_trans->delayed_refs.root = RB_ROOT;
10256bec294SChris Mason 	cur_trans->delayed_refs.num_entries = 0;
103c3e69d58SChris Mason 	cur_trans->delayed_refs.num_heads_ready = 0;
104c3e69d58SChris Mason 	cur_trans->delayed_refs.num_heads = 0;
10556bec294SChris Mason 	cur_trans->delayed_refs.flushing = 0;
106c3e69d58SChris Mason 	cur_trans->delayed_refs.run_delayed_start = 0;
107a4abeea4SJosef Bacik 	spin_lock_init(&cur_trans->commit_lock);
10856bec294SChris Mason 	spin_lock_init(&cur_trans->delayed_refs.lock);
10956bec294SChris Mason 
1103063d29fSChris Mason 	INIT_LIST_HEAD(&cur_trans->pending_snapshots);
1118fd17795SChris Mason 	list_add_tail(&cur_trans->list, &root->fs_info->trans_list);
112d1310b2eSChris Mason 	extent_io_tree_init(&cur_trans->dirty_pages,
1135f39d397SChris Mason 			     root->fs_info->btree_inode->i_mapping,
1145f39d397SChris Mason 			     GFP_NOFS);
115a4abeea4SJosef Bacik 	root->fs_info->generation++;
116a4abeea4SJosef Bacik 	cur_trans->transid = root->fs_info->generation;
11748ec2cf8SChris Mason 	root->fs_info->running_transaction = cur_trans;
118a4abeea4SJosef Bacik 	spin_unlock(&root->fs_info->trans_lock);
11915ee9bc7SJosef Bacik 
12079154b1bSChris Mason 	return 0;
12179154b1bSChris Mason }
12279154b1bSChris Mason 
123d352ac68SChris Mason /*
124d397712bSChris Mason  * this does all the record keeping required to make sure that a reference
125d397712bSChris Mason  * counted root is properly recorded in a given transaction.  This is required
126d397712bSChris Mason  * to make sure the old root from before we joined the transaction is deleted
127d397712bSChris Mason  * when the transaction commits
128d352ac68SChris Mason  */
129a4abeea4SJosef Bacik int btrfs_record_root_in_trans(struct btrfs_trans_handle *trans,
1305d4f98a2SYan Zheng 			       struct btrfs_root *root)
1316702ed49SChris Mason {
1325d4f98a2SYan Zheng 	if (root->ref_cows && root->last_trans < trans->transid) {
1336702ed49SChris Mason 		WARN_ON(root == root->fs_info->extent_root);
1345d4f98a2SYan Zheng 		WARN_ON(root->commit_root != root->node);
1355d4f98a2SYan Zheng 
136a4abeea4SJosef Bacik 		spin_lock(&root->fs_info->fs_roots_radix_lock);
137a4abeea4SJosef Bacik 		if (root->last_trans == trans->transid) {
138a4abeea4SJosef Bacik 			spin_unlock(&root->fs_info->fs_roots_radix_lock);
139a4abeea4SJosef Bacik 			return 0;
140a4abeea4SJosef Bacik 		}
141a4abeea4SJosef Bacik 		root->last_trans = trans->transid;
1426702ed49SChris Mason 		radix_tree_tag_set(&root->fs_info->fs_roots_radix,
1436702ed49SChris Mason 			   (unsigned long)root->root_key.objectid,
1446702ed49SChris Mason 			   BTRFS_ROOT_TRANS_TAG);
145a4abeea4SJosef Bacik 		spin_unlock(&root->fs_info->fs_roots_radix_lock);
1465d4f98a2SYan Zheng 		btrfs_init_reloc_root(trans, root);
1476702ed49SChris Mason 	}
1485d4f98a2SYan Zheng 	return 0;
1496702ed49SChris Mason }
1505d4f98a2SYan Zheng 
151d352ac68SChris Mason /* wait for commit against the current transaction to become unblocked
152d352ac68SChris Mason  * when this is done, it is safe to start a new transaction, but the current
153d352ac68SChris Mason  * transaction might not be fully on disk.
154d352ac68SChris Mason  */
15537d1aeeeSChris Mason static void wait_current_trans(struct btrfs_root *root)
15679154b1bSChris Mason {
157f9295749SChris Mason 	struct btrfs_transaction *cur_trans;
15879154b1bSChris Mason 
159a4abeea4SJosef Bacik 	spin_lock(&root->fs_info->trans_lock);
160f9295749SChris Mason 	cur_trans = root->fs_info->running_transaction;
16137d1aeeeSChris Mason 	if (cur_trans && cur_trans->blocked) {
162f9295749SChris Mason 		DEFINE_WAIT(wait);
16313c5a93eSJosef Bacik 		atomic_inc(&cur_trans->use_count);
164a4abeea4SJosef Bacik 		spin_unlock(&root->fs_info->trans_lock);
165f9295749SChris Mason 		while (1) {
166f9295749SChris Mason 			prepare_to_wait(&root->fs_info->transaction_wait, &wait,
167f9295749SChris Mason 					TASK_UNINTERRUPTIBLE);
168471fa17dSZhao Lei 			if (!cur_trans->blocked)
169471fa17dSZhao Lei 				break;
170f9295749SChris Mason 			schedule();
171f9295749SChris Mason 		}
172471fa17dSZhao Lei 		finish_wait(&root->fs_info->transaction_wait, &wait);
173f9295749SChris Mason 		put_transaction(cur_trans);
174a4abeea4SJosef Bacik 	} else {
175a4abeea4SJosef Bacik 		spin_unlock(&root->fs_info->trans_lock);
176f9295749SChris Mason 	}
17737d1aeeeSChris Mason }
17837d1aeeeSChris Mason 
179249ac1e5SJosef Bacik enum btrfs_trans_type {
180249ac1e5SJosef Bacik 	TRANS_START,
181249ac1e5SJosef Bacik 	TRANS_JOIN,
182249ac1e5SJosef Bacik 	TRANS_USERSPACE,
1830af3d00bSJosef Bacik 	TRANS_JOIN_NOLOCK,
184249ac1e5SJosef Bacik };
185249ac1e5SJosef Bacik 
186a22285a6SYan, Zheng static int may_wait_transaction(struct btrfs_root *root, int type)
18737d1aeeeSChris Mason {
188a4abeea4SJosef Bacik 	if (root->fs_info->log_root_recovering)
189a4abeea4SJosef Bacik 		return 0;
190a4abeea4SJosef Bacik 
191a4abeea4SJosef Bacik 	if (type == TRANS_USERSPACE)
192a22285a6SYan, Zheng 		return 1;
193a4abeea4SJosef Bacik 
194a4abeea4SJosef Bacik 	if (type == TRANS_START &&
195a4abeea4SJosef Bacik 	    !atomic_read(&root->fs_info->open_ioctl_trans))
196a4abeea4SJosef Bacik 		return 1;
197a4abeea4SJosef Bacik 
198a22285a6SYan, Zheng 	return 0;
199a22285a6SYan, Zheng }
200a22285a6SYan, Zheng 
201a22285a6SYan, Zheng static struct btrfs_trans_handle *start_transaction(struct btrfs_root *root,
202a22285a6SYan, Zheng 						    u64 num_items, int type)
203a22285a6SYan, Zheng {
204a22285a6SYan, Zheng 	struct btrfs_trans_handle *h;
205a22285a6SYan, Zheng 	struct btrfs_transaction *cur_trans;
20606d5a589SJosef Bacik 	int retries = 0;
207a22285a6SYan, Zheng 	int ret;
208acce952bSliubo 
209acce952bSliubo 	if (root->fs_info->fs_state & BTRFS_SUPER_FLAG_ERROR)
210acce952bSliubo 		return ERR_PTR(-EROFS);
2112a1eb461SJosef Bacik 
2122a1eb461SJosef Bacik 	if (current->journal_info) {
2132a1eb461SJosef Bacik 		WARN_ON(type != TRANS_JOIN && type != TRANS_JOIN_NOLOCK);
2142a1eb461SJosef Bacik 		h = current->journal_info;
2152a1eb461SJosef Bacik 		h->use_count++;
2162a1eb461SJosef Bacik 		h->orig_rsv = h->block_rsv;
2172a1eb461SJosef Bacik 		h->block_rsv = NULL;
2182a1eb461SJosef Bacik 		goto got_it;
2192a1eb461SJosef Bacik 	}
220a22285a6SYan, Zheng again:
221a22285a6SYan, Zheng 	h = kmem_cache_alloc(btrfs_trans_handle_cachep, GFP_NOFS);
222a22285a6SYan, Zheng 	if (!h)
223a22285a6SYan, Zheng 		return ERR_PTR(-ENOMEM);
224a22285a6SYan, Zheng 
225a22285a6SYan, Zheng 	if (may_wait_transaction(root, type))
22637d1aeeeSChris Mason 		wait_current_trans(root);
227a22285a6SYan, Zheng 
228a4abeea4SJosef Bacik 	do {
229a4abeea4SJosef Bacik 		ret = join_transaction(root, type == TRANS_JOIN_NOLOCK);
230a4abeea4SJosef Bacik 		if (ret == -EBUSY)
231a4abeea4SJosef Bacik 			wait_current_trans(root);
232a4abeea4SJosef Bacik 	} while (ret == -EBUSY);
233a4abeea4SJosef Bacik 
234db5b493aSTsutomu Itoh 	if (ret < 0) {
2356e8df2aeSYoshinori Sano 		kmem_cache_free(btrfs_trans_handle_cachep, h);
236db5b493aSTsutomu Itoh 		return ERR_PTR(ret);
237db5b493aSTsutomu Itoh 	}
2380f7d52f4SChris Mason 
239a22285a6SYan, Zheng 	cur_trans = root->fs_info->running_transaction;
240a22285a6SYan, Zheng 
241a22285a6SYan, Zheng 	h->transid = cur_trans->transid;
242a22285a6SYan, Zheng 	h->transaction = cur_trans;
24379154b1bSChris Mason 	h->blocks_used = 0;
244d2fb3437SYan Zheng 	h->block_group = 0;
245a22285a6SYan, Zheng 	h->bytes_reserved = 0;
24656bec294SChris Mason 	h->delayed_ref_updates = 0;
2472a1eb461SJosef Bacik 	h->use_count = 1;
248f0486c68SYan, Zheng 	h->block_rsv = NULL;
2492a1eb461SJosef Bacik 	h->orig_rsv = NULL;
250b7ec40d7SChris Mason 
251a22285a6SYan, Zheng 	smp_mb();
252a22285a6SYan, Zheng 	if (cur_trans->blocked && may_wait_transaction(root, type)) {
253a22285a6SYan, Zheng 		btrfs_commit_transaction(h, root);
254a22285a6SYan, Zheng 		goto again;
255a22285a6SYan, Zheng 	}
2569ed74f2dSJosef Bacik 
257a22285a6SYan, Zheng 	if (num_items > 0) {
2588bb8ab2eSJosef Bacik 		ret = btrfs_trans_reserve_metadata(h, root, num_items);
25906d5a589SJosef Bacik 		if (ret == -EAGAIN && !retries) {
26006d5a589SJosef Bacik 			retries++;
261a22285a6SYan, Zheng 			btrfs_commit_transaction(h, root);
262a22285a6SYan, Zheng 			goto again;
26306d5a589SJosef Bacik 		} else if (ret == -EAGAIN) {
26406d5a589SJosef Bacik 			/*
26506d5a589SJosef Bacik 			 * We have already retried and got EAGAIN, so really we
26606d5a589SJosef Bacik 			 * don't have space, so set ret to -ENOSPC.
26706d5a589SJosef Bacik 			 */
26806d5a589SJosef Bacik 			ret = -ENOSPC;
269a22285a6SYan, Zheng 		}
27006d5a589SJosef Bacik 
271a22285a6SYan, Zheng 		if (ret < 0) {
272a22285a6SYan, Zheng 			btrfs_end_transaction(h, root);
273a22285a6SYan, Zheng 			return ERR_PTR(ret);
274a22285a6SYan, Zheng 		}
275a22285a6SYan, Zheng 	}
276a22285a6SYan, Zheng 
2772a1eb461SJosef Bacik got_it:
278a4abeea4SJosef Bacik 	btrfs_record_root_in_trans(h, root);
279a22285a6SYan, Zheng 
280a22285a6SYan, Zheng 	if (!current->journal_info && type != TRANS_USERSPACE)
281a22285a6SYan, Zheng 		current->journal_info = h;
28279154b1bSChris Mason 	return h;
28379154b1bSChris Mason }
28479154b1bSChris Mason 
285f9295749SChris Mason struct btrfs_trans_handle *btrfs_start_transaction(struct btrfs_root *root,
286a22285a6SYan, Zheng 						   int num_items)
287f9295749SChris Mason {
288a22285a6SYan, Zheng 	return start_transaction(root, num_items, TRANS_START);
289f9295749SChris Mason }
2907a7eaa40SJosef Bacik struct btrfs_trans_handle *btrfs_join_transaction(struct btrfs_root *root)
291f9295749SChris Mason {
292a22285a6SYan, Zheng 	return start_transaction(root, 0, TRANS_JOIN);
293f9295749SChris Mason }
294f9295749SChris Mason 
2957a7eaa40SJosef Bacik struct btrfs_trans_handle *btrfs_join_transaction_nolock(struct btrfs_root *root)
2960af3d00bSJosef Bacik {
2970af3d00bSJosef Bacik 	return start_transaction(root, 0, TRANS_JOIN_NOLOCK);
2980af3d00bSJosef Bacik }
2990af3d00bSJosef Bacik 
3007a7eaa40SJosef Bacik struct btrfs_trans_handle *btrfs_start_ioctl_transaction(struct btrfs_root *root)
3019ca9ee09SSage Weil {
3027a7eaa40SJosef Bacik 	return start_transaction(root, 0, TRANS_USERSPACE);
3039ca9ee09SSage Weil }
3049ca9ee09SSage Weil 
305d352ac68SChris Mason /* wait for a transaction commit to be fully complete */
30689ce8a63SChris Mason static noinline int wait_for_commit(struct btrfs_root *root,
30789ce8a63SChris Mason 				    struct btrfs_transaction *commit)
30889ce8a63SChris Mason {
30989ce8a63SChris Mason 	DEFINE_WAIT(wait);
31089ce8a63SChris Mason 	while (!commit->commit_done) {
31189ce8a63SChris Mason 		prepare_to_wait(&commit->commit_wait, &wait,
31289ce8a63SChris Mason 				TASK_UNINTERRUPTIBLE);
31389ce8a63SChris Mason 		if (commit->commit_done)
31489ce8a63SChris Mason 			break;
31589ce8a63SChris Mason 		schedule();
31689ce8a63SChris Mason 	}
31789ce8a63SChris Mason 	finish_wait(&commit->commit_wait, &wait);
31889ce8a63SChris Mason 	return 0;
31989ce8a63SChris Mason }
32089ce8a63SChris Mason 
32146204592SSage Weil int btrfs_wait_for_commit(struct btrfs_root *root, u64 transid)
32246204592SSage Weil {
32346204592SSage Weil 	struct btrfs_transaction *cur_trans = NULL, *t;
32446204592SSage Weil 	int ret;
32546204592SSage Weil 
32646204592SSage Weil 	ret = 0;
32746204592SSage Weil 	if (transid) {
32846204592SSage Weil 		if (transid <= root->fs_info->last_trans_committed)
329a4abeea4SJosef Bacik 			goto out;
33046204592SSage Weil 
33146204592SSage Weil 		/* find specified transaction */
332a4abeea4SJosef Bacik 		spin_lock(&root->fs_info->trans_lock);
33346204592SSage Weil 		list_for_each_entry(t, &root->fs_info->trans_list, list) {
33446204592SSage Weil 			if (t->transid == transid) {
33546204592SSage Weil 				cur_trans = t;
336a4abeea4SJosef Bacik 				atomic_inc(&cur_trans->use_count);
33746204592SSage Weil 				break;
33846204592SSage Weil 			}
33946204592SSage Weil 			if (t->transid > transid)
34046204592SSage Weil 				break;
34146204592SSage Weil 		}
342a4abeea4SJosef Bacik 		spin_unlock(&root->fs_info->trans_lock);
34346204592SSage Weil 		ret = -EINVAL;
34446204592SSage Weil 		if (!cur_trans)
345a4abeea4SJosef Bacik 			goto out;  /* bad transid */
34646204592SSage Weil 	} else {
34746204592SSage Weil 		/* find newest transaction that is committing | committed */
348a4abeea4SJosef Bacik 		spin_lock(&root->fs_info->trans_lock);
34946204592SSage Weil 		list_for_each_entry_reverse(t, &root->fs_info->trans_list,
35046204592SSage Weil 					    list) {
35146204592SSage Weil 			if (t->in_commit) {
35246204592SSage Weil 				if (t->commit_done)
353a4abeea4SJosef Bacik 					goto out;
35446204592SSage Weil 				cur_trans = t;
355a4abeea4SJosef Bacik 				atomic_inc(&cur_trans->use_count);
35646204592SSage Weil 				break;
35746204592SSage Weil 			}
35846204592SSage Weil 		}
359a4abeea4SJosef Bacik 		spin_unlock(&root->fs_info->trans_lock);
36046204592SSage Weil 		if (!cur_trans)
361a4abeea4SJosef Bacik 			goto out;  /* nothing committing|committed */
36246204592SSage Weil 	}
36346204592SSage Weil 
36446204592SSage Weil 	wait_for_commit(root, cur_trans);
36546204592SSage Weil 
36646204592SSage Weil 	put_transaction(cur_trans);
36746204592SSage Weil 	ret = 0;
368a4abeea4SJosef Bacik out:
36946204592SSage Weil 	return ret;
37046204592SSage Weil }
37146204592SSage Weil 
3725d4f98a2SYan Zheng #if 0
373d352ac68SChris Mason /*
374d397712bSChris Mason  * rate limit against the drop_snapshot code.  This helps to slow down new
375d397712bSChris Mason  * operations if the drop_snapshot code isn't able to keep up.
376d352ac68SChris Mason  */
37737d1aeeeSChris Mason static void throttle_on_drops(struct btrfs_root *root)
378ab78c84dSChris Mason {
379ab78c84dSChris Mason 	struct btrfs_fs_info *info = root->fs_info;
3802dd3e67bSChris Mason 	int harder_count = 0;
381ab78c84dSChris Mason 
3822dd3e67bSChris Mason harder:
383ab78c84dSChris Mason 	if (atomic_read(&info->throttles)) {
384ab78c84dSChris Mason 		DEFINE_WAIT(wait);
385ab78c84dSChris Mason 		int thr;
386ab78c84dSChris Mason 		thr = atomic_read(&info->throttle_gen);
387ab78c84dSChris Mason 
388ab78c84dSChris Mason 		do {
389ab78c84dSChris Mason 			prepare_to_wait(&info->transaction_throttle,
390ab78c84dSChris Mason 					&wait, TASK_UNINTERRUPTIBLE);
391ab78c84dSChris Mason 			if (!atomic_read(&info->throttles)) {
392ab78c84dSChris Mason 				finish_wait(&info->transaction_throttle, &wait);
393ab78c84dSChris Mason 				break;
394ab78c84dSChris Mason 			}
395ab78c84dSChris Mason 			schedule();
396ab78c84dSChris Mason 			finish_wait(&info->transaction_throttle, &wait);
397ab78c84dSChris Mason 		} while (thr == atomic_read(&info->throttle_gen));
3982dd3e67bSChris Mason 		harder_count++;
3992dd3e67bSChris Mason 
4002dd3e67bSChris Mason 		if (root->fs_info->total_ref_cache_size > 1 * 1024 * 1024 &&
4012dd3e67bSChris Mason 		    harder_count < 2)
4022dd3e67bSChris Mason 			goto harder;
4032dd3e67bSChris Mason 
4042dd3e67bSChris Mason 		if (root->fs_info->total_ref_cache_size > 5 * 1024 * 1024 &&
4052dd3e67bSChris Mason 		    harder_count < 10)
4062dd3e67bSChris Mason 			goto harder;
4072dd3e67bSChris Mason 
4082dd3e67bSChris Mason 		if (root->fs_info->total_ref_cache_size > 10 * 1024 * 1024 &&
4092dd3e67bSChris Mason 		    harder_count < 20)
4102dd3e67bSChris Mason 			goto harder;
411ab78c84dSChris Mason 	}
412ab78c84dSChris Mason }
4135d4f98a2SYan Zheng #endif
414ab78c84dSChris Mason 
41537d1aeeeSChris Mason void btrfs_throttle(struct btrfs_root *root)
41637d1aeeeSChris Mason {
417a4abeea4SJosef Bacik 	if (!atomic_read(&root->fs_info->open_ioctl_trans))
41837d1aeeeSChris Mason 		wait_current_trans(root);
41937d1aeeeSChris Mason }
42037d1aeeeSChris Mason 
4218929ecfaSYan, Zheng static int should_end_transaction(struct btrfs_trans_handle *trans,
4228929ecfaSYan, Zheng 				  struct btrfs_root *root)
4238929ecfaSYan, Zheng {
4248929ecfaSYan, Zheng 	int ret;
4258929ecfaSYan, Zheng 	ret = btrfs_block_rsv_check(trans, root,
4268929ecfaSYan, Zheng 				    &root->fs_info->global_block_rsv, 0, 5);
4278929ecfaSYan, Zheng 	return ret ? 1 : 0;
4288929ecfaSYan, Zheng }
4298929ecfaSYan, Zheng 
4308929ecfaSYan, Zheng int btrfs_should_end_transaction(struct btrfs_trans_handle *trans,
4318929ecfaSYan, Zheng 				 struct btrfs_root *root)
4328929ecfaSYan, Zheng {
4338929ecfaSYan, Zheng 	struct btrfs_transaction *cur_trans = trans->transaction;
4348929ecfaSYan, Zheng 	int updates;
4358929ecfaSYan, Zheng 
436a4abeea4SJosef Bacik 	smp_mb();
4378929ecfaSYan, Zheng 	if (cur_trans->blocked || cur_trans->delayed_refs.flushing)
4388929ecfaSYan, Zheng 		return 1;
4398929ecfaSYan, Zheng 
4408929ecfaSYan, Zheng 	updates = trans->delayed_ref_updates;
4418929ecfaSYan, Zheng 	trans->delayed_ref_updates = 0;
4428929ecfaSYan, Zheng 	if (updates)
4438929ecfaSYan, Zheng 		btrfs_run_delayed_refs(trans, root, updates);
4448929ecfaSYan, Zheng 
4458929ecfaSYan, Zheng 	return should_end_transaction(trans, root);
4468929ecfaSYan, Zheng }
4478929ecfaSYan, Zheng 
44889ce8a63SChris Mason static int __btrfs_end_transaction(struct btrfs_trans_handle *trans,
4490af3d00bSJosef Bacik 			  struct btrfs_root *root, int throttle, int lock)
45079154b1bSChris Mason {
4518929ecfaSYan, Zheng 	struct btrfs_transaction *cur_trans = trans->transaction;
452ab78c84dSChris Mason 	struct btrfs_fs_info *info = root->fs_info;
453c3e69d58SChris Mason 	int count = 0;
454d6e4a428SChris Mason 
4552a1eb461SJosef Bacik 	if (--trans->use_count) {
4562a1eb461SJosef Bacik 		trans->block_rsv = trans->orig_rsv;
4572a1eb461SJosef Bacik 		return 0;
4582a1eb461SJosef Bacik 	}
4592a1eb461SJosef Bacik 
460c3e69d58SChris Mason 	while (count < 4) {
461c3e69d58SChris Mason 		unsigned long cur = trans->delayed_ref_updates;
462c3e69d58SChris Mason 		trans->delayed_ref_updates = 0;
463c3e69d58SChris Mason 		if (cur &&
464c3e69d58SChris Mason 		    trans->transaction->delayed_refs.num_heads_ready > 64) {
465c3e69d58SChris Mason 			trans->delayed_ref_updates = 0;
466b7ec40d7SChris Mason 
467b7ec40d7SChris Mason 			/*
468b7ec40d7SChris Mason 			 * do a full flush if the transaction is trying
469b7ec40d7SChris Mason 			 * to close
470b7ec40d7SChris Mason 			 */
471b7ec40d7SChris Mason 			if (trans->transaction->delayed_refs.flushing)
472b7ec40d7SChris Mason 				cur = 0;
473c3e69d58SChris Mason 			btrfs_run_delayed_refs(trans, root, cur);
474c3e69d58SChris Mason 		} else {
475c3e69d58SChris Mason 			break;
476c3e69d58SChris Mason 		}
477c3e69d58SChris Mason 		count++;
47856bec294SChris Mason 	}
47956bec294SChris Mason 
480a22285a6SYan, Zheng 	btrfs_trans_release_metadata(trans, root);
481a22285a6SYan, Zheng 
482a4abeea4SJosef Bacik 	if (lock && !atomic_read(&root->fs_info->open_ioctl_trans) &&
483a4abeea4SJosef Bacik 	    should_end_transaction(trans, root)) {
4848929ecfaSYan, Zheng 		trans->transaction->blocked = 1;
485a4abeea4SJosef Bacik 		smp_wmb();
486a4abeea4SJosef Bacik 	}
4878929ecfaSYan, Zheng 
4880af3d00bSJosef Bacik 	if (lock && cur_trans->blocked && !cur_trans->in_commit) {
4898929ecfaSYan, Zheng 		if (throttle)
4908929ecfaSYan, Zheng 			return btrfs_commit_transaction(trans, root);
4918929ecfaSYan, Zheng 		else
4928929ecfaSYan, Zheng 			wake_up_process(info->transaction_kthread);
4938929ecfaSYan, Zheng 	}
4948929ecfaSYan, Zheng 
4958929ecfaSYan, Zheng 	WARN_ON(cur_trans != info->running_transaction);
49613c5a93eSJosef Bacik 	WARN_ON(atomic_read(&cur_trans->num_writers) < 1);
49713c5a93eSJosef Bacik 	atomic_dec(&cur_trans->num_writers);
49889ce8a63SChris Mason 
49999d16cbcSSage Weil 	smp_mb();
50079154b1bSChris Mason 	if (waitqueue_active(&cur_trans->writer_wait))
50179154b1bSChris Mason 		wake_up(&cur_trans->writer_wait);
50279154b1bSChris Mason 	put_transaction(cur_trans);
5039ed74f2dSJosef Bacik 
5049ed74f2dSJosef Bacik 	if (current->journal_info == trans)
5059ed74f2dSJosef Bacik 		current->journal_info = NULL;
506d6025579SChris Mason 	memset(trans, 0, sizeof(*trans));
5072c90e5d6SChris Mason 	kmem_cache_free(btrfs_trans_handle_cachep, trans);
508ab78c84dSChris Mason 
50924bbcf04SYan, Zheng 	if (throttle)
51024bbcf04SYan, Zheng 		btrfs_run_delayed_iputs(root);
51124bbcf04SYan, Zheng 
51279154b1bSChris Mason 	return 0;
51379154b1bSChris Mason }
51479154b1bSChris Mason 
51589ce8a63SChris Mason int btrfs_end_transaction(struct btrfs_trans_handle *trans,
51689ce8a63SChris Mason 			  struct btrfs_root *root)
51789ce8a63SChris Mason {
5180af3d00bSJosef Bacik 	return __btrfs_end_transaction(trans, root, 0, 1);
51989ce8a63SChris Mason }
52089ce8a63SChris Mason 
52189ce8a63SChris Mason int btrfs_end_transaction_throttle(struct btrfs_trans_handle *trans,
52289ce8a63SChris Mason 				   struct btrfs_root *root)
52389ce8a63SChris Mason {
5240af3d00bSJosef Bacik 	return __btrfs_end_transaction(trans, root, 1, 1);
5250af3d00bSJosef Bacik }
5260af3d00bSJosef Bacik 
5270af3d00bSJosef Bacik int btrfs_end_transaction_nolock(struct btrfs_trans_handle *trans,
5280af3d00bSJosef Bacik 				 struct btrfs_root *root)
5290af3d00bSJosef Bacik {
5300af3d00bSJosef Bacik 	return __btrfs_end_transaction(trans, root, 0, 0);
53189ce8a63SChris Mason }
53289ce8a63SChris Mason 
533d352ac68SChris Mason /*
534d352ac68SChris Mason  * when btree blocks are allocated, they have some corresponding bits set for
535d352ac68SChris Mason  * them in one of two extent_io trees.  This is used to make sure all of
536690587d1SChris Mason  * those extents are sent to disk but does not wait on them
537d352ac68SChris Mason  */
538690587d1SChris Mason int btrfs_write_marked_extents(struct btrfs_root *root,
5398cef4e16SYan, Zheng 			       struct extent_io_tree *dirty_pages, int mark)
54079154b1bSChris Mason {
5417c4452b9SChris Mason 	int ret;
542777e6bd7SChris Mason 	int err = 0;
5437c4452b9SChris Mason 	int werr = 0;
5447c4452b9SChris Mason 	struct page *page;
5457c4452b9SChris Mason 	struct inode *btree_inode = root->fs_info->btree_inode;
546777e6bd7SChris Mason 	u64 start = 0;
5475f39d397SChris Mason 	u64 end;
5485f39d397SChris Mason 	unsigned long index;
5497c4452b9SChris Mason 
5507c4452b9SChris Mason 	while (1) {
551777e6bd7SChris Mason 		ret = find_first_extent_bit(dirty_pages, start, &start, &end,
5528cef4e16SYan, Zheng 					    mark);
5535f39d397SChris Mason 		if (ret)
5547c4452b9SChris Mason 			break;
5555f39d397SChris Mason 		while (start <= end) {
556777e6bd7SChris Mason 			cond_resched();
557777e6bd7SChris Mason 
5585f39d397SChris Mason 			index = start >> PAGE_CACHE_SHIFT;
55935ebb934SChris Mason 			start = (u64)(index + 1) << PAGE_CACHE_SHIFT;
5604bef0848SChris Mason 			page = find_get_page(btree_inode->i_mapping, index);
5617c4452b9SChris Mason 			if (!page)
5627c4452b9SChris Mason 				continue;
5634bef0848SChris Mason 
5644bef0848SChris Mason 			btree_lock_page_hook(page);
5654bef0848SChris Mason 			if (!page->mapping) {
5664bef0848SChris Mason 				unlock_page(page);
5674bef0848SChris Mason 				page_cache_release(page);
5684bef0848SChris Mason 				continue;
5694bef0848SChris Mason 			}
5704bef0848SChris Mason 
5716702ed49SChris Mason 			if (PageWriteback(page)) {
5726702ed49SChris Mason 				if (PageDirty(page))
5736702ed49SChris Mason 					wait_on_page_writeback(page);
5746702ed49SChris Mason 				else {
5756702ed49SChris Mason 					unlock_page(page);
5766702ed49SChris Mason 					page_cache_release(page);
5776702ed49SChris Mason 					continue;
5786702ed49SChris Mason 				}
5796702ed49SChris Mason 			}
5807c4452b9SChris Mason 			err = write_one_page(page, 0);
5817c4452b9SChris Mason 			if (err)
5827c4452b9SChris Mason 				werr = err;
5837c4452b9SChris Mason 			page_cache_release(page);
5847c4452b9SChris Mason 		}
5857c4452b9SChris Mason 	}
586690587d1SChris Mason 	if (err)
587690587d1SChris Mason 		werr = err;
588690587d1SChris Mason 	return werr;
589690587d1SChris Mason }
590690587d1SChris Mason 
591690587d1SChris Mason /*
592690587d1SChris Mason  * when btree blocks are allocated, they have some corresponding bits set for
593690587d1SChris Mason  * them in one of two extent_io trees.  This is used to make sure all of
594690587d1SChris Mason  * those extents are on disk for transaction or log commit.  We wait
595690587d1SChris Mason  * on all the pages and clear them from the dirty pages state tree
596690587d1SChris Mason  */
597690587d1SChris Mason int btrfs_wait_marked_extents(struct btrfs_root *root,
5988cef4e16SYan, Zheng 			      struct extent_io_tree *dirty_pages, int mark)
599690587d1SChris Mason {
600690587d1SChris Mason 	int ret;
601690587d1SChris Mason 	int err = 0;
602690587d1SChris Mason 	int werr = 0;
603690587d1SChris Mason 	struct page *page;
604690587d1SChris Mason 	struct inode *btree_inode = root->fs_info->btree_inode;
605690587d1SChris Mason 	u64 start = 0;
606690587d1SChris Mason 	u64 end;
607690587d1SChris Mason 	unsigned long index;
608690587d1SChris Mason 
609777e6bd7SChris Mason 	while (1) {
6108cef4e16SYan, Zheng 		ret = find_first_extent_bit(dirty_pages, start, &start, &end,
6118cef4e16SYan, Zheng 					    mark);
612777e6bd7SChris Mason 		if (ret)
613777e6bd7SChris Mason 			break;
614777e6bd7SChris Mason 
6158cef4e16SYan, Zheng 		clear_extent_bits(dirty_pages, start, end, mark, GFP_NOFS);
616777e6bd7SChris Mason 		while (start <= end) {
617777e6bd7SChris Mason 			index = start >> PAGE_CACHE_SHIFT;
618777e6bd7SChris Mason 			start = (u64)(index + 1) << PAGE_CACHE_SHIFT;
619777e6bd7SChris Mason 			page = find_get_page(btree_inode->i_mapping, index);
620777e6bd7SChris Mason 			if (!page)
621777e6bd7SChris Mason 				continue;
622777e6bd7SChris Mason 			if (PageDirty(page)) {
6234bef0848SChris Mason 				btree_lock_page_hook(page);
6244bef0848SChris Mason 				wait_on_page_writeback(page);
625777e6bd7SChris Mason 				err = write_one_page(page, 0);
626777e6bd7SChris Mason 				if (err)
627777e6bd7SChris Mason 					werr = err;
628777e6bd7SChris Mason 			}
629777e6bd7SChris Mason 			wait_on_page_writeback(page);
630777e6bd7SChris Mason 			page_cache_release(page);
631777e6bd7SChris Mason 			cond_resched();
632777e6bd7SChris Mason 		}
633777e6bd7SChris Mason 	}
6347c4452b9SChris Mason 	if (err)
6357c4452b9SChris Mason 		werr = err;
6367c4452b9SChris Mason 	return werr;
63779154b1bSChris Mason }
63879154b1bSChris Mason 
639690587d1SChris Mason /*
640690587d1SChris Mason  * when btree blocks are allocated, they have some corresponding bits set for
641690587d1SChris Mason  * them in one of two extent_io trees.  This is used to make sure all of
642690587d1SChris Mason  * those extents are on disk for transaction or log commit
643690587d1SChris Mason  */
644690587d1SChris Mason int btrfs_write_and_wait_marked_extents(struct btrfs_root *root,
6458cef4e16SYan, Zheng 				struct extent_io_tree *dirty_pages, int mark)
646690587d1SChris Mason {
647690587d1SChris Mason 	int ret;
648690587d1SChris Mason 	int ret2;
649690587d1SChris Mason 
6508cef4e16SYan, Zheng 	ret = btrfs_write_marked_extents(root, dirty_pages, mark);
6518cef4e16SYan, Zheng 	ret2 = btrfs_wait_marked_extents(root, dirty_pages, mark);
652690587d1SChris Mason 	return ret || ret2;
653690587d1SChris Mason }
654690587d1SChris Mason 
655d0c803c4SChris Mason int btrfs_write_and_wait_transaction(struct btrfs_trans_handle *trans,
656d0c803c4SChris Mason 				     struct btrfs_root *root)
657d0c803c4SChris Mason {
658d0c803c4SChris Mason 	if (!trans || !trans->transaction) {
659d0c803c4SChris Mason 		struct inode *btree_inode;
660d0c803c4SChris Mason 		btree_inode = root->fs_info->btree_inode;
661d0c803c4SChris Mason 		return filemap_write_and_wait(btree_inode->i_mapping);
662d0c803c4SChris Mason 	}
663d0c803c4SChris Mason 	return btrfs_write_and_wait_marked_extents(root,
6648cef4e16SYan, Zheng 					   &trans->transaction->dirty_pages,
6658cef4e16SYan, Zheng 					   EXTENT_DIRTY);
666d0c803c4SChris Mason }
667d0c803c4SChris Mason 
668d352ac68SChris Mason /*
669d352ac68SChris Mason  * this is used to update the root pointer in the tree of tree roots.
670d352ac68SChris Mason  *
671d352ac68SChris Mason  * But, in the case of the extent allocation tree, updating the root
672d352ac68SChris Mason  * pointer may allocate blocks which may change the root of the extent
673d352ac68SChris Mason  * allocation tree.
674d352ac68SChris Mason  *
675d352ac68SChris Mason  * So, this loops and repeats and makes sure the cowonly root didn't
676d352ac68SChris Mason  * change while the root pointer was being updated in the metadata.
677d352ac68SChris Mason  */
6780b86a832SChris Mason static int update_cowonly_root(struct btrfs_trans_handle *trans,
67979154b1bSChris Mason 			       struct btrfs_root *root)
68079154b1bSChris Mason {
68179154b1bSChris Mason 	int ret;
6820b86a832SChris Mason 	u64 old_root_bytenr;
68386b9f2ecSYan, Zheng 	u64 old_root_used;
6840b86a832SChris Mason 	struct btrfs_root *tree_root = root->fs_info->tree_root;
68579154b1bSChris Mason 
68686b9f2ecSYan, Zheng 	old_root_used = btrfs_root_used(&root->root_item);
6870b86a832SChris Mason 	btrfs_write_dirty_block_groups(trans, root);
68856bec294SChris Mason 
68979154b1bSChris Mason 	while (1) {
6900b86a832SChris Mason 		old_root_bytenr = btrfs_root_bytenr(&root->root_item);
69186b9f2ecSYan, Zheng 		if (old_root_bytenr == root->node->start &&
69286b9f2ecSYan, Zheng 		    old_root_used == btrfs_root_used(&root->root_item))
69379154b1bSChris Mason 			break;
69487ef2bb4SChris Mason 
6955d4f98a2SYan Zheng 		btrfs_set_root_node(&root->root_item, root->node);
69679154b1bSChris Mason 		ret = btrfs_update_root(trans, tree_root,
6970b86a832SChris Mason 					&root->root_key,
6980b86a832SChris Mason 					&root->root_item);
69979154b1bSChris Mason 		BUG_ON(ret);
70056bec294SChris Mason 
70186b9f2ecSYan, Zheng 		old_root_used = btrfs_root_used(&root->root_item);
7024a8c9a62SYan Zheng 		ret = btrfs_write_dirty_block_groups(trans, root);
70356bec294SChris Mason 		BUG_ON(ret);
7040b86a832SChris Mason 	}
705276e680dSYan Zheng 
706276e680dSYan Zheng 	if (root != root->fs_info->extent_root)
707817d52f8SJosef Bacik 		switch_commit_root(root);
708276e680dSYan Zheng 
7090b86a832SChris Mason 	return 0;
7100b86a832SChris Mason }
7110b86a832SChris Mason 
712d352ac68SChris Mason /*
713d352ac68SChris Mason  * update all the cowonly tree roots on disk
714d352ac68SChris Mason  */
7155d4f98a2SYan Zheng static noinline int commit_cowonly_roots(struct btrfs_trans_handle *trans,
7160b86a832SChris Mason 					 struct btrfs_root *root)
7170b86a832SChris Mason {
7180b86a832SChris Mason 	struct btrfs_fs_info *fs_info = root->fs_info;
7190b86a832SChris Mason 	struct list_head *next;
72084234f3aSYan Zheng 	struct extent_buffer *eb;
72156bec294SChris Mason 	int ret;
72284234f3aSYan Zheng 
72356bec294SChris Mason 	ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
72456bec294SChris Mason 	BUG_ON(ret);
72587ef2bb4SChris Mason 
72684234f3aSYan Zheng 	eb = btrfs_lock_root_node(fs_info->tree_root);
7279fa8cfe7SChris Mason 	btrfs_cow_block(trans, fs_info->tree_root, eb, NULL, 0, &eb);
72884234f3aSYan Zheng 	btrfs_tree_unlock(eb);
72984234f3aSYan Zheng 	free_extent_buffer(eb);
7300b86a832SChris Mason 
73156bec294SChris Mason 	ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
73256bec294SChris Mason 	BUG_ON(ret);
73387ef2bb4SChris Mason 
7340b86a832SChris Mason 	while (!list_empty(&fs_info->dirty_cowonly_roots)) {
7350b86a832SChris Mason 		next = fs_info->dirty_cowonly_roots.next;
7360b86a832SChris Mason 		list_del_init(next);
7370b86a832SChris Mason 		root = list_entry(next, struct btrfs_root, dirty_list);
73887ef2bb4SChris Mason 
7390b86a832SChris Mason 		update_cowonly_root(trans, root);
74079154b1bSChris Mason 	}
741276e680dSYan Zheng 
742276e680dSYan Zheng 	down_write(&fs_info->extent_commit_sem);
743276e680dSYan Zheng 	switch_commit_root(fs_info->extent_root);
744276e680dSYan Zheng 	up_write(&fs_info->extent_commit_sem);
745276e680dSYan Zheng 
74679154b1bSChris Mason 	return 0;
74779154b1bSChris Mason }
74879154b1bSChris Mason 
749d352ac68SChris Mason /*
750d352ac68SChris Mason  * dead roots are old snapshots that need to be deleted.  This allocates
751d352ac68SChris Mason  * a dirty root struct and adds it into the list of dead roots that need to
752d352ac68SChris Mason  * be deleted
753d352ac68SChris Mason  */
7545d4f98a2SYan Zheng int btrfs_add_dead_root(struct btrfs_root *root)
7555eda7b5eSChris Mason {
756a4abeea4SJosef Bacik 	spin_lock(&root->fs_info->trans_lock);
7575d4f98a2SYan Zheng 	list_add(&root->root_list, &root->fs_info->dead_roots);
758a4abeea4SJosef Bacik 	spin_unlock(&root->fs_info->trans_lock);
7595eda7b5eSChris Mason 	return 0;
7605eda7b5eSChris Mason }
7615eda7b5eSChris Mason 
762d352ac68SChris Mason /*
7635d4f98a2SYan Zheng  * update all the cowonly tree roots on disk
764d352ac68SChris Mason  */
7655d4f98a2SYan Zheng static noinline int commit_fs_roots(struct btrfs_trans_handle *trans,
7665d4f98a2SYan Zheng 				    struct btrfs_root *root)
7670f7d52f4SChris Mason {
7680f7d52f4SChris Mason 	struct btrfs_root *gang[8];
7695d4f98a2SYan Zheng 	struct btrfs_fs_info *fs_info = root->fs_info;
7700f7d52f4SChris Mason 	int i;
7710f7d52f4SChris Mason 	int ret;
77254aa1f4dSChris Mason 	int err = 0;
77354aa1f4dSChris Mason 
774a4abeea4SJosef Bacik 	spin_lock(&fs_info->fs_roots_radix_lock);
7750f7d52f4SChris Mason 	while (1) {
7765d4f98a2SYan Zheng 		ret = radix_tree_gang_lookup_tag(&fs_info->fs_roots_radix,
7775d4f98a2SYan Zheng 						 (void **)gang, 0,
7780f7d52f4SChris Mason 						 ARRAY_SIZE(gang),
7790f7d52f4SChris Mason 						 BTRFS_ROOT_TRANS_TAG);
7800f7d52f4SChris Mason 		if (ret == 0)
7810f7d52f4SChris Mason 			break;
7820f7d52f4SChris Mason 		for (i = 0; i < ret; i++) {
7830f7d52f4SChris Mason 			root = gang[i];
7845d4f98a2SYan Zheng 			radix_tree_tag_clear(&fs_info->fs_roots_radix,
7852619ba1fSChris Mason 					(unsigned long)root->root_key.objectid,
7860f7d52f4SChris Mason 					BTRFS_ROOT_TRANS_TAG);
787a4abeea4SJosef Bacik 			spin_unlock(&fs_info->fs_roots_radix_lock);
78831153d81SYan Zheng 
789e02119d5SChris Mason 			btrfs_free_log(trans, root);
7905d4f98a2SYan Zheng 			btrfs_update_reloc_root(trans, root);
791d68fc57bSYan, Zheng 			btrfs_orphan_commit_root(trans, root);
792e02119d5SChris Mason 
793978d910dSYan Zheng 			if (root->commit_root != root->node) {
794817d52f8SJosef Bacik 				switch_commit_root(root);
795978d910dSYan Zheng 				btrfs_set_root_node(&root->root_item,
796978d910dSYan Zheng 						    root->node);
797978d910dSYan Zheng 			}
79831153d81SYan Zheng 
7995d4f98a2SYan Zheng 			err = btrfs_update_root(trans, fs_info->tree_root,
8000f7d52f4SChris Mason 						&root->root_key,
8010f7d52f4SChris Mason 						&root->root_item);
802a4abeea4SJosef Bacik 			spin_lock(&fs_info->fs_roots_radix_lock);
80354aa1f4dSChris Mason 			if (err)
80454aa1f4dSChris Mason 				break;
8050f7d52f4SChris Mason 		}
8069f3a7427SChris Mason 	}
807a4abeea4SJosef Bacik 	spin_unlock(&fs_info->fs_roots_radix_lock);
80854aa1f4dSChris Mason 	return err;
8090f7d52f4SChris Mason }
8100f7d52f4SChris Mason 
811d352ac68SChris Mason /*
812d352ac68SChris Mason  * defrag a given btree.  If cacheonly == 1, this won't read from the disk,
813d352ac68SChris Mason  * otherwise every leaf in the btree is read and defragged.
814d352ac68SChris Mason  */
815e9d0b13bSChris Mason int btrfs_defrag_root(struct btrfs_root *root, int cacheonly)
816e9d0b13bSChris Mason {
817e9d0b13bSChris Mason 	struct btrfs_fs_info *info = root->fs_info;
818e9d0b13bSChris Mason 	struct btrfs_trans_handle *trans;
8198929ecfaSYan, Zheng 	int ret;
820d3c2fdcfSChris Mason 	unsigned long nr;
821e9d0b13bSChris Mason 
8228929ecfaSYan, Zheng 	if (xchg(&root->defrag_running, 1))
823e9d0b13bSChris Mason 		return 0;
8248929ecfaSYan, Zheng 
8256b80053dSChris Mason 	while (1) {
8268929ecfaSYan, Zheng 		trans = btrfs_start_transaction(root, 0);
8278929ecfaSYan, Zheng 		if (IS_ERR(trans))
8288929ecfaSYan, Zheng 			return PTR_ERR(trans);
8298929ecfaSYan, Zheng 
830e9d0b13bSChris Mason 		ret = btrfs_defrag_leaves(trans, root, cacheonly);
8318929ecfaSYan, Zheng 
832d3c2fdcfSChris Mason 		nr = trans->blocks_used;
833e9d0b13bSChris Mason 		btrfs_end_transaction(trans, root);
834d3c2fdcfSChris Mason 		btrfs_btree_balance_dirty(info->tree_root, nr);
835e9d0b13bSChris Mason 		cond_resched();
836e9d0b13bSChris Mason 
8373f157a2fSChris Mason 		if (root->fs_info->closing || ret != -EAGAIN)
838e9d0b13bSChris Mason 			break;
839e9d0b13bSChris Mason 	}
840e9d0b13bSChris Mason 	root->defrag_running = 0;
8418929ecfaSYan, Zheng 	return ret;
842e9d0b13bSChris Mason }
843e9d0b13bSChris Mason 
8442c47e605SYan Zheng #if 0
845d352ac68SChris Mason /*
846b7ec40d7SChris Mason  * when dropping snapshots, we generate a ton of delayed refs, and it makes
847b7ec40d7SChris Mason  * sense not to join the transaction while it is trying to flush the current
848b7ec40d7SChris Mason  * queue of delayed refs out.
849b7ec40d7SChris Mason  *
850b7ec40d7SChris Mason  * This is used by the drop snapshot code only
851b7ec40d7SChris Mason  */
852b7ec40d7SChris Mason static noinline int wait_transaction_pre_flush(struct btrfs_fs_info *info)
853b7ec40d7SChris Mason {
854b7ec40d7SChris Mason 	DEFINE_WAIT(wait);
855b7ec40d7SChris Mason 
856b7ec40d7SChris Mason 	mutex_lock(&info->trans_mutex);
857b7ec40d7SChris Mason 	while (info->running_transaction &&
858b7ec40d7SChris Mason 	       info->running_transaction->delayed_refs.flushing) {
859b7ec40d7SChris Mason 		prepare_to_wait(&info->transaction_wait, &wait,
860b7ec40d7SChris Mason 				TASK_UNINTERRUPTIBLE);
861b7ec40d7SChris Mason 		mutex_unlock(&info->trans_mutex);
86259bc5c75SChris Mason 
863b7ec40d7SChris Mason 		schedule();
86459bc5c75SChris Mason 
865b7ec40d7SChris Mason 		mutex_lock(&info->trans_mutex);
866b7ec40d7SChris Mason 		finish_wait(&info->transaction_wait, &wait);
867b7ec40d7SChris Mason 	}
868b7ec40d7SChris Mason 	mutex_unlock(&info->trans_mutex);
869b7ec40d7SChris Mason 	return 0;
870b7ec40d7SChris Mason }
871b7ec40d7SChris Mason 
872b7ec40d7SChris Mason /*
873d352ac68SChris Mason  * Given a list of roots that need to be deleted, call btrfs_drop_snapshot on
874d352ac68SChris Mason  * all of them
875d352ac68SChris Mason  */
8765d4f98a2SYan Zheng int btrfs_drop_dead_root(struct btrfs_root *root)
8770f7d52f4SChris Mason {
8780f7d52f4SChris Mason 	struct btrfs_trans_handle *trans;
8795d4f98a2SYan Zheng 	struct btrfs_root *tree_root = root->fs_info->tree_root;
880d3c2fdcfSChris Mason 	unsigned long nr;
8815d4f98a2SYan Zheng 	int ret;
88258176a96SJosef Bacik 
8839f3a7427SChris Mason 	while (1) {
884b7ec40d7SChris Mason 		/*
885b7ec40d7SChris Mason 		 * we don't want to jump in and create a bunch of
886b7ec40d7SChris Mason 		 * delayed refs if the transaction is starting to close
887b7ec40d7SChris Mason 		 */
888b7ec40d7SChris Mason 		wait_transaction_pre_flush(tree_root->fs_info);
8890f7d52f4SChris Mason 		trans = btrfs_start_transaction(tree_root, 1);
890b7ec40d7SChris Mason 
891b7ec40d7SChris Mason 		/*
892b7ec40d7SChris Mason 		 * we've joined a transaction, make sure it isn't
893b7ec40d7SChris Mason 		 * closing right now
894b7ec40d7SChris Mason 		 */
895b7ec40d7SChris Mason 		if (trans->transaction->delayed_refs.flushing) {
896b7ec40d7SChris Mason 			btrfs_end_transaction(trans, tree_root);
897b7ec40d7SChris Mason 			continue;
898b7ec40d7SChris Mason 		}
899b7ec40d7SChris Mason 
9005d4f98a2SYan Zheng 		ret = btrfs_drop_snapshot(trans, root);
901d397712bSChris Mason 		if (ret != -EAGAIN)
9029f3a7427SChris Mason 			break;
90358176a96SJosef Bacik 
9045d4f98a2SYan Zheng 		ret = btrfs_update_root(trans, tree_root,
9055d4f98a2SYan Zheng 					&root->root_key,
9065d4f98a2SYan Zheng 					&root->root_item);
9075d4f98a2SYan Zheng 		if (ret)
90854aa1f4dSChris Mason 			break;
909bcc63abbSYan 
910d3c2fdcfSChris Mason 		nr = trans->blocks_used;
9110f7d52f4SChris Mason 		ret = btrfs_end_transaction(trans, tree_root);
9120f7d52f4SChris Mason 		BUG_ON(ret);
9135eda7b5eSChris Mason 
914d3c2fdcfSChris Mason 		btrfs_btree_balance_dirty(tree_root, nr);
9154dc11904SChris Mason 		cond_resched();
9160f7d52f4SChris Mason 	}
9175d4f98a2SYan Zheng 	BUG_ON(ret);
9185d4f98a2SYan Zheng 
9195d4f98a2SYan Zheng 	ret = btrfs_del_root(trans, tree_root, &root->root_key);
9205d4f98a2SYan Zheng 	BUG_ON(ret);
9215d4f98a2SYan Zheng 
9225d4f98a2SYan Zheng 	nr = trans->blocks_used;
9235d4f98a2SYan Zheng 	ret = btrfs_end_transaction(trans, tree_root);
9245d4f98a2SYan Zheng 	BUG_ON(ret);
9255d4f98a2SYan Zheng 
9265d4f98a2SYan Zheng 	free_extent_buffer(root->node);
9275d4f98a2SYan Zheng 	free_extent_buffer(root->commit_root);
9285d4f98a2SYan Zheng 	kfree(root);
9295d4f98a2SYan Zheng 
9305d4f98a2SYan Zheng 	btrfs_btree_balance_dirty(tree_root, nr);
93154aa1f4dSChris Mason 	return ret;
9320f7d52f4SChris Mason }
9332c47e605SYan Zheng #endif
9340f7d52f4SChris Mason 
935d352ac68SChris Mason /*
936d352ac68SChris Mason  * new snapshots need to be created at a very specific time in the
937d352ac68SChris Mason  * transaction commit.  This does the actual creation
938d352ac68SChris Mason  */
93980b6794dSChris Mason static noinline int create_pending_snapshot(struct btrfs_trans_handle *trans,
9403063d29fSChris Mason 				   struct btrfs_fs_info *fs_info,
9413063d29fSChris Mason 				   struct btrfs_pending_snapshot *pending)
9423063d29fSChris Mason {
9433063d29fSChris Mason 	struct btrfs_key key;
94480b6794dSChris Mason 	struct btrfs_root_item *new_root_item;
9453063d29fSChris Mason 	struct btrfs_root *tree_root = fs_info->tree_root;
9463063d29fSChris Mason 	struct btrfs_root *root = pending->root;
9476bdb72deSSage Weil 	struct btrfs_root *parent_root;
9486bdb72deSSage Weil 	struct inode *parent_inode;
9496a912213SJosef Bacik 	struct dentry *parent;
950a22285a6SYan, Zheng 	struct dentry *dentry;
9513063d29fSChris Mason 	struct extent_buffer *tmp;
952925baeddSChris Mason 	struct extent_buffer *old;
9533063d29fSChris Mason 	int ret;
954d68fc57bSYan, Zheng 	u64 to_reserve = 0;
9556bdb72deSSage Weil 	u64 index = 0;
956a22285a6SYan, Zheng 	u64 objectid;
957b83cc969SLi Zefan 	u64 root_flags;
9583063d29fSChris Mason 
95980b6794dSChris Mason 	new_root_item = kmalloc(sizeof(*new_root_item), GFP_NOFS);
96080b6794dSChris Mason 	if (!new_root_item) {
961a22285a6SYan, Zheng 		pending->error = -ENOMEM;
96280b6794dSChris Mason 		goto fail;
96380b6794dSChris Mason 	}
964a22285a6SYan, Zheng 
9653063d29fSChris Mason 	ret = btrfs_find_free_objectid(trans, tree_root, 0, &objectid);
966a22285a6SYan, Zheng 	if (ret) {
967a22285a6SYan, Zheng 		pending->error = ret;
9683063d29fSChris Mason 		goto fail;
969a22285a6SYan, Zheng 	}
9703063d29fSChris Mason 
9713fd0a558SYan, Zheng 	btrfs_reloc_pre_snapshot(trans, pending, &to_reserve);
972d68fc57bSYan, Zheng 	btrfs_orphan_pre_snapshot(trans, pending, &to_reserve);
973d68fc57bSYan, Zheng 
974d68fc57bSYan, Zheng 	if (to_reserve > 0) {
975d68fc57bSYan, Zheng 		ret = btrfs_block_rsv_add(trans, root, &pending->block_rsv,
9768bb8ab2eSJosef Bacik 					  to_reserve);
977d68fc57bSYan, Zheng 		if (ret) {
978d68fc57bSYan, Zheng 			pending->error = ret;
979d68fc57bSYan, Zheng 			goto fail;
980d68fc57bSYan, Zheng 		}
981d68fc57bSYan, Zheng 	}
982d68fc57bSYan, Zheng 
9833063d29fSChris Mason 	key.objectid = objectid;
984a22285a6SYan, Zheng 	key.offset = (u64)-1;
985a22285a6SYan, Zheng 	key.type = BTRFS_ROOT_ITEM_KEY;
9863063d29fSChris Mason 
987a22285a6SYan, Zheng 	trans->block_rsv = &pending->block_rsv;
9886bdb72deSSage Weil 
989a22285a6SYan, Zheng 	dentry = pending->dentry;
9906a912213SJosef Bacik 	parent = dget_parent(dentry);
9916a912213SJosef Bacik 	parent_inode = parent->d_inode;
992a22285a6SYan, Zheng 	parent_root = BTRFS_I(parent_inode)->root;
993a4abeea4SJosef Bacik 	btrfs_record_root_in_trans(trans, parent_root);
994a22285a6SYan, Zheng 
9956bdb72deSSage Weil 	/*
9966bdb72deSSage Weil 	 * insert the directory item
9976bdb72deSSage Weil 	 */
9986bdb72deSSage Weil 	ret = btrfs_set_inode_index(parent_inode, &index);
9996bdb72deSSage Weil 	BUG_ON(ret);
10006bdb72deSSage Weil 	ret = btrfs_insert_dir_item(trans, parent_root,
1001a22285a6SYan, Zheng 				dentry->d_name.name, dentry->d_name.len,
1002a22285a6SYan, Zheng 				parent_inode->i_ino, &key,
1003a22285a6SYan, Zheng 				BTRFS_FT_DIR, index);
10046bdb72deSSage Weil 	BUG_ON(ret);
10056bdb72deSSage Weil 
1006a22285a6SYan, Zheng 	btrfs_i_size_write(parent_inode, parent_inode->i_size +
1007a22285a6SYan, Zheng 					 dentry->d_name.len * 2);
10086bdb72deSSage Weil 	ret = btrfs_update_inode(trans, parent_root, parent_inode);
10096bdb72deSSage Weil 	BUG_ON(ret);
10106bdb72deSSage Weil 
1011a4abeea4SJosef Bacik 	btrfs_record_root_in_trans(trans, root);
10126bdb72deSSage Weil 	btrfs_set_root_last_snapshot(&root->root_item, trans->transid);
10136bdb72deSSage Weil 	memcpy(new_root_item, &root->root_item, sizeof(*new_root_item));
101408fe4db1SLi Zefan 	btrfs_check_and_init_root_item(new_root_item);
10156bdb72deSSage Weil 
1016b83cc969SLi Zefan 	root_flags = btrfs_root_flags(new_root_item);
1017b83cc969SLi Zefan 	if (pending->readonly)
1018b83cc969SLi Zefan 		root_flags |= BTRFS_ROOT_SUBVOL_RDONLY;
1019b83cc969SLi Zefan 	else
1020b83cc969SLi Zefan 		root_flags &= ~BTRFS_ROOT_SUBVOL_RDONLY;
1021b83cc969SLi Zefan 	btrfs_set_root_flags(new_root_item, root_flags);
1022b83cc969SLi Zefan 
1023925baeddSChris Mason 	old = btrfs_lock_root_node(root);
10249fa8cfe7SChris Mason 	btrfs_cow_block(trans, root, old, NULL, 0, &old);
10255d4f98a2SYan Zheng 	btrfs_set_lock_blocking(old);
10263063d29fSChris Mason 
1027925baeddSChris Mason 	btrfs_copy_root(trans, root, old, &tmp, objectid);
1028925baeddSChris Mason 	btrfs_tree_unlock(old);
1029925baeddSChris Mason 	free_extent_buffer(old);
10303063d29fSChris Mason 
10315d4f98a2SYan Zheng 	btrfs_set_root_node(new_root_item, tmp);
1032a22285a6SYan, Zheng 	/* record when the snapshot was created in key.offset */
1033a22285a6SYan, Zheng 	key.offset = trans->transid;
1034a22285a6SYan, Zheng 	ret = btrfs_insert_root(trans, tree_root, &key, new_root_item);
1035925baeddSChris Mason 	btrfs_tree_unlock(tmp);
10363063d29fSChris Mason 	free_extent_buffer(tmp);
10370660b5afSChris Mason 	BUG_ON(ret);
10380660b5afSChris Mason 
1039a22285a6SYan, Zheng 	/*
1040a22285a6SYan, Zheng 	 * insert root back/forward references
1041a22285a6SYan, Zheng 	 */
1042a22285a6SYan, Zheng 	ret = btrfs_add_root_ref(trans, tree_root, objectid,
1043a22285a6SYan, Zheng 				 parent_root->root_key.objectid,
1044a22285a6SYan, Zheng 				 parent_inode->i_ino, index,
1045a22285a6SYan, Zheng 				 dentry->d_name.name, dentry->d_name.len);
1046a22285a6SYan, Zheng 	BUG_ON(ret);
10476a912213SJosef Bacik 	dput(parent);
1048a22285a6SYan, Zheng 
1049a22285a6SYan, Zheng 	key.offset = (u64)-1;
1050a22285a6SYan, Zheng 	pending->snap = btrfs_read_fs_root_no_name(root->fs_info, &key);
1051a22285a6SYan, Zheng 	BUG_ON(IS_ERR(pending->snap));
1052d68fc57bSYan, Zheng 
10533fd0a558SYan, Zheng 	btrfs_reloc_post_snapshot(trans, pending);
1054d68fc57bSYan, Zheng 	btrfs_orphan_post_snapshot(trans, pending);
10553063d29fSChris Mason fail:
10566bdb72deSSage Weil 	kfree(new_root_item);
1057a22285a6SYan, Zheng 	btrfs_block_rsv_release(root, &pending->block_rsv, (u64)-1);
1058a22285a6SYan, Zheng 	return 0;
10593063d29fSChris Mason }
10603063d29fSChris Mason 
1061d352ac68SChris Mason /*
1062d352ac68SChris Mason  * create all the snapshots we've scheduled for creation
1063d352ac68SChris Mason  */
106480b6794dSChris Mason static noinline int create_pending_snapshots(struct btrfs_trans_handle *trans,
10653063d29fSChris Mason 					     struct btrfs_fs_info *fs_info)
10663063d29fSChris Mason {
10673063d29fSChris Mason 	struct btrfs_pending_snapshot *pending;
10683063d29fSChris Mason 	struct list_head *head = &trans->transaction->pending_snapshots;
10693de4586cSChris Mason 	int ret;
10703de4586cSChris Mason 
1071c6e30871SQinghuang Feng 	list_for_each_entry(pending, head, list) {
10723de4586cSChris Mason 		ret = create_pending_snapshot(trans, fs_info, pending);
10733de4586cSChris Mason 		BUG_ON(ret);
10743de4586cSChris Mason 	}
10753de4586cSChris Mason 	return 0;
10763de4586cSChris Mason }
10773de4586cSChris Mason 
10785d4f98a2SYan Zheng static void update_super_roots(struct btrfs_root *root)
10795d4f98a2SYan Zheng {
10805d4f98a2SYan Zheng 	struct btrfs_root_item *root_item;
10815d4f98a2SYan Zheng 	struct btrfs_super_block *super;
10825d4f98a2SYan Zheng 
10835d4f98a2SYan Zheng 	super = &root->fs_info->super_copy;
10845d4f98a2SYan Zheng 
10855d4f98a2SYan Zheng 	root_item = &root->fs_info->chunk_root->root_item;
10865d4f98a2SYan Zheng 	super->chunk_root = root_item->bytenr;
10875d4f98a2SYan Zheng 	super->chunk_root_generation = root_item->generation;
10885d4f98a2SYan Zheng 	super->chunk_root_level = root_item->level;
10895d4f98a2SYan Zheng 
10905d4f98a2SYan Zheng 	root_item = &root->fs_info->tree_root->root_item;
10915d4f98a2SYan Zheng 	super->root = root_item->bytenr;
10925d4f98a2SYan Zheng 	super->generation = root_item->generation;
10935d4f98a2SYan Zheng 	super->root_level = root_item->level;
10940af3d00bSJosef Bacik 	if (super->cache_generation != 0 || btrfs_test_opt(root, SPACE_CACHE))
10950af3d00bSJosef Bacik 		super->cache_generation = root_item->generation;
10965d4f98a2SYan Zheng }
10975d4f98a2SYan Zheng 
1098f36f3042SChris Mason int btrfs_transaction_in_commit(struct btrfs_fs_info *info)
1099f36f3042SChris Mason {
1100f36f3042SChris Mason 	int ret = 0;
1101a4abeea4SJosef Bacik 	spin_lock(&info->trans_lock);
1102f36f3042SChris Mason 	if (info->running_transaction)
1103f36f3042SChris Mason 		ret = info->running_transaction->in_commit;
1104a4abeea4SJosef Bacik 	spin_unlock(&info->trans_lock);
1105f36f3042SChris Mason 	return ret;
1106f36f3042SChris Mason }
1107f36f3042SChris Mason 
11088929ecfaSYan, Zheng int btrfs_transaction_blocked(struct btrfs_fs_info *info)
11098929ecfaSYan, Zheng {
11108929ecfaSYan, Zheng 	int ret = 0;
1111a4abeea4SJosef Bacik 	spin_lock(&info->trans_lock);
11128929ecfaSYan, Zheng 	if (info->running_transaction)
11138929ecfaSYan, Zheng 		ret = info->running_transaction->blocked;
1114a4abeea4SJosef Bacik 	spin_unlock(&info->trans_lock);
11158929ecfaSYan, Zheng 	return ret;
11168929ecfaSYan, Zheng }
11178929ecfaSYan, Zheng 
1118bb9c12c9SSage Weil /*
1119bb9c12c9SSage Weil  * wait for the current transaction commit to start and block subsequent
1120bb9c12c9SSage Weil  * transaction joins
1121bb9c12c9SSage Weil  */
1122bb9c12c9SSage Weil static void wait_current_trans_commit_start(struct btrfs_root *root,
1123bb9c12c9SSage Weil 					    struct btrfs_transaction *trans)
1124bb9c12c9SSage Weil {
1125bb9c12c9SSage Weil 	DEFINE_WAIT(wait);
1126bb9c12c9SSage Weil 
1127bb9c12c9SSage Weil 	if (trans->in_commit)
1128bb9c12c9SSage Weil 		return;
1129bb9c12c9SSage Weil 
1130bb9c12c9SSage Weil 	while (1) {
1131bb9c12c9SSage Weil 		prepare_to_wait(&root->fs_info->transaction_blocked_wait, &wait,
1132bb9c12c9SSage Weil 				TASK_UNINTERRUPTIBLE);
1133bb9c12c9SSage Weil 		if (trans->in_commit) {
1134bb9c12c9SSage Weil 			finish_wait(&root->fs_info->transaction_blocked_wait,
1135bb9c12c9SSage Weil 				    &wait);
1136bb9c12c9SSage Weil 			break;
1137bb9c12c9SSage Weil 		}
1138bb9c12c9SSage Weil 		schedule();
1139bb9c12c9SSage Weil 		finish_wait(&root->fs_info->transaction_blocked_wait, &wait);
1140bb9c12c9SSage Weil 	}
1141bb9c12c9SSage Weil }
1142bb9c12c9SSage Weil 
1143bb9c12c9SSage Weil /*
1144bb9c12c9SSage Weil  * wait for the current transaction to start and then become unblocked.
1145bb9c12c9SSage Weil  * caller holds ref.
1146bb9c12c9SSage Weil  */
1147bb9c12c9SSage Weil static void wait_current_trans_commit_start_and_unblock(struct btrfs_root *root,
1148bb9c12c9SSage Weil 					 struct btrfs_transaction *trans)
1149bb9c12c9SSage Weil {
1150bb9c12c9SSage Weil 	DEFINE_WAIT(wait);
1151bb9c12c9SSage Weil 
1152bb9c12c9SSage Weil 	if (trans->commit_done || (trans->in_commit && !trans->blocked))
1153bb9c12c9SSage Weil 		return;
1154bb9c12c9SSage Weil 
1155bb9c12c9SSage Weil 	while (1) {
1156bb9c12c9SSage Weil 		prepare_to_wait(&root->fs_info->transaction_wait, &wait,
1157bb9c12c9SSage Weil 				TASK_UNINTERRUPTIBLE);
1158bb9c12c9SSage Weil 		if (trans->commit_done ||
1159bb9c12c9SSage Weil 		    (trans->in_commit && !trans->blocked)) {
1160bb9c12c9SSage Weil 			finish_wait(&root->fs_info->transaction_wait,
1161bb9c12c9SSage Weil 				    &wait);
1162bb9c12c9SSage Weil 			break;
1163bb9c12c9SSage Weil 		}
1164bb9c12c9SSage Weil 		schedule();
1165bb9c12c9SSage Weil 		finish_wait(&root->fs_info->transaction_wait,
1166bb9c12c9SSage Weil 			    &wait);
1167bb9c12c9SSage Weil 	}
1168bb9c12c9SSage Weil }
1169bb9c12c9SSage Weil 
1170bb9c12c9SSage Weil /*
1171bb9c12c9SSage Weil  * commit transactions asynchronously. once btrfs_commit_transaction_async
1172bb9c12c9SSage Weil  * returns, any subsequent transaction will not be allowed to join.
1173bb9c12c9SSage Weil  */
1174bb9c12c9SSage Weil struct btrfs_async_commit {
1175bb9c12c9SSage Weil 	struct btrfs_trans_handle *newtrans;
1176bb9c12c9SSage Weil 	struct btrfs_root *root;
1177bb9c12c9SSage Weil 	struct delayed_work work;
1178bb9c12c9SSage Weil };
1179bb9c12c9SSage Weil 
1180bb9c12c9SSage Weil static void do_async_commit(struct work_struct *work)
1181bb9c12c9SSage Weil {
1182bb9c12c9SSage Weil 	struct btrfs_async_commit *ac =
1183bb9c12c9SSage Weil 		container_of(work, struct btrfs_async_commit, work.work);
1184bb9c12c9SSage Weil 
1185bb9c12c9SSage Weil 	btrfs_commit_transaction(ac->newtrans, ac->root);
1186bb9c12c9SSage Weil 	kfree(ac);
1187bb9c12c9SSage Weil }
1188bb9c12c9SSage Weil 
1189bb9c12c9SSage Weil int btrfs_commit_transaction_async(struct btrfs_trans_handle *trans,
1190bb9c12c9SSage Weil 				   struct btrfs_root *root,
1191bb9c12c9SSage Weil 				   int wait_for_unblock)
1192bb9c12c9SSage Weil {
1193bb9c12c9SSage Weil 	struct btrfs_async_commit *ac;
1194bb9c12c9SSage Weil 	struct btrfs_transaction *cur_trans;
1195bb9c12c9SSage Weil 
1196bb9c12c9SSage Weil 	ac = kmalloc(sizeof(*ac), GFP_NOFS);
1197db5b493aSTsutomu Itoh 	if (!ac)
1198db5b493aSTsutomu Itoh 		return -ENOMEM;
1199bb9c12c9SSage Weil 
1200bb9c12c9SSage Weil 	INIT_DELAYED_WORK(&ac->work, do_async_commit);
1201bb9c12c9SSage Weil 	ac->root = root;
12027a7eaa40SJosef Bacik 	ac->newtrans = btrfs_join_transaction(root);
12033612b495STsutomu Itoh 	if (IS_ERR(ac->newtrans)) {
12043612b495STsutomu Itoh 		int err = PTR_ERR(ac->newtrans);
12053612b495STsutomu Itoh 		kfree(ac);
12063612b495STsutomu Itoh 		return err;
12073612b495STsutomu Itoh 	}
1208bb9c12c9SSage Weil 
1209bb9c12c9SSage Weil 	/* take transaction reference */
1210bb9c12c9SSage Weil 	cur_trans = trans->transaction;
121113c5a93eSJosef Bacik 	atomic_inc(&cur_trans->use_count);
1212bb9c12c9SSage Weil 
1213bb9c12c9SSage Weil 	btrfs_end_transaction(trans, root);
1214bb9c12c9SSage Weil 	schedule_delayed_work(&ac->work, 0);
1215bb9c12c9SSage Weil 
1216bb9c12c9SSage Weil 	/* wait for transaction to start and unblock */
1217bb9c12c9SSage Weil 	if (wait_for_unblock)
1218bb9c12c9SSage Weil 		wait_current_trans_commit_start_and_unblock(root, cur_trans);
1219bb9c12c9SSage Weil 	else
1220bb9c12c9SSage Weil 		wait_current_trans_commit_start(root, cur_trans);
1221bb9c12c9SSage Weil 	put_transaction(cur_trans);
1222bb9c12c9SSage Weil 
1223bb9c12c9SSage Weil 	return 0;
1224bb9c12c9SSage Weil }
1225bb9c12c9SSage Weil 
1226bb9c12c9SSage Weil /*
1227bb9c12c9SSage Weil  * btrfs_transaction state sequence:
1228bb9c12c9SSage Weil  *    in_commit = 0, blocked = 0  (initial)
1229bb9c12c9SSage Weil  *    in_commit = 1, blocked = 1
1230bb9c12c9SSage Weil  *    blocked = 0
1231bb9c12c9SSage Weil  *    commit_done = 1
1232bb9c12c9SSage Weil  */
123379154b1bSChris Mason int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
123479154b1bSChris Mason 			     struct btrfs_root *root)
123579154b1bSChris Mason {
123615ee9bc7SJosef Bacik 	unsigned long joined = 0;
123779154b1bSChris Mason 	struct btrfs_transaction *cur_trans;
12388fd17795SChris Mason 	struct btrfs_transaction *prev_trans = NULL;
123979154b1bSChris Mason 	DEFINE_WAIT(wait);
124015ee9bc7SJosef Bacik 	int ret;
124189573b9cSChris Mason 	int should_grow = 0;
124289573b9cSChris Mason 	unsigned long now = get_seconds();
1243dccae999SSage Weil 	int flush_on_commit = btrfs_test_opt(root, FLUSHONCOMMIT);
124479154b1bSChris Mason 
12455a3f23d5SChris Mason 	btrfs_run_ordered_operations(root, 0);
12465a3f23d5SChris Mason 
124756bec294SChris Mason 	/* make a pass through all the delayed refs we have so far
124856bec294SChris Mason 	 * any runnings procs may add more while we are here
124956bec294SChris Mason 	 */
125056bec294SChris Mason 	ret = btrfs_run_delayed_refs(trans, root, 0);
125156bec294SChris Mason 	BUG_ON(ret);
125256bec294SChris Mason 
1253a22285a6SYan, Zheng 	btrfs_trans_release_metadata(trans, root);
1254a22285a6SYan, Zheng 
1255b7ec40d7SChris Mason 	cur_trans = trans->transaction;
125656bec294SChris Mason 	/*
125756bec294SChris Mason 	 * set the flushing flag so procs in this transaction have to
125856bec294SChris Mason 	 * start sending their work down.
125956bec294SChris Mason 	 */
1260b7ec40d7SChris Mason 	cur_trans->delayed_refs.flushing = 1;
126156bec294SChris Mason 
1262c3e69d58SChris Mason 	ret = btrfs_run_delayed_refs(trans, root, 0);
126356bec294SChris Mason 	BUG_ON(ret);
126456bec294SChris Mason 
1265a4abeea4SJosef Bacik 	spin_lock(&cur_trans->commit_lock);
1266b7ec40d7SChris Mason 	if (cur_trans->in_commit) {
1267a4abeea4SJosef Bacik 		spin_unlock(&cur_trans->commit_lock);
126813c5a93eSJosef Bacik 		atomic_inc(&cur_trans->use_count);
126979154b1bSChris Mason 		btrfs_end_transaction(trans, root);
1270ccd467d6SChris Mason 
127179154b1bSChris Mason 		ret = wait_for_commit(root, cur_trans);
127279154b1bSChris Mason 		BUG_ON(ret);
127315ee9bc7SJosef Bacik 
127479154b1bSChris Mason 		put_transaction(cur_trans);
127515ee9bc7SJosef Bacik 
127679154b1bSChris Mason 		return 0;
127779154b1bSChris Mason 	}
12784313b399SChris Mason 
12792c90e5d6SChris Mason 	trans->transaction->in_commit = 1;
1280f9295749SChris Mason 	trans->transaction->blocked = 1;
1281a4abeea4SJosef Bacik 	spin_unlock(&cur_trans->commit_lock);
1282bb9c12c9SSage Weil 	wake_up(&root->fs_info->transaction_blocked_wait);
1283bb9c12c9SSage Weil 
1284a4abeea4SJosef Bacik 	spin_lock(&root->fs_info->trans_lock);
1285ccd467d6SChris Mason 	if (cur_trans->list.prev != &root->fs_info->trans_list) {
1286ccd467d6SChris Mason 		prev_trans = list_entry(cur_trans->list.prev,
1287ccd467d6SChris Mason 					struct btrfs_transaction, list);
1288ccd467d6SChris Mason 		if (!prev_trans->commit_done) {
128913c5a93eSJosef Bacik 			atomic_inc(&prev_trans->use_count);
1290a4abeea4SJosef Bacik 			spin_unlock(&root->fs_info->trans_lock);
1291ccd467d6SChris Mason 
1292ccd467d6SChris Mason 			wait_for_commit(root, prev_trans);
1293ccd467d6SChris Mason 
129415ee9bc7SJosef Bacik 			put_transaction(prev_trans);
1295a4abeea4SJosef Bacik 		} else {
1296a4abeea4SJosef Bacik 			spin_unlock(&root->fs_info->trans_lock);
1297ccd467d6SChris Mason 		}
1298a4abeea4SJosef Bacik 	} else {
1299a4abeea4SJosef Bacik 		spin_unlock(&root->fs_info->trans_lock);
1300ccd467d6SChris Mason 	}
130115ee9bc7SJosef Bacik 
130289573b9cSChris Mason 	if (now < cur_trans->start_time || now - cur_trans->start_time < 1)
130389573b9cSChris Mason 		should_grow = 1;
130489573b9cSChris Mason 
130515ee9bc7SJosef Bacik 	do {
13067ea394f1SYan Zheng 		int snap_pending = 0;
1307a4abeea4SJosef Bacik 
130815ee9bc7SJosef Bacik 		joined = cur_trans->num_joined;
13097ea394f1SYan Zheng 		if (!list_empty(&trans->transaction->pending_snapshots))
13107ea394f1SYan Zheng 			snap_pending = 1;
13117ea394f1SYan Zheng 
13122c90e5d6SChris Mason 		WARN_ON(cur_trans != trans->transaction);
131315ee9bc7SJosef Bacik 
13140bdb1db2SSage Weil 		if (flush_on_commit || snap_pending) {
131524bbcf04SYan, Zheng 			btrfs_start_delalloc_inodes(root, 1);
131624bbcf04SYan, Zheng 			ret = btrfs_wait_ordered_extents(root, 0, 1);
1317ebecd3d9SSage Weil 			BUG_ON(ret);
13187ea394f1SYan Zheng 		}
13197ea394f1SYan Zheng 
13205a3f23d5SChris Mason 		/*
13215a3f23d5SChris Mason 		 * rename don't use btrfs_join_transaction, so, once we
13225a3f23d5SChris Mason 		 * set the transaction to blocked above, we aren't going
13235a3f23d5SChris Mason 		 * to get any new ordered operations.  We can safely run
13245a3f23d5SChris Mason 		 * it here and no for sure that nothing new will be added
13255a3f23d5SChris Mason 		 * to the list
13265a3f23d5SChris Mason 		 */
13275a3f23d5SChris Mason 		btrfs_run_ordered_operations(root, 1);
13285a3f23d5SChris Mason 
1329ed3b3d31SChris Mason 		prepare_to_wait(&cur_trans->writer_wait, &wait,
1330ed3b3d31SChris Mason 				TASK_UNINTERRUPTIBLE);
1331ed3b3d31SChris Mason 
133213c5a93eSJosef Bacik 		if (atomic_read(&cur_trans->num_writers) > 1)
133399d16cbcSSage Weil 			schedule_timeout(MAX_SCHEDULE_TIMEOUT);
133499d16cbcSSage Weil 		else if (should_grow)
133599d16cbcSSage Weil 			schedule_timeout(1);
133615ee9bc7SJosef Bacik 
133715ee9bc7SJosef Bacik 		finish_wait(&cur_trans->writer_wait, &wait);
1338a4abeea4SJosef Bacik 		spin_lock(&root->fs_info->trans_lock);
1339a4abeea4SJosef Bacik 		root->fs_info->trans_no_join = 1;
1340a4abeea4SJosef Bacik 		spin_unlock(&root->fs_info->trans_lock);
134113c5a93eSJosef Bacik 	} while (atomic_read(&cur_trans->num_writers) > 1 ||
134289573b9cSChris Mason 		 (should_grow && cur_trans->num_joined != joined));
134315ee9bc7SJosef Bacik 
13443063d29fSChris Mason 	ret = create_pending_snapshots(trans, root->fs_info);
13453063d29fSChris Mason 	BUG_ON(ret);
13463063d29fSChris Mason 
134756bec294SChris Mason 	ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
134856bec294SChris Mason 	BUG_ON(ret);
134956bec294SChris Mason 
13502c90e5d6SChris Mason 	WARN_ON(cur_trans != trans->transaction);
1351dc17ff8fSChris Mason 
1352e02119d5SChris Mason 	/* btrfs_commit_tree_roots is responsible for getting the
1353e02119d5SChris Mason 	 * various roots consistent with each other.  Every pointer
1354e02119d5SChris Mason 	 * in the tree of tree roots has to point to the most up to date
1355e02119d5SChris Mason 	 * root for every subvolume and other tree.  So, we have to keep
1356e02119d5SChris Mason 	 * the tree logging code from jumping in and changing any
1357e02119d5SChris Mason 	 * of the trees.
1358e02119d5SChris Mason 	 *
1359e02119d5SChris Mason 	 * At this point in the commit, there can't be any tree-log
1360e02119d5SChris Mason 	 * writers, but a little lower down we drop the trans mutex
1361e02119d5SChris Mason 	 * and let new people in.  By holding the tree_log_mutex
1362e02119d5SChris Mason 	 * from now until after the super is written, we avoid races
1363e02119d5SChris Mason 	 * with the tree-log code.
1364e02119d5SChris Mason 	 */
1365e02119d5SChris Mason 	mutex_lock(&root->fs_info->tree_log_mutex);
13661a40e23bSZheng Yan 
13675d4f98a2SYan Zheng 	ret = commit_fs_roots(trans, root);
136854aa1f4dSChris Mason 	BUG_ON(ret);
136954aa1f4dSChris Mason 
13705d4f98a2SYan Zheng 	/* commit_fs_roots gets rid of all the tree log roots, it is now
1371e02119d5SChris Mason 	 * safe to free the root of tree log roots
1372e02119d5SChris Mason 	 */
1373e02119d5SChris Mason 	btrfs_free_log_root_tree(trans, root->fs_info);
1374e02119d5SChris Mason 
13755d4f98a2SYan Zheng 	ret = commit_cowonly_roots(trans, root);
137679154b1bSChris Mason 	BUG_ON(ret);
137754aa1f4dSChris Mason 
137811833d66SYan Zheng 	btrfs_prepare_extent_commit(trans, root);
137911833d66SYan Zheng 
138078fae27eSChris Mason 	cur_trans = root->fs_info->running_transaction;
13815f39d397SChris Mason 
13825d4f98a2SYan Zheng 	btrfs_set_root_node(&root->fs_info->tree_root->root_item,
13835d4f98a2SYan Zheng 			    root->fs_info->tree_root->node);
1384817d52f8SJosef Bacik 	switch_commit_root(root->fs_info->tree_root);
13855d4f98a2SYan Zheng 
13865d4f98a2SYan Zheng 	btrfs_set_root_node(&root->fs_info->chunk_root->root_item,
13875d4f98a2SYan Zheng 			    root->fs_info->chunk_root->node);
1388817d52f8SJosef Bacik 	switch_commit_root(root->fs_info->chunk_root);
13895d4f98a2SYan Zheng 
13905d4f98a2SYan Zheng 	update_super_roots(root);
1391e02119d5SChris Mason 
1392e02119d5SChris Mason 	if (!root->fs_info->log_root_recovering) {
1393e02119d5SChris Mason 		btrfs_set_super_log_root(&root->fs_info->super_copy, 0);
1394e02119d5SChris Mason 		btrfs_set_super_log_root_level(&root->fs_info->super_copy, 0);
1395e02119d5SChris Mason 	}
1396e02119d5SChris Mason 
1397a061fc8dSChris Mason 	memcpy(&root->fs_info->super_for_commit, &root->fs_info->super_copy,
13984b52dff6SChris Mason 	       sizeof(root->fs_info->super_copy));
1399ccd467d6SChris Mason 
1400f9295749SChris Mason 	trans->transaction->blocked = 0;
1401a4abeea4SJosef Bacik 	spin_lock(&root->fs_info->trans_lock);
1402a4abeea4SJosef Bacik 	root->fs_info->running_transaction = NULL;
1403a4abeea4SJosef Bacik 	root->fs_info->trans_no_join = 0;
1404a4abeea4SJosef Bacik 	spin_unlock(&root->fs_info->trans_lock);
1405b7ec40d7SChris Mason 
1406f9295749SChris Mason 	wake_up(&root->fs_info->transaction_wait);
1407e6dcd2dcSChris Mason 
140879154b1bSChris Mason 	ret = btrfs_write_and_wait_transaction(trans, root);
140979154b1bSChris Mason 	BUG_ON(ret);
1410a512bbf8SYan Zheng 	write_ctree_super(trans, root, 0);
14114313b399SChris Mason 
1412e02119d5SChris Mason 	/*
1413e02119d5SChris Mason 	 * the super is written, we can safely allow the tree-loggers
1414e02119d5SChris Mason 	 * to go about their business
1415e02119d5SChris Mason 	 */
1416e02119d5SChris Mason 	mutex_unlock(&root->fs_info->tree_log_mutex);
1417e02119d5SChris Mason 
141811833d66SYan Zheng 	btrfs_finish_extent_commit(trans, root);
14194313b399SChris Mason 
14202c90e5d6SChris Mason 	cur_trans->commit_done = 1;
1421b7ec40d7SChris Mason 
142215ee9bc7SJosef Bacik 	root->fs_info->last_trans_committed = cur_trans->transid;
1423817d52f8SJosef Bacik 
14242c90e5d6SChris Mason 	wake_up(&cur_trans->commit_wait);
14253de4586cSChris Mason 
1426a4abeea4SJosef Bacik 	spin_lock(&root->fs_info->trans_lock);
142713c5a93eSJosef Bacik 	list_del_init(&cur_trans->list);
1428a4abeea4SJosef Bacik 	spin_unlock(&root->fs_info->trans_lock);
1429a4abeea4SJosef Bacik 
143079154b1bSChris Mason 	put_transaction(cur_trans);
143178fae27eSChris Mason 	put_transaction(cur_trans);
143258176a96SJosef Bacik 
14331abe9b8aSliubo 	trace_btrfs_transaction_commit(root);
14341abe9b8aSliubo 
14359ed74f2dSJosef Bacik 	if (current->journal_info == trans)
14369ed74f2dSJosef Bacik 		current->journal_info = NULL;
14379ed74f2dSJosef Bacik 
14382c90e5d6SChris Mason 	kmem_cache_free(btrfs_trans_handle_cachep, trans);
143924bbcf04SYan, Zheng 
144024bbcf04SYan, Zheng 	if (current != root->fs_info->transaction_kthread)
144124bbcf04SYan, Zheng 		btrfs_run_delayed_iputs(root);
144224bbcf04SYan, Zheng 
144379154b1bSChris Mason 	return ret;
144479154b1bSChris Mason }
144579154b1bSChris Mason 
1446d352ac68SChris Mason /*
1447d352ac68SChris Mason  * interface function to delete all the snapshots we have scheduled for deletion
1448d352ac68SChris Mason  */
1449e9d0b13bSChris Mason int btrfs_clean_old_snapshots(struct btrfs_root *root)
1450e9d0b13bSChris Mason {
14515d4f98a2SYan Zheng 	LIST_HEAD(list);
14525d4f98a2SYan Zheng 	struct btrfs_fs_info *fs_info = root->fs_info;
1453e9d0b13bSChris Mason 
1454a4abeea4SJosef Bacik 	spin_lock(&fs_info->trans_lock);
14555d4f98a2SYan Zheng 	list_splice_init(&fs_info->dead_roots, &list);
1456a4abeea4SJosef Bacik 	spin_unlock(&fs_info->trans_lock);
14575d4f98a2SYan Zheng 
14585d4f98a2SYan Zheng 	while (!list_empty(&list)) {
14595d4f98a2SYan Zheng 		root = list_entry(list.next, struct btrfs_root, root_list);
146076dda93cSYan, Zheng 		list_del(&root->root_list);
146176dda93cSYan, Zheng 
146276dda93cSYan, Zheng 		if (btrfs_header_backref_rev(root->node) <
146376dda93cSYan, Zheng 		    BTRFS_MIXED_BACKREF_REV)
14643fd0a558SYan, Zheng 			btrfs_drop_snapshot(root, NULL, 0);
146576dda93cSYan, Zheng 		else
14663fd0a558SYan, Zheng 			btrfs_drop_snapshot(root, NULL, 1);
1467e9d0b13bSChris Mason 	}
1468e9d0b13bSChris Mason 	return 0;
1469e9d0b13bSChris Mason }
1470