xref: /openbmc/linux/fs/btrfs/transaction.c (revision 56bec294)
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++;
56e18e4809SChris Mason 		root->fs_info->last_alloc = 0;
574529ba49SChris Mason 		root->fs_info->last_data_alloc = 0;
5815ee9bc7SJosef Bacik 		cur_trans->num_writers = 1;
5915ee9bc7SJosef Bacik 		cur_trans->num_joined = 0;
600f7d52f4SChris Mason 		cur_trans->transid = root->fs_info->generation;
6179154b1bSChris Mason 		init_waitqueue_head(&cur_trans->writer_wait);
6279154b1bSChris Mason 		init_waitqueue_head(&cur_trans->commit_wait);
6379154b1bSChris Mason 		cur_trans->in_commit = 0;
64f9295749SChris Mason 		cur_trans->blocked = 0;
65d5719762SChris Mason 		cur_trans->use_count = 1;
6679154b1bSChris Mason 		cur_trans->commit_done = 0;
6708607c1bSChris Mason 		cur_trans->start_time = get_seconds();
6856bec294SChris Mason 
6956bec294SChris Mason 		cur_trans->delayed_refs.root.rb_node = NULL;
7056bec294SChris Mason 		cur_trans->delayed_refs.num_entries = 0;
7156bec294SChris Mason 		cur_trans->delayed_refs.flushing = 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  */
96e02119d5SChris Mason noinline int btrfs_record_root_in_trans(struct btrfs_root *root)
976702ed49SChris Mason {
98f321e491SYan Zheng 	struct btrfs_dirty_root *dirty;
996702ed49SChris Mason 	u64 running_trans_id = root->fs_info->running_transaction->transid;
1006702ed49SChris Mason 	if (root->ref_cows && root->last_trans < running_trans_id) {
1016702ed49SChris Mason 		WARN_ON(root == root->fs_info->extent_root);
1026702ed49SChris Mason 		if (root->root_item.refs != 0) {
1036702ed49SChris Mason 			radix_tree_tag_set(&root->fs_info->fs_roots_radix,
1046702ed49SChris Mason 				   (unsigned long)root->root_key.objectid,
1056702ed49SChris Mason 				   BTRFS_ROOT_TRANS_TAG);
10631153d81SYan Zheng 
10731153d81SYan Zheng 			dirty = kmalloc(sizeof(*dirty), GFP_NOFS);
10831153d81SYan Zheng 			BUG_ON(!dirty);
10931153d81SYan Zheng 			dirty->root = kmalloc(sizeof(*dirty->root), GFP_NOFS);
11031153d81SYan Zheng 			BUG_ON(!dirty->root);
11131153d81SYan Zheng 			dirty->latest_root = root;
11231153d81SYan Zheng 			INIT_LIST_HEAD(&dirty->list);
11331153d81SYan Zheng 
114925baeddSChris Mason 			root->commit_root = btrfs_root_node(root);
11531153d81SYan Zheng 
11631153d81SYan Zheng 			memcpy(dirty->root, root, sizeof(*root));
11731153d81SYan Zheng 			spin_lock_init(&dirty->root->node_lock);
118bcc63abbSYan 			spin_lock_init(&dirty->root->list_lock);
11931153d81SYan Zheng 			mutex_init(&dirty->root->objectid_mutex);
1205b21f2edSZheng Yan 			mutex_init(&dirty->root->log_mutex);
121bcc63abbSYan 			INIT_LIST_HEAD(&dirty->root->dead_list);
12231153d81SYan Zheng 			dirty->root->node = root->commit_root;
12331153d81SYan Zheng 			dirty->root->commit_root = NULL;
124bcc63abbSYan 
125bcc63abbSYan 			spin_lock(&root->list_lock);
126bcc63abbSYan 			list_add(&dirty->root->dead_list, &root->dead_list);
127bcc63abbSYan 			spin_unlock(&root->list_lock);
128bcc63abbSYan 
129bcc63abbSYan 			root->dirty_root = dirty;
1306702ed49SChris Mason 		} else {
1316702ed49SChris Mason 			WARN_ON(1);
1326702ed49SChris Mason 		}
1336702ed49SChris Mason 		root->last_trans = running_trans_id;
1346702ed49SChris Mason 	}
1356702ed49SChris Mason 	return 0;
1366702ed49SChris Mason }
1376702ed49SChris Mason 
138d352ac68SChris Mason /* wait for commit against the current transaction to become unblocked
139d352ac68SChris Mason  * when this is done, it is safe to start a new transaction, but the current
140d352ac68SChris Mason  * transaction might not be fully on disk.
141d352ac68SChris Mason  */
14237d1aeeeSChris Mason static void wait_current_trans(struct btrfs_root *root)
14379154b1bSChris Mason {
144f9295749SChris Mason 	struct btrfs_transaction *cur_trans;
14579154b1bSChris Mason 
146f9295749SChris Mason 	cur_trans = root->fs_info->running_transaction;
14737d1aeeeSChris Mason 	if (cur_trans && cur_trans->blocked) {
148f9295749SChris Mason 		DEFINE_WAIT(wait);
149f9295749SChris Mason 		cur_trans->use_count++;
150f9295749SChris Mason 		while (1) {
151f9295749SChris Mason 			prepare_to_wait(&root->fs_info->transaction_wait, &wait,
152f9295749SChris Mason 					TASK_UNINTERRUPTIBLE);
153f9295749SChris Mason 			if (cur_trans->blocked) {
154f9295749SChris Mason 				mutex_unlock(&root->fs_info->trans_mutex);
155f9295749SChris Mason 				schedule();
156f9295749SChris Mason 				mutex_lock(&root->fs_info->trans_mutex);
157f9295749SChris Mason 				finish_wait(&root->fs_info->transaction_wait,
158f9295749SChris Mason 					    &wait);
159f9295749SChris Mason 			} else {
160f9295749SChris Mason 				finish_wait(&root->fs_info->transaction_wait,
161f9295749SChris Mason 					    &wait);
162f9295749SChris Mason 				break;
163f9295749SChris Mason 			}
164f9295749SChris Mason 		}
165f9295749SChris Mason 		put_transaction(cur_trans);
166f9295749SChris Mason 	}
16737d1aeeeSChris Mason }
16837d1aeeeSChris Mason 
169e02119d5SChris Mason static struct btrfs_trans_handle *start_transaction(struct btrfs_root *root,
1709ca9ee09SSage Weil 					     int num_blocks, int wait)
17137d1aeeeSChris Mason {
17237d1aeeeSChris Mason 	struct btrfs_trans_handle *h =
17337d1aeeeSChris Mason 		kmem_cache_alloc(btrfs_trans_handle_cachep, GFP_NOFS);
17437d1aeeeSChris Mason 	int ret;
17537d1aeeeSChris Mason 
17637d1aeeeSChris Mason 	mutex_lock(&root->fs_info->trans_mutex);
1774bef0848SChris Mason 	if (!root->fs_info->log_root_recovering &&
1784bef0848SChris Mason 	    ((wait == 1 && !root->fs_info->open_ioctl_trans) || wait == 2))
17937d1aeeeSChris Mason 		wait_current_trans(root);
18079154b1bSChris Mason 	ret = join_transaction(root);
18179154b1bSChris Mason 	BUG_ON(ret);
1820f7d52f4SChris Mason 
183e02119d5SChris Mason 	btrfs_record_root_in_trans(root);
1846702ed49SChris Mason 	h->transid = root->fs_info->running_transaction->transid;
18579154b1bSChris Mason 	h->transaction = root->fs_info->running_transaction;
18679154b1bSChris Mason 	h->blocks_reserved = num_blocks;
18779154b1bSChris Mason 	h->blocks_used = 0;
188d2fb3437SYan Zheng 	h->block_group = 0;
18926b8003fSChris Mason 	h->alloc_exclude_nr = 0;
19026b8003fSChris Mason 	h->alloc_exclude_start = 0;
19156bec294SChris Mason 	h->delayed_ref_updates = 0;
19279154b1bSChris Mason 	root->fs_info->running_transaction->use_count++;
19379154b1bSChris Mason 	mutex_unlock(&root->fs_info->trans_mutex);
19479154b1bSChris Mason 	return h;
19579154b1bSChris Mason }
19679154b1bSChris Mason 
197f9295749SChris Mason struct btrfs_trans_handle *btrfs_start_transaction(struct btrfs_root *root,
198f9295749SChris Mason 						   int num_blocks)
199f9295749SChris Mason {
2009ca9ee09SSage Weil 	return start_transaction(root, num_blocks, 1);
201f9295749SChris Mason }
202f9295749SChris Mason struct btrfs_trans_handle *btrfs_join_transaction(struct btrfs_root *root,
203f9295749SChris Mason 						   int num_blocks)
204f9295749SChris Mason {
2059ca9ee09SSage Weil 	return start_transaction(root, num_blocks, 0);
206f9295749SChris Mason }
207f9295749SChris Mason 
2089ca9ee09SSage Weil struct btrfs_trans_handle *btrfs_start_ioctl_transaction(struct btrfs_root *r,
2099ca9ee09SSage Weil 							 int num_blocks)
2109ca9ee09SSage Weil {
2119ca9ee09SSage Weil 	return start_transaction(r, num_blocks, 2);
2129ca9ee09SSage Weil }
2139ca9ee09SSage Weil 
214d352ac68SChris Mason /* wait for a transaction commit to be fully complete */
21589ce8a63SChris Mason static noinline int wait_for_commit(struct btrfs_root *root,
21689ce8a63SChris Mason 				    struct btrfs_transaction *commit)
21789ce8a63SChris Mason {
21889ce8a63SChris Mason 	DEFINE_WAIT(wait);
21989ce8a63SChris Mason 	mutex_lock(&root->fs_info->trans_mutex);
22089ce8a63SChris Mason 	while (!commit->commit_done) {
22189ce8a63SChris Mason 		prepare_to_wait(&commit->commit_wait, &wait,
22289ce8a63SChris Mason 				TASK_UNINTERRUPTIBLE);
22389ce8a63SChris Mason 		if (commit->commit_done)
22489ce8a63SChris Mason 			break;
22589ce8a63SChris Mason 		mutex_unlock(&root->fs_info->trans_mutex);
22689ce8a63SChris Mason 		schedule();
22789ce8a63SChris Mason 		mutex_lock(&root->fs_info->trans_mutex);
22889ce8a63SChris Mason 	}
22989ce8a63SChris Mason 	mutex_unlock(&root->fs_info->trans_mutex);
23089ce8a63SChris Mason 	finish_wait(&commit->commit_wait, &wait);
23189ce8a63SChris Mason 	return 0;
23289ce8a63SChris Mason }
23389ce8a63SChris Mason 
234d352ac68SChris Mason /*
235d397712bSChris Mason  * rate limit against the drop_snapshot code.  This helps to slow down new
236d397712bSChris Mason  * operations if the drop_snapshot code isn't able to keep up.
237d352ac68SChris Mason  */
23837d1aeeeSChris Mason static void throttle_on_drops(struct btrfs_root *root)
239ab78c84dSChris Mason {
240ab78c84dSChris Mason 	struct btrfs_fs_info *info = root->fs_info;
2412dd3e67bSChris Mason 	int harder_count = 0;
242ab78c84dSChris Mason 
2432dd3e67bSChris Mason harder:
244ab78c84dSChris Mason 	if (atomic_read(&info->throttles)) {
245ab78c84dSChris Mason 		DEFINE_WAIT(wait);
246ab78c84dSChris Mason 		int thr;
247ab78c84dSChris Mason 		thr = atomic_read(&info->throttle_gen);
248ab78c84dSChris Mason 
249ab78c84dSChris Mason 		do {
250ab78c84dSChris Mason 			prepare_to_wait(&info->transaction_throttle,
251ab78c84dSChris Mason 					&wait, TASK_UNINTERRUPTIBLE);
252ab78c84dSChris Mason 			if (!atomic_read(&info->throttles)) {
253ab78c84dSChris Mason 				finish_wait(&info->transaction_throttle, &wait);
254ab78c84dSChris Mason 				break;
255ab78c84dSChris Mason 			}
256ab78c84dSChris Mason 			schedule();
257ab78c84dSChris Mason 			finish_wait(&info->transaction_throttle, &wait);
258ab78c84dSChris Mason 		} while (thr == atomic_read(&info->throttle_gen));
2592dd3e67bSChris Mason 		harder_count++;
2602dd3e67bSChris Mason 
2612dd3e67bSChris Mason 		if (root->fs_info->total_ref_cache_size > 1 * 1024 * 1024 &&
2622dd3e67bSChris Mason 		    harder_count < 2)
2632dd3e67bSChris Mason 			goto harder;
2642dd3e67bSChris Mason 
2652dd3e67bSChris Mason 		if (root->fs_info->total_ref_cache_size > 5 * 1024 * 1024 &&
2662dd3e67bSChris Mason 		    harder_count < 10)
2672dd3e67bSChris Mason 			goto harder;
2682dd3e67bSChris Mason 
2692dd3e67bSChris Mason 		if (root->fs_info->total_ref_cache_size > 10 * 1024 * 1024 &&
2702dd3e67bSChris Mason 		    harder_count < 20)
2712dd3e67bSChris Mason 			goto harder;
272ab78c84dSChris Mason 	}
273ab78c84dSChris Mason }
274ab78c84dSChris Mason 
27537d1aeeeSChris Mason void btrfs_throttle(struct btrfs_root *root)
27637d1aeeeSChris Mason {
27737d1aeeeSChris Mason 	mutex_lock(&root->fs_info->trans_mutex);
2789ca9ee09SSage Weil 	if (!root->fs_info->open_ioctl_trans)
27937d1aeeeSChris Mason 		wait_current_trans(root);
28037d1aeeeSChris Mason 	mutex_unlock(&root->fs_info->trans_mutex);
28137d1aeeeSChris Mason 
28237d1aeeeSChris Mason 	throttle_on_drops(root);
28337d1aeeeSChris Mason }
28437d1aeeeSChris Mason 
28589ce8a63SChris Mason static int __btrfs_end_transaction(struct btrfs_trans_handle *trans,
28689ce8a63SChris Mason 			  struct btrfs_root *root, int throttle)
28779154b1bSChris Mason {
28879154b1bSChris Mason 	struct btrfs_transaction *cur_trans;
289ab78c84dSChris Mason 	struct btrfs_fs_info *info = root->fs_info;
290d6e4a428SChris Mason 
29156bec294SChris Mason 	if (trans->delayed_ref_updates &&
29256bec294SChris Mason 	    (trans->transaction->delayed_refs.flushing ||
29356bec294SChris Mason 	    trans->transaction->delayed_refs.num_entries > 16384)) {
29456bec294SChris Mason 		btrfs_run_delayed_refs(trans, root, trans->delayed_ref_updates);
29556bec294SChris Mason 	} else if (trans->transaction->delayed_refs.num_entries > 64) {
29656bec294SChris Mason 		wake_up_process(root->fs_info->transaction_kthread);
29756bec294SChris Mason 	}
29856bec294SChris Mason 
299ab78c84dSChris Mason 	mutex_lock(&info->trans_mutex);
300ab78c84dSChris Mason 	cur_trans = info->running_transaction;
301ccd467d6SChris Mason 	WARN_ON(cur_trans != trans->transaction);
302d5719762SChris Mason 	WARN_ON(cur_trans->num_writers < 1);
303ccd467d6SChris Mason 	cur_trans->num_writers--;
30489ce8a63SChris Mason 
30579154b1bSChris Mason 	if (waitqueue_active(&cur_trans->writer_wait))
30679154b1bSChris Mason 		wake_up(&cur_trans->writer_wait);
30779154b1bSChris Mason 	put_transaction(cur_trans);
308ab78c84dSChris Mason 	mutex_unlock(&info->trans_mutex);
309d6025579SChris Mason 	memset(trans, 0, sizeof(*trans));
3102c90e5d6SChris Mason 	kmem_cache_free(btrfs_trans_handle_cachep, trans);
311ab78c84dSChris Mason 
312ab78c84dSChris Mason 	if (throttle)
31337d1aeeeSChris Mason 		throttle_on_drops(root);
314ab78c84dSChris Mason 
31579154b1bSChris Mason 	return 0;
31679154b1bSChris Mason }
31779154b1bSChris Mason 
31889ce8a63SChris Mason int btrfs_end_transaction(struct btrfs_trans_handle *trans,
31989ce8a63SChris Mason 			  struct btrfs_root *root)
32089ce8a63SChris Mason {
32189ce8a63SChris Mason 	return __btrfs_end_transaction(trans, root, 0);
32289ce8a63SChris Mason }
32389ce8a63SChris Mason 
32489ce8a63SChris Mason int btrfs_end_transaction_throttle(struct btrfs_trans_handle *trans,
32589ce8a63SChris Mason 				   struct btrfs_root *root)
32689ce8a63SChris Mason {
32789ce8a63SChris Mason 	return __btrfs_end_transaction(trans, root, 1);
32889ce8a63SChris Mason }
32989ce8a63SChris Mason 
330d352ac68SChris Mason /*
331d352ac68SChris Mason  * when btree blocks are allocated, they have some corresponding bits set for
332d352ac68SChris Mason  * them in one of two extent_io trees.  This is used to make sure all of
333d352ac68SChris Mason  * those extents are on disk for transaction or log commit
334d352ac68SChris Mason  */
335d0c803c4SChris Mason int btrfs_write_and_wait_marked_extents(struct btrfs_root *root,
336d0c803c4SChris Mason 					struct extent_io_tree *dirty_pages)
33779154b1bSChris Mason {
3387c4452b9SChris Mason 	int ret;
339777e6bd7SChris Mason 	int err = 0;
3407c4452b9SChris Mason 	int werr = 0;
3417c4452b9SChris Mason 	struct page *page;
3427c4452b9SChris Mason 	struct inode *btree_inode = root->fs_info->btree_inode;
343777e6bd7SChris Mason 	u64 start = 0;
3445f39d397SChris Mason 	u64 end;
3455f39d397SChris Mason 	unsigned long index;
3467c4452b9SChris Mason 
3477c4452b9SChris Mason 	while (1) {
348777e6bd7SChris Mason 		ret = find_first_extent_bit(dirty_pages, start, &start, &end,
3495f39d397SChris Mason 					    EXTENT_DIRTY);
3505f39d397SChris Mason 		if (ret)
3517c4452b9SChris Mason 			break;
3525f39d397SChris Mason 		while (start <= end) {
353777e6bd7SChris Mason 			cond_resched();
354777e6bd7SChris Mason 
3555f39d397SChris Mason 			index = start >> PAGE_CACHE_SHIFT;
35635ebb934SChris Mason 			start = (u64)(index + 1) << PAGE_CACHE_SHIFT;
3574bef0848SChris Mason 			page = find_get_page(btree_inode->i_mapping, index);
3587c4452b9SChris Mason 			if (!page)
3597c4452b9SChris Mason 				continue;
3604bef0848SChris Mason 
3614bef0848SChris Mason 			btree_lock_page_hook(page);
3624bef0848SChris Mason 			if (!page->mapping) {
3634bef0848SChris Mason 				unlock_page(page);
3644bef0848SChris Mason 				page_cache_release(page);
3654bef0848SChris Mason 				continue;
3664bef0848SChris Mason 			}
3674bef0848SChris Mason 
3686702ed49SChris Mason 			if (PageWriteback(page)) {
3696702ed49SChris Mason 				if (PageDirty(page))
3706702ed49SChris Mason 					wait_on_page_writeback(page);
3716702ed49SChris Mason 				else {
3726702ed49SChris Mason 					unlock_page(page);
3736702ed49SChris Mason 					page_cache_release(page);
3746702ed49SChris Mason 					continue;
3756702ed49SChris Mason 				}
3766702ed49SChris Mason 			}
3777c4452b9SChris Mason 			err = write_one_page(page, 0);
3787c4452b9SChris Mason 			if (err)
3797c4452b9SChris Mason 				werr = err;
3807c4452b9SChris Mason 			page_cache_release(page);
3817c4452b9SChris Mason 		}
3827c4452b9SChris Mason 	}
383777e6bd7SChris Mason 	while (1) {
384777e6bd7SChris Mason 		ret = find_first_extent_bit(dirty_pages, 0, &start, &end,
385777e6bd7SChris Mason 					    EXTENT_DIRTY);
386777e6bd7SChris Mason 		if (ret)
387777e6bd7SChris Mason 			break;
388777e6bd7SChris Mason 
389777e6bd7SChris Mason 		clear_extent_dirty(dirty_pages, start, end, GFP_NOFS);
390777e6bd7SChris Mason 		while (start <= end) {
391777e6bd7SChris Mason 			index = start >> PAGE_CACHE_SHIFT;
392777e6bd7SChris Mason 			start = (u64)(index + 1) << PAGE_CACHE_SHIFT;
393777e6bd7SChris Mason 			page = find_get_page(btree_inode->i_mapping, index);
394777e6bd7SChris Mason 			if (!page)
395777e6bd7SChris Mason 				continue;
396777e6bd7SChris Mason 			if (PageDirty(page)) {
3974bef0848SChris Mason 				btree_lock_page_hook(page);
3984bef0848SChris Mason 				wait_on_page_writeback(page);
399777e6bd7SChris Mason 				err = write_one_page(page, 0);
400777e6bd7SChris Mason 				if (err)
401777e6bd7SChris Mason 					werr = err;
402777e6bd7SChris Mason 			}
403777e6bd7SChris Mason 			wait_on_page_writeback(page);
404777e6bd7SChris Mason 			page_cache_release(page);
405777e6bd7SChris Mason 			cond_resched();
406777e6bd7SChris Mason 		}
407777e6bd7SChris Mason 	}
4087c4452b9SChris Mason 	if (err)
4097c4452b9SChris Mason 		werr = err;
4107c4452b9SChris Mason 	return werr;
41179154b1bSChris Mason }
41279154b1bSChris Mason 
413d0c803c4SChris Mason int btrfs_write_and_wait_transaction(struct btrfs_trans_handle *trans,
414d0c803c4SChris Mason 				     struct btrfs_root *root)
415d0c803c4SChris Mason {
416d0c803c4SChris Mason 	if (!trans || !trans->transaction) {
417d0c803c4SChris Mason 		struct inode *btree_inode;
418d0c803c4SChris Mason 		btree_inode = root->fs_info->btree_inode;
419d0c803c4SChris Mason 		return filemap_write_and_wait(btree_inode->i_mapping);
420d0c803c4SChris Mason 	}
421d0c803c4SChris Mason 	return btrfs_write_and_wait_marked_extents(root,
422d0c803c4SChris Mason 					   &trans->transaction->dirty_pages);
423d0c803c4SChris Mason }
424d0c803c4SChris Mason 
425d352ac68SChris Mason /*
426d352ac68SChris Mason  * this is used to update the root pointer in the tree of tree roots.
427d352ac68SChris Mason  *
428d352ac68SChris Mason  * But, in the case of the extent allocation tree, updating the root
429d352ac68SChris Mason  * pointer may allocate blocks which may change the root of the extent
430d352ac68SChris Mason  * allocation tree.
431d352ac68SChris Mason  *
432d352ac68SChris Mason  * So, this loops and repeats and makes sure the cowonly root didn't
433d352ac68SChris Mason  * change while the root pointer was being updated in the metadata.
434d352ac68SChris Mason  */
4350b86a832SChris Mason static int update_cowonly_root(struct btrfs_trans_handle *trans,
43679154b1bSChris Mason 			       struct btrfs_root *root)
43779154b1bSChris Mason {
43879154b1bSChris Mason 	int ret;
4390b86a832SChris Mason 	u64 old_root_bytenr;
4400b86a832SChris Mason 	struct btrfs_root *tree_root = root->fs_info->tree_root;
44179154b1bSChris Mason 
4420b86a832SChris Mason 	btrfs_write_dirty_block_groups(trans, root);
44356bec294SChris Mason 
44456bec294SChris Mason 	ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
44556bec294SChris Mason 	BUG_ON(ret);
44687ef2bb4SChris 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;
4510b86a832SChris Mason 		btrfs_set_root_bytenr(&root->root_item,
4520b86a832SChris Mason 				       root->node->start);
4530b86a832SChris Mason 		btrfs_set_root_level(&root->root_item,
4540b86a832SChris Mason 				     btrfs_header_level(root->node));
45584234f3aSYan Zheng 		btrfs_set_root_generation(&root->root_item, trans->transid);
45687ef2bb4SChris Mason 
45779154b1bSChris Mason 		ret = btrfs_update_root(trans, tree_root,
4580b86a832SChris Mason 					&root->root_key,
4590b86a832SChris Mason 					&root->root_item);
46079154b1bSChris Mason 		BUG_ON(ret);
4610b86a832SChris Mason 		btrfs_write_dirty_block_groups(trans, root);
46256bec294SChris Mason 
46356bec294SChris Mason 		ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
46456bec294SChris Mason 		BUG_ON(ret);
4650b86a832SChris Mason 	}
4660b86a832SChris Mason 	return 0;
4670b86a832SChris Mason }
4680b86a832SChris Mason 
469d352ac68SChris Mason /*
470d352ac68SChris Mason  * update all the cowonly tree roots on disk
471d352ac68SChris Mason  */
4720b86a832SChris Mason int btrfs_commit_tree_roots(struct btrfs_trans_handle *trans,
4730b86a832SChris Mason 			    struct btrfs_root *root)
4740b86a832SChris Mason {
4750b86a832SChris Mason 	struct btrfs_fs_info *fs_info = root->fs_info;
4760b86a832SChris Mason 	struct list_head *next;
47784234f3aSYan Zheng 	struct extent_buffer *eb;
47856bec294SChris Mason 	int ret;
47984234f3aSYan Zheng 
48056bec294SChris Mason 	ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
48156bec294SChris Mason 	BUG_ON(ret);
48287ef2bb4SChris Mason 
48384234f3aSYan Zheng 	eb = btrfs_lock_root_node(fs_info->tree_root);
4849fa8cfe7SChris Mason 	btrfs_cow_block(trans, fs_info->tree_root, eb, NULL, 0, &eb);
48584234f3aSYan Zheng 	btrfs_tree_unlock(eb);
48684234f3aSYan Zheng 	free_extent_buffer(eb);
4870b86a832SChris Mason 
48856bec294SChris Mason 	ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
48956bec294SChris Mason 	BUG_ON(ret);
49087ef2bb4SChris Mason 
4910b86a832SChris Mason 	while (!list_empty(&fs_info->dirty_cowonly_roots)) {
4920b86a832SChris Mason 		next = fs_info->dirty_cowonly_roots.next;
4930b86a832SChris Mason 		list_del_init(next);
4940b86a832SChris Mason 		root = list_entry(next, struct btrfs_root, dirty_list);
49587ef2bb4SChris Mason 
4960b86a832SChris Mason 		update_cowonly_root(trans, root);
49756bec294SChris Mason 
49856bec294SChris Mason 		ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
49956bec294SChris Mason 		BUG_ON(ret);
50079154b1bSChris Mason 	}
50179154b1bSChris Mason 	return 0;
50279154b1bSChris Mason }
50379154b1bSChris Mason 
504d352ac68SChris Mason /*
505d352ac68SChris Mason  * dead roots are old snapshots that need to be deleted.  This allocates
506d352ac68SChris Mason  * a dirty root struct and adds it into the list of dead roots that need to
507d352ac68SChris Mason  * be deleted
508d352ac68SChris Mason  */
509b48652c1SYan Zheng int btrfs_add_dead_root(struct btrfs_root *root, struct btrfs_root *latest)
5105eda7b5eSChris Mason {
511f321e491SYan Zheng 	struct btrfs_dirty_root *dirty;
5125eda7b5eSChris Mason 
5135eda7b5eSChris Mason 	dirty = kmalloc(sizeof(*dirty), GFP_NOFS);
5145eda7b5eSChris Mason 	if (!dirty)
5155eda7b5eSChris Mason 		return -ENOMEM;
5165eda7b5eSChris Mason 	dirty->root = root;
5175ce14bbcSChris Mason 	dirty->latest_root = latest;
518b48652c1SYan Zheng 
519b48652c1SYan Zheng 	mutex_lock(&root->fs_info->trans_mutex);
520b48652c1SYan Zheng 	list_add(&dirty->list, &latest->fs_info->dead_roots);
521b48652c1SYan Zheng 	mutex_unlock(&root->fs_info->trans_mutex);
5225eda7b5eSChris Mason 	return 0;
5235eda7b5eSChris Mason }
5245eda7b5eSChris Mason 
525d352ac68SChris Mason /*
526d352ac68SChris Mason  * at transaction commit time we need to schedule the old roots for
527d352ac68SChris Mason  * deletion via btrfs_drop_snapshot.  This runs through all the
528d352ac68SChris Mason  * reference counted roots that were modified in the current
529d352ac68SChris Mason  * transaction and puts them into the drop list
530d352ac68SChris Mason  */
53180b6794dSChris Mason static noinline int add_dirty_roots(struct btrfs_trans_handle *trans,
53235b7e476SChris Mason 				    struct radix_tree_root *radix,
53335b7e476SChris Mason 				    struct list_head *list)
5340f7d52f4SChris Mason {
535f321e491SYan Zheng 	struct btrfs_dirty_root *dirty;
5360f7d52f4SChris Mason 	struct btrfs_root *gang[8];
5370f7d52f4SChris Mason 	struct btrfs_root *root;
5380f7d52f4SChris Mason 	int i;
5390f7d52f4SChris Mason 	int ret;
54054aa1f4dSChris Mason 	int err = 0;
5415eda7b5eSChris Mason 	u32 refs;
54254aa1f4dSChris Mason 
5430f7d52f4SChris Mason 	while (1) {
5440f7d52f4SChris Mason 		ret = radix_tree_gang_lookup_tag(radix, (void **)gang, 0,
5450f7d52f4SChris Mason 						 ARRAY_SIZE(gang),
5460f7d52f4SChris Mason 						 BTRFS_ROOT_TRANS_TAG);
5470f7d52f4SChris Mason 		if (ret == 0)
5480f7d52f4SChris Mason 			break;
5490f7d52f4SChris Mason 		for (i = 0; i < ret; i++) {
5500f7d52f4SChris Mason 			root = gang[i];
5512619ba1fSChris Mason 			radix_tree_tag_clear(radix,
5522619ba1fSChris Mason 				     (unsigned long)root->root_key.objectid,
5530f7d52f4SChris Mason 				     BTRFS_ROOT_TRANS_TAG);
55431153d81SYan Zheng 
55531153d81SYan Zheng 			BUG_ON(!root->ref_tree);
556017e5369SChris Mason 			dirty = root->dirty_root;
55731153d81SYan Zheng 
558e02119d5SChris Mason 			btrfs_free_log(trans, root);
559f82d02d9SYan Zheng 			btrfs_free_reloc_root(trans, root);
560e02119d5SChris Mason 
5610f7d52f4SChris Mason 			if (root->commit_root == root->node) {
562db94535dSChris Mason 				WARN_ON(root->node->start !=
563db94535dSChris Mason 					btrfs_root_bytenr(&root->root_item));
56431153d81SYan Zheng 
5655f39d397SChris Mason 				free_extent_buffer(root->commit_root);
5660f7d52f4SChris Mason 				root->commit_root = NULL;
5677ea394f1SYan Zheng 				root->dirty_root = NULL;
56831153d81SYan Zheng 
569bcc63abbSYan 				spin_lock(&root->list_lock);
570bcc63abbSYan 				list_del_init(&dirty->root->dead_list);
571bcc63abbSYan 				spin_unlock(&root->list_lock);
572bcc63abbSYan 
57331153d81SYan Zheng 				kfree(dirty->root);
57431153d81SYan Zheng 				kfree(dirty);
57558176a96SJosef Bacik 
57658176a96SJosef Bacik 				/* make sure to update the root on disk
57758176a96SJosef Bacik 				 * so we get any updates to the block used
57858176a96SJosef Bacik 				 * counts
57958176a96SJosef Bacik 				 */
58058176a96SJosef Bacik 				err = btrfs_update_root(trans,
58158176a96SJosef Bacik 						root->fs_info->tree_root,
58258176a96SJosef Bacik 						&root->root_key,
58358176a96SJosef Bacik 						&root->root_item);
5840f7d52f4SChris Mason 				continue;
5850f7d52f4SChris Mason 			}
5869f3a7427SChris Mason 
5879f3a7427SChris Mason 			memset(&root->root_item.drop_progress, 0,
5889f3a7427SChris Mason 			       sizeof(struct btrfs_disk_key));
5899f3a7427SChris Mason 			root->root_item.drop_level = 0;
5900f7d52f4SChris Mason 			root->commit_root = NULL;
5917ea394f1SYan Zheng 			root->dirty_root = NULL;
5920f7d52f4SChris Mason 			root->root_key.offset = root->fs_info->generation;
593db94535dSChris Mason 			btrfs_set_root_bytenr(&root->root_item,
594db94535dSChris Mason 					      root->node->start);
595db94535dSChris Mason 			btrfs_set_root_level(&root->root_item,
596db94535dSChris Mason 					     btrfs_header_level(root->node));
59784234f3aSYan Zheng 			btrfs_set_root_generation(&root->root_item,
59884234f3aSYan Zheng 						  root->root_key.offset);
59984234f3aSYan Zheng 
6000f7d52f4SChris Mason 			err = btrfs_insert_root(trans, root->fs_info->tree_root,
6010f7d52f4SChris Mason 						&root->root_key,
6020f7d52f4SChris Mason 						&root->root_item);
60354aa1f4dSChris Mason 			if (err)
60454aa1f4dSChris Mason 				break;
6059f3a7427SChris Mason 
6069f3a7427SChris Mason 			refs = btrfs_root_refs(&dirty->root->root_item);
6079f3a7427SChris Mason 			btrfs_set_root_refs(&dirty->root->root_item, refs - 1);
6085eda7b5eSChris Mason 			err = btrfs_update_root(trans, root->fs_info->tree_root,
6099f3a7427SChris Mason 						&dirty->root->root_key,
6109f3a7427SChris Mason 						&dirty->root->root_item);
6115eda7b5eSChris Mason 
6125eda7b5eSChris Mason 			BUG_ON(err);
6139f3a7427SChris Mason 			if (refs == 1) {
6140f7d52f4SChris Mason 				list_add(&dirty->list, list);
6159f3a7427SChris Mason 			} else {
6169f3a7427SChris Mason 				WARN_ON(1);
61731153d81SYan Zheng 				free_extent_buffer(dirty->root->node);
6189f3a7427SChris Mason 				kfree(dirty->root);
6195eda7b5eSChris Mason 				kfree(dirty);
6200f7d52f4SChris Mason 			}
6210f7d52f4SChris Mason 		}
6229f3a7427SChris Mason 	}
62354aa1f4dSChris Mason 	return err;
6240f7d52f4SChris Mason }
6250f7d52f4SChris Mason 
626d352ac68SChris Mason /*
627d352ac68SChris Mason  * defrag a given btree.  If cacheonly == 1, this won't read from the disk,
628d352ac68SChris Mason  * otherwise every leaf in the btree is read and defragged.
629d352ac68SChris Mason  */
630e9d0b13bSChris Mason int btrfs_defrag_root(struct btrfs_root *root, int cacheonly)
631e9d0b13bSChris Mason {
632e9d0b13bSChris Mason 	struct btrfs_fs_info *info = root->fs_info;
633e9d0b13bSChris Mason 	int ret;
634e9d0b13bSChris Mason 	struct btrfs_trans_handle *trans;
635d3c2fdcfSChris Mason 	unsigned long nr;
636e9d0b13bSChris Mason 
637a2135011SChris Mason 	smp_mb();
638e9d0b13bSChris Mason 	if (root->defrag_running)
639e9d0b13bSChris Mason 		return 0;
640e9d0b13bSChris Mason 	trans = btrfs_start_transaction(root, 1);
6416b80053dSChris Mason 	while (1) {
642e9d0b13bSChris Mason 		root->defrag_running = 1;
643e9d0b13bSChris Mason 		ret = btrfs_defrag_leaves(trans, root, cacheonly);
644d3c2fdcfSChris Mason 		nr = trans->blocks_used;
645e9d0b13bSChris Mason 		btrfs_end_transaction(trans, root);
646d3c2fdcfSChris Mason 		btrfs_btree_balance_dirty(info->tree_root, nr);
647e9d0b13bSChris Mason 		cond_resched();
648e9d0b13bSChris Mason 
649e9d0b13bSChris Mason 		trans = btrfs_start_transaction(root, 1);
6503f157a2fSChris Mason 		if (root->fs_info->closing || ret != -EAGAIN)
651e9d0b13bSChris Mason 			break;
652e9d0b13bSChris Mason 	}
653e9d0b13bSChris Mason 	root->defrag_running = 0;
654a2135011SChris Mason 	smp_mb();
655e9d0b13bSChris Mason 	btrfs_end_transaction(trans, root);
656e9d0b13bSChris Mason 	return 0;
657e9d0b13bSChris Mason }
658e9d0b13bSChris Mason 
659d352ac68SChris Mason /*
660d352ac68SChris Mason  * Given a list of roots that need to be deleted, call btrfs_drop_snapshot on
661d352ac68SChris Mason  * all of them
662d352ac68SChris Mason  */
66380b6794dSChris Mason static noinline int drop_dirty_roots(struct btrfs_root *tree_root,
66435b7e476SChris Mason 				     struct list_head *list)
6650f7d52f4SChris Mason {
666f321e491SYan Zheng 	struct btrfs_dirty_root *dirty;
6670f7d52f4SChris Mason 	struct btrfs_trans_handle *trans;
668d3c2fdcfSChris Mason 	unsigned long nr;
669db94535dSChris Mason 	u64 num_bytes;
670db94535dSChris Mason 	u64 bytes_used;
671bcc63abbSYan 	u64 max_useless;
67254aa1f4dSChris Mason 	int ret = 0;
6739f3a7427SChris Mason 	int err;
6749f3a7427SChris Mason 
6750f7d52f4SChris Mason 	while (!list_empty(list)) {
67658176a96SJosef Bacik 		struct btrfs_root *root;
67758176a96SJosef Bacik 
678f321e491SYan Zheng 		dirty = list_entry(list->prev, struct btrfs_dirty_root, list);
6790f7d52f4SChris Mason 		list_del_init(&dirty->list);
6805eda7b5eSChris Mason 
681db94535dSChris Mason 		num_bytes = btrfs_root_used(&dirty->root->root_item);
68258176a96SJosef Bacik 		root = dirty->latest_root;
683a2135011SChris Mason 		atomic_inc(&root->fs_info->throttles);
68458176a96SJosef Bacik 
6859f3a7427SChris Mason 		while (1) {
6860f7d52f4SChris Mason 			trans = btrfs_start_transaction(tree_root, 1);
6875b21f2edSZheng Yan 			mutex_lock(&root->fs_info->drop_mutex);
6889f3a7427SChris Mason 			ret = btrfs_drop_snapshot(trans, dirty->root);
689d397712bSChris Mason 			if (ret != -EAGAIN)
6909f3a7427SChris Mason 				break;
6915b21f2edSZheng Yan 			mutex_unlock(&root->fs_info->drop_mutex);
69258176a96SJosef Bacik 
6939f3a7427SChris Mason 			err = btrfs_update_root(trans,
6949f3a7427SChris Mason 					tree_root,
6959f3a7427SChris Mason 					&dirty->root->root_key,
6969f3a7427SChris Mason 					&dirty->root->root_item);
6979f3a7427SChris Mason 			if (err)
6989f3a7427SChris Mason 				ret = err;
699d3c2fdcfSChris Mason 			nr = trans->blocks_used;
700017e5369SChris Mason 			ret = btrfs_end_transaction(trans, tree_root);
7010f7d52f4SChris Mason 			BUG_ON(ret);
702a2135011SChris Mason 
703d3c2fdcfSChris Mason 			btrfs_btree_balance_dirty(tree_root, nr);
7044dc11904SChris Mason 			cond_resched();
7059f3a7427SChris Mason 		}
7069f3a7427SChris Mason 		BUG_ON(ret);
707a2135011SChris Mason 		atomic_dec(&root->fs_info->throttles);
708017e5369SChris Mason 		wake_up(&root->fs_info->transaction_throttle);
70958176a96SJosef Bacik 
710db94535dSChris Mason 		num_bytes -= btrfs_root_used(&dirty->root->root_item);
711db94535dSChris Mason 		bytes_used = btrfs_root_used(&root->root_item);
712db94535dSChris Mason 		if (num_bytes) {
71324562425SYan Zheng 			mutex_lock(&root->fs_info->trans_mutex);
714e02119d5SChris Mason 			btrfs_record_root_in_trans(root);
71524562425SYan Zheng 			mutex_unlock(&root->fs_info->trans_mutex);
7165f39d397SChris Mason 			btrfs_set_root_used(&root->root_item,
717db94535dSChris Mason 					    bytes_used - num_bytes);
71858176a96SJosef Bacik 		}
719a2135011SChris Mason 
7209f3a7427SChris Mason 		ret = btrfs_del_root(trans, tree_root, &dirty->root->root_key);
72158176a96SJosef Bacik 		if (ret) {
72258176a96SJosef Bacik 			BUG();
72354aa1f4dSChris Mason 			break;
72458176a96SJosef Bacik 		}
725a2135011SChris Mason 		mutex_unlock(&root->fs_info->drop_mutex);
726a2135011SChris Mason 
727bcc63abbSYan 		spin_lock(&root->list_lock);
728bcc63abbSYan 		list_del_init(&dirty->root->dead_list);
729bcc63abbSYan 		if (!list_empty(&root->dead_list)) {
730bcc63abbSYan 			struct btrfs_root *oldest;
731bcc63abbSYan 			oldest = list_entry(root->dead_list.prev,
732bcc63abbSYan 					    struct btrfs_root, dead_list);
733bcc63abbSYan 			max_useless = oldest->root_key.offset - 1;
734bcc63abbSYan 		} else {
735bcc63abbSYan 			max_useless = root->root_key.offset - 1;
736bcc63abbSYan 		}
737bcc63abbSYan 		spin_unlock(&root->list_lock);
738bcc63abbSYan 
739d3c2fdcfSChris Mason 		nr = trans->blocks_used;
7400f7d52f4SChris Mason 		ret = btrfs_end_transaction(trans, tree_root);
7410f7d52f4SChris Mason 		BUG_ON(ret);
7425eda7b5eSChris Mason 
743e4657689SZheng Yan 		ret = btrfs_remove_leaf_refs(root, max_useless, 0);
744bcc63abbSYan 		BUG_ON(ret);
745bcc63abbSYan 
746f510cfecSChris Mason 		free_extent_buffer(dirty->root->node);
7475eda7b5eSChris Mason 		kfree(dirty->root);
7480f7d52f4SChris Mason 		kfree(dirty);
749d3c2fdcfSChris Mason 
750d3c2fdcfSChris Mason 		btrfs_btree_balance_dirty(tree_root, nr);
7514dc11904SChris Mason 		cond_resched();
7520f7d52f4SChris Mason 	}
75354aa1f4dSChris Mason 	return ret;
7540f7d52f4SChris Mason }
7550f7d52f4SChris Mason 
756d352ac68SChris Mason /*
757d352ac68SChris Mason  * new snapshots need to be created at a very specific time in the
758d352ac68SChris Mason  * transaction commit.  This does the actual creation
759d352ac68SChris Mason  */
76080b6794dSChris Mason static noinline int create_pending_snapshot(struct btrfs_trans_handle *trans,
7613063d29fSChris Mason 				   struct btrfs_fs_info *fs_info,
7623063d29fSChris Mason 				   struct btrfs_pending_snapshot *pending)
7633063d29fSChris Mason {
7643063d29fSChris Mason 	struct btrfs_key key;
76580b6794dSChris Mason 	struct btrfs_root_item *new_root_item;
7663063d29fSChris Mason 	struct btrfs_root *tree_root = fs_info->tree_root;
7673063d29fSChris Mason 	struct btrfs_root *root = pending->root;
7683063d29fSChris Mason 	struct extent_buffer *tmp;
769925baeddSChris Mason 	struct extent_buffer *old;
7703063d29fSChris Mason 	int ret;
7713063d29fSChris Mason 	u64 objectid;
7723063d29fSChris Mason 
77380b6794dSChris Mason 	new_root_item = kmalloc(sizeof(*new_root_item), GFP_NOFS);
77480b6794dSChris Mason 	if (!new_root_item) {
77580b6794dSChris Mason 		ret = -ENOMEM;
77680b6794dSChris Mason 		goto fail;
77780b6794dSChris Mason 	}
7783063d29fSChris Mason 	ret = btrfs_find_free_objectid(trans, tree_root, 0, &objectid);
7793063d29fSChris Mason 	if (ret)
7803063d29fSChris Mason 		goto fail;
7813063d29fSChris Mason 
78280ff3856SYan Zheng 	btrfs_record_root_in_trans(root);
78380ff3856SYan Zheng 	btrfs_set_root_last_snapshot(&root->root_item, trans->transid);
78480b6794dSChris Mason 	memcpy(new_root_item, &root->root_item, sizeof(*new_root_item));
7853063d29fSChris Mason 
7863063d29fSChris Mason 	key.objectid = objectid;
7875b21f2edSZheng Yan 	key.offset = trans->transid;
7883063d29fSChris Mason 	btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
7893063d29fSChris Mason 
790925baeddSChris Mason 	old = btrfs_lock_root_node(root);
7919fa8cfe7SChris Mason 	btrfs_cow_block(trans, root, old, NULL, 0, &old);
7923063d29fSChris Mason 
793925baeddSChris Mason 	btrfs_copy_root(trans, root, old, &tmp, objectid);
794925baeddSChris Mason 	btrfs_tree_unlock(old);
795925baeddSChris Mason 	free_extent_buffer(old);
7963063d29fSChris Mason 
79780b6794dSChris Mason 	btrfs_set_root_bytenr(new_root_item, tmp->start);
79880b6794dSChris Mason 	btrfs_set_root_level(new_root_item, btrfs_header_level(tmp));
79984234f3aSYan Zheng 	btrfs_set_root_generation(new_root_item, trans->transid);
8003063d29fSChris Mason 	ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
80180b6794dSChris Mason 				new_root_item);
802925baeddSChris Mason 	btrfs_tree_unlock(tmp);
8033063d29fSChris Mason 	free_extent_buffer(tmp);
8043063d29fSChris Mason 	if (ret)
8053063d29fSChris Mason 		goto fail;
8063063d29fSChris Mason 
8073de4586cSChris Mason 	key.offset = (u64)-1;
8083de4586cSChris Mason 	memcpy(&pending->root_key, &key, sizeof(key));
8093de4586cSChris Mason fail:
8103de4586cSChris Mason 	kfree(new_root_item);
8113de4586cSChris Mason 	return ret;
8123de4586cSChris Mason }
8133de4586cSChris Mason 
8143de4586cSChris Mason static noinline int finish_pending_snapshot(struct btrfs_fs_info *fs_info,
8153de4586cSChris Mason 				   struct btrfs_pending_snapshot *pending)
8163de4586cSChris Mason {
8173de4586cSChris Mason 	int ret;
8183de4586cSChris Mason 	int namelen;
8193de4586cSChris Mason 	u64 index = 0;
8203de4586cSChris Mason 	struct btrfs_trans_handle *trans;
8213de4586cSChris Mason 	struct inode *parent_inode;
8223de4586cSChris Mason 	struct inode *inode;
8230660b5afSChris Mason 	struct btrfs_root *parent_root;
8243de4586cSChris Mason 
8253394e160SChris Mason 	parent_inode = pending->dentry->d_parent->d_inode;
8260660b5afSChris Mason 	parent_root = BTRFS_I(parent_inode)->root;
827180591bcSYan Zheng 	trans = btrfs_join_transaction(parent_root, 1);
8283de4586cSChris Mason 
8293063d29fSChris Mason 	/*
8303063d29fSChris Mason 	 * insert the directory item
8313063d29fSChris Mason 	 */
8323b96362cSSven Wegener 	namelen = strlen(pending->name);
8333de4586cSChris Mason 	ret = btrfs_set_inode_index(parent_inode, &index);
8340660b5afSChris Mason 	ret = btrfs_insert_dir_item(trans, parent_root,
8353b96362cSSven Wegener 			    pending->name, namelen,
8363de4586cSChris Mason 			    parent_inode->i_ino,
8373de4586cSChris Mason 			    &pending->root_key, BTRFS_FT_DIR, index);
8383063d29fSChris Mason 
8393063d29fSChris Mason 	if (ret)
8403063d29fSChris Mason 		goto fail;
8410660b5afSChris Mason 
84252c26179SYan Zheng 	btrfs_i_size_write(parent_inode, parent_inode->i_size + namelen * 2);
84352c26179SYan Zheng 	ret = btrfs_update_inode(trans, parent_root, parent_inode);
84452c26179SYan Zheng 	BUG_ON(ret);
84552c26179SYan Zheng 
8460660b5afSChris Mason 	/* add the backref first */
8470660b5afSChris Mason 	ret = btrfs_add_root_ref(trans, parent_root->fs_info->tree_root,
8480660b5afSChris Mason 				 pending->root_key.objectid,
8490660b5afSChris Mason 				 BTRFS_ROOT_BACKREF_KEY,
8500660b5afSChris Mason 				 parent_root->root_key.objectid,
8510660b5afSChris Mason 				 parent_inode->i_ino, index, pending->name,
8520660b5afSChris Mason 				 namelen);
8530660b5afSChris Mason 
8540660b5afSChris Mason 	BUG_ON(ret);
8550660b5afSChris Mason 
8560660b5afSChris Mason 	/* now add the forward ref */
8570660b5afSChris Mason 	ret = btrfs_add_root_ref(trans, parent_root->fs_info->tree_root,
8580660b5afSChris Mason 				 parent_root->root_key.objectid,
8590660b5afSChris Mason 				 BTRFS_ROOT_REF_KEY,
8600660b5afSChris Mason 				 pending->root_key.objectid,
8610660b5afSChris Mason 				 parent_inode->i_ino, index, pending->name,
8620660b5afSChris Mason 				 namelen);
8630660b5afSChris Mason 
8643de4586cSChris Mason 	inode = btrfs_lookup_dentry(parent_inode, pending->dentry);
8653de4586cSChris Mason 	d_instantiate(pending->dentry, inode);
8663063d29fSChris Mason fail:
8673de4586cSChris Mason 	btrfs_end_transaction(trans, fs_info->fs_root);
8683063d29fSChris Mason 	return ret;
8693063d29fSChris Mason }
8703063d29fSChris Mason 
871d352ac68SChris Mason /*
872d352ac68SChris Mason  * create all the snapshots we've scheduled for creation
873d352ac68SChris Mason  */
87480b6794dSChris Mason static noinline int create_pending_snapshots(struct btrfs_trans_handle *trans,
8753063d29fSChris Mason 					     struct btrfs_fs_info *fs_info)
8763063d29fSChris Mason {
8773063d29fSChris Mason 	struct btrfs_pending_snapshot *pending;
8783063d29fSChris Mason 	struct list_head *head = &trans->transaction->pending_snapshots;
8793de4586cSChris Mason 	int ret;
8803de4586cSChris Mason 
881c6e30871SQinghuang Feng 	list_for_each_entry(pending, head, list) {
8823de4586cSChris Mason 		ret = create_pending_snapshot(trans, fs_info, pending);
8833de4586cSChris Mason 		BUG_ON(ret);
8843de4586cSChris Mason 	}
8853de4586cSChris Mason 	return 0;
8863de4586cSChris Mason }
8873de4586cSChris Mason 
8883de4586cSChris Mason static noinline int finish_pending_snapshots(struct btrfs_trans_handle *trans,
8893de4586cSChris Mason 					     struct btrfs_fs_info *fs_info)
8903de4586cSChris Mason {
8913de4586cSChris Mason 	struct btrfs_pending_snapshot *pending;
8923de4586cSChris Mason 	struct list_head *head = &trans->transaction->pending_snapshots;
8933063d29fSChris Mason 	int ret;
8943063d29fSChris Mason 
8953063d29fSChris Mason 	while (!list_empty(head)) {
8963063d29fSChris Mason 		pending = list_entry(head->next,
8973063d29fSChris Mason 				     struct btrfs_pending_snapshot, list);
8983de4586cSChris Mason 		ret = finish_pending_snapshot(fs_info, pending);
8993063d29fSChris Mason 		BUG_ON(ret);
9003063d29fSChris Mason 		list_del(&pending->list);
9013063d29fSChris Mason 		kfree(pending->name);
9023063d29fSChris Mason 		kfree(pending);
9033063d29fSChris Mason 	}
904dc17ff8fSChris Mason 	return 0;
905dc17ff8fSChris Mason }
906dc17ff8fSChris Mason 
90779154b1bSChris Mason int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
90879154b1bSChris Mason 			     struct btrfs_root *root)
90979154b1bSChris Mason {
91015ee9bc7SJosef Bacik 	unsigned long joined = 0;
91115ee9bc7SJosef Bacik 	unsigned long timeout = 1;
91279154b1bSChris Mason 	struct btrfs_transaction *cur_trans;
9138fd17795SChris Mason 	struct btrfs_transaction *prev_trans = NULL;
9140b86a832SChris Mason 	struct btrfs_root *chunk_root = root->fs_info->chunk_root;
9150f7d52f4SChris Mason 	struct list_head dirty_fs_roots;
916d1310b2eSChris Mason 	struct extent_io_tree *pinned_copy;
91779154b1bSChris Mason 	DEFINE_WAIT(wait);
91815ee9bc7SJosef Bacik 	int ret;
91979154b1bSChris Mason 
92056bec294SChris Mason 	/* make a pass through all the delayed refs we have so far
92156bec294SChris Mason 	 * any runnings procs may add more while we are here
92256bec294SChris Mason 	 */
92356bec294SChris Mason 	ret = btrfs_run_delayed_refs(trans, root, 0);
92456bec294SChris Mason 	BUG_ON(ret);
92556bec294SChris Mason 
92656bec294SChris Mason 	/*
92756bec294SChris Mason 	 * set the flushing flag so procs in this transaction have to
92856bec294SChris Mason 	 * start sending their work down.
92956bec294SChris Mason 	 */
93056bec294SChris Mason 	trans->transaction->delayed_refs.flushing = 1;
93156bec294SChris Mason 
93256bec294SChris Mason 	ret = btrfs_run_delayed_refs(trans, root, (u64)-1);
93356bec294SChris Mason 	BUG_ON(ret);
93456bec294SChris Mason 
9350f7d52f4SChris Mason 	INIT_LIST_HEAD(&dirty_fs_roots);
93679154b1bSChris Mason 	mutex_lock(&root->fs_info->trans_mutex);
93779154b1bSChris Mason 	if (trans->transaction->in_commit) {
93879154b1bSChris Mason 		cur_trans = trans->transaction;
93979154b1bSChris Mason 		trans->transaction->use_count++;
940ccd467d6SChris Mason 		mutex_unlock(&root->fs_info->trans_mutex);
94179154b1bSChris Mason 		btrfs_end_transaction(trans, root);
942ccd467d6SChris Mason 
94379154b1bSChris Mason 		ret = wait_for_commit(root, cur_trans);
94479154b1bSChris Mason 		BUG_ON(ret);
94515ee9bc7SJosef Bacik 
94615ee9bc7SJosef Bacik 		mutex_lock(&root->fs_info->trans_mutex);
94779154b1bSChris Mason 		put_transaction(cur_trans);
94815ee9bc7SJosef Bacik 		mutex_unlock(&root->fs_info->trans_mutex);
94915ee9bc7SJosef Bacik 
95079154b1bSChris Mason 		return 0;
95179154b1bSChris Mason 	}
9524313b399SChris Mason 
9534313b399SChris Mason 	pinned_copy = kmalloc(sizeof(*pinned_copy), GFP_NOFS);
9544313b399SChris Mason 	if (!pinned_copy)
9554313b399SChris Mason 		return -ENOMEM;
9564313b399SChris Mason 
957d1310b2eSChris Mason 	extent_io_tree_init(pinned_copy,
9584313b399SChris Mason 			     root->fs_info->btree_inode->i_mapping, GFP_NOFS);
9594313b399SChris Mason 
9602c90e5d6SChris Mason 	trans->transaction->in_commit = 1;
961f9295749SChris Mason 	trans->transaction->blocked = 1;
962ccd467d6SChris Mason 	cur_trans = trans->transaction;
963ccd467d6SChris Mason 	if (cur_trans->list.prev != &root->fs_info->trans_list) {
964ccd467d6SChris Mason 		prev_trans = list_entry(cur_trans->list.prev,
965ccd467d6SChris Mason 					struct btrfs_transaction, list);
966ccd467d6SChris Mason 		if (!prev_trans->commit_done) {
967ccd467d6SChris Mason 			prev_trans->use_count++;
968ccd467d6SChris Mason 			mutex_unlock(&root->fs_info->trans_mutex);
969ccd467d6SChris Mason 
970ccd467d6SChris Mason 			wait_for_commit(root, prev_trans);
971ccd467d6SChris Mason 
972ccd467d6SChris Mason 			mutex_lock(&root->fs_info->trans_mutex);
97315ee9bc7SJosef Bacik 			put_transaction(prev_trans);
974ccd467d6SChris Mason 		}
975ccd467d6SChris Mason 	}
97615ee9bc7SJosef Bacik 
97715ee9bc7SJosef Bacik 	do {
9787ea394f1SYan Zheng 		int snap_pending = 0;
97915ee9bc7SJosef Bacik 		joined = cur_trans->num_joined;
9807ea394f1SYan Zheng 		if (!list_empty(&trans->transaction->pending_snapshots))
9817ea394f1SYan Zheng 			snap_pending = 1;
9827ea394f1SYan Zheng 
9832c90e5d6SChris Mason 		WARN_ON(cur_trans != trans->transaction);
98415ee9bc7SJosef Bacik 		prepare_to_wait(&cur_trans->writer_wait, &wait,
98579154b1bSChris Mason 				TASK_UNINTERRUPTIBLE);
98615ee9bc7SJosef Bacik 
98715ee9bc7SJosef Bacik 		if (cur_trans->num_writers > 1)
98815ee9bc7SJosef Bacik 			timeout = MAX_SCHEDULE_TIMEOUT;
98915ee9bc7SJosef Bacik 		else
99015ee9bc7SJosef Bacik 			timeout = 1;
99115ee9bc7SJosef Bacik 
99279154b1bSChris Mason 		mutex_unlock(&root->fs_info->trans_mutex);
99315ee9bc7SJosef Bacik 
9947ea394f1SYan Zheng 		if (snap_pending) {
9957ea394f1SYan Zheng 			ret = btrfs_wait_ordered_extents(root, 1);
9967ea394f1SYan Zheng 			BUG_ON(ret);
9977ea394f1SYan Zheng 		}
9987ea394f1SYan Zheng 
99915ee9bc7SJosef Bacik 		schedule_timeout(timeout);
100015ee9bc7SJosef Bacik 
100179154b1bSChris Mason 		mutex_lock(&root->fs_info->trans_mutex);
100215ee9bc7SJosef Bacik 		finish_wait(&cur_trans->writer_wait, &wait);
100315ee9bc7SJosef Bacik 	} while (cur_trans->num_writers > 1 ||
100415ee9bc7SJosef Bacik 		 (cur_trans->num_joined != joined));
100515ee9bc7SJosef Bacik 
10063063d29fSChris Mason 	ret = create_pending_snapshots(trans, root->fs_info);
10073063d29fSChris Mason 	BUG_ON(ret);
10083063d29fSChris Mason 
100956bec294SChris Mason 	ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
101056bec294SChris Mason 	BUG_ON(ret);
101156bec294SChris Mason 
10122c90e5d6SChris Mason 	WARN_ON(cur_trans != trans->transaction);
1013dc17ff8fSChris Mason 
1014e02119d5SChris Mason 	/* btrfs_commit_tree_roots is responsible for getting the
1015e02119d5SChris Mason 	 * various roots consistent with each other.  Every pointer
1016e02119d5SChris Mason 	 * in the tree of tree roots has to point to the most up to date
1017e02119d5SChris Mason 	 * root for every subvolume and other tree.  So, we have to keep
1018e02119d5SChris Mason 	 * the tree logging code from jumping in and changing any
1019e02119d5SChris Mason 	 * of the trees.
1020e02119d5SChris Mason 	 *
1021e02119d5SChris Mason 	 * At this point in the commit, there can't be any tree-log
1022e02119d5SChris Mason 	 * writers, but a little lower down we drop the trans mutex
1023e02119d5SChris Mason 	 * and let new people in.  By holding the tree_log_mutex
1024e02119d5SChris Mason 	 * from now until after the super is written, we avoid races
1025e02119d5SChris Mason 	 * with the tree-log code.
1026e02119d5SChris Mason 	 */
1027e02119d5SChris Mason 	mutex_lock(&root->fs_info->tree_log_mutex);
10281a40e23bSZheng Yan 	/*
10291a40e23bSZheng Yan 	 * keep tree reloc code from adding new reloc trees
10301a40e23bSZheng Yan 	 */
10311a40e23bSZheng Yan 	mutex_lock(&root->fs_info->tree_reloc_mutex);
10321a40e23bSZheng Yan 
1033e02119d5SChris Mason 
103454aa1f4dSChris Mason 	ret = add_dirty_roots(trans, &root->fs_info->fs_roots_radix,
103554aa1f4dSChris Mason 			      &dirty_fs_roots);
103654aa1f4dSChris Mason 	BUG_ON(ret);
103754aa1f4dSChris Mason 
1038e02119d5SChris Mason 	/* add_dirty_roots gets rid of all the tree log roots, it is now
1039e02119d5SChris Mason 	 * safe to free the root of tree log roots
1040e02119d5SChris Mason 	 */
1041e02119d5SChris Mason 	btrfs_free_log_root_tree(trans, root->fs_info);
1042e02119d5SChris Mason 
104379154b1bSChris Mason 	ret = btrfs_commit_tree_roots(trans, root);
104479154b1bSChris Mason 	BUG_ON(ret);
104554aa1f4dSChris Mason 
104678fae27eSChris Mason 	cur_trans = root->fs_info->running_transaction;
1047cee36a03SChris Mason 	spin_lock(&root->fs_info->new_trans_lock);
104878fae27eSChris Mason 	root->fs_info->running_transaction = NULL;
1049cee36a03SChris Mason 	spin_unlock(&root->fs_info->new_trans_lock);
10504b52dff6SChris Mason 	btrfs_set_super_generation(&root->fs_info->super_copy,
10514b52dff6SChris Mason 				   cur_trans->transid);
10524b52dff6SChris Mason 	btrfs_set_super_root(&root->fs_info->super_copy,
1053db94535dSChris Mason 			     root->fs_info->tree_root->node->start);
1054db94535dSChris Mason 	btrfs_set_super_root_level(&root->fs_info->super_copy,
1055db94535dSChris Mason 			   btrfs_header_level(root->fs_info->tree_root->node));
10565f39d397SChris Mason 
10570b86a832SChris Mason 	btrfs_set_super_chunk_root(&root->fs_info->super_copy,
10580b86a832SChris Mason 				   chunk_root->node->start);
10590b86a832SChris Mason 	btrfs_set_super_chunk_root_level(&root->fs_info->super_copy,
10600b86a832SChris Mason 					 btrfs_header_level(chunk_root->node));
106184234f3aSYan Zheng 	btrfs_set_super_chunk_root_generation(&root->fs_info->super_copy,
106284234f3aSYan Zheng 				btrfs_header_generation(chunk_root->node));
1063e02119d5SChris Mason 
1064e02119d5SChris Mason 	if (!root->fs_info->log_root_recovering) {
1065e02119d5SChris Mason 		btrfs_set_super_log_root(&root->fs_info->super_copy, 0);
1066e02119d5SChris Mason 		btrfs_set_super_log_root_level(&root->fs_info->super_copy, 0);
1067e02119d5SChris Mason 	}
1068e02119d5SChris Mason 
1069a061fc8dSChris Mason 	memcpy(&root->fs_info->super_for_commit, &root->fs_info->super_copy,
10704b52dff6SChris Mason 	       sizeof(root->fs_info->super_copy));
1071ccd467d6SChris Mason 
10724313b399SChris Mason 	btrfs_copy_pinned(root, pinned_copy);
1073ccd467d6SChris Mason 
1074f9295749SChris Mason 	trans->transaction->blocked = 0;
1075e6dcd2dcSChris Mason 	wake_up(&root->fs_info->transaction_throttle);
1076f9295749SChris Mason 	wake_up(&root->fs_info->transaction_wait);
1077e6dcd2dcSChris Mason 
107878fae27eSChris Mason 	mutex_unlock(&root->fs_info->trans_mutex);
107979154b1bSChris Mason 	ret = btrfs_write_and_wait_transaction(trans, root);
108079154b1bSChris Mason 	BUG_ON(ret);
1081a512bbf8SYan Zheng 	write_ctree_super(trans, root, 0);
10824313b399SChris Mason 
1083e02119d5SChris Mason 	/*
1084e02119d5SChris Mason 	 * the super is written, we can safely allow the tree-loggers
1085e02119d5SChris Mason 	 * to go about their business
1086e02119d5SChris Mason 	 */
1087e02119d5SChris Mason 	mutex_unlock(&root->fs_info->tree_log_mutex);
1088e02119d5SChris Mason 
10894313b399SChris Mason 	btrfs_finish_extent_commit(trans, root, pinned_copy);
10904313b399SChris Mason 	kfree(pinned_copy);
10914313b399SChris Mason 
10921a40e23bSZheng Yan 	btrfs_drop_dead_reloc_roots(root);
10931a40e23bSZheng Yan 	mutex_unlock(&root->fs_info->tree_reloc_mutex);
10941a40e23bSZheng Yan 
10953de4586cSChris Mason 	/* do the directory inserts of any pending snapshot creations */
10963de4586cSChris Mason 	finish_pending_snapshots(trans, root->fs_info);
10973de4586cSChris Mason 
10981a40e23bSZheng Yan 	mutex_lock(&root->fs_info->trans_mutex);
10991a40e23bSZheng Yan 
11002c90e5d6SChris Mason 	cur_trans->commit_done = 1;
110115ee9bc7SJosef Bacik 	root->fs_info->last_trans_committed = cur_trans->transid;
11022c90e5d6SChris Mason 	wake_up(&cur_trans->commit_wait);
11033de4586cSChris Mason 
110479154b1bSChris Mason 	put_transaction(cur_trans);
110578fae27eSChris Mason 	put_transaction(cur_trans);
110658176a96SJosef Bacik 
1107bcc63abbSYan 	list_splice_init(&dirty_fs_roots, &root->fs_info->dead_roots);
1108facda1e7SChris Mason 	if (root->fs_info->closing)
1109facda1e7SChris Mason 		list_splice_init(&root->fs_info->dead_roots, &dirty_fs_roots);
111058176a96SJosef Bacik 
111178fae27eSChris Mason 	mutex_unlock(&root->fs_info->trans_mutex);
11123de4586cSChris Mason 
11132c90e5d6SChris Mason 	kmem_cache_free(btrfs_trans_handle_cachep, trans);
111479154b1bSChris Mason 
1115d397712bSChris Mason 	if (root->fs_info->closing)
11160f7d52f4SChris Mason 		drop_dirty_roots(root->fs_info->tree_root, &dirty_fs_roots);
111779154b1bSChris Mason 	return ret;
111879154b1bSChris Mason }
111979154b1bSChris Mason 
1120d352ac68SChris Mason /*
1121d352ac68SChris Mason  * interface function to delete all the snapshots we have scheduled for deletion
1122d352ac68SChris Mason  */
1123e9d0b13bSChris Mason int btrfs_clean_old_snapshots(struct btrfs_root *root)
1124e9d0b13bSChris Mason {
1125e9d0b13bSChris Mason 	struct list_head dirty_roots;
1126e9d0b13bSChris Mason 	INIT_LIST_HEAD(&dirty_roots);
1127a74a4b97SChris Mason again:
1128e9d0b13bSChris Mason 	mutex_lock(&root->fs_info->trans_mutex);
1129e9d0b13bSChris Mason 	list_splice_init(&root->fs_info->dead_roots, &dirty_roots);
1130e9d0b13bSChris Mason 	mutex_unlock(&root->fs_info->trans_mutex);
1131e9d0b13bSChris Mason 
1132e9d0b13bSChris Mason 	if (!list_empty(&dirty_roots)) {
1133e9d0b13bSChris Mason 		drop_dirty_roots(root, &dirty_roots);
1134a74a4b97SChris Mason 		goto again;
1135e9d0b13bSChris Mason 	}
1136e9d0b13bSChris Mason 	return 0;
1137e9d0b13bSChris Mason }
1138