xref: /openbmc/linux/fs/btrfs/transaction.c (revision 4a8c9a62)
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 
43d352ac68SChris Mason /*
44d352ac68SChris Mason  * either allocate a new transaction or hop into the existing one
45d352ac68SChris Mason  */
4680b6794dSChris Mason static noinline int join_transaction(struct btrfs_root *root)
4779154b1bSChris Mason {
4879154b1bSChris Mason 	struct btrfs_transaction *cur_trans;
4979154b1bSChris Mason 	cur_trans = root->fs_info->running_transaction;
5079154b1bSChris Mason 	if (!cur_trans) {
512c90e5d6SChris Mason 		cur_trans = kmem_cache_alloc(btrfs_transaction_cachep,
522c90e5d6SChris Mason 					     GFP_NOFS);
5379154b1bSChris Mason 		BUG_ON(!cur_trans);
540f7d52f4SChris Mason 		root->fs_info->generation++;
5515ee9bc7SJosef Bacik 		cur_trans->num_writers = 1;
5615ee9bc7SJosef Bacik 		cur_trans->num_joined = 0;
570f7d52f4SChris Mason 		cur_trans->transid = root->fs_info->generation;
5879154b1bSChris Mason 		init_waitqueue_head(&cur_trans->writer_wait);
5979154b1bSChris Mason 		init_waitqueue_head(&cur_trans->commit_wait);
6079154b1bSChris Mason 		cur_trans->in_commit = 0;
61f9295749SChris Mason 		cur_trans->blocked = 0;
62d5719762SChris Mason 		cur_trans->use_count = 1;
6379154b1bSChris Mason 		cur_trans->commit_done = 0;
6408607c1bSChris Mason 		cur_trans->start_time = get_seconds();
6556bec294SChris Mason 
6656bec294SChris Mason 		cur_trans->delayed_refs.root.rb_node = NULL;
6756bec294SChris Mason 		cur_trans->delayed_refs.num_entries = 0;
68c3e69d58SChris Mason 		cur_trans->delayed_refs.num_heads_ready = 0;
69c3e69d58SChris Mason 		cur_trans->delayed_refs.num_heads = 0;
7056bec294SChris Mason 		cur_trans->delayed_refs.flushing = 0;
71c3e69d58SChris Mason 		cur_trans->delayed_refs.run_delayed_start = 0;
7256bec294SChris Mason 		spin_lock_init(&cur_trans->delayed_refs.lock);
7356bec294SChris Mason 
743063d29fSChris Mason 		INIT_LIST_HEAD(&cur_trans->pending_snapshots);
758fd17795SChris Mason 		list_add_tail(&cur_trans->list, &root->fs_info->trans_list);
76d1310b2eSChris Mason 		extent_io_tree_init(&cur_trans->dirty_pages,
775f39d397SChris Mason 				     root->fs_info->btree_inode->i_mapping,
785f39d397SChris Mason 				     GFP_NOFS);
7948ec2cf8SChris Mason 		spin_lock(&root->fs_info->new_trans_lock);
8048ec2cf8SChris Mason 		root->fs_info->running_transaction = cur_trans;
8148ec2cf8SChris Mason 		spin_unlock(&root->fs_info->new_trans_lock);
8215ee9bc7SJosef Bacik 	} else {
8379154b1bSChris Mason 		cur_trans->num_writers++;
8415ee9bc7SJosef Bacik 		cur_trans->num_joined++;
8515ee9bc7SJosef Bacik 	}
8615ee9bc7SJosef Bacik 
8779154b1bSChris Mason 	return 0;
8879154b1bSChris Mason }
8979154b1bSChris Mason 
90d352ac68SChris Mason /*
91d397712bSChris Mason  * this does all the record keeping required to make sure that a reference
92d397712bSChris Mason  * counted root is properly recorded in a given transaction.  This is required
93d397712bSChris Mason  * to make sure the old root from before we joined the transaction is deleted
94d397712bSChris Mason  * when the transaction commits
95d352ac68SChris Mason  */
965d4f98a2SYan Zheng static noinline int record_root_in_trans(struct btrfs_trans_handle *trans,
975d4f98a2SYan Zheng 					 struct btrfs_root *root)
986702ed49SChris Mason {
995d4f98a2SYan Zheng 	if (root->ref_cows && root->last_trans < trans->transid) {
1006702ed49SChris Mason 		WARN_ON(root == root->fs_info->extent_root);
1015d4f98a2SYan Zheng 		WARN_ON(root->root_item.refs == 0);
1025d4f98a2SYan Zheng 		WARN_ON(root->commit_root != root->node);
1035d4f98a2SYan Zheng 
1046702ed49SChris Mason 		radix_tree_tag_set(&root->fs_info->fs_roots_radix,
1056702ed49SChris Mason 			   (unsigned long)root->root_key.objectid,
1066702ed49SChris Mason 			   BTRFS_ROOT_TRANS_TAG);
1075d4f98a2SYan Zheng 		root->last_trans = trans->transid;
1085d4f98a2SYan Zheng 		btrfs_init_reloc_root(trans, root);
1096702ed49SChris Mason 	}
1105d4f98a2SYan Zheng 	return 0;
1116702ed49SChris Mason }
1125d4f98a2SYan Zheng 
1135d4f98a2SYan Zheng int btrfs_record_root_in_trans(struct btrfs_trans_handle *trans,
1145d4f98a2SYan Zheng 			       struct btrfs_root *root)
1155d4f98a2SYan Zheng {
1165d4f98a2SYan Zheng 	if (!root->ref_cows)
1175d4f98a2SYan Zheng 		return 0;
1185d4f98a2SYan Zheng 
1195d4f98a2SYan Zheng 	mutex_lock(&root->fs_info->trans_mutex);
1205d4f98a2SYan Zheng 	if (root->last_trans == trans->transid) {
1215d4f98a2SYan Zheng 		mutex_unlock(&root->fs_info->trans_mutex);
1225d4f98a2SYan Zheng 		return 0;
1235d4f98a2SYan Zheng 	}
1245d4f98a2SYan Zheng 
1255d4f98a2SYan Zheng 	record_root_in_trans(trans, root);
1265d4f98a2SYan Zheng 	mutex_unlock(&root->fs_info->trans_mutex);
1276702ed49SChris Mason 	return 0;
1286702ed49SChris Mason }
1296702ed49SChris Mason 
130d352ac68SChris Mason /* wait for commit against the current transaction to become unblocked
131d352ac68SChris Mason  * when this is done, it is safe to start a new transaction, but the current
132d352ac68SChris Mason  * transaction might not be fully on disk.
133d352ac68SChris Mason  */
13437d1aeeeSChris Mason static void wait_current_trans(struct btrfs_root *root)
13579154b1bSChris Mason {
136f9295749SChris Mason 	struct btrfs_transaction *cur_trans;
13779154b1bSChris Mason 
138f9295749SChris Mason 	cur_trans = root->fs_info->running_transaction;
13937d1aeeeSChris Mason 	if (cur_trans && cur_trans->blocked) {
140f9295749SChris Mason 		DEFINE_WAIT(wait);
141f9295749SChris Mason 		cur_trans->use_count++;
142f9295749SChris Mason 		while (1) {
143f9295749SChris Mason 			prepare_to_wait(&root->fs_info->transaction_wait, &wait,
144f9295749SChris Mason 					TASK_UNINTERRUPTIBLE);
145f9295749SChris Mason 			if (cur_trans->blocked) {
146f9295749SChris Mason 				mutex_unlock(&root->fs_info->trans_mutex);
147f9295749SChris Mason 				schedule();
148f9295749SChris Mason 				mutex_lock(&root->fs_info->trans_mutex);
149f9295749SChris Mason 				finish_wait(&root->fs_info->transaction_wait,
150f9295749SChris Mason 					    &wait);
151f9295749SChris Mason 			} else {
152f9295749SChris Mason 				finish_wait(&root->fs_info->transaction_wait,
153f9295749SChris Mason 					    &wait);
154f9295749SChris Mason 				break;
155f9295749SChris Mason 			}
156f9295749SChris Mason 		}
157f9295749SChris Mason 		put_transaction(cur_trans);
158f9295749SChris Mason 	}
15937d1aeeeSChris Mason }
16037d1aeeeSChris Mason 
161e02119d5SChris Mason static struct btrfs_trans_handle *start_transaction(struct btrfs_root *root,
1629ca9ee09SSage Weil 					     int num_blocks, int wait)
16337d1aeeeSChris Mason {
16437d1aeeeSChris Mason 	struct btrfs_trans_handle *h =
16537d1aeeeSChris Mason 		kmem_cache_alloc(btrfs_trans_handle_cachep, GFP_NOFS);
16637d1aeeeSChris Mason 	int ret;
16737d1aeeeSChris Mason 
16837d1aeeeSChris Mason 	mutex_lock(&root->fs_info->trans_mutex);
1694bef0848SChris Mason 	if (!root->fs_info->log_root_recovering &&
1704bef0848SChris Mason 	    ((wait == 1 && !root->fs_info->open_ioctl_trans) || wait == 2))
17137d1aeeeSChris Mason 		wait_current_trans(root);
17279154b1bSChris Mason 	ret = join_transaction(root);
17379154b1bSChris Mason 	BUG_ON(ret);
1740f7d52f4SChris Mason 
1756702ed49SChris Mason 	h->transid = root->fs_info->running_transaction->transid;
17679154b1bSChris Mason 	h->transaction = root->fs_info->running_transaction;
17779154b1bSChris Mason 	h->blocks_reserved = num_blocks;
17879154b1bSChris Mason 	h->blocks_used = 0;
179d2fb3437SYan Zheng 	h->block_group = 0;
18026b8003fSChris Mason 	h->alloc_exclude_nr = 0;
18126b8003fSChris Mason 	h->alloc_exclude_start = 0;
18256bec294SChris Mason 	h->delayed_ref_updates = 0;
183b7ec40d7SChris Mason 
18479154b1bSChris Mason 	root->fs_info->running_transaction->use_count++;
1855d4f98a2SYan Zheng 	record_root_in_trans(h, root);
18679154b1bSChris Mason 	mutex_unlock(&root->fs_info->trans_mutex);
18779154b1bSChris Mason 	return h;
18879154b1bSChris Mason }
18979154b1bSChris Mason 
190f9295749SChris Mason struct btrfs_trans_handle *btrfs_start_transaction(struct btrfs_root *root,
191f9295749SChris Mason 						   int num_blocks)
192f9295749SChris Mason {
1939ca9ee09SSage Weil 	return start_transaction(root, num_blocks, 1);
194f9295749SChris Mason }
195f9295749SChris Mason struct btrfs_trans_handle *btrfs_join_transaction(struct btrfs_root *root,
196f9295749SChris Mason 						   int num_blocks)
197f9295749SChris Mason {
1989ca9ee09SSage Weil 	return start_transaction(root, num_blocks, 0);
199f9295749SChris Mason }
200f9295749SChris Mason 
2019ca9ee09SSage Weil struct btrfs_trans_handle *btrfs_start_ioctl_transaction(struct btrfs_root *r,
2029ca9ee09SSage Weil 							 int num_blocks)
2039ca9ee09SSage Weil {
2049ca9ee09SSage Weil 	return start_transaction(r, num_blocks, 2);
2059ca9ee09SSage Weil }
2069ca9ee09SSage Weil 
207d352ac68SChris Mason /* wait for a transaction commit to be fully complete */
20889ce8a63SChris Mason static noinline int wait_for_commit(struct btrfs_root *root,
20989ce8a63SChris Mason 				    struct btrfs_transaction *commit)
21089ce8a63SChris Mason {
21189ce8a63SChris Mason 	DEFINE_WAIT(wait);
21289ce8a63SChris Mason 	mutex_lock(&root->fs_info->trans_mutex);
21389ce8a63SChris Mason 	while (!commit->commit_done) {
21489ce8a63SChris Mason 		prepare_to_wait(&commit->commit_wait, &wait,
21589ce8a63SChris Mason 				TASK_UNINTERRUPTIBLE);
21689ce8a63SChris Mason 		if (commit->commit_done)
21789ce8a63SChris Mason 			break;
21889ce8a63SChris Mason 		mutex_unlock(&root->fs_info->trans_mutex);
21989ce8a63SChris Mason 		schedule();
22089ce8a63SChris Mason 		mutex_lock(&root->fs_info->trans_mutex);
22189ce8a63SChris Mason 	}
22289ce8a63SChris Mason 	mutex_unlock(&root->fs_info->trans_mutex);
22389ce8a63SChris Mason 	finish_wait(&commit->commit_wait, &wait);
22489ce8a63SChris Mason 	return 0;
22589ce8a63SChris Mason }
22689ce8a63SChris Mason 
2275d4f98a2SYan Zheng #if 0
228d352ac68SChris Mason /*
229d397712bSChris Mason  * rate limit against the drop_snapshot code.  This helps to slow down new
230d397712bSChris Mason  * operations if the drop_snapshot code isn't able to keep up.
231d352ac68SChris Mason  */
23237d1aeeeSChris Mason static void throttle_on_drops(struct btrfs_root *root)
233ab78c84dSChris Mason {
234ab78c84dSChris Mason 	struct btrfs_fs_info *info = root->fs_info;
2352dd3e67bSChris Mason 	int harder_count = 0;
236ab78c84dSChris Mason 
2372dd3e67bSChris Mason harder:
238ab78c84dSChris Mason 	if (atomic_read(&info->throttles)) {
239ab78c84dSChris Mason 		DEFINE_WAIT(wait);
240ab78c84dSChris Mason 		int thr;
241ab78c84dSChris Mason 		thr = atomic_read(&info->throttle_gen);
242ab78c84dSChris Mason 
243ab78c84dSChris Mason 		do {
244ab78c84dSChris Mason 			prepare_to_wait(&info->transaction_throttle,
245ab78c84dSChris Mason 					&wait, TASK_UNINTERRUPTIBLE);
246ab78c84dSChris Mason 			if (!atomic_read(&info->throttles)) {
247ab78c84dSChris Mason 				finish_wait(&info->transaction_throttle, &wait);
248ab78c84dSChris Mason 				break;
249ab78c84dSChris Mason 			}
250ab78c84dSChris Mason 			schedule();
251ab78c84dSChris Mason 			finish_wait(&info->transaction_throttle, &wait);
252ab78c84dSChris Mason 		} while (thr == atomic_read(&info->throttle_gen));
2532dd3e67bSChris Mason 		harder_count++;
2542dd3e67bSChris Mason 
2552dd3e67bSChris Mason 		if (root->fs_info->total_ref_cache_size > 1 * 1024 * 1024 &&
2562dd3e67bSChris Mason 		    harder_count < 2)
2572dd3e67bSChris Mason 			goto harder;
2582dd3e67bSChris Mason 
2592dd3e67bSChris Mason 		if (root->fs_info->total_ref_cache_size > 5 * 1024 * 1024 &&
2602dd3e67bSChris Mason 		    harder_count < 10)
2612dd3e67bSChris Mason 			goto harder;
2622dd3e67bSChris Mason 
2632dd3e67bSChris Mason 		if (root->fs_info->total_ref_cache_size > 10 * 1024 * 1024 &&
2642dd3e67bSChris Mason 		    harder_count < 20)
2652dd3e67bSChris Mason 			goto harder;
266ab78c84dSChris Mason 	}
267ab78c84dSChris Mason }
2685d4f98a2SYan Zheng #endif
269ab78c84dSChris Mason 
27037d1aeeeSChris Mason void btrfs_throttle(struct btrfs_root *root)
27137d1aeeeSChris Mason {
27237d1aeeeSChris Mason 	mutex_lock(&root->fs_info->trans_mutex);
2739ca9ee09SSage Weil 	if (!root->fs_info->open_ioctl_trans)
27437d1aeeeSChris Mason 		wait_current_trans(root);
27537d1aeeeSChris Mason 	mutex_unlock(&root->fs_info->trans_mutex);
27637d1aeeeSChris Mason }
27737d1aeeeSChris Mason 
27889ce8a63SChris Mason static int __btrfs_end_transaction(struct btrfs_trans_handle *trans,
27989ce8a63SChris Mason 			  struct btrfs_root *root, int throttle)
28079154b1bSChris Mason {
28179154b1bSChris Mason 	struct btrfs_transaction *cur_trans;
282ab78c84dSChris Mason 	struct btrfs_fs_info *info = root->fs_info;
283c3e69d58SChris Mason 	int count = 0;
284d6e4a428SChris Mason 
285c3e69d58SChris Mason 	while (count < 4) {
286c3e69d58SChris Mason 		unsigned long cur = trans->delayed_ref_updates;
287c3e69d58SChris Mason 		trans->delayed_ref_updates = 0;
288c3e69d58SChris Mason 		if (cur &&
289c3e69d58SChris Mason 		    trans->transaction->delayed_refs.num_heads_ready > 64) {
290c3e69d58SChris Mason 			trans->delayed_ref_updates = 0;
291b7ec40d7SChris Mason 
292b7ec40d7SChris Mason 			/*
293b7ec40d7SChris Mason 			 * do a full flush if the transaction is trying
294b7ec40d7SChris Mason 			 * to close
295b7ec40d7SChris Mason 			 */
296b7ec40d7SChris Mason 			if (trans->transaction->delayed_refs.flushing)
297b7ec40d7SChris Mason 				cur = 0;
298c3e69d58SChris Mason 			btrfs_run_delayed_refs(trans, root, cur);
299c3e69d58SChris Mason 		} else {
300c3e69d58SChris Mason 			break;
301c3e69d58SChris Mason 		}
302c3e69d58SChris Mason 		count++;
30356bec294SChris Mason 	}
30456bec294SChris Mason 
305ab78c84dSChris Mason 	mutex_lock(&info->trans_mutex);
306ab78c84dSChris Mason 	cur_trans = info->running_transaction;
307ccd467d6SChris Mason 	WARN_ON(cur_trans != trans->transaction);
308d5719762SChris Mason 	WARN_ON(cur_trans->num_writers < 1);
309ccd467d6SChris Mason 	cur_trans->num_writers--;
31089ce8a63SChris Mason 
31179154b1bSChris Mason 	if (waitqueue_active(&cur_trans->writer_wait))
31279154b1bSChris Mason 		wake_up(&cur_trans->writer_wait);
31379154b1bSChris Mason 	put_transaction(cur_trans);
314ab78c84dSChris Mason 	mutex_unlock(&info->trans_mutex);
315d6025579SChris Mason 	memset(trans, 0, sizeof(*trans));
3162c90e5d6SChris Mason 	kmem_cache_free(btrfs_trans_handle_cachep, trans);
317ab78c84dSChris Mason 
31879154b1bSChris Mason 	return 0;
31979154b1bSChris Mason }
32079154b1bSChris Mason 
32189ce8a63SChris Mason int btrfs_end_transaction(struct btrfs_trans_handle *trans,
32289ce8a63SChris Mason 			  struct btrfs_root *root)
32389ce8a63SChris Mason {
32489ce8a63SChris Mason 	return __btrfs_end_transaction(trans, root, 0);
32589ce8a63SChris Mason }
32689ce8a63SChris Mason 
32789ce8a63SChris Mason int btrfs_end_transaction_throttle(struct btrfs_trans_handle *trans,
32889ce8a63SChris Mason 				   struct btrfs_root *root)
32989ce8a63SChris Mason {
33089ce8a63SChris Mason 	return __btrfs_end_transaction(trans, root, 1);
33189ce8a63SChris Mason }
33289ce8a63SChris Mason 
333d352ac68SChris Mason /*
334d352ac68SChris Mason  * when btree blocks are allocated, they have some corresponding bits set for
335d352ac68SChris Mason  * them in one of two extent_io trees.  This is used to make sure all of
336d352ac68SChris Mason  * those extents are on disk for transaction or log commit
337d352ac68SChris Mason  */
338d0c803c4SChris Mason int btrfs_write_and_wait_marked_extents(struct btrfs_root *root,
339d0c803c4SChris Mason 					struct extent_io_tree *dirty_pages)
34079154b1bSChris Mason {
3417c4452b9SChris Mason 	int ret;
342777e6bd7SChris Mason 	int err = 0;
3437c4452b9SChris Mason 	int werr = 0;
3447c4452b9SChris Mason 	struct page *page;
3457c4452b9SChris Mason 	struct inode *btree_inode = root->fs_info->btree_inode;
346777e6bd7SChris Mason 	u64 start = 0;
3475f39d397SChris Mason 	u64 end;
3485f39d397SChris Mason 	unsigned long index;
3497c4452b9SChris Mason 
3507c4452b9SChris Mason 	while (1) {
351777e6bd7SChris Mason 		ret = find_first_extent_bit(dirty_pages, start, &start, &end,
3525f39d397SChris Mason 					    EXTENT_DIRTY);
3535f39d397SChris Mason 		if (ret)
3547c4452b9SChris Mason 			break;
3555f39d397SChris Mason 		while (start <= end) {
356777e6bd7SChris Mason 			cond_resched();
357777e6bd7SChris Mason 
3585f39d397SChris Mason 			index = start >> PAGE_CACHE_SHIFT;
35935ebb934SChris Mason 			start = (u64)(index + 1) << PAGE_CACHE_SHIFT;
3604bef0848SChris Mason 			page = find_get_page(btree_inode->i_mapping, index);
3617c4452b9SChris Mason 			if (!page)
3627c4452b9SChris Mason 				continue;
3634bef0848SChris Mason 
3644bef0848SChris Mason 			btree_lock_page_hook(page);
3654bef0848SChris Mason 			if (!page->mapping) {
3664bef0848SChris Mason 				unlock_page(page);
3674bef0848SChris Mason 				page_cache_release(page);
3684bef0848SChris Mason 				continue;
3694bef0848SChris Mason 			}
3704bef0848SChris Mason 
3716702ed49SChris Mason 			if (PageWriteback(page)) {
3726702ed49SChris Mason 				if (PageDirty(page))
3736702ed49SChris Mason 					wait_on_page_writeback(page);
3746702ed49SChris Mason 				else {
3756702ed49SChris Mason 					unlock_page(page);
3766702ed49SChris Mason 					page_cache_release(page);
3776702ed49SChris Mason 					continue;
3786702ed49SChris Mason 				}
3796702ed49SChris Mason 			}
3807c4452b9SChris Mason 			err = write_one_page(page, 0);
3817c4452b9SChris Mason 			if (err)
3827c4452b9SChris Mason 				werr = err;
3837c4452b9SChris Mason 			page_cache_release(page);
3847c4452b9SChris Mason 		}
3857c4452b9SChris Mason 	}
386777e6bd7SChris Mason 	while (1) {
387777e6bd7SChris Mason 		ret = find_first_extent_bit(dirty_pages, 0, &start, &end,
388777e6bd7SChris Mason 					    EXTENT_DIRTY);
389777e6bd7SChris Mason 		if (ret)
390777e6bd7SChris Mason 			break;
391777e6bd7SChris Mason 
392777e6bd7SChris Mason 		clear_extent_dirty(dirty_pages, start, end, GFP_NOFS);
393777e6bd7SChris Mason 		while (start <= end) {
394777e6bd7SChris Mason 			index = start >> PAGE_CACHE_SHIFT;
395777e6bd7SChris Mason 			start = (u64)(index + 1) << PAGE_CACHE_SHIFT;
396777e6bd7SChris Mason 			page = find_get_page(btree_inode->i_mapping, index);
397777e6bd7SChris Mason 			if (!page)
398777e6bd7SChris Mason 				continue;
399777e6bd7SChris Mason 			if (PageDirty(page)) {
4004bef0848SChris Mason 				btree_lock_page_hook(page);
4014bef0848SChris Mason 				wait_on_page_writeback(page);
402777e6bd7SChris Mason 				err = write_one_page(page, 0);
403777e6bd7SChris Mason 				if (err)
404777e6bd7SChris Mason 					werr = err;
405777e6bd7SChris Mason 			}
406777e6bd7SChris Mason 			wait_on_page_writeback(page);
407777e6bd7SChris Mason 			page_cache_release(page);
408777e6bd7SChris Mason 			cond_resched();
409777e6bd7SChris Mason 		}
410777e6bd7SChris Mason 	}
4117c4452b9SChris Mason 	if (err)
4127c4452b9SChris Mason 		werr = err;
4137c4452b9SChris Mason 	return werr;
41479154b1bSChris Mason }
41579154b1bSChris Mason 
416d0c803c4SChris Mason int btrfs_write_and_wait_transaction(struct btrfs_trans_handle *trans,
417d0c803c4SChris Mason 				     struct btrfs_root *root)
418d0c803c4SChris Mason {
419d0c803c4SChris Mason 	if (!trans || !trans->transaction) {
420d0c803c4SChris Mason 		struct inode *btree_inode;
421d0c803c4SChris Mason 		btree_inode = root->fs_info->btree_inode;
422d0c803c4SChris Mason 		return filemap_write_and_wait(btree_inode->i_mapping);
423d0c803c4SChris Mason 	}
424d0c803c4SChris Mason 	return btrfs_write_and_wait_marked_extents(root,
425d0c803c4SChris Mason 					   &trans->transaction->dirty_pages);
426d0c803c4SChris Mason }
427d0c803c4SChris Mason 
428d352ac68SChris Mason /*
429d352ac68SChris Mason  * this is used to update the root pointer in the tree of tree roots.
430d352ac68SChris Mason  *
431d352ac68SChris Mason  * But, in the case of the extent allocation tree, updating the root
432d352ac68SChris Mason  * pointer may allocate blocks which may change the root of the extent
433d352ac68SChris Mason  * allocation tree.
434d352ac68SChris Mason  *
435d352ac68SChris Mason  * So, this loops and repeats and makes sure the cowonly root didn't
436d352ac68SChris Mason  * change while the root pointer was being updated in the metadata.
437d352ac68SChris Mason  */
4380b86a832SChris Mason static int update_cowonly_root(struct btrfs_trans_handle *trans,
43979154b1bSChris Mason 			       struct btrfs_root *root)
44079154b1bSChris Mason {
44179154b1bSChris Mason 	int ret;
4420b86a832SChris Mason 	u64 old_root_bytenr;
4430b86a832SChris Mason 	struct btrfs_root *tree_root = root->fs_info->tree_root;
44479154b1bSChris Mason 
4450b86a832SChris Mason 	btrfs_write_dirty_block_groups(trans, root);
44656bec294SChris Mason 
44779154b1bSChris Mason 	while (1) {
4480b86a832SChris Mason 		old_root_bytenr = btrfs_root_bytenr(&root->root_item);
4490b86a832SChris Mason 		if (old_root_bytenr == root->node->start)
45079154b1bSChris Mason 			break;
45187ef2bb4SChris Mason 
4525d4f98a2SYan Zheng 		btrfs_set_root_node(&root->root_item, root->node);
45379154b1bSChris Mason 		ret = btrfs_update_root(trans, tree_root,
4540b86a832SChris Mason 					&root->root_key,
4550b86a832SChris Mason 					&root->root_item);
45679154b1bSChris Mason 		BUG_ON(ret);
45756bec294SChris Mason 
4584a8c9a62SYan Zheng 		ret = btrfs_write_dirty_block_groups(trans, root);
45956bec294SChris Mason 		BUG_ON(ret);
4600b86a832SChris Mason 	}
4615d4f98a2SYan Zheng 	free_extent_buffer(root->commit_root);
4625d4f98a2SYan Zheng 	root->commit_root = btrfs_root_node(root);
4630b86a832SChris Mason 	return 0;
4640b86a832SChris Mason }
4650b86a832SChris Mason 
466d352ac68SChris Mason /*
467d352ac68SChris Mason  * update all the cowonly tree roots on disk
468d352ac68SChris Mason  */
4695d4f98a2SYan Zheng static noinline int commit_cowonly_roots(struct btrfs_trans_handle *trans,
4700b86a832SChris Mason 					 struct btrfs_root *root)
4710b86a832SChris Mason {
4720b86a832SChris Mason 	struct btrfs_fs_info *fs_info = root->fs_info;
4730b86a832SChris Mason 	struct list_head *next;
47484234f3aSYan Zheng 	struct extent_buffer *eb;
47556bec294SChris Mason 	int ret;
47684234f3aSYan Zheng 
47756bec294SChris Mason 	ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
47856bec294SChris Mason 	BUG_ON(ret);
47987ef2bb4SChris Mason 
48084234f3aSYan Zheng 	eb = btrfs_lock_root_node(fs_info->tree_root);
4819fa8cfe7SChris Mason 	btrfs_cow_block(trans, fs_info->tree_root, eb, NULL, 0, &eb);
48284234f3aSYan Zheng 	btrfs_tree_unlock(eb);
48384234f3aSYan Zheng 	free_extent_buffer(eb);
4840b86a832SChris Mason 
48556bec294SChris Mason 	ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
48656bec294SChris Mason 	BUG_ON(ret);
48787ef2bb4SChris Mason 
4880b86a832SChris Mason 	while (!list_empty(&fs_info->dirty_cowonly_roots)) {
4890b86a832SChris Mason 		next = fs_info->dirty_cowonly_roots.next;
4900b86a832SChris Mason 		list_del_init(next);
4910b86a832SChris Mason 		root = list_entry(next, struct btrfs_root, dirty_list);
49287ef2bb4SChris Mason 
4930b86a832SChris Mason 		update_cowonly_root(trans, root);
49479154b1bSChris Mason 	}
49579154b1bSChris Mason 	return 0;
49679154b1bSChris Mason }
49779154b1bSChris Mason 
498d352ac68SChris Mason /*
499d352ac68SChris Mason  * dead roots are old snapshots that need to be deleted.  This allocates
500d352ac68SChris Mason  * a dirty root struct and adds it into the list of dead roots that need to
501d352ac68SChris Mason  * be deleted
502d352ac68SChris Mason  */
5035d4f98a2SYan Zheng int btrfs_add_dead_root(struct btrfs_root *root)
5045eda7b5eSChris Mason {
505b48652c1SYan Zheng 	mutex_lock(&root->fs_info->trans_mutex);
5065d4f98a2SYan Zheng 	list_add(&root->root_list, &root->fs_info->dead_roots);
507b48652c1SYan Zheng 	mutex_unlock(&root->fs_info->trans_mutex);
5085eda7b5eSChris Mason 	return 0;
5095eda7b5eSChris Mason }
5105eda7b5eSChris Mason 
511d352ac68SChris Mason /*
5125d4f98a2SYan Zheng  * update all the cowonly tree roots on disk
513d352ac68SChris Mason  */
5145d4f98a2SYan Zheng static noinline int commit_fs_roots(struct btrfs_trans_handle *trans,
5155d4f98a2SYan Zheng 				    struct btrfs_root *root)
5160f7d52f4SChris Mason {
5170f7d52f4SChris Mason 	struct btrfs_root *gang[8];
5185d4f98a2SYan Zheng 	struct btrfs_fs_info *fs_info = root->fs_info;
5190f7d52f4SChris Mason 	int i;
5200f7d52f4SChris Mason 	int ret;
52154aa1f4dSChris Mason 	int err = 0;
52254aa1f4dSChris Mason 
5230f7d52f4SChris Mason 	while (1) {
5245d4f98a2SYan Zheng 		ret = radix_tree_gang_lookup_tag(&fs_info->fs_roots_radix,
5255d4f98a2SYan Zheng 						 (void **)gang, 0,
5260f7d52f4SChris Mason 						 ARRAY_SIZE(gang),
5270f7d52f4SChris Mason 						 BTRFS_ROOT_TRANS_TAG);
5280f7d52f4SChris Mason 		if (ret == 0)
5290f7d52f4SChris Mason 			break;
5300f7d52f4SChris Mason 		for (i = 0; i < ret; i++) {
5310f7d52f4SChris Mason 			root = gang[i];
5325d4f98a2SYan Zheng 			radix_tree_tag_clear(&fs_info->fs_roots_radix,
5332619ba1fSChris Mason 					(unsigned long)root->root_key.objectid,
5340f7d52f4SChris Mason 					BTRFS_ROOT_TRANS_TAG);
53531153d81SYan Zheng 
536e02119d5SChris Mason 			btrfs_free_log(trans, root);
5375d4f98a2SYan Zheng 			btrfs_update_reloc_root(trans, root);
538e02119d5SChris Mason 
539978d910dSYan Zheng 			if (root->commit_root != root->node) {
5405f39d397SChris Mason 				free_extent_buffer(root->commit_root);
5415d4f98a2SYan Zheng 				root->commit_root = btrfs_root_node(root);
542978d910dSYan Zheng 				btrfs_set_root_node(&root->root_item,
543978d910dSYan Zheng 						    root->node);
544978d910dSYan Zheng 			}
54531153d81SYan Zheng 
5465d4f98a2SYan Zheng 			err = btrfs_update_root(trans, fs_info->tree_root,
5470f7d52f4SChris Mason 						&root->root_key,
5480f7d52f4SChris Mason 						&root->root_item);
54954aa1f4dSChris Mason 			if (err)
55054aa1f4dSChris Mason 				break;
5510f7d52f4SChris Mason 		}
5529f3a7427SChris Mason 	}
55354aa1f4dSChris Mason 	return err;
5540f7d52f4SChris Mason }
5550f7d52f4SChris Mason 
556d352ac68SChris Mason /*
557d352ac68SChris Mason  * defrag a given btree.  If cacheonly == 1, this won't read from the disk,
558d352ac68SChris Mason  * otherwise every leaf in the btree is read and defragged.
559d352ac68SChris Mason  */
560e9d0b13bSChris Mason int btrfs_defrag_root(struct btrfs_root *root, int cacheonly)
561e9d0b13bSChris Mason {
562e9d0b13bSChris Mason 	struct btrfs_fs_info *info = root->fs_info;
563e9d0b13bSChris Mason 	int ret;
564e9d0b13bSChris Mason 	struct btrfs_trans_handle *trans;
565d3c2fdcfSChris Mason 	unsigned long nr;
566e9d0b13bSChris Mason 
567a2135011SChris Mason 	smp_mb();
568e9d0b13bSChris Mason 	if (root->defrag_running)
569e9d0b13bSChris Mason 		return 0;
570e9d0b13bSChris Mason 	trans = btrfs_start_transaction(root, 1);
5716b80053dSChris Mason 	while (1) {
572e9d0b13bSChris Mason 		root->defrag_running = 1;
573e9d0b13bSChris Mason 		ret = btrfs_defrag_leaves(trans, root, cacheonly);
574d3c2fdcfSChris Mason 		nr = trans->blocks_used;
575e9d0b13bSChris Mason 		btrfs_end_transaction(trans, root);
576d3c2fdcfSChris Mason 		btrfs_btree_balance_dirty(info->tree_root, nr);
577e9d0b13bSChris Mason 		cond_resched();
578e9d0b13bSChris Mason 
579e9d0b13bSChris Mason 		trans = btrfs_start_transaction(root, 1);
5803f157a2fSChris Mason 		if (root->fs_info->closing || ret != -EAGAIN)
581e9d0b13bSChris Mason 			break;
582e9d0b13bSChris Mason 	}
583e9d0b13bSChris Mason 	root->defrag_running = 0;
584a2135011SChris Mason 	smp_mb();
585e9d0b13bSChris Mason 	btrfs_end_transaction(trans, root);
586e9d0b13bSChris Mason 	return 0;
587e9d0b13bSChris Mason }
588e9d0b13bSChris Mason 
5892c47e605SYan Zheng #if 0
590d352ac68SChris Mason /*
591b7ec40d7SChris Mason  * when dropping snapshots, we generate a ton of delayed refs, and it makes
592b7ec40d7SChris Mason  * sense not to join the transaction while it is trying to flush the current
593b7ec40d7SChris Mason  * queue of delayed refs out.
594b7ec40d7SChris Mason  *
595b7ec40d7SChris Mason  * This is used by the drop snapshot code only
596b7ec40d7SChris Mason  */
597b7ec40d7SChris Mason static noinline int wait_transaction_pre_flush(struct btrfs_fs_info *info)
598b7ec40d7SChris Mason {
599b7ec40d7SChris Mason 	DEFINE_WAIT(wait);
600b7ec40d7SChris Mason 
601b7ec40d7SChris Mason 	mutex_lock(&info->trans_mutex);
602b7ec40d7SChris Mason 	while (info->running_transaction &&
603b7ec40d7SChris Mason 	       info->running_transaction->delayed_refs.flushing) {
604b7ec40d7SChris Mason 		prepare_to_wait(&info->transaction_wait, &wait,
605b7ec40d7SChris Mason 				TASK_UNINTERRUPTIBLE);
606b7ec40d7SChris Mason 		mutex_unlock(&info->trans_mutex);
60759bc5c75SChris Mason 
608b7ec40d7SChris Mason 		schedule();
60959bc5c75SChris Mason 
610b7ec40d7SChris Mason 		mutex_lock(&info->trans_mutex);
611b7ec40d7SChris Mason 		finish_wait(&info->transaction_wait, &wait);
612b7ec40d7SChris Mason 	}
613b7ec40d7SChris Mason 	mutex_unlock(&info->trans_mutex);
614b7ec40d7SChris Mason 	return 0;
615b7ec40d7SChris Mason }
616b7ec40d7SChris Mason 
617b7ec40d7SChris Mason /*
618d352ac68SChris Mason  * Given a list of roots that need to be deleted, call btrfs_drop_snapshot on
619d352ac68SChris Mason  * all of them
620d352ac68SChris Mason  */
6215d4f98a2SYan Zheng int btrfs_drop_dead_root(struct btrfs_root *root)
6220f7d52f4SChris Mason {
6230f7d52f4SChris Mason 	struct btrfs_trans_handle *trans;
6245d4f98a2SYan Zheng 	struct btrfs_root *tree_root = root->fs_info->tree_root;
625d3c2fdcfSChris Mason 	unsigned long nr;
6265d4f98a2SYan Zheng 	int ret;
62758176a96SJosef Bacik 
6289f3a7427SChris Mason 	while (1) {
629b7ec40d7SChris Mason 		/*
630b7ec40d7SChris Mason 		 * we don't want to jump in and create a bunch of
631b7ec40d7SChris Mason 		 * delayed refs if the transaction is starting to close
632b7ec40d7SChris Mason 		 */
633b7ec40d7SChris Mason 		wait_transaction_pre_flush(tree_root->fs_info);
6340f7d52f4SChris Mason 		trans = btrfs_start_transaction(tree_root, 1);
635b7ec40d7SChris Mason 
636b7ec40d7SChris Mason 		/*
637b7ec40d7SChris Mason 		 * we've joined a transaction, make sure it isn't
638b7ec40d7SChris Mason 		 * closing right now
639b7ec40d7SChris Mason 		 */
640b7ec40d7SChris Mason 		if (trans->transaction->delayed_refs.flushing) {
641b7ec40d7SChris Mason 			btrfs_end_transaction(trans, tree_root);
642b7ec40d7SChris Mason 			continue;
643b7ec40d7SChris Mason 		}
644b7ec40d7SChris Mason 
6455d4f98a2SYan Zheng 		ret = btrfs_drop_snapshot(trans, root);
646d397712bSChris Mason 		if (ret != -EAGAIN)
6479f3a7427SChris Mason 			break;
64858176a96SJosef Bacik 
6495d4f98a2SYan Zheng 		ret = btrfs_update_root(trans, tree_root,
6505d4f98a2SYan Zheng 					&root->root_key,
6515d4f98a2SYan Zheng 					&root->root_item);
6525d4f98a2SYan Zheng 		if (ret)
65354aa1f4dSChris Mason 			break;
654bcc63abbSYan 
655d3c2fdcfSChris Mason 		nr = trans->blocks_used;
6560f7d52f4SChris Mason 		ret = btrfs_end_transaction(trans, tree_root);
6570f7d52f4SChris Mason 		BUG_ON(ret);
6585eda7b5eSChris Mason 
659d3c2fdcfSChris Mason 		btrfs_btree_balance_dirty(tree_root, nr);
6604dc11904SChris Mason 		cond_resched();
6610f7d52f4SChris Mason 	}
6625d4f98a2SYan Zheng 	BUG_ON(ret);
6635d4f98a2SYan Zheng 
6645d4f98a2SYan Zheng 	ret = btrfs_del_root(trans, tree_root, &root->root_key);
6655d4f98a2SYan Zheng 	BUG_ON(ret);
6665d4f98a2SYan Zheng 
6675d4f98a2SYan Zheng 	nr = trans->blocks_used;
6685d4f98a2SYan Zheng 	ret = btrfs_end_transaction(trans, tree_root);
6695d4f98a2SYan Zheng 	BUG_ON(ret);
6705d4f98a2SYan Zheng 
6715d4f98a2SYan Zheng 	free_extent_buffer(root->node);
6725d4f98a2SYan Zheng 	free_extent_buffer(root->commit_root);
6735d4f98a2SYan Zheng 	kfree(root);
6745d4f98a2SYan Zheng 
6755d4f98a2SYan Zheng 	btrfs_btree_balance_dirty(tree_root, nr);
67654aa1f4dSChris Mason 	return ret;
6770f7d52f4SChris Mason }
6782c47e605SYan Zheng #endif
6790f7d52f4SChris Mason 
680d352ac68SChris Mason /*
681d352ac68SChris Mason  * new snapshots need to be created at a very specific time in the
682d352ac68SChris Mason  * transaction commit.  This does the actual creation
683d352ac68SChris Mason  */
68480b6794dSChris Mason static noinline int create_pending_snapshot(struct btrfs_trans_handle *trans,
6853063d29fSChris Mason 				   struct btrfs_fs_info *fs_info,
6863063d29fSChris Mason 				   struct btrfs_pending_snapshot *pending)
6873063d29fSChris Mason {
6883063d29fSChris Mason 	struct btrfs_key key;
68980b6794dSChris Mason 	struct btrfs_root_item *new_root_item;
6903063d29fSChris Mason 	struct btrfs_root *tree_root = fs_info->tree_root;
6913063d29fSChris Mason 	struct btrfs_root *root = pending->root;
6923063d29fSChris Mason 	struct extent_buffer *tmp;
693925baeddSChris Mason 	struct extent_buffer *old;
6943063d29fSChris Mason 	int ret;
6953063d29fSChris Mason 	u64 objectid;
6963063d29fSChris Mason 
69780b6794dSChris Mason 	new_root_item = kmalloc(sizeof(*new_root_item), GFP_NOFS);
69880b6794dSChris Mason 	if (!new_root_item) {
69980b6794dSChris Mason 		ret = -ENOMEM;
70080b6794dSChris Mason 		goto fail;
70180b6794dSChris Mason 	}
7023063d29fSChris Mason 	ret = btrfs_find_free_objectid(trans, tree_root, 0, &objectid);
7033063d29fSChris Mason 	if (ret)
7043063d29fSChris Mason 		goto fail;
7053063d29fSChris Mason 
7065d4f98a2SYan Zheng 	record_root_in_trans(trans, root);
70780ff3856SYan Zheng 	btrfs_set_root_last_snapshot(&root->root_item, trans->transid);
70880b6794dSChris Mason 	memcpy(new_root_item, &root->root_item, sizeof(*new_root_item));
7093063d29fSChris Mason 
7103063d29fSChris Mason 	key.objectid = objectid;
7115d4f98a2SYan Zheng 	key.offset = 0;
7123063d29fSChris Mason 	btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
7133063d29fSChris Mason 
714925baeddSChris Mason 	old = btrfs_lock_root_node(root);
7159fa8cfe7SChris Mason 	btrfs_cow_block(trans, root, old, NULL, 0, &old);
7165d4f98a2SYan Zheng 	btrfs_set_lock_blocking(old);
7173063d29fSChris Mason 
718925baeddSChris Mason 	btrfs_copy_root(trans, root, old, &tmp, objectid);
719925baeddSChris Mason 	btrfs_tree_unlock(old);
720925baeddSChris Mason 	free_extent_buffer(old);
7213063d29fSChris Mason 
7225d4f98a2SYan Zheng 	btrfs_set_root_node(new_root_item, tmp);
7233063d29fSChris Mason 	ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
72480b6794dSChris Mason 				new_root_item);
725925baeddSChris Mason 	btrfs_tree_unlock(tmp);
7263063d29fSChris Mason 	free_extent_buffer(tmp);
7273063d29fSChris Mason 	if (ret)
7283063d29fSChris Mason 		goto fail;
7293063d29fSChris Mason 
7303de4586cSChris Mason 	key.offset = (u64)-1;
7313de4586cSChris Mason 	memcpy(&pending->root_key, &key, sizeof(key));
7323de4586cSChris Mason fail:
7333de4586cSChris Mason 	kfree(new_root_item);
7343de4586cSChris Mason 	return ret;
7353de4586cSChris Mason }
7363de4586cSChris Mason 
7373de4586cSChris Mason static noinline int finish_pending_snapshot(struct btrfs_fs_info *fs_info,
7383de4586cSChris Mason 				   struct btrfs_pending_snapshot *pending)
7393de4586cSChris Mason {
7403de4586cSChris Mason 	int ret;
7413de4586cSChris Mason 	int namelen;
7423de4586cSChris Mason 	u64 index = 0;
7433de4586cSChris Mason 	struct btrfs_trans_handle *trans;
7443de4586cSChris Mason 	struct inode *parent_inode;
7453de4586cSChris Mason 	struct inode *inode;
7460660b5afSChris Mason 	struct btrfs_root *parent_root;
7473de4586cSChris Mason 
7483394e160SChris Mason 	parent_inode = pending->dentry->d_parent->d_inode;
7490660b5afSChris Mason 	parent_root = BTRFS_I(parent_inode)->root;
750180591bcSYan Zheng 	trans = btrfs_join_transaction(parent_root, 1);
7513de4586cSChris Mason 
7523063d29fSChris Mason 	/*
7533063d29fSChris Mason 	 * insert the directory item
7543063d29fSChris Mason 	 */
7553b96362cSSven Wegener 	namelen = strlen(pending->name);
7563de4586cSChris Mason 	ret = btrfs_set_inode_index(parent_inode, &index);
7570660b5afSChris Mason 	ret = btrfs_insert_dir_item(trans, parent_root,
7583b96362cSSven Wegener 			    pending->name, namelen,
7593de4586cSChris Mason 			    parent_inode->i_ino,
7603de4586cSChris Mason 			    &pending->root_key, BTRFS_FT_DIR, index);
7613063d29fSChris Mason 
7623063d29fSChris Mason 	if (ret)
7633063d29fSChris Mason 		goto fail;
7640660b5afSChris Mason 
76552c26179SYan Zheng 	btrfs_i_size_write(parent_inode, parent_inode->i_size + namelen * 2);
76652c26179SYan Zheng 	ret = btrfs_update_inode(trans, parent_root, parent_inode);
76752c26179SYan Zheng 	BUG_ON(ret);
76852c26179SYan Zheng 
7690660b5afSChris Mason 	/* add the backref first */
7700660b5afSChris Mason 	ret = btrfs_add_root_ref(trans, parent_root->fs_info->tree_root,
7710660b5afSChris Mason 				 pending->root_key.objectid,
7720660b5afSChris Mason 				 BTRFS_ROOT_BACKREF_KEY,
7730660b5afSChris Mason 				 parent_root->root_key.objectid,
7740660b5afSChris Mason 				 parent_inode->i_ino, index, pending->name,
7750660b5afSChris Mason 				 namelen);
7760660b5afSChris Mason 
7770660b5afSChris Mason 	BUG_ON(ret);
7780660b5afSChris Mason 
7790660b5afSChris Mason 	/* now add the forward ref */
7800660b5afSChris Mason 	ret = btrfs_add_root_ref(trans, parent_root->fs_info->tree_root,
7810660b5afSChris Mason 				 parent_root->root_key.objectid,
7820660b5afSChris Mason 				 BTRFS_ROOT_REF_KEY,
7830660b5afSChris Mason 				 pending->root_key.objectid,
7840660b5afSChris Mason 				 parent_inode->i_ino, index, pending->name,
7850660b5afSChris Mason 				 namelen);
7860660b5afSChris Mason 
7873de4586cSChris Mason 	inode = btrfs_lookup_dentry(parent_inode, pending->dentry);
7883de4586cSChris Mason 	d_instantiate(pending->dentry, inode);
7893063d29fSChris Mason fail:
7903de4586cSChris Mason 	btrfs_end_transaction(trans, fs_info->fs_root);
7913063d29fSChris Mason 	return ret;
7923063d29fSChris Mason }
7933063d29fSChris Mason 
794d352ac68SChris Mason /*
795d352ac68SChris Mason  * create all the snapshots we've scheduled for creation
796d352ac68SChris Mason  */
79780b6794dSChris Mason static noinline int create_pending_snapshots(struct btrfs_trans_handle *trans,
7983063d29fSChris Mason 					     struct btrfs_fs_info *fs_info)
7993063d29fSChris Mason {
8003063d29fSChris Mason 	struct btrfs_pending_snapshot *pending;
8013063d29fSChris Mason 	struct list_head *head = &trans->transaction->pending_snapshots;
8023de4586cSChris Mason 	int ret;
8033de4586cSChris Mason 
804c6e30871SQinghuang Feng 	list_for_each_entry(pending, head, list) {
8053de4586cSChris Mason 		ret = create_pending_snapshot(trans, fs_info, pending);
8063de4586cSChris Mason 		BUG_ON(ret);
8073de4586cSChris Mason 	}
8083de4586cSChris Mason 	return 0;
8093de4586cSChris Mason }
8103de4586cSChris Mason 
8113de4586cSChris Mason static noinline int finish_pending_snapshots(struct btrfs_trans_handle *trans,
8123de4586cSChris Mason 					     struct btrfs_fs_info *fs_info)
8133de4586cSChris Mason {
8143de4586cSChris Mason 	struct btrfs_pending_snapshot *pending;
8153de4586cSChris Mason 	struct list_head *head = &trans->transaction->pending_snapshots;
8163063d29fSChris Mason 	int ret;
8173063d29fSChris Mason 
8183063d29fSChris Mason 	while (!list_empty(head)) {
8193063d29fSChris Mason 		pending = list_entry(head->next,
8203063d29fSChris Mason 				     struct btrfs_pending_snapshot, list);
8213de4586cSChris Mason 		ret = finish_pending_snapshot(fs_info, pending);
8223063d29fSChris Mason 		BUG_ON(ret);
8233063d29fSChris Mason 		list_del(&pending->list);
8243063d29fSChris Mason 		kfree(pending->name);
8253063d29fSChris Mason 		kfree(pending);
8263063d29fSChris Mason 	}
827dc17ff8fSChris Mason 	return 0;
828dc17ff8fSChris Mason }
829dc17ff8fSChris Mason 
8305d4f98a2SYan Zheng static void update_super_roots(struct btrfs_root *root)
8315d4f98a2SYan Zheng {
8325d4f98a2SYan Zheng 	struct btrfs_root_item *root_item;
8335d4f98a2SYan Zheng 	struct btrfs_super_block *super;
8345d4f98a2SYan Zheng 
8355d4f98a2SYan Zheng 	super = &root->fs_info->super_copy;
8365d4f98a2SYan Zheng 
8375d4f98a2SYan Zheng 	root_item = &root->fs_info->chunk_root->root_item;
8385d4f98a2SYan Zheng 	super->chunk_root = root_item->bytenr;
8395d4f98a2SYan Zheng 	super->chunk_root_generation = root_item->generation;
8405d4f98a2SYan Zheng 	super->chunk_root_level = root_item->level;
8415d4f98a2SYan Zheng 
8425d4f98a2SYan Zheng 	root_item = &root->fs_info->tree_root->root_item;
8435d4f98a2SYan Zheng 	super->root = root_item->bytenr;
8445d4f98a2SYan Zheng 	super->generation = root_item->generation;
8455d4f98a2SYan Zheng 	super->root_level = root_item->level;
8465d4f98a2SYan Zheng }
8475d4f98a2SYan Zheng 
84879154b1bSChris Mason int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
84979154b1bSChris Mason 			     struct btrfs_root *root)
85079154b1bSChris Mason {
85115ee9bc7SJosef Bacik 	unsigned long joined = 0;
85215ee9bc7SJosef Bacik 	unsigned long timeout = 1;
85379154b1bSChris Mason 	struct btrfs_transaction *cur_trans;
8548fd17795SChris Mason 	struct btrfs_transaction *prev_trans = NULL;
855d1310b2eSChris Mason 	struct extent_io_tree *pinned_copy;
85679154b1bSChris Mason 	DEFINE_WAIT(wait);
85715ee9bc7SJosef Bacik 	int ret;
85889573b9cSChris Mason 	int should_grow = 0;
85989573b9cSChris Mason 	unsigned long now = get_seconds();
860dccae999SSage Weil 	int flush_on_commit = btrfs_test_opt(root, FLUSHONCOMMIT);
86179154b1bSChris Mason 
8625a3f23d5SChris Mason 	btrfs_run_ordered_operations(root, 0);
8635a3f23d5SChris Mason 
86456bec294SChris Mason 	/* make a pass through all the delayed refs we have so far
86556bec294SChris Mason 	 * any runnings procs may add more while we are here
86656bec294SChris Mason 	 */
86756bec294SChris Mason 	ret = btrfs_run_delayed_refs(trans, root, 0);
86856bec294SChris Mason 	BUG_ON(ret);
86956bec294SChris Mason 
870b7ec40d7SChris Mason 	cur_trans = trans->transaction;
87156bec294SChris Mason 	/*
87256bec294SChris Mason 	 * set the flushing flag so procs in this transaction have to
87356bec294SChris Mason 	 * start sending their work down.
87456bec294SChris Mason 	 */
875b7ec40d7SChris Mason 	cur_trans->delayed_refs.flushing = 1;
87656bec294SChris Mason 
877c3e69d58SChris Mason 	ret = btrfs_run_delayed_refs(trans, root, 0);
87856bec294SChris Mason 	BUG_ON(ret);
87956bec294SChris Mason 
88079154b1bSChris Mason 	mutex_lock(&root->fs_info->trans_mutex);
881b7ec40d7SChris Mason 	if (cur_trans->in_commit) {
882b7ec40d7SChris Mason 		cur_trans->use_count++;
883ccd467d6SChris Mason 		mutex_unlock(&root->fs_info->trans_mutex);
88479154b1bSChris Mason 		btrfs_end_transaction(trans, root);
885ccd467d6SChris Mason 
88679154b1bSChris Mason 		ret = wait_for_commit(root, cur_trans);
88779154b1bSChris Mason 		BUG_ON(ret);
88815ee9bc7SJosef Bacik 
88915ee9bc7SJosef Bacik 		mutex_lock(&root->fs_info->trans_mutex);
89079154b1bSChris Mason 		put_transaction(cur_trans);
89115ee9bc7SJosef Bacik 		mutex_unlock(&root->fs_info->trans_mutex);
89215ee9bc7SJosef Bacik 
89379154b1bSChris Mason 		return 0;
89479154b1bSChris Mason 	}
8954313b399SChris Mason 
8964313b399SChris Mason 	pinned_copy = kmalloc(sizeof(*pinned_copy), GFP_NOFS);
8974313b399SChris Mason 	if (!pinned_copy)
8984313b399SChris Mason 		return -ENOMEM;
8994313b399SChris Mason 
900d1310b2eSChris Mason 	extent_io_tree_init(pinned_copy,
9014313b399SChris Mason 			     root->fs_info->btree_inode->i_mapping, GFP_NOFS);
9024313b399SChris Mason 
9032c90e5d6SChris Mason 	trans->transaction->in_commit = 1;
904f9295749SChris Mason 	trans->transaction->blocked = 1;
905ccd467d6SChris Mason 	if (cur_trans->list.prev != &root->fs_info->trans_list) {
906ccd467d6SChris Mason 		prev_trans = list_entry(cur_trans->list.prev,
907ccd467d6SChris Mason 					struct btrfs_transaction, list);
908ccd467d6SChris Mason 		if (!prev_trans->commit_done) {
909ccd467d6SChris Mason 			prev_trans->use_count++;
910ccd467d6SChris Mason 			mutex_unlock(&root->fs_info->trans_mutex);
911ccd467d6SChris Mason 
912ccd467d6SChris Mason 			wait_for_commit(root, prev_trans);
913ccd467d6SChris Mason 
914ccd467d6SChris Mason 			mutex_lock(&root->fs_info->trans_mutex);
91515ee9bc7SJosef Bacik 			put_transaction(prev_trans);
916ccd467d6SChris Mason 		}
917ccd467d6SChris Mason 	}
91815ee9bc7SJosef Bacik 
91989573b9cSChris Mason 	if (now < cur_trans->start_time || now - cur_trans->start_time < 1)
92089573b9cSChris Mason 		should_grow = 1;
92189573b9cSChris Mason 
92215ee9bc7SJosef Bacik 	do {
9237ea394f1SYan Zheng 		int snap_pending = 0;
92415ee9bc7SJosef Bacik 		joined = cur_trans->num_joined;
9257ea394f1SYan Zheng 		if (!list_empty(&trans->transaction->pending_snapshots))
9267ea394f1SYan Zheng 			snap_pending = 1;
9277ea394f1SYan Zheng 
9282c90e5d6SChris Mason 		WARN_ON(cur_trans != trans->transaction);
92915ee9bc7SJosef Bacik 		prepare_to_wait(&cur_trans->writer_wait, &wait,
93079154b1bSChris Mason 				TASK_UNINTERRUPTIBLE);
93115ee9bc7SJosef Bacik 
93215ee9bc7SJosef Bacik 		if (cur_trans->num_writers > 1)
93315ee9bc7SJosef Bacik 			timeout = MAX_SCHEDULE_TIMEOUT;
93489573b9cSChris Mason 		else if (should_grow)
93515ee9bc7SJosef Bacik 			timeout = 1;
93615ee9bc7SJosef Bacik 
93779154b1bSChris Mason 		mutex_unlock(&root->fs_info->trans_mutex);
93815ee9bc7SJosef Bacik 
939dccae999SSage Weil 		if (flush_on_commit || snap_pending) {
940dccae999SSage Weil 			if (flush_on_commit)
941dccae999SSage Weil 				btrfs_start_delalloc_inodes(root);
9427ea394f1SYan Zheng 			ret = btrfs_wait_ordered_extents(root, 1);
9437ea394f1SYan Zheng 			BUG_ON(ret);
9447ea394f1SYan Zheng 		}
9457ea394f1SYan Zheng 
9465a3f23d5SChris Mason 		/*
9475a3f23d5SChris Mason 		 * rename don't use btrfs_join_transaction, so, once we
9485a3f23d5SChris Mason 		 * set the transaction to blocked above, we aren't going
9495a3f23d5SChris Mason 		 * to get any new ordered operations.  We can safely run
9505a3f23d5SChris Mason 		 * it here and no for sure that nothing new will be added
9515a3f23d5SChris Mason 		 * to the list
9525a3f23d5SChris Mason 		 */
9535a3f23d5SChris Mason 		btrfs_run_ordered_operations(root, 1);
9545a3f23d5SChris Mason 
95589573b9cSChris Mason 		smp_mb();
95689573b9cSChris Mason 		if (cur_trans->num_writers > 1 || should_grow)
95715ee9bc7SJosef Bacik 			schedule_timeout(timeout);
95815ee9bc7SJosef Bacik 
95979154b1bSChris Mason 		mutex_lock(&root->fs_info->trans_mutex);
96015ee9bc7SJosef Bacik 		finish_wait(&cur_trans->writer_wait, &wait);
96115ee9bc7SJosef Bacik 	} while (cur_trans->num_writers > 1 ||
96289573b9cSChris Mason 		 (should_grow && cur_trans->num_joined != joined));
96315ee9bc7SJosef Bacik 
9643063d29fSChris Mason 	ret = create_pending_snapshots(trans, root->fs_info);
9653063d29fSChris Mason 	BUG_ON(ret);
9663063d29fSChris Mason 
96756bec294SChris Mason 	ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
96856bec294SChris Mason 	BUG_ON(ret);
96956bec294SChris Mason 
9702c90e5d6SChris Mason 	WARN_ON(cur_trans != trans->transaction);
971dc17ff8fSChris Mason 
972e02119d5SChris Mason 	/* btrfs_commit_tree_roots is responsible for getting the
973e02119d5SChris Mason 	 * various roots consistent with each other.  Every pointer
974e02119d5SChris Mason 	 * in the tree of tree roots has to point to the most up to date
975e02119d5SChris Mason 	 * root for every subvolume and other tree.  So, we have to keep
976e02119d5SChris Mason 	 * the tree logging code from jumping in and changing any
977e02119d5SChris Mason 	 * of the trees.
978e02119d5SChris Mason 	 *
979e02119d5SChris Mason 	 * At this point in the commit, there can't be any tree-log
980e02119d5SChris Mason 	 * writers, but a little lower down we drop the trans mutex
981e02119d5SChris Mason 	 * and let new people in.  By holding the tree_log_mutex
982e02119d5SChris Mason 	 * from now until after the super is written, we avoid races
983e02119d5SChris Mason 	 * with the tree-log code.
984e02119d5SChris Mason 	 */
985e02119d5SChris Mason 	mutex_lock(&root->fs_info->tree_log_mutex);
9861a40e23bSZheng Yan 
9875d4f98a2SYan Zheng 	ret = commit_fs_roots(trans, root);
98854aa1f4dSChris Mason 	BUG_ON(ret);
98954aa1f4dSChris Mason 
9905d4f98a2SYan Zheng 	/* commit_fs_roots gets rid of all the tree log roots, it is now
991e02119d5SChris Mason 	 * safe to free the root of tree log roots
992e02119d5SChris Mason 	 */
993e02119d5SChris Mason 	btrfs_free_log_root_tree(trans, root->fs_info);
994e02119d5SChris Mason 
9955d4f98a2SYan Zheng 	ret = commit_cowonly_roots(trans, root);
99679154b1bSChris Mason 	BUG_ON(ret);
99754aa1f4dSChris Mason 
99878fae27eSChris Mason 	cur_trans = root->fs_info->running_transaction;
999cee36a03SChris Mason 	spin_lock(&root->fs_info->new_trans_lock);
100078fae27eSChris Mason 	root->fs_info->running_transaction = NULL;
1001cee36a03SChris Mason 	spin_unlock(&root->fs_info->new_trans_lock);
10025f39d397SChris Mason 
10035d4f98a2SYan Zheng 	btrfs_set_root_node(&root->fs_info->tree_root->root_item,
10045d4f98a2SYan Zheng 			    root->fs_info->tree_root->node);
10055d4f98a2SYan Zheng 	free_extent_buffer(root->fs_info->tree_root->commit_root);
10065d4f98a2SYan Zheng 	root->fs_info->tree_root->commit_root =
10075d4f98a2SYan Zheng 				btrfs_root_node(root->fs_info->tree_root);
10085d4f98a2SYan Zheng 
10095d4f98a2SYan Zheng 	btrfs_set_root_node(&root->fs_info->chunk_root->root_item,
10105d4f98a2SYan Zheng 			    root->fs_info->chunk_root->node);
10115d4f98a2SYan Zheng 	free_extent_buffer(root->fs_info->chunk_root->commit_root);
10125d4f98a2SYan Zheng 	root->fs_info->chunk_root->commit_root =
10135d4f98a2SYan Zheng 				btrfs_root_node(root->fs_info->chunk_root);
10145d4f98a2SYan Zheng 
10155d4f98a2SYan Zheng 	update_super_roots(root);
1016e02119d5SChris Mason 
1017e02119d5SChris Mason 	if (!root->fs_info->log_root_recovering) {
1018e02119d5SChris Mason 		btrfs_set_super_log_root(&root->fs_info->super_copy, 0);
1019e02119d5SChris Mason 		btrfs_set_super_log_root_level(&root->fs_info->super_copy, 0);
1020e02119d5SChris Mason 	}
1021e02119d5SChris Mason 
1022a061fc8dSChris Mason 	memcpy(&root->fs_info->super_for_commit, &root->fs_info->super_copy,
10234b52dff6SChris Mason 	       sizeof(root->fs_info->super_copy));
1024ccd467d6SChris Mason 
10254313b399SChris Mason 	btrfs_copy_pinned(root, pinned_copy);
1026ccd467d6SChris Mason 
1027f9295749SChris Mason 	trans->transaction->blocked = 0;
1028b7ec40d7SChris Mason 
1029f9295749SChris Mason 	wake_up(&root->fs_info->transaction_wait);
1030e6dcd2dcSChris Mason 
103178fae27eSChris Mason 	mutex_unlock(&root->fs_info->trans_mutex);
103279154b1bSChris Mason 	ret = btrfs_write_and_wait_transaction(trans, root);
103379154b1bSChris Mason 	BUG_ON(ret);
1034a512bbf8SYan Zheng 	write_ctree_super(trans, root, 0);
10354313b399SChris Mason 
1036e02119d5SChris Mason 	/*
1037e02119d5SChris Mason 	 * the super is written, we can safely allow the tree-loggers
1038e02119d5SChris Mason 	 * to go about their business
1039e02119d5SChris Mason 	 */
1040e02119d5SChris Mason 	mutex_unlock(&root->fs_info->tree_log_mutex);
1041e02119d5SChris Mason 
10424313b399SChris Mason 	btrfs_finish_extent_commit(trans, root, pinned_copy);
10434313b399SChris Mason 	kfree(pinned_copy);
10444313b399SChris Mason 
10453de4586cSChris Mason 	/* do the directory inserts of any pending snapshot creations */
10463de4586cSChris Mason 	finish_pending_snapshots(trans, root->fs_info);
10473de4586cSChris Mason 
10481a40e23bSZheng Yan 	mutex_lock(&root->fs_info->trans_mutex);
10491a40e23bSZheng Yan 
10502c90e5d6SChris Mason 	cur_trans->commit_done = 1;
1051b7ec40d7SChris Mason 
105215ee9bc7SJosef Bacik 	root->fs_info->last_trans_committed = cur_trans->transid;
10532c90e5d6SChris Mason 	wake_up(&cur_trans->commit_wait);
10543de4586cSChris Mason 
105579154b1bSChris Mason 	put_transaction(cur_trans);
105678fae27eSChris Mason 	put_transaction(cur_trans);
105758176a96SJosef Bacik 
105878fae27eSChris Mason 	mutex_unlock(&root->fs_info->trans_mutex);
10593de4586cSChris Mason 
10602c90e5d6SChris Mason 	kmem_cache_free(btrfs_trans_handle_cachep, trans);
106179154b1bSChris Mason 	return ret;
106279154b1bSChris Mason }
106379154b1bSChris Mason 
1064d352ac68SChris Mason /*
1065d352ac68SChris Mason  * interface function to delete all the snapshots we have scheduled for deletion
1066d352ac68SChris Mason  */
1067e9d0b13bSChris Mason int btrfs_clean_old_snapshots(struct btrfs_root *root)
1068e9d0b13bSChris Mason {
10695d4f98a2SYan Zheng 	LIST_HEAD(list);
10705d4f98a2SYan Zheng 	struct btrfs_fs_info *fs_info = root->fs_info;
1071e9d0b13bSChris Mason 
10725d4f98a2SYan Zheng 	mutex_lock(&fs_info->trans_mutex);
10735d4f98a2SYan Zheng 	list_splice_init(&fs_info->dead_roots, &list);
10745d4f98a2SYan Zheng 	mutex_unlock(&fs_info->trans_mutex);
10755d4f98a2SYan Zheng 
10765d4f98a2SYan Zheng 	while (!list_empty(&list)) {
10775d4f98a2SYan Zheng 		root = list_entry(list.next, struct btrfs_root, root_list);
10785d4f98a2SYan Zheng 		list_del_init(&root->root_list);
10792c47e605SYan Zheng 		btrfs_drop_snapshot(root, 0);
1080e9d0b13bSChris Mason 	}
1081e9d0b13bSChris Mason 	return 0;
1082e9d0b13bSChris Mason }
1083