xref: /openbmc/linux/fs/btrfs/transaction.c (revision 690587d1)
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>
2034088780SChris Mason #include <linux/sched.h>
21d3c2fdcfSChris Mason #include <linux/writeback.h>
225f39d397SChris Mason #include <linux/pagemap.h>
235f2cc086SChris Mason #include <linux/blkdev.h>
2479154b1bSChris Mason #include "ctree.h"
2579154b1bSChris Mason #include "disk-io.h"
2679154b1bSChris Mason #include "transaction.h"
27925baeddSChris Mason #include "locking.h"
28e02119d5SChris Mason #include "tree-log.h"
2979154b1bSChris Mason 
300f7d52f4SChris Mason #define BTRFS_ROOT_TRANS_TAG 0
310f7d52f4SChris Mason 
3280b6794dSChris Mason static noinline void put_transaction(struct btrfs_transaction *transaction)
3379154b1bSChris Mason {
342c90e5d6SChris Mason 	WARN_ON(transaction->use_count == 0);
3579154b1bSChris Mason 	transaction->use_count--;
3678fae27eSChris Mason 	if (transaction->use_count == 0) {
378fd17795SChris Mason 		list_del_init(&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  */
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);
5979154b1bSChris Mason 		BUG_ON(!cur_trans);
600f7d52f4SChris Mason 		root->fs_info->generation++;
6115ee9bc7SJosef Bacik 		cur_trans->num_writers = 1;
6215ee9bc7SJosef Bacik 		cur_trans->num_joined = 0;
630f7d52f4SChris Mason 		cur_trans->transid = root->fs_info->generation;
6479154b1bSChris Mason 		init_waitqueue_head(&cur_trans->writer_wait);
6579154b1bSChris Mason 		init_waitqueue_head(&cur_trans->commit_wait);
6679154b1bSChris Mason 		cur_trans->in_commit = 0;
67f9295749SChris Mason 		cur_trans->blocked = 0;
68d5719762SChris Mason 		cur_trans->use_count = 1;
6979154b1bSChris Mason 		cur_trans->commit_done = 0;
7008607c1bSChris Mason 		cur_trans->start_time = get_seconds();
7156bec294SChris Mason 
7256bec294SChris Mason 		cur_trans->delayed_refs.root.rb_node = NULL;
7356bec294SChris Mason 		cur_trans->delayed_refs.num_entries = 0;
74c3e69d58SChris Mason 		cur_trans->delayed_refs.num_heads_ready = 0;
75c3e69d58SChris Mason 		cur_trans->delayed_refs.num_heads = 0;
7656bec294SChris Mason 		cur_trans->delayed_refs.flushing = 0;
77c3e69d58SChris Mason 		cur_trans->delayed_refs.run_delayed_start = 0;
7856bec294SChris Mason 		spin_lock_init(&cur_trans->delayed_refs.lock);
7956bec294SChris Mason 
803063d29fSChris Mason 		INIT_LIST_HEAD(&cur_trans->pending_snapshots);
818fd17795SChris Mason 		list_add_tail(&cur_trans->list, &root->fs_info->trans_list);
82d1310b2eSChris Mason 		extent_io_tree_init(&cur_trans->dirty_pages,
835f39d397SChris Mason 				     root->fs_info->btree_inode->i_mapping,
845f39d397SChris Mason 				     GFP_NOFS);
8548ec2cf8SChris Mason 		spin_lock(&root->fs_info->new_trans_lock);
8648ec2cf8SChris Mason 		root->fs_info->running_transaction = cur_trans;
8748ec2cf8SChris Mason 		spin_unlock(&root->fs_info->new_trans_lock);
8815ee9bc7SJosef Bacik 	} else {
8979154b1bSChris Mason 		cur_trans->num_writers++;
9015ee9bc7SJosef Bacik 		cur_trans->num_joined++;
9115ee9bc7SJosef Bacik 	}
9215ee9bc7SJosef Bacik 
9379154b1bSChris Mason 	return 0;
9479154b1bSChris Mason }
9579154b1bSChris Mason 
96d352ac68SChris Mason /*
97d397712bSChris Mason  * this does all the record keeping required to make sure that a reference
98d397712bSChris Mason  * counted root is properly recorded in a given transaction.  This is required
99d397712bSChris Mason  * to make sure the old root from before we joined the transaction is deleted
100d397712bSChris Mason  * when the transaction commits
101d352ac68SChris Mason  */
1025d4f98a2SYan Zheng static noinline int record_root_in_trans(struct btrfs_trans_handle *trans,
1035d4f98a2SYan Zheng 					 struct btrfs_root *root)
1046702ed49SChris Mason {
1055d4f98a2SYan Zheng 	if (root->ref_cows && root->last_trans < trans->transid) {
1066702ed49SChris Mason 		WARN_ON(root == root->fs_info->extent_root);
1075d4f98a2SYan Zheng 		WARN_ON(root->commit_root != root->node);
1085d4f98a2SYan Zheng 
1096702ed49SChris Mason 		radix_tree_tag_set(&root->fs_info->fs_roots_radix,
1106702ed49SChris Mason 			   (unsigned long)root->root_key.objectid,
1116702ed49SChris Mason 			   BTRFS_ROOT_TRANS_TAG);
1125d4f98a2SYan Zheng 		root->last_trans = trans->transid;
1135d4f98a2SYan Zheng 		btrfs_init_reloc_root(trans, root);
1146702ed49SChris Mason 	}
1155d4f98a2SYan Zheng 	return 0;
1166702ed49SChris Mason }
1175d4f98a2SYan Zheng 
1185d4f98a2SYan Zheng int btrfs_record_root_in_trans(struct btrfs_trans_handle *trans,
1195d4f98a2SYan Zheng 			       struct btrfs_root *root)
1205d4f98a2SYan Zheng {
1215d4f98a2SYan Zheng 	if (!root->ref_cows)
1225d4f98a2SYan Zheng 		return 0;
1235d4f98a2SYan Zheng 
1245d4f98a2SYan Zheng 	mutex_lock(&root->fs_info->trans_mutex);
1255d4f98a2SYan Zheng 	if (root->last_trans == trans->transid) {
1265d4f98a2SYan Zheng 		mutex_unlock(&root->fs_info->trans_mutex);
1275d4f98a2SYan Zheng 		return 0;
1285d4f98a2SYan Zheng 	}
1295d4f98a2SYan Zheng 
1305d4f98a2SYan Zheng 	record_root_in_trans(trans, root);
1315d4f98a2SYan Zheng 	mutex_unlock(&root->fs_info->trans_mutex);
1326702ed49SChris Mason 	return 0;
1336702ed49SChris Mason }
1346702ed49SChris Mason 
135d352ac68SChris Mason /* wait for commit against the current transaction to become unblocked
136d352ac68SChris Mason  * when this is done, it is safe to start a new transaction, but the current
137d352ac68SChris Mason  * transaction might not be fully on disk.
138d352ac68SChris Mason  */
13937d1aeeeSChris Mason static void wait_current_trans(struct btrfs_root *root)
14079154b1bSChris Mason {
141f9295749SChris Mason 	struct btrfs_transaction *cur_trans;
14279154b1bSChris Mason 
143f9295749SChris Mason 	cur_trans = root->fs_info->running_transaction;
14437d1aeeeSChris Mason 	if (cur_trans && cur_trans->blocked) {
145f9295749SChris Mason 		DEFINE_WAIT(wait);
146f9295749SChris Mason 		cur_trans->use_count++;
147f9295749SChris Mason 		while (1) {
148f9295749SChris Mason 			prepare_to_wait(&root->fs_info->transaction_wait, &wait,
149f9295749SChris Mason 					TASK_UNINTERRUPTIBLE);
150f9295749SChris Mason 			if (cur_trans->blocked) {
151f9295749SChris Mason 				mutex_unlock(&root->fs_info->trans_mutex);
152f9295749SChris Mason 				schedule();
153f9295749SChris Mason 				mutex_lock(&root->fs_info->trans_mutex);
154f9295749SChris Mason 				finish_wait(&root->fs_info->transaction_wait,
155f9295749SChris Mason 					    &wait);
156f9295749SChris Mason 			} else {
157f9295749SChris Mason 				finish_wait(&root->fs_info->transaction_wait,
158f9295749SChris Mason 					    &wait);
159f9295749SChris Mason 				break;
160f9295749SChris Mason 			}
161f9295749SChris Mason 		}
162f9295749SChris Mason 		put_transaction(cur_trans);
163f9295749SChris Mason 	}
16437d1aeeeSChris Mason }
16537d1aeeeSChris Mason 
166e02119d5SChris Mason static struct btrfs_trans_handle *start_transaction(struct btrfs_root *root,
1679ca9ee09SSage Weil 					     int num_blocks, int wait)
16837d1aeeeSChris Mason {
16937d1aeeeSChris Mason 	struct btrfs_trans_handle *h =
17037d1aeeeSChris Mason 		kmem_cache_alloc(btrfs_trans_handle_cachep, GFP_NOFS);
17137d1aeeeSChris Mason 	int ret;
17237d1aeeeSChris Mason 
17337d1aeeeSChris Mason 	mutex_lock(&root->fs_info->trans_mutex);
1744bef0848SChris Mason 	if (!root->fs_info->log_root_recovering &&
1754bef0848SChris Mason 	    ((wait == 1 && !root->fs_info->open_ioctl_trans) || wait == 2))
17637d1aeeeSChris Mason 		wait_current_trans(root);
17779154b1bSChris Mason 	ret = join_transaction(root);
17879154b1bSChris Mason 	BUG_ON(ret);
1790f7d52f4SChris Mason 
1806702ed49SChris Mason 	h->transid = root->fs_info->running_transaction->transid;
18179154b1bSChris Mason 	h->transaction = root->fs_info->running_transaction;
18279154b1bSChris Mason 	h->blocks_reserved = num_blocks;
18379154b1bSChris Mason 	h->blocks_used = 0;
184d2fb3437SYan Zheng 	h->block_group = 0;
18526b8003fSChris Mason 	h->alloc_exclude_nr = 0;
18626b8003fSChris Mason 	h->alloc_exclude_start = 0;
18756bec294SChris Mason 	h->delayed_ref_updates = 0;
188b7ec40d7SChris Mason 
1899ed74f2dSJosef Bacik 	if (!current->journal_info)
1909ed74f2dSJosef Bacik 		current->journal_info = h;
1919ed74f2dSJosef Bacik 
19279154b1bSChris Mason 	root->fs_info->running_transaction->use_count++;
1935d4f98a2SYan Zheng 	record_root_in_trans(h, root);
19479154b1bSChris Mason 	mutex_unlock(&root->fs_info->trans_mutex);
19579154b1bSChris Mason 	return h;
19679154b1bSChris Mason }
19779154b1bSChris Mason 
198f9295749SChris Mason struct btrfs_trans_handle *btrfs_start_transaction(struct btrfs_root *root,
199f9295749SChris Mason 						   int num_blocks)
200f9295749SChris Mason {
2019ca9ee09SSage Weil 	return start_transaction(root, num_blocks, 1);
202f9295749SChris Mason }
203f9295749SChris Mason struct btrfs_trans_handle *btrfs_join_transaction(struct btrfs_root *root,
204f9295749SChris Mason 						   int num_blocks)
205f9295749SChris Mason {
2069ca9ee09SSage Weil 	return start_transaction(root, num_blocks, 0);
207f9295749SChris Mason }
208f9295749SChris Mason 
2099ca9ee09SSage Weil struct btrfs_trans_handle *btrfs_start_ioctl_transaction(struct btrfs_root *r,
2109ca9ee09SSage Weil 							 int num_blocks)
2119ca9ee09SSage Weil {
2129ca9ee09SSage Weil 	return start_transaction(r, num_blocks, 2);
2139ca9ee09SSage Weil }
2149ca9ee09SSage Weil 
215d352ac68SChris Mason /* wait for a transaction commit to be fully complete */
21689ce8a63SChris Mason static noinline int wait_for_commit(struct btrfs_root *root,
21789ce8a63SChris Mason 				    struct btrfs_transaction *commit)
21889ce8a63SChris Mason {
21989ce8a63SChris Mason 	DEFINE_WAIT(wait);
22089ce8a63SChris Mason 	mutex_lock(&root->fs_info->trans_mutex);
22189ce8a63SChris Mason 	while (!commit->commit_done) {
22289ce8a63SChris Mason 		prepare_to_wait(&commit->commit_wait, &wait,
22389ce8a63SChris Mason 				TASK_UNINTERRUPTIBLE);
22489ce8a63SChris Mason 		if (commit->commit_done)
22589ce8a63SChris Mason 			break;
22689ce8a63SChris Mason 		mutex_unlock(&root->fs_info->trans_mutex);
22789ce8a63SChris Mason 		schedule();
22889ce8a63SChris Mason 		mutex_lock(&root->fs_info->trans_mutex);
22989ce8a63SChris Mason 	}
23089ce8a63SChris Mason 	mutex_unlock(&root->fs_info->trans_mutex);
23189ce8a63SChris Mason 	finish_wait(&commit->commit_wait, &wait);
23289ce8a63SChris Mason 	return 0;
23389ce8a63SChris Mason }
23489ce8a63SChris Mason 
2355d4f98a2SYan Zheng #if 0
236d352ac68SChris Mason /*
237d397712bSChris Mason  * rate limit against the drop_snapshot code.  This helps to slow down new
238d397712bSChris Mason  * operations if the drop_snapshot code isn't able to keep up.
239d352ac68SChris Mason  */
24037d1aeeeSChris Mason static void throttle_on_drops(struct btrfs_root *root)
241ab78c84dSChris Mason {
242ab78c84dSChris Mason 	struct btrfs_fs_info *info = root->fs_info;
2432dd3e67bSChris Mason 	int harder_count = 0;
244ab78c84dSChris Mason 
2452dd3e67bSChris Mason harder:
246ab78c84dSChris Mason 	if (atomic_read(&info->throttles)) {
247ab78c84dSChris Mason 		DEFINE_WAIT(wait);
248ab78c84dSChris Mason 		int thr;
249ab78c84dSChris Mason 		thr = atomic_read(&info->throttle_gen);
250ab78c84dSChris Mason 
251ab78c84dSChris Mason 		do {
252ab78c84dSChris Mason 			prepare_to_wait(&info->transaction_throttle,
253ab78c84dSChris Mason 					&wait, TASK_UNINTERRUPTIBLE);
254ab78c84dSChris Mason 			if (!atomic_read(&info->throttles)) {
255ab78c84dSChris Mason 				finish_wait(&info->transaction_throttle, &wait);
256ab78c84dSChris Mason 				break;
257ab78c84dSChris Mason 			}
258ab78c84dSChris Mason 			schedule();
259ab78c84dSChris Mason 			finish_wait(&info->transaction_throttle, &wait);
260ab78c84dSChris Mason 		} while (thr == atomic_read(&info->throttle_gen));
2612dd3e67bSChris Mason 		harder_count++;
2622dd3e67bSChris Mason 
2632dd3e67bSChris Mason 		if (root->fs_info->total_ref_cache_size > 1 * 1024 * 1024 &&
2642dd3e67bSChris Mason 		    harder_count < 2)
2652dd3e67bSChris Mason 			goto harder;
2662dd3e67bSChris Mason 
2672dd3e67bSChris Mason 		if (root->fs_info->total_ref_cache_size > 5 * 1024 * 1024 &&
2682dd3e67bSChris Mason 		    harder_count < 10)
2692dd3e67bSChris Mason 			goto harder;
2702dd3e67bSChris Mason 
2712dd3e67bSChris Mason 		if (root->fs_info->total_ref_cache_size > 10 * 1024 * 1024 &&
2722dd3e67bSChris Mason 		    harder_count < 20)
2732dd3e67bSChris Mason 			goto harder;
274ab78c84dSChris Mason 	}
275ab78c84dSChris Mason }
2765d4f98a2SYan Zheng #endif
277ab78c84dSChris Mason 
27837d1aeeeSChris Mason void btrfs_throttle(struct btrfs_root *root)
27937d1aeeeSChris Mason {
28037d1aeeeSChris Mason 	mutex_lock(&root->fs_info->trans_mutex);
2819ca9ee09SSage Weil 	if (!root->fs_info->open_ioctl_trans)
28237d1aeeeSChris Mason 		wait_current_trans(root);
28337d1aeeeSChris Mason 	mutex_unlock(&root->fs_info->trans_mutex);
28437d1aeeeSChris Mason }
28537d1aeeeSChris Mason 
28689ce8a63SChris Mason static int __btrfs_end_transaction(struct btrfs_trans_handle *trans,
28789ce8a63SChris Mason 			  struct btrfs_root *root, int throttle)
28879154b1bSChris Mason {
28979154b1bSChris Mason 	struct btrfs_transaction *cur_trans;
290ab78c84dSChris Mason 	struct btrfs_fs_info *info = root->fs_info;
291c3e69d58SChris Mason 	int count = 0;
292d6e4a428SChris Mason 
293c3e69d58SChris Mason 	while (count < 4) {
294c3e69d58SChris Mason 		unsigned long cur = trans->delayed_ref_updates;
295c3e69d58SChris Mason 		trans->delayed_ref_updates = 0;
296c3e69d58SChris Mason 		if (cur &&
297c3e69d58SChris Mason 		    trans->transaction->delayed_refs.num_heads_ready > 64) {
298c3e69d58SChris Mason 			trans->delayed_ref_updates = 0;
299b7ec40d7SChris Mason 
300b7ec40d7SChris Mason 			/*
301b7ec40d7SChris Mason 			 * do a full flush if the transaction is trying
302b7ec40d7SChris Mason 			 * to close
303b7ec40d7SChris Mason 			 */
304b7ec40d7SChris Mason 			if (trans->transaction->delayed_refs.flushing)
305b7ec40d7SChris Mason 				cur = 0;
306c3e69d58SChris Mason 			btrfs_run_delayed_refs(trans, root, cur);
307c3e69d58SChris Mason 		} else {
308c3e69d58SChris Mason 			break;
309c3e69d58SChris Mason 		}
310c3e69d58SChris Mason 		count++;
31156bec294SChris Mason 	}
31256bec294SChris Mason 
313ab78c84dSChris Mason 	mutex_lock(&info->trans_mutex);
314ab78c84dSChris Mason 	cur_trans = info->running_transaction;
315ccd467d6SChris Mason 	WARN_ON(cur_trans != trans->transaction);
316d5719762SChris Mason 	WARN_ON(cur_trans->num_writers < 1);
317ccd467d6SChris Mason 	cur_trans->num_writers--;
31889ce8a63SChris Mason 
31979154b1bSChris Mason 	if (waitqueue_active(&cur_trans->writer_wait))
32079154b1bSChris Mason 		wake_up(&cur_trans->writer_wait);
32179154b1bSChris Mason 	put_transaction(cur_trans);
322ab78c84dSChris Mason 	mutex_unlock(&info->trans_mutex);
3239ed74f2dSJosef Bacik 
3249ed74f2dSJosef Bacik 	if (current->journal_info == trans)
3259ed74f2dSJosef Bacik 		current->journal_info = NULL;
326d6025579SChris Mason 	memset(trans, 0, sizeof(*trans));
3272c90e5d6SChris Mason 	kmem_cache_free(btrfs_trans_handle_cachep, trans);
328ab78c84dSChris Mason 
32979154b1bSChris Mason 	return 0;
33079154b1bSChris Mason }
33179154b1bSChris Mason 
33289ce8a63SChris Mason int btrfs_end_transaction(struct btrfs_trans_handle *trans,
33389ce8a63SChris Mason 			  struct btrfs_root *root)
33489ce8a63SChris Mason {
33589ce8a63SChris Mason 	return __btrfs_end_transaction(trans, root, 0);
33689ce8a63SChris Mason }
33789ce8a63SChris Mason 
33889ce8a63SChris Mason int btrfs_end_transaction_throttle(struct btrfs_trans_handle *trans,
33989ce8a63SChris Mason 				   struct btrfs_root *root)
34089ce8a63SChris Mason {
34189ce8a63SChris Mason 	return __btrfs_end_transaction(trans, root, 1);
34289ce8a63SChris Mason }
34389ce8a63SChris Mason 
344d352ac68SChris Mason /*
345d352ac68SChris Mason  * when btree blocks are allocated, they have some corresponding bits set for
346d352ac68SChris Mason  * them in one of two extent_io trees.  This is used to make sure all of
347690587d1SChris Mason  * those extents are sent to disk but does not wait on them
348d352ac68SChris Mason  */
349690587d1SChris Mason int btrfs_write_marked_extents(struct btrfs_root *root,
350d0c803c4SChris Mason 			       struct extent_io_tree *dirty_pages)
35179154b1bSChris Mason {
3527c4452b9SChris Mason 	int ret;
353777e6bd7SChris Mason 	int err = 0;
3547c4452b9SChris Mason 	int werr = 0;
3557c4452b9SChris Mason 	struct page *page;
3567c4452b9SChris Mason 	struct inode *btree_inode = root->fs_info->btree_inode;
357777e6bd7SChris Mason 	u64 start = 0;
3585f39d397SChris Mason 	u64 end;
3595f39d397SChris Mason 	unsigned long index;
3607c4452b9SChris Mason 
3617c4452b9SChris Mason 	while (1) {
362777e6bd7SChris Mason 		ret = find_first_extent_bit(dirty_pages, start, &start, &end,
3635f39d397SChris Mason 					    EXTENT_DIRTY);
3645f39d397SChris Mason 		if (ret)
3657c4452b9SChris Mason 			break;
3665f39d397SChris Mason 		while (start <= end) {
367777e6bd7SChris Mason 			cond_resched();
368777e6bd7SChris Mason 
3695f39d397SChris Mason 			index = start >> PAGE_CACHE_SHIFT;
37035ebb934SChris Mason 			start = (u64)(index + 1) << PAGE_CACHE_SHIFT;
3714bef0848SChris Mason 			page = find_get_page(btree_inode->i_mapping, index);
3727c4452b9SChris Mason 			if (!page)
3737c4452b9SChris Mason 				continue;
3744bef0848SChris Mason 
3754bef0848SChris Mason 			btree_lock_page_hook(page);
3764bef0848SChris Mason 			if (!page->mapping) {
3774bef0848SChris Mason 				unlock_page(page);
3784bef0848SChris Mason 				page_cache_release(page);
3794bef0848SChris Mason 				continue;
3804bef0848SChris Mason 			}
3814bef0848SChris Mason 
3826702ed49SChris Mason 			if (PageWriteback(page)) {
3836702ed49SChris Mason 				if (PageDirty(page))
3846702ed49SChris Mason 					wait_on_page_writeback(page);
3856702ed49SChris Mason 				else {
3866702ed49SChris Mason 					unlock_page(page);
3876702ed49SChris Mason 					page_cache_release(page);
3886702ed49SChris Mason 					continue;
3896702ed49SChris Mason 				}
3906702ed49SChris Mason 			}
3917c4452b9SChris Mason 			err = write_one_page(page, 0);
3927c4452b9SChris Mason 			if (err)
3937c4452b9SChris Mason 				werr = err;
3947c4452b9SChris Mason 			page_cache_release(page);
3957c4452b9SChris Mason 		}
3967c4452b9SChris Mason 	}
397690587d1SChris Mason 	if (err)
398690587d1SChris Mason 		werr = err;
399690587d1SChris Mason 	return werr;
400690587d1SChris Mason }
401690587d1SChris Mason 
402690587d1SChris Mason /*
403690587d1SChris Mason  * when btree blocks are allocated, they have some corresponding bits set for
404690587d1SChris Mason  * them in one of two extent_io trees.  This is used to make sure all of
405690587d1SChris Mason  * those extents are on disk for transaction or log commit.  We wait
406690587d1SChris Mason  * on all the pages and clear them from the dirty pages state tree
407690587d1SChris Mason  */
408690587d1SChris Mason int btrfs_wait_marked_extents(struct btrfs_root *root,
409690587d1SChris Mason 			      struct extent_io_tree *dirty_pages)
410690587d1SChris Mason {
411690587d1SChris Mason 	int ret;
412690587d1SChris Mason 	int err = 0;
413690587d1SChris Mason 	int werr = 0;
414690587d1SChris Mason 	struct page *page;
415690587d1SChris Mason 	struct inode *btree_inode = root->fs_info->btree_inode;
416690587d1SChris Mason 	u64 start = 0;
417690587d1SChris Mason 	u64 end;
418690587d1SChris Mason 	unsigned long index;
419690587d1SChris Mason 
420777e6bd7SChris Mason 	while (1) {
421777e6bd7SChris Mason 		ret = find_first_extent_bit(dirty_pages, 0, &start, &end,
422777e6bd7SChris Mason 					    EXTENT_DIRTY);
423777e6bd7SChris Mason 		if (ret)
424777e6bd7SChris Mason 			break;
425777e6bd7SChris Mason 
426777e6bd7SChris Mason 		clear_extent_dirty(dirty_pages, start, end, GFP_NOFS);
427777e6bd7SChris Mason 		while (start <= end) {
428777e6bd7SChris Mason 			index = start >> PAGE_CACHE_SHIFT;
429777e6bd7SChris Mason 			start = (u64)(index + 1) << PAGE_CACHE_SHIFT;
430777e6bd7SChris Mason 			page = find_get_page(btree_inode->i_mapping, index);
431777e6bd7SChris Mason 			if (!page)
432777e6bd7SChris Mason 				continue;
433777e6bd7SChris Mason 			if (PageDirty(page)) {
4344bef0848SChris Mason 				btree_lock_page_hook(page);
4354bef0848SChris Mason 				wait_on_page_writeback(page);
436777e6bd7SChris Mason 				err = write_one_page(page, 0);
437777e6bd7SChris Mason 				if (err)
438777e6bd7SChris Mason 					werr = err;
439777e6bd7SChris Mason 			}
440777e6bd7SChris Mason 			wait_on_page_writeback(page);
441777e6bd7SChris Mason 			page_cache_release(page);
442777e6bd7SChris Mason 			cond_resched();
443777e6bd7SChris Mason 		}
444777e6bd7SChris Mason 	}
4457c4452b9SChris Mason 	if (err)
4467c4452b9SChris Mason 		werr = err;
4477c4452b9SChris Mason 	return werr;
44879154b1bSChris Mason }
44979154b1bSChris Mason 
450690587d1SChris Mason /*
451690587d1SChris Mason  * when btree blocks are allocated, they have some corresponding bits set for
452690587d1SChris Mason  * them in one of two extent_io trees.  This is used to make sure all of
453690587d1SChris Mason  * those extents are on disk for transaction or log commit
454690587d1SChris Mason  */
455690587d1SChris Mason int btrfs_write_and_wait_marked_extents(struct btrfs_root *root,
456690587d1SChris Mason 					struct extent_io_tree *dirty_pages)
457690587d1SChris Mason {
458690587d1SChris Mason 	int ret;
459690587d1SChris Mason 	int ret2;
460690587d1SChris Mason 
461690587d1SChris Mason 	ret = btrfs_write_marked_extents(root, dirty_pages);
462690587d1SChris Mason 	ret2 = btrfs_wait_marked_extents(root, dirty_pages);
463690587d1SChris Mason 	return ret || ret2;
464690587d1SChris Mason }
465690587d1SChris Mason 
466d0c803c4SChris Mason int btrfs_write_and_wait_transaction(struct btrfs_trans_handle *trans,
467d0c803c4SChris Mason 				     struct btrfs_root *root)
468d0c803c4SChris Mason {
469d0c803c4SChris Mason 	if (!trans || !trans->transaction) {
470d0c803c4SChris Mason 		struct inode *btree_inode;
471d0c803c4SChris Mason 		btree_inode = root->fs_info->btree_inode;
472d0c803c4SChris Mason 		return filemap_write_and_wait(btree_inode->i_mapping);
473d0c803c4SChris Mason 	}
474d0c803c4SChris Mason 	return btrfs_write_and_wait_marked_extents(root,
475d0c803c4SChris Mason 					   &trans->transaction->dirty_pages);
476d0c803c4SChris Mason }
477d0c803c4SChris Mason 
478d352ac68SChris Mason /*
479d352ac68SChris Mason  * this is used to update the root pointer in the tree of tree roots.
480d352ac68SChris Mason  *
481d352ac68SChris Mason  * But, in the case of the extent allocation tree, updating the root
482d352ac68SChris Mason  * pointer may allocate blocks which may change the root of the extent
483d352ac68SChris Mason  * allocation tree.
484d352ac68SChris Mason  *
485d352ac68SChris Mason  * So, this loops and repeats and makes sure the cowonly root didn't
486d352ac68SChris Mason  * change while the root pointer was being updated in the metadata.
487d352ac68SChris Mason  */
4880b86a832SChris Mason static int update_cowonly_root(struct btrfs_trans_handle *trans,
48979154b1bSChris Mason 			       struct btrfs_root *root)
49079154b1bSChris Mason {
49179154b1bSChris Mason 	int ret;
4920b86a832SChris Mason 	u64 old_root_bytenr;
4930b86a832SChris Mason 	struct btrfs_root *tree_root = root->fs_info->tree_root;
49479154b1bSChris Mason 
4950b86a832SChris Mason 	btrfs_write_dirty_block_groups(trans, root);
49656bec294SChris Mason 
49779154b1bSChris Mason 	while (1) {
4980b86a832SChris Mason 		old_root_bytenr = btrfs_root_bytenr(&root->root_item);
4990b86a832SChris Mason 		if (old_root_bytenr == root->node->start)
50079154b1bSChris Mason 			break;
50187ef2bb4SChris Mason 
5025d4f98a2SYan Zheng 		btrfs_set_root_node(&root->root_item, root->node);
50379154b1bSChris Mason 		ret = btrfs_update_root(trans, tree_root,
5040b86a832SChris Mason 					&root->root_key,
5050b86a832SChris Mason 					&root->root_item);
50679154b1bSChris Mason 		BUG_ON(ret);
50756bec294SChris Mason 
5084a8c9a62SYan Zheng 		ret = btrfs_write_dirty_block_groups(trans, root);
50956bec294SChris Mason 		BUG_ON(ret);
5100b86a832SChris Mason 	}
511276e680dSYan Zheng 
512276e680dSYan Zheng 	if (root != root->fs_info->extent_root)
513817d52f8SJosef Bacik 		switch_commit_root(root);
514276e680dSYan Zheng 
5150b86a832SChris Mason 	return 0;
5160b86a832SChris Mason }
5170b86a832SChris Mason 
518d352ac68SChris Mason /*
519d352ac68SChris Mason  * update all the cowonly tree roots on disk
520d352ac68SChris Mason  */
5215d4f98a2SYan Zheng static noinline int commit_cowonly_roots(struct btrfs_trans_handle *trans,
5220b86a832SChris Mason 					 struct btrfs_root *root)
5230b86a832SChris Mason {
5240b86a832SChris Mason 	struct btrfs_fs_info *fs_info = root->fs_info;
5250b86a832SChris Mason 	struct list_head *next;
52684234f3aSYan Zheng 	struct extent_buffer *eb;
52756bec294SChris Mason 	int ret;
52884234f3aSYan Zheng 
52956bec294SChris Mason 	ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
53056bec294SChris Mason 	BUG_ON(ret);
53187ef2bb4SChris Mason 
53284234f3aSYan Zheng 	eb = btrfs_lock_root_node(fs_info->tree_root);
5339fa8cfe7SChris Mason 	btrfs_cow_block(trans, fs_info->tree_root, eb, NULL, 0, &eb);
53484234f3aSYan Zheng 	btrfs_tree_unlock(eb);
53584234f3aSYan Zheng 	free_extent_buffer(eb);
5360b86a832SChris Mason 
53756bec294SChris Mason 	ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
53856bec294SChris Mason 	BUG_ON(ret);
53987ef2bb4SChris Mason 
5400b86a832SChris Mason 	while (!list_empty(&fs_info->dirty_cowonly_roots)) {
5410b86a832SChris Mason 		next = fs_info->dirty_cowonly_roots.next;
5420b86a832SChris Mason 		list_del_init(next);
5430b86a832SChris Mason 		root = list_entry(next, struct btrfs_root, dirty_list);
54487ef2bb4SChris Mason 
5450b86a832SChris Mason 		update_cowonly_root(trans, root);
54679154b1bSChris Mason 	}
547276e680dSYan Zheng 
548276e680dSYan Zheng 	down_write(&fs_info->extent_commit_sem);
549276e680dSYan Zheng 	switch_commit_root(fs_info->extent_root);
550276e680dSYan Zheng 	up_write(&fs_info->extent_commit_sem);
551276e680dSYan Zheng 
55279154b1bSChris Mason 	return 0;
55379154b1bSChris Mason }
55479154b1bSChris Mason 
555d352ac68SChris Mason /*
556d352ac68SChris Mason  * dead roots are old snapshots that need to be deleted.  This allocates
557d352ac68SChris Mason  * a dirty root struct and adds it into the list of dead roots that need to
558d352ac68SChris Mason  * be deleted
559d352ac68SChris Mason  */
5605d4f98a2SYan Zheng int btrfs_add_dead_root(struct btrfs_root *root)
5615eda7b5eSChris Mason {
562b48652c1SYan Zheng 	mutex_lock(&root->fs_info->trans_mutex);
5635d4f98a2SYan Zheng 	list_add(&root->root_list, &root->fs_info->dead_roots);
564b48652c1SYan Zheng 	mutex_unlock(&root->fs_info->trans_mutex);
5655eda7b5eSChris Mason 	return 0;
5665eda7b5eSChris Mason }
5675eda7b5eSChris Mason 
568d352ac68SChris Mason /*
5695d4f98a2SYan Zheng  * update all the cowonly tree roots on disk
570d352ac68SChris Mason  */
5715d4f98a2SYan Zheng static noinline int commit_fs_roots(struct btrfs_trans_handle *trans,
5725d4f98a2SYan Zheng 				    struct btrfs_root *root)
5730f7d52f4SChris Mason {
5740f7d52f4SChris Mason 	struct btrfs_root *gang[8];
5755d4f98a2SYan Zheng 	struct btrfs_fs_info *fs_info = root->fs_info;
5760f7d52f4SChris Mason 	int i;
5770f7d52f4SChris Mason 	int ret;
57854aa1f4dSChris Mason 	int err = 0;
57954aa1f4dSChris Mason 
5800f7d52f4SChris Mason 	while (1) {
5815d4f98a2SYan Zheng 		ret = radix_tree_gang_lookup_tag(&fs_info->fs_roots_radix,
5825d4f98a2SYan Zheng 						 (void **)gang, 0,
5830f7d52f4SChris Mason 						 ARRAY_SIZE(gang),
5840f7d52f4SChris Mason 						 BTRFS_ROOT_TRANS_TAG);
5850f7d52f4SChris Mason 		if (ret == 0)
5860f7d52f4SChris Mason 			break;
5870f7d52f4SChris Mason 		for (i = 0; i < ret; i++) {
5880f7d52f4SChris Mason 			root = gang[i];
5895d4f98a2SYan Zheng 			radix_tree_tag_clear(&fs_info->fs_roots_radix,
5902619ba1fSChris Mason 					(unsigned long)root->root_key.objectid,
5910f7d52f4SChris Mason 					BTRFS_ROOT_TRANS_TAG);
59231153d81SYan Zheng 
593e02119d5SChris Mason 			btrfs_free_log(trans, root);
5945d4f98a2SYan Zheng 			btrfs_update_reloc_root(trans, root);
595e02119d5SChris Mason 
596978d910dSYan Zheng 			if (root->commit_root != root->node) {
597817d52f8SJosef Bacik 				switch_commit_root(root);
598978d910dSYan Zheng 				btrfs_set_root_node(&root->root_item,
599978d910dSYan Zheng 						    root->node);
600978d910dSYan Zheng 			}
60131153d81SYan Zheng 
6025d4f98a2SYan Zheng 			err = btrfs_update_root(trans, fs_info->tree_root,
6030f7d52f4SChris Mason 						&root->root_key,
6040f7d52f4SChris Mason 						&root->root_item);
60554aa1f4dSChris Mason 			if (err)
60654aa1f4dSChris Mason 				break;
6070f7d52f4SChris Mason 		}
6089f3a7427SChris Mason 	}
60954aa1f4dSChris Mason 	return err;
6100f7d52f4SChris Mason }
6110f7d52f4SChris Mason 
612d352ac68SChris Mason /*
613d352ac68SChris Mason  * defrag a given btree.  If cacheonly == 1, this won't read from the disk,
614d352ac68SChris Mason  * otherwise every leaf in the btree is read and defragged.
615d352ac68SChris Mason  */
616e9d0b13bSChris Mason int btrfs_defrag_root(struct btrfs_root *root, int cacheonly)
617e9d0b13bSChris Mason {
618e9d0b13bSChris Mason 	struct btrfs_fs_info *info = root->fs_info;
619e9d0b13bSChris Mason 	int ret;
620e9d0b13bSChris Mason 	struct btrfs_trans_handle *trans;
621d3c2fdcfSChris Mason 	unsigned long nr;
622e9d0b13bSChris Mason 
623a2135011SChris Mason 	smp_mb();
624e9d0b13bSChris Mason 	if (root->defrag_running)
625e9d0b13bSChris Mason 		return 0;
626e9d0b13bSChris Mason 	trans = btrfs_start_transaction(root, 1);
6276b80053dSChris Mason 	while (1) {
628e9d0b13bSChris Mason 		root->defrag_running = 1;
629e9d0b13bSChris Mason 		ret = btrfs_defrag_leaves(trans, root, cacheonly);
630d3c2fdcfSChris Mason 		nr = trans->blocks_used;
631e9d0b13bSChris Mason 		btrfs_end_transaction(trans, root);
632d3c2fdcfSChris Mason 		btrfs_btree_balance_dirty(info->tree_root, nr);
633e9d0b13bSChris Mason 		cond_resched();
634e9d0b13bSChris Mason 
635e9d0b13bSChris Mason 		trans = btrfs_start_transaction(root, 1);
6363f157a2fSChris Mason 		if (root->fs_info->closing || ret != -EAGAIN)
637e9d0b13bSChris Mason 			break;
638e9d0b13bSChris Mason 	}
639e9d0b13bSChris Mason 	root->defrag_running = 0;
640a2135011SChris Mason 	smp_mb();
641e9d0b13bSChris Mason 	btrfs_end_transaction(trans, root);
642e9d0b13bSChris Mason 	return 0;
643e9d0b13bSChris Mason }
644e9d0b13bSChris Mason 
6452c47e605SYan Zheng #if 0
646d352ac68SChris Mason /*
647b7ec40d7SChris Mason  * when dropping snapshots, we generate a ton of delayed refs, and it makes
648b7ec40d7SChris Mason  * sense not to join the transaction while it is trying to flush the current
649b7ec40d7SChris Mason  * queue of delayed refs out.
650b7ec40d7SChris Mason  *
651b7ec40d7SChris Mason  * This is used by the drop snapshot code only
652b7ec40d7SChris Mason  */
653b7ec40d7SChris Mason static noinline int wait_transaction_pre_flush(struct btrfs_fs_info *info)
654b7ec40d7SChris Mason {
655b7ec40d7SChris Mason 	DEFINE_WAIT(wait);
656b7ec40d7SChris Mason 
657b7ec40d7SChris Mason 	mutex_lock(&info->trans_mutex);
658b7ec40d7SChris Mason 	while (info->running_transaction &&
659b7ec40d7SChris Mason 	       info->running_transaction->delayed_refs.flushing) {
660b7ec40d7SChris Mason 		prepare_to_wait(&info->transaction_wait, &wait,
661b7ec40d7SChris Mason 				TASK_UNINTERRUPTIBLE);
662b7ec40d7SChris Mason 		mutex_unlock(&info->trans_mutex);
66359bc5c75SChris Mason 
664b7ec40d7SChris Mason 		schedule();
66559bc5c75SChris Mason 
666b7ec40d7SChris Mason 		mutex_lock(&info->trans_mutex);
667b7ec40d7SChris Mason 		finish_wait(&info->transaction_wait, &wait);
668b7ec40d7SChris Mason 	}
669b7ec40d7SChris Mason 	mutex_unlock(&info->trans_mutex);
670b7ec40d7SChris Mason 	return 0;
671b7ec40d7SChris Mason }
672b7ec40d7SChris Mason 
673b7ec40d7SChris Mason /*
674d352ac68SChris Mason  * Given a list of roots that need to be deleted, call btrfs_drop_snapshot on
675d352ac68SChris Mason  * all of them
676d352ac68SChris Mason  */
6775d4f98a2SYan Zheng int btrfs_drop_dead_root(struct btrfs_root *root)
6780f7d52f4SChris Mason {
6790f7d52f4SChris Mason 	struct btrfs_trans_handle *trans;
6805d4f98a2SYan Zheng 	struct btrfs_root *tree_root = root->fs_info->tree_root;
681d3c2fdcfSChris Mason 	unsigned long nr;
6825d4f98a2SYan Zheng 	int ret;
68358176a96SJosef Bacik 
6849f3a7427SChris Mason 	while (1) {
685b7ec40d7SChris Mason 		/*
686b7ec40d7SChris Mason 		 * we don't want to jump in and create a bunch of
687b7ec40d7SChris Mason 		 * delayed refs if the transaction is starting to close
688b7ec40d7SChris Mason 		 */
689b7ec40d7SChris Mason 		wait_transaction_pre_flush(tree_root->fs_info);
6900f7d52f4SChris Mason 		trans = btrfs_start_transaction(tree_root, 1);
691b7ec40d7SChris Mason 
692b7ec40d7SChris Mason 		/*
693b7ec40d7SChris Mason 		 * we've joined a transaction, make sure it isn't
694b7ec40d7SChris Mason 		 * closing right now
695b7ec40d7SChris Mason 		 */
696b7ec40d7SChris Mason 		if (trans->transaction->delayed_refs.flushing) {
697b7ec40d7SChris Mason 			btrfs_end_transaction(trans, tree_root);
698b7ec40d7SChris Mason 			continue;
699b7ec40d7SChris Mason 		}
700b7ec40d7SChris Mason 
7015d4f98a2SYan Zheng 		ret = btrfs_drop_snapshot(trans, root);
702d397712bSChris Mason 		if (ret != -EAGAIN)
7039f3a7427SChris Mason 			break;
70458176a96SJosef Bacik 
7055d4f98a2SYan Zheng 		ret = btrfs_update_root(trans, tree_root,
7065d4f98a2SYan Zheng 					&root->root_key,
7075d4f98a2SYan Zheng 					&root->root_item);
7085d4f98a2SYan Zheng 		if (ret)
70954aa1f4dSChris Mason 			break;
710bcc63abbSYan 
711d3c2fdcfSChris Mason 		nr = trans->blocks_used;
7120f7d52f4SChris Mason 		ret = btrfs_end_transaction(trans, tree_root);
7130f7d52f4SChris Mason 		BUG_ON(ret);
7145eda7b5eSChris Mason 
715d3c2fdcfSChris Mason 		btrfs_btree_balance_dirty(tree_root, nr);
7164dc11904SChris Mason 		cond_resched();
7170f7d52f4SChris Mason 	}
7185d4f98a2SYan Zheng 	BUG_ON(ret);
7195d4f98a2SYan Zheng 
7205d4f98a2SYan Zheng 	ret = btrfs_del_root(trans, tree_root, &root->root_key);
7215d4f98a2SYan Zheng 	BUG_ON(ret);
7225d4f98a2SYan Zheng 
7235d4f98a2SYan Zheng 	nr = trans->blocks_used;
7245d4f98a2SYan Zheng 	ret = btrfs_end_transaction(trans, tree_root);
7255d4f98a2SYan Zheng 	BUG_ON(ret);
7265d4f98a2SYan Zheng 
7275d4f98a2SYan Zheng 	free_extent_buffer(root->node);
7285d4f98a2SYan Zheng 	free_extent_buffer(root->commit_root);
7295d4f98a2SYan Zheng 	kfree(root);
7305d4f98a2SYan Zheng 
7315d4f98a2SYan Zheng 	btrfs_btree_balance_dirty(tree_root, nr);
73254aa1f4dSChris Mason 	return ret;
7330f7d52f4SChris Mason }
7342c47e605SYan Zheng #endif
7350f7d52f4SChris Mason 
736d352ac68SChris Mason /*
737d352ac68SChris Mason  * new snapshots need to be created at a very specific time in the
738d352ac68SChris Mason  * transaction commit.  This does the actual creation
739d352ac68SChris Mason  */
74080b6794dSChris Mason static noinline int create_pending_snapshot(struct btrfs_trans_handle *trans,
7413063d29fSChris Mason 				   struct btrfs_fs_info *fs_info,
7423063d29fSChris Mason 				   struct btrfs_pending_snapshot *pending)
7433063d29fSChris Mason {
7443063d29fSChris Mason 	struct btrfs_key key;
74580b6794dSChris Mason 	struct btrfs_root_item *new_root_item;
7463063d29fSChris Mason 	struct btrfs_root *tree_root = fs_info->tree_root;
7473063d29fSChris Mason 	struct btrfs_root *root = pending->root;
7483063d29fSChris Mason 	struct extent_buffer *tmp;
749925baeddSChris Mason 	struct extent_buffer *old;
7503063d29fSChris Mason 	int ret;
7513063d29fSChris Mason 	u64 objectid;
7523063d29fSChris Mason 
75380b6794dSChris Mason 	new_root_item = kmalloc(sizeof(*new_root_item), GFP_NOFS);
75480b6794dSChris Mason 	if (!new_root_item) {
75580b6794dSChris Mason 		ret = -ENOMEM;
75680b6794dSChris Mason 		goto fail;
75780b6794dSChris Mason 	}
7583063d29fSChris Mason 	ret = btrfs_find_free_objectid(trans, tree_root, 0, &objectid);
7593063d29fSChris Mason 	if (ret)
7603063d29fSChris Mason 		goto fail;
7613063d29fSChris Mason 
7625d4f98a2SYan Zheng 	record_root_in_trans(trans, root);
76380ff3856SYan Zheng 	btrfs_set_root_last_snapshot(&root->root_item, trans->transid);
76480b6794dSChris Mason 	memcpy(new_root_item, &root->root_item, sizeof(*new_root_item));
7653063d29fSChris Mason 
7663063d29fSChris Mason 	key.objectid = objectid;
7671c4850e2SYan, Zheng 	/* record when the snapshot was created in key.offset */
7681c4850e2SYan, Zheng 	key.offset = trans->transid;
7693063d29fSChris Mason 	btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
7703063d29fSChris Mason 
771925baeddSChris Mason 	old = btrfs_lock_root_node(root);
7729fa8cfe7SChris Mason 	btrfs_cow_block(trans, root, old, NULL, 0, &old);
7735d4f98a2SYan Zheng 	btrfs_set_lock_blocking(old);
7743063d29fSChris Mason 
775925baeddSChris Mason 	btrfs_copy_root(trans, root, old, &tmp, objectid);
776925baeddSChris Mason 	btrfs_tree_unlock(old);
777925baeddSChris Mason 	free_extent_buffer(old);
7783063d29fSChris Mason 
7795d4f98a2SYan Zheng 	btrfs_set_root_node(new_root_item, tmp);
7803063d29fSChris Mason 	ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
78180b6794dSChris Mason 				new_root_item);
782925baeddSChris Mason 	btrfs_tree_unlock(tmp);
7833063d29fSChris Mason 	free_extent_buffer(tmp);
7843063d29fSChris Mason 	if (ret)
7853063d29fSChris Mason 		goto fail;
7863063d29fSChris Mason 
7873de4586cSChris Mason 	key.offset = (u64)-1;
7883de4586cSChris Mason 	memcpy(&pending->root_key, &key, sizeof(key));
7893de4586cSChris Mason fail:
7903de4586cSChris Mason 	kfree(new_root_item);
7919ed74f2dSJosef Bacik 	btrfs_unreserve_metadata_space(root, 6);
7923de4586cSChris Mason 	return ret;
7933de4586cSChris Mason }
7943de4586cSChris Mason 
7953de4586cSChris Mason static noinline int finish_pending_snapshot(struct btrfs_fs_info *fs_info,
7963de4586cSChris Mason 				   struct btrfs_pending_snapshot *pending)
7973de4586cSChris Mason {
7983de4586cSChris Mason 	int ret;
7993de4586cSChris Mason 	int namelen;
8003de4586cSChris Mason 	u64 index = 0;
8013de4586cSChris Mason 	struct btrfs_trans_handle *trans;
8023de4586cSChris Mason 	struct inode *parent_inode;
8033de4586cSChris Mason 	struct inode *inode;
8040660b5afSChris Mason 	struct btrfs_root *parent_root;
8053de4586cSChris Mason 
8063394e160SChris Mason 	parent_inode = pending->dentry->d_parent->d_inode;
8070660b5afSChris Mason 	parent_root = BTRFS_I(parent_inode)->root;
808180591bcSYan Zheng 	trans = btrfs_join_transaction(parent_root, 1);
8093de4586cSChris Mason 
8103063d29fSChris Mason 	/*
8113063d29fSChris Mason 	 * insert the directory item
8123063d29fSChris Mason 	 */
8133b96362cSSven Wegener 	namelen = strlen(pending->name);
8143de4586cSChris Mason 	ret = btrfs_set_inode_index(parent_inode, &index);
8150660b5afSChris Mason 	ret = btrfs_insert_dir_item(trans, parent_root,
8163b96362cSSven Wegener 			    pending->name, namelen,
8173de4586cSChris Mason 			    parent_inode->i_ino,
8183de4586cSChris Mason 			    &pending->root_key, BTRFS_FT_DIR, index);
8193063d29fSChris Mason 
8203063d29fSChris Mason 	if (ret)
8213063d29fSChris Mason 		goto fail;
8220660b5afSChris Mason 
82352c26179SYan Zheng 	btrfs_i_size_write(parent_inode, parent_inode->i_size + namelen * 2);
82452c26179SYan Zheng 	ret = btrfs_update_inode(trans, parent_root, parent_inode);
82552c26179SYan Zheng 	BUG_ON(ret);
82652c26179SYan Zheng 
8270660b5afSChris Mason 	ret = btrfs_add_root_ref(trans, parent_root->fs_info->tree_root,
8280660b5afSChris Mason 				 pending->root_key.objectid,
8290660b5afSChris Mason 				 parent_root->root_key.objectid,
8300660b5afSChris Mason 				 parent_inode->i_ino, index, pending->name,
8310660b5afSChris Mason 				 namelen);
8320660b5afSChris Mason 
8330660b5afSChris Mason 	BUG_ON(ret);
8340660b5afSChris Mason 
8353de4586cSChris Mason 	inode = btrfs_lookup_dentry(parent_inode, pending->dentry);
8363de4586cSChris Mason 	d_instantiate(pending->dentry, inode);
8373063d29fSChris Mason fail:
8383de4586cSChris Mason 	btrfs_end_transaction(trans, fs_info->fs_root);
8393063d29fSChris Mason 	return ret;
8403063d29fSChris Mason }
8413063d29fSChris Mason 
842d352ac68SChris Mason /*
843d352ac68SChris Mason  * create all the snapshots we've scheduled for creation
844d352ac68SChris Mason  */
84580b6794dSChris Mason static noinline int create_pending_snapshots(struct btrfs_trans_handle *trans,
8463063d29fSChris Mason 					     struct btrfs_fs_info *fs_info)
8473063d29fSChris Mason {
8483063d29fSChris Mason 	struct btrfs_pending_snapshot *pending;
8493063d29fSChris Mason 	struct list_head *head = &trans->transaction->pending_snapshots;
8503de4586cSChris Mason 	int ret;
8513de4586cSChris Mason 
852c6e30871SQinghuang Feng 	list_for_each_entry(pending, head, list) {
8533de4586cSChris Mason 		ret = create_pending_snapshot(trans, fs_info, pending);
8543de4586cSChris Mason 		BUG_ON(ret);
8553de4586cSChris Mason 	}
8563de4586cSChris Mason 	return 0;
8573de4586cSChris Mason }
8583de4586cSChris Mason 
8593de4586cSChris Mason static noinline int finish_pending_snapshots(struct btrfs_trans_handle *trans,
8603de4586cSChris Mason 					     struct btrfs_fs_info *fs_info)
8613de4586cSChris Mason {
8623de4586cSChris Mason 	struct btrfs_pending_snapshot *pending;
8633de4586cSChris Mason 	struct list_head *head = &trans->transaction->pending_snapshots;
8643063d29fSChris Mason 	int ret;
8653063d29fSChris Mason 
8663063d29fSChris Mason 	while (!list_empty(head)) {
8673063d29fSChris Mason 		pending = list_entry(head->next,
8683063d29fSChris Mason 				     struct btrfs_pending_snapshot, list);
8693de4586cSChris Mason 		ret = finish_pending_snapshot(fs_info, pending);
8703063d29fSChris Mason 		BUG_ON(ret);
8713063d29fSChris Mason 		list_del(&pending->list);
8723063d29fSChris Mason 		kfree(pending->name);
8733063d29fSChris Mason 		kfree(pending);
8743063d29fSChris Mason 	}
875dc17ff8fSChris Mason 	return 0;
876dc17ff8fSChris Mason }
877dc17ff8fSChris Mason 
8785d4f98a2SYan Zheng static void update_super_roots(struct btrfs_root *root)
8795d4f98a2SYan Zheng {
8805d4f98a2SYan Zheng 	struct btrfs_root_item *root_item;
8815d4f98a2SYan Zheng 	struct btrfs_super_block *super;
8825d4f98a2SYan Zheng 
8835d4f98a2SYan Zheng 	super = &root->fs_info->super_copy;
8845d4f98a2SYan Zheng 
8855d4f98a2SYan Zheng 	root_item = &root->fs_info->chunk_root->root_item;
8865d4f98a2SYan Zheng 	super->chunk_root = root_item->bytenr;
8875d4f98a2SYan Zheng 	super->chunk_root_generation = root_item->generation;
8885d4f98a2SYan Zheng 	super->chunk_root_level = root_item->level;
8895d4f98a2SYan Zheng 
8905d4f98a2SYan Zheng 	root_item = &root->fs_info->tree_root->root_item;
8915d4f98a2SYan Zheng 	super->root = root_item->bytenr;
8925d4f98a2SYan Zheng 	super->generation = root_item->generation;
8935d4f98a2SYan Zheng 	super->root_level = root_item->level;
8945d4f98a2SYan Zheng }
8955d4f98a2SYan Zheng 
896f36f3042SChris Mason int btrfs_transaction_in_commit(struct btrfs_fs_info *info)
897f36f3042SChris Mason {
898f36f3042SChris Mason 	int ret = 0;
899f36f3042SChris Mason 	spin_lock(&info->new_trans_lock);
900f36f3042SChris Mason 	if (info->running_transaction)
901f36f3042SChris Mason 		ret = info->running_transaction->in_commit;
902f36f3042SChris Mason 	spin_unlock(&info->new_trans_lock);
903f36f3042SChris Mason 	return ret;
904f36f3042SChris Mason }
905f36f3042SChris Mason 
90679154b1bSChris Mason int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
90779154b1bSChris Mason 			     struct btrfs_root *root)
90879154b1bSChris Mason {
90915ee9bc7SJosef Bacik 	unsigned long joined = 0;
91015ee9bc7SJosef Bacik 	unsigned long timeout = 1;
91179154b1bSChris Mason 	struct btrfs_transaction *cur_trans;
9128fd17795SChris Mason 	struct btrfs_transaction *prev_trans = NULL;
91379154b1bSChris Mason 	DEFINE_WAIT(wait);
91415ee9bc7SJosef Bacik 	int ret;
91589573b9cSChris Mason 	int should_grow = 0;
91689573b9cSChris Mason 	unsigned long now = get_seconds();
917dccae999SSage Weil 	int flush_on_commit = btrfs_test_opt(root, FLUSHONCOMMIT);
91879154b1bSChris Mason 
9195a3f23d5SChris Mason 	btrfs_run_ordered_operations(root, 0);
9205a3f23d5SChris Mason 
92156bec294SChris Mason 	/* make a pass through all the delayed refs we have so far
92256bec294SChris Mason 	 * any runnings procs may add more while we are here
92356bec294SChris Mason 	 */
92456bec294SChris Mason 	ret = btrfs_run_delayed_refs(trans, root, 0);
92556bec294SChris Mason 	BUG_ON(ret);
92656bec294SChris Mason 
927b7ec40d7SChris Mason 	cur_trans = trans->transaction;
92856bec294SChris Mason 	/*
92956bec294SChris Mason 	 * set the flushing flag so procs in this transaction have to
93056bec294SChris Mason 	 * start sending their work down.
93156bec294SChris Mason 	 */
932b7ec40d7SChris Mason 	cur_trans->delayed_refs.flushing = 1;
93356bec294SChris Mason 
934c3e69d58SChris Mason 	ret = btrfs_run_delayed_refs(trans, root, 0);
93556bec294SChris Mason 	BUG_ON(ret);
93656bec294SChris Mason 
93779154b1bSChris Mason 	mutex_lock(&root->fs_info->trans_mutex);
938b7ec40d7SChris Mason 	if (cur_trans->in_commit) {
939b7ec40d7SChris Mason 		cur_trans->use_count++;
940ccd467d6SChris Mason 		mutex_unlock(&root->fs_info->trans_mutex);
94179154b1bSChris Mason 		btrfs_end_transaction(trans, root);
942ccd467d6SChris Mason 
94379154b1bSChris Mason 		ret = wait_for_commit(root, cur_trans);
94479154b1bSChris Mason 		BUG_ON(ret);
94515ee9bc7SJosef Bacik 
94615ee9bc7SJosef Bacik 		mutex_lock(&root->fs_info->trans_mutex);
94779154b1bSChris Mason 		put_transaction(cur_trans);
94815ee9bc7SJosef Bacik 		mutex_unlock(&root->fs_info->trans_mutex);
94915ee9bc7SJosef Bacik 
95079154b1bSChris Mason 		return 0;
95179154b1bSChris Mason 	}
9524313b399SChris Mason 
9532c90e5d6SChris Mason 	trans->transaction->in_commit = 1;
954f9295749SChris Mason 	trans->transaction->blocked = 1;
955ccd467d6SChris Mason 	if (cur_trans->list.prev != &root->fs_info->trans_list) {
956ccd467d6SChris Mason 		prev_trans = list_entry(cur_trans->list.prev,
957ccd467d6SChris Mason 					struct btrfs_transaction, list);
958ccd467d6SChris Mason 		if (!prev_trans->commit_done) {
959ccd467d6SChris Mason 			prev_trans->use_count++;
960ccd467d6SChris Mason 			mutex_unlock(&root->fs_info->trans_mutex);
961ccd467d6SChris Mason 
962ccd467d6SChris Mason 			wait_for_commit(root, prev_trans);
963ccd467d6SChris Mason 
964ccd467d6SChris Mason 			mutex_lock(&root->fs_info->trans_mutex);
96515ee9bc7SJosef Bacik 			put_transaction(prev_trans);
966ccd467d6SChris Mason 		}
967ccd467d6SChris Mason 	}
96815ee9bc7SJosef Bacik 
96989573b9cSChris Mason 	if (now < cur_trans->start_time || now - cur_trans->start_time < 1)
97089573b9cSChris Mason 		should_grow = 1;
97189573b9cSChris Mason 
97215ee9bc7SJosef Bacik 	do {
9737ea394f1SYan Zheng 		int snap_pending = 0;
97415ee9bc7SJosef Bacik 		joined = cur_trans->num_joined;
9757ea394f1SYan Zheng 		if (!list_empty(&trans->transaction->pending_snapshots))
9767ea394f1SYan Zheng 			snap_pending = 1;
9777ea394f1SYan Zheng 
9782c90e5d6SChris Mason 		WARN_ON(cur_trans != trans->transaction);
97915ee9bc7SJosef Bacik 		prepare_to_wait(&cur_trans->writer_wait, &wait,
98079154b1bSChris Mason 				TASK_UNINTERRUPTIBLE);
98115ee9bc7SJosef Bacik 
98215ee9bc7SJosef Bacik 		if (cur_trans->num_writers > 1)
98315ee9bc7SJosef Bacik 			timeout = MAX_SCHEDULE_TIMEOUT;
98489573b9cSChris Mason 		else if (should_grow)
98515ee9bc7SJosef Bacik 			timeout = 1;
98615ee9bc7SJosef Bacik 
98779154b1bSChris Mason 		mutex_unlock(&root->fs_info->trans_mutex);
98815ee9bc7SJosef Bacik 
989ebecd3d9SSage Weil 		if (flush_on_commit) {
990dccae999SSage Weil 			btrfs_start_delalloc_inodes(root);
991ebecd3d9SSage Weil 			ret = btrfs_wait_ordered_extents(root, 0);
992ebecd3d9SSage Weil 			BUG_ON(ret);
993ebecd3d9SSage Weil 		} else if (snap_pending) {
9947ea394f1SYan Zheng 			ret = btrfs_wait_ordered_extents(root, 1);
9957ea394f1SYan Zheng 			BUG_ON(ret);
9967ea394f1SYan Zheng 		}
9977ea394f1SYan Zheng 
9985a3f23d5SChris Mason 		/*
9995a3f23d5SChris Mason 		 * rename don't use btrfs_join_transaction, so, once we
10005a3f23d5SChris Mason 		 * set the transaction to blocked above, we aren't going
10015a3f23d5SChris Mason 		 * to get any new ordered operations.  We can safely run
10025a3f23d5SChris Mason 		 * it here and no for sure that nothing new will be added
10035a3f23d5SChris Mason 		 * to the list
10045a3f23d5SChris Mason 		 */
10055a3f23d5SChris Mason 		btrfs_run_ordered_operations(root, 1);
10065a3f23d5SChris Mason 
100789573b9cSChris Mason 		smp_mb();
100889573b9cSChris Mason 		if (cur_trans->num_writers > 1 || should_grow)
100915ee9bc7SJosef Bacik 			schedule_timeout(timeout);
101015ee9bc7SJosef Bacik 
101179154b1bSChris Mason 		mutex_lock(&root->fs_info->trans_mutex);
101215ee9bc7SJosef Bacik 		finish_wait(&cur_trans->writer_wait, &wait);
101315ee9bc7SJosef Bacik 	} while (cur_trans->num_writers > 1 ||
101489573b9cSChris Mason 		 (should_grow && cur_trans->num_joined != joined));
101515ee9bc7SJosef Bacik 
10163063d29fSChris Mason 	ret = create_pending_snapshots(trans, root->fs_info);
10173063d29fSChris Mason 	BUG_ON(ret);
10183063d29fSChris Mason 
101956bec294SChris Mason 	ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
102056bec294SChris Mason 	BUG_ON(ret);
102156bec294SChris Mason 
10222c90e5d6SChris Mason 	WARN_ON(cur_trans != trans->transaction);
1023dc17ff8fSChris Mason 
1024e02119d5SChris Mason 	/* btrfs_commit_tree_roots is responsible for getting the
1025e02119d5SChris Mason 	 * various roots consistent with each other.  Every pointer
1026e02119d5SChris Mason 	 * in the tree of tree roots has to point to the most up to date
1027e02119d5SChris Mason 	 * root for every subvolume and other tree.  So, we have to keep
1028e02119d5SChris Mason 	 * the tree logging code from jumping in and changing any
1029e02119d5SChris Mason 	 * of the trees.
1030e02119d5SChris Mason 	 *
1031e02119d5SChris Mason 	 * At this point in the commit, there can't be any tree-log
1032e02119d5SChris Mason 	 * writers, but a little lower down we drop the trans mutex
1033e02119d5SChris Mason 	 * and let new people in.  By holding the tree_log_mutex
1034e02119d5SChris Mason 	 * from now until after the super is written, we avoid races
1035e02119d5SChris Mason 	 * with the tree-log code.
1036e02119d5SChris Mason 	 */
1037e02119d5SChris Mason 	mutex_lock(&root->fs_info->tree_log_mutex);
10381a40e23bSZheng Yan 
10395d4f98a2SYan Zheng 	ret = commit_fs_roots(trans, root);
104054aa1f4dSChris Mason 	BUG_ON(ret);
104154aa1f4dSChris Mason 
10425d4f98a2SYan Zheng 	/* commit_fs_roots gets rid of all the tree log roots, it is now
1043e02119d5SChris Mason 	 * safe to free the root of tree log roots
1044e02119d5SChris Mason 	 */
1045e02119d5SChris Mason 	btrfs_free_log_root_tree(trans, root->fs_info);
1046e02119d5SChris Mason 
10475d4f98a2SYan Zheng 	ret = commit_cowonly_roots(trans, root);
104879154b1bSChris Mason 	BUG_ON(ret);
104954aa1f4dSChris Mason 
105011833d66SYan Zheng 	btrfs_prepare_extent_commit(trans, root);
105111833d66SYan Zheng 
105278fae27eSChris Mason 	cur_trans = root->fs_info->running_transaction;
1053cee36a03SChris Mason 	spin_lock(&root->fs_info->new_trans_lock);
105478fae27eSChris Mason 	root->fs_info->running_transaction = NULL;
1055cee36a03SChris Mason 	spin_unlock(&root->fs_info->new_trans_lock);
10565f39d397SChris Mason 
10575d4f98a2SYan Zheng 	btrfs_set_root_node(&root->fs_info->tree_root->root_item,
10585d4f98a2SYan Zheng 			    root->fs_info->tree_root->node);
1059817d52f8SJosef Bacik 	switch_commit_root(root->fs_info->tree_root);
10605d4f98a2SYan Zheng 
10615d4f98a2SYan Zheng 	btrfs_set_root_node(&root->fs_info->chunk_root->root_item,
10625d4f98a2SYan Zheng 			    root->fs_info->chunk_root->node);
1063817d52f8SJosef Bacik 	switch_commit_root(root->fs_info->chunk_root);
10645d4f98a2SYan Zheng 
10655d4f98a2SYan Zheng 	update_super_roots(root);
1066e02119d5SChris Mason 
1067e02119d5SChris Mason 	if (!root->fs_info->log_root_recovering) {
1068e02119d5SChris Mason 		btrfs_set_super_log_root(&root->fs_info->super_copy, 0);
1069e02119d5SChris Mason 		btrfs_set_super_log_root_level(&root->fs_info->super_copy, 0);
1070e02119d5SChris Mason 	}
1071e02119d5SChris Mason 
1072a061fc8dSChris Mason 	memcpy(&root->fs_info->super_for_commit, &root->fs_info->super_copy,
10734b52dff6SChris Mason 	       sizeof(root->fs_info->super_copy));
1074ccd467d6SChris Mason 
1075f9295749SChris Mason 	trans->transaction->blocked = 0;
1076b7ec40d7SChris Mason 
1077f9295749SChris Mason 	wake_up(&root->fs_info->transaction_wait);
1078e6dcd2dcSChris Mason 
107978fae27eSChris Mason 	mutex_unlock(&root->fs_info->trans_mutex);
108079154b1bSChris Mason 	ret = btrfs_write_and_wait_transaction(trans, root);
108179154b1bSChris Mason 	BUG_ON(ret);
1082a512bbf8SYan Zheng 	write_ctree_super(trans, root, 0);
10834313b399SChris Mason 
1084e02119d5SChris Mason 	/*
1085e02119d5SChris Mason 	 * the super is written, we can safely allow the tree-loggers
1086e02119d5SChris Mason 	 * to go about their business
1087e02119d5SChris Mason 	 */
1088e02119d5SChris Mason 	mutex_unlock(&root->fs_info->tree_log_mutex);
1089e02119d5SChris Mason 
109011833d66SYan Zheng 	btrfs_finish_extent_commit(trans, root);
10914313b399SChris Mason 
10923de4586cSChris Mason 	/* do the directory inserts of any pending snapshot creations */
10933de4586cSChris Mason 	finish_pending_snapshots(trans, root->fs_info);
10943de4586cSChris Mason 
10951a40e23bSZheng Yan 	mutex_lock(&root->fs_info->trans_mutex);
10961a40e23bSZheng Yan 
10972c90e5d6SChris Mason 	cur_trans->commit_done = 1;
1098b7ec40d7SChris Mason 
109915ee9bc7SJosef Bacik 	root->fs_info->last_trans_committed = cur_trans->transid;
1100817d52f8SJosef Bacik 
11012c90e5d6SChris Mason 	wake_up(&cur_trans->commit_wait);
11023de4586cSChris Mason 
110379154b1bSChris Mason 	put_transaction(cur_trans);
110478fae27eSChris Mason 	put_transaction(cur_trans);
110558176a96SJosef Bacik 
110678fae27eSChris Mason 	mutex_unlock(&root->fs_info->trans_mutex);
11073de4586cSChris Mason 
11089ed74f2dSJosef Bacik 	if (current->journal_info == trans)
11099ed74f2dSJosef Bacik 		current->journal_info = NULL;
11109ed74f2dSJosef Bacik 
11112c90e5d6SChris Mason 	kmem_cache_free(btrfs_trans_handle_cachep, trans);
111279154b1bSChris Mason 	return ret;
111379154b1bSChris Mason }
111479154b1bSChris Mason 
1115d352ac68SChris Mason /*
1116d352ac68SChris Mason  * interface function to delete all the snapshots we have scheduled for deletion
1117d352ac68SChris Mason  */
1118e9d0b13bSChris Mason int btrfs_clean_old_snapshots(struct btrfs_root *root)
1119e9d0b13bSChris Mason {
11205d4f98a2SYan Zheng 	LIST_HEAD(list);
11215d4f98a2SYan Zheng 	struct btrfs_fs_info *fs_info = root->fs_info;
1122e9d0b13bSChris Mason 
11235d4f98a2SYan Zheng 	mutex_lock(&fs_info->trans_mutex);
11245d4f98a2SYan Zheng 	list_splice_init(&fs_info->dead_roots, &list);
11255d4f98a2SYan Zheng 	mutex_unlock(&fs_info->trans_mutex);
11265d4f98a2SYan Zheng 
11275d4f98a2SYan Zheng 	while (!list_empty(&list)) {
11285d4f98a2SYan Zheng 		root = list_entry(list.next, struct btrfs_root, root_list);
112976dda93cSYan, Zheng 		list_del(&root->root_list);
113076dda93cSYan, Zheng 
113176dda93cSYan, Zheng 		if (btrfs_header_backref_rev(root->node) <
113276dda93cSYan, Zheng 		    BTRFS_MIXED_BACKREF_REV)
11332c47e605SYan Zheng 			btrfs_drop_snapshot(root, 0);
113476dda93cSYan, Zheng 		else
113576dda93cSYan, Zheng 			btrfs_drop_snapshot(root, 1);
1136e9d0b13bSChris Mason 	}
1137e9d0b13bSChris Mason 	return 0;
1138e9d0b13bSChris Mason }
1139