xref: /openbmc/linux/fs/btrfs/transaction.c (revision 5a0e3ad6)
16cbd5570SChris Mason /*
26cbd5570SChris Mason  * Copyright (C) 2007 Oracle.  All rights reserved.
36cbd5570SChris Mason  *
46cbd5570SChris Mason  * This program is free software; you can redistribute it and/or
56cbd5570SChris Mason  * modify it under the terms of the GNU General Public
66cbd5570SChris Mason  * License v2 as published by the Free Software Foundation.
76cbd5570SChris Mason  *
86cbd5570SChris Mason  * This program is distributed in the hope that it will be useful,
96cbd5570SChris Mason  * but WITHOUT ANY WARRANTY; without even the implied warranty of
106cbd5570SChris Mason  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
116cbd5570SChris Mason  * General Public License for more details.
126cbd5570SChris Mason  *
136cbd5570SChris Mason  * You should have received a copy of the GNU General Public
146cbd5570SChris Mason  * License along with this program; if not, write to the
156cbd5570SChris Mason  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
166cbd5570SChris Mason  * Boston, MA 021110-1307, USA.
176cbd5570SChris Mason  */
186cbd5570SChris Mason 
1979154b1bSChris Mason #include <linux/fs.h>
205a0e3ad6STejun Heo #include <linux/slab.h>
2134088780SChris Mason #include <linux/sched.h>
22d3c2fdcfSChris Mason #include <linux/writeback.h>
235f39d397SChris Mason #include <linux/pagemap.h>
245f2cc086SChris Mason #include <linux/blkdev.h>
2579154b1bSChris Mason #include "ctree.h"
2679154b1bSChris Mason #include "disk-io.h"
2779154b1bSChris Mason #include "transaction.h"
28925baeddSChris Mason #include "locking.h"
29e02119d5SChris Mason #include "tree-log.h"
3079154b1bSChris Mason 
310f7d52f4SChris Mason #define BTRFS_ROOT_TRANS_TAG 0
320f7d52f4SChris Mason 
3380b6794dSChris Mason static noinline void put_transaction(struct btrfs_transaction *transaction)
3479154b1bSChris Mason {
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 
44817d52f8SJosef Bacik static noinline void switch_commit_root(struct btrfs_root *root)
45817d52f8SJosef Bacik {
46817d52f8SJosef Bacik 	free_extent_buffer(root->commit_root);
47817d52f8SJosef Bacik 	root->commit_root = btrfs_root_node(root);
48817d52f8SJosef Bacik }
49817d52f8SJosef Bacik 
50d352ac68SChris Mason /*
51d352ac68SChris Mason  * either allocate a new transaction or hop into the existing one
52d352ac68SChris Mason  */
5380b6794dSChris Mason static noinline int join_transaction(struct btrfs_root *root)
5479154b1bSChris Mason {
5579154b1bSChris Mason 	struct btrfs_transaction *cur_trans;
5679154b1bSChris Mason 	cur_trans = root->fs_info->running_transaction;
5779154b1bSChris Mason 	if (!cur_trans) {
582c90e5d6SChris Mason 		cur_trans = kmem_cache_alloc(btrfs_transaction_cachep,
592c90e5d6SChris Mason 					     GFP_NOFS);
6079154b1bSChris Mason 		BUG_ON(!cur_trans);
610f7d52f4SChris Mason 		root->fs_info->generation++;
6215ee9bc7SJosef Bacik 		cur_trans->num_writers = 1;
6315ee9bc7SJosef Bacik 		cur_trans->num_joined = 0;
640f7d52f4SChris Mason 		cur_trans->transid = root->fs_info->generation;
6579154b1bSChris Mason 		init_waitqueue_head(&cur_trans->writer_wait);
6679154b1bSChris Mason 		init_waitqueue_head(&cur_trans->commit_wait);
6779154b1bSChris Mason 		cur_trans->in_commit = 0;
68f9295749SChris Mason 		cur_trans->blocked = 0;
69d5719762SChris Mason 		cur_trans->use_count = 1;
7079154b1bSChris Mason 		cur_trans->commit_done = 0;
7108607c1bSChris Mason 		cur_trans->start_time = get_seconds();
7256bec294SChris Mason 
736bef4d31SEric Paris 		cur_trans->delayed_refs.root = RB_ROOT;
7456bec294SChris Mason 		cur_trans->delayed_refs.num_entries = 0;
75c3e69d58SChris Mason 		cur_trans->delayed_refs.num_heads_ready = 0;
76c3e69d58SChris Mason 		cur_trans->delayed_refs.num_heads = 0;
7756bec294SChris Mason 		cur_trans->delayed_refs.flushing = 0;
78c3e69d58SChris Mason 		cur_trans->delayed_refs.run_delayed_start = 0;
7956bec294SChris Mason 		spin_lock_init(&cur_trans->delayed_refs.lock);
8056bec294SChris Mason 
813063d29fSChris Mason 		INIT_LIST_HEAD(&cur_trans->pending_snapshots);
828fd17795SChris Mason 		list_add_tail(&cur_trans->list, &root->fs_info->trans_list);
83d1310b2eSChris Mason 		extent_io_tree_init(&cur_trans->dirty_pages,
845f39d397SChris Mason 				     root->fs_info->btree_inode->i_mapping,
855f39d397SChris Mason 				     GFP_NOFS);
8648ec2cf8SChris Mason 		spin_lock(&root->fs_info->new_trans_lock);
8748ec2cf8SChris Mason 		root->fs_info->running_transaction = cur_trans;
8848ec2cf8SChris Mason 		spin_unlock(&root->fs_info->new_trans_lock);
8915ee9bc7SJosef Bacik 	} else {
9079154b1bSChris Mason 		cur_trans->num_writers++;
9115ee9bc7SJosef Bacik 		cur_trans->num_joined++;
9215ee9bc7SJosef Bacik 	}
9315ee9bc7SJosef Bacik 
9479154b1bSChris Mason 	return 0;
9579154b1bSChris Mason }
9679154b1bSChris Mason 
97d352ac68SChris Mason /*
98d397712bSChris Mason  * this does all the record keeping required to make sure that a reference
99d397712bSChris Mason  * counted root is properly recorded in a given transaction.  This is required
100d397712bSChris Mason  * to make sure the old root from before we joined the transaction is deleted
101d397712bSChris Mason  * when the transaction commits
102d352ac68SChris Mason  */
1035d4f98a2SYan Zheng static noinline int record_root_in_trans(struct btrfs_trans_handle *trans,
1045d4f98a2SYan Zheng 					 struct btrfs_root *root)
1056702ed49SChris Mason {
1065d4f98a2SYan Zheng 	if (root->ref_cows && root->last_trans < trans->transid) {
1076702ed49SChris Mason 		WARN_ON(root == root->fs_info->extent_root);
1085d4f98a2SYan Zheng 		WARN_ON(root->commit_root != root->node);
1095d4f98a2SYan Zheng 
1106702ed49SChris Mason 		radix_tree_tag_set(&root->fs_info->fs_roots_radix,
1116702ed49SChris Mason 			   (unsigned long)root->root_key.objectid,
1126702ed49SChris Mason 			   BTRFS_ROOT_TRANS_TAG);
1135d4f98a2SYan Zheng 		root->last_trans = trans->transid;
1145d4f98a2SYan Zheng 		btrfs_init_reloc_root(trans, root);
1156702ed49SChris Mason 	}
1165d4f98a2SYan Zheng 	return 0;
1176702ed49SChris Mason }
1185d4f98a2SYan Zheng 
1195d4f98a2SYan Zheng int btrfs_record_root_in_trans(struct btrfs_trans_handle *trans,
1205d4f98a2SYan Zheng 			       struct btrfs_root *root)
1215d4f98a2SYan Zheng {
1225d4f98a2SYan Zheng 	if (!root->ref_cows)
1235d4f98a2SYan Zheng 		return 0;
1245d4f98a2SYan Zheng 
1255d4f98a2SYan Zheng 	mutex_lock(&root->fs_info->trans_mutex);
1265d4f98a2SYan Zheng 	if (root->last_trans == trans->transid) {
1275d4f98a2SYan Zheng 		mutex_unlock(&root->fs_info->trans_mutex);
1285d4f98a2SYan Zheng 		return 0;
1295d4f98a2SYan Zheng 	}
1305d4f98a2SYan Zheng 
1315d4f98a2SYan Zheng 	record_root_in_trans(trans, root);
1325d4f98a2SYan Zheng 	mutex_unlock(&root->fs_info->trans_mutex);
1336702ed49SChris Mason 	return 0;
1346702ed49SChris Mason }
1356702ed49SChris Mason 
136d352ac68SChris Mason /* wait for commit against the current transaction to become unblocked
137d352ac68SChris Mason  * when this is done, it is safe to start a new transaction, but the current
138d352ac68SChris Mason  * transaction might not be fully on disk.
139d352ac68SChris Mason  */
14037d1aeeeSChris Mason static void wait_current_trans(struct btrfs_root *root)
14179154b1bSChris Mason {
142f9295749SChris Mason 	struct btrfs_transaction *cur_trans;
14379154b1bSChris Mason 
144f9295749SChris Mason 	cur_trans = root->fs_info->running_transaction;
14537d1aeeeSChris Mason 	if (cur_trans && cur_trans->blocked) {
146f9295749SChris Mason 		DEFINE_WAIT(wait);
147f9295749SChris Mason 		cur_trans->use_count++;
148f9295749SChris Mason 		while (1) {
149f9295749SChris Mason 			prepare_to_wait(&root->fs_info->transaction_wait, &wait,
150f9295749SChris Mason 					TASK_UNINTERRUPTIBLE);
151f9295749SChris Mason 			if (cur_trans->blocked) {
152f9295749SChris Mason 				mutex_unlock(&root->fs_info->trans_mutex);
153f9295749SChris Mason 				schedule();
154f9295749SChris Mason 				mutex_lock(&root->fs_info->trans_mutex);
155f9295749SChris Mason 				finish_wait(&root->fs_info->transaction_wait,
156f9295749SChris Mason 					    &wait);
157f9295749SChris Mason 			} else {
158f9295749SChris Mason 				finish_wait(&root->fs_info->transaction_wait,
159f9295749SChris Mason 					    &wait);
160f9295749SChris Mason 				break;
161f9295749SChris Mason 			}
162f9295749SChris Mason 		}
163f9295749SChris Mason 		put_transaction(cur_trans);
164f9295749SChris Mason 	}
16537d1aeeeSChris Mason }
16637d1aeeeSChris Mason 
167249ac1e5SJosef Bacik enum btrfs_trans_type {
168249ac1e5SJosef Bacik 	TRANS_START,
169249ac1e5SJosef Bacik 	TRANS_JOIN,
170249ac1e5SJosef Bacik 	TRANS_USERSPACE,
171249ac1e5SJosef Bacik };
172249ac1e5SJosef Bacik 
173e02119d5SChris Mason static struct btrfs_trans_handle *start_transaction(struct btrfs_root *root,
174249ac1e5SJosef Bacik 					     int num_blocks, int type)
17537d1aeeeSChris Mason {
17637d1aeeeSChris Mason 	struct btrfs_trans_handle *h =
17737d1aeeeSChris Mason 		kmem_cache_alloc(btrfs_trans_handle_cachep, GFP_NOFS);
17837d1aeeeSChris Mason 	int ret;
17937d1aeeeSChris Mason 
18037d1aeeeSChris Mason 	mutex_lock(&root->fs_info->trans_mutex);
1814bef0848SChris Mason 	if (!root->fs_info->log_root_recovering &&
182249ac1e5SJosef Bacik 	    ((type == TRANS_START && !root->fs_info->open_ioctl_trans) ||
183249ac1e5SJosef Bacik 	     type == TRANS_USERSPACE))
18437d1aeeeSChris Mason 		wait_current_trans(root);
18579154b1bSChris Mason 	ret = join_transaction(root);
18679154b1bSChris Mason 	BUG_ON(ret);
1870f7d52f4SChris Mason 
1886702ed49SChris Mason 	h->transid = root->fs_info->running_transaction->transid;
18979154b1bSChris Mason 	h->transaction = root->fs_info->running_transaction;
19079154b1bSChris Mason 	h->blocks_reserved = num_blocks;
19179154b1bSChris Mason 	h->blocks_used = 0;
192d2fb3437SYan Zheng 	h->block_group = 0;
19326b8003fSChris Mason 	h->alloc_exclude_nr = 0;
19426b8003fSChris Mason 	h->alloc_exclude_start = 0;
19556bec294SChris Mason 	h->delayed_ref_updates = 0;
196b7ec40d7SChris Mason 
197249ac1e5SJosef Bacik 	if (!current->journal_info && type != TRANS_USERSPACE)
1989ed74f2dSJosef Bacik 		current->journal_info = h;
1999ed74f2dSJosef Bacik 
20079154b1bSChris Mason 	root->fs_info->running_transaction->use_count++;
2015d4f98a2SYan Zheng 	record_root_in_trans(h, root);
20279154b1bSChris Mason 	mutex_unlock(&root->fs_info->trans_mutex);
20379154b1bSChris Mason 	return h;
20479154b1bSChris Mason }
20579154b1bSChris Mason 
206f9295749SChris Mason struct btrfs_trans_handle *btrfs_start_transaction(struct btrfs_root *root,
207f9295749SChris Mason 						   int num_blocks)
208f9295749SChris Mason {
209249ac1e5SJosef Bacik 	return start_transaction(root, num_blocks, TRANS_START);
210f9295749SChris Mason }
211f9295749SChris Mason struct btrfs_trans_handle *btrfs_join_transaction(struct btrfs_root *root,
212f9295749SChris Mason 						   int num_blocks)
213f9295749SChris Mason {
214249ac1e5SJosef Bacik 	return start_transaction(root, num_blocks, TRANS_JOIN);
215f9295749SChris Mason }
216f9295749SChris Mason 
2179ca9ee09SSage Weil struct btrfs_trans_handle *btrfs_start_ioctl_transaction(struct btrfs_root *r,
2189ca9ee09SSage Weil 							 int num_blocks)
2199ca9ee09SSage Weil {
220249ac1e5SJosef Bacik 	return start_transaction(r, num_blocks, TRANS_USERSPACE);
2219ca9ee09SSage Weil }
2229ca9ee09SSage Weil 
223d352ac68SChris Mason /* wait for a transaction commit to be fully complete */
22489ce8a63SChris Mason static noinline int wait_for_commit(struct btrfs_root *root,
22589ce8a63SChris Mason 				    struct btrfs_transaction *commit)
22689ce8a63SChris Mason {
22789ce8a63SChris Mason 	DEFINE_WAIT(wait);
22889ce8a63SChris Mason 	mutex_lock(&root->fs_info->trans_mutex);
22989ce8a63SChris Mason 	while (!commit->commit_done) {
23089ce8a63SChris Mason 		prepare_to_wait(&commit->commit_wait, &wait,
23189ce8a63SChris Mason 				TASK_UNINTERRUPTIBLE);
23289ce8a63SChris Mason 		if (commit->commit_done)
23389ce8a63SChris Mason 			break;
23489ce8a63SChris Mason 		mutex_unlock(&root->fs_info->trans_mutex);
23589ce8a63SChris Mason 		schedule();
23689ce8a63SChris Mason 		mutex_lock(&root->fs_info->trans_mutex);
23789ce8a63SChris Mason 	}
23889ce8a63SChris Mason 	mutex_unlock(&root->fs_info->trans_mutex);
23989ce8a63SChris Mason 	finish_wait(&commit->commit_wait, &wait);
24089ce8a63SChris Mason 	return 0;
24189ce8a63SChris Mason }
24289ce8a63SChris Mason 
2435d4f98a2SYan Zheng #if 0
244d352ac68SChris Mason /*
245d397712bSChris Mason  * rate limit against the drop_snapshot code.  This helps to slow down new
246d397712bSChris Mason  * operations if the drop_snapshot code isn't able to keep up.
247d352ac68SChris Mason  */
24837d1aeeeSChris Mason static void throttle_on_drops(struct btrfs_root *root)
249ab78c84dSChris Mason {
250ab78c84dSChris Mason 	struct btrfs_fs_info *info = root->fs_info;
2512dd3e67bSChris Mason 	int harder_count = 0;
252ab78c84dSChris Mason 
2532dd3e67bSChris Mason harder:
254ab78c84dSChris Mason 	if (atomic_read(&info->throttles)) {
255ab78c84dSChris Mason 		DEFINE_WAIT(wait);
256ab78c84dSChris Mason 		int thr;
257ab78c84dSChris Mason 		thr = atomic_read(&info->throttle_gen);
258ab78c84dSChris Mason 
259ab78c84dSChris Mason 		do {
260ab78c84dSChris Mason 			prepare_to_wait(&info->transaction_throttle,
261ab78c84dSChris Mason 					&wait, TASK_UNINTERRUPTIBLE);
262ab78c84dSChris Mason 			if (!atomic_read(&info->throttles)) {
263ab78c84dSChris Mason 				finish_wait(&info->transaction_throttle, &wait);
264ab78c84dSChris Mason 				break;
265ab78c84dSChris Mason 			}
266ab78c84dSChris Mason 			schedule();
267ab78c84dSChris Mason 			finish_wait(&info->transaction_throttle, &wait);
268ab78c84dSChris Mason 		} while (thr == atomic_read(&info->throttle_gen));
2692dd3e67bSChris Mason 		harder_count++;
2702dd3e67bSChris Mason 
2712dd3e67bSChris Mason 		if (root->fs_info->total_ref_cache_size > 1 * 1024 * 1024 &&
2722dd3e67bSChris Mason 		    harder_count < 2)
2732dd3e67bSChris Mason 			goto harder;
2742dd3e67bSChris Mason 
2752dd3e67bSChris Mason 		if (root->fs_info->total_ref_cache_size > 5 * 1024 * 1024 &&
2762dd3e67bSChris Mason 		    harder_count < 10)
2772dd3e67bSChris Mason 			goto harder;
2782dd3e67bSChris Mason 
2792dd3e67bSChris Mason 		if (root->fs_info->total_ref_cache_size > 10 * 1024 * 1024 &&
2802dd3e67bSChris Mason 		    harder_count < 20)
2812dd3e67bSChris Mason 			goto harder;
282ab78c84dSChris Mason 	}
283ab78c84dSChris Mason }
2845d4f98a2SYan Zheng #endif
285ab78c84dSChris Mason 
28637d1aeeeSChris Mason void btrfs_throttle(struct btrfs_root *root)
28737d1aeeeSChris Mason {
28837d1aeeeSChris Mason 	mutex_lock(&root->fs_info->trans_mutex);
2899ca9ee09SSage Weil 	if (!root->fs_info->open_ioctl_trans)
29037d1aeeeSChris Mason 		wait_current_trans(root);
29137d1aeeeSChris Mason 	mutex_unlock(&root->fs_info->trans_mutex);
29237d1aeeeSChris Mason }
29337d1aeeeSChris Mason 
29489ce8a63SChris Mason static int __btrfs_end_transaction(struct btrfs_trans_handle *trans,
29589ce8a63SChris Mason 			  struct btrfs_root *root, int throttle)
29679154b1bSChris Mason {
29779154b1bSChris Mason 	struct btrfs_transaction *cur_trans;
298ab78c84dSChris Mason 	struct btrfs_fs_info *info = root->fs_info;
299c3e69d58SChris Mason 	int count = 0;
300d6e4a428SChris Mason 
301c3e69d58SChris Mason 	while (count < 4) {
302c3e69d58SChris Mason 		unsigned long cur = trans->delayed_ref_updates;
303c3e69d58SChris Mason 		trans->delayed_ref_updates = 0;
304c3e69d58SChris Mason 		if (cur &&
305c3e69d58SChris Mason 		    trans->transaction->delayed_refs.num_heads_ready > 64) {
306c3e69d58SChris Mason 			trans->delayed_ref_updates = 0;
307b7ec40d7SChris Mason 
308b7ec40d7SChris Mason 			/*
309b7ec40d7SChris Mason 			 * do a full flush if the transaction is trying
310b7ec40d7SChris Mason 			 * to close
311b7ec40d7SChris Mason 			 */
312b7ec40d7SChris Mason 			if (trans->transaction->delayed_refs.flushing)
313b7ec40d7SChris Mason 				cur = 0;
314c3e69d58SChris Mason 			btrfs_run_delayed_refs(trans, root, cur);
315c3e69d58SChris Mason 		} else {
316c3e69d58SChris Mason 			break;
317c3e69d58SChris Mason 		}
318c3e69d58SChris Mason 		count++;
31956bec294SChris Mason 	}
32056bec294SChris Mason 
321ab78c84dSChris Mason 	mutex_lock(&info->trans_mutex);
322ab78c84dSChris Mason 	cur_trans = info->running_transaction;
323ccd467d6SChris Mason 	WARN_ON(cur_trans != trans->transaction);
324d5719762SChris Mason 	WARN_ON(cur_trans->num_writers < 1);
325ccd467d6SChris Mason 	cur_trans->num_writers--;
32689ce8a63SChris Mason 
32779154b1bSChris Mason 	if (waitqueue_active(&cur_trans->writer_wait))
32879154b1bSChris Mason 		wake_up(&cur_trans->writer_wait);
32979154b1bSChris Mason 	put_transaction(cur_trans);
330ab78c84dSChris Mason 	mutex_unlock(&info->trans_mutex);
3319ed74f2dSJosef Bacik 
3329ed74f2dSJosef Bacik 	if (current->journal_info == trans)
3339ed74f2dSJosef Bacik 		current->journal_info = NULL;
334d6025579SChris Mason 	memset(trans, 0, sizeof(*trans));
3352c90e5d6SChris Mason 	kmem_cache_free(btrfs_trans_handle_cachep, trans);
336ab78c84dSChris Mason 
33724bbcf04SYan, Zheng 	if (throttle)
33824bbcf04SYan, Zheng 		btrfs_run_delayed_iputs(root);
33924bbcf04SYan, Zheng 
34079154b1bSChris Mason 	return 0;
34179154b1bSChris Mason }
34279154b1bSChris Mason 
34389ce8a63SChris Mason int btrfs_end_transaction(struct btrfs_trans_handle *trans,
34489ce8a63SChris Mason 			  struct btrfs_root *root)
34589ce8a63SChris Mason {
34689ce8a63SChris Mason 	return __btrfs_end_transaction(trans, root, 0);
34789ce8a63SChris Mason }
34889ce8a63SChris Mason 
34989ce8a63SChris Mason int btrfs_end_transaction_throttle(struct btrfs_trans_handle *trans,
35089ce8a63SChris Mason 				   struct btrfs_root *root)
35189ce8a63SChris Mason {
35289ce8a63SChris Mason 	return __btrfs_end_transaction(trans, root, 1);
35389ce8a63SChris Mason }
35489ce8a63SChris Mason 
355d352ac68SChris Mason /*
356d352ac68SChris Mason  * when btree blocks are allocated, they have some corresponding bits set for
357d352ac68SChris Mason  * them in one of two extent_io trees.  This is used to make sure all of
358690587d1SChris Mason  * those extents are sent to disk but does not wait on them
359d352ac68SChris Mason  */
360690587d1SChris Mason int btrfs_write_marked_extents(struct btrfs_root *root,
3618cef4e16SYan, Zheng 			       struct extent_io_tree *dirty_pages, int mark)
36279154b1bSChris Mason {
3637c4452b9SChris Mason 	int ret;
364777e6bd7SChris Mason 	int err = 0;
3657c4452b9SChris Mason 	int werr = 0;
3667c4452b9SChris Mason 	struct page *page;
3677c4452b9SChris Mason 	struct inode *btree_inode = root->fs_info->btree_inode;
368777e6bd7SChris Mason 	u64 start = 0;
3695f39d397SChris Mason 	u64 end;
3705f39d397SChris Mason 	unsigned long index;
3717c4452b9SChris Mason 
3727c4452b9SChris Mason 	while (1) {
373777e6bd7SChris Mason 		ret = find_first_extent_bit(dirty_pages, start, &start, &end,
3748cef4e16SYan, Zheng 					    mark);
3755f39d397SChris Mason 		if (ret)
3767c4452b9SChris Mason 			break;
3775f39d397SChris Mason 		while (start <= end) {
378777e6bd7SChris Mason 			cond_resched();
379777e6bd7SChris Mason 
3805f39d397SChris Mason 			index = start >> PAGE_CACHE_SHIFT;
38135ebb934SChris Mason 			start = (u64)(index + 1) << PAGE_CACHE_SHIFT;
3824bef0848SChris Mason 			page = find_get_page(btree_inode->i_mapping, index);
3837c4452b9SChris Mason 			if (!page)
3847c4452b9SChris Mason 				continue;
3854bef0848SChris Mason 
3864bef0848SChris Mason 			btree_lock_page_hook(page);
3874bef0848SChris Mason 			if (!page->mapping) {
3884bef0848SChris Mason 				unlock_page(page);
3894bef0848SChris Mason 				page_cache_release(page);
3904bef0848SChris Mason 				continue;
3914bef0848SChris Mason 			}
3924bef0848SChris Mason 
3936702ed49SChris Mason 			if (PageWriteback(page)) {
3946702ed49SChris Mason 				if (PageDirty(page))
3956702ed49SChris Mason 					wait_on_page_writeback(page);
3966702ed49SChris Mason 				else {
3976702ed49SChris Mason 					unlock_page(page);
3986702ed49SChris Mason 					page_cache_release(page);
3996702ed49SChris Mason 					continue;
4006702ed49SChris Mason 				}
4016702ed49SChris Mason 			}
4027c4452b9SChris Mason 			err = write_one_page(page, 0);
4037c4452b9SChris Mason 			if (err)
4047c4452b9SChris Mason 				werr = err;
4057c4452b9SChris Mason 			page_cache_release(page);
4067c4452b9SChris Mason 		}
4077c4452b9SChris Mason 	}
408690587d1SChris Mason 	if (err)
409690587d1SChris Mason 		werr = err;
410690587d1SChris Mason 	return werr;
411690587d1SChris Mason }
412690587d1SChris Mason 
413690587d1SChris Mason /*
414690587d1SChris Mason  * when btree blocks are allocated, they have some corresponding bits set for
415690587d1SChris Mason  * them in one of two extent_io trees.  This is used to make sure all of
416690587d1SChris Mason  * those extents are on disk for transaction or log commit.  We wait
417690587d1SChris Mason  * on all the pages and clear them from the dirty pages state tree
418690587d1SChris Mason  */
419690587d1SChris Mason int btrfs_wait_marked_extents(struct btrfs_root *root,
4208cef4e16SYan, Zheng 			      struct extent_io_tree *dirty_pages, int mark)
421690587d1SChris Mason {
422690587d1SChris Mason 	int ret;
423690587d1SChris Mason 	int err = 0;
424690587d1SChris Mason 	int werr = 0;
425690587d1SChris Mason 	struct page *page;
426690587d1SChris Mason 	struct inode *btree_inode = root->fs_info->btree_inode;
427690587d1SChris Mason 	u64 start = 0;
428690587d1SChris Mason 	u64 end;
429690587d1SChris Mason 	unsigned long index;
430690587d1SChris Mason 
431777e6bd7SChris Mason 	while (1) {
4328cef4e16SYan, Zheng 		ret = find_first_extent_bit(dirty_pages, start, &start, &end,
4338cef4e16SYan, Zheng 					    mark);
434777e6bd7SChris Mason 		if (ret)
435777e6bd7SChris Mason 			break;
436777e6bd7SChris Mason 
4378cef4e16SYan, Zheng 		clear_extent_bits(dirty_pages, start, end, mark, GFP_NOFS);
438777e6bd7SChris Mason 		while (start <= end) {
439777e6bd7SChris Mason 			index = start >> PAGE_CACHE_SHIFT;
440777e6bd7SChris Mason 			start = (u64)(index + 1) << PAGE_CACHE_SHIFT;
441777e6bd7SChris Mason 			page = find_get_page(btree_inode->i_mapping, index);
442777e6bd7SChris Mason 			if (!page)
443777e6bd7SChris Mason 				continue;
444777e6bd7SChris Mason 			if (PageDirty(page)) {
4454bef0848SChris Mason 				btree_lock_page_hook(page);
4464bef0848SChris Mason 				wait_on_page_writeback(page);
447777e6bd7SChris Mason 				err = write_one_page(page, 0);
448777e6bd7SChris Mason 				if (err)
449777e6bd7SChris Mason 					werr = err;
450777e6bd7SChris Mason 			}
451777e6bd7SChris Mason 			wait_on_page_writeback(page);
452777e6bd7SChris Mason 			page_cache_release(page);
453777e6bd7SChris Mason 			cond_resched();
454777e6bd7SChris Mason 		}
455777e6bd7SChris Mason 	}
4567c4452b9SChris Mason 	if (err)
4577c4452b9SChris Mason 		werr = err;
4587c4452b9SChris Mason 	return werr;
45979154b1bSChris Mason }
46079154b1bSChris Mason 
461690587d1SChris Mason /*
462690587d1SChris Mason  * when btree blocks are allocated, they have some corresponding bits set for
463690587d1SChris Mason  * them in one of two extent_io trees.  This is used to make sure all of
464690587d1SChris Mason  * those extents are on disk for transaction or log commit
465690587d1SChris Mason  */
466690587d1SChris Mason int btrfs_write_and_wait_marked_extents(struct btrfs_root *root,
4678cef4e16SYan, Zheng 				struct extent_io_tree *dirty_pages, int mark)
468690587d1SChris Mason {
469690587d1SChris Mason 	int ret;
470690587d1SChris Mason 	int ret2;
471690587d1SChris Mason 
4728cef4e16SYan, Zheng 	ret = btrfs_write_marked_extents(root, dirty_pages, mark);
4738cef4e16SYan, Zheng 	ret2 = btrfs_wait_marked_extents(root, dirty_pages, mark);
474690587d1SChris Mason 	return ret || ret2;
475690587d1SChris Mason }
476690587d1SChris Mason 
477d0c803c4SChris Mason int btrfs_write_and_wait_transaction(struct btrfs_trans_handle *trans,
478d0c803c4SChris Mason 				     struct btrfs_root *root)
479d0c803c4SChris Mason {
480d0c803c4SChris Mason 	if (!trans || !trans->transaction) {
481d0c803c4SChris Mason 		struct inode *btree_inode;
482d0c803c4SChris Mason 		btree_inode = root->fs_info->btree_inode;
483d0c803c4SChris Mason 		return filemap_write_and_wait(btree_inode->i_mapping);
484d0c803c4SChris Mason 	}
485d0c803c4SChris Mason 	return btrfs_write_and_wait_marked_extents(root,
4868cef4e16SYan, Zheng 					   &trans->transaction->dirty_pages,
4878cef4e16SYan, Zheng 					   EXTENT_DIRTY);
488d0c803c4SChris Mason }
489d0c803c4SChris Mason 
490d352ac68SChris Mason /*
491d352ac68SChris Mason  * this is used to update the root pointer in the tree of tree roots.
492d352ac68SChris Mason  *
493d352ac68SChris Mason  * But, in the case of the extent allocation tree, updating the root
494d352ac68SChris Mason  * pointer may allocate blocks which may change the root of the extent
495d352ac68SChris Mason  * allocation tree.
496d352ac68SChris Mason  *
497d352ac68SChris Mason  * So, this loops and repeats and makes sure the cowonly root didn't
498d352ac68SChris Mason  * change while the root pointer was being updated in the metadata.
499d352ac68SChris Mason  */
5000b86a832SChris Mason static int update_cowonly_root(struct btrfs_trans_handle *trans,
50179154b1bSChris Mason 			       struct btrfs_root *root)
50279154b1bSChris Mason {
50379154b1bSChris Mason 	int ret;
5040b86a832SChris Mason 	u64 old_root_bytenr;
50586b9f2ecSYan, Zheng 	u64 old_root_used;
5060b86a832SChris Mason 	struct btrfs_root *tree_root = root->fs_info->tree_root;
50779154b1bSChris Mason 
50886b9f2ecSYan, Zheng 	old_root_used = btrfs_root_used(&root->root_item);
5090b86a832SChris Mason 	btrfs_write_dirty_block_groups(trans, root);
51056bec294SChris Mason 
51179154b1bSChris Mason 	while (1) {
5120b86a832SChris Mason 		old_root_bytenr = btrfs_root_bytenr(&root->root_item);
51386b9f2ecSYan, Zheng 		if (old_root_bytenr == root->node->start &&
51486b9f2ecSYan, Zheng 		    old_root_used == btrfs_root_used(&root->root_item))
51579154b1bSChris Mason 			break;
51687ef2bb4SChris Mason 
5175d4f98a2SYan Zheng 		btrfs_set_root_node(&root->root_item, root->node);
51879154b1bSChris Mason 		ret = btrfs_update_root(trans, tree_root,
5190b86a832SChris Mason 					&root->root_key,
5200b86a832SChris Mason 					&root->root_item);
52179154b1bSChris Mason 		BUG_ON(ret);
52256bec294SChris Mason 
52386b9f2ecSYan, Zheng 		old_root_used = btrfs_root_used(&root->root_item);
5244a8c9a62SYan Zheng 		ret = btrfs_write_dirty_block_groups(trans, root);
52556bec294SChris Mason 		BUG_ON(ret);
5260b86a832SChris Mason 	}
527276e680dSYan Zheng 
528276e680dSYan Zheng 	if (root != root->fs_info->extent_root)
529817d52f8SJosef Bacik 		switch_commit_root(root);
530276e680dSYan Zheng 
5310b86a832SChris Mason 	return 0;
5320b86a832SChris Mason }
5330b86a832SChris Mason 
534d352ac68SChris Mason /*
535d352ac68SChris Mason  * update all the cowonly tree roots on disk
536d352ac68SChris Mason  */
5375d4f98a2SYan Zheng static noinline int commit_cowonly_roots(struct btrfs_trans_handle *trans,
5380b86a832SChris Mason 					 struct btrfs_root *root)
5390b86a832SChris Mason {
5400b86a832SChris Mason 	struct btrfs_fs_info *fs_info = root->fs_info;
5410b86a832SChris Mason 	struct list_head *next;
54284234f3aSYan Zheng 	struct extent_buffer *eb;
54356bec294SChris Mason 	int ret;
54484234f3aSYan Zheng 
54556bec294SChris Mason 	ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
54656bec294SChris Mason 	BUG_ON(ret);
54787ef2bb4SChris Mason 
54884234f3aSYan Zheng 	eb = btrfs_lock_root_node(fs_info->tree_root);
5499fa8cfe7SChris Mason 	btrfs_cow_block(trans, fs_info->tree_root, eb, NULL, 0, &eb);
55084234f3aSYan Zheng 	btrfs_tree_unlock(eb);
55184234f3aSYan Zheng 	free_extent_buffer(eb);
5520b86a832SChris Mason 
55356bec294SChris Mason 	ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
55456bec294SChris Mason 	BUG_ON(ret);
55587ef2bb4SChris Mason 
5560b86a832SChris Mason 	while (!list_empty(&fs_info->dirty_cowonly_roots)) {
5570b86a832SChris Mason 		next = fs_info->dirty_cowonly_roots.next;
5580b86a832SChris Mason 		list_del_init(next);
5590b86a832SChris Mason 		root = list_entry(next, struct btrfs_root, dirty_list);
56087ef2bb4SChris Mason 
5610b86a832SChris Mason 		update_cowonly_root(trans, root);
56279154b1bSChris Mason 	}
563276e680dSYan Zheng 
564276e680dSYan Zheng 	down_write(&fs_info->extent_commit_sem);
565276e680dSYan Zheng 	switch_commit_root(fs_info->extent_root);
566276e680dSYan Zheng 	up_write(&fs_info->extent_commit_sem);
567276e680dSYan Zheng 
56879154b1bSChris Mason 	return 0;
56979154b1bSChris Mason }
57079154b1bSChris Mason 
571d352ac68SChris Mason /*
572d352ac68SChris Mason  * dead roots are old snapshots that need to be deleted.  This allocates
573d352ac68SChris Mason  * a dirty root struct and adds it into the list of dead roots that need to
574d352ac68SChris Mason  * be deleted
575d352ac68SChris Mason  */
5765d4f98a2SYan Zheng int btrfs_add_dead_root(struct btrfs_root *root)
5775eda7b5eSChris Mason {
578b48652c1SYan Zheng 	mutex_lock(&root->fs_info->trans_mutex);
5795d4f98a2SYan Zheng 	list_add(&root->root_list, &root->fs_info->dead_roots);
580b48652c1SYan Zheng 	mutex_unlock(&root->fs_info->trans_mutex);
5815eda7b5eSChris Mason 	return 0;
5825eda7b5eSChris Mason }
5835eda7b5eSChris Mason 
584d352ac68SChris Mason /*
5855d4f98a2SYan Zheng  * update all the cowonly tree roots on disk
586d352ac68SChris Mason  */
5875d4f98a2SYan Zheng static noinline int commit_fs_roots(struct btrfs_trans_handle *trans,
5885d4f98a2SYan Zheng 				    struct btrfs_root *root)
5890f7d52f4SChris Mason {
5900f7d52f4SChris Mason 	struct btrfs_root *gang[8];
5915d4f98a2SYan Zheng 	struct btrfs_fs_info *fs_info = root->fs_info;
5920f7d52f4SChris Mason 	int i;
5930f7d52f4SChris Mason 	int ret;
59454aa1f4dSChris Mason 	int err = 0;
59554aa1f4dSChris Mason 
5960f7d52f4SChris Mason 	while (1) {
5975d4f98a2SYan Zheng 		ret = radix_tree_gang_lookup_tag(&fs_info->fs_roots_radix,
5985d4f98a2SYan Zheng 						 (void **)gang, 0,
5990f7d52f4SChris Mason 						 ARRAY_SIZE(gang),
6000f7d52f4SChris Mason 						 BTRFS_ROOT_TRANS_TAG);
6010f7d52f4SChris Mason 		if (ret == 0)
6020f7d52f4SChris Mason 			break;
6030f7d52f4SChris Mason 		for (i = 0; i < ret; i++) {
6040f7d52f4SChris Mason 			root = gang[i];
6055d4f98a2SYan Zheng 			radix_tree_tag_clear(&fs_info->fs_roots_radix,
6062619ba1fSChris Mason 					(unsigned long)root->root_key.objectid,
6070f7d52f4SChris Mason 					BTRFS_ROOT_TRANS_TAG);
60831153d81SYan Zheng 
609e02119d5SChris Mason 			btrfs_free_log(trans, root);
6105d4f98a2SYan Zheng 			btrfs_update_reloc_root(trans, root);
611e02119d5SChris Mason 
612978d910dSYan Zheng 			if (root->commit_root != root->node) {
613817d52f8SJosef Bacik 				switch_commit_root(root);
614978d910dSYan Zheng 				btrfs_set_root_node(&root->root_item,
615978d910dSYan Zheng 						    root->node);
616978d910dSYan Zheng 			}
61731153d81SYan Zheng 
6185d4f98a2SYan Zheng 			err = btrfs_update_root(trans, fs_info->tree_root,
6190f7d52f4SChris Mason 						&root->root_key,
6200f7d52f4SChris Mason 						&root->root_item);
62154aa1f4dSChris Mason 			if (err)
62254aa1f4dSChris Mason 				break;
6230f7d52f4SChris Mason 		}
6249f3a7427SChris Mason 	}
62554aa1f4dSChris Mason 	return err;
6260f7d52f4SChris Mason }
6270f7d52f4SChris Mason 
628d352ac68SChris Mason /*
629d352ac68SChris Mason  * defrag a given btree.  If cacheonly == 1, this won't read from the disk,
630d352ac68SChris Mason  * otherwise every leaf in the btree is read and defragged.
631d352ac68SChris Mason  */
632e9d0b13bSChris Mason int btrfs_defrag_root(struct btrfs_root *root, int cacheonly)
633e9d0b13bSChris Mason {
634e9d0b13bSChris Mason 	struct btrfs_fs_info *info = root->fs_info;
635e9d0b13bSChris Mason 	int ret;
636e9d0b13bSChris Mason 	struct btrfs_trans_handle *trans;
637d3c2fdcfSChris Mason 	unsigned long nr;
638e9d0b13bSChris Mason 
639a2135011SChris Mason 	smp_mb();
640e9d0b13bSChris Mason 	if (root->defrag_running)
641e9d0b13bSChris Mason 		return 0;
642e9d0b13bSChris Mason 	trans = btrfs_start_transaction(root, 1);
6436b80053dSChris Mason 	while (1) {
644e9d0b13bSChris Mason 		root->defrag_running = 1;
645e9d0b13bSChris Mason 		ret = btrfs_defrag_leaves(trans, root, cacheonly);
646d3c2fdcfSChris Mason 		nr = trans->blocks_used;
647e9d0b13bSChris Mason 		btrfs_end_transaction(trans, root);
648d3c2fdcfSChris Mason 		btrfs_btree_balance_dirty(info->tree_root, nr);
649e9d0b13bSChris Mason 		cond_resched();
650e9d0b13bSChris Mason 
651e9d0b13bSChris Mason 		trans = btrfs_start_transaction(root, 1);
6523f157a2fSChris Mason 		if (root->fs_info->closing || ret != -EAGAIN)
653e9d0b13bSChris Mason 			break;
654e9d0b13bSChris Mason 	}
655e9d0b13bSChris Mason 	root->defrag_running = 0;
656a2135011SChris Mason 	smp_mb();
657e9d0b13bSChris Mason 	btrfs_end_transaction(trans, root);
658e9d0b13bSChris Mason 	return 0;
659e9d0b13bSChris Mason }
660e9d0b13bSChris Mason 
6612c47e605SYan Zheng #if 0
662d352ac68SChris Mason /*
663b7ec40d7SChris Mason  * when dropping snapshots, we generate a ton of delayed refs, and it makes
664b7ec40d7SChris Mason  * sense not to join the transaction while it is trying to flush the current
665b7ec40d7SChris Mason  * queue of delayed refs out.
666b7ec40d7SChris Mason  *
667b7ec40d7SChris Mason  * This is used by the drop snapshot code only
668b7ec40d7SChris Mason  */
669b7ec40d7SChris Mason static noinline int wait_transaction_pre_flush(struct btrfs_fs_info *info)
670b7ec40d7SChris Mason {
671b7ec40d7SChris Mason 	DEFINE_WAIT(wait);
672b7ec40d7SChris Mason 
673b7ec40d7SChris Mason 	mutex_lock(&info->trans_mutex);
674b7ec40d7SChris Mason 	while (info->running_transaction &&
675b7ec40d7SChris Mason 	       info->running_transaction->delayed_refs.flushing) {
676b7ec40d7SChris Mason 		prepare_to_wait(&info->transaction_wait, &wait,
677b7ec40d7SChris Mason 				TASK_UNINTERRUPTIBLE);
678b7ec40d7SChris Mason 		mutex_unlock(&info->trans_mutex);
67959bc5c75SChris Mason 
680b7ec40d7SChris Mason 		schedule();
68159bc5c75SChris Mason 
682b7ec40d7SChris Mason 		mutex_lock(&info->trans_mutex);
683b7ec40d7SChris Mason 		finish_wait(&info->transaction_wait, &wait);
684b7ec40d7SChris Mason 	}
685b7ec40d7SChris Mason 	mutex_unlock(&info->trans_mutex);
686b7ec40d7SChris Mason 	return 0;
687b7ec40d7SChris Mason }
688b7ec40d7SChris Mason 
689b7ec40d7SChris Mason /*
690d352ac68SChris Mason  * Given a list of roots that need to be deleted, call btrfs_drop_snapshot on
691d352ac68SChris Mason  * all of them
692d352ac68SChris Mason  */
6935d4f98a2SYan Zheng int btrfs_drop_dead_root(struct btrfs_root *root)
6940f7d52f4SChris Mason {
6950f7d52f4SChris Mason 	struct btrfs_trans_handle *trans;
6965d4f98a2SYan Zheng 	struct btrfs_root *tree_root = root->fs_info->tree_root;
697d3c2fdcfSChris Mason 	unsigned long nr;
6985d4f98a2SYan Zheng 	int ret;
69958176a96SJosef Bacik 
7009f3a7427SChris Mason 	while (1) {
701b7ec40d7SChris Mason 		/*
702b7ec40d7SChris Mason 		 * we don't want to jump in and create a bunch of
703b7ec40d7SChris Mason 		 * delayed refs if the transaction is starting to close
704b7ec40d7SChris Mason 		 */
705b7ec40d7SChris Mason 		wait_transaction_pre_flush(tree_root->fs_info);
7060f7d52f4SChris Mason 		trans = btrfs_start_transaction(tree_root, 1);
707b7ec40d7SChris Mason 
708b7ec40d7SChris Mason 		/*
709b7ec40d7SChris Mason 		 * we've joined a transaction, make sure it isn't
710b7ec40d7SChris Mason 		 * closing right now
711b7ec40d7SChris Mason 		 */
712b7ec40d7SChris Mason 		if (trans->transaction->delayed_refs.flushing) {
713b7ec40d7SChris Mason 			btrfs_end_transaction(trans, tree_root);
714b7ec40d7SChris Mason 			continue;
715b7ec40d7SChris Mason 		}
716b7ec40d7SChris Mason 
7175d4f98a2SYan Zheng 		ret = btrfs_drop_snapshot(trans, root);
718d397712bSChris Mason 		if (ret != -EAGAIN)
7199f3a7427SChris Mason 			break;
72058176a96SJosef Bacik 
7215d4f98a2SYan Zheng 		ret = btrfs_update_root(trans, tree_root,
7225d4f98a2SYan Zheng 					&root->root_key,
7235d4f98a2SYan Zheng 					&root->root_item);
7245d4f98a2SYan Zheng 		if (ret)
72554aa1f4dSChris Mason 			break;
726bcc63abbSYan 
727d3c2fdcfSChris Mason 		nr = trans->blocks_used;
7280f7d52f4SChris Mason 		ret = btrfs_end_transaction(trans, tree_root);
7290f7d52f4SChris Mason 		BUG_ON(ret);
7305eda7b5eSChris Mason 
731d3c2fdcfSChris Mason 		btrfs_btree_balance_dirty(tree_root, nr);
7324dc11904SChris Mason 		cond_resched();
7330f7d52f4SChris Mason 	}
7345d4f98a2SYan Zheng 	BUG_ON(ret);
7355d4f98a2SYan Zheng 
7365d4f98a2SYan Zheng 	ret = btrfs_del_root(trans, tree_root, &root->root_key);
7375d4f98a2SYan Zheng 	BUG_ON(ret);
7385d4f98a2SYan Zheng 
7395d4f98a2SYan Zheng 	nr = trans->blocks_used;
7405d4f98a2SYan Zheng 	ret = btrfs_end_transaction(trans, tree_root);
7415d4f98a2SYan Zheng 	BUG_ON(ret);
7425d4f98a2SYan Zheng 
7435d4f98a2SYan Zheng 	free_extent_buffer(root->node);
7445d4f98a2SYan Zheng 	free_extent_buffer(root->commit_root);
7455d4f98a2SYan Zheng 	kfree(root);
7465d4f98a2SYan Zheng 
7475d4f98a2SYan Zheng 	btrfs_btree_balance_dirty(tree_root, nr);
74854aa1f4dSChris Mason 	return ret;
7490f7d52f4SChris Mason }
7502c47e605SYan Zheng #endif
7510f7d52f4SChris Mason 
752d352ac68SChris Mason /*
753d352ac68SChris Mason  * new snapshots need to be created at a very specific time in the
754d352ac68SChris Mason  * transaction commit.  This does the actual creation
755d352ac68SChris Mason  */
75680b6794dSChris Mason static noinline int create_pending_snapshot(struct btrfs_trans_handle *trans,
7573063d29fSChris Mason 				   struct btrfs_fs_info *fs_info,
7583063d29fSChris Mason 				   struct btrfs_pending_snapshot *pending)
7593063d29fSChris Mason {
7603063d29fSChris Mason 	struct btrfs_key key;
76180b6794dSChris Mason 	struct btrfs_root_item *new_root_item;
7623063d29fSChris Mason 	struct btrfs_root *tree_root = fs_info->tree_root;
7633063d29fSChris Mason 	struct btrfs_root *root = pending->root;
7643063d29fSChris Mason 	struct extent_buffer *tmp;
765925baeddSChris Mason 	struct extent_buffer *old;
7663063d29fSChris Mason 	int ret;
7673063d29fSChris Mason 	u64 objectid;
7683063d29fSChris Mason 
76980b6794dSChris Mason 	new_root_item = kmalloc(sizeof(*new_root_item), GFP_NOFS);
77080b6794dSChris Mason 	if (!new_root_item) {
77180b6794dSChris Mason 		ret = -ENOMEM;
77280b6794dSChris Mason 		goto fail;
77380b6794dSChris Mason 	}
7743063d29fSChris Mason 	ret = btrfs_find_free_objectid(trans, tree_root, 0, &objectid);
7753063d29fSChris Mason 	if (ret)
7763063d29fSChris Mason 		goto fail;
7773063d29fSChris Mason 
7785d4f98a2SYan Zheng 	record_root_in_trans(trans, root);
77980ff3856SYan Zheng 	btrfs_set_root_last_snapshot(&root->root_item, trans->transid);
78080b6794dSChris Mason 	memcpy(new_root_item, &root->root_item, sizeof(*new_root_item));
7813063d29fSChris Mason 
7823063d29fSChris Mason 	key.objectid = objectid;
7831c4850e2SYan, Zheng 	/* record when the snapshot was created in key.offset */
7841c4850e2SYan, Zheng 	key.offset = trans->transid;
7853063d29fSChris Mason 	btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
7863063d29fSChris Mason 
787925baeddSChris Mason 	old = btrfs_lock_root_node(root);
7889fa8cfe7SChris Mason 	btrfs_cow_block(trans, root, old, NULL, 0, &old);
7895d4f98a2SYan Zheng 	btrfs_set_lock_blocking(old);
7903063d29fSChris Mason 
791925baeddSChris Mason 	btrfs_copy_root(trans, root, old, &tmp, objectid);
792925baeddSChris Mason 	btrfs_tree_unlock(old);
793925baeddSChris Mason 	free_extent_buffer(old);
7943063d29fSChris Mason 
7955d4f98a2SYan Zheng 	btrfs_set_root_node(new_root_item, tmp);
7963063d29fSChris Mason 	ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
79780b6794dSChris Mason 				new_root_item);
798925baeddSChris Mason 	btrfs_tree_unlock(tmp);
7993063d29fSChris Mason 	free_extent_buffer(tmp);
8003063d29fSChris Mason 	if (ret)
8013063d29fSChris Mason 		goto fail;
8023063d29fSChris Mason 
8033de4586cSChris Mason 	key.offset = (u64)-1;
8043de4586cSChris Mason 	memcpy(&pending->root_key, &key, sizeof(key));
8053de4586cSChris Mason fail:
8063de4586cSChris Mason 	kfree(new_root_item);
8073de4586cSChris Mason 	return ret;
8083de4586cSChris Mason }
8093de4586cSChris Mason 
8103de4586cSChris Mason static noinline int finish_pending_snapshot(struct btrfs_fs_info *fs_info,
8113de4586cSChris Mason 				   struct btrfs_pending_snapshot *pending)
8123de4586cSChris Mason {
8133de4586cSChris Mason 	int ret;
8143de4586cSChris Mason 	int namelen;
8153de4586cSChris Mason 	u64 index = 0;
8163de4586cSChris Mason 	struct btrfs_trans_handle *trans;
8173de4586cSChris Mason 	struct inode *parent_inode;
8180660b5afSChris Mason 	struct btrfs_root *parent_root;
8193de4586cSChris Mason 
8203394e160SChris Mason 	parent_inode = pending->dentry->d_parent->d_inode;
8210660b5afSChris Mason 	parent_root = BTRFS_I(parent_inode)->root;
822180591bcSYan Zheng 	trans = btrfs_join_transaction(parent_root, 1);
8233de4586cSChris Mason 
8243063d29fSChris Mason 	/*
8253063d29fSChris Mason 	 * insert the directory item
8263063d29fSChris Mason 	 */
8273b96362cSSven Wegener 	namelen = strlen(pending->name);
8283de4586cSChris Mason 	ret = btrfs_set_inode_index(parent_inode, &index);
8290660b5afSChris Mason 	ret = btrfs_insert_dir_item(trans, parent_root,
8303b96362cSSven Wegener 			    pending->name, namelen,
8313de4586cSChris Mason 			    parent_inode->i_ino,
8323de4586cSChris Mason 			    &pending->root_key, BTRFS_FT_DIR, index);
8333063d29fSChris Mason 
8343063d29fSChris Mason 	if (ret)
8353063d29fSChris Mason 		goto fail;
8360660b5afSChris Mason 
83752c26179SYan Zheng 	btrfs_i_size_write(parent_inode, parent_inode->i_size + namelen * 2);
83852c26179SYan Zheng 	ret = btrfs_update_inode(trans, parent_root, parent_inode);
83952c26179SYan Zheng 	BUG_ON(ret);
84052c26179SYan Zheng 
8410660b5afSChris Mason 	ret = btrfs_add_root_ref(trans, parent_root->fs_info->tree_root,
8420660b5afSChris Mason 				 pending->root_key.objectid,
8430660b5afSChris Mason 				 parent_root->root_key.objectid,
8440660b5afSChris Mason 				 parent_inode->i_ino, index, pending->name,
8450660b5afSChris Mason 				 namelen);
8460660b5afSChris Mason 
8470660b5afSChris Mason 	BUG_ON(ret);
8480660b5afSChris Mason 
8493063d29fSChris Mason fail:
8503de4586cSChris Mason 	btrfs_end_transaction(trans, fs_info->fs_root);
8513063d29fSChris Mason 	return ret;
8523063d29fSChris Mason }
8533063d29fSChris Mason 
854d352ac68SChris Mason /*
855d352ac68SChris Mason  * create all the snapshots we've scheduled for creation
856d352ac68SChris Mason  */
85780b6794dSChris Mason static noinline int create_pending_snapshots(struct btrfs_trans_handle *trans,
8583063d29fSChris Mason 					     struct btrfs_fs_info *fs_info)
8593063d29fSChris Mason {
8603063d29fSChris Mason 	struct btrfs_pending_snapshot *pending;
8613063d29fSChris Mason 	struct list_head *head = &trans->transaction->pending_snapshots;
8623de4586cSChris Mason 	int ret;
8633de4586cSChris Mason 
864c6e30871SQinghuang Feng 	list_for_each_entry(pending, head, list) {
8653de4586cSChris Mason 		ret = create_pending_snapshot(trans, fs_info, pending);
8663de4586cSChris Mason 		BUG_ON(ret);
8673de4586cSChris Mason 	}
8683de4586cSChris Mason 	return 0;
8693de4586cSChris Mason }
8703de4586cSChris Mason 
8713de4586cSChris Mason static noinline int finish_pending_snapshots(struct btrfs_trans_handle *trans,
8723de4586cSChris Mason 					     struct btrfs_fs_info *fs_info)
8733de4586cSChris Mason {
8743de4586cSChris Mason 	struct btrfs_pending_snapshot *pending;
8753de4586cSChris Mason 	struct list_head *head = &trans->transaction->pending_snapshots;
8763063d29fSChris Mason 	int ret;
8773063d29fSChris Mason 
8783063d29fSChris Mason 	while (!list_empty(head)) {
8793063d29fSChris Mason 		pending = list_entry(head->next,
8803063d29fSChris Mason 				     struct btrfs_pending_snapshot, list);
8813de4586cSChris Mason 		ret = finish_pending_snapshot(fs_info, pending);
8823063d29fSChris Mason 		BUG_ON(ret);
8833063d29fSChris Mason 		list_del(&pending->list);
8843063d29fSChris Mason 		kfree(pending->name);
8853063d29fSChris Mason 		kfree(pending);
8863063d29fSChris Mason 	}
887dc17ff8fSChris Mason 	return 0;
888dc17ff8fSChris Mason }
889dc17ff8fSChris Mason 
8905d4f98a2SYan Zheng static void update_super_roots(struct btrfs_root *root)
8915d4f98a2SYan Zheng {
8925d4f98a2SYan Zheng 	struct btrfs_root_item *root_item;
8935d4f98a2SYan Zheng 	struct btrfs_super_block *super;
8945d4f98a2SYan Zheng 
8955d4f98a2SYan Zheng 	super = &root->fs_info->super_copy;
8965d4f98a2SYan Zheng 
8975d4f98a2SYan Zheng 	root_item = &root->fs_info->chunk_root->root_item;
8985d4f98a2SYan Zheng 	super->chunk_root = root_item->bytenr;
8995d4f98a2SYan Zheng 	super->chunk_root_generation = root_item->generation;
9005d4f98a2SYan Zheng 	super->chunk_root_level = root_item->level;
9015d4f98a2SYan Zheng 
9025d4f98a2SYan Zheng 	root_item = &root->fs_info->tree_root->root_item;
9035d4f98a2SYan Zheng 	super->root = root_item->bytenr;
9045d4f98a2SYan Zheng 	super->generation = root_item->generation;
9055d4f98a2SYan Zheng 	super->root_level = root_item->level;
9065d4f98a2SYan Zheng }
9075d4f98a2SYan Zheng 
908f36f3042SChris Mason int btrfs_transaction_in_commit(struct btrfs_fs_info *info)
909f36f3042SChris Mason {
910f36f3042SChris Mason 	int ret = 0;
911f36f3042SChris Mason 	spin_lock(&info->new_trans_lock);
912f36f3042SChris Mason 	if (info->running_transaction)
913f36f3042SChris Mason 		ret = info->running_transaction->in_commit;
914f36f3042SChris Mason 	spin_unlock(&info->new_trans_lock);
915f36f3042SChris Mason 	return ret;
916f36f3042SChris Mason }
917f36f3042SChris Mason 
91879154b1bSChris Mason int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
91979154b1bSChris Mason 			     struct btrfs_root *root)
92079154b1bSChris Mason {
92115ee9bc7SJosef Bacik 	unsigned long joined = 0;
92215ee9bc7SJosef Bacik 	unsigned long timeout = 1;
92379154b1bSChris Mason 	struct btrfs_transaction *cur_trans;
9248fd17795SChris Mason 	struct btrfs_transaction *prev_trans = NULL;
92579154b1bSChris Mason 	DEFINE_WAIT(wait);
92615ee9bc7SJosef Bacik 	int ret;
92789573b9cSChris Mason 	int should_grow = 0;
92889573b9cSChris Mason 	unsigned long now = get_seconds();
929dccae999SSage Weil 	int flush_on_commit = btrfs_test_opt(root, FLUSHONCOMMIT);
93079154b1bSChris Mason 
9315a3f23d5SChris Mason 	btrfs_run_ordered_operations(root, 0);
9325a3f23d5SChris Mason 
93356bec294SChris Mason 	/* make a pass through all the delayed refs we have so far
93456bec294SChris Mason 	 * any runnings procs may add more while we are here
93556bec294SChris Mason 	 */
93656bec294SChris Mason 	ret = btrfs_run_delayed_refs(trans, root, 0);
93756bec294SChris Mason 	BUG_ON(ret);
93856bec294SChris Mason 
939b7ec40d7SChris Mason 	cur_trans = trans->transaction;
94056bec294SChris Mason 	/*
94156bec294SChris Mason 	 * set the flushing flag so procs in this transaction have to
94256bec294SChris Mason 	 * start sending their work down.
94356bec294SChris Mason 	 */
944b7ec40d7SChris Mason 	cur_trans->delayed_refs.flushing = 1;
94556bec294SChris Mason 
946c3e69d58SChris Mason 	ret = btrfs_run_delayed_refs(trans, root, 0);
94756bec294SChris Mason 	BUG_ON(ret);
94856bec294SChris Mason 
94979154b1bSChris Mason 	mutex_lock(&root->fs_info->trans_mutex);
950b7ec40d7SChris Mason 	if (cur_trans->in_commit) {
951b7ec40d7SChris Mason 		cur_trans->use_count++;
952ccd467d6SChris Mason 		mutex_unlock(&root->fs_info->trans_mutex);
95379154b1bSChris Mason 		btrfs_end_transaction(trans, root);
954ccd467d6SChris Mason 
95579154b1bSChris Mason 		ret = wait_for_commit(root, cur_trans);
95679154b1bSChris Mason 		BUG_ON(ret);
95715ee9bc7SJosef Bacik 
95815ee9bc7SJosef Bacik 		mutex_lock(&root->fs_info->trans_mutex);
95979154b1bSChris Mason 		put_transaction(cur_trans);
96015ee9bc7SJosef Bacik 		mutex_unlock(&root->fs_info->trans_mutex);
96115ee9bc7SJosef Bacik 
96279154b1bSChris Mason 		return 0;
96379154b1bSChris Mason 	}
9644313b399SChris Mason 
9652c90e5d6SChris Mason 	trans->transaction->in_commit = 1;
966f9295749SChris Mason 	trans->transaction->blocked = 1;
967ccd467d6SChris Mason 	if (cur_trans->list.prev != &root->fs_info->trans_list) {
968ccd467d6SChris Mason 		prev_trans = list_entry(cur_trans->list.prev,
969ccd467d6SChris Mason 					struct btrfs_transaction, list);
970ccd467d6SChris Mason 		if (!prev_trans->commit_done) {
971ccd467d6SChris Mason 			prev_trans->use_count++;
972ccd467d6SChris Mason 			mutex_unlock(&root->fs_info->trans_mutex);
973ccd467d6SChris Mason 
974ccd467d6SChris Mason 			wait_for_commit(root, prev_trans);
975ccd467d6SChris Mason 
976ccd467d6SChris Mason 			mutex_lock(&root->fs_info->trans_mutex);
97715ee9bc7SJosef Bacik 			put_transaction(prev_trans);
978ccd467d6SChris Mason 		}
979ccd467d6SChris Mason 	}
98015ee9bc7SJosef Bacik 
98189573b9cSChris Mason 	if (now < cur_trans->start_time || now - cur_trans->start_time < 1)
98289573b9cSChris Mason 		should_grow = 1;
98389573b9cSChris Mason 
98415ee9bc7SJosef Bacik 	do {
9857ea394f1SYan Zheng 		int snap_pending = 0;
98615ee9bc7SJosef Bacik 		joined = cur_trans->num_joined;
9877ea394f1SYan Zheng 		if (!list_empty(&trans->transaction->pending_snapshots))
9887ea394f1SYan Zheng 			snap_pending = 1;
9897ea394f1SYan Zheng 
9902c90e5d6SChris Mason 		WARN_ON(cur_trans != trans->transaction);
99115ee9bc7SJosef Bacik 		prepare_to_wait(&cur_trans->writer_wait, &wait,
99279154b1bSChris Mason 				TASK_UNINTERRUPTIBLE);
99315ee9bc7SJosef Bacik 
99415ee9bc7SJosef Bacik 		if (cur_trans->num_writers > 1)
99515ee9bc7SJosef Bacik 			timeout = MAX_SCHEDULE_TIMEOUT;
99689573b9cSChris Mason 		else if (should_grow)
99715ee9bc7SJosef Bacik 			timeout = 1;
99815ee9bc7SJosef Bacik 
99979154b1bSChris Mason 		mutex_unlock(&root->fs_info->trans_mutex);
100015ee9bc7SJosef Bacik 
10010bdb1db2SSage Weil 		if (flush_on_commit || snap_pending) {
100224bbcf04SYan, Zheng 			btrfs_start_delalloc_inodes(root, 1);
100324bbcf04SYan, Zheng 			ret = btrfs_wait_ordered_extents(root, 0, 1);
1004ebecd3d9SSage Weil 			BUG_ON(ret);
10057ea394f1SYan Zheng 		}
10067ea394f1SYan Zheng 
10075a3f23d5SChris Mason 		/*
10085a3f23d5SChris Mason 		 * rename don't use btrfs_join_transaction, so, once we
10095a3f23d5SChris Mason 		 * set the transaction to blocked above, we aren't going
10105a3f23d5SChris Mason 		 * to get any new ordered operations.  We can safely run
10115a3f23d5SChris Mason 		 * it here and no for sure that nothing new will be added
10125a3f23d5SChris Mason 		 * to the list
10135a3f23d5SChris Mason 		 */
10145a3f23d5SChris Mason 		btrfs_run_ordered_operations(root, 1);
10155a3f23d5SChris Mason 
101689573b9cSChris Mason 		smp_mb();
101789573b9cSChris Mason 		if (cur_trans->num_writers > 1 || should_grow)
101815ee9bc7SJosef Bacik 			schedule_timeout(timeout);
101915ee9bc7SJosef Bacik 
102079154b1bSChris Mason 		mutex_lock(&root->fs_info->trans_mutex);
102115ee9bc7SJosef Bacik 		finish_wait(&cur_trans->writer_wait, &wait);
102215ee9bc7SJosef Bacik 	} while (cur_trans->num_writers > 1 ||
102389573b9cSChris Mason 		 (should_grow && cur_trans->num_joined != joined));
102415ee9bc7SJosef Bacik 
10253063d29fSChris Mason 	ret = create_pending_snapshots(trans, root->fs_info);
10263063d29fSChris Mason 	BUG_ON(ret);
10273063d29fSChris Mason 
102856bec294SChris Mason 	ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
102956bec294SChris Mason 	BUG_ON(ret);
103056bec294SChris Mason 
10312c90e5d6SChris Mason 	WARN_ON(cur_trans != trans->transaction);
1032dc17ff8fSChris Mason 
1033e02119d5SChris Mason 	/* btrfs_commit_tree_roots is responsible for getting the
1034e02119d5SChris Mason 	 * various roots consistent with each other.  Every pointer
1035e02119d5SChris Mason 	 * in the tree of tree roots has to point to the most up to date
1036e02119d5SChris Mason 	 * root for every subvolume and other tree.  So, we have to keep
1037e02119d5SChris Mason 	 * the tree logging code from jumping in and changing any
1038e02119d5SChris Mason 	 * of the trees.
1039e02119d5SChris Mason 	 *
1040e02119d5SChris Mason 	 * At this point in the commit, there can't be any tree-log
1041e02119d5SChris Mason 	 * writers, but a little lower down we drop the trans mutex
1042e02119d5SChris Mason 	 * and let new people in.  By holding the tree_log_mutex
1043e02119d5SChris Mason 	 * from now until after the super is written, we avoid races
1044e02119d5SChris Mason 	 * with the tree-log code.
1045e02119d5SChris Mason 	 */
1046e02119d5SChris Mason 	mutex_lock(&root->fs_info->tree_log_mutex);
10471a40e23bSZheng Yan 
10485d4f98a2SYan Zheng 	ret = commit_fs_roots(trans, root);
104954aa1f4dSChris Mason 	BUG_ON(ret);
105054aa1f4dSChris Mason 
10515d4f98a2SYan Zheng 	/* commit_fs_roots gets rid of all the tree log roots, it is now
1052e02119d5SChris Mason 	 * safe to free the root of tree log roots
1053e02119d5SChris Mason 	 */
1054e02119d5SChris Mason 	btrfs_free_log_root_tree(trans, root->fs_info);
1055e02119d5SChris Mason 
10565d4f98a2SYan Zheng 	ret = commit_cowonly_roots(trans, root);
105779154b1bSChris Mason 	BUG_ON(ret);
105854aa1f4dSChris Mason 
105911833d66SYan Zheng 	btrfs_prepare_extent_commit(trans, root);
106011833d66SYan Zheng 
106178fae27eSChris Mason 	cur_trans = root->fs_info->running_transaction;
1062cee36a03SChris Mason 	spin_lock(&root->fs_info->new_trans_lock);
106378fae27eSChris Mason 	root->fs_info->running_transaction = NULL;
1064cee36a03SChris Mason 	spin_unlock(&root->fs_info->new_trans_lock);
10655f39d397SChris Mason 
10665d4f98a2SYan Zheng 	btrfs_set_root_node(&root->fs_info->tree_root->root_item,
10675d4f98a2SYan Zheng 			    root->fs_info->tree_root->node);
1068817d52f8SJosef Bacik 	switch_commit_root(root->fs_info->tree_root);
10695d4f98a2SYan Zheng 
10705d4f98a2SYan Zheng 	btrfs_set_root_node(&root->fs_info->chunk_root->root_item,
10715d4f98a2SYan Zheng 			    root->fs_info->chunk_root->node);
1072817d52f8SJosef Bacik 	switch_commit_root(root->fs_info->chunk_root);
10735d4f98a2SYan Zheng 
10745d4f98a2SYan Zheng 	update_super_roots(root);
1075e02119d5SChris Mason 
1076e02119d5SChris Mason 	if (!root->fs_info->log_root_recovering) {
1077e02119d5SChris Mason 		btrfs_set_super_log_root(&root->fs_info->super_copy, 0);
1078e02119d5SChris Mason 		btrfs_set_super_log_root_level(&root->fs_info->super_copy, 0);
1079e02119d5SChris Mason 	}
1080e02119d5SChris Mason 
1081a061fc8dSChris Mason 	memcpy(&root->fs_info->super_for_commit, &root->fs_info->super_copy,
10824b52dff6SChris Mason 	       sizeof(root->fs_info->super_copy));
1083ccd467d6SChris Mason 
1084f9295749SChris Mason 	trans->transaction->blocked = 0;
1085b7ec40d7SChris Mason 
1086f9295749SChris Mason 	wake_up(&root->fs_info->transaction_wait);
1087e6dcd2dcSChris Mason 
108878fae27eSChris Mason 	mutex_unlock(&root->fs_info->trans_mutex);
108979154b1bSChris Mason 	ret = btrfs_write_and_wait_transaction(trans, root);
109079154b1bSChris Mason 	BUG_ON(ret);
1091a512bbf8SYan Zheng 	write_ctree_super(trans, root, 0);
10924313b399SChris Mason 
1093e02119d5SChris Mason 	/*
1094e02119d5SChris Mason 	 * the super is written, we can safely allow the tree-loggers
1095e02119d5SChris Mason 	 * to go about their business
1096e02119d5SChris Mason 	 */
1097e02119d5SChris Mason 	mutex_unlock(&root->fs_info->tree_log_mutex);
1098e02119d5SChris Mason 
109911833d66SYan Zheng 	btrfs_finish_extent_commit(trans, root);
11004313b399SChris Mason 
11013de4586cSChris Mason 	/* do the directory inserts of any pending snapshot creations */
11023de4586cSChris Mason 	finish_pending_snapshots(trans, root->fs_info);
11033de4586cSChris Mason 
11041a40e23bSZheng Yan 	mutex_lock(&root->fs_info->trans_mutex);
11051a40e23bSZheng Yan 
11062c90e5d6SChris Mason 	cur_trans->commit_done = 1;
1107b7ec40d7SChris Mason 
110815ee9bc7SJosef Bacik 	root->fs_info->last_trans_committed = cur_trans->transid;
1109817d52f8SJosef Bacik 
11102c90e5d6SChris Mason 	wake_up(&cur_trans->commit_wait);
11113de4586cSChris Mason 
111279154b1bSChris Mason 	put_transaction(cur_trans);
111378fae27eSChris Mason 	put_transaction(cur_trans);
111458176a96SJosef Bacik 
111578fae27eSChris Mason 	mutex_unlock(&root->fs_info->trans_mutex);
11163de4586cSChris Mason 
11179ed74f2dSJosef Bacik 	if (current->journal_info == trans)
11189ed74f2dSJosef Bacik 		current->journal_info = NULL;
11199ed74f2dSJosef Bacik 
11202c90e5d6SChris Mason 	kmem_cache_free(btrfs_trans_handle_cachep, trans);
112124bbcf04SYan, Zheng 
112224bbcf04SYan, Zheng 	if (current != root->fs_info->transaction_kthread)
112324bbcf04SYan, Zheng 		btrfs_run_delayed_iputs(root);
112424bbcf04SYan, Zheng 
112579154b1bSChris Mason 	return ret;
112679154b1bSChris Mason }
112779154b1bSChris Mason 
1128d352ac68SChris Mason /*
1129d352ac68SChris Mason  * interface function to delete all the snapshots we have scheduled for deletion
1130d352ac68SChris Mason  */
1131e9d0b13bSChris Mason int btrfs_clean_old_snapshots(struct btrfs_root *root)
1132e9d0b13bSChris Mason {
11335d4f98a2SYan Zheng 	LIST_HEAD(list);
11345d4f98a2SYan Zheng 	struct btrfs_fs_info *fs_info = root->fs_info;
1135e9d0b13bSChris Mason 
11365d4f98a2SYan Zheng 	mutex_lock(&fs_info->trans_mutex);
11375d4f98a2SYan Zheng 	list_splice_init(&fs_info->dead_roots, &list);
11385d4f98a2SYan Zheng 	mutex_unlock(&fs_info->trans_mutex);
11395d4f98a2SYan Zheng 
11405d4f98a2SYan Zheng 	while (!list_empty(&list)) {
11415d4f98a2SYan Zheng 		root = list_entry(list.next, struct btrfs_root, root_list);
114276dda93cSYan, Zheng 		list_del(&root->root_list);
114376dda93cSYan, Zheng 
114476dda93cSYan, Zheng 		if (btrfs_header_backref_rev(root->node) <
114576dda93cSYan, Zheng 		    BTRFS_MIXED_BACKREF_REV)
11462c47e605SYan Zheng 			btrfs_drop_snapshot(root, 0);
114776dda93cSYan, Zheng 		else
114876dda93cSYan, Zheng 			btrfs_drop_snapshot(root, 1);
1149e9d0b13bSChris Mason 	}
1150e9d0b13bSChris Mason 	return 0;
1151e9d0b13bSChris Mason }
1152