xref: /openbmc/linux/fs/btrfs/transaction.c (revision 59bc5c75)
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"
2831153d81SYan Zheng #include "ref-cache.h"
29e02119d5SChris Mason #include "tree-log.h"
3079154b1bSChris Mason 
310f7d52f4SChris Mason #define BTRFS_ROOT_TRANS_TAG 0
320f7d52f4SChris Mason 
3380b6794dSChris Mason static noinline void put_transaction(struct btrfs_transaction *transaction)
3479154b1bSChris Mason {
352c90e5d6SChris Mason 	WARN_ON(transaction->use_count == 0);
3679154b1bSChris Mason 	transaction->use_count--;
3778fae27eSChris Mason 	if (transaction->use_count == 0) {
388fd17795SChris Mason 		list_del_init(&transaction->list);
392c90e5d6SChris Mason 		memset(transaction, 0, sizeof(*transaction));
402c90e5d6SChris Mason 		kmem_cache_free(btrfs_transaction_cachep, transaction);
4179154b1bSChris Mason 	}
4278fae27eSChris Mason }
4379154b1bSChris Mason 
44d352ac68SChris Mason /*
45d352ac68SChris Mason  * either allocate a new transaction or hop into the existing one
46d352ac68SChris Mason  */
4780b6794dSChris Mason static noinline int join_transaction(struct btrfs_root *root)
4879154b1bSChris Mason {
4979154b1bSChris Mason 	struct btrfs_transaction *cur_trans;
5079154b1bSChris Mason 	cur_trans = root->fs_info->running_transaction;
5179154b1bSChris Mason 	if (!cur_trans) {
522c90e5d6SChris Mason 		cur_trans = kmem_cache_alloc(btrfs_transaction_cachep,
532c90e5d6SChris Mason 					     GFP_NOFS);
5479154b1bSChris Mason 		BUG_ON(!cur_trans);
550f7d52f4SChris Mason 		root->fs_info->generation++;
5615ee9bc7SJosef Bacik 		cur_trans->num_writers = 1;
5715ee9bc7SJosef Bacik 		cur_trans->num_joined = 0;
580f7d52f4SChris Mason 		cur_trans->transid = root->fs_info->generation;
5979154b1bSChris Mason 		init_waitqueue_head(&cur_trans->writer_wait);
6079154b1bSChris Mason 		init_waitqueue_head(&cur_trans->commit_wait);
6179154b1bSChris Mason 		cur_trans->in_commit = 0;
62f9295749SChris Mason 		cur_trans->blocked = 0;
63d5719762SChris Mason 		cur_trans->use_count = 1;
6479154b1bSChris Mason 		cur_trans->commit_done = 0;
6508607c1bSChris Mason 		cur_trans->start_time = get_seconds();
6656bec294SChris Mason 
6756bec294SChris Mason 		cur_trans->delayed_refs.root.rb_node = NULL;
6856bec294SChris Mason 		cur_trans->delayed_refs.num_entries = 0;
69c3e69d58SChris Mason 		cur_trans->delayed_refs.num_heads_ready = 0;
70c3e69d58SChris Mason 		cur_trans->delayed_refs.num_heads = 0;
7156bec294SChris Mason 		cur_trans->delayed_refs.flushing = 0;
72c3e69d58SChris Mason 		cur_trans->delayed_refs.run_delayed_start = 0;
7356bec294SChris Mason 		spin_lock_init(&cur_trans->delayed_refs.lock);
7456bec294SChris Mason 
753063d29fSChris Mason 		INIT_LIST_HEAD(&cur_trans->pending_snapshots);
768fd17795SChris Mason 		list_add_tail(&cur_trans->list, &root->fs_info->trans_list);
77d1310b2eSChris Mason 		extent_io_tree_init(&cur_trans->dirty_pages,
785f39d397SChris Mason 				     root->fs_info->btree_inode->i_mapping,
795f39d397SChris Mason 				     GFP_NOFS);
8048ec2cf8SChris Mason 		spin_lock(&root->fs_info->new_trans_lock);
8148ec2cf8SChris Mason 		root->fs_info->running_transaction = cur_trans;
8248ec2cf8SChris Mason 		spin_unlock(&root->fs_info->new_trans_lock);
8315ee9bc7SJosef Bacik 	} else {
8479154b1bSChris Mason 		cur_trans->num_writers++;
8515ee9bc7SJosef Bacik 		cur_trans->num_joined++;
8615ee9bc7SJosef Bacik 	}
8715ee9bc7SJosef Bacik 
8879154b1bSChris Mason 	return 0;
8979154b1bSChris Mason }
9079154b1bSChris Mason 
91d352ac68SChris Mason /*
92d397712bSChris Mason  * this does all the record keeping required to make sure that a reference
93d397712bSChris Mason  * counted root is properly recorded in a given transaction.  This is required
94d397712bSChris Mason  * to make sure the old root from before we joined the transaction is deleted
95d397712bSChris Mason  * when the transaction commits
96d352ac68SChris Mason  */
97e02119d5SChris Mason noinline int btrfs_record_root_in_trans(struct btrfs_root *root)
986702ed49SChris Mason {
99f321e491SYan Zheng 	struct btrfs_dirty_root *dirty;
1006702ed49SChris Mason 	u64 running_trans_id = root->fs_info->running_transaction->transid;
1016702ed49SChris Mason 	if (root->ref_cows && root->last_trans < running_trans_id) {
1026702ed49SChris Mason 		WARN_ON(root == root->fs_info->extent_root);
1036702ed49SChris Mason 		if (root->root_item.refs != 0) {
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);
10731153d81SYan Zheng 
10831153d81SYan Zheng 			dirty = kmalloc(sizeof(*dirty), GFP_NOFS);
10931153d81SYan Zheng 			BUG_ON(!dirty);
11031153d81SYan Zheng 			dirty->root = kmalloc(sizeof(*dirty->root), GFP_NOFS);
11131153d81SYan Zheng 			BUG_ON(!dirty->root);
11231153d81SYan Zheng 			dirty->latest_root = root;
11331153d81SYan Zheng 			INIT_LIST_HEAD(&dirty->list);
11431153d81SYan Zheng 
115925baeddSChris Mason 			root->commit_root = btrfs_root_node(root);
11631153d81SYan Zheng 
11731153d81SYan Zheng 			memcpy(dirty->root, root, sizeof(*root));
11831153d81SYan Zheng 			spin_lock_init(&dirty->root->node_lock);
119bcc63abbSYan 			spin_lock_init(&dirty->root->list_lock);
12031153d81SYan Zheng 			mutex_init(&dirty->root->objectid_mutex);
1215b21f2edSZheng Yan 			mutex_init(&dirty->root->log_mutex);
122bcc63abbSYan 			INIT_LIST_HEAD(&dirty->root->dead_list);
12331153d81SYan Zheng 			dirty->root->node = root->commit_root;
12431153d81SYan Zheng 			dirty->root->commit_root = NULL;
125bcc63abbSYan 
126bcc63abbSYan 			spin_lock(&root->list_lock);
127bcc63abbSYan 			list_add(&dirty->root->dead_list, &root->dead_list);
128bcc63abbSYan 			spin_unlock(&root->list_lock);
129bcc63abbSYan 
130bcc63abbSYan 			root->dirty_root = dirty;
1316702ed49SChris Mason 		} else {
1326702ed49SChris Mason 			WARN_ON(1);
1336702ed49SChris Mason 		}
1346702ed49SChris Mason 		root->last_trans = running_trans_id;
1356702ed49SChris Mason 	}
1366702ed49SChris Mason 	return 0;
1376702ed49SChris Mason }
1386702ed49SChris Mason 
139d352ac68SChris Mason /* wait for commit against the current transaction to become unblocked
140d352ac68SChris Mason  * when this is done, it is safe to start a new transaction, but the current
141d352ac68SChris Mason  * transaction might not be fully on disk.
142d352ac68SChris Mason  */
14337d1aeeeSChris Mason static void wait_current_trans(struct btrfs_root *root)
14479154b1bSChris Mason {
145f9295749SChris Mason 	struct btrfs_transaction *cur_trans;
14679154b1bSChris Mason 
147f9295749SChris Mason 	cur_trans = root->fs_info->running_transaction;
14837d1aeeeSChris Mason 	if (cur_trans && cur_trans->blocked) {
149f9295749SChris Mason 		DEFINE_WAIT(wait);
150f9295749SChris Mason 		cur_trans->use_count++;
151f9295749SChris Mason 		while (1) {
152f9295749SChris Mason 			prepare_to_wait(&root->fs_info->transaction_wait, &wait,
153f9295749SChris Mason 					TASK_UNINTERRUPTIBLE);
154f9295749SChris Mason 			if (cur_trans->blocked) {
155f9295749SChris Mason 				mutex_unlock(&root->fs_info->trans_mutex);
156f9295749SChris Mason 				schedule();
157f9295749SChris Mason 				mutex_lock(&root->fs_info->trans_mutex);
158f9295749SChris Mason 				finish_wait(&root->fs_info->transaction_wait,
159f9295749SChris Mason 					    &wait);
160f9295749SChris Mason 			} else {
161f9295749SChris Mason 				finish_wait(&root->fs_info->transaction_wait,
162f9295749SChris Mason 					    &wait);
163f9295749SChris Mason 				break;
164f9295749SChris Mason 			}
165f9295749SChris Mason 		}
166f9295749SChris Mason 		put_transaction(cur_trans);
167f9295749SChris Mason 	}
16837d1aeeeSChris Mason }
16937d1aeeeSChris Mason 
170e02119d5SChris Mason static struct btrfs_trans_handle *start_transaction(struct btrfs_root *root,
1719ca9ee09SSage Weil 					     int num_blocks, int wait)
17237d1aeeeSChris Mason {
17337d1aeeeSChris Mason 	struct btrfs_trans_handle *h =
17437d1aeeeSChris Mason 		kmem_cache_alloc(btrfs_trans_handle_cachep, GFP_NOFS);
17537d1aeeeSChris Mason 	int ret;
17637d1aeeeSChris Mason 
17737d1aeeeSChris Mason 	mutex_lock(&root->fs_info->trans_mutex);
1784bef0848SChris Mason 	if (!root->fs_info->log_root_recovering &&
1794bef0848SChris Mason 	    ((wait == 1 && !root->fs_info->open_ioctl_trans) || wait == 2))
18037d1aeeeSChris Mason 		wait_current_trans(root);
18179154b1bSChris Mason 	ret = join_transaction(root);
18279154b1bSChris Mason 	BUG_ON(ret);
1830f7d52f4SChris Mason 
184e02119d5SChris Mason 	btrfs_record_root_in_trans(root);
1856702ed49SChris Mason 	h->transid = root->fs_info->running_transaction->transid;
18679154b1bSChris Mason 	h->transaction = root->fs_info->running_transaction;
18779154b1bSChris Mason 	h->blocks_reserved = num_blocks;
18879154b1bSChris Mason 	h->blocks_used = 0;
189d2fb3437SYan Zheng 	h->block_group = 0;
19026b8003fSChris Mason 	h->alloc_exclude_nr = 0;
19126b8003fSChris Mason 	h->alloc_exclude_start = 0;
19256bec294SChris Mason 	h->delayed_ref_updates = 0;
193b7ec40d7SChris Mason 
19479154b1bSChris Mason 	root->fs_info->running_transaction->use_count++;
19579154b1bSChris Mason 	mutex_unlock(&root->fs_info->trans_mutex);
19679154b1bSChris Mason 	return h;
19779154b1bSChris Mason }
19879154b1bSChris Mason 
199f9295749SChris Mason struct btrfs_trans_handle *btrfs_start_transaction(struct btrfs_root *root,
200f9295749SChris Mason 						   int num_blocks)
201f9295749SChris Mason {
2029ca9ee09SSage Weil 	return start_transaction(root, num_blocks, 1);
203f9295749SChris Mason }
204f9295749SChris Mason struct btrfs_trans_handle *btrfs_join_transaction(struct btrfs_root *root,
205f9295749SChris Mason 						   int num_blocks)
206f9295749SChris Mason {
2079ca9ee09SSage Weil 	return start_transaction(root, num_blocks, 0);
208f9295749SChris Mason }
209f9295749SChris Mason 
2109ca9ee09SSage Weil struct btrfs_trans_handle *btrfs_start_ioctl_transaction(struct btrfs_root *r,
2119ca9ee09SSage Weil 							 int num_blocks)
2129ca9ee09SSage Weil {
2139ca9ee09SSage Weil 	return start_transaction(r, num_blocks, 2);
2149ca9ee09SSage Weil }
2159ca9ee09SSage Weil 
216d352ac68SChris Mason /* wait for a transaction commit to be fully complete */
21789ce8a63SChris Mason static noinline int wait_for_commit(struct btrfs_root *root,
21889ce8a63SChris Mason 				    struct btrfs_transaction *commit)
21989ce8a63SChris Mason {
22089ce8a63SChris Mason 	DEFINE_WAIT(wait);
22189ce8a63SChris Mason 	mutex_lock(&root->fs_info->trans_mutex);
22289ce8a63SChris Mason 	while (!commit->commit_done) {
22389ce8a63SChris Mason 		prepare_to_wait(&commit->commit_wait, &wait,
22489ce8a63SChris Mason 				TASK_UNINTERRUPTIBLE);
22589ce8a63SChris Mason 		if (commit->commit_done)
22689ce8a63SChris Mason 			break;
22789ce8a63SChris Mason 		mutex_unlock(&root->fs_info->trans_mutex);
22889ce8a63SChris Mason 		schedule();
22989ce8a63SChris Mason 		mutex_lock(&root->fs_info->trans_mutex);
23089ce8a63SChris Mason 	}
23189ce8a63SChris Mason 	mutex_unlock(&root->fs_info->trans_mutex);
23289ce8a63SChris Mason 	finish_wait(&commit->commit_wait, &wait);
23389ce8a63SChris Mason 	return 0;
23489ce8a63SChris Mason }
23589ce8a63SChris Mason 
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 }
276ab78c84dSChris Mason 
27737d1aeeeSChris Mason void btrfs_throttle(struct btrfs_root *root)
27837d1aeeeSChris Mason {
27937d1aeeeSChris Mason 	mutex_lock(&root->fs_info->trans_mutex);
2809ca9ee09SSage Weil 	if (!root->fs_info->open_ioctl_trans)
28137d1aeeeSChris Mason 		wait_current_trans(root);
28237d1aeeeSChris Mason 	mutex_unlock(&root->fs_info->trans_mutex);
28337d1aeeeSChris Mason 	throttle_on_drops(root);
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);
323d6025579SChris Mason 	memset(trans, 0, sizeof(*trans));
3242c90e5d6SChris Mason 	kmem_cache_free(btrfs_trans_handle_cachep, trans);
325ab78c84dSChris Mason 
326ab78c84dSChris Mason 	if (throttle)
32737d1aeeeSChris Mason 		throttle_on_drops(root);
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
347d352ac68SChris Mason  * those extents are on disk for transaction or log commit
348d352ac68SChris Mason  */
349d0c803c4SChris Mason int btrfs_write_and_wait_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 	}
397777e6bd7SChris Mason 	while (1) {
398777e6bd7SChris Mason 		ret = find_first_extent_bit(dirty_pages, 0, &start, &end,
399777e6bd7SChris Mason 					    EXTENT_DIRTY);
400777e6bd7SChris Mason 		if (ret)
401777e6bd7SChris Mason 			break;
402777e6bd7SChris Mason 
403777e6bd7SChris Mason 		clear_extent_dirty(dirty_pages, start, end, GFP_NOFS);
404777e6bd7SChris Mason 		while (start <= end) {
405777e6bd7SChris Mason 			index = start >> PAGE_CACHE_SHIFT;
406777e6bd7SChris Mason 			start = (u64)(index + 1) << PAGE_CACHE_SHIFT;
407777e6bd7SChris Mason 			page = find_get_page(btree_inode->i_mapping, index);
408777e6bd7SChris Mason 			if (!page)
409777e6bd7SChris Mason 				continue;
410777e6bd7SChris Mason 			if (PageDirty(page)) {
4114bef0848SChris Mason 				btree_lock_page_hook(page);
4124bef0848SChris Mason 				wait_on_page_writeback(page);
413777e6bd7SChris Mason 				err = write_one_page(page, 0);
414777e6bd7SChris Mason 				if (err)
415777e6bd7SChris Mason 					werr = err;
416777e6bd7SChris Mason 			}
417777e6bd7SChris Mason 			wait_on_page_writeback(page);
418777e6bd7SChris Mason 			page_cache_release(page);
419777e6bd7SChris Mason 			cond_resched();
420777e6bd7SChris Mason 		}
421777e6bd7SChris Mason 	}
4227c4452b9SChris Mason 	if (err)
4237c4452b9SChris Mason 		werr = err;
4247c4452b9SChris Mason 	return werr;
42579154b1bSChris Mason }
42679154b1bSChris Mason 
427d0c803c4SChris Mason int btrfs_write_and_wait_transaction(struct btrfs_trans_handle *trans,
428d0c803c4SChris Mason 				     struct btrfs_root *root)
429d0c803c4SChris Mason {
430d0c803c4SChris Mason 	if (!trans || !trans->transaction) {
431d0c803c4SChris Mason 		struct inode *btree_inode;
432d0c803c4SChris Mason 		btree_inode = root->fs_info->btree_inode;
433d0c803c4SChris Mason 		return filemap_write_and_wait(btree_inode->i_mapping);
434d0c803c4SChris Mason 	}
435d0c803c4SChris Mason 	return btrfs_write_and_wait_marked_extents(root,
436d0c803c4SChris Mason 					   &trans->transaction->dirty_pages);
437d0c803c4SChris Mason }
438d0c803c4SChris Mason 
439d352ac68SChris Mason /*
440d352ac68SChris Mason  * this is used to update the root pointer in the tree of tree roots.
441d352ac68SChris Mason  *
442d352ac68SChris Mason  * But, in the case of the extent allocation tree, updating the root
443d352ac68SChris Mason  * pointer may allocate blocks which may change the root of the extent
444d352ac68SChris Mason  * allocation tree.
445d352ac68SChris Mason  *
446d352ac68SChris Mason  * So, this loops and repeats and makes sure the cowonly root didn't
447d352ac68SChris Mason  * change while the root pointer was being updated in the metadata.
448d352ac68SChris Mason  */
4490b86a832SChris Mason static int update_cowonly_root(struct btrfs_trans_handle *trans,
45079154b1bSChris Mason 			       struct btrfs_root *root)
45179154b1bSChris Mason {
45279154b1bSChris Mason 	int ret;
4530b86a832SChris Mason 	u64 old_root_bytenr;
4540b86a832SChris Mason 	struct btrfs_root *tree_root = root->fs_info->tree_root;
45579154b1bSChris Mason 
4560b86a832SChris Mason 	btrfs_write_dirty_block_groups(trans, root);
45756bec294SChris Mason 
45856bec294SChris Mason 	ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
45956bec294SChris Mason 	BUG_ON(ret);
46087ef2bb4SChris Mason 
46179154b1bSChris Mason 	while (1) {
4620b86a832SChris Mason 		old_root_bytenr = btrfs_root_bytenr(&root->root_item);
4630b86a832SChris Mason 		if (old_root_bytenr == root->node->start)
46479154b1bSChris Mason 			break;
4650b86a832SChris Mason 		btrfs_set_root_bytenr(&root->root_item,
4660b86a832SChris Mason 				       root->node->start);
4670b86a832SChris Mason 		btrfs_set_root_level(&root->root_item,
4680b86a832SChris Mason 				     btrfs_header_level(root->node));
46984234f3aSYan Zheng 		btrfs_set_root_generation(&root->root_item, trans->transid);
47087ef2bb4SChris Mason 
47179154b1bSChris Mason 		ret = btrfs_update_root(trans, tree_root,
4720b86a832SChris Mason 					&root->root_key,
4730b86a832SChris Mason 					&root->root_item);
47479154b1bSChris Mason 		BUG_ON(ret);
4750b86a832SChris Mason 		btrfs_write_dirty_block_groups(trans, root);
47656bec294SChris Mason 
47756bec294SChris Mason 		ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
47856bec294SChris Mason 		BUG_ON(ret);
4790b86a832SChris Mason 	}
4800b86a832SChris Mason 	return 0;
4810b86a832SChris Mason }
4820b86a832SChris Mason 
483d352ac68SChris Mason /*
484d352ac68SChris Mason  * update all the cowonly tree roots on disk
485d352ac68SChris Mason  */
4860b86a832SChris Mason int btrfs_commit_tree_roots(struct btrfs_trans_handle *trans,
4870b86a832SChris Mason 			    struct btrfs_root *root)
4880b86a832SChris Mason {
4890b86a832SChris Mason 	struct btrfs_fs_info *fs_info = root->fs_info;
4900b86a832SChris Mason 	struct list_head *next;
49184234f3aSYan Zheng 	struct extent_buffer *eb;
49256bec294SChris Mason 	int ret;
49384234f3aSYan Zheng 
49456bec294SChris Mason 	ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
49556bec294SChris Mason 	BUG_ON(ret);
49687ef2bb4SChris Mason 
49784234f3aSYan Zheng 	eb = btrfs_lock_root_node(fs_info->tree_root);
4989fa8cfe7SChris Mason 	btrfs_cow_block(trans, fs_info->tree_root, eb, NULL, 0, &eb);
49984234f3aSYan Zheng 	btrfs_tree_unlock(eb);
50084234f3aSYan Zheng 	free_extent_buffer(eb);
5010b86a832SChris Mason 
50256bec294SChris Mason 	ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
50356bec294SChris Mason 	BUG_ON(ret);
50487ef2bb4SChris Mason 
5050b86a832SChris Mason 	while (!list_empty(&fs_info->dirty_cowonly_roots)) {
5060b86a832SChris Mason 		next = fs_info->dirty_cowonly_roots.next;
5070b86a832SChris Mason 		list_del_init(next);
5080b86a832SChris Mason 		root = list_entry(next, struct btrfs_root, dirty_list);
50987ef2bb4SChris Mason 
5100b86a832SChris Mason 		update_cowonly_root(trans, root);
51156bec294SChris Mason 
51256bec294SChris Mason 		ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
51356bec294SChris Mason 		BUG_ON(ret);
51479154b1bSChris Mason 	}
51579154b1bSChris Mason 	return 0;
51679154b1bSChris Mason }
51779154b1bSChris Mason 
518d352ac68SChris Mason /*
519d352ac68SChris Mason  * dead roots are old snapshots that need to be deleted.  This allocates
520d352ac68SChris Mason  * a dirty root struct and adds it into the list of dead roots that need to
521d352ac68SChris Mason  * be deleted
522d352ac68SChris Mason  */
523b48652c1SYan Zheng int btrfs_add_dead_root(struct btrfs_root *root, struct btrfs_root *latest)
5245eda7b5eSChris Mason {
525f321e491SYan Zheng 	struct btrfs_dirty_root *dirty;
5265eda7b5eSChris Mason 
5275eda7b5eSChris Mason 	dirty = kmalloc(sizeof(*dirty), GFP_NOFS);
5285eda7b5eSChris Mason 	if (!dirty)
5295eda7b5eSChris Mason 		return -ENOMEM;
5305eda7b5eSChris Mason 	dirty->root = root;
5315ce14bbcSChris Mason 	dirty->latest_root = latest;
532b48652c1SYan Zheng 
533b48652c1SYan Zheng 	mutex_lock(&root->fs_info->trans_mutex);
534b48652c1SYan Zheng 	list_add(&dirty->list, &latest->fs_info->dead_roots);
535b48652c1SYan Zheng 	mutex_unlock(&root->fs_info->trans_mutex);
5365eda7b5eSChris Mason 	return 0;
5375eda7b5eSChris Mason }
5385eda7b5eSChris Mason 
539d352ac68SChris Mason /*
540d352ac68SChris Mason  * at transaction commit time we need to schedule the old roots for
541d352ac68SChris Mason  * deletion via btrfs_drop_snapshot.  This runs through all the
542d352ac68SChris Mason  * reference counted roots that were modified in the current
543d352ac68SChris Mason  * transaction and puts them into the drop list
544d352ac68SChris Mason  */
54580b6794dSChris Mason static noinline int add_dirty_roots(struct btrfs_trans_handle *trans,
54635b7e476SChris Mason 				    struct radix_tree_root *radix,
54735b7e476SChris Mason 				    struct list_head *list)
5480f7d52f4SChris Mason {
549f321e491SYan Zheng 	struct btrfs_dirty_root *dirty;
5500f7d52f4SChris Mason 	struct btrfs_root *gang[8];
5510f7d52f4SChris Mason 	struct btrfs_root *root;
5520f7d52f4SChris Mason 	int i;
5530f7d52f4SChris Mason 	int ret;
55454aa1f4dSChris Mason 	int err = 0;
5555eda7b5eSChris Mason 	u32 refs;
55654aa1f4dSChris Mason 
5570f7d52f4SChris Mason 	while (1) {
5580f7d52f4SChris Mason 		ret = radix_tree_gang_lookup_tag(radix, (void **)gang, 0,
5590f7d52f4SChris Mason 						 ARRAY_SIZE(gang),
5600f7d52f4SChris Mason 						 BTRFS_ROOT_TRANS_TAG);
5610f7d52f4SChris Mason 		if (ret == 0)
5620f7d52f4SChris Mason 			break;
5630f7d52f4SChris Mason 		for (i = 0; i < ret; i++) {
5640f7d52f4SChris Mason 			root = gang[i];
5652619ba1fSChris Mason 			radix_tree_tag_clear(radix,
5662619ba1fSChris Mason 				     (unsigned long)root->root_key.objectid,
5670f7d52f4SChris Mason 				     BTRFS_ROOT_TRANS_TAG);
56831153d81SYan Zheng 
56931153d81SYan Zheng 			BUG_ON(!root->ref_tree);
570017e5369SChris Mason 			dirty = root->dirty_root;
57131153d81SYan Zheng 
572e02119d5SChris Mason 			btrfs_free_log(trans, root);
573f82d02d9SYan Zheng 			btrfs_free_reloc_root(trans, root);
574e02119d5SChris Mason 
5750f7d52f4SChris Mason 			if (root->commit_root == root->node) {
576db94535dSChris Mason 				WARN_ON(root->node->start !=
577db94535dSChris Mason 					btrfs_root_bytenr(&root->root_item));
57831153d81SYan Zheng 
5795f39d397SChris Mason 				free_extent_buffer(root->commit_root);
5800f7d52f4SChris Mason 				root->commit_root = NULL;
5817ea394f1SYan Zheng 				root->dirty_root = NULL;
58231153d81SYan Zheng 
583bcc63abbSYan 				spin_lock(&root->list_lock);
584bcc63abbSYan 				list_del_init(&dirty->root->dead_list);
585bcc63abbSYan 				spin_unlock(&root->list_lock);
586bcc63abbSYan 
58731153d81SYan Zheng 				kfree(dirty->root);
58831153d81SYan Zheng 				kfree(dirty);
58958176a96SJosef Bacik 
59058176a96SJosef Bacik 				/* make sure to update the root on disk
59158176a96SJosef Bacik 				 * so we get any updates to the block used
59258176a96SJosef Bacik 				 * counts
59358176a96SJosef Bacik 				 */
59458176a96SJosef Bacik 				err = btrfs_update_root(trans,
59558176a96SJosef Bacik 						root->fs_info->tree_root,
59658176a96SJosef Bacik 						&root->root_key,
59758176a96SJosef Bacik 						&root->root_item);
5980f7d52f4SChris Mason 				continue;
5990f7d52f4SChris Mason 			}
6009f3a7427SChris Mason 
6019f3a7427SChris Mason 			memset(&root->root_item.drop_progress, 0,
6029f3a7427SChris Mason 			       sizeof(struct btrfs_disk_key));
6039f3a7427SChris Mason 			root->root_item.drop_level = 0;
6040f7d52f4SChris Mason 			root->commit_root = NULL;
6057ea394f1SYan Zheng 			root->dirty_root = NULL;
6060f7d52f4SChris Mason 			root->root_key.offset = root->fs_info->generation;
607db94535dSChris Mason 			btrfs_set_root_bytenr(&root->root_item,
608db94535dSChris Mason 					      root->node->start);
609db94535dSChris Mason 			btrfs_set_root_level(&root->root_item,
610db94535dSChris Mason 					     btrfs_header_level(root->node));
61184234f3aSYan Zheng 			btrfs_set_root_generation(&root->root_item,
61284234f3aSYan Zheng 						  root->root_key.offset);
61384234f3aSYan Zheng 
6140f7d52f4SChris Mason 			err = btrfs_insert_root(trans, root->fs_info->tree_root,
6150f7d52f4SChris Mason 						&root->root_key,
6160f7d52f4SChris Mason 						&root->root_item);
61754aa1f4dSChris Mason 			if (err)
61854aa1f4dSChris Mason 				break;
6199f3a7427SChris Mason 
6209f3a7427SChris Mason 			refs = btrfs_root_refs(&dirty->root->root_item);
6219f3a7427SChris Mason 			btrfs_set_root_refs(&dirty->root->root_item, refs - 1);
6225eda7b5eSChris Mason 			err = btrfs_update_root(trans, root->fs_info->tree_root,
6239f3a7427SChris Mason 						&dirty->root->root_key,
6249f3a7427SChris Mason 						&dirty->root->root_item);
6255eda7b5eSChris Mason 
6265eda7b5eSChris Mason 			BUG_ON(err);
6279f3a7427SChris Mason 			if (refs == 1) {
6280f7d52f4SChris Mason 				list_add(&dirty->list, list);
6299f3a7427SChris Mason 			} else {
6309f3a7427SChris Mason 				WARN_ON(1);
63131153d81SYan Zheng 				free_extent_buffer(dirty->root->node);
6329f3a7427SChris Mason 				kfree(dirty->root);
6335eda7b5eSChris Mason 				kfree(dirty);
6340f7d52f4SChris Mason 			}
6350f7d52f4SChris Mason 		}
6369f3a7427SChris Mason 	}
63754aa1f4dSChris Mason 	return err;
6380f7d52f4SChris Mason }
6390f7d52f4SChris Mason 
640d352ac68SChris Mason /*
641d352ac68SChris Mason  * defrag a given btree.  If cacheonly == 1, this won't read from the disk,
642d352ac68SChris Mason  * otherwise every leaf in the btree is read and defragged.
643d352ac68SChris Mason  */
644e9d0b13bSChris Mason int btrfs_defrag_root(struct btrfs_root *root, int cacheonly)
645e9d0b13bSChris Mason {
646e9d0b13bSChris Mason 	struct btrfs_fs_info *info = root->fs_info;
647e9d0b13bSChris Mason 	int ret;
648e9d0b13bSChris Mason 	struct btrfs_trans_handle *trans;
649d3c2fdcfSChris Mason 	unsigned long nr;
650e9d0b13bSChris Mason 
651a2135011SChris Mason 	smp_mb();
652e9d0b13bSChris Mason 	if (root->defrag_running)
653e9d0b13bSChris Mason 		return 0;
654e9d0b13bSChris Mason 	trans = btrfs_start_transaction(root, 1);
6556b80053dSChris Mason 	while (1) {
656e9d0b13bSChris Mason 		root->defrag_running = 1;
657e9d0b13bSChris Mason 		ret = btrfs_defrag_leaves(trans, root, cacheonly);
658d3c2fdcfSChris Mason 		nr = trans->blocks_used;
659e9d0b13bSChris Mason 		btrfs_end_transaction(trans, root);
660d3c2fdcfSChris Mason 		btrfs_btree_balance_dirty(info->tree_root, nr);
661e9d0b13bSChris Mason 		cond_resched();
662e9d0b13bSChris Mason 
663e9d0b13bSChris Mason 		trans = btrfs_start_transaction(root, 1);
6643f157a2fSChris Mason 		if (root->fs_info->closing || ret != -EAGAIN)
665e9d0b13bSChris Mason 			break;
666e9d0b13bSChris Mason 	}
667e9d0b13bSChris Mason 	root->defrag_running = 0;
668a2135011SChris Mason 	smp_mb();
669e9d0b13bSChris Mason 	btrfs_end_transaction(trans, root);
670e9d0b13bSChris Mason 	return 0;
671e9d0b13bSChris Mason }
672e9d0b13bSChris Mason 
673d352ac68SChris Mason /*
674b7ec40d7SChris Mason  * when dropping snapshots, we generate a ton of delayed refs, and it makes
675b7ec40d7SChris Mason  * sense not to join the transaction while it is trying to flush the current
676b7ec40d7SChris Mason  * queue of delayed refs out.
677b7ec40d7SChris Mason  *
678b7ec40d7SChris Mason  * This is used by the drop snapshot code only
679b7ec40d7SChris Mason  */
680b7ec40d7SChris Mason static noinline int wait_transaction_pre_flush(struct btrfs_fs_info *info)
681b7ec40d7SChris Mason {
682b7ec40d7SChris Mason 	DEFINE_WAIT(wait);
683b7ec40d7SChris Mason 
684b7ec40d7SChris Mason 	mutex_lock(&info->trans_mutex);
685b7ec40d7SChris Mason 	while (info->running_transaction &&
686b7ec40d7SChris Mason 	       info->running_transaction->delayed_refs.flushing) {
687b7ec40d7SChris Mason 		prepare_to_wait(&info->transaction_wait, &wait,
688b7ec40d7SChris Mason 				TASK_UNINTERRUPTIBLE);
689b7ec40d7SChris Mason 		mutex_unlock(&info->trans_mutex);
69059bc5c75SChris Mason 
69159bc5c75SChris Mason 		atomic_dec(&info->throttles);
69259bc5c75SChris Mason 		wake_up(&info->transaction_throttle);
69359bc5c75SChris Mason 
694b7ec40d7SChris Mason 		schedule();
69559bc5c75SChris Mason 
69659bc5c75SChris Mason 		atomic_inc(&info->throttles);
697b7ec40d7SChris Mason 		mutex_lock(&info->trans_mutex);
698b7ec40d7SChris Mason 		finish_wait(&info->transaction_wait, &wait);
699b7ec40d7SChris Mason 	}
700b7ec40d7SChris Mason 	mutex_unlock(&info->trans_mutex);
701b7ec40d7SChris Mason 	return 0;
702b7ec40d7SChris Mason }
703b7ec40d7SChris Mason 
704b7ec40d7SChris Mason /*
705d352ac68SChris Mason  * Given a list of roots that need to be deleted, call btrfs_drop_snapshot on
706d352ac68SChris Mason  * all of them
707d352ac68SChris Mason  */
70880b6794dSChris Mason static noinline int drop_dirty_roots(struct btrfs_root *tree_root,
70935b7e476SChris Mason 				     struct list_head *list)
7100f7d52f4SChris Mason {
711f321e491SYan Zheng 	struct btrfs_dirty_root *dirty;
7120f7d52f4SChris Mason 	struct btrfs_trans_handle *trans;
713d3c2fdcfSChris Mason 	unsigned long nr;
714db94535dSChris Mason 	u64 num_bytes;
715db94535dSChris Mason 	u64 bytes_used;
716bcc63abbSYan 	u64 max_useless;
71754aa1f4dSChris Mason 	int ret = 0;
7189f3a7427SChris Mason 	int err;
7199f3a7427SChris Mason 
7200f7d52f4SChris Mason 	while (!list_empty(list)) {
72158176a96SJosef Bacik 		struct btrfs_root *root;
72258176a96SJosef Bacik 
723f321e491SYan Zheng 		dirty = list_entry(list->prev, struct btrfs_dirty_root, list);
7240f7d52f4SChris Mason 		list_del_init(&dirty->list);
7255eda7b5eSChris Mason 
726db94535dSChris Mason 		num_bytes = btrfs_root_used(&dirty->root->root_item);
72758176a96SJosef Bacik 		root = dirty->latest_root;
728a2135011SChris Mason 		atomic_inc(&root->fs_info->throttles);
72958176a96SJosef Bacik 
7309f3a7427SChris Mason 		while (1) {
731b7ec40d7SChris Mason 			/*
732b7ec40d7SChris Mason 			 * we don't want to jump in and create a bunch of
733b7ec40d7SChris Mason 			 * delayed refs if the transaction is starting to close
734b7ec40d7SChris Mason 			 */
735b7ec40d7SChris Mason 			wait_transaction_pre_flush(tree_root->fs_info);
7360f7d52f4SChris Mason 			trans = btrfs_start_transaction(tree_root, 1);
737b7ec40d7SChris Mason 
738b7ec40d7SChris Mason 			/*
739b7ec40d7SChris Mason 			 * we've joined a transaction, make sure it isn't
740b7ec40d7SChris Mason 			 * closing right now
741b7ec40d7SChris Mason 			 */
742b7ec40d7SChris Mason 			if (trans->transaction->delayed_refs.flushing) {
743b7ec40d7SChris Mason 				btrfs_end_transaction(trans, tree_root);
744b7ec40d7SChris Mason 				continue;
745b7ec40d7SChris Mason 			}
746b7ec40d7SChris Mason 
7475b21f2edSZheng Yan 			mutex_lock(&root->fs_info->drop_mutex);
7489f3a7427SChris Mason 			ret = btrfs_drop_snapshot(trans, dirty->root);
749d397712bSChris Mason 			if (ret != -EAGAIN)
7509f3a7427SChris Mason 				break;
7515b21f2edSZheng Yan 			mutex_unlock(&root->fs_info->drop_mutex);
75258176a96SJosef Bacik 
7539f3a7427SChris Mason 			err = btrfs_update_root(trans,
7549f3a7427SChris Mason 					tree_root,
7559f3a7427SChris Mason 					&dirty->root->root_key,
7569f3a7427SChris Mason 					&dirty->root->root_item);
7579f3a7427SChris Mason 			if (err)
7589f3a7427SChris Mason 				ret = err;
759d3c2fdcfSChris Mason 			nr = trans->blocks_used;
760017e5369SChris Mason 			ret = btrfs_end_transaction(trans, tree_root);
7610f7d52f4SChris Mason 			BUG_ON(ret);
762a2135011SChris Mason 
763d3c2fdcfSChris Mason 			btrfs_btree_balance_dirty(tree_root, nr);
7644dc11904SChris Mason 			cond_resched();
7659f3a7427SChris Mason 		}
7669f3a7427SChris Mason 		BUG_ON(ret);
767a2135011SChris Mason 		atomic_dec(&root->fs_info->throttles);
768017e5369SChris Mason 		wake_up(&root->fs_info->transaction_throttle);
76958176a96SJosef Bacik 
770db94535dSChris Mason 		num_bytes -= btrfs_root_used(&dirty->root->root_item);
771db94535dSChris Mason 		bytes_used = btrfs_root_used(&root->root_item);
772db94535dSChris Mason 		if (num_bytes) {
77324562425SYan Zheng 			mutex_lock(&root->fs_info->trans_mutex);
774e02119d5SChris Mason 			btrfs_record_root_in_trans(root);
77524562425SYan Zheng 			mutex_unlock(&root->fs_info->trans_mutex);
7765f39d397SChris Mason 			btrfs_set_root_used(&root->root_item,
777db94535dSChris Mason 					    bytes_used - num_bytes);
77858176a96SJosef Bacik 		}
779a2135011SChris Mason 
7809f3a7427SChris Mason 		ret = btrfs_del_root(trans, tree_root, &dirty->root->root_key);
78158176a96SJosef Bacik 		if (ret) {
78258176a96SJosef Bacik 			BUG();
78354aa1f4dSChris Mason 			break;
78458176a96SJosef Bacik 		}
785a2135011SChris Mason 		mutex_unlock(&root->fs_info->drop_mutex);
786a2135011SChris Mason 
787bcc63abbSYan 		spin_lock(&root->list_lock);
788bcc63abbSYan 		list_del_init(&dirty->root->dead_list);
789bcc63abbSYan 		if (!list_empty(&root->dead_list)) {
790bcc63abbSYan 			struct btrfs_root *oldest;
791bcc63abbSYan 			oldest = list_entry(root->dead_list.prev,
792bcc63abbSYan 					    struct btrfs_root, dead_list);
793bcc63abbSYan 			max_useless = oldest->root_key.offset - 1;
794bcc63abbSYan 		} else {
795bcc63abbSYan 			max_useless = root->root_key.offset - 1;
796bcc63abbSYan 		}
797bcc63abbSYan 		spin_unlock(&root->list_lock);
798bcc63abbSYan 
799d3c2fdcfSChris Mason 		nr = trans->blocks_used;
8000f7d52f4SChris Mason 		ret = btrfs_end_transaction(trans, tree_root);
8010f7d52f4SChris Mason 		BUG_ON(ret);
8025eda7b5eSChris Mason 
803e4657689SZheng Yan 		ret = btrfs_remove_leaf_refs(root, max_useless, 0);
804bcc63abbSYan 		BUG_ON(ret);
805bcc63abbSYan 
806f510cfecSChris Mason 		free_extent_buffer(dirty->root->node);
8075eda7b5eSChris Mason 		kfree(dirty->root);
8080f7d52f4SChris Mason 		kfree(dirty);
809d3c2fdcfSChris Mason 
810d3c2fdcfSChris Mason 		btrfs_btree_balance_dirty(tree_root, nr);
8114dc11904SChris Mason 		cond_resched();
8120f7d52f4SChris Mason 	}
81354aa1f4dSChris Mason 	return ret;
8140f7d52f4SChris Mason }
8150f7d52f4SChris Mason 
816d352ac68SChris Mason /*
817d352ac68SChris Mason  * new snapshots need to be created at a very specific time in the
818d352ac68SChris Mason  * transaction commit.  This does the actual creation
819d352ac68SChris Mason  */
82080b6794dSChris Mason static noinline int create_pending_snapshot(struct btrfs_trans_handle *trans,
8213063d29fSChris Mason 				   struct btrfs_fs_info *fs_info,
8223063d29fSChris Mason 				   struct btrfs_pending_snapshot *pending)
8233063d29fSChris Mason {
8243063d29fSChris Mason 	struct btrfs_key key;
82580b6794dSChris Mason 	struct btrfs_root_item *new_root_item;
8263063d29fSChris Mason 	struct btrfs_root *tree_root = fs_info->tree_root;
8273063d29fSChris Mason 	struct btrfs_root *root = pending->root;
8283063d29fSChris Mason 	struct extent_buffer *tmp;
829925baeddSChris Mason 	struct extent_buffer *old;
8303063d29fSChris Mason 	int ret;
8313063d29fSChris Mason 	u64 objectid;
8323063d29fSChris Mason 
83380b6794dSChris Mason 	new_root_item = kmalloc(sizeof(*new_root_item), GFP_NOFS);
83480b6794dSChris Mason 	if (!new_root_item) {
83580b6794dSChris Mason 		ret = -ENOMEM;
83680b6794dSChris Mason 		goto fail;
83780b6794dSChris Mason 	}
8383063d29fSChris Mason 	ret = btrfs_find_free_objectid(trans, tree_root, 0, &objectid);
8393063d29fSChris Mason 	if (ret)
8403063d29fSChris Mason 		goto fail;
8413063d29fSChris Mason 
84280ff3856SYan Zheng 	btrfs_record_root_in_trans(root);
84380ff3856SYan Zheng 	btrfs_set_root_last_snapshot(&root->root_item, trans->transid);
84480b6794dSChris Mason 	memcpy(new_root_item, &root->root_item, sizeof(*new_root_item));
8453063d29fSChris Mason 
8463063d29fSChris Mason 	key.objectid = objectid;
8475b21f2edSZheng Yan 	key.offset = trans->transid;
8483063d29fSChris Mason 	btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
8493063d29fSChris Mason 
850925baeddSChris Mason 	old = btrfs_lock_root_node(root);
8519fa8cfe7SChris Mason 	btrfs_cow_block(trans, root, old, NULL, 0, &old);
8523063d29fSChris Mason 
853925baeddSChris Mason 	btrfs_copy_root(trans, root, old, &tmp, objectid);
854925baeddSChris Mason 	btrfs_tree_unlock(old);
855925baeddSChris Mason 	free_extent_buffer(old);
8563063d29fSChris Mason 
85780b6794dSChris Mason 	btrfs_set_root_bytenr(new_root_item, tmp->start);
85880b6794dSChris Mason 	btrfs_set_root_level(new_root_item, btrfs_header_level(tmp));
85984234f3aSYan Zheng 	btrfs_set_root_generation(new_root_item, trans->transid);
8603063d29fSChris Mason 	ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
86180b6794dSChris Mason 				new_root_item);
862925baeddSChris Mason 	btrfs_tree_unlock(tmp);
8633063d29fSChris Mason 	free_extent_buffer(tmp);
8643063d29fSChris Mason 	if (ret)
8653063d29fSChris Mason 		goto fail;
8663063d29fSChris Mason 
8673de4586cSChris Mason 	key.offset = (u64)-1;
8683de4586cSChris Mason 	memcpy(&pending->root_key, &key, sizeof(key));
8693de4586cSChris Mason fail:
8703de4586cSChris Mason 	kfree(new_root_item);
8713de4586cSChris Mason 	return ret;
8723de4586cSChris Mason }
8733de4586cSChris Mason 
8743de4586cSChris Mason static noinline int finish_pending_snapshot(struct btrfs_fs_info *fs_info,
8753de4586cSChris Mason 				   struct btrfs_pending_snapshot *pending)
8763de4586cSChris Mason {
8773de4586cSChris Mason 	int ret;
8783de4586cSChris Mason 	int namelen;
8793de4586cSChris Mason 	u64 index = 0;
8803de4586cSChris Mason 	struct btrfs_trans_handle *trans;
8813de4586cSChris Mason 	struct inode *parent_inode;
8823de4586cSChris Mason 	struct inode *inode;
8830660b5afSChris Mason 	struct btrfs_root *parent_root;
8843de4586cSChris Mason 
8853394e160SChris Mason 	parent_inode = pending->dentry->d_parent->d_inode;
8860660b5afSChris Mason 	parent_root = BTRFS_I(parent_inode)->root;
887180591bcSYan Zheng 	trans = btrfs_join_transaction(parent_root, 1);
8883de4586cSChris Mason 
8893063d29fSChris Mason 	/*
8903063d29fSChris Mason 	 * insert the directory item
8913063d29fSChris Mason 	 */
8923b96362cSSven Wegener 	namelen = strlen(pending->name);
8933de4586cSChris Mason 	ret = btrfs_set_inode_index(parent_inode, &index);
8940660b5afSChris Mason 	ret = btrfs_insert_dir_item(trans, parent_root,
8953b96362cSSven Wegener 			    pending->name, namelen,
8963de4586cSChris Mason 			    parent_inode->i_ino,
8973de4586cSChris Mason 			    &pending->root_key, BTRFS_FT_DIR, index);
8983063d29fSChris Mason 
8993063d29fSChris Mason 	if (ret)
9003063d29fSChris Mason 		goto fail;
9010660b5afSChris Mason 
90252c26179SYan Zheng 	btrfs_i_size_write(parent_inode, parent_inode->i_size + namelen * 2);
90352c26179SYan Zheng 	ret = btrfs_update_inode(trans, parent_root, parent_inode);
90452c26179SYan Zheng 	BUG_ON(ret);
90552c26179SYan Zheng 
9060660b5afSChris Mason 	/* add the backref first */
9070660b5afSChris Mason 	ret = btrfs_add_root_ref(trans, parent_root->fs_info->tree_root,
9080660b5afSChris Mason 				 pending->root_key.objectid,
9090660b5afSChris Mason 				 BTRFS_ROOT_BACKREF_KEY,
9100660b5afSChris Mason 				 parent_root->root_key.objectid,
9110660b5afSChris Mason 				 parent_inode->i_ino, index, pending->name,
9120660b5afSChris Mason 				 namelen);
9130660b5afSChris Mason 
9140660b5afSChris Mason 	BUG_ON(ret);
9150660b5afSChris Mason 
9160660b5afSChris Mason 	/* now add the forward ref */
9170660b5afSChris Mason 	ret = btrfs_add_root_ref(trans, parent_root->fs_info->tree_root,
9180660b5afSChris Mason 				 parent_root->root_key.objectid,
9190660b5afSChris Mason 				 BTRFS_ROOT_REF_KEY,
9200660b5afSChris Mason 				 pending->root_key.objectid,
9210660b5afSChris Mason 				 parent_inode->i_ino, index, pending->name,
9220660b5afSChris Mason 				 namelen);
9230660b5afSChris Mason 
9243de4586cSChris Mason 	inode = btrfs_lookup_dentry(parent_inode, pending->dentry);
9253de4586cSChris Mason 	d_instantiate(pending->dentry, inode);
9263063d29fSChris Mason fail:
9273de4586cSChris Mason 	btrfs_end_transaction(trans, fs_info->fs_root);
9283063d29fSChris Mason 	return ret;
9293063d29fSChris Mason }
9303063d29fSChris Mason 
931d352ac68SChris Mason /*
932d352ac68SChris Mason  * create all the snapshots we've scheduled for creation
933d352ac68SChris Mason  */
93480b6794dSChris Mason static noinline int create_pending_snapshots(struct btrfs_trans_handle *trans,
9353063d29fSChris Mason 					     struct btrfs_fs_info *fs_info)
9363063d29fSChris Mason {
9373063d29fSChris Mason 	struct btrfs_pending_snapshot *pending;
9383063d29fSChris Mason 	struct list_head *head = &trans->transaction->pending_snapshots;
9393de4586cSChris Mason 	int ret;
9403de4586cSChris Mason 
941c6e30871SQinghuang Feng 	list_for_each_entry(pending, head, list) {
9423de4586cSChris Mason 		ret = create_pending_snapshot(trans, fs_info, pending);
9433de4586cSChris Mason 		BUG_ON(ret);
9443de4586cSChris Mason 	}
9453de4586cSChris Mason 	return 0;
9463de4586cSChris Mason }
9473de4586cSChris Mason 
9483de4586cSChris Mason static noinline int finish_pending_snapshots(struct btrfs_trans_handle *trans,
9493de4586cSChris Mason 					     struct btrfs_fs_info *fs_info)
9503de4586cSChris Mason {
9513de4586cSChris Mason 	struct btrfs_pending_snapshot *pending;
9523de4586cSChris Mason 	struct list_head *head = &trans->transaction->pending_snapshots;
9533063d29fSChris Mason 	int ret;
9543063d29fSChris Mason 
9553063d29fSChris Mason 	while (!list_empty(head)) {
9563063d29fSChris Mason 		pending = list_entry(head->next,
9573063d29fSChris Mason 				     struct btrfs_pending_snapshot, list);
9583de4586cSChris Mason 		ret = finish_pending_snapshot(fs_info, pending);
9593063d29fSChris Mason 		BUG_ON(ret);
9603063d29fSChris Mason 		list_del(&pending->list);
9613063d29fSChris Mason 		kfree(pending->name);
9623063d29fSChris Mason 		kfree(pending);
9633063d29fSChris Mason 	}
964dc17ff8fSChris Mason 	return 0;
965dc17ff8fSChris Mason }
966dc17ff8fSChris Mason 
96779154b1bSChris Mason int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
96879154b1bSChris Mason 			     struct btrfs_root *root)
96979154b1bSChris Mason {
97015ee9bc7SJosef Bacik 	unsigned long joined = 0;
97115ee9bc7SJosef Bacik 	unsigned long timeout = 1;
97279154b1bSChris Mason 	struct btrfs_transaction *cur_trans;
9738fd17795SChris Mason 	struct btrfs_transaction *prev_trans = NULL;
9740b86a832SChris Mason 	struct btrfs_root *chunk_root = root->fs_info->chunk_root;
9750f7d52f4SChris Mason 	struct list_head dirty_fs_roots;
976d1310b2eSChris Mason 	struct extent_io_tree *pinned_copy;
97779154b1bSChris Mason 	DEFINE_WAIT(wait);
97815ee9bc7SJosef Bacik 	int ret;
97989573b9cSChris Mason 	int should_grow = 0;
98089573b9cSChris Mason 	unsigned long now = get_seconds();
981dccae999SSage Weil 	int flush_on_commit = btrfs_test_opt(root, FLUSHONCOMMIT);
98279154b1bSChris Mason 
9835a3f23d5SChris Mason 	btrfs_run_ordered_operations(root, 0);
9845a3f23d5SChris Mason 
98556bec294SChris Mason 	/* make a pass through all the delayed refs we have so far
98656bec294SChris Mason 	 * any runnings procs may add more while we are here
98756bec294SChris Mason 	 */
98856bec294SChris Mason 	ret = btrfs_run_delayed_refs(trans, root, 0);
98956bec294SChris Mason 	BUG_ON(ret);
99056bec294SChris Mason 
991b7ec40d7SChris Mason 	cur_trans = trans->transaction;
99256bec294SChris Mason 	/*
99356bec294SChris Mason 	 * set the flushing flag so procs in this transaction have to
99456bec294SChris Mason 	 * start sending their work down.
99556bec294SChris Mason 	 */
996b7ec40d7SChris Mason 	cur_trans->delayed_refs.flushing = 1;
99756bec294SChris Mason 
998c3e69d58SChris Mason 	ret = btrfs_run_delayed_refs(trans, root, 0);
99956bec294SChris Mason 	BUG_ON(ret);
100056bec294SChris Mason 
100179154b1bSChris Mason 	mutex_lock(&root->fs_info->trans_mutex);
1002b7ec40d7SChris Mason 	INIT_LIST_HEAD(&dirty_fs_roots);
1003b7ec40d7SChris Mason 	if (cur_trans->in_commit) {
1004b7ec40d7SChris Mason 		cur_trans->use_count++;
1005ccd467d6SChris Mason 		mutex_unlock(&root->fs_info->trans_mutex);
100679154b1bSChris Mason 		btrfs_end_transaction(trans, root);
1007ccd467d6SChris Mason 
100879154b1bSChris Mason 		ret = wait_for_commit(root, cur_trans);
100979154b1bSChris Mason 		BUG_ON(ret);
101015ee9bc7SJosef Bacik 
101115ee9bc7SJosef Bacik 		mutex_lock(&root->fs_info->trans_mutex);
101279154b1bSChris Mason 		put_transaction(cur_trans);
101315ee9bc7SJosef Bacik 		mutex_unlock(&root->fs_info->trans_mutex);
101415ee9bc7SJosef Bacik 
101579154b1bSChris Mason 		return 0;
101679154b1bSChris Mason 	}
10174313b399SChris Mason 
10184313b399SChris Mason 	pinned_copy = kmalloc(sizeof(*pinned_copy), GFP_NOFS);
10194313b399SChris Mason 	if (!pinned_copy)
10204313b399SChris Mason 		return -ENOMEM;
10214313b399SChris Mason 
1022d1310b2eSChris Mason 	extent_io_tree_init(pinned_copy,
10234313b399SChris Mason 			     root->fs_info->btree_inode->i_mapping, GFP_NOFS);
10244313b399SChris Mason 
10252c90e5d6SChris Mason 	trans->transaction->in_commit = 1;
1026f9295749SChris Mason 	trans->transaction->blocked = 1;
1027ccd467d6SChris Mason 	if (cur_trans->list.prev != &root->fs_info->trans_list) {
1028ccd467d6SChris Mason 		prev_trans = list_entry(cur_trans->list.prev,
1029ccd467d6SChris Mason 					struct btrfs_transaction, list);
1030ccd467d6SChris Mason 		if (!prev_trans->commit_done) {
1031ccd467d6SChris Mason 			prev_trans->use_count++;
1032ccd467d6SChris Mason 			mutex_unlock(&root->fs_info->trans_mutex);
1033ccd467d6SChris Mason 
1034ccd467d6SChris Mason 			wait_for_commit(root, prev_trans);
1035ccd467d6SChris Mason 
1036ccd467d6SChris Mason 			mutex_lock(&root->fs_info->trans_mutex);
103715ee9bc7SJosef Bacik 			put_transaction(prev_trans);
1038ccd467d6SChris Mason 		}
1039ccd467d6SChris Mason 	}
104015ee9bc7SJosef Bacik 
104189573b9cSChris Mason 	if (now < cur_trans->start_time || now - cur_trans->start_time < 1)
104289573b9cSChris Mason 		should_grow = 1;
104389573b9cSChris Mason 
104415ee9bc7SJosef Bacik 	do {
10457ea394f1SYan Zheng 		int snap_pending = 0;
104615ee9bc7SJosef Bacik 		joined = cur_trans->num_joined;
10477ea394f1SYan Zheng 		if (!list_empty(&trans->transaction->pending_snapshots))
10487ea394f1SYan Zheng 			snap_pending = 1;
10497ea394f1SYan Zheng 
10502c90e5d6SChris Mason 		WARN_ON(cur_trans != trans->transaction);
105115ee9bc7SJosef Bacik 		prepare_to_wait(&cur_trans->writer_wait, &wait,
105279154b1bSChris Mason 				TASK_UNINTERRUPTIBLE);
105315ee9bc7SJosef Bacik 
105415ee9bc7SJosef Bacik 		if (cur_trans->num_writers > 1)
105515ee9bc7SJosef Bacik 			timeout = MAX_SCHEDULE_TIMEOUT;
105689573b9cSChris Mason 		else if (should_grow)
105715ee9bc7SJosef Bacik 			timeout = 1;
105815ee9bc7SJosef Bacik 
105979154b1bSChris Mason 		mutex_unlock(&root->fs_info->trans_mutex);
106015ee9bc7SJosef Bacik 
1061dccae999SSage Weil 		if (flush_on_commit || snap_pending) {
1062dccae999SSage Weil 			if (flush_on_commit)
1063dccae999SSage Weil 				btrfs_start_delalloc_inodes(root);
10647ea394f1SYan Zheng 			ret = btrfs_wait_ordered_extents(root, 1);
10657ea394f1SYan Zheng 			BUG_ON(ret);
10667ea394f1SYan Zheng 		}
10677ea394f1SYan Zheng 
10685a3f23d5SChris Mason 		/*
10695a3f23d5SChris Mason 		 * rename don't use btrfs_join_transaction, so, once we
10705a3f23d5SChris Mason 		 * set the transaction to blocked above, we aren't going
10715a3f23d5SChris Mason 		 * to get any new ordered operations.  We can safely run
10725a3f23d5SChris Mason 		 * it here and no for sure that nothing new will be added
10735a3f23d5SChris Mason 		 * to the list
10745a3f23d5SChris Mason 		 */
10755a3f23d5SChris Mason 		btrfs_run_ordered_operations(root, 1);
10765a3f23d5SChris Mason 
107789573b9cSChris Mason 		smp_mb();
107889573b9cSChris Mason 		if (cur_trans->num_writers > 1 || should_grow)
107915ee9bc7SJosef Bacik 			schedule_timeout(timeout);
108015ee9bc7SJosef Bacik 
108179154b1bSChris Mason 		mutex_lock(&root->fs_info->trans_mutex);
108215ee9bc7SJosef Bacik 		finish_wait(&cur_trans->writer_wait, &wait);
108315ee9bc7SJosef Bacik 	} while (cur_trans->num_writers > 1 ||
108489573b9cSChris Mason 		 (should_grow && cur_trans->num_joined != joined));
108515ee9bc7SJosef Bacik 
10863063d29fSChris Mason 	ret = create_pending_snapshots(trans, root->fs_info);
10873063d29fSChris Mason 	BUG_ON(ret);
10883063d29fSChris Mason 
108956bec294SChris Mason 	ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
109056bec294SChris Mason 	BUG_ON(ret);
109156bec294SChris Mason 
10922c90e5d6SChris Mason 	WARN_ON(cur_trans != trans->transaction);
1093dc17ff8fSChris Mason 
1094e02119d5SChris Mason 	/* btrfs_commit_tree_roots is responsible for getting the
1095e02119d5SChris Mason 	 * various roots consistent with each other.  Every pointer
1096e02119d5SChris Mason 	 * in the tree of tree roots has to point to the most up to date
1097e02119d5SChris Mason 	 * root for every subvolume and other tree.  So, we have to keep
1098e02119d5SChris Mason 	 * the tree logging code from jumping in and changing any
1099e02119d5SChris Mason 	 * of the trees.
1100e02119d5SChris Mason 	 *
1101e02119d5SChris Mason 	 * At this point in the commit, there can't be any tree-log
1102e02119d5SChris Mason 	 * writers, but a little lower down we drop the trans mutex
1103e02119d5SChris Mason 	 * and let new people in.  By holding the tree_log_mutex
1104e02119d5SChris Mason 	 * from now until after the super is written, we avoid races
1105e02119d5SChris Mason 	 * with the tree-log code.
1106e02119d5SChris Mason 	 */
1107e02119d5SChris Mason 	mutex_lock(&root->fs_info->tree_log_mutex);
11081a40e23bSZheng Yan 	/*
11091a40e23bSZheng Yan 	 * keep tree reloc code from adding new reloc trees
11101a40e23bSZheng Yan 	 */
11111a40e23bSZheng Yan 	mutex_lock(&root->fs_info->tree_reloc_mutex);
11121a40e23bSZheng Yan 
1113e02119d5SChris Mason 
111454aa1f4dSChris Mason 	ret = add_dirty_roots(trans, &root->fs_info->fs_roots_radix,
111554aa1f4dSChris Mason 			      &dirty_fs_roots);
111654aa1f4dSChris Mason 	BUG_ON(ret);
111754aa1f4dSChris Mason 
1118e02119d5SChris Mason 	/* add_dirty_roots gets rid of all the tree log roots, it is now
1119e02119d5SChris Mason 	 * safe to free the root of tree log roots
1120e02119d5SChris Mason 	 */
1121e02119d5SChris Mason 	btrfs_free_log_root_tree(trans, root->fs_info);
1122e02119d5SChris Mason 
112379154b1bSChris Mason 	ret = btrfs_commit_tree_roots(trans, root);
112479154b1bSChris Mason 	BUG_ON(ret);
112554aa1f4dSChris Mason 
112678fae27eSChris Mason 	cur_trans = root->fs_info->running_transaction;
1127cee36a03SChris Mason 	spin_lock(&root->fs_info->new_trans_lock);
112878fae27eSChris Mason 	root->fs_info->running_transaction = NULL;
1129cee36a03SChris Mason 	spin_unlock(&root->fs_info->new_trans_lock);
11304b52dff6SChris Mason 	btrfs_set_super_generation(&root->fs_info->super_copy,
11314b52dff6SChris Mason 				   cur_trans->transid);
11324b52dff6SChris Mason 	btrfs_set_super_root(&root->fs_info->super_copy,
1133db94535dSChris Mason 			     root->fs_info->tree_root->node->start);
1134db94535dSChris Mason 	btrfs_set_super_root_level(&root->fs_info->super_copy,
1135db94535dSChris Mason 			   btrfs_header_level(root->fs_info->tree_root->node));
11365f39d397SChris Mason 
11370b86a832SChris Mason 	btrfs_set_super_chunk_root(&root->fs_info->super_copy,
11380b86a832SChris Mason 				   chunk_root->node->start);
11390b86a832SChris Mason 	btrfs_set_super_chunk_root_level(&root->fs_info->super_copy,
11400b86a832SChris Mason 					 btrfs_header_level(chunk_root->node));
114184234f3aSYan Zheng 	btrfs_set_super_chunk_root_generation(&root->fs_info->super_copy,
114284234f3aSYan Zheng 				btrfs_header_generation(chunk_root->node));
1143e02119d5SChris Mason 
1144e02119d5SChris Mason 	if (!root->fs_info->log_root_recovering) {
1145e02119d5SChris Mason 		btrfs_set_super_log_root(&root->fs_info->super_copy, 0);
1146e02119d5SChris Mason 		btrfs_set_super_log_root_level(&root->fs_info->super_copy, 0);
1147e02119d5SChris Mason 	}
1148e02119d5SChris Mason 
1149a061fc8dSChris Mason 	memcpy(&root->fs_info->super_for_commit, &root->fs_info->super_copy,
11504b52dff6SChris Mason 	       sizeof(root->fs_info->super_copy));
1151ccd467d6SChris Mason 
11524313b399SChris Mason 	btrfs_copy_pinned(root, pinned_copy);
1153ccd467d6SChris Mason 
1154f9295749SChris Mason 	trans->transaction->blocked = 0;
1155b7ec40d7SChris Mason 
1156e6dcd2dcSChris Mason 	wake_up(&root->fs_info->transaction_throttle);
1157f9295749SChris Mason 	wake_up(&root->fs_info->transaction_wait);
1158e6dcd2dcSChris Mason 
115978fae27eSChris Mason 	mutex_unlock(&root->fs_info->trans_mutex);
116079154b1bSChris Mason 	ret = btrfs_write_and_wait_transaction(trans, root);
116179154b1bSChris Mason 	BUG_ON(ret);
1162a512bbf8SYan Zheng 	write_ctree_super(trans, root, 0);
11634313b399SChris Mason 
1164e02119d5SChris Mason 	/*
1165e02119d5SChris Mason 	 * the super is written, we can safely allow the tree-loggers
1166e02119d5SChris Mason 	 * to go about their business
1167e02119d5SChris Mason 	 */
1168e02119d5SChris Mason 	mutex_unlock(&root->fs_info->tree_log_mutex);
1169e02119d5SChris Mason 
11704313b399SChris Mason 	btrfs_finish_extent_commit(trans, root, pinned_copy);
11714313b399SChris Mason 	kfree(pinned_copy);
11724313b399SChris Mason 
11731a40e23bSZheng Yan 	btrfs_drop_dead_reloc_roots(root);
11741a40e23bSZheng Yan 	mutex_unlock(&root->fs_info->tree_reloc_mutex);
11751a40e23bSZheng Yan 
11763de4586cSChris Mason 	/* do the directory inserts of any pending snapshot creations */
11773de4586cSChris Mason 	finish_pending_snapshots(trans, root->fs_info);
11783de4586cSChris Mason 
11791a40e23bSZheng Yan 	mutex_lock(&root->fs_info->trans_mutex);
11801a40e23bSZheng Yan 
11812c90e5d6SChris Mason 	cur_trans->commit_done = 1;
1182b7ec40d7SChris Mason 
118315ee9bc7SJosef Bacik 	root->fs_info->last_trans_committed = cur_trans->transid;
11842c90e5d6SChris Mason 	wake_up(&cur_trans->commit_wait);
11853de4586cSChris Mason 
118679154b1bSChris Mason 	put_transaction(cur_trans);
118778fae27eSChris Mason 	put_transaction(cur_trans);
118858176a96SJosef Bacik 
1189bcc63abbSYan 	list_splice_init(&dirty_fs_roots, &root->fs_info->dead_roots);
1190facda1e7SChris Mason 	if (root->fs_info->closing)
1191facda1e7SChris Mason 		list_splice_init(&root->fs_info->dead_roots, &dirty_fs_roots);
119258176a96SJosef Bacik 
119378fae27eSChris Mason 	mutex_unlock(&root->fs_info->trans_mutex);
11943de4586cSChris Mason 
11952c90e5d6SChris Mason 	kmem_cache_free(btrfs_trans_handle_cachep, trans);
119679154b1bSChris Mason 
1197d397712bSChris Mason 	if (root->fs_info->closing)
11980f7d52f4SChris Mason 		drop_dirty_roots(root->fs_info->tree_root, &dirty_fs_roots);
119979154b1bSChris Mason 	return ret;
120079154b1bSChris Mason }
120179154b1bSChris Mason 
1202d352ac68SChris Mason /*
1203d352ac68SChris Mason  * interface function to delete all the snapshots we have scheduled for deletion
1204d352ac68SChris Mason  */
1205e9d0b13bSChris Mason int btrfs_clean_old_snapshots(struct btrfs_root *root)
1206e9d0b13bSChris Mason {
1207e9d0b13bSChris Mason 	struct list_head dirty_roots;
1208e9d0b13bSChris Mason 	INIT_LIST_HEAD(&dirty_roots);
1209a74a4b97SChris Mason again:
1210e9d0b13bSChris Mason 	mutex_lock(&root->fs_info->trans_mutex);
1211e9d0b13bSChris Mason 	list_splice_init(&root->fs_info->dead_roots, &dirty_roots);
1212e9d0b13bSChris Mason 	mutex_unlock(&root->fs_info->trans_mutex);
1213e9d0b13bSChris Mason 
1214e9d0b13bSChris Mason 	if (!list_empty(&dirty_roots)) {
1215e9d0b13bSChris Mason 		drop_dirty_roots(root, &dirty_roots);
1216a74a4b97SChris Mason 		goto again;
1217e9d0b13bSChris Mason 	}
1218e9d0b13bSChris Mason 	return 0;
1219e9d0b13bSChris Mason }
1220