xref: /openbmc/linux/fs/btrfs/transaction.c (revision 18f687d5)
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>
258ea05e3aSAlexander Block #include <linux/uuid.h>
2679154b1bSChris Mason #include "ctree.h"
2779154b1bSChris Mason #include "disk-io.h"
2879154b1bSChris Mason #include "transaction.h"
29925baeddSChris Mason #include "locking.h"
30e02119d5SChris Mason #include "tree-log.h"
31581bb050SLi Zefan #include "inode-map.h"
32733f4fbbSStefan Behrens #include "volumes.h"
338dabb742SStefan Behrens #include "dev-replace.h"
3479154b1bSChris Mason 
350f7d52f4SChris Mason #define BTRFS_ROOT_TRANS_TAG 0
360f7d52f4SChris Mason 
374a9d8bdeSMiao Xie static unsigned int btrfs_blocked_trans_types[TRANS_STATE_MAX] = {
384a9d8bdeSMiao Xie 	[TRANS_STATE_RUNNING]		= 0U,
394a9d8bdeSMiao Xie 	[TRANS_STATE_BLOCKED]		= (__TRANS_USERSPACE |
404a9d8bdeSMiao Xie 					   __TRANS_START),
414a9d8bdeSMiao Xie 	[TRANS_STATE_COMMIT_START]	= (__TRANS_USERSPACE |
424a9d8bdeSMiao Xie 					   __TRANS_START |
434a9d8bdeSMiao Xie 					   __TRANS_ATTACH),
444a9d8bdeSMiao Xie 	[TRANS_STATE_COMMIT_DOING]	= (__TRANS_USERSPACE |
454a9d8bdeSMiao Xie 					   __TRANS_START |
464a9d8bdeSMiao Xie 					   __TRANS_ATTACH |
474a9d8bdeSMiao Xie 					   __TRANS_JOIN),
484a9d8bdeSMiao Xie 	[TRANS_STATE_UNBLOCKED]		= (__TRANS_USERSPACE |
494a9d8bdeSMiao Xie 					   __TRANS_START |
504a9d8bdeSMiao Xie 					   __TRANS_ATTACH |
514a9d8bdeSMiao Xie 					   __TRANS_JOIN |
524a9d8bdeSMiao Xie 					   __TRANS_JOIN_NOLOCK),
534a9d8bdeSMiao Xie 	[TRANS_STATE_COMPLETED]		= (__TRANS_USERSPACE |
544a9d8bdeSMiao Xie 					   __TRANS_START |
554a9d8bdeSMiao Xie 					   __TRANS_ATTACH |
564a9d8bdeSMiao Xie 					   __TRANS_JOIN |
574a9d8bdeSMiao Xie 					   __TRANS_JOIN_NOLOCK),
584a9d8bdeSMiao Xie };
594a9d8bdeSMiao Xie 
60724e2315SJosef Bacik void btrfs_put_transaction(struct btrfs_transaction *transaction)
6179154b1bSChris Mason {
6213c5a93eSJosef Bacik 	WARN_ON(atomic_read(&transaction->use_count) == 0);
6313c5a93eSJosef Bacik 	if (atomic_dec_and_test(&transaction->use_count)) {
64a4abeea4SJosef Bacik 		BUG_ON(!list_empty(&transaction->list));
65c46effa6SLiu Bo 		WARN_ON(!RB_EMPTY_ROOT(&transaction->delayed_refs.root));
66c46effa6SLiu Bo 		WARN_ON(!RB_EMPTY_ROOT(&transaction->delayed_refs.href_root));
676df9a95eSJosef Bacik 		while (!list_empty(&transaction->pending_chunks)) {
686df9a95eSJosef Bacik 			struct extent_map *em;
696df9a95eSJosef Bacik 
706df9a95eSJosef Bacik 			em = list_first_entry(&transaction->pending_chunks,
716df9a95eSJosef Bacik 					      struct extent_map, list);
726df9a95eSJosef Bacik 			list_del_init(&em->list);
736df9a95eSJosef Bacik 			free_extent_map(em);
746df9a95eSJosef Bacik 		}
752c90e5d6SChris Mason 		kmem_cache_free(btrfs_transaction_cachep, transaction);
7679154b1bSChris Mason 	}
7778fae27eSChris Mason }
7879154b1bSChris Mason 
79817d52f8SJosef Bacik static noinline void switch_commit_root(struct btrfs_root *root)
80817d52f8SJosef Bacik {
81817d52f8SJosef Bacik 	free_extent_buffer(root->commit_root);
82817d52f8SJosef Bacik 	root->commit_root = btrfs_root_node(root);
83817d52f8SJosef Bacik }
84817d52f8SJosef Bacik 
850860adfdSMiao Xie static inline void extwriter_counter_inc(struct btrfs_transaction *trans,
860860adfdSMiao Xie 					 unsigned int type)
870860adfdSMiao Xie {
880860adfdSMiao Xie 	if (type & TRANS_EXTWRITERS)
890860adfdSMiao Xie 		atomic_inc(&trans->num_extwriters);
900860adfdSMiao Xie }
910860adfdSMiao Xie 
920860adfdSMiao Xie static inline void extwriter_counter_dec(struct btrfs_transaction *trans,
930860adfdSMiao Xie 					 unsigned int type)
940860adfdSMiao Xie {
950860adfdSMiao Xie 	if (type & TRANS_EXTWRITERS)
960860adfdSMiao Xie 		atomic_dec(&trans->num_extwriters);
970860adfdSMiao Xie }
980860adfdSMiao Xie 
990860adfdSMiao Xie static inline void extwriter_counter_init(struct btrfs_transaction *trans,
1000860adfdSMiao Xie 					  unsigned int type)
1010860adfdSMiao Xie {
1020860adfdSMiao Xie 	atomic_set(&trans->num_extwriters, ((type & TRANS_EXTWRITERS) ? 1 : 0));
1030860adfdSMiao Xie }
1040860adfdSMiao Xie 
1050860adfdSMiao Xie static inline int extwriter_counter_read(struct btrfs_transaction *trans)
1060860adfdSMiao Xie {
1070860adfdSMiao Xie 	return atomic_read(&trans->num_extwriters);
108178260b2SMiao Xie }
109178260b2SMiao Xie 
110d352ac68SChris Mason /*
111d352ac68SChris Mason  * either allocate a new transaction or hop into the existing one
112d352ac68SChris Mason  */
1130860adfdSMiao Xie static noinline int join_transaction(struct btrfs_root *root, unsigned int type)
11479154b1bSChris Mason {
11579154b1bSChris Mason 	struct btrfs_transaction *cur_trans;
11619ae4e81SJan Schmidt 	struct btrfs_fs_info *fs_info = root->fs_info;
117a4abeea4SJosef Bacik 
11819ae4e81SJan Schmidt 	spin_lock(&fs_info->trans_lock);
119d43317dcSChris Mason loop:
12049b25e05SJeff Mahoney 	/* The file system has been taken offline. No new transactions. */
12187533c47SMiao Xie 	if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) {
12219ae4e81SJan Schmidt 		spin_unlock(&fs_info->trans_lock);
12349b25e05SJeff Mahoney 		return -EROFS;
12449b25e05SJeff Mahoney 	}
12549b25e05SJeff Mahoney 
12619ae4e81SJan Schmidt 	cur_trans = fs_info->running_transaction;
127a4abeea4SJosef Bacik 	if (cur_trans) {
128871383beSDavid Sterba 		if (cur_trans->aborted) {
12919ae4e81SJan Schmidt 			spin_unlock(&fs_info->trans_lock);
13049b25e05SJeff Mahoney 			return cur_trans->aborted;
131871383beSDavid Sterba 		}
1324a9d8bdeSMiao Xie 		if (btrfs_blocked_trans_types[cur_trans->state] & type) {
133178260b2SMiao Xie 			spin_unlock(&fs_info->trans_lock);
134178260b2SMiao Xie 			return -EBUSY;
135178260b2SMiao Xie 		}
136a4abeea4SJosef Bacik 		atomic_inc(&cur_trans->use_count);
137a4abeea4SJosef Bacik 		atomic_inc(&cur_trans->num_writers);
1380860adfdSMiao Xie 		extwriter_counter_inc(cur_trans, type);
13919ae4e81SJan Schmidt 		spin_unlock(&fs_info->trans_lock);
140a4abeea4SJosef Bacik 		return 0;
141a4abeea4SJosef Bacik 	}
14219ae4e81SJan Schmidt 	spin_unlock(&fs_info->trans_lock);
143a4abeea4SJosef Bacik 
144354aa0fbSMiao Xie 	/*
145354aa0fbSMiao Xie 	 * If we are ATTACH, we just want to catch the current transaction,
146354aa0fbSMiao Xie 	 * and commit it. If there is no transaction, just return ENOENT.
147354aa0fbSMiao Xie 	 */
148354aa0fbSMiao Xie 	if (type == TRANS_ATTACH)
149354aa0fbSMiao Xie 		return -ENOENT;
150354aa0fbSMiao Xie 
1514a9d8bdeSMiao Xie 	/*
1524a9d8bdeSMiao Xie 	 * JOIN_NOLOCK only happens during the transaction commit, so
1534a9d8bdeSMiao Xie 	 * it is impossible that ->running_transaction is NULL
1544a9d8bdeSMiao Xie 	 */
1554a9d8bdeSMiao Xie 	BUG_ON(type == TRANS_JOIN_NOLOCK);
1564a9d8bdeSMiao Xie 
157a4abeea4SJosef Bacik 	cur_trans = kmem_cache_alloc(btrfs_transaction_cachep, GFP_NOFS);
158db5b493aSTsutomu Itoh 	if (!cur_trans)
159db5b493aSTsutomu Itoh 		return -ENOMEM;
160d43317dcSChris Mason 
16119ae4e81SJan Schmidt 	spin_lock(&fs_info->trans_lock);
16219ae4e81SJan Schmidt 	if (fs_info->running_transaction) {
163d43317dcSChris Mason 		/*
164d43317dcSChris Mason 		 * someone started a transaction after we unlocked.  Make sure
1654a9d8bdeSMiao Xie 		 * to redo the checks above
166d43317dcSChris Mason 		 */
167a4abeea4SJosef Bacik 		kmem_cache_free(btrfs_transaction_cachep, cur_trans);
168d43317dcSChris Mason 		goto loop;
16987533c47SMiao Xie 	} else if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) {
170e4b50e14SDan Carpenter 		spin_unlock(&fs_info->trans_lock);
1717b8b92afSJosef Bacik 		kmem_cache_free(btrfs_transaction_cachep, cur_trans);
1727b8b92afSJosef Bacik 		return -EROFS;
173a4abeea4SJosef Bacik 	}
174d43317dcSChris Mason 
17513c5a93eSJosef Bacik 	atomic_set(&cur_trans->num_writers, 1);
1760860adfdSMiao Xie 	extwriter_counter_init(cur_trans, type);
17779154b1bSChris Mason 	init_waitqueue_head(&cur_trans->writer_wait);
17879154b1bSChris Mason 	init_waitqueue_head(&cur_trans->commit_wait);
1794a9d8bdeSMiao Xie 	cur_trans->state = TRANS_STATE_RUNNING;
180a4abeea4SJosef Bacik 	/*
181a4abeea4SJosef Bacik 	 * One for this trans handle, one so it will live on until we
182a4abeea4SJosef Bacik 	 * commit the transaction.
183a4abeea4SJosef Bacik 	 */
184a4abeea4SJosef Bacik 	atomic_set(&cur_trans->use_count, 2);
18508607c1bSChris Mason 	cur_trans->start_time = get_seconds();
18656bec294SChris Mason 
1876bef4d31SEric Paris 	cur_trans->delayed_refs.root = RB_ROOT;
188c46effa6SLiu Bo 	cur_trans->delayed_refs.href_root = RB_ROOT;
18956bec294SChris Mason 	cur_trans->delayed_refs.num_entries = 0;
190c3e69d58SChris Mason 	cur_trans->delayed_refs.num_heads_ready = 0;
191c3e69d58SChris Mason 	cur_trans->delayed_refs.num_heads = 0;
19256bec294SChris Mason 	cur_trans->delayed_refs.flushing = 0;
193c3e69d58SChris Mason 	cur_trans->delayed_refs.run_delayed_start = 0;
19420b297d6SJan Schmidt 
19520b297d6SJan Schmidt 	/*
19620b297d6SJan Schmidt 	 * although the tree mod log is per file system and not per transaction,
19720b297d6SJan Schmidt 	 * the log must never go across transaction boundaries.
19820b297d6SJan Schmidt 	 */
19920b297d6SJan Schmidt 	smp_mb();
20031b1a2bdSJulia Lawall 	if (!list_empty(&fs_info->tree_mod_seq_list))
201efe120a0SFrank Holton 		WARN(1, KERN_ERR "BTRFS: tree_mod_seq_list not empty when "
20220b297d6SJan Schmidt 			"creating a fresh transaction\n");
20331b1a2bdSJulia Lawall 	if (!RB_EMPTY_ROOT(&fs_info->tree_mod_log))
204efe120a0SFrank Holton 		WARN(1, KERN_ERR "BTRFS: tree_mod_log rb tree not empty when "
20520b297d6SJan Schmidt 			"creating a fresh transaction\n");
206fc36ed7eSJan Schmidt 	atomic64_set(&fs_info->tree_mod_seq, 0);
20720b297d6SJan Schmidt 
20856bec294SChris Mason 	spin_lock_init(&cur_trans->delayed_refs.lock);
209bb721703SChris Mason 	atomic_set(&cur_trans->delayed_refs.procs_running_refs, 0);
210bb721703SChris Mason 	atomic_set(&cur_trans->delayed_refs.ref_seq, 0);
211bb721703SChris Mason 	init_waitqueue_head(&cur_trans->delayed_refs.wait);
21256bec294SChris Mason 
2133063d29fSChris Mason 	INIT_LIST_HEAD(&cur_trans->pending_snapshots);
214569e0f35SJosef Bacik 	INIT_LIST_HEAD(&cur_trans->ordered_operations);
2156df9a95eSJosef Bacik 	INIT_LIST_HEAD(&cur_trans->pending_chunks);
21619ae4e81SJan Schmidt 	list_add_tail(&cur_trans->list, &fs_info->trans_list);
217d1310b2eSChris Mason 	extent_io_tree_init(&cur_trans->dirty_pages,
21819ae4e81SJan Schmidt 			     fs_info->btree_inode->i_mapping);
21919ae4e81SJan Schmidt 	fs_info->generation++;
22019ae4e81SJan Schmidt 	cur_trans->transid = fs_info->generation;
22119ae4e81SJan Schmidt 	fs_info->running_transaction = cur_trans;
22249b25e05SJeff Mahoney 	cur_trans->aborted = 0;
22319ae4e81SJan Schmidt 	spin_unlock(&fs_info->trans_lock);
22415ee9bc7SJosef Bacik 
22579154b1bSChris Mason 	return 0;
22679154b1bSChris Mason }
22779154b1bSChris Mason 
228d352ac68SChris Mason /*
229d397712bSChris Mason  * this does all the record keeping required to make sure that a reference
230d397712bSChris Mason  * counted root is properly recorded in a given transaction.  This is required
231d397712bSChris Mason  * to make sure the old root from before we joined the transaction is deleted
232d397712bSChris Mason  * when the transaction commits
233d352ac68SChris Mason  */
2347585717fSChris Mason static int record_root_in_trans(struct btrfs_trans_handle *trans,
2355d4f98a2SYan Zheng 			       struct btrfs_root *root)
2366702ed49SChris Mason {
2375d4f98a2SYan Zheng 	if (root->ref_cows && root->last_trans < trans->transid) {
2386702ed49SChris Mason 		WARN_ON(root == root->fs_info->extent_root);
2395d4f98a2SYan Zheng 		WARN_ON(root->commit_root != root->node);
2405d4f98a2SYan Zheng 
2417585717fSChris Mason 		/*
2427585717fSChris Mason 		 * see below for in_trans_setup usage rules
2437585717fSChris Mason 		 * we have the reloc mutex held now, so there
2447585717fSChris Mason 		 * is only one writer in this function
2457585717fSChris Mason 		 */
2467585717fSChris Mason 		root->in_trans_setup = 1;
2477585717fSChris Mason 
2487585717fSChris Mason 		/* make sure readers find in_trans_setup before
2497585717fSChris Mason 		 * they find our root->last_trans update
2507585717fSChris Mason 		 */
2517585717fSChris Mason 		smp_wmb();
2527585717fSChris Mason 
253a4abeea4SJosef Bacik 		spin_lock(&root->fs_info->fs_roots_radix_lock);
254a4abeea4SJosef Bacik 		if (root->last_trans == trans->transid) {
255a4abeea4SJosef Bacik 			spin_unlock(&root->fs_info->fs_roots_radix_lock);
256a4abeea4SJosef Bacik 			return 0;
257a4abeea4SJosef Bacik 		}
2586702ed49SChris Mason 		radix_tree_tag_set(&root->fs_info->fs_roots_radix,
2596702ed49SChris Mason 			   (unsigned long)root->root_key.objectid,
2606702ed49SChris Mason 			   BTRFS_ROOT_TRANS_TAG);
261a4abeea4SJosef Bacik 		spin_unlock(&root->fs_info->fs_roots_radix_lock);
2627585717fSChris Mason 		root->last_trans = trans->transid;
2637585717fSChris Mason 
2647585717fSChris Mason 		/* this is pretty tricky.  We don't want to
2657585717fSChris Mason 		 * take the relocation lock in btrfs_record_root_in_trans
2667585717fSChris Mason 		 * unless we're really doing the first setup for this root in
2677585717fSChris Mason 		 * this transaction.
2687585717fSChris Mason 		 *
2697585717fSChris Mason 		 * Normally we'd use root->last_trans as a flag to decide
2707585717fSChris Mason 		 * if we want to take the expensive mutex.
2717585717fSChris Mason 		 *
2727585717fSChris Mason 		 * But, we have to set root->last_trans before we
2737585717fSChris Mason 		 * init the relocation root, otherwise, we trip over warnings
2747585717fSChris Mason 		 * in ctree.c.  The solution used here is to flag ourselves
2757585717fSChris Mason 		 * with root->in_trans_setup.  When this is 1, we're still
2767585717fSChris Mason 		 * fixing up the reloc trees and everyone must wait.
2777585717fSChris Mason 		 *
2787585717fSChris Mason 		 * When this is zero, they can trust root->last_trans and fly
2797585717fSChris Mason 		 * through btrfs_record_root_in_trans without having to take the
2807585717fSChris Mason 		 * lock.  smp_wmb() makes sure that all the writes above are
2817585717fSChris Mason 		 * done before we pop in the zero below
2827585717fSChris Mason 		 */
2835d4f98a2SYan Zheng 		btrfs_init_reloc_root(trans, root);
2847585717fSChris Mason 		smp_wmb();
2857585717fSChris Mason 		root->in_trans_setup = 0;
2866702ed49SChris Mason 	}
2875d4f98a2SYan Zheng 	return 0;
2886702ed49SChris Mason }
2895d4f98a2SYan Zheng 
2907585717fSChris Mason 
2917585717fSChris Mason int btrfs_record_root_in_trans(struct btrfs_trans_handle *trans,
2927585717fSChris Mason 			       struct btrfs_root *root)
2937585717fSChris Mason {
2947585717fSChris Mason 	if (!root->ref_cows)
2957585717fSChris Mason 		return 0;
2967585717fSChris Mason 
2977585717fSChris Mason 	/*
2987585717fSChris Mason 	 * see record_root_in_trans for comments about in_trans_setup usage
2997585717fSChris Mason 	 * and barriers
3007585717fSChris Mason 	 */
3017585717fSChris Mason 	smp_rmb();
3027585717fSChris Mason 	if (root->last_trans == trans->transid &&
3037585717fSChris Mason 	    !root->in_trans_setup)
3047585717fSChris Mason 		return 0;
3057585717fSChris Mason 
3067585717fSChris Mason 	mutex_lock(&root->fs_info->reloc_mutex);
3077585717fSChris Mason 	record_root_in_trans(trans, root);
3087585717fSChris Mason 	mutex_unlock(&root->fs_info->reloc_mutex);
3097585717fSChris Mason 
3107585717fSChris Mason 	return 0;
3117585717fSChris Mason }
3127585717fSChris Mason 
3134a9d8bdeSMiao Xie static inline int is_transaction_blocked(struct btrfs_transaction *trans)
3144a9d8bdeSMiao Xie {
3154a9d8bdeSMiao Xie 	return (trans->state >= TRANS_STATE_BLOCKED &&
316501407aaSJosef Bacik 		trans->state < TRANS_STATE_UNBLOCKED &&
317501407aaSJosef Bacik 		!trans->aborted);
3184a9d8bdeSMiao Xie }
3194a9d8bdeSMiao Xie 
320d352ac68SChris Mason /* wait for commit against the current transaction to become unblocked
321d352ac68SChris Mason  * when this is done, it is safe to start a new transaction, but the current
322d352ac68SChris Mason  * transaction might not be fully on disk.
323d352ac68SChris Mason  */
32437d1aeeeSChris Mason static void wait_current_trans(struct btrfs_root *root)
32579154b1bSChris Mason {
326f9295749SChris Mason 	struct btrfs_transaction *cur_trans;
32779154b1bSChris Mason 
328a4abeea4SJosef Bacik 	spin_lock(&root->fs_info->trans_lock);
329f9295749SChris Mason 	cur_trans = root->fs_info->running_transaction;
3304a9d8bdeSMiao Xie 	if (cur_trans && is_transaction_blocked(cur_trans)) {
33113c5a93eSJosef Bacik 		atomic_inc(&cur_trans->use_count);
332a4abeea4SJosef Bacik 		spin_unlock(&root->fs_info->trans_lock);
33372d63ed6SLi Zefan 
33472d63ed6SLi Zefan 		wait_event(root->fs_info->transaction_wait,
335501407aaSJosef Bacik 			   cur_trans->state >= TRANS_STATE_UNBLOCKED ||
336501407aaSJosef Bacik 			   cur_trans->aborted);
337724e2315SJosef Bacik 		btrfs_put_transaction(cur_trans);
338a4abeea4SJosef Bacik 	} else {
339a4abeea4SJosef Bacik 		spin_unlock(&root->fs_info->trans_lock);
340f9295749SChris Mason 	}
34137d1aeeeSChris Mason }
34237d1aeeeSChris Mason 
343a22285a6SYan, Zheng static int may_wait_transaction(struct btrfs_root *root, int type)
34437d1aeeeSChris Mason {
345a4abeea4SJosef Bacik 	if (root->fs_info->log_root_recovering)
346a4abeea4SJosef Bacik 		return 0;
347a4abeea4SJosef Bacik 
348a4abeea4SJosef Bacik 	if (type == TRANS_USERSPACE)
349a22285a6SYan, Zheng 		return 1;
350a4abeea4SJosef Bacik 
351a4abeea4SJosef Bacik 	if (type == TRANS_START &&
352a4abeea4SJosef Bacik 	    !atomic_read(&root->fs_info->open_ioctl_trans))
353a4abeea4SJosef Bacik 		return 1;
354a4abeea4SJosef Bacik 
355a22285a6SYan, Zheng 	return 0;
356a22285a6SYan, Zheng }
357a22285a6SYan, Zheng 
35820dd2cbfSMiao Xie static inline bool need_reserve_reloc_root(struct btrfs_root *root)
35920dd2cbfSMiao Xie {
36020dd2cbfSMiao Xie 	if (!root->fs_info->reloc_ctl ||
36120dd2cbfSMiao Xie 	    !root->ref_cows ||
36220dd2cbfSMiao Xie 	    root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID ||
36320dd2cbfSMiao Xie 	    root->reloc_root)
36420dd2cbfSMiao Xie 		return false;
36520dd2cbfSMiao Xie 
36620dd2cbfSMiao Xie 	return true;
36720dd2cbfSMiao Xie }
36820dd2cbfSMiao Xie 
36908e007d2SMiao Xie static struct btrfs_trans_handle *
3700860adfdSMiao Xie start_transaction(struct btrfs_root *root, u64 num_items, unsigned int type,
37108e007d2SMiao Xie 		  enum btrfs_reserve_flush_enum flush)
372a22285a6SYan, Zheng {
373a22285a6SYan, Zheng 	struct btrfs_trans_handle *h;
374a22285a6SYan, Zheng 	struct btrfs_transaction *cur_trans;
375b5009945SJosef Bacik 	u64 num_bytes = 0;
376c5567237SArne Jansen 	u64 qgroup_reserved = 0;
37720dd2cbfSMiao Xie 	bool reloc_reserved = false;
37820dd2cbfSMiao Xie 	int ret;
379acce952bSliubo 
38087533c47SMiao Xie 	if (test_bit(BTRFS_FS_STATE_ERROR, &root->fs_info->fs_state))
381acce952bSliubo 		return ERR_PTR(-EROFS);
3822a1eb461SJosef Bacik 
3832a1eb461SJosef Bacik 	if (current->journal_info) {
3840860adfdSMiao Xie 		WARN_ON(type & TRANS_EXTWRITERS);
3852a1eb461SJosef Bacik 		h = current->journal_info;
3862a1eb461SJosef Bacik 		h->use_count++;
387b7d5b0a8SMiao Xie 		WARN_ON(h->use_count > 2);
3882a1eb461SJosef Bacik 		h->orig_rsv = h->block_rsv;
3892a1eb461SJosef Bacik 		h->block_rsv = NULL;
3902a1eb461SJosef Bacik 		goto got_it;
3912a1eb461SJosef Bacik 	}
392b5009945SJosef Bacik 
393b5009945SJosef Bacik 	/*
394b5009945SJosef Bacik 	 * Do the reservation before we join the transaction so we can do all
395b5009945SJosef Bacik 	 * the appropriate flushing if need be.
396b5009945SJosef Bacik 	 */
397b5009945SJosef Bacik 	if (num_items > 0 && root != root->fs_info->chunk_root) {
398c5567237SArne Jansen 		if (root->fs_info->quota_enabled &&
399c5567237SArne Jansen 		    is_fstree(root->root_key.objectid)) {
400c5567237SArne Jansen 			qgroup_reserved = num_items * root->leafsize;
401c5567237SArne Jansen 			ret = btrfs_qgroup_reserve(root, qgroup_reserved);
402c5567237SArne Jansen 			if (ret)
403c5567237SArne Jansen 				return ERR_PTR(ret);
404c5567237SArne Jansen 		}
405c5567237SArne Jansen 
406b5009945SJosef Bacik 		num_bytes = btrfs_calc_trans_metadata_size(root, num_items);
40720dd2cbfSMiao Xie 		/*
40820dd2cbfSMiao Xie 		 * Do the reservation for the relocation root creation
40920dd2cbfSMiao Xie 		 */
41020dd2cbfSMiao Xie 		if (unlikely(need_reserve_reloc_root(root))) {
41120dd2cbfSMiao Xie 			num_bytes += root->nodesize;
41220dd2cbfSMiao Xie 			reloc_reserved = true;
41320dd2cbfSMiao Xie 		}
41420dd2cbfSMiao Xie 
4154a92b1b8SJosef Bacik 		ret = btrfs_block_rsv_add(root,
416b5009945SJosef Bacik 					  &root->fs_info->trans_block_rsv,
41708e007d2SMiao Xie 					  num_bytes, flush);
418b5009945SJosef Bacik 		if (ret)
419843fcf35SMiao Xie 			goto reserve_fail;
420b5009945SJosef Bacik 	}
421a22285a6SYan, Zheng again:
422a22285a6SYan, Zheng 	h = kmem_cache_alloc(btrfs_trans_handle_cachep, GFP_NOFS);
423843fcf35SMiao Xie 	if (!h) {
424843fcf35SMiao Xie 		ret = -ENOMEM;
425843fcf35SMiao Xie 		goto alloc_fail;
426843fcf35SMiao Xie 	}
427a22285a6SYan, Zheng 
42898114659SJosef Bacik 	/*
42998114659SJosef Bacik 	 * If we are JOIN_NOLOCK we're already committing a transaction and
43098114659SJosef Bacik 	 * waiting on this guy, so we don't need to do the sb_start_intwrite
43198114659SJosef Bacik 	 * because we're already holding a ref.  We need this because we could
43298114659SJosef Bacik 	 * have raced in and did an fsync() on a file which can kick a commit
43398114659SJosef Bacik 	 * and then we deadlock with somebody doing a freeze.
434354aa0fbSMiao Xie 	 *
435354aa0fbSMiao Xie 	 * If we are ATTACH, it means we just want to catch the current
436354aa0fbSMiao Xie 	 * transaction and commit it, so we needn't do sb_start_intwrite().
43798114659SJosef Bacik 	 */
4380860adfdSMiao Xie 	if (type & __TRANS_FREEZABLE)
439b2b5ef5cSJan Kara 		sb_start_intwrite(root->fs_info->sb);
440b2b5ef5cSJan Kara 
441a22285a6SYan, Zheng 	if (may_wait_transaction(root, type))
44237d1aeeeSChris Mason 		wait_current_trans(root);
443a22285a6SYan, Zheng 
444a4abeea4SJosef Bacik 	do {
445354aa0fbSMiao Xie 		ret = join_transaction(root, type);
446178260b2SMiao Xie 		if (ret == -EBUSY) {
447a4abeea4SJosef Bacik 			wait_current_trans(root);
448178260b2SMiao Xie 			if (unlikely(type == TRANS_ATTACH))
449178260b2SMiao Xie 				ret = -ENOENT;
450178260b2SMiao Xie 		}
451a4abeea4SJosef Bacik 	} while (ret == -EBUSY);
452a4abeea4SJosef Bacik 
453db5b493aSTsutomu Itoh 	if (ret < 0) {
454354aa0fbSMiao Xie 		/* We must get the transaction if we are JOIN_NOLOCK. */
455354aa0fbSMiao Xie 		BUG_ON(type == TRANS_JOIN_NOLOCK);
456843fcf35SMiao Xie 		goto join_fail;
457db5b493aSTsutomu Itoh 	}
4580f7d52f4SChris Mason 
459a22285a6SYan, Zheng 	cur_trans = root->fs_info->running_transaction;
460a22285a6SYan, Zheng 
461a22285a6SYan, Zheng 	h->transid = cur_trans->transid;
462a22285a6SYan, Zheng 	h->transaction = cur_trans;
46379154b1bSChris Mason 	h->blocks_used = 0;
464a22285a6SYan, Zheng 	h->bytes_reserved = 0;
465d13603efSArne Jansen 	h->root = root;
46656bec294SChris Mason 	h->delayed_ref_updates = 0;
4672a1eb461SJosef Bacik 	h->use_count = 1;
4680e721106SJosef Bacik 	h->adding_csums = 0;
469f0486c68SYan, Zheng 	h->block_rsv = NULL;
4702a1eb461SJosef Bacik 	h->orig_rsv = NULL;
47149b25e05SJeff Mahoney 	h->aborted = 0;
4724b824906SMiao Xie 	h->qgroup_reserved = 0;
473bed92eaeSArne Jansen 	h->delayed_ref_elem.seq = 0;
474a698d075SMiao Xie 	h->type = type;
475c6b305a8SJosef Bacik 	h->allocating_chunk = false;
47620dd2cbfSMiao Xie 	h->reloc_reserved = false;
477bed92eaeSArne Jansen 	INIT_LIST_HEAD(&h->qgroup_ref_list);
478ea658badSJosef Bacik 	INIT_LIST_HEAD(&h->new_bgs);
479b7ec40d7SChris Mason 
480a22285a6SYan, Zheng 	smp_mb();
4814a9d8bdeSMiao Xie 	if (cur_trans->state >= TRANS_STATE_BLOCKED &&
4824a9d8bdeSMiao Xie 	    may_wait_transaction(root, type)) {
483a22285a6SYan, Zheng 		btrfs_commit_transaction(h, root);
484a22285a6SYan, Zheng 		goto again;
485a22285a6SYan, Zheng 	}
4869ed74f2dSJosef Bacik 
487b5009945SJosef Bacik 	if (num_bytes) {
4888c2a3ca2SJosef Bacik 		trace_btrfs_space_reservation(root->fs_info, "transaction",
4892bcc0328SLiu Bo 					      h->transid, num_bytes, 1);
490b5009945SJosef Bacik 		h->block_rsv = &root->fs_info->trans_block_rsv;
491b5009945SJosef Bacik 		h->bytes_reserved = num_bytes;
49220dd2cbfSMiao Xie 		h->reloc_reserved = reloc_reserved;
493a22285a6SYan, Zheng 	}
4944b824906SMiao Xie 	h->qgroup_reserved = qgroup_reserved;
495a22285a6SYan, Zheng 
4962a1eb461SJosef Bacik got_it:
497a4abeea4SJosef Bacik 	btrfs_record_root_in_trans(h, root);
498a22285a6SYan, Zheng 
499a22285a6SYan, Zheng 	if (!current->journal_info && type != TRANS_USERSPACE)
500a22285a6SYan, Zheng 		current->journal_info = h;
50179154b1bSChris Mason 	return h;
502843fcf35SMiao Xie 
503843fcf35SMiao Xie join_fail:
5040860adfdSMiao Xie 	if (type & __TRANS_FREEZABLE)
505843fcf35SMiao Xie 		sb_end_intwrite(root->fs_info->sb);
506843fcf35SMiao Xie 	kmem_cache_free(btrfs_trans_handle_cachep, h);
507843fcf35SMiao Xie alloc_fail:
508843fcf35SMiao Xie 	if (num_bytes)
509843fcf35SMiao Xie 		btrfs_block_rsv_release(root, &root->fs_info->trans_block_rsv,
510843fcf35SMiao Xie 					num_bytes);
511843fcf35SMiao Xie reserve_fail:
512843fcf35SMiao Xie 	if (qgroup_reserved)
513843fcf35SMiao Xie 		btrfs_qgroup_free(root, qgroup_reserved);
514843fcf35SMiao Xie 	return ERR_PTR(ret);
51579154b1bSChris Mason }
51679154b1bSChris Mason 
517f9295749SChris Mason struct btrfs_trans_handle *btrfs_start_transaction(struct btrfs_root *root,
518a22285a6SYan, Zheng 						   int num_items)
519f9295749SChris Mason {
52008e007d2SMiao Xie 	return start_transaction(root, num_items, TRANS_START,
52108e007d2SMiao Xie 				 BTRFS_RESERVE_FLUSH_ALL);
522f9295749SChris Mason }
5238407aa46SMiao Xie 
52408e007d2SMiao Xie struct btrfs_trans_handle *btrfs_start_transaction_lflush(
5258407aa46SMiao Xie 					struct btrfs_root *root, int num_items)
5268407aa46SMiao Xie {
52708e007d2SMiao Xie 	return start_transaction(root, num_items, TRANS_START,
52808e007d2SMiao Xie 				 BTRFS_RESERVE_FLUSH_LIMIT);
5298407aa46SMiao Xie }
5308407aa46SMiao Xie 
5317a7eaa40SJosef Bacik struct btrfs_trans_handle *btrfs_join_transaction(struct btrfs_root *root)
532f9295749SChris Mason {
5338407aa46SMiao Xie 	return start_transaction(root, 0, TRANS_JOIN, 0);
534f9295749SChris Mason }
535f9295749SChris Mason 
5367a7eaa40SJosef Bacik struct btrfs_trans_handle *btrfs_join_transaction_nolock(struct btrfs_root *root)
5370af3d00bSJosef Bacik {
5388407aa46SMiao Xie 	return start_transaction(root, 0, TRANS_JOIN_NOLOCK, 0);
5390af3d00bSJosef Bacik }
5400af3d00bSJosef Bacik 
5417a7eaa40SJosef Bacik struct btrfs_trans_handle *btrfs_start_ioctl_transaction(struct btrfs_root *root)
5429ca9ee09SSage Weil {
5438407aa46SMiao Xie 	return start_transaction(root, 0, TRANS_USERSPACE, 0);
5449ca9ee09SSage Weil }
5459ca9ee09SSage Weil 
546d4edf39bSMiao Xie /*
547d4edf39bSMiao Xie  * btrfs_attach_transaction() - catch the running transaction
548d4edf39bSMiao Xie  *
549d4edf39bSMiao Xie  * It is used when we want to commit the current the transaction, but
550d4edf39bSMiao Xie  * don't want to start a new one.
551d4edf39bSMiao Xie  *
552d4edf39bSMiao Xie  * Note: If this function return -ENOENT, it just means there is no
553d4edf39bSMiao Xie  * running transaction. But it is possible that the inactive transaction
554d4edf39bSMiao Xie  * is still in the memory, not fully on disk. If you hope there is no
555d4edf39bSMiao Xie  * inactive transaction in the fs when -ENOENT is returned, you should
556d4edf39bSMiao Xie  * invoke
557d4edf39bSMiao Xie  *     btrfs_attach_transaction_barrier()
558d4edf39bSMiao Xie  */
559354aa0fbSMiao Xie struct btrfs_trans_handle *btrfs_attach_transaction(struct btrfs_root *root)
56060376ce4SJosef Bacik {
561354aa0fbSMiao Xie 	return start_transaction(root, 0, TRANS_ATTACH, 0);
56260376ce4SJosef Bacik }
56360376ce4SJosef Bacik 
564d4edf39bSMiao Xie /*
56590b6d283SWang Sheng-Hui  * btrfs_attach_transaction_barrier() - catch the running transaction
566d4edf39bSMiao Xie  *
567d4edf39bSMiao Xie  * It is similar to the above function, the differentia is this one
568d4edf39bSMiao Xie  * will wait for all the inactive transactions until they fully
569d4edf39bSMiao Xie  * complete.
570d4edf39bSMiao Xie  */
571d4edf39bSMiao Xie struct btrfs_trans_handle *
572d4edf39bSMiao Xie btrfs_attach_transaction_barrier(struct btrfs_root *root)
573d4edf39bSMiao Xie {
574d4edf39bSMiao Xie 	struct btrfs_trans_handle *trans;
575d4edf39bSMiao Xie 
576d4edf39bSMiao Xie 	trans = start_transaction(root, 0, TRANS_ATTACH, 0);
577d4edf39bSMiao Xie 	if (IS_ERR(trans) && PTR_ERR(trans) == -ENOENT)
578d4edf39bSMiao Xie 		btrfs_wait_for_commit(root, 0);
579d4edf39bSMiao Xie 
580d4edf39bSMiao Xie 	return trans;
581d4edf39bSMiao Xie }
582d4edf39bSMiao Xie 
583d352ac68SChris Mason /* wait for a transaction commit to be fully complete */
584b9c8300cSLi Zefan static noinline void wait_for_commit(struct btrfs_root *root,
58589ce8a63SChris Mason 				    struct btrfs_transaction *commit)
58689ce8a63SChris Mason {
5874a9d8bdeSMiao Xie 	wait_event(commit->commit_wait, commit->state == TRANS_STATE_COMPLETED);
58889ce8a63SChris Mason }
58989ce8a63SChris Mason 
59046204592SSage Weil int btrfs_wait_for_commit(struct btrfs_root *root, u64 transid)
59146204592SSage Weil {
59246204592SSage Weil 	struct btrfs_transaction *cur_trans = NULL, *t;
5938cd2807fSMiao Xie 	int ret = 0;
59446204592SSage Weil 
59546204592SSage Weil 	if (transid) {
59646204592SSage Weil 		if (transid <= root->fs_info->last_trans_committed)
597a4abeea4SJosef Bacik 			goto out;
59846204592SSage Weil 
5998cd2807fSMiao Xie 		ret = -EINVAL;
60046204592SSage Weil 		/* find specified transaction */
601a4abeea4SJosef Bacik 		spin_lock(&root->fs_info->trans_lock);
60246204592SSage Weil 		list_for_each_entry(t, &root->fs_info->trans_list, list) {
60346204592SSage Weil 			if (t->transid == transid) {
60446204592SSage Weil 				cur_trans = t;
605a4abeea4SJosef Bacik 				atomic_inc(&cur_trans->use_count);
6068cd2807fSMiao Xie 				ret = 0;
60746204592SSage Weil 				break;
60846204592SSage Weil 			}
6098cd2807fSMiao Xie 			if (t->transid > transid) {
6108cd2807fSMiao Xie 				ret = 0;
61146204592SSage Weil 				break;
61246204592SSage Weil 			}
6138cd2807fSMiao Xie 		}
614a4abeea4SJosef Bacik 		spin_unlock(&root->fs_info->trans_lock);
6158cd2807fSMiao Xie 		/* The specified transaction doesn't exist */
61646204592SSage Weil 		if (!cur_trans)
6178cd2807fSMiao Xie 			goto out;
61846204592SSage Weil 	} else {
61946204592SSage Weil 		/* find newest transaction that is committing | committed */
620a4abeea4SJosef Bacik 		spin_lock(&root->fs_info->trans_lock);
62146204592SSage Weil 		list_for_each_entry_reverse(t, &root->fs_info->trans_list,
62246204592SSage Weil 					    list) {
6234a9d8bdeSMiao Xie 			if (t->state >= TRANS_STATE_COMMIT_START) {
6244a9d8bdeSMiao Xie 				if (t->state == TRANS_STATE_COMPLETED)
6253473f3c0SJosef Bacik 					break;
62646204592SSage Weil 				cur_trans = t;
627a4abeea4SJosef Bacik 				atomic_inc(&cur_trans->use_count);
62846204592SSage Weil 				break;
62946204592SSage Weil 			}
63046204592SSage Weil 		}
631a4abeea4SJosef Bacik 		spin_unlock(&root->fs_info->trans_lock);
63246204592SSage Weil 		if (!cur_trans)
633a4abeea4SJosef Bacik 			goto out;  /* nothing committing|committed */
63446204592SSage Weil 	}
63546204592SSage Weil 
63646204592SSage Weil 	wait_for_commit(root, cur_trans);
637724e2315SJosef Bacik 	btrfs_put_transaction(cur_trans);
638a4abeea4SJosef Bacik out:
63946204592SSage Weil 	return ret;
64046204592SSage Weil }
64146204592SSage Weil 
64237d1aeeeSChris Mason void btrfs_throttle(struct btrfs_root *root)
64337d1aeeeSChris Mason {
644a4abeea4SJosef Bacik 	if (!atomic_read(&root->fs_info->open_ioctl_trans))
64537d1aeeeSChris Mason 		wait_current_trans(root);
64637d1aeeeSChris Mason }
64737d1aeeeSChris Mason 
6488929ecfaSYan, Zheng static int should_end_transaction(struct btrfs_trans_handle *trans,
6498929ecfaSYan, Zheng 				  struct btrfs_root *root)
6508929ecfaSYan, Zheng {
6511be41b78SJosef Bacik 	if (root->fs_info->global_block_rsv.space_info->full &&
6521be41b78SJosef Bacik 	    btrfs_should_throttle_delayed_refs(trans, root))
6531be41b78SJosef Bacik 		return 1;
65436ba022aSJosef Bacik 
6551be41b78SJosef Bacik 	return !!btrfs_block_rsv_check(root, &root->fs_info->global_block_rsv, 5);
6568929ecfaSYan, Zheng }
6578929ecfaSYan, Zheng 
6588929ecfaSYan, Zheng int btrfs_should_end_transaction(struct btrfs_trans_handle *trans,
6598929ecfaSYan, Zheng 				 struct btrfs_root *root)
6608929ecfaSYan, Zheng {
6618929ecfaSYan, Zheng 	struct btrfs_transaction *cur_trans = trans->transaction;
6628929ecfaSYan, Zheng 	int updates;
66349b25e05SJeff Mahoney 	int err;
6648929ecfaSYan, Zheng 
665a4abeea4SJosef Bacik 	smp_mb();
6664a9d8bdeSMiao Xie 	if (cur_trans->state >= TRANS_STATE_BLOCKED ||
6674a9d8bdeSMiao Xie 	    cur_trans->delayed_refs.flushing)
6688929ecfaSYan, Zheng 		return 1;
6698929ecfaSYan, Zheng 
6708929ecfaSYan, Zheng 	updates = trans->delayed_ref_updates;
6718929ecfaSYan, Zheng 	trans->delayed_ref_updates = 0;
67249b25e05SJeff Mahoney 	if (updates) {
67349b25e05SJeff Mahoney 		err = btrfs_run_delayed_refs(trans, root, updates);
67449b25e05SJeff Mahoney 		if (err) /* Error code will also eval true */
67549b25e05SJeff Mahoney 			return err;
67649b25e05SJeff Mahoney 	}
6778929ecfaSYan, Zheng 
6788929ecfaSYan, Zheng 	return should_end_transaction(trans, root);
6798929ecfaSYan, Zheng }
6808929ecfaSYan, Zheng 
68189ce8a63SChris Mason static int __btrfs_end_transaction(struct btrfs_trans_handle *trans,
682a698d075SMiao Xie 			  struct btrfs_root *root, int throttle)
68379154b1bSChris Mason {
6848929ecfaSYan, Zheng 	struct btrfs_transaction *cur_trans = trans->transaction;
685ab78c84dSChris Mason 	struct btrfs_fs_info *info = root->fs_info;
6861be41b78SJosef Bacik 	unsigned long cur = trans->delayed_ref_updates;
687a698d075SMiao Xie 	int lock = (trans->type != TRANS_JOIN_NOLOCK);
6884edc2ca3SDave Jones 	int err = 0;
689d6e4a428SChris Mason 
6902a1eb461SJosef Bacik 	if (--trans->use_count) {
6912a1eb461SJosef Bacik 		trans->block_rsv = trans->orig_rsv;
6922a1eb461SJosef Bacik 		return 0;
6932a1eb461SJosef Bacik 	}
6942a1eb461SJosef Bacik 
695edf39272SJan Schmidt 	/*
696edf39272SJan Schmidt 	 * do the qgroup accounting as early as possible
697edf39272SJan Schmidt 	 */
698edf39272SJan Schmidt 	err = btrfs_delayed_refs_qgroup_accounting(trans, info);
699edf39272SJan Schmidt 
700b24e03dbSJosef Bacik 	btrfs_trans_release_metadata(trans, root);
7014c13d758SJosef Bacik 	trans->block_rsv = NULL;
702c5567237SArne Jansen 
703c5567237SArne Jansen 	if (trans->qgroup_reserved) {
7047c2ec3f0SLiu Bo 		/*
7057c2ec3f0SLiu Bo 		 * the same root has to be passed here between start_transaction
7067c2ec3f0SLiu Bo 		 * and end_transaction. Subvolume quota depends on this.
7077c2ec3f0SLiu Bo 		 */
7087c2ec3f0SLiu Bo 		btrfs_qgroup_free(trans->root, trans->qgroup_reserved);
709c5567237SArne Jansen 		trans->qgroup_reserved = 0;
710c5567237SArne Jansen 	}
711c5567237SArne Jansen 
712ea658badSJosef Bacik 	if (!list_empty(&trans->new_bgs))
713ea658badSJosef Bacik 		btrfs_create_pending_block_groups(trans, root);
714ea658badSJosef Bacik 
715c3e69d58SChris Mason 	trans->delayed_ref_updates = 0;
7161be41b78SJosef Bacik 	if (btrfs_should_throttle_delayed_refs(trans, root)) {
7171be41b78SJosef Bacik 		cur = max_t(unsigned long, cur, 1);
718c3e69d58SChris Mason 		trans->delayed_ref_updates = 0;
719c3e69d58SChris Mason 		btrfs_run_delayed_refs(trans, root, cur);
72056bec294SChris Mason 	}
721bb721703SChris Mason 
7220e721106SJosef Bacik 	btrfs_trans_release_metadata(trans, root);
7230e721106SJosef Bacik 	trans->block_rsv = NULL;
72456bec294SChris Mason 
725ea658badSJosef Bacik 	if (!list_empty(&trans->new_bgs))
726ea658badSJosef Bacik 		btrfs_create_pending_block_groups(trans, root);
727ea658badSJosef Bacik 
728a4abeea4SJosef Bacik 	if (lock && !atomic_read(&root->fs_info->open_ioctl_trans) &&
7294a9d8bdeSMiao Xie 	    should_end_transaction(trans, root) &&
7304a9d8bdeSMiao Xie 	    ACCESS_ONCE(cur_trans->state) == TRANS_STATE_RUNNING) {
7314a9d8bdeSMiao Xie 		spin_lock(&info->trans_lock);
7324a9d8bdeSMiao Xie 		if (cur_trans->state == TRANS_STATE_RUNNING)
7334a9d8bdeSMiao Xie 			cur_trans->state = TRANS_STATE_BLOCKED;
7344a9d8bdeSMiao Xie 		spin_unlock(&info->trans_lock);
735a4abeea4SJosef Bacik 	}
7368929ecfaSYan, Zheng 
7374a9d8bdeSMiao Xie 	if (lock && ACCESS_ONCE(cur_trans->state) == TRANS_STATE_BLOCKED) {
73881317fdeSJosef Bacik 		if (throttle) {
73981317fdeSJosef Bacik 			/*
74081317fdeSJosef Bacik 			 * We may race with somebody else here so end up having
74181317fdeSJosef Bacik 			 * to call end_transaction on ourselves again, so inc
74281317fdeSJosef Bacik 			 * our use_count.
74381317fdeSJosef Bacik 			 */
74481317fdeSJosef Bacik 			trans->use_count++;
7458929ecfaSYan, Zheng 			return btrfs_commit_transaction(trans, root);
74681317fdeSJosef Bacik 		} else {
7478929ecfaSYan, Zheng 			wake_up_process(info->transaction_kthread);
7488929ecfaSYan, Zheng 		}
74981317fdeSJosef Bacik 	}
7508929ecfaSYan, Zheng 
7510860adfdSMiao Xie 	if (trans->type & __TRANS_FREEZABLE)
7526df7881aSJosef Bacik 		sb_end_intwrite(root->fs_info->sb);
7536df7881aSJosef Bacik 
7548929ecfaSYan, Zheng 	WARN_ON(cur_trans != info->running_transaction);
75513c5a93eSJosef Bacik 	WARN_ON(atomic_read(&cur_trans->num_writers) < 1);
75613c5a93eSJosef Bacik 	atomic_dec(&cur_trans->num_writers);
7570860adfdSMiao Xie 	extwriter_counter_dec(cur_trans, trans->type);
75889ce8a63SChris Mason 
75999d16cbcSSage Weil 	smp_mb();
76079154b1bSChris Mason 	if (waitqueue_active(&cur_trans->writer_wait))
76179154b1bSChris Mason 		wake_up(&cur_trans->writer_wait);
762724e2315SJosef Bacik 	btrfs_put_transaction(cur_trans);
7639ed74f2dSJosef Bacik 
7649ed74f2dSJosef Bacik 	if (current->journal_info == trans)
7659ed74f2dSJosef Bacik 		current->journal_info = NULL;
766ab78c84dSChris Mason 
76724bbcf04SYan, Zheng 	if (throttle)
76824bbcf04SYan, Zheng 		btrfs_run_delayed_iputs(root);
76924bbcf04SYan, Zheng 
77049b25e05SJeff Mahoney 	if (trans->aborted ||
7714e121c06SJosef Bacik 	    test_bit(BTRFS_FS_STATE_ERROR, &root->fs_info->fs_state)) {
7724e121c06SJosef Bacik 		wake_up_process(info->transaction_kthread);
7734edc2ca3SDave Jones 		err = -EIO;
7744e121c06SJosef Bacik 	}
775edf39272SJan Schmidt 	assert_qgroups_uptodate(trans);
77649b25e05SJeff Mahoney 
7774edc2ca3SDave Jones 	kmem_cache_free(btrfs_trans_handle_cachep, trans);
7784edc2ca3SDave Jones 	return err;
77979154b1bSChris Mason }
78079154b1bSChris Mason 
78189ce8a63SChris Mason int btrfs_end_transaction(struct btrfs_trans_handle *trans,
78289ce8a63SChris Mason 			  struct btrfs_root *root)
78389ce8a63SChris Mason {
78498ad43beSWang Shilong 	return __btrfs_end_transaction(trans, root, 0);
78589ce8a63SChris Mason }
78689ce8a63SChris Mason 
78789ce8a63SChris Mason int btrfs_end_transaction_throttle(struct btrfs_trans_handle *trans,
78889ce8a63SChris Mason 				   struct btrfs_root *root)
78989ce8a63SChris Mason {
79098ad43beSWang Shilong 	return __btrfs_end_transaction(trans, root, 1);
79116cdcec7SMiao Xie }
79216cdcec7SMiao Xie 
793d352ac68SChris Mason /*
794d352ac68SChris Mason  * when btree blocks are allocated, they have some corresponding bits set for
795d352ac68SChris Mason  * them in one of two extent_io trees.  This is used to make sure all of
796690587d1SChris Mason  * those extents are sent to disk but does not wait on them
797d352ac68SChris Mason  */
798690587d1SChris Mason int btrfs_write_marked_extents(struct btrfs_root *root,
7998cef4e16SYan, Zheng 			       struct extent_io_tree *dirty_pages, int mark)
80079154b1bSChris Mason {
801777e6bd7SChris Mason 	int err = 0;
8027c4452b9SChris Mason 	int werr = 0;
8031728366eSJosef Bacik 	struct address_space *mapping = root->fs_info->btree_inode->i_mapping;
804e6138876SJosef Bacik 	struct extent_state *cached_state = NULL;
805777e6bd7SChris Mason 	u64 start = 0;
8065f39d397SChris Mason 	u64 end;
8077c4452b9SChris Mason 
8081728366eSJosef Bacik 	while (!find_first_extent_bit(dirty_pages, start, &start, &end,
809e6138876SJosef Bacik 				      mark, &cached_state)) {
810e6138876SJosef Bacik 		convert_extent_bit(dirty_pages, start, end, EXTENT_NEED_WAIT,
811e6138876SJosef Bacik 				   mark, &cached_state, GFP_NOFS);
812e6138876SJosef Bacik 		cached_state = NULL;
8131728366eSJosef Bacik 		err = filemap_fdatawrite_range(mapping, start, end);
8147c4452b9SChris Mason 		if (err)
8157c4452b9SChris Mason 			werr = err;
8161728366eSJosef Bacik 		cond_resched();
8171728366eSJosef Bacik 		start = end + 1;
8187c4452b9SChris Mason 	}
819690587d1SChris Mason 	if (err)
820690587d1SChris Mason 		werr = err;
821690587d1SChris Mason 	return werr;
822690587d1SChris Mason }
823690587d1SChris Mason 
824690587d1SChris Mason /*
825690587d1SChris Mason  * when btree blocks are allocated, they have some corresponding bits set for
826690587d1SChris Mason  * them in one of two extent_io trees.  This is used to make sure all of
827690587d1SChris Mason  * those extents are on disk for transaction or log commit.  We wait
828690587d1SChris Mason  * on all the pages and clear them from the dirty pages state tree
829690587d1SChris Mason  */
830690587d1SChris Mason int btrfs_wait_marked_extents(struct btrfs_root *root,
8318cef4e16SYan, Zheng 			      struct extent_io_tree *dirty_pages, int mark)
832690587d1SChris Mason {
833690587d1SChris Mason 	int err = 0;
834690587d1SChris Mason 	int werr = 0;
8351728366eSJosef Bacik 	struct address_space *mapping = root->fs_info->btree_inode->i_mapping;
836e6138876SJosef Bacik 	struct extent_state *cached_state = NULL;
837690587d1SChris Mason 	u64 start = 0;
838690587d1SChris Mason 	u64 end;
839690587d1SChris Mason 
8401728366eSJosef Bacik 	while (!find_first_extent_bit(dirty_pages, start, &start, &end,
841e6138876SJosef Bacik 				      EXTENT_NEED_WAIT, &cached_state)) {
842e6138876SJosef Bacik 		clear_extent_bit(dirty_pages, start, end, EXTENT_NEED_WAIT,
843e6138876SJosef Bacik 				 0, 0, &cached_state, GFP_NOFS);
8441728366eSJosef Bacik 		err = filemap_fdatawait_range(mapping, start, end);
845777e6bd7SChris Mason 		if (err)
846777e6bd7SChris Mason 			werr = err;
847777e6bd7SChris Mason 		cond_resched();
8481728366eSJosef Bacik 		start = end + 1;
849777e6bd7SChris Mason 	}
8507c4452b9SChris Mason 	if (err)
8517c4452b9SChris Mason 		werr = err;
8527c4452b9SChris Mason 	return werr;
85379154b1bSChris Mason }
85479154b1bSChris Mason 
855690587d1SChris Mason /*
856690587d1SChris Mason  * when btree blocks are allocated, they have some corresponding bits set for
857690587d1SChris Mason  * them in one of two extent_io trees.  This is used to make sure all of
858690587d1SChris Mason  * those extents are on disk for transaction or log commit
859690587d1SChris Mason  */
860171170c1SSergei Trofimovich static int btrfs_write_and_wait_marked_extents(struct btrfs_root *root,
8618cef4e16SYan, Zheng 				struct extent_io_tree *dirty_pages, int mark)
862690587d1SChris Mason {
863690587d1SChris Mason 	int ret;
864690587d1SChris Mason 	int ret2;
865c6adc9ccSMiao Xie 	struct blk_plug plug;
866690587d1SChris Mason 
867c6adc9ccSMiao Xie 	blk_start_plug(&plug);
8688cef4e16SYan, Zheng 	ret = btrfs_write_marked_extents(root, dirty_pages, mark);
869c6adc9ccSMiao Xie 	blk_finish_plug(&plug);
8708cef4e16SYan, Zheng 	ret2 = btrfs_wait_marked_extents(root, dirty_pages, mark);
871bf0da8c1SChris Mason 
872bf0da8c1SChris Mason 	if (ret)
873bf0da8c1SChris Mason 		return ret;
874bf0da8c1SChris Mason 	if (ret2)
875bf0da8c1SChris Mason 		return ret2;
876bf0da8c1SChris Mason 	return 0;
877690587d1SChris Mason }
878690587d1SChris Mason 
879d0c803c4SChris Mason int btrfs_write_and_wait_transaction(struct btrfs_trans_handle *trans,
880d0c803c4SChris Mason 				     struct btrfs_root *root)
881d0c803c4SChris Mason {
882d0c803c4SChris Mason 	if (!trans || !trans->transaction) {
883d0c803c4SChris Mason 		struct inode *btree_inode;
884d0c803c4SChris Mason 		btree_inode = root->fs_info->btree_inode;
885d0c803c4SChris Mason 		return filemap_write_and_wait(btree_inode->i_mapping);
886d0c803c4SChris Mason 	}
887d0c803c4SChris Mason 	return btrfs_write_and_wait_marked_extents(root,
8888cef4e16SYan, Zheng 					   &trans->transaction->dirty_pages,
8898cef4e16SYan, Zheng 					   EXTENT_DIRTY);
890d0c803c4SChris Mason }
891d0c803c4SChris Mason 
892d352ac68SChris Mason /*
893d352ac68SChris Mason  * this is used to update the root pointer in the tree of tree roots.
894d352ac68SChris Mason  *
895d352ac68SChris Mason  * But, in the case of the extent allocation tree, updating the root
896d352ac68SChris Mason  * pointer may allocate blocks which may change the root of the extent
897d352ac68SChris Mason  * allocation tree.
898d352ac68SChris Mason  *
899d352ac68SChris Mason  * So, this loops and repeats and makes sure the cowonly root didn't
900d352ac68SChris Mason  * change while the root pointer was being updated in the metadata.
901d352ac68SChris Mason  */
9020b86a832SChris Mason static int update_cowonly_root(struct btrfs_trans_handle *trans,
90379154b1bSChris Mason 			       struct btrfs_root *root)
90479154b1bSChris Mason {
90579154b1bSChris Mason 	int ret;
9060b86a832SChris Mason 	u64 old_root_bytenr;
90786b9f2ecSYan, Zheng 	u64 old_root_used;
9080b86a832SChris Mason 	struct btrfs_root *tree_root = root->fs_info->tree_root;
90979154b1bSChris Mason 
91086b9f2ecSYan, Zheng 	old_root_used = btrfs_root_used(&root->root_item);
9110b86a832SChris Mason 	btrfs_write_dirty_block_groups(trans, root);
91256bec294SChris Mason 
91379154b1bSChris Mason 	while (1) {
9140b86a832SChris Mason 		old_root_bytenr = btrfs_root_bytenr(&root->root_item);
91586b9f2ecSYan, Zheng 		if (old_root_bytenr == root->node->start &&
91686b9f2ecSYan, Zheng 		    old_root_used == btrfs_root_used(&root->root_item))
91779154b1bSChris Mason 			break;
91887ef2bb4SChris Mason 
9195d4f98a2SYan Zheng 		btrfs_set_root_node(&root->root_item, root->node);
92079154b1bSChris Mason 		ret = btrfs_update_root(trans, tree_root,
9210b86a832SChris Mason 					&root->root_key,
9220b86a832SChris Mason 					&root->root_item);
92349b25e05SJeff Mahoney 		if (ret)
92449b25e05SJeff Mahoney 			return ret;
92556bec294SChris Mason 
92686b9f2ecSYan, Zheng 		old_root_used = btrfs_root_used(&root->root_item);
9274a8c9a62SYan Zheng 		ret = btrfs_write_dirty_block_groups(trans, root);
92849b25e05SJeff Mahoney 		if (ret)
92949b25e05SJeff Mahoney 			return ret;
9300b86a832SChris Mason 	}
931276e680dSYan Zheng 
932276e680dSYan Zheng 	if (root != root->fs_info->extent_root)
933817d52f8SJosef Bacik 		switch_commit_root(root);
934276e680dSYan Zheng 
9350b86a832SChris Mason 	return 0;
9360b86a832SChris Mason }
9370b86a832SChris Mason 
938d352ac68SChris Mason /*
939d352ac68SChris Mason  * update all the cowonly tree roots on disk
94049b25e05SJeff Mahoney  *
94149b25e05SJeff Mahoney  * The error handling in this function may not be obvious. Any of the
94249b25e05SJeff Mahoney  * failures will cause the file system to go offline. We still need
94349b25e05SJeff Mahoney  * to clean up the delayed refs.
944d352ac68SChris Mason  */
9455d4f98a2SYan Zheng static noinline int commit_cowonly_roots(struct btrfs_trans_handle *trans,
9460b86a832SChris Mason 					 struct btrfs_root *root)
9470b86a832SChris Mason {
9480b86a832SChris Mason 	struct btrfs_fs_info *fs_info = root->fs_info;
9490b86a832SChris Mason 	struct list_head *next;
95084234f3aSYan Zheng 	struct extent_buffer *eb;
95156bec294SChris Mason 	int ret;
95284234f3aSYan Zheng 
95356bec294SChris Mason 	ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
95449b25e05SJeff Mahoney 	if (ret)
95549b25e05SJeff Mahoney 		return ret;
95687ef2bb4SChris Mason 
95784234f3aSYan Zheng 	eb = btrfs_lock_root_node(fs_info->tree_root);
95849b25e05SJeff Mahoney 	ret = btrfs_cow_block(trans, fs_info->tree_root, eb, NULL,
95949b25e05SJeff Mahoney 			      0, &eb);
96084234f3aSYan Zheng 	btrfs_tree_unlock(eb);
96184234f3aSYan Zheng 	free_extent_buffer(eb);
9620b86a832SChris Mason 
96349b25e05SJeff Mahoney 	if (ret)
96449b25e05SJeff Mahoney 		return ret;
96549b25e05SJeff Mahoney 
96656bec294SChris Mason 	ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
96749b25e05SJeff Mahoney 	if (ret)
96849b25e05SJeff Mahoney 		return ret;
96987ef2bb4SChris Mason 
970733f4fbbSStefan Behrens 	ret = btrfs_run_dev_stats(trans, root->fs_info);
971c16ce190SJosef Bacik 	if (ret)
972c16ce190SJosef Bacik 		return ret;
9738dabb742SStefan Behrens 	ret = btrfs_run_dev_replace(trans, root->fs_info);
974c16ce190SJosef Bacik 	if (ret)
975c16ce190SJosef Bacik 		return ret;
976546adb0dSJan Schmidt 	ret = btrfs_run_qgroups(trans, root->fs_info);
977c16ce190SJosef Bacik 	if (ret)
978c16ce190SJosef Bacik 		return ret;
979546adb0dSJan Schmidt 
980546adb0dSJan Schmidt 	/* run_qgroups might have added some more refs */
981546adb0dSJan Schmidt 	ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
982c16ce190SJosef Bacik 	if (ret)
983c16ce190SJosef Bacik 		return ret;
984546adb0dSJan Schmidt 
9850b86a832SChris Mason 	while (!list_empty(&fs_info->dirty_cowonly_roots)) {
9860b86a832SChris Mason 		next = fs_info->dirty_cowonly_roots.next;
9870b86a832SChris Mason 		list_del_init(next);
9880b86a832SChris Mason 		root = list_entry(next, struct btrfs_root, dirty_list);
98987ef2bb4SChris Mason 
99049b25e05SJeff Mahoney 		ret = update_cowonly_root(trans, root);
99149b25e05SJeff Mahoney 		if (ret)
99249b25e05SJeff Mahoney 			return ret;
99379154b1bSChris Mason 	}
994276e680dSYan Zheng 
995276e680dSYan Zheng 	down_write(&fs_info->extent_commit_sem);
996276e680dSYan Zheng 	switch_commit_root(fs_info->extent_root);
997276e680dSYan Zheng 	up_write(&fs_info->extent_commit_sem);
998276e680dSYan Zheng 
9998dabb742SStefan Behrens 	btrfs_after_dev_replace_commit(fs_info);
10008dabb742SStefan Behrens 
100179154b1bSChris Mason 	return 0;
100279154b1bSChris Mason }
100379154b1bSChris Mason 
1004d352ac68SChris Mason /*
1005d352ac68SChris Mason  * dead roots are old snapshots that need to be deleted.  This allocates
1006d352ac68SChris Mason  * a dirty root struct and adds it into the list of dead roots that need to
1007d352ac68SChris Mason  * be deleted
1008d352ac68SChris Mason  */
1009cfad392bSJosef Bacik void btrfs_add_dead_root(struct btrfs_root *root)
10105eda7b5eSChris Mason {
1011a4abeea4SJosef Bacik 	spin_lock(&root->fs_info->trans_lock);
1012cfad392bSJosef Bacik 	if (list_empty(&root->root_list))
10139d1a2a3aSDavid Sterba 		list_add_tail(&root->root_list, &root->fs_info->dead_roots);
1014a4abeea4SJosef Bacik 	spin_unlock(&root->fs_info->trans_lock);
10155eda7b5eSChris Mason }
10165eda7b5eSChris Mason 
1017d352ac68SChris Mason /*
10185d4f98a2SYan Zheng  * update all the cowonly tree roots on disk
1019d352ac68SChris Mason  */
10205d4f98a2SYan Zheng static noinline int commit_fs_roots(struct btrfs_trans_handle *trans,
10215d4f98a2SYan Zheng 				    struct btrfs_root *root)
10220f7d52f4SChris Mason {
10230f7d52f4SChris Mason 	struct btrfs_root *gang[8];
10245d4f98a2SYan Zheng 	struct btrfs_fs_info *fs_info = root->fs_info;
10250f7d52f4SChris Mason 	int i;
10260f7d52f4SChris Mason 	int ret;
102754aa1f4dSChris Mason 	int err = 0;
102854aa1f4dSChris Mason 
1029a4abeea4SJosef Bacik 	spin_lock(&fs_info->fs_roots_radix_lock);
10300f7d52f4SChris Mason 	while (1) {
10315d4f98a2SYan Zheng 		ret = radix_tree_gang_lookup_tag(&fs_info->fs_roots_radix,
10325d4f98a2SYan Zheng 						 (void **)gang, 0,
10330f7d52f4SChris Mason 						 ARRAY_SIZE(gang),
10340f7d52f4SChris Mason 						 BTRFS_ROOT_TRANS_TAG);
10350f7d52f4SChris Mason 		if (ret == 0)
10360f7d52f4SChris Mason 			break;
10370f7d52f4SChris Mason 		for (i = 0; i < ret; i++) {
10380f7d52f4SChris Mason 			root = gang[i];
10395d4f98a2SYan Zheng 			radix_tree_tag_clear(&fs_info->fs_roots_radix,
10402619ba1fSChris Mason 					(unsigned long)root->root_key.objectid,
10410f7d52f4SChris Mason 					BTRFS_ROOT_TRANS_TAG);
1042a4abeea4SJosef Bacik 			spin_unlock(&fs_info->fs_roots_radix_lock);
104331153d81SYan Zheng 
1044e02119d5SChris Mason 			btrfs_free_log(trans, root);
10455d4f98a2SYan Zheng 			btrfs_update_reloc_root(trans, root);
1046d68fc57bSYan, Zheng 			btrfs_orphan_commit_root(trans, root);
1047e02119d5SChris Mason 
104882d5902dSLi Zefan 			btrfs_save_ino_cache(root, trans);
104982d5902dSLi Zefan 
1050f1ebcc74SLiu Bo 			/* see comments in should_cow_block() */
1051f1ebcc74SLiu Bo 			root->force_cow = 0;
1052f1ebcc74SLiu Bo 			smp_wmb();
1053f1ebcc74SLiu Bo 
1054978d910dSYan Zheng 			if (root->commit_root != root->node) {
1055581bb050SLi Zefan 				mutex_lock(&root->fs_commit_mutex);
1056817d52f8SJosef Bacik 				switch_commit_root(root);
1057581bb050SLi Zefan 				btrfs_unpin_free_ino(root);
1058581bb050SLi Zefan 				mutex_unlock(&root->fs_commit_mutex);
1059581bb050SLi Zefan 
1060978d910dSYan Zheng 				btrfs_set_root_node(&root->root_item,
1061978d910dSYan Zheng 						    root->node);
1062978d910dSYan Zheng 			}
106331153d81SYan Zheng 
10645d4f98a2SYan Zheng 			err = btrfs_update_root(trans, fs_info->tree_root,
10650f7d52f4SChris Mason 						&root->root_key,
10660f7d52f4SChris Mason 						&root->root_item);
1067a4abeea4SJosef Bacik 			spin_lock(&fs_info->fs_roots_radix_lock);
106854aa1f4dSChris Mason 			if (err)
106954aa1f4dSChris Mason 				break;
10700f7d52f4SChris Mason 		}
10719f3a7427SChris Mason 	}
1072a4abeea4SJosef Bacik 	spin_unlock(&fs_info->fs_roots_radix_lock);
107354aa1f4dSChris Mason 	return err;
10740f7d52f4SChris Mason }
10750f7d52f4SChris Mason 
1076d352ac68SChris Mason /*
1077de78b51aSEric Sandeen  * defrag a given btree.
1078de78b51aSEric Sandeen  * Every leaf in the btree is read and defragged.
1079d352ac68SChris Mason  */
1080de78b51aSEric Sandeen int btrfs_defrag_root(struct btrfs_root *root)
1081e9d0b13bSChris Mason {
1082e9d0b13bSChris Mason 	struct btrfs_fs_info *info = root->fs_info;
1083e9d0b13bSChris Mason 	struct btrfs_trans_handle *trans;
10848929ecfaSYan, Zheng 	int ret;
1085e9d0b13bSChris Mason 
10868929ecfaSYan, Zheng 	if (xchg(&root->defrag_running, 1))
1087e9d0b13bSChris Mason 		return 0;
10888929ecfaSYan, Zheng 
10896b80053dSChris Mason 	while (1) {
10908929ecfaSYan, Zheng 		trans = btrfs_start_transaction(root, 0);
10918929ecfaSYan, Zheng 		if (IS_ERR(trans))
10928929ecfaSYan, Zheng 			return PTR_ERR(trans);
10938929ecfaSYan, Zheng 
1094de78b51aSEric Sandeen 		ret = btrfs_defrag_leaves(trans, root);
10958929ecfaSYan, Zheng 
1096e9d0b13bSChris Mason 		btrfs_end_transaction(trans, root);
1097b53d3f5dSLiu Bo 		btrfs_btree_balance_dirty(info->tree_root);
1098e9d0b13bSChris Mason 		cond_resched();
1099e9d0b13bSChris Mason 
11007841cb28SDavid Sterba 		if (btrfs_fs_closing(root->fs_info) || ret != -EAGAIN)
1101e9d0b13bSChris Mason 			break;
1102210549ebSDavid Sterba 
1103210549ebSDavid Sterba 		if (btrfs_defrag_cancelled(root->fs_info)) {
1104efe120a0SFrank Holton 			pr_debug("BTRFS: defrag_root cancelled\n");
1105210549ebSDavid Sterba 			ret = -EAGAIN;
1106210549ebSDavid Sterba 			break;
1107210549ebSDavid Sterba 		}
1108e9d0b13bSChris Mason 	}
1109e9d0b13bSChris Mason 	root->defrag_running = 0;
11108929ecfaSYan, Zheng 	return ret;
1111e9d0b13bSChris Mason }
1112e9d0b13bSChris Mason 
1113d352ac68SChris Mason /*
1114d352ac68SChris Mason  * new snapshots need to be created at a very specific time in the
1115aec8030aSMiao Xie  * transaction commit.  This does the actual creation.
1116aec8030aSMiao Xie  *
1117aec8030aSMiao Xie  * Note:
1118aec8030aSMiao Xie  * If the error which may affect the commitment of the current transaction
1119aec8030aSMiao Xie  * happens, we should return the error number. If the error which just affect
1120aec8030aSMiao Xie  * the creation of the pending snapshots, just return 0.
1121d352ac68SChris Mason  */
112280b6794dSChris Mason static noinline int create_pending_snapshot(struct btrfs_trans_handle *trans,
11233063d29fSChris Mason 				   struct btrfs_fs_info *fs_info,
11243063d29fSChris Mason 				   struct btrfs_pending_snapshot *pending)
11253063d29fSChris Mason {
11263063d29fSChris Mason 	struct btrfs_key key;
112780b6794dSChris Mason 	struct btrfs_root_item *new_root_item;
11283063d29fSChris Mason 	struct btrfs_root *tree_root = fs_info->tree_root;
11293063d29fSChris Mason 	struct btrfs_root *root = pending->root;
11306bdb72deSSage Weil 	struct btrfs_root *parent_root;
113198c9942aSLiu Bo 	struct btrfs_block_rsv *rsv;
11326bdb72deSSage Weil 	struct inode *parent_inode;
113342874b3dSMiao Xie 	struct btrfs_path *path;
113442874b3dSMiao Xie 	struct btrfs_dir_item *dir_item;
1135a22285a6SYan, Zheng 	struct dentry *dentry;
11363063d29fSChris Mason 	struct extent_buffer *tmp;
1137925baeddSChris Mason 	struct extent_buffer *old;
11388ea05e3aSAlexander Block 	struct timespec cur_time = CURRENT_TIME;
1139aec8030aSMiao Xie 	int ret = 0;
1140d68fc57bSYan, Zheng 	u64 to_reserve = 0;
11416bdb72deSSage Weil 	u64 index = 0;
1142a22285a6SYan, Zheng 	u64 objectid;
1143b83cc969SLi Zefan 	u64 root_flags;
11448ea05e3aSAlexander Block 	uuid_le new_uuid;
11453063d29fSChris Mason 
114642874b3dSMiao Xie 	path = btrfs_alloc_path();
114742874b3dSMiao Xie 	if (!path) {
1148aec8030aSMiao Xie 		pending->error = -ENOMEM;
1149aec8030aSMiao Xie 		return 0;
115042874b3dSMiao Xie 	}
115142874b3dSMiao Xie 
115280b6794dSChris Mason 	new_root_item = kmalloc(sizeof(*new_root_item), GFP_NOFS);
115380b6794dSChris Mason 	if (!new_root_item) {
1154aec8030aSMiao Xie 		pending->error = -ENOMEM;
11556fa9700eSMiao Xie 		goto root_item_alloc_fail;
115680b6794dSChris Mason 	}
1157a22285a6SYan, Zheng 
1158aec8030aSMiao Xie 	pending->error = btrfs_find_free_objectid(tree_root, &objectid);
1159aec8030aSMiao Xie 	if (pending->error)
11606fa9700eSMiao Xie 		goto no_free_objectid;
11613063d29fSChris Mason 
11623fd0a558SYan, Zheng 	btrfs_reloc_pre_snapshot(trans, pending, &to_reserve);
1163d68fc57bSYan, Zheng 
1164d68fc57bSYan, Zheng 	if (to_reserve > 0) {
1165aec8030aSMiao Xie 		pending->error = btrfs_block_rsv_add(root,
1166aec8030aSMiao Xie 						     &pending->block_rsv,
116708e007d2SMiao Xie 						     to_reserve,
116808e007d2SMiao Xie 						     BTRFS_RESERVE_NO_FLUSH);
1169aec8030aSMiao Xie 		if (pending->error)
11706fa9700eSMiao Xie 			goto no_free_objectid;
1171d68fc57bSYan, Zheng 	}
1172d68fc57bSYan, Zheng 
1173aec8030aSMiao Xie 	pending->error = btrfs_qgroup_inherit(trans, fs_info,
1174aec8030aSMiao Xie 					      root->root_key.objectid,
11756f72c7e2SArne Jansen 					      objectid, pending->inherit);
1176aec8030aSMiao Xie 	if (pending->error)
11776fa9700eSMiao Xie 		goto no_free_objectid;
11786f72c7e2SArne Jansen 
11793063d29fSChris Mason 	key.objectid = objectid;
1180a22285a6SYan, Zheng 	key.offset = (u64)-1;
1181a22285a6SYan, Zheng 	key.type = BTRFS_ROOT_ITEM_KEY;
11823063d29fSChris Mason 
11836fa9700eSMiao Xie 	rsv = trans->block_rsv;
1184a22285a6SYan, Zheng 	trans->block_rsv = &pending->block_rsv;
11852382c5ccSLiu Bo 	trans->bytes_reserved = trans->block_rsv->reserved;
11866bdb72deSSage Weil 
1187a22285a6SYan, Zheng 	dentry = pending->dentry;
1188e9662f70SMiao Xie 	parent_inode = pending->dir;
1189a22285a6SYan, Zheng 	parent_root = BTRFS_I(parent_inode)->root;
11907585717fSChris Mason 	record_root_in_trans(trans, parent_root);
1191a22285a6SYan, Zheng 
11926bdb72deSSage Weil 	/*
11936bdb72deSSage Weil 	 * insert the directory item
11946bdb72deSSage Weil 	 */
11956bdb72deSSage Weil 	ret = btrfs_set_inode_index(parent_inode, &index);
119649b25e05SJeff Mahoney 	BUG_ON(ret); /* -ENOMEM */
119742874b3dSMiao Xie 
119842874b3dSMiao Xie 	/* check if there is a file/dir which has the same name. */
119942874b3dSMiao Xie 	dir_item = btrfs_lookup_dir_item(NULL, parent_root, path,
120042874b3dSMiao Xie 					 btrfs_ino(parent_inode),
120142874b3dSMiao Xie 					 dentry->d_name.name,
120242874b3dSMiao Xie 					 dentry->d_name.len, 0);
120342874b3dSMiao Xie 	if (dir_item != NULL && !IS_ERR(dir_item)) {
1204fe66a05aSChris Mason 		pending->error = -EEXIST;
1205aec8030aSMiao Xie 		goto dir_item_existed;
120642874b3dSMiao Xie 	} else if (IS_ERR(dir_item)) {
120742874b3dSMiao Xie 		ret = PTR_ERR(dir_item);
12088732d44fSMiao Xie 		btrfs_abort_transaction(trans, root, ret);
12098732d44fSMiao Xie 		goto fail;
121079787eaaSJeff Mahoney 	}
121142874b3dSMiao Xie 	btrfs_release_path(path);
12126bdb72deSSage Weil 
1213e999376fSChris Mason 	/*
1214e999376fSChris Mason 	 * pull in the delayed directory update
1215e999376fSChris Mason 	 * and the delayed inode item
1216e999376fSChris Mason 	 * otherwise we corrupt the FS during
1217e999376fSChris Mason 	 * snapshot
1218e999376fSChris Mason 	 */
1219e999376fSChris Mason 	ret = btrfs_run_delayed_items(trans, root);
12208732d44fSMiao Xie 	if (ret) {	/* Transaction aborted */
12218732d44fSMiao Xie 		btrfs_abort_transaction(trans, root, ret);
12228732d44fSMiao Xie 		goto fail;
12238732d44fSMiao Xie 	}
1224e999376fSChris Mason 
12257585717fSChris Mason 	record_root_in_trans(trans, root);
12266bdb72deSSage Weil 	btrfs_set_root_last_snapshot(&root->root_item, trans->transid);
12276bdb72deSSage Weil 	memcpy(new_root_item, &root->root_item, sizeof(*new_root_item));
122808fe4db1SLi Zefan 	btrfs_check_and_init_root_item(new_root_item);
12296bdb72deSSage Weil 
1230b83cc969SLi Zefan 	root_flags = btrfs_root_flags(new_root_item);
1231b83cc969SLi Zefan 	if (pending->readonly)
1232b83cc969SLi Zefan 		root_flags |= BTRFS_ROOT_SUBVOL_RDONLY;
1233b83cc969SLi Zefan 	else
1234b83cc969SLi Zefan 		root_flags &= ~BTRFS_ROOT_SUBVOL_RDONLY;
1235b83cc969SLi Zefan 	btrfs_set_root_flags(new_root_item, root_flags);
1236b83cc969SLi Zefan 
12378ea05e3aSAlexander Block 	btrfs_set_root_generation_v2(new_root_item,
12388ea05e3aSAlexander Block 			trans->transid);
12398ea05e3aSAlexander Block 	uuid_le_gen(&new_uuid);
12408ea05e3aSAlexander Block 	memcpy(new_root_item->uuid, new_uuid.b, BTRFS_UUID_SIZE);
12418ea05e3aSAlexander Block 	memcpy(new_root_item->parent_uuid, root->root_item.uuid,
12428ea05e3aSAlexander Block 			BTRFS_UUID_SIZE);
124370023da2SStefan Behrens 	if (!(root_flags & BTRFS_ROOT_SUBVOL_RDONLY)) {
124470023da2SStefan Behrens 		memset(new_root_item->received_uuid, 0,
124570023da2SStefan Behrens 		       sizeof(new_root_item->received_uuid));
12468ea05e3aSAlexander Block 		memset(&new_root_item->stime, 0, sizeof(new_root_item->stime));
12478ea05e3aSAlexander Block 		memset(&new_root_item->rtime, 0, sizeof(new_root_item->rtime));
12488ea05e3aSAlexander Block 		btrfs_set_root_stransid(new_root_item, 0);
12498ea05e3aSAlexander Block 		btrfs_set_root_rtransid(new_root_item, 0);
125070023da2SStefan Behrens 	}
12513cae210fSQu Wenruo 	btrfs_set_stack_timespec_sec(&new_root_item->otime, cur_time.tv_sec);
12523cae210fSQu Wenruo 	btrfs_set_stack_timespec_nsec(&new_root_item->otime, cur_time.tv_nsec);
125370023da2SStefan Behrens 	btrfs_set_root_otransid(new_root_item, trans->transid);
12548ea05e3aSAlexander Block 
1255925baeddSChris Mason 	old = btrfs_lock_root_node(root);
125649b25e05SJeff Mahoney 	ret = btrfs_cow_block(trans, root, old, NULL, 0, &old);
125779787eaaSJeff Mahoney 	if (ret) {
125879787eaaSJeff Mahoney 		btrfs_tree_unlock(old);
125979787eaaSJeff Mahoney 		free_extent_buffer(old);
12608732d44fSMiao Xie 		btrfs_abort_transaction(trans, root, ret);
12618732d44fSMiao Xie 		goto fail;
126279787eaaSJeff Mahoney 	}
126349b25e05SJeff Mahoney 
12645d4f98a2SYan Zheng 	btrfs_set_lock_blocking(old);
12653063d29fSChris Mason 
126649b25e05SJeff Mahoney 	ret = btrfs_copy_root(trans, root, old, &tmp, objectid);
126779787eaaSJeff Mahoney 	/* clean up in any case */
1268925baeddSChris Mason 	btrfs_tree_unlock(old);
1269925baeddSChris Mason 	free_extent_buffer(old);
12708732d44fSMiao Xie 	if (ret) {
12718732d44fSMiao Xie 		btrfs_abort_transaction(trans, root, ret);
12728732d44fSMiao Xie 		goto fail;
12738732d44fSMiao Xie 	}
12743063d29fSChris Mason 
1275f1ebcc74SLiu Bo 	/* see comments in should_cow_block() */
1276f1ebcc74SLiu Bo 	root->force_cow = 1;
1277f1ebcc74SLiu Bo 	smp_wmb();
1278f1ebcc74SLiu Bo 
12795d4f98a2SYan Zheng 	btrfs_set_root_node(new_root_item, tmp);
1280a22285a6SYan, Zheng 	/* record when the snapshot was created in key.offset */
1281a22285a6SYan, Zheng 	key.offset = trans->transid;
1282a22285a6SYan, Zheng 	ret = btrfs_insert_root(trans, tree_root, &key, new_root_item);
1283925baeddSChris Mason 	btrfs_tree_unlock(tmp);
12843063d29fSChris Mason 	free_extent_buffer(tmp);
12858732d44fSMiao Xie 	if (ret) {
12868732d44fSMiao Xie 		btrfs_abort_transaction(trans, root, ret);
12878732d44fSMiao Xie 		goto fail;
12888732d44fSMiao Xie 	}
12890660b5afSChris Mason 
1290a22285a6SYan, Zheng 	/*
1291a22285a6SYan, Zheng 	 * insert root back/forward references
1292a22285a6SYan, Zheng 	 */
1293a22285a6SYan, Zheng 	ret = btrfs_add_root_ref(trans, tree_root, objectid,
1294a22285a6SYan, Zheng 				 parent_root->root_key.objectid,
129533345d01SLi Zefan 				 btrfs_ino(parent_inode), index,
1296a22285a6SYan, Zheng 				 dentry->d_name.name, dentry->d_name.len);
12978732d44fSMiao Xie 	if (ret) {
12988732d44fSMiao Xie 		btrfs_abort_transaction(trans, root, ret);
12998732d44fSMiao Xie 		goto fail;
13008732d44fSMiao Xie 	}
1301a22285a6SYan, Zheng 
1302a22285a6SYan, Zheng 	key.offset = (u64)-1;
1303a22285a6SYan, Zheng 	pending->snap = btrfs_read_fs_root_no_name(root->fs_info, &key);
130479787eaaSJeff Mahoney 	if (IS_ERR(pending->snap)) {
130579787eaaSJeff Mahoney 		ret = PTR_ERR(pending->snap);
13068732d44fSMiao Xie 		btrfs_abort_transaction(trans, root, ret);
13078732d44fSMiao Xie 		goto fail;
130879787eaaSJeff Mahoney 	}
1309d68fc57bSYan, Zheng 
131049b25e05SJeff Mahoney 	ret = btrfs_reloc_post_snapshot(trans, pending);
13118732d44fSMiao Xie 	if (ret) {
13128732d44fSMiao Xie 		btrfs_abort_transaction(trans, root, ret);
13138732d44fSMiao Xie 		goto fail;
13148732d44fSMiao Xie 	}
1315361048f5SMiao Xie 
1316361048f5SMiao Xie 	ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
13178732d44fSMiao Xie 	if (ret) {
13188732d44fSMiao Xie 		btrfs_abort_transaction(trans, root, ret);
13198732d44fSMiao Xie 		goto fail;
13208732d44fSMiao Xie 	}
132142874b3dSMiao Xie 
132242874b3dSMiao Xie 	ret = btrfs_insert_dir_item(trans, parent_root,
132342874b3dSMiao Xie 				    dentry->d_name.name, dentry->d_name.len,
132442874b3dSMiao Xie 				    parent_inode, &key,
132542874b3dSMiao Xie 				    BTRFS_FT_DIR, index);
132642874b3dSMiao Xie 	/* We have check then name at the beginning, so it is impossible. */
13279c52057cSChris Mason 	BUG_ON(ret == -EEXIST || ret == -EOVERFLOW);
13288732d44fSMiao Xie 	if (ret) {
13298732d44fSMiao Xie 		btrfs_abort_transaction(trans, root, ret);
13308732d44fSMiao Xie 		goto fail;
13318732d44fSMiao Xie 	}
133242874b3dSMiao Xie 
133342874b3dSMiao Xie 	btrfs_i_size_write(parent_inode, parent_inode->i_size +
133442874b3dSMiao Xie 					 dentry->d_name.len * 2);
133542874b3dSMiao Xie 	parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
1336be6aef60SJosef Bacik 	ret = btrfs_update_inode_fallback(trans, parent_root, parent_inode);
1337dd5f9615SStefan Behrens 	if (ret) {
13388732d44fSMiao Xie 		btrfs_abort_transaction(trans, root, ret);
1339dd5f9615SStefan Behrens 		goto fail;
1340dd5f9615SStefan Behrens 	}
1341dd5f9615SStefan Behrens 	ret = btrfs_uuid_tree_add(trans, fs_info->uuid_root, new_uuid.b,
1342dd5f9615SStefan Behrens 				  BTRFS_UUID_KEY_SUBVOL, objectid);
1343dd5f9615SStefan Behrens 	if (ret) {
1344dd5f9615SStefan Behrens 		btrfs_abort_transaction(trans, root, ret);
1345dd5f9615SStefan Behrens 		goto fail;
1346dd5f9615SStefan Behrens 	}
1347dd5f9615SStefan Behrens 	if (!btrfs_is_empty_uuid(new_root_item->received_uuid)) {
1348dd5f9615SStefan Behrens 		ret = btrfs_uuid_tree_add(trans, fs_info->uuid_root,
1349dd5f9615SStefan Behrens 					  new_root_item->received_uuid,
1350dd5f9615SStefan Behrens 					  BTRFS_UUID_KEY_RECEIVED_SUBVOL,
1351dd5f9615SStefan Behrens 					  objectid);
1352dd5f9615SStefan Behrens 		if (ret && ret != -EEXIST) {
1353dd5f9615SStefan Behrens 			btrfs_abort_transaction(trans, root, ret);
1354dd5f9615SStefan Behrens 			goto fail;
1355dd5f9615SStefan Behrens 		}
1356dd5f9615SStefan Behrens 	}
13573063d29fSChris Mason fail:
1358aec8030aSMiao Xie 	pending->error = ret;
1359aec8030aSMiao Xie dir_item_existed:
136098c9942aSLiu Bo 	trans->block_rsv = rsv;
13612382c5ccSLiu Bo 	trans->bytes_reserved = 0;
13626fa9700eSMiao Xie no_free_objectid:
13636fa9700eSMiao Xie 	kfree(new_root_item);
13646fa9700eSMiao Xie root_item_alloc_fail:
136542874b3dSMiao Xie 	btrfs_free_path(path);
136649b25e05SJeff Mahoney 	return ret;
13673063d29fSChris Mason }
13683063d29fSChris Mason 
1369d352ac68SChris Mason /*
1370d352ac68SChris Mason  * create all the snapshots we've scheduled for creation
1371d352ac68SChris Mason  */
137280b6794dSChris Mason static noinline int create_pending_snapshots(struct btrfs_trans_handle *trans,
13733063d29fSChris Mason 					     struct btrfs_fs_info *fs_info)
13743063d29fSChris Mason {
1375aec8030aSMiao Xie 	struct btrfs_pending_snapshot *pending, *next;
13763063d29fSChris Mason 	struct list_head *head = &trans->transaction->pending_snapshots;
1377aec8030aSMiao Xie 	int ret = 0;
13783de4586cSChris Mason 
1379aec8030aSMiao Xie 	list_for_each_entry_safe(pending, next, head, list) {
1380aec8030aSMiao Xie 		list_del(&pending->list);
1381aec8030aSMiao Xie 		ret = create_pending_snapshot(trans, fs_info, pending);
1382aec8030aSMiao Xie 		if (ret)
1383aec8030aSMiao Xie 			break;
1384aec8030aSMiao Xie 	}
1385aec8030aSMiao Xie 	return ret;
13863de4586cSChris Mason }
13873de4586cSChris Mason 
13885d4f98a2SYan Zheng static void update_super_roots(struct btrfs_root *root)
13895d4f98a2SYan Zheng {
13905d4f98a2SYan Zheng 	struct btrfs_root_item *root_item;
13915d4f98a2SYan Zheng 	struct btrfs_super_block *super;
13925d4f98a2SYan Zheng 
13936c41761fSDavid Sterba 	super = root->fs_info->super_copy;
13945d4f98a2SYan Zheng 
13955d4f98a2SYan Zheng 	root_item = &root->fs_info->chunk_root->root_item;
13965d4f98a2SYan Zheng 	super->chunk_root = root_item->bytenr;
13975d4f98a2SYan Zheng 	super->chunk_root_generation = root_item->generation;
13985d4f98a2SYan Zheng 	super->chunk_root_level = root_item->level;
13995d4f98a2SYan Zheng 
14005d4f98a2SYan Zheng 	root_item = &root->fs_info->tree_root->root_item;
14015d4f98a2SYan Zheng 	super->root = root_item->bytenr;
14025d4f98a2SYan Zheng 	super->generation = root_item->generation;
14035d4f98a2SYan Zheng 	super->root_level = root_item->level;
140473bc1876SJosef Bacik 	if (btrfs_test_opt(root, SPACE_CACHE))
14050af3d00bSJosef Bacik 		super->cache_generation = root_item->generation;
140670f80175SStefan Behrens 	if (root->fs_info->update_uuid_tree_gen)
140726432799SStefan Behrens 		super->uuid_tree_generation = root_item->generation;
14085d4f98a2SYan Zheng }
14095d4f98a2SYan Zheng 
1410f36f3042SChris Mason int btrfs_transaction_in_commit(struct btrfs_fs_info *info)
1411f36f3042SChris Mason {
14124a9d8bdeSMiao Xie 	struct btrfs_transaction *trans;
1413f36f3042SChris Mason 	int ret = 0;
14144a9d8bdeSMiao Xie 
1415a4abeea4SJosef Bacik 	spin_lock(&info->trans_lock);
14164a9d8bdeSMiao Xie 	trans = info->running_transaction;
14174a9d8bdeSMiao Xie 	if (trans)
14184a9d8bdeSMiao Xie 		ret = (trans->state >= TRANS_STATE_COMMIT_START);
1419a4abeea4SJosef Bacik 	spin_unlock(&info->trans_lock);
1420f36f3042SChris Mason 	return ret;
1421f36f3042SChris Mason }
1422f36f3042SChris Mason 
14238929ecfaSYan, Zheng int btrfs_transaction_blocked(struct btrfs_fs_info *info)
14248929ecfaSYan, Zheng {
14254a9d8bdeSMiao Xie 	struct btrfs_transaction *trans;
14268929ecfaSYan, Zheng 	int ret = 0;
14274a9d8bdeSMiao Xie 
1428a4abeea4SJosef Bacik 	spin_lock(&info->trans_lock);
14294a9d8bdeSMiao Xie 	trans = info->running_transaction;
14304a9d8bdeSMiao Xie 	if (trans)
14314a9d8bdeSMiao Xie 		ret = is_transaction_blocked(trans);
1432a4abeea4SJosef Bacik 	spin_unlock(&info->trans_lock);
14338929ecfaSYan, Zheng 	return ret;
14348929ecfaSYan, Zheng }
14358929ecfaSYan, Zheng 
1436bb9c12c9SSage Weil /*
1437bb9c12c9SSage Weil  * wait for the current transaction commit to start and block subsequent
1438bb9c12c9SSage Weil  * transaction joins
1439bb9c12c9SSage Weil  */
1440bb9c12c9SSage Weil static void wait_current_trans_commit_start(struct btrfs_root *root,
1441bb9c12c9SSage Weil 					    struct btrfs_transaction *trans)
1442bb9c12c9SSage Weil {
14434a9d8bdeSMiao Xie 	wait_event(root->fs_info->transaction_blocked_wait,
1444501407aaSJosef Bacik 		   trans->state >= TRANS_STATE_COMMIT_START ||
1445501407aaSJosef Bacik 		   trans->aborted);
1446bb9c12c9SSage Weil }
1447bb9c12c9SSage Weil 
1448bb9c12c9SSage Weil /*
1449bb9c12c9SSage Weil  * wait for the current transaction to start and then become unblocked.
1450bb9c12c9SSage Weil  * caller holds ref.
1451bb9c12c9SSage Weil  */
1452bb9c12c9SSage Weil static void wait_current_trans_commit_start_and_unblock(struct btrfs_root *root,
1453bb9c12c9SSage Weil 					 struct btrfs_transaction *trans)
1454bb9c12c9SSage Weil {
145572d63ed6SLi Zefan 	wait_event(root->fs_info->transaction_wait,
1456501407aaSJosef Bacik 		   trans->state >= TRANS_STATE_UNBLOCKED ||
1457501407aaSJosef Bacik 		   trans->aborted);
1458bb9c12c9SSage Weil }
1459bb9c12c9SSage Weil 
1460bb9c12c9SSage Weil /*
1461bb9c12c9SSage Weil  * commit transactions asynchronously. once btrfs_commit_transaction_async
1462bb9c12c9SSage Weil  * returns, any subsequent transaction will not be allowed to join.
1463bb9c12c9SSage Weil  */
1464bb9c12c9SSage Weil struct btrfs_async_commit {
1465bb9c12c9SSage Weil 	struct btrfs_trans_handle *newtrans;
1466bb9c12c9SSage Weil 	struct btrfs_root *root;
14677892b5afSMiao Xie 	struct work_struct work;
1468bb9c12c9SSage Weil };
1469bb9c12c9SSage Weil 
1470bb9c12c9SSage Weil static void do_async_commit(struct work_struct *work)
1471bb9c12c9SSage Weil {
1472bb9c12c9SSage Weil 	struct btrfs_async_commit *ac =
14737892b5afSMiao Xie 		container_of(work, struct btrfs_async_commit, work);
1474bb9c12c9SSage Weil 
14756fc4e354SSage Weil 	/*
14766fc4e354SSage Weil 	 * We've got freeze protection passed with the transaction.
14776fc4e354SSage Weil 	 * Tell lockdep about it.
14786fc4e354SSage Weil 	 */
1479b1a06a4bSLiu Bo 	if (ac->newtrans->type & __TRANS_FREEZABLE)
14806fc4e354SSage Weil 		rwsem_acquire_read(
14816fc4e354SSage Weil 		     &ac->root->fs_info->sb->s_writers.lock_map[SB_FREEZE_FS-1],
14826fc4e354SSage Weil 		     0, 1, _THIS_IP_);
14836fc4e354SSage Weil 
1484e209db7aSSage Weil 	current->journal_info = ac->newtrans;
1485e209db7aSSage Weil 
1486bb9c12c9SSage Weil 	btrfs_commit_transaction(ac->newtrans, ac->root);
1487bb9c12c9SSage Weil 	kfree(ac);
1488bb9c12c9SSage Weil }
1489bb9c12c9SSage Weil 
1490bb9c12c9SSage Weil int btrfs_commit_transaction_async(struct btrfs_trans_handle *trans,
1491bb9c12c9SSage Weil 				   struct btrfs_root *root,
1492bb9c12c9SSage Weil 				   int wait_for_unblock)
1493bb9c12c9SSage Weil {
1494bb9c12c9SSage Weil 	struct btrfs_async_commit *ac;
1495bb9c12c9SSage Weil 	struct btrfs_transaction *cur_trans;
1496bb9c12c9SSage Weil 
1497bb9c12c9SSage Weil 	ac = kmalloc(sizeof(*ac), GFP_NOFS);
1498db5b493aSTsutomu Itoh 	if (!ac)
1499db5b493aSTsutomu Itoh 		return -ENOMEM;
1500bb9c12c9SSage Weil 
15017892b5afSMiao Xie 	INIT_WORK(&ac->work, do_async_commit);
1502bb9c12c9SSage Weil 	ac->root = root;
15037a7eaa40SJosef Bacik 	ac->newtrans = btrfs_join_transaction(root);
15043612b495STsutomu Itoh 	if (IS_ERR(ac->newtrans)) {
15053612b495STsutomu Itoh 		int err = PTR_ERR(ac->newtrans);
15063612b495STsutomu Itoh 		kfree(ac);
15073612b495STsutomu Itoh 		return err;
15083612b495STsutomu Itoh 	}
1509bb9c12c9SSage Weil 
1510bb9c12c9SSage Weil 	/* take transaction reference */
1511bb9c12c9SSage Weil 	cur_trans = trans->transaction;
151213c5a93eSJosef Bacik 	atomic_inc(&cur_trans->use_count);
1513bb9c12c9SSage Weil 
1514bb9c12c9SSage Weil 	btrfs_end_transaction(trans, root);
15156fc4e354SSage Weil 
15166fc4e354SSage Weil 	/*
15176fc4e354SSage Weil 	 * Tell lockdep we've released the freeze rwsem, since the
15186fc4e354SSage Weil 	 * async commit thread will be the one to unlock it.
15196fc4e354SSage Weil 	 */
1520b1a06a4bSLiu Bo 	if (ac->newtrans->type & __TRANS_FREEZABLE)
1521ff7c1d33SMiao Xie 		rwsem_release(
1522ff7c1d33SMiao Xie 			&root->fs_info->sb->s_writers.lock_map[SB_FREEZE_FS-1],
15236fc4e354SSage Weil 			1, _THIS_IP_);
15246fc4e354SSage Weil 
15257892b5afSMiao Xie 	schedule_work(&ac->work);
1526bb9c12c9SSage Weil 
1527bb9c12c9SSage Weil 	/* wait for transaction to start and unblock */
1528bb9c12c9SSage Weil 	if (wait_for_unblock)
1529bb9c12c9SSage Weil 		wait_current_trans_commit_start_and_unblock(root, cur_trans);
1530bb9c12c9SSage Weil 	else
1531bb9c12c9SSage Weil 		wait_current_trans_commit_start(root, cur_trans);
1532bb9c12c9SSage Weil 
153338e88054SSage Weil 	if (current->journal_info == trans)
153438e88054SSage Weil 		current->journal_info = NULL;
153538e88054SSage Weil 
1536724e2315SJosef Bacik 	btrfs_put_transaction(cur_trans);
1537bb9c12c9SSage Weil 	return 0;
1538bb9c12c9SSage Weil }
1539bb9c12c9SSage Weil 
154049b25e05SJeff Mahoney 
154149b25e05SJeff Mahoney static void cleanup_transaction(struct btrfs_trans_handle *trans,
15427b8b92afSJosef Bacik 				struct btrfs_root *root, int err)
154349b25e05SJeff Mahoney {
154449b25e05SJeff Mahoney 	struct btrfs_transaction *cur_trans = trans->transaction;
1545f094ac32SLiu Bo 	DEFINE_WAIT(wait);
154649b25e05SJeff Mahoney 
154749b25e05SJeff Mahoney 	WARN_ON(trans->use_count > 1);
154849b25e05SJeff Mahoney 
15497b8b92afSJosef Bacik 	btrfs_abort_transaction(trans, root, err);
15507b8b92afSJosef Bacik 
155149b25e05SJeff Mahoney 	spin_lock(&root->fs_info->trans_lock);
155266b6135bSLiu Bo 
155325d8c284SMiao Xie 	/*
155425d8c284SMiao Xie 	 * If the transaction is removed from the list, it means this
155525d8c284SMiao Xie 	 * transaction has been committed successfully, so it is impossible
155625d8c284SMiao Xie 	 * to call the cleanup function.
155725d8c284SMiao Xie 	 */
155825d8c284SMiao Xie 	BUG_ON(list_empty(&cur_trans->list));
155966b6135bSLiu Bo 
156049b25e05SJeff Mahoney 	list_del_init(&cur_trans->list);
1561d7096fc3SJosef Bacik 	if (cur_trans == root->fs_info->running_transaction) {
15624a9d8bdeSMiao Xie 		cur_trans->state = TRANS_STATE_COMMIT_DOING;
1563f094ac32SLiu Bo 		spin_unlock(&root->fs_info->trans_lock);
1564f094ac32SLiu Bo 		wait_event(cur_trans->writer_wait,
1565f094ac32SLiu Bo 			   atomic_read(&cur_trans->num_writers) == 1);
1566f094ac32SLiu Bo 
1567f094ac32SLiu Bo 		spin_lock(&root->fs_info->trans_lock);
1568d7096fc3SJosef Bacik 	}
156949b25e05SJeff Mahoney 	spin_unlock(&root->fs_info->trans_lock);
157049b25e05SJeff Mahoney 
157149b25e05SJeff Mahoney 	btrfs_cleanup_one_transaction(trans->transaction, root);
157249b25e05SJeff Mahoney 
15734a9d8bdeSMiao Xie 	spin_lock(&root->fs_info->trans_lock);
15744a9d8bdeSMiao Xie 	if (cur_trans == root->fs_info->running_transaction)
15754a9d8bdeSMiao Xie 		root->fs_info->running_transaction = NULL;
15764a9d8bdeSMiao Xie 	spin_unlock(&root->fs_info->trans_lock);
15774a9d8bdeSMiao Xie 
1578e0228285SJosef Bacik 	if (trans->type & __TRANS_FREEZABLE)
1579e0228285SJosef Bacik 		sb_end_intwrite(root->fs_info->sb);
1580724e2315SJosef Bacik 	btrfs_put_transaction(cur_trans);
1581724e2315SJosef Bacik 	btrfs_put_transaction(cur_trans);
158249b25e05SJeff Mahoney 
158349b25e05SJeff Mahoney 	trace_btrfs_transaction_commit(root);
158449b25e05SJeff Mahoney 
158549b25e05SJeff Mahoney 	btrfs_scrub_continue(root);
158649b25e05SJeff Mahoney 
158749b25e05SJeff Mahoney 	if (current->journal_info == trans)
158849b25e05SJeff Mahoney 		current->journal_info = NULL;
158949b25e05SJeff Mahoney 
159049b25e05SJeff Mahoney 	kmem_cache_free(btrfs_trans_handle_cachep, trans);
159149b25e05SJeff Mahoney }
159249b25e05SJeff Mahoney 
1593ca469637SMiao Xie static int btrfs_flush_all_pending_stuffs(struct btrfs_trans_handle *trans,
1594ca469637SMiao Xie 					  struct btrfs_root *root)
1595ca469637SMiao Xie {
1596ca469637SMiao Xie 	int ret;
1597ca469637SMiao Xie 
1598ca469637SMiao Xie 	ret = btrfs_run_delayed_items(trans, root);
1599ca469637SMiao Xie 	/*
1600ca469637SMiao Xie 	 * running the delayed items may have added new refs. account
1601ca469637SMiao Xie 	 * them now so that they hinder processing of more delayed refs
1602ca469637SMiao Xie 	 * as little as possible.
1603ca469637SMiao Xie 	 */
160480d94fb3SFilipe David Borba Manana 	if (ret) {
1605ca469637SMiao Xie 		btrfs_delayed_refs_qgroup_accounting(trans, root->fs_info);
160680d94fb3SFilipe David Borba Manana 		return ret;
160780d94fb3SFilipe David Borba Manana 	}
160880d94fb3SFilipe David Borba Manana 
160980d94fb3SFilipe David Borba Manana 	ret = btrfs_delayed_refs_qgroup_accounting(trans, root->fs_info);
161080d94fb3SFilipe David Borba Manana 	if (ret)
161180d94fb3SFilipe David Borba Manana 		return ret;
1612ca469637SMiao Xie 
1613ca469637SMiao Xie 	/*
1614ca469637SMiao Xie 	 * rename don't use btrfs_join_transaction, so, once we
1615ca469637SMiao Xie 	 * set the transaction to blocked above, we aren't going
1616ca469637SMiao Xie 	 * to get any new ordered operations.  We can safely run
1617ca469637SMiao Xie 	 * it here and no for sure that nothing new will be added
1618ca469637SMiao Xie 	 * to the list
1619ca469637SMiao Xie 	 */
1620569e0f35SJosef Bacik 	ret = btrfs_run_ordered_operations(trans, root, 1);
1621ca469637SMiao Xie 
1622eebc6084SMiao Xie 	return ret;
1623ca469637SMiao Xie }
1624ca469637SMiao Xie 
162582436617SMiao Xie static inline int btrfs_start_delalloc_flush(struct btrfs_fs_info *fs_info)
162682436617SMiao Xie {
162782436617SMiao Xie 	if (btrfs_test_opt(fs_info->tree_root, FLUSHONCOMMIT))
162891aef86fSMiao Xie 		return btrfs_start_delalloc_roots(fs_info, 1);
162982436617SMiao Xie 	return 0;
163082436617SMiao Xie }
163182436617SMiao Xie 
163282436617SMiao Xie static inline void btrfs_wait_delalloc_flush(struct btrfs_fs_info *fs_info)
163382436617SMiao Xie {
163482436617SMiao Xie 	if (btrfs_test_opt(fs_info->tree_root, FLUSHONCOMMIT))
1635b0244199SMiao Xie 		btrfs_wait_ordered_roots(fs_info, -1);
163682436617SMiao Xie }
163782436617SMiao Xie 
163879154b1bSChris Mason int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
163979154b1bSChris Mason 			     struct btrfs_root *root)
164079154b1bSChris Mason {
164149b25e05SJeff Mahoney 	struct btrfs_transaction *cur_trans = trans->transaction;
16428fd17795SChris Mason 	struct btrfs_transaction *prev_trans = NULL;
164325287e0aSMiao Xie 	int ret;
164479154b1bSChris Mason 
1645569e0f35SJosef Bacik 	ret = btrfs_run_ordered_operations(trans, root, 0);
164625287e0aSMiao Xie 	if (ret) {
164725287e0aSMiao Xie 		btrfs_abort_transaction(trans, root, ret);
1648e4a2bcacSJosef Bacik 		btrfs_end_transaction(trans, root);
1649e4a2bcacSJosef Bacik 		return ret;
165025287e0aSMiao Xie 	}
165125287e0aSMiao Xie 
16528d25a086SMiao Xie 	/* Stop the commit early if ->aborted is set */
16538d25a086SMiao Xie 	if (unlikely(ACCESS_ONCE(cur_trans->aborted))) {
165425287e0aSMiao Xie 		ret = cur_trans->aborted;
1655e4a2bcacSJosef Bacik 		btrfs_end_transaction(trans, root);
1656e4a2bcacSJosef Bacik 		return ret;
165725287e0aSMiao Xie 	}
165849b25e05SJeff Mahoney 
165956bec294SChris Mason 	/* make a pass through all the delayed refs we have so far
166056bec294SChris Mason 	 * any runnings procs may add more while we are here
166156bec294SChris Mason 	 */
166256bec294SChris Mason 	ret = btrfs_run_delayed_refs(trans, root, 0);
1663e4a2bcacSJosef Bacik 	if (ret) {
1664e4a2bcacSJosef Bacik 		btrfs_end_transaction(trans, root);
1665e4a2bcacSJosef Bacik 		return ret;
1666e4a2bcacSJosef Bacik 	}
166756bec294SChris Mason 
16680e721106SJosef Bacik 	btrfs_trans_release_metadata(trans, root);
16690e721106SJosef Bacik 	trans->block_rsv = NULL;
1670272d26d0SMiao Xie 	if (trans->qgroup_reserved) {
1671272d26d0SMiao Xie 		btrfs_qgroup_free(root, trans->qgroup_reserved);
1672272d26d0SMiao Xie 		trans->qgroup_reserved = 0;
1673272d26d0SMiao Xie 	}
16740e721106SJosef Bacik 
1675b7ec40d7SChris Mason 	cur_trans = trans->transaction;
167649b25e05SJeff Mahoney 
167756bec294SChris Mason 	/*
167856bec294SChris Mason 	 * set the flushing flag so procs in this transaction have to
167956bec294SChris Mason 	 * start sending their work down.
168056bec294SChris Mason 	 */
1681b7ec40d7SChris Mason 	cur_trans->delayed_refs.flushing = 1;
16821be41b78SJosef Bacik 	smp_wmb();
168356bec294SChris Mason 
1684ea658badSJosef Bacik 	if (!list_empty(&trans->new_bgs))
1685ea658badSJosef Bacik 		btrfs_create_pending_block_groups(trans, root);
1686ea658badSJosef Bacik 
1687c3e69d58SChris Mason 	ret = btrfs_run_delayed_refs(trans, root, 0);
1688e4a2bcacSJosef Bacik 	if (ret) {
1689e4a2bcacSJosef Bacik 		btrfs_end_transaction(trans, root);
1690e4a2bcacSJosef Bacik 		return ret;
1691e4a2bcacSJosef Bacik 	}
169256bec294SChris Mason 
16934a9d8bdeSMiao Xie 	spin_lock(&root->fs_info->trans_lock);
16944a9d8bdeSMiao Xie 	if (cur_trans->state >= TRANS_STATE_COMMIT_START) {
16954a9d8bdeSMiao Xie 		spin_unlock(&root->fs_info->trans_lock);
169613c5a93eSJosef Bacik 		atomic_inc(&cur_trans->use_count);
169749b25e05SJeff Mahoney 		ret = btrfs_end_transaction(trans, root);
1698ccd467d6SChris Mason 
1699b9c8300cSLi Zefan 		wait_for_commit(root, cur_trans);
170015ee9bc7SJosef Bacik 
1701724e2315SJosef Bacik 		btrfs_put_transaction(cur_trans);
170215ee9bc7SJosef Bacik 
170349b25e05SJeff Mahoney 		return ret;
170479154b1bSChris Mason 	}
17054313b399SChris Mason 
17064a9d8bdeSMiao Xie 	cur_trans->state = TRANS_STATE_COMMIT_START;
1707bb9c12c9SSage Weil 	wake_up(&root->fs_info->transaction_blocked_wait);
1708bb9c12c9SSage Weil 
1709ccd467d6SChris Mason 	if (cur_trans->list.prev != &root->fs_info->trans_list) {
1710ccd467d6SChris Mason 		prev_trans = list_entry(cur_trans->list.prev,
1711ccd467d6SChris Mason 					struct btrfs_transaction, list);
17124a9d8bdeSMiao Xie 		if (prev_trans->state != TRANS_STATE_COMPLETED) {
171313c5a93eSJosef Bacik 			atomic_inc(&prev_trans->use_count);
1714a4abeea4SJosef Bacik 			spin_unlock(&root->fs_info->trans_lock);
1715ccd467d6SChris Mason 
1716ccd467d6SChris Mason 			wait_for_commit(root, prev_trans);
1717ccd467d6SChris Mason 
1718724e2315SJosef Bacik 			btrfs_put_transaction(prev_trans);
1719a4abeea4SJosef Bacik 		} else {
1720a4abeea4SJosef Bacik 			spin_unlock(&root->fs_info->trans_lock);
1721ccd467d6SChris Mason 		}
1722a4abeea4SJosef Bacik 	} else {
1723a4abeea4SJosef Bacik 		spin_unlock(&root->fs_info->trans_lock);
1724ccd467d6SChris Mason 	}
172515ee9bc7SJosef Bacik 
17260860adfdSMiao Xie 	extwriter_counter_dec(cur_trans, trans->type);
17270860adfdSMiao Xie 
172882436617SMiao Xie 	ret = btrfs_start_delalloc_flush(root->fs_info);
172982436617SMiao Xie 	if (ret)
173082436617SMiao Xie 		goto cleanup_transaction;
173182436617SMiao Xie 
1732ca469637SMiao Xie 	ret = btrfs_flush_all_pending_stuffs(trans, root);
173349b25e05SJeff Mahoney 	if (ret)
173449b25e05SJeff Mahoney 		goto cleanup_transaction;
173516cdcec7SMiao Xie 
1736581227d0SMiao Xie 	wait_event(cur_trans->writer_wait,
1737581227d0SMiao Xie 		   extwriter_counter_read(cur_trans) == 0);
1738ed3b3d31SChris Mason 
1739581227d0SMiao Xie 	/* some pending stuffs might be added after the previous flush. */
1740ca469637SMiao Xie 	ret = btrfs_flush_all_pending_stuffs(trans, root);
1741ca469637SMiao Xie 	if (ret)
1742ca469637SMiao Xie 		goto cleanup_transaction;
1743ca469637SMiao Xie 
174482436617SMiao Xie 	btrfs_wait_delalloc_flush(root->fs_info);
1745cb7ab021SWang Shilong 
1746cb7ab021SWang Shilong 	btrfs_scrub_pause(root);
1747ed0ca140SJosef Bacik 	/*
1748ed0ca140SJosef Bacik 	 * Ok now we need to make sure to block out any other joins while we
1749ed0ca140SJosef Bacik 	 * commit the transaction.  We could have started a join before setting
17504a9d8bdeSMiao Xie 	 * COMMIT_DOING so make sure to wait for num_writers to == 1 again.
1751ed0ca140SJosef Bacik 	 */
1752a4abeea4SJosef Bacik 	spin_lock(&root->fs_info->trans_lock);
17534a9d8bdeSMiao Xie 	cur_trans->state = TRANS_STATE_COMMIT_DOING;
1754a4abeea4SJosef Bacik 	spin_unlock(&root->fs_info->trans_lock);
1755ed0ca140SJosef Bacik 	wait_event(cur_trans->writer_wait,
1756ed0ca140SJosef Bacik 		   atomic_read(&cur_trans->num_writers) == 1);
175715ee9bc7SJosef Bacik 
17582cba30f1SMiao Xie 	/* ->aborted might be set after the previous check, so check it */
17592cba30f1SMiao Xie 	if (unlikely(ACCESS_ONCE(cur_trans->aborted))) {
17602cba30f1SMiao Xie 		ret = cur_trans->aborted;
17612cba30f1SMiao Xie 		goto cleanup_transaction;
17622cba30f1SMiao Xie 	}
17637585717fSChris Mason 	/*
17647585717fSChris Mason 	 * the reloc mutex makes sure that we stop
17657585717fSChris Mason 	 * the balancing code from coming in and moving
17667585717fSChris Mason 	 * extents around in the middle of the commit
17677585717fSChris Mason 	 */
17687585717fSChris Mason 	mutex_lock(&root->fs_info->reloc_mutex);
17697585717fSChris Mason 
177042874b3dSMiao Xie 	/*
177142874b3dSMiao Xie 	 * We needn't worry about the delayed items because we will
177242874b3dSMiao Xie 	 * deal with them in create_pending_snapshot(), which is the
177342874b3dSMiao Xie 	 * core function of the snapshot creation.
177442874b3dSMiao Xie 	 */
177542874b3dSMiao Xie 	ret = create_pending_snapshots(trans, root->fs_info);
177649b25e05SJeff Mahoney 	if (ret) {
177749b25e05SJeff Mahoney 		mutex_unlock(&root->fs_info->reloc_mutex);
177849b25e05SJeff Mahoney 		goto cleanup_transaction;
177949b25e05SJeff Mahoney 	}
17803063d29fSChris Mason 
178142874b3dSMiao Xie 	/*
178242874b3dSMiao Xie 	 * We insert the dir indexes of the snapshots and update the inode
178342874b3dSMiao Xie 	 * of the snapshots' parents after the snapshot creation, so there
178442874b3dSMiao Xie 	 * are some delayed items which are not dealt with. Now deal with
178542874b3dSMiao Xie 	 * them.
178642874b3dSMiao Xie 	 *
178742874b3dSMiao Xie 	 * We needn't worry that this operation will corrupt the snapshots,
178842874b3dSMiao Xie 	 * because all the tree which are snapshoted will be forced to COW
178942874b3dSMiao Xie 	 * the nodes and leaves.
179042874b3dSMiao Xie 	 */
179142874b3dSMiao Xie 	ret = btrfs_run_delayed_items(trans, root);
179249b25e05SJeff Mahoney 	if (ret) {
179349b25e05SJeff Mahoney 		mutex_unlock(&root->fs_info->reloc_mutex);
179449b25e05SJeff Mahoney 		goto cleanup_transaction;
179549b25e05SJeff Mahoney 	}
179616cdcec7SMiao Xie 
179756bec294SChris Mason 	ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
179849b25e05SJeff Mahoney 	if (ret) {
179949b25e05SJeff Mahoney 		mutex_unlock(&root->fs_info->reloc_mutex);
180049b25e05SJeff Mahoney 		goto cleanup_transaction;
180149b25e05SJeff Mahoney 	}
180256bec294SChris Mason 
1803e999376fSChris Mason 	/*
1804e999376fSChris Mason 	 * make sure none of the code above managed to slip in a
1805e999376fSChris Mason 	 * delayed item
1806e999376fSChris Mason 	 */
1807e999376fSChris Mason 	btrfs_assert_delayed_root_empty(root);
1808e999376fSChris Mason 
18092c90e5d6SChris Mason 	WARN_ON(cur_trans != trans->transaction);
1810dc17ff8fSChris Mason 
1811e02119d5SChris Mason 	/* btrfs_commit_tree_roots is responsible for getting the
1812e02119d5SChris Mason 	 * various roots consistent with each other.  Every pointer
1813e02119d5SChris Mason 	 * in the tree of tree roots has to point to the most up to date
1814e02119d5SChris Mason 	 * root for every subvolume and other tree.  So, we have to keep
1815e02119d5SChris Mason 	 * the tree logging code from jumping in and changing any
1816e02119d5SChris Mason 	 * of the trees.
1817e02119d5SChris Mason 	 *
1818e02119d5SChris Mason 	 * At this point in the commit, there can't be any tree-log
1819e02119d5SChris Mason 	 * writers, but a little lower down we drop the trans mutex
1820e02119d5SChris Mason 	 * and let new people in.  By holding the tree_log_mutex
1821e02119d5SChris Mason 	 * from now until after the super is written, we avoid races
1822e02119d5SChris Mason 	 * with the tree-log code.
1823e02119d5SChris Mason 	 */
1824e02119d5SChris Mason 	mutex_lock(&root->fs_info->tree_log_mutex);
18251a40e23bSZheng Yan 
18265d4f98a2SYan Zheng 	ret = commit_fs_roots(trans, root);
182749b25e05SJeff Mahoney 	if (ret) {
182849b25e05SJeff Mahoney 		mutex_unlock(&root->fs_info->tree_log_mutex);
1829871383beSDavid Sterba 		mutex_unlock(&root->fs_info->reloc_mutex);
183049b25e05SJeff Mahoney 		goto cleanup_transaction;
183149b25e05SJeff Mahoney 	}
183254aa1f4dSChris Mason 
18335d4f98a2SYan Zheng 	/* commit_fs_roots gets rid of all the tree log roots, it is now
1834e02119d5SChris Mason 	 * safe to free the root of tree log roots
1835e02119d5SChris Mason 	 */
1836e02119d5SChris Mason 	btrfs_free_log_root_tree(trans, root->fs_info);
1837e02119d5SChris Mason 
18385d4f98a2SYan Zheng 	ret = commit_cowonly_roots(trans, root);
183949b25e05SJeff Mahoney 	if (ret) {
184049b25e05SJeff Mahoney 		mutex_unlock(&root->fs_info->tree_log_mutex);
1841871383beSDavid Sterba 		mutex_unlock(&root->fs_info->reloc_mutex);
184249b25e05SJeff Mahoney 		goto cleanup_transaction;
184349b25e05SJeff Mahoney 	}
184454aa1f4dSChris Mason 
18452cba30f1SMiao Xie 	/*
18462cba30f1SMiao Xie 	 * The tasks which save the space cache and inode cache may also
18472cba30f1SMiao Xie 	 * update ->aborted, check it.
18482cba30f1SMiao Xie 	 */
18492cba30f1SMiao Xie 	if (unlikely(ACCESS_ONCE(cur_trans->aborted))) {
18502cba30f1SMiao Xie 		ret = cur_trans->aborted;
18512cba30f1SMiao Xie 		mutex_unlock(&root->fs_info->tree_log_mutex);
18522cba30f1SMiao Xie 		mutex_unlock(&root->fs_info->reloc_mutex);
18532cba30f1SMiao Xie 		goto cleanup_transaction;
18542cba30f1SMiao Xie 	}
18552cba30f1SMiao Xie 
185611833d66SYan Zheng 	btrfs_prepare_extent_commit(trans, root);
185711833d66SYan Zheng 
185878fae27eSChris Mason 	cur_trans = root->fs_info->running_transaction;
18595f39d397SChris Mason 
18605d4f98a2SYan Zheng 	btrfs_set_root_node(&root->fs_info->tree_root->root_item,
18615d4f98a2SYan Zheng 			    root->fs_info->tree_root->node);
1862817d52f8SJosef Bacik 	switch_commit_root(root->fs_info->tree_root);
18635d4f98a2SYan Zheng 
18645d4f98a2SYan Zheng 	btrfs_set_root_node(&root->fs_info->chunk_root->root_item,
18655d4f98a2SYan Zheng 			    root->fs_info->chunk_root->node);
1866817d52f8SJosef Bacik 	switch_commit_root(root->fs_info->chunk_root);
18675d4f98a2SYan Zheng 
1868edf39272SJan Schmidt 	assert_qgroups_uptodate(trans);
18695d4f98a2SYan Zheng 	update_super_roots(root);
1870e02119d5SChris Mason 
18716c41761fSDavid Sterba 	btrfs_set_super_log_root(root->fs_info->super_copy, 0);
18726c41761fSDavid Sterba 	btrfs_set_super_log_root_level(root->fs_info->super_copy, 0);
18736c41761fSDavid Sterba 	memcpy(root->fs_info->super_for_commit, root->fs_info->super_copy,
18746c41761fSDavid Sterba 	       sizeof(*root->fs_info->super_copy));
1875ccd467d6SChris Mason 
1876a4abeea4SJosef Bacik 	spin_lock(&root->fs_info->trans_lock);
18774a9d8bdeSMiao Xie 	cur_trans->state = TRANS_STATE_UNBLOCKED;
1878a4abeea4SJosef Bacik 	root->fs_info->running_transaction = NULL;
1879a4abeea4SJosef Bacik 	spin_unlock(&root->fs_info->trans_lock);
18807585717fSChris Mason 	mutex_unlock(&root->fs_info->reloc_mutex);
1881b7ec40d7SChris Mason 
1882f9295749SChris Mason 	wake_up(&root->fs_info->transaction_wait);
1883e6dcd2dcSChris Mason 
188479154b1bSChris Mason 	ret = btrfs_write_and_wait_transaction(trans, root);
188549b25e05SJeff Mahoney 	if (ret) {
188649b25e05SJeff Mahoney 		btrfs_error(root->fs_info, ret,
188708748810SDavid Sterba 			    "Error while writing out transaction");
188849b25e05SJeff Mahoney 		mutex_unlock(&root->fs_info->tree_log_mutex);
188949b25e05SJeff Mahoney 		goto cleanup_transaction;
189049b25e05SJeff Mahoney 	}
189149b25e05SJeff Mahoney 
189249b25e05SJeff Mahoney 	ret = write_ctree_super(trans, root, 0);
189349b25e05SJeff Mahoney 	if (ret) {
189449b25e05SJeff Mahoney 		mutex_unlock(&root->fs_info->tree_log_mutex);
189549b25e05SJeff Mahoney 		goto cleanup_transaction;
189649b25e05SJeff Mahoney 	}
18974313b399SChris Mason 
1898e02119d5SChris Mason 	/*
1899e02119d5SChris Mason 	 * the super is written, we can safely allow the tree-loggers
1900e02119d5SChris Mason 	 * to go about their business
1901e02119d5SChris Mason 	 */
1902e02119d5SChris Mason 	mutex_unlock(&root->fs_info->tree_log_mutex);
1903e02119d5SChris Mason 
190411833d66SYan Zheng 	btrfs_finish_extent_commit(trans, root);
19054313b399SChris Mason 
190615ee9bc7SJosef Bacik 	root->fs_info->last_trans_committed = cur_trans->transid;
19074a9d8bdeSMiao Xie 	/*
19084a9d8bdeSMiao Xie 	 * We needn't acquire the lock here because there is no other task
19094a9d8bdeSMiao Xie 	 * which can change it.
19104a9d8bdeSMiao Xie 	 */
19114a9d8bdeSMiao Xie 	cur_trans->state = TRANS_STATE_COMPLETED;
19122c90e5d6SChris Mason 	wake_up(&cur_trans->commit_wait);
19133de4586cSChris Mason 
1914a4abeea4SJosef Bacik 	spin_lock(&root->fs_info->trans_lock);
191513c5a93eSJosef Bacik 	list_del_init(&cur_trans->list);
1916a4abeea4SJosef Bacik 	spin_unlock(&root->fs_info->trans_lock);
1917a4abeea4SJosef Bacik 
1918724e2315SJosef Bacik 	btrfs_put_transaction(cur_trans);
1919724e2315SJosef Bacik 	btrfs_put_transaction(cur_trans);
192058176a96SJosef Bacik 
19210860adfdSMiao Xie 	if (trans->type & __TRANS_FREEZABLE)
1922b2b5ef5cSJan Kara 		sb_end_intwrite(root->fs_info->sb);
1923b2b5ef5cSJan Kara 
19241abe9b8aSliubo 	trace_btrfs_transaction_commit(root);
19251abe9b8aSliubo 
1926a2de733cSArne Jansen 	btrfs_scrub_continue(root);
1927a2de733cSArne Jansen 
19289ed74f2dSJosef Bacik 	if (current->journal_info == trans)
19299ed74f2dSJosef Bacik 		current->journal_info = NULL;
19309ed74f2dSJosef Bacik 
19312c90e5d6SChris Mason 	kmem_cache_free(btrfs_trans_handle_cachep, trans);
193224bbcf04SYan, Zheng 
193324bbcf04SYan, Zheng 	if (current != root->fs_info->transaction_kthread)
193424bbcf04SYan, Zheng 		btrfs_run_delayed_iputs(root);
193524bbcf04SYan, Zheng 
193679154b1bSChris Mason 	return ret;
193749b25e05SJeff Mahoney 
193849b25e05SJeff Mahoney cleanup_transaction:
19390e721106SJosef Bacik 	btrfs_trans_release_metadata(trans, root);
19400e721106SJosef Bacik 	trans->block_rsv = NULL;
1941272d26d0SMiao Xie 	if (trans->qgroup_reserved) {
1942272d26d0SMiao Xie 		btrfs_qgroup_free(root, trans->qgroup_reserved);
1943272d26d0SMiao Xie 		trans->qgroup_reserved = 0;
1944272d26d0SMiao Xie 	}
1945c2cf52ebSSimon Kirby 	btrfs_warn(root->fs_info, "Skipping commit of aborted transaction.");
194649b25e05SJeff Mahoney 	if (current->journal_info == trans)
194749b25e05SJeff Mahoney 		current->journal_info = NULL;
19487b8b92afSJosef Bacik 	cleanup_transaction(trans, root, ret);
194949b25e05SJeff Mahoney 
195049b25e05SJeff Mahoney 	return ret;
195179154b1bSChris Mason }
195279154b1bSChris Mason 
1953d352ac68SChris Mason /*
19549d1a2a3aSDavid Sterba  * return < 0 if error
19559d1a2a3aSDavid Sterba  * 0 if there are no more dead_roots at the time of call
19569d1a2a3aSDavid Sterba  * 1 there are more to be processed, call me again
19579d1a2a3aSDavid Sterba  *
19589d1a2a3aSDavid Sterba  * The return value indicates there are certainly more snapshots to delete, but
19599d1a2a3aSDavid Sterba  * if there comes a new one during processing, it may return 0. We don't mind,
19609d1a2a3aSDavid Sterba  * because btrfs_commit_super will poke cleaner thread and it will process it a
19619d1a2a3aSDavid Sterba  * few seconds later.
1962d352ac68SChris Mason  */
19639d1a2a3aSDavid Sterba int btrfs_clean_one_deleted_snapshot(struct btrfs_root *root)
1964e9d0b13bSChris Mason {
19659d1a2a3aSDavid Sterba 	int ret;
19665d4f98a2SYan Zheng 	struct btrfs_fs_info *fs_info = root->fs_info;
1967e9d0b13bSChris Mason 
1968a4abeea4SJosef Bacik 	spin_lock(&fs_info->trans_lock);
19699d1a2a3aSDavid Sterba 	if (list_empty(&fs_info->dead_roots)) {
19709d1a2a3aSDavid Sterba 		spin_unlock(&fs_info->trans_lock);
19719d1a2a3aSDavid Sterba 		return 0;
19729d1a2a3aSDavid Sterba 	}
19739d1a2a3aSDavid Sterba 	root = list_first_entry(&fs_info->dead_roots,
19749d1a2a3aSDavid Sterba 			struct btrfs_root, root_list);
197518f687d5SWang Shilong 	/*
197618f687d5SWang Shilong 	 * Make sure root is not involved in send,
197718f687d5SWang Shilong 	 * if we fail with first root, we return
197818f687d5SWang Shilong 	 * directly rather than continue.
197918f687d5SWang Shilong 	 */
198018f687d5SWang Shilong 	spin_lock(&root->root_item_lock);
198118f687d5SWang Shilong 	if (root->send_in_progress) {
198218f687d5SWang Shilong 		spin_unlock(&fs_info->trans_lock);
198318f687d5SWang Shilong 		spin_unlock(&root->root_item_lock);
198418f687d5SWang Shilong 		return 0;
198518f687d5SWang Shilong 	}
198618f687d5SWang Shilong 	spin_unlock(&root->root_item_lock);
198718f687d5SWang Shilong 
1988cfad392bSJosef Bacik 	list_del_init(&root->root_list);
1989a4abeea4SJosef Bacik 	spin_unlock(&fs_info->trans_lock);
19905d4f98a2SYan Zheng 
1991efe120a0SFrank Holton 	pr_debug("BTRFS: cleaner removing %llu\n", root->objectid);
199276dda93cSYan, Zheng 
199316cdcec7SMiao Xie 	btrfs_kill_all_delayed_nodes(root);
199416cdcec7SMiao Xie 
199576dda93cSYan, Zheng 	if (btrfs_header_backref_rev(root->node) <
199676dda93cSYan, Zheng 			BTRFS_MIXED_BACKREF_REV)
19972c536799SJeff Mahoney 		ret = btrfs_drop_snapshot(root, NULL, 0, 0);
199876dda93cSYan, Zheng 	else
19992c536799SJeff Mahoney 		ret = btrfs_drop_snapshot(root, NULL, 1, 0);
20009d1a2a3aSDavid Sterba 	/*
20019d1a2a3aSDavid Sterba 	 * If we encounter a transaction abort during snapshot cleaning, we
20029d1a2a3aSDavid Sterba 	 * don't want to crash here
20039d1a2a3aSDavid Sterba 	 */
20046596a928SJosef Bacik 	return (ret < 0) ? 0 : 1;
2005e9d0b13bSChris Mason }
2006