xref: /openbmc/linux/fs/btrfs/transaction.c (revision f45c752b)
1c1d7c514SDavid Sterba // SPDX-License-Identifier: GPL-2.0
26cbd5570SChris Mason /*
36cbd5570SChris Mason  * Copyright (C) 2007 Oracle.  All rights reserved.
46cbd5570SChris Mason  */
56cbd5570SChris Mason 
679154b1bSChris Mason #include <linux/fs.h>
75a0e3ad6STejun Heo #include <linux/slab.h>
834088780SChris Mason #include <linux/sched.h>
9d3c2fdcfSChris Mason #include <linux/writeback.h>
105f39d397SChris Mason #include <linux/pagemap.h>
115f2cc086SChris Mason #include <linux/blkdev.h>
128ea05e3aSAlexander Block #include <linux/uuid.h>
1379154b1bSChris Mason #include "ctree.h"
1479154b1bSChris Mason #include "disk-io.h"
1579154b1bSChris Mason #include "transaction.h"
16925baeddSChris Mason #include "locking.h"
17e02119d5SChris Mason #include "tree-log.h"
18581bb050SLi Zefan #include "inode-map.h"
19733f4fbbSStefan Behrens #include "volumes.h"
208dabb742SStefan Behrens #include "dev-replace.h"
21fcebe456SJosef Bacik #include "qgroup.h"
2279154b1bSChris Mason 
230f7d52f4SChris Mason #define BTRFS_ROOT_TRANS_TAG 0
240f7d52f4SChris Mason 
25e8c9f186SDavid Sterba static const unsigned int btrfs_blocked_trans_types[TRANS_STATE_MAX] = {
264a9d8bdeSMiao Xie 	[TRANS_STATE_RUNNING]		= 0U,
27bcf3a3e7SNikolay Borisov 	[TRANS_STATE_BLOCKED]		=  __TRANS_START,
28bcf3a3e7SNikolay Borisov 	[TRANS_STATE_COMMIT_START]	= (__TRANS_START | __TRANS_ATTACH),
29bcf3a3e7SNikolay Borisov 	[TRANS_STATE_COMMIT_DOING]	= (__TRANS_START |
304a9d8bdeSMiao Xie 					   __TRANS_ATTACH |
314a9d8bdeSMiao Xie 					   __TRANS_JOIN),
32bcf3a3e7SNikolay Borisov 	[TRANS_STATE_UNBLOCKED]		= (__TRANS_START |
334a9d8bdeSMiao Xie 					   __TRANS_ATTACH |
344a9d8bdeSMiao Xie 					   __TRANS_JOIN |
354a9d8bdeSMiao Xie 					   __TRANS_JOIN_NOLOCK),
36bcf3a3e7SNikolay Borisov 	[TRANS_STATE_COMPLETED]		= (__TRANS_START |
374a9d8bdeSMiao Xie 					   __TRANS_ATTACH |
384a9d8bdeSMiao Xie 					   __TRANS_JOIN |
394a9d8bdeSMiao Xie 					   __TRANS_JOIN_NOLOCK),
404a9d8bdeSMiao Xie };
414a9d8bdeSMiao Xie 
42724e2315SJosef Bacik void btrfs_put_transaction(struct btrfs_transaction *transaction)
4379154b1bSChris Mason {
449b64f57dSElena Reshetova 	WARN_ON(refcount_read(&transaction->use_count) == 0);
459b64f57dSElena Reshetova 	if (refcount_dec_and_test(&transaction->use_count)) {
46a4abeea4SJosef Bacik 		BUG_ON(!list_empty(&transaction->list));
475c9d028bSLiu Bo 		WARN_ON(!RB_EMPTY_ROOT(
485c9d028bSLiu Bo 				&transaction->delayed_refs.href_root.rb_root));
491262133bSJosef Bacik 		if (transaction->delayed_refs.pending_csums)
50ab8d0fc4SJeff Mahoney 			btrfs_err(transaction->fs_info,
51ab8d0fc4SJeff Mahoney 				  "pending csums is %llu",
521262133bSJosef Bacik 				  transaction->delayed_refs.pending_csums);
536df9a95eSJosef Bacik 		while (!list_empty(&transaction->pending_chunks)) {
546df9a95eSJosef Bacik 			struct extent_map *em;
556df9a95eSJosef Bacik 
566df9a95eSJosef Bacik 			em = list_first_entry(&transaction->pending_chunks,
576df9a95eSJosef Bacik 					      struct extent_map, list);
586df9a95eSJosef Bacik 			list_del_init(&em->list);
596df9a95eSJosef Bacik 			free_extent_map(em);
606df9a95eSJosef Bacik 		}
617785a663SFilipe Manana 		/*
627785a663SFilipe Manana 		 * If any block groups are found in ->deleted_bgs then it's
637785a663SFilipe Manana 		 * because the transaction was aborted and a commit did not
647785a663SFilipe Manana 		 * happen (things failed before writing the new superblock
657785a663SFilipe Manana 		 * and calling btrfs_finish_extent_commit()), so we can not
667785a663SFilipe Manana 		 * discard the physical locations of the block groups.
677785a663SFilipe Manana 		 */
687785a663SFilipe Manana 		while (!list_empty(&transaction->deleted_bgs)) {
697785a663SFilipe Manana 			struct btrfs_block_group_cache *cache;
707785a663SFilipe Manana 
717785a663SFilipe Manana 			cache = list_first_entry(&transaction->deleted_bgs,
727785a663SFilipe Manana 						 struct btrfs_block_group_cache,
737785a663SFilipe Manana 						 bg_list);
747785a663SFilipe Manana 			list_del_init(&cache->bg_list);
757785a663SFilipe Manana 			btrfs_put_block_group_trimming(cache);
767785a663SFilipe Manana 			btrfs_put_block_group(cache);
777785a663SFilipe Manana 		}
784b5faeacSDavid Sterba 		kfree(transaction);
7979154b1bSChris Mason 	}
8078fae27eSChris Mason }
8179154b1bSChris Mason 
82663dfbb0SFilipe Manana static void clear_btree_io_tree(struct extent_io_tree *tree)
83663dfbb0SFilipe Manana {
84663dfbb0SFilipe Manana 	spin_lock(&tree->lock);
85b666a9cdSDavid Sterba 	/*
86b666a9cdSDavid Sterba 	 * Do a single barrier for the waitqueue_active check here, the state
87b666a9cdSDavid Sterba 	 * of the waitqueue should not change once clear_btree_io_tree is
88b666a9cdSDavid Sterba 	 * called.
89b666a9cdSDavid Sterba 	 */
90b666a9cdSDavid Sterba 	smp_mb();
91663dfbb0SFilipe Manana 	while (!RB_EMPTY_ROOT(&tree->state)) {
92663dfbb0SFilipe Manana 		struct rb_node *node;
93663dfbb0SFilipe Manana 		struct extent_state *state;
94663dfbb0SFilipe Manana 
95663dfbb0SFilipe Manana 		node = rb_first(&tree->state);
96663dfbb0SFilipe Manana 		state = rb_entry(node, struct extent_state, rb_node);
97663dfbb0SFilipe Manana 		rb_erase(&state->rb_node, &tree->state);
98663dfbb0SFilipe Manana 		RB_CLEAR_NODE(&state->rb_node);
99663dfbb0SFilipe Manana 		/*
100663dfbb0SFilipe Manana 		 * btree io trees aren't supposed to have tasks waiting for
101663dfbb0SFilipe Manana 		 * changes in the flags of extent states ever.
102663dfbb0SFilipe Manana 		 */
103663dfbb0SFilipe Manana 		ASSERT(!waitqueue_active(&state->wq));
104663dfbb0SFilipe Manana 		free_extent_state(state);
105351810c1SDavid Sterba 
106351810c1SDavid Sterba 		cond_resched_lock(&tree->lock);
107663dfbb0SFilipe Manana 	}
108663dfbb0SFilipe Manana 	spin_unlock(&tree->lock);
109663dfbb0SFilipe Manana }
110663dfbb0SFilipe Manana 
11116916a88SNikolay Borisov static noinline void switch_commit_roots(struct btrfs_transaction *trans)
112817d52f8SJosef Bacik {
11316916a88SNikolay Borisov 	struct btrfs_fs_info *fs_info = trans->fs_info;
1149e351cc8SJosef Bacik 	struct btrfs_root *root, *tmp;
1159e351cc8SJosef Bacik 
1169e351cc8SJosef Bacik 	down_write(&fs_info->commit_root_sem);
1179e351cc8SJosef Bacik 	list_for_each_entry_safe(root, tmp, &trans->switch_commits,
1189e351cc8SJosef Bacik 				 dirty_list) {
1199e351cc8SJosef Bacik 		list_del_init(&root->dirty_list);
120817d52f8SJosef Bacik 		free_extent_buffer(root->commit_root);
121817d52f8SJosef Bacik 		root->commit_root = btrfs_root_node(root);
1224fd786e6SMisono Tomohiro 		if (is_fstree(root->root_key.objectid))
1239e351cc8SJosef Bacik 			btrfs_unpin_free_ino(root);
124663dfbb0SFilipe Manana 		clear_btree_io_tree(&root->dirty_log_pages);
1259e351cc8SJosef Bacik 	}
1262b9dbef2SJosef Bacik 
1272b9dbef2SJosef Bacik 	/* We can free old roots now. */
1282b9dbef2SJosef Bacik 	spin_lock(&trans->dropped_roots_lock);
1292b9dbef2SJosef Bacik 	while (!list_empty(&trans->dropped_roots)) {
1302b9dbef2SJosef Bacik 		root = list_first_entry(&trans->dropped_roots,
1312b9dbef2SJosef Bacik 					struct btrfs_root, root_list);
1322b9dbef2SJosef Bacik 		list_del_init(&root->root_list);
1332b9dbef2SJosef Bacik 		spin_unlock(&trans->dropped_roots_lock);
1342b9dbef2SJosef Bacik 		btrfs_drop_and_free_fs_root(fs_info, root);
1352b9dbef2SJosef Bacik 		spin_lock(&trans->dropped_roots_lock);
1362b9dbef2SJosef Bacik 	}
1372b9dbef2SJosef Bacik 	spin_unlock(&trans->dropped_roots_lock);
1389e351cc8SJosef Bacik 	up_write(&fs_info->commit_root_sem);
139817d52f8SJosef Bacik }
140817d52f8SJosef Bacik 
1410860adfdSMiao Xie static inline void extwriter_counter_inc(struct btrfs_transaction *trans,
1420860adfdSMiao Xie 					 unsigned int type)
1430860adfdSMiao Xie {
1440860adfdSMiao Xie 	if (type & TRANS_EXTWRITERS)
1450860adfdSMiao Xie 		atomic_inc(&trans->num_extwriters);
1460860adfdSMiao Xie }
1470860adfdSMiao Xie 
1480860adfdSMiao Xie static inline void extwriter_counter_dec(struct btrfs_transaction *trans,
1490860adfdSMiao Xie 					 unsigned int type)
1500860adfdSMiao Xie {
1510860adfdSMiao Xie 	if (type & TRANS_EXTWRITERS)
1520860adfdSMiao Xie 		atomic_dec(&trans->num_extwriters);
1530860adfdSMiao Xie }
1540860adfdSMiao Xie 
1550860adfdSMiao Xie static inline void extwriter_counter_init(struct btrfs_transaction *trans,
1560860adfdSMiao Xie 					  unsigned int type)
1570860adfdSMiao Xie {
1580860adfdSMiao Xie 	atomic_set(&trans->num_extwriters, ((type & TRANS_EXTWRITERS) ? 1 : 0));
1590860adfdSMiao Xie }
1600860adfdSMiao Xie 
1610860adfdSMiao Xie static inline int extwriter_counter_read(struct btrfs_transaction *trans)
1620860adfdSMiao Xie {
1630860adfdSMiao Xie 	return atomic_read(&trans->num_extwriters);
164178260b2SMiao Xie }
165178260b2SMiao Xie 
166d352ac68SChris Mason /*
167d352ac68SChris Mason  * either allocate a new transaction or hop into the existing one
168d352ac68SChris Mason  */
1692ff7e61eSJeff Mahoney static noinline int join_transaction(struct btrfs_fs_info *fs_info,
1702ff7e61eSJeff Mahoney 				     unsigned int type)
17179154b1bSChris Mason {
17279154b1bSChris Mason 	struct btrfs_transaction *cur_trans;
173a4abeea4SJosef Bacik 
17419ae4e81SJan Schmidt 	spin_lock(&fs_info->trans_lock);
175d43317dcSChris Mason loop:
17649b25e05SJeff Mahoney 	/* The file system has been taken offline. No new transactions. */
17787533c47SMiao Xie 	if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) {
17819ae4e81SJan Schmidt 		spin_unlock(&fs_info->trans_lock);
17949b25e05SJeff Mahoney 		return -EROFS;
18049b25e05SJeff Mahoney 	}
18149b25e05SJeff Mahoney 
18219ae4e81SJan Schmidt 	cur_trans = fs_info->running_transaction;
183a4abeea4SJosef Bacik 	if (cur_trans) {
184871383beSDavid Sterba 		if (cur_trans->aborted) {
18519ae4e81SJan Schmidt 			spin_unlock(&fs_info->trans_lock);
18649b25e05SJeff Mahoney 			return cur_trans->aborted;
187871383beSDavid Sterba 		}
1884a9d8bdeSMiao Xie 		if (btrfs_blocked_trans_types[cur_trans->state] & type) {
189178260b2SMiao Xie 			spin_unlock(&fs_info->trans_lock);
190178260b2SMiao Xie 			return -EBUSY;
191178260b2SMiao Xie 		}
1929b64f57dSElena Reshetova 		refcount_inc(&cur_trans->use_count);
193a4abeea4SJosef Bacik 		atomic_inc(&cur_trans->num_writers);
1940860adfdSMiao Xie 		extwriter_counter_inc(cur_trans, type);
19519ae4e81SJan Schmidt 		spin_unlock(&fs_info->trans_lock);
196a4abeea4SJosef Bacik 		return 0;
197a4abeea4SJosef Bacik 	}
19819ae4e81SJan Schmidt 	spin_unlock(&fs_info->trans_lock);
199a4abeea4SJosef Bacik 
200354aa0fbSMiao Xie 	/*
201354aa0fbSMiao Xie 	 * If we are ATTACH, we just want to catch the current transaction,
202354aa0fbSMiao Xie 	 * and commit it. If there is no transaction, just return ENOENT.
203354aa0fbSMiao Xie 	 */
204354aa0fbSMiao Xie 	if (type == TRANS_ATTACH)
205354aa0fbSMiao Xie 		return -ENOENT;
206354aa0fbSMiao Xie 
2074a9d8bdeSMiao Xie 	/*
2084a9d8bdeSMiao Xie 	 * JOIN_NOLOCK only happens during the transaction commit, so
2094a9d8bdeSMiao Xie 	 * it is impossible that ->running_transaction is NULL
2104a9d8bdeSMiao Xie 	 */
2114a9d8bdeSMiao Xie 	BUG_ON(type == TRANS_JOIN_NOLOCK);
2124a9d8bdeSMiao Xie 
2134b5faeacSDavid Sterba 	cur_trans = kmalloc(sizeof(*cur_trans), GFP_NOFS);
214db5b493aSTsutomu Itoh 	if (!cur_trans)
215db5b493aSTsutomu Itoh 		return -ENOMEM;
216d43317dcSChris Mason 
21719ae4e81SJan Schmidt 	spin_lock(&fs_info->trans_lock);
21819ae4e81SJan Schmidt 	if (fs_info->running_transaction) {
219d43317dcSChris Mason 		/*
220d43317dcSChris Mason 		 * someone started a transaction after we unlocked.  Make sure
2214a9d8bdeSMiao Xie 		 * to redo the checks above
222d43317dcSChris Mason 		 */
2234b5faeacSDavid Sterba 		kfree(cur_trans);
224d43317dcSChris Mason 		goto loop;
22587533c47SMiao Xie 	} else if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) {
226e4b50e14SDan Carpenter 		spin_unlock(&fs_info->trans_lock);
2274b5faeacSDavid Sterba 		kfree(cur_trans);
2287b8b92afSJosef Bacik 		return -EROFS;
229a4abeea4SJosef Bacik 	}
230d43317dcSChris Mason 
231ab8d0fc4SJeff Mahoney 	cur_trans->fs_info = fs_info;
23213c5a93eSJosef Bacik 	atomic_set(&cur_trans->num_writers, 1);
2330860adfdSMiao Xie 	extwriter_counter_init(cur_trans, type);
23479154b1bSChris Mason 	init_waitqueue_head(&cur_trans->writer_wait);
23579154b1bSChris Mason 	init_waitqueue_head(&cur_trans->commit_wait);
236161c3549SJosef Bacik 	init_waitqueue_head(&cur_trans->pending_wait);
2374a9d8bdeSMiao Xie 	cur_trans->state = TRANS_STATE_RUNNING;
238a4abeea4SJosef Bacik 	/*
239a4abeea4SJosef Bacik 	 * One for this trans handle, one so it will live on until we
240a4abeea4SJosef Bacik 	 * commit the transaction.
241a4abeea4SJosef Bacik 	 */
2429b64f57dSElena Reshetova 	refcount_set(&cur_trans->use_count, 2);
243161c3549SJosef Bacik 	atomic_set(&cur_trans->pending_ordered, 0);
2443204d33cSJosef Bacik 	cur_trans->flags = 0;
245afd48513SArnd Bergmann 	cur_trans->start_time = ktime_get_seconds();
24656bec294SChris Mason 
247a099d0fdSAlexandru Moise 	memset(&cur_trans->delayed_refs, 0, sizeof(cur_trans->delayed_refs));
248a099d0fdSAlexandru Moise 
2495c9d028bSLiu Bo 	cur_trans->delayed_refs.href_root = RB_ROOT_CACHED;
2503368d001SQu Wenruo 	cur_trans->delayed_refs.dirty_extent_root = RB_ROOT;
251d7df2c79SJosef Bacik 	atomic_set(&cur_trans->delayed_refs.num_entries, 0);
25220b297d6SJan Schmidt 
25320b297d6SJan Schmidt 	/*
25420b297d6SJan Schmidt 	 * although the tree mod log is per file system and not per transaction,
25520b297d6SJan Schmidt 	 * the log must never go across transaction boundaries.
25620b297d6SJan Schmidt 	 */
25720b297d6SJan Schmidt 	smp_mb();
25831b1a2bdSJulia Lawall 	if (!list_empty(&fs_info->tree_mod_seq_list))
2595d163e0eSJeff Mahoney 		WARN(1, KERN_ERR "BTRFS: tree_mod_seq_list not empty when creating a fresh transaction\n");
26031b1a2bdSJulia Lawall 	if (!RB_EMPTY_ROOT(&fs_info->tree_mod_log))
2615d163e0eSJeff Mahoney 		WARN(1, KERN_ERR "BTRFS: tree_mod_log rb tree not empty when creating a fresh transaction\n");
262fc36ed7eSJan Schmidt 	atomic64_set(&fs_info->tree_mod_seq, 0);
26320b297d6SJan Schmidt 
26456bec294SChris Mason 	spin_lock_init(&cur_trans->delayed_refs.lock);
26556bec294SChris Mason 
2663063d29fSChris Mason 	INIT_LIST_HEAD(&cur_trans->pending_snapshots);
2676df9a95eSJosef Bacik 	INIT_LIST_HEAD(&cur_trans->pending_chunks);
2689e351cc8SJosef Bacik 	INIT_LIST_HEAD(&cur_trans->switch_commits);
269ce93ec54SJosef Bacik 	INIT_LIST_HEAD(&cur_trans->dirty_bgs);
2701bbc621eSChris Mason 	INIT_LIST_HEAD(&cur_trans->io_bgs);
2712b9dbef2SJosef Bacik 	INIT_LIST_HEAD(&cur_trans->dropped_roots);
2721bbc621eSChris Mason 	mutex_init(&cur_trans->cache_write_mutex);
273cb723e49SJosef Bacik 	cur_trans->num_dirty_bgs = 0;
274ce93ec54SJosef Bacik 	spin_lock_init(&cur_trans->dirty_bgs_lock);
275e33e17eeSJeff Mahoney 	INIT_LIST_HEAD(&cur_trans->deleted_bgs);
2762b9dbef2SJosef Bacik 	spin_lock_init(&cur_trans->dropped_roots_lock);
27719ae4e81SJan Schmidt 	list_add_tail(&cur_trans->list, &fs_info->trans_list);
278d1310b2eSChris Mason 	extent_io_tree_init(&cur_trans->dirty_pages,
279c6100a4bSJosef Bacik 			     fs_info->btree_inode);
28019ae4e81SJan Schmidt 	fs_info->generation++;
28119ae4e81SJan Schmidt 	cur_trans->transid = fs_info->generation;
28219ae4e81SJan Schmidt 	fs_info->running_transaction = cur_trans;
28349b25e05SJeff Mahoney 	cur_trans->aborted = 0;
28419ae4e81SJan Schmidt 	spin_unlock(&fs_info->trans_lock);
28515ee9bc7SJosef Bacik 
28679154b1bSChris Mason 	return 0;
28779154b1bSChris Mason }
28879154b1bSChris Mason 
289d352ac68SChris Mason /*
290d397712bSChris Mason  * this does all the record keeping required to make sure that a reference
291d397712bSChris Mason  * counted root is properly recorded in a given transaction.  This is required
292d397712bSChris Mason  * to make sure the old root from before we joined the transaction is deleted
293d397712bSChris Mason  * when the transaction commits
294d352ac68SChris Mason  */
2957585717fSChris Mason static int record_root_in_trans(struct btrfs_trans_handle *trans,
2966426c7adSQu Wenruo 			       struct btrfs_root *root,
2976426c7adSQu Wenruo 			       int force)
2986702ed49SChris Mason {
2990b246afaSJeff Mahoney 	struct btrfs_fs_info *fs_info = root->fs_info;
3000b246afaSJeff Mahoney 
3016426c7adSQu Wenruo 	if ((test_bit(BTRFS_ROOT_REF_COWS, &root->state) &&
3026426c7adSQu Wenruo 	    root->last_trans < trans->transid) || force) {
3030b246afaSJeff Mahoney 		WARN_ON(root == fs_info->extent_root);
3044d31778aSQu Wenruo 		WARN_ON(!force && root->commit_root != root->node);
3055d4f98a2SYan Zheng 
3067585717fSChris Mason 		/*
30727cdeb70SMiao Xie 		 * see below for IN_TRANS_SETUP usage rules
3087585717fSChris Mason 		 * we have the reloc mutex held now, so there
3097585717fSChris Mason 		 * is only one writer in this function
3107585717fSChris Mason 		 */
31127cdeb70SMiao Xie 		set_bit(BTRFS_ROOT_IN_TRANS_SETUP, &root->state);
3127585717fSChris Mason 
31327cdeb70SMiao Xie 		/* make sure readers find IN_TRANS_SETUP before
3147585717fSChris Mason 		 * they find our root->last_trans update
3157585717fSChris Mason 		 */
3167585717fSChris Mason 		smp_wmb();
3177585717fSChris Mason 
3180b246afaSJeff Mahoney 		spin_lock(&fs_info->fs_roots_radix_lock);
3196426c7adSQu Wenruo 		if (root->last_trans == trans->transid && !force) {
3200b246afaSJeff Mahoney 			spin_unlock(&fs_info->fs_roots_radix_lock);
321a4abeea4SJosef Bacik 			return 0;
322a4abeea4SJosef Bacik 		}
3230b246afaSJeff Mahoney 		radix_tree_tag_set(&fs_info->fs_roots_radix,
3246702ed49SChris Mason 				   (unsigned long)root->root_key.objectid,
3256702ed49SChris Mason 				   BTRFS_ROOT_TRANS_TAG);
3260b246afaSJeff Mahoney 		spin_unlock(&fs_info->fs_roots_radix_lock);
3277585717fSChris Mason 		root->last_trans = trans->transid;
3287585717fSChris Mason 
3297585717fSChris Mason 		/* this is pretty tricky.  We don't want to
3307585717fSChris Mason 		 * take the relocation lock in btrfs_record_root_in_trans
3317585717fSChris Mason 		 * unless we're really doing the first setup for this root in
3327585717fSChris Mason 		 * this transaction.
3337585717fSChris Mason 		 *
3347585717fSChris Mason 		 * Normally we'd use root->last_trans as a flag to decide
3357585717fSChris Mason 		 * if we want to take the expensive mutex.
3367585717fSChris Mason 		 *
3377585717fSChris Mason 		 * But, we have to set root->last_trans before we
3387585717fSChris Mason 		 * init the relocation root, otherwise, we trip over warnings
3397585717fSChris Mason 		 * in ctree.c.  The solution used here is to flag ourselves
34027cdeb70SMiao Xie 		 * with root IN_TRANS_SETUP.  When this is 1, we're still
3417585717fSChris Mason 		 * fixing up the reloc trees and everyone must wait.
3427585717fSChris Mason 		 *
3437585717fSChris Mason 		 * When this is zero, they can trust root->last_trans and fly
3447585717fSChris Mason 		 * through btrfs_record_root_in_trans without having to take the
3457585717fSChris Mason 		 * lock.  smp_wmb() makes sure that all the writes above are
3467585717fSChris Mason 		 * done before we pop in the zero below
3477585717fSChris Mason 		 */
3485d4f98a2SYan Zheng 		btrfs_init_reloc_root(trans, root);
349c7548af6SChris Mason 		smp_mb__before_atomic();
35027cdeb70SMiao Xie 		clear_bit(BTRFS_ROOT_IN_TRANS_SETUP, &root->state);
3516702ed49SChris Mason 	}
3525d4f98a2SYan Zheng 	return 0;
3536702ed49SChris Mason }
3545d4f98a2SYan Zheng 
3557585717fSChris Mason 
3562b9dbef2SJosef Bacik void btrfs_add_dropped_root(struct btrfs_trans_handle *trans,
3572b9dbef2SJosef Bacik 			    struct btrfs_root *root)
3582b9dbef2SJosef Bacik {
3590b246afaSJeff Mahoney 	struct btrfs_fs_info *fs_info = root->fs_info;
3602b9dbef2SJosef Bacik 	struct btrfs_transaction *cur_trans = trans->transaction;
3612b9dbef2SJosef Bacik 
3622b9dbef2SJosef Bacik 	/* Add ourselves to the transaction dropped list */
3632b9dbef2SJosef Bacik 	spin_lock(&cur_trans->dropped_roots_lock);
3642b9dbef2SJosef Bacik 	list_add_tail(&root->root_list, &cur_trans->dropped_roots);
3652b9dbef2SJosef Bacik 	spin_unlock(&cur_trans->dropped_roots_lock);
3662b9dbef2SJosef Bacik 
3672b9dbef2SJosef Bacik 	/* Make sure we don't try to update the root at commit time */
3680b246afaSJeff Mahoney 	spin_lock(&fs_info->fs_roots_radix_lock);
3690b246afaSJeff Mahoney 	radix_tree_tag_clear(&fs_info->fs_roots_radix,
3702b9dbef2SJosef Bacik 			     (unsigned long)root->root_key.objectid,
3712b9dbef2SJosef Bacik 			     BTRFS_ROOT_TRANS_TAG);
3720b246afaSJeff Mahoney 	spin_unlock(&fs_info->fs_roots_radix_lock);
3732b9dbef2SJosef Bacik }
3742b9dbef2SJosef Bacik 
3757585717fSChris Mason int btrfs_record_root_in_trans(struct btrfs_trans_handle *trans,
3767585717fSChris Mason 			       struct btrfs_root *root)
3777585717fSChris Mason {
3780b246afaSJeff Mahoney 	struct btrfs_fs_info *fs_info = root->fs_info;
3790b246afaSJeff Mahoney 
38027cdeb70SMiao Xie 	if (!test_bit(BTRFS_ROOT_REF_COWS, &root->state))
3817585717fSChris Mason 		return 0;
3827585717fSChris Mason 
3837585717fSChris Mason 	/*
38427cdeb70SMiao Xie 	 * see record_root_in_trans for comments about IN_TRANS_SETUP usage
3857585717fSChris Mason 	 * and barriers
3867585717fSChris Mason 	 */
3877585717fSChris Mason 	smp_rmb();
3887585717fSChris Mason 	if (root->last_trans == trans->transid &&
38927cdeb70SMiao Xie 	    !test_bit(BTRFS_ROOT_IN_TRANS_SETUP, &root->state))
3907585717fSChris Mason 		return 0;
3917585717fSChris Mason 
3920b246afaSJeff Mahoney 	mutex_lock(&fs_info->reloc_mutex);
3936426c7adSQu Wenruo 	record_root_in_trans(trans, root, 0);
3940b246afaSJeff Mahoney 	mutex_unlock(&fs_info->reloc_mutex);
3957585717fSChris Mason 
3967585717fSChris Mason 	return 0;
3977585717fSChris Mason }
3987585717fSChris Mason 
3994a9d8bdeSMiao Xie static inline int is_transaction_blocked(struct btrfs_transaction *trans)
4004a9d8bdeSMiao Xie {
4014a9d8bdeSMiao Xie 	return (trans->state >= TRANS_STATE_BLOCKED &&
402501407aaSJosef Bacik 		trans->state < TRANS_STATE_UNBLOCKED &&
403501407aaSJosef Bacik 		!trans->aborted);
4044a9d8bdeSMiao Xie }
4054a9d8bdeSMiao Xie 
406d352ac68SChris Mason /* wait for commit against the current transaction to become unblocked
407d352ac68SChris Mason  * when this is done, it is safe to start a new transaction, but the current
408d352ac68SChris Mason  * transaction might not be fully on disk.
409d352ac68SChris Mason  */
4102ff7e61eSJeff Mahoney static void wait_current_trans(struct btrfs_fs_info *fs_info)
41179154b1bSChris Mason {
412f9295749SChris Mason 	struct btrfs_transaction *cur_trans;
41379154b1bSChris Mason 
4140b246afaSJeff Mahoney 	spin_lock(&fs_info->trans_lock);
4150b246afaSJeff Mahoney 	cur_trans = fs_info->running_transaction;
4164a9d8bdeSMiao Xie 	if (cur_trans && is_transaction_blocked(cur_trans)) {
4179b64f57dSElena Reshetova 		refcount_inc(&cur_trans->use_count);
4180b246afaSJeff Mahoney 		spin_unlock(&fs_info->trans_lock);
41972d63ed6SLi Zefan 
4200b246afaSJeff Mahoney 		wait_event(fs_info->transaction_wait,
421501407aaSJosef Bacik 			   cur_trans->state >= TRANS_STATE_UNBLOCKED ||
422501407aaSJosef Bacik 			   cur_trans->aborted);
423724e2315SJosef Bacik 		btrfs_put_transaction(cur_trans);
424a4abeea4SJosef Bacik 	} else {
4250b246afaSJeff Mahoney 		spin_unlock(&fs_info->trans_lock);
426f9295749SChris Mason 	}
42737d1aeeeSChris Mason }
42837d1aeeeSChris Mason 
4292ff7e61eSJeff Mahoney static int may_wait_transaction(struct btrfs_fs_info *fs_info, int type)
43037d1aeeeSChris Mason {
4310b246afaSJeff Mahoney 	if (test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags))
432a4abeea4SJosef Bacik 		return 0;
433a4abeea4SJosef Bacik 
43492e2f7e3SNikolay Borisov 	if (type == TRANS_START)
435a4abeea4SJosef Bacik 		return 1;
436a4abeea4SJosef Bacik 
437a22285a6SYan, Zheng 	return 0;
438a22285a6SYan, Zheng }
439a22285a6SYan, Zheng 
44020dd2cbfSMiao Xie static inline bool need_reserve_reloc_root(struct btrfs_root *root)
44120dd2cbfSMiao Xie {
4420b246afaSJeff Mahoney 	struct btrfs_fs_info *fs_info = root->fs_info;
4430b246afaSJeff Mahoney 
4440b246afaSJeff Mahoney 	if (!fs_info->reloc_ctl ||
44527cdeb70SMiao Xie 	    !test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
44620dd2cbfSMiao Xie 	    root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID ||
44720dd2cbfSMiao Xie 	    root->reloc_root)
44820dd2cbfSMiao Xie 		return false;
44920dd2cbfSMiao Xie 
45020dd2cbfSMiao Xie 	return true;
45120dd2cbfSMiao Xie }
45220dd2cbfSMiao Xie 
45308e007d2SMiao Xie static struct btrfs_trans_handle *
4545aed1dd8SAlexandru Moise start_transaction(struct btrfs_root *root, unsigned int num_items,
455003d7c59SJeff Mahoney 		  unsigned int type, enum btrfs_reserve_flush_enum flush,
456003d7c59SJeff Mahoney 		  bool enforce_qgroups)
457a22285a6SYan, Zheng {
4580b246afaSJeff Mahoney 	struct btrfs_fs_info *fs_info = root->fs_info;
4590b246afaSJeff Mahoney 
460a22285a6SYan, Zheng 	struct btrfs_trans_handle *h;
461a22285a6SYan, Zheng 	struct btrfs_transaction *cur_trans;
462b5009945SJosef Bacik 	u64 num_bytes = 0;
463c5567237SArne Jansen 	u64 qgroup_reserved = 0;
46420dd2cbfSMiao Xie 	bool reloc_reserved = false;
46520dd2cbfSMiao Xie 	int ret;
466acce952bSliubo 
46746c4e71eSFilipe Manana 	/* Send isn't supposed to start transactions. */
4682755a0deSDavid Sterba 	ASSERT(current->journal_info != BTRFS_SEND_TRANS_STUB);
46946c4e71eSFilipe Manana 
4700b246afaSJeff Mahoney 	if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
471acce952bSliubo 		return ERR_PTR(-EROFS);
4722a1eb461SJosef Bacik 
47346c4e71eSFilipe Manana 	if (current->journal_info) {
4740860adfdSMiao Xie 		WARN_ON(type & TRANS_EXTWRITERS);
4752a1eb461SJosef Bacik 		h = current->journal_info;
476b50fff81SDavid Sterba 		refcount_inc(&h->use_count);
477b50fff81SDavid Sterba 		WARN_ON(refcount_read(&h->use_count) > 2);
4782a1eb461SJosef Bacik 		h->orig_rsv = h->block_rsv;
4792a1eb461SJosef Bacik 		h->block_rsv = NULL;
4802a1eb461SJosef Bacik 		goto got_it;
4812a1eb461SJosef Bacik 	}
482b5009945SJosef Bacik 
483b5009945SJosef Bacik 	/*
484b5009945SJosef Bacik 	 * Do the reservation before we join the transaction so we can do all
485b5009945SJosef Bacik 	 * the appropriate flushing if need be.
486b5009945SJosef Bacik 	 */
487003d7c59SJeff Mahoney 	if (num_items && root != fs_info->chunk_root) {
4880b246afaSJeff Mahoney 		qgroup_reserved = num_items * fs_info->nodesize;
489733e03a0SQu Wenruo 		ret = btrfs_qgroup_reserve_meta_pertrans(root, qgroup_reserved,
490003d7c59SJeff Mahoney 				enforce_qgroups);
491c5567237SArne Jansen 		if (ret)
492c5567237SArne Jansen 			return ERR_PTR(ret);
493c5567237SArne Jansen 
4940b246afaSJeff Mahoney 		num_bytes = btrfs_calc_trans_metadata_size(fs_info, num_items);
49520dd2cbfSMiao Xie 		/*
49620dd2cbfSMiao Xie 		 * Do the reservation for the relocation root creation
49720dd2cbfSMiao Xie 		 */
498ee39b432SDavid Sterba 		if (need_reserve_reloc_root(root)) {
4990b246afaSJeff Mahoney 			num_bytes += fs_info->nodesize;
50020dd2cbfSMiao Xie 			reloc_reserved = true;
50120dd2cbfSMiao Xie 		}
50220dd2cbfSMiao Xie 
5030b246afaSJeff Mahoney 		ret = btrfs_block_rsv_add(root, &fs_info->trans_block_rsv,
50408e007d2SMiao Xie 					  num_bytes, flush);
505b5009945SJosef Bacik 		if (ret)
506843fcf35SMiao Xie 			goto reserve_fail;
507b5009945SJosef Bacik 	}
508a22285a6SYan, Zheng again:
509f2f767e7SAlexandru Moise 	h = kmem_cache_zalloc(btrfs_trans_handle_cachep, GFP_NOFS);
510843fcf35SMiao Xie 	if (!h) {
511843fcf35SMiao Xie 		ret = -ENOMEM;
512843fcf35SMiao Xie 		goto alloc_fail;
513843fcf35SMiao Xie 	}
514a22285a6SYan, Zheng 
51598114659SJosef Bacik 	/*
51698114659SJosef Bacik 	 * If we are JOIN_NOLOCK we're already committing a transaction and
51798114659SJosef Bacik 	 * waiting on this guy, so we don't need to do the sb_start_intwrite
51898114659SJosef Bacik 	 * because we're already holding a ref.  We need this because we could
51998114659SJosef Bacik 	 * have raced in and did an fsync() on a file which can kick a commit
52098114659SJosef Bacik 	 * and then we deadlock with somebody doing a freeze.
521354aa0fbSMiao Xie 	 *
522354aa0fbSMiao Xie 	 * If we are ATTACH, it means we just want to catch the current
523354aa0fbSMiao Xie 	 * transaction and commit it, so we needn't do sb_start_intwrite().
52498114659SJosef Bacik 	 */
5250860adfdSMiao Xie 	if (type & __TRANS_FREEZABLE)
5260b246afaSJeff Mahoney 		sb_start_intwrite(fs_info->sb);
527b2b5ef5cSJan Kara 
5282ff7e61eSJeff Mahoney 	if (may_wait_transaction(fs_info, type))
5292ff7e61eSJeff Mahoney 		wait_current_trans(fs_info);
530a22285a6SYan, Zheng 
531a4abeea4SJosef Bacik 	do {
5322ff7e61eSJeff Mahoney 		ret = join_transaction(fs_info, type);
533178260b2SMiao Xie 		if (ret == -EBUSY) {
5342ff7e61eSJeff Mahoney 			wait_current_trans(fs_info);
535178260b2SMiao Xie 			if (unlikely(type == TRANS_ATTACH))
536178260b2SMiao Xie 				ret = -ENOENT;
537178260b2SMiao Xie 		}
538a4abeea4SJosef Bacik 	} while (ret == -EBUSY);
539a4abeea4SJosef Bacik 
540a43f7f82SLiu Bo 	if (ret < 0)
541843fcf35SMiao Xie 		goto join_fail;
5420f7d52f4SChris Mason 
5430b246afaSJeff Mahoney 	cur_trans = fs_info->running_transaction;
544a22285a6SYan, Zheng 
545a22285a6SYan, Zheng 	h->transid = cur_trans->transid;
546a22285a6SYan, Zheng 	h->transaction = cur_trans;
547d13603efSArne Jansen 	h->root = root;
548b50fff81SDavid Sterba 	refcount_set(&h->use_count, 1);
54964b63580SJeff Mahoney 	h->fs_info = root->fs_info;
5507174109cSQu Wenruo 
551a698d075SMiao Xie 	h->type = type;
552d9a0540aSFilipe Manana 	h->can_flush_pending_bgs = true;
553ea658badSJosef Bacik 	INIT_LIST_HEAD(&h->new_bgs);
554b7ec40d7SChris Mason 
555a22285a6SYan, Zheng 	smp_mb();
5564a9d8bdeSMiao Xie 	if (cur_trans->state >= TRANS_STATE_BLOCKED &&
5572ff7e61eSJeff Mahoney 	    may_wait_transaction(fs_info, type)) {
558abdd2e80SFilipe Manana 		current->journal_info = h;
5593a45bb20SJeff Mahoney 		btrfs_commit_transaction(h);
560a22285a6SYan, Zheng 		goto again;
561a22285a6SYan, Zheng 	}
5629ed74f2dSJosef Bacik 
563b5009945SJosef Bacik 	if (num_bytes) {
5640b246afaSJeff Mahoney 		trace_btrfs_space_reservation(fs_info, "transaction",
5652bcc0328SLiu Bo 					      h->transid, num_bytes, 1);
5660b246afaSJeff Mahoney 		h->block_rsv = &fs_info->trans_block_rsv;
567b5009945SJosef Bacik 		h->bytes_reserved = num_bytes;
56820dd2cbfSMiao Xie 		h->reloc_reserved = reloc_reserved;
569a22285a6SYan, Zheng 	}
570a22285a6SYan, Zheng 
5712a1eb461SJosef Bacik got_it:
572a4abeea4SJosef Bacik 	btrfs_record_root_in_trans(h, root);
573a22285a6SYan, Zheng 
574bcf3a3e7SNikolay Borisov 	if (!current->journal_info)
575a22285a6SYan, Zheng 		current->journal_info = h;
57679154b1bSChris Mason 	return h;
577843fcf35SMiao Xie 
578843fcf35SMiao Xie join_fail:
5790860adfdSMiao Xie 	if (type & __TRANS_FREEZABLE)
5800b246afaSJeff Mahoney 		sb_end_intwrite(fs_info->sb);
581843fcf35SMiao Xie 	kmem_cache_free(btrfs_trans_handle_cachep, h);
582843fcf35SMiao Xie alloc_fail:
583843fcf35SMiao Xie 	if (num_bytes)
5842ff7e61eSJeff Mahoney 		btrfs_block_rsv_release(fs_info, &fs_info->trans_block_rsv,
585843fcf35SMiao Xie 					num_bytes);
586843fcf35SMiao Xie reserve_fail:
587733e03a0SQu Wenruo 	btrfs_qgroup_free_meta_pertrans(root, qgroup_reserved);
588843fcf35SMiao Xie 	return ERR_PTR(ret);
58979154b1bSChris Mason }
59079154b1bSChris Mason 
591f9295749SChris Mason struct btrfs_trans_handle *btrfs_start_transaction(struct btrfs_root *root,
5925aed1dd8SAlexandru Moise 						   unsigned int num_items)
593f9295749SChris Mason {
59408e007d2SMiao Xie 	return start_transaction(root, num_items, TRANS_START,
595003d7c59SJeff Mahoney 				 BTRFS_RESERVE_FLUSH_ALL, true);
596f9295749SChris Mason }
597003d7c59SJeff Mahoney 
5988eab77ffSFilipe Manana struct btrfs_trans_handle *btrfs_start_transaction_fallback_global_rsv(
5998eab77ffSFilipe Manana 					struct btrfs_root *root,
6008eab77ffSFilipe Manana 					unsigned int num_items,
6018eab77ffSFilipe Manana 					int min_factor)
6028eab77ffSFilipe Manana {
6030b246afaSJeff Mahoney 	struct btrfs_fs_info *fs_info = root->fs_info;
6048eab77ffSFilipe Manana 	struct btrfs_trans_handle *trans;
6058eab77ffSFilipe Manana 	u64 num_bytes;
6068eab77ffSFilipe Manana 	int ret;
6078eab77ffSFilipe Manana 
608003d7c59SJeff Mahoney 	/*
609003d7c59SJeff Mahoney 	 * We have two callers: unlink and block group removal.  The
610003d7c59SJeff Mahoney 	 * former should succeed even if we will temporarily exceed
611003d7c59SJeff Mahoney 	 * quota and the latter operates on the extent root so
612003d7c59SJeff Mahoney 	 * qgroup enforcement is ignored anyway.
613003d7c59SJeff Mahoney 	 */
614003d7c59SJeff Mahoney 	trans = start_transaction(root, num_items, TRANS_START,
615003d7c59SJeff Mahoney 				  BTRFS_RESERVE_FLUSH_ALL, false);
6168eab77ffSFilipe Manana 	if (!IS_ERR(trans) || PTR_ERR(trans) != -ENOSPC)
6178eab77ffSFilipe Manana 		return trans;
6188eab77ffSFilipe Manana 
6198eab77ffSFilipe Manana 	trans = btrfs_start_transaction(root, 0);
6208eab77ffSFilipe Manana 	if (IS_ERR(trans))
6218eab77ffSFilipe Manana 		return trans;
6228eab77ffSFilipe Manana 
6230b246afaSJeff Mahoney 	num_bytes = btrfs_calc_trans_metadata_size(fs_info, num_items);
6240b246afaSJeff Mahoney 	ret = btrfs_cond_migrate_bytes(fs_info, &fs_info->trans_block_rsv,
6250b246afaSJeff Mahoney 				       num_bytes, min_factor);
6268eab77ffSFilipe Manana 	if (ret) {
6273a45bb20SJeff Mahoney 		btrfs_end_transaction(trans);
6288eab77ffSFilipe Manana 		return ERR_PTR(ret);
6298eab77ffSFilipe Manana 	}
6308eab77ffSFilipe Manana 
6310b246afaSJeff Mahoney 	trans->block_rsv = &fs_info->trans_block_rsv;
6328eab77ffSFilipe Manana 	trans->bytes_reserved = num_bytes;
6330b246afaSJeff Mahoney 	trace_btrfs_space_reservation(fs_info, "transaction",
63488d3a5aaSJosef Bacik 				      trans->transid, num_bytes, 1);
6358eab77ffSFilipe Manana 
6368eab77ffSFilipe Manana 	return trans;
6378eab77ffSFilipe Manana }
6388407aa46SMiao Xie 
6397a7eaa40SJosef Bacik struct btrfs_trans_handle *btrfs_join_transaction(struct btrfs_root *root)
640f9295749SChris Mason {
641003d7c59SJeff Mahoney 	return start_transaction(root, 0, TRANS_JOIN, BTRFS_RESERVE_NO_FLUSH,
642003d7c59SJeff Mahoney 				 true);
643f9295749SChris Mason }
644f9295749SChris Mason 
6457a7eaa40SJosef Bacik struct btrfs_trans_handle *btrfs_join_transaction_nolock(struct btrfs_root *root)
6460af3d00bSJosef Bacik {
647575a75d6SAlexandru Moise 	return start_transaction(root, 0, TRANS_JOIN_NOLOCK,
648003d7c59SJeff Mahoney 				 BTRFS_RESERVE_NO_FLUSH, true);
6490af3d00bSJosef Bacik }
6500af3d00bSJosef Bacik 
651d4edf39bSMiao Xie /*
652d4edf39bSMiao Xie  * btrfs_attach_transaction() - catch the running transaction
653d4edf39bSMiao Xie  *
654d4edf39bSMiao Xie  * It is used when we want to commit the current the transaction, but
655d4edf39bSMiao Xie  * don't want to start a new one.
656d4edf39bSMiao Xie  *
657d4edf39bSMiao Xie  * Note: If this function return -ENOENT, it just means there is no
658d4edf39bSMiao Xie  * running transaction. But it is possible that the inactive transaction
659d4edf39bSMiao Xie  * is still in the memory, not fully on disk. If you hope there is no
660d4edf39bSMiao Xie  * inactive transaction in the fs when -ENOENT is returned, you should
661d4edf39bSMiao Xie  * invoke
662d4edf39bSMiao Xie  *     btrfs_attach_transaction_barrier()
663d4edf39bSMiao Xie  */
664354aa0fbSMiao Xie struct btrfs_trans_handle *btrfs_attach_transaction(struct btrfs_root *root)
66560376ce4SJosef Bacik {
666575a75d6SAlexandru Moise 	return start_transaction(root, 0, TRANS_ATTACH,
667003d7c59SJeff Mahoney 				 BTRFS_RESERVE_NO_FLUSH, true);
66860376ce4SJosef Bacik }
66960376ce4SJosef Bacik 
670d4edf39bSMiao Xie /*
67190b6d283SWang Sheng-Hui  * btrfs_attach_transaction_barrier() - catch the running transaction
672d4edf39bSMiao Xie  *
673d4edf39bSMiao Xie  * It is similar to the above function, the differentia is this one
674d4edf39bSMiao Xie  * will wait for all the inactive transactions until they fully
675d4edf39bSMiao Xie  * complete.
676d4edf39bSMiao Xie  */
677d4edf39bSMiao Xie struct btrfs_trans_handle *
678d4edf39bSMiao Xie btrfs_attach_transaction_barrier(struct btrfs_root *root)
679d4edf39bSMiao Xie {
680d4edf39bSMiao Xie 	struct btrfs_trans_handle *trans;
681d4edf39bSMiao Xie 
682575a75d6SAlexandru Moise 	trans = start_transaction(root, 0, TRANS_ATTACH,
683003d7c59SJeff Mahoney 				  BTRFS_RESERVE_NO_FLUSH, true);
6848d9e220cSAl Viro 	if (trans == ERR_PTR(-ENOENT))
6852ff7e61eSJeff Mahoney 		btrfs_wait_for_commit(root->fs_info, 0);
686d4edf39bSMiao Xie 
687d4edf39bSMiao Xie 	return trans;
688d4edf39bSMiao Xie }
689d4edf39bSMiao Xie 
690d352ac68SChris Mason /* wait for a transaction commit to be fully complete */
6912ff7e61eSJeff Mahoney static noinline void wait_for_commit(struct btrfs_transaction *commit)
69289ce8a63SChris Mason {
6934a9d8bdeSMiao Xie 	wait_event(commit->commit_wait, commit->state == TRANS_STATE_COMPLETED);
69489ce8a63SChris Mason }
69589ce8a63SChris Mason 
6962ff7e61eSJeff Mahoney int btrfs_wait_for_commit(struct btrfs_fs_info *fs_info, u64 transid)
69746204592SSage Weil {
69846204592SSage Weil 	struct btrfs_transaction *cur_trans = NULL, *t;
6998cd2807fSMiao Xie 	int ret = 0;
70046204592SSage Weil 
70146204592SSage Weil 	if (transid) {
7020b246afaSJeff Mahoney 		if (transid <= fs_info->last_trans_committed)
703a4abeea4SJosef Bacik 			goto out;
70446204592SSage Weil 
70546204592SSage Weil 		/* find specified transaction */
7060b246afaSJeff Mahoney 		spin_lock(&fs_info->trans_lock);
7070b246afaSJeff Mahoney 		list_for_each_entry(t, &fs_info->trans_list, list) {
70846204592SSage Weil 			if (t->transid == transid) {
70946204592SSage Weil 				cur_trans = t;
7109b64f57dSElena Reshetova 				refcount_inc(&cur_trans->use_count);
7118cd2807fSMiao Xie 				ret = 0;
71246204592SSage Weil 				break;
71346204592SSage Weil 			}
7148cd2807fSMiao Xie 			if (t->transid > transid) {
7158cd2807fSMiao Xie 				ret = 0;
71646204592SSage Weil 				break;
71746204592SSage Weil 			}
7188cd2807fSMiao Xie 		}
7190b246afaSJeff Mahoney 		spin_unlock(&fs_info->trans_lock);
72042383020SSage Weil 
72142383020SSage Weil 		/*
72242383020SSage Weil 		 * The specified transaction doesn't exist, or we
72342383020SSage Weil 		 * raced with btrfs_commit_transaction
72442383020SSage Weil 		 */
72542383020SSage Weil 		if (!cur_trans) {
7260b246afaSJeff Mahoney 			if (transid > fs_info->last_trans_committed)
72742383020SSage Weil 				ret = -EINVAL;
7288cd2807fSMiao Xie 			goto out;
72942383020SSage Weil 		}
73046204592SSage Weil 	} else {
73146204592SSage Weil 		/* find newest transaction that is committing | committed */
7320b246afaSJeff Mahoney 		spin_lock(&fs_info->trans_lock);
7330b246afaSJeff Mahoney 		list_for_each_entry_reverse(t, &fs_info->trans_list,
73446204592SSage Weil 					    list) {
7354a9d8bdeSMiao Xie 			if (t->state >= TRANS_STATE_COMMIT_START) {
7364a9d8bdeSMiao Xie 				if (t->state == TRANS_STATE_COMPLETED)
7373473f3c0SJosef Bacik 					break;
73846204592SSage Weil 				cur_trans = t;
7399b64f57dSElena Reshetova 				refcount_inc(&cur_trans->use_count);
74046204592SSage Weil 				break;
74146204592SSage Weil 			}
74246204592SSage Weil 		}
7430b246afaSJeff Mahoney 		spin_unlock(&fs_info->trans_lock);
74446204592SSage Weil 		if (!cur_trans)
745a4abeea4SJosef Bacik 			goto out;  /* nothing committing|committed */
74646204592SSage Weil 	}
74746204592SSage Weil 
7482ff7e61eSJeff Mahoney 	wait_for_commit(cur_trans);
749724e2315SJosef Bacik 	btrfs_put_transaction(cur_trans);
750a4abeea4SJosef Bacik out:
75146204592SSage Weil 	return ret;
75246204592SSage Weil }
75346204592SSage Weil 
7542ff7e61eSJeff Mahoney void btrfs_throttle(struct btrfs_fs_info *fs_info)
75537d1aeeeSChris Mason {
7562ff7e61eSJeff Mahoney 	wait_current_trans(fs_info);
75737d1aeeeSChris Mason }
75837d1aeeeSChris Mason 
7592ff7e61eSJeff Mahoney static int should_end_transaction(struct btrfs_trans_handle *trans)
7608929ecfaSYan, Zheng {
7612ff7e61eSJeff Mahoney 	struct btrfs_fs_info *fs_info = trans->fs_info;
7620b246afaSJeff Mahoney 
7637c777430SJosef Bacik 	if (btrfs_check_space_for_delayed_refs(trans, fs_info))
7641be41b78SJosef Bacik 		return 1;
76536ba022aSJosef Bacik 
7662ff7e61eSJeff Mahoney 	return !!btrfs_block_rsv_check(&fs_info->global_block_rsv, 5);
7678929ecfaSYan, Zheng }
7688929ecfaSYan, Zheng 
7693a45bb20SJeff Mahoney int btrfs_should_end_transaction(struct btrfs_trans_handle *trans)
7708929ecfaSYan, Zheng {
7718929ecfaSYan, Zheng 	struct btrfs_transaction *cur_trans = trans->transaction;
7728929ecfaSYan, Zheng 	int updates;
77349b25e05SJeff Mahoney 	int err;
7748929ecfaSYan, Zheng 
775a4abeea4SJosef Bacik 	smp_mb();
7764a9d8bdeSMiao Xie 	if (cur_trans->state >= TRANS_STATE_BLOCKED ||
7774a9d8bdeSMiao Xie 	    cur_trans->delayed_refs.flushing)
7788929ecfaSYan, Zheng 		return 1;
7798929ecfaSYan, Zheng 
7808929ecfaSYan, Zheng 	updates = trans->delayed_ref_updates;
7818929ecfaSYan, Zheng 	trans->delayed_ref_updates = 0;
78249b25e05SJeff Mahoney 	if (updates) {
783c79a70b1SNikolay Borisov 		err = btrfs_run_delayed_refs(trans, updates * 2);
78449b25e05SJeff Mahoney 		if (err) /* Error code will also eval true */
78549b25e05SJeff Mahoney 			return err;
78649b25e05SJeff Mahoney 	}
7878929ecfaSYan, Zheng 
7882ff7e61eSJeff Mahoney 	return should_end_transaction(trans);
7898929ecfaSYan, Zheng }
7908929ecfaSYan, Zheng 
791dc60c525SNikolay Borisov static void btrfs_trans_release_metadata(struct btrfs_trans_handle *trans)
792dc60c525SNikolay Borisov 
7930e34693fSNikolay Borisov {
794dc60c525SNikolay Borisov 	struct btrfs_fs_info *fs_info = trans->fs_info;
795dc60c525SNikolay Borisov 
7960e34693fSNikolay Borisov 	if (!trans->block_rsv) {
7970e34693fSNikolay Borisov 		ASSERT(!trans->bytes_reserved);
7980e34693fSNikolay Borisov 		return;
7990e34693fSNikolay Borisov 	}
8000e34693fSNikolay Borisov 
8010e34693fSNikolay Borisov 	if (!trans->bytes_reserved)
8020e34693fSNikolay Borisov 		return;
8030e34693fSNikolay Borisov 
8040e34693fSNikolay Borisov 	ASSERT(trans->block_rsv == &fs_info->trans_block_rsv);
8050e34693fSNikolay Borisov 	trace_btrfs_space_reservation(fs_info, "transaction",
8060e34693fSNikolay Borisov 				      trans->transid, trans->bytes_reserved, 0);
8070e34693fSNikolay Borisov 	btrfs_block_rsv_release(fs_info, trans->block_rsv,
8080e34693fSNikolay Borisov 				trans->bytes_reserved);
8090e34693fSNikolay Borisov 	trans->bytes_reserved = 0;
8100e34693fSNikolay Borisov }
8110e34693fSNikolay Borisov 
81289ce8a63SChris Mason static int __btrfs_end_transaction(struct btrfs_trans_handle *trans,
8133a45bb20SJeff Mahoney 				   int throttle)
81479154b1bSChris Mason {
8153a45bb20SJeff Mahoney 	struct btrfs_fs_info *info = trans->fs_info;
8168929ecfaSYan, Zheng 	struct btrfs_transaction *cur_trans = trans->transaction;
81731b9655fSJosef Bacik 	u64 transid = trans->transid;
8181be41b78SJosef Bacik 	unsigned long cur = trans->delayed_ref_updates;
819a698d075SMiao Xie 	int lock = (trans->type != TRANS_JOIN_NOLOCK);
8204edc2ca3SDave Jones 	int err = 0;
821a79b7d4bSChris Mason 	int must_run_delayed_refs = 0;
822d6e4a428SChris Mason 
823b50fff81SDavid Sterba 	if (refcount_read(&trans->use_count) > 1) {
824b50fff81SDavid Sterba 		refcount_dec(&trans->use_count);
8252a1eb461SJosef Bacik 		trans->block_rsv = trans->orig_rsv;
8262a1eb461SJosef Bacik 		return 0;
8272a1eb461SJosef Bacik 	}
8282a1eb461SJosef Bacik 
829dc60c525SNikolay Borisov 	btrfs_trans_release_metadata(trans);
8304c13d758SJosef Bacik 	trans->block_rsv = NULL;
831c5567237SArne Jansen 
832ea658badSJosef Bacik 	if (!list_empty(&trans->new_bgs))
8336c686b35SNikolay Borisov 		btrfs_create_pending_block_groups(trans);
834ea658badSJosef Bacik 
835c3e69d58SChris Mason 	trans->delayed_ref_updates = 0;
836a79b7d4bSChris Mason 	if (!trans->sync) {
837a79b7d4bSChris Mason 		must_run_delayed_refs =
8382ff7e61eSJeff Mahoney 			btrfs_should_throttle_delayed_refs(trans, info);
8390a2b2a84SJosef Bacik 		cur = max_t(unsigned long, cur, 32);
840a79b7d4bSChris Mason 
841a79b7d4bSChris Mason 		/*
842a79b7d4bSChris Mason 		 * don't make the caller wait if they are from a NOLOCK
843a79b7d4bSChris Mason 		 * or ATTACH transaction, it will deadlock with commit
844a79b7d4bSChris Mason 		 */
845a79b7d4bSChris Mason 		if (must_run_delayed_refs == 1 &&
846a79b7d4bSChris Mason 		    (trans->type & (__TRANS_JOIN_NOLOCK | __TRANS_ATTACH)))
847a79b7d4bSChris Mason 			must_run_delayed_refs = 2;
84856bec294SChris Mason 	}
849bb721703SChris Mason 
850dc60c525SNikolay Borisov 	btrfs_trans_release_metadata(trans);
8510e721106SJosef Bacik 	trans->block_rsv = NULL;
85256bec294SChris Mason 
853ea658badSJosef Bacik 	if (!list_empty(&trans->new_bgs))
8546c686b35SNikolay Borisov 		btrfs_create_pending_block_groups(trans);
855ea658badSJosef Bacik 
8564fbcdf66SFilipe Manana 	btrfs_trans_release_chunk_metadata(trans);
8574fbcdf66SFilipe Manana 
85892e2f7e3SNikolay Borisov 	if (lock && should_end_transaction(trans) &&
85920c7bcecSSeraphime Kirkovski 	    READ_ONCE(cur_trans->state) == TRANS_STATE_RUNNING) {
8604a9d8bdeSMiao Xie 		spin_lock(&info->trans_lock);
8614a9d8bdeSMiao Xie 		if (cur_trans->state == TRANS_STATE_RUNNING)
8624a9d8bdeSMiao Xie 			cur_trans->state = TRANS_STATE_BLOCKED;
8634a9d8bdeSMiao Xie 		spin_unlock(&info->trans_lock);
864a4abeea4SJosef Bacik 	}
8658929ecfaSYan, Zheng 
86620c7bcecSSeraphime Kirkovski 	if (lock && READ_ONCE(cur_trans->state) == TRANS_STATE_BLOCKED) {
8673bbb24b2SJosef Bacik 		if (throttle)
8683a45bb20SJeff Mahoney 			return btrfs_commit_transaction(trans);
8693bbb24b2SJosef Bacik 		else
8708929ecfaSYan, Zheng 			wake_up_process(info->transaction_kthread);
8718929ecfaSYan, Zheng 	}
8728929ecfaSYan, Zheng 
8730860adfdSMiao Xie 	if (trans->type & __TRANS_FREEZABLE)
8740b246afaSJeff Mahoney 		sb_end_intwrite(info->sb);
8756df7881aSJosef Bacik 
8768929ecfaSYan, Zheng 	WARN_ON(cur_trans != info->running_transaction);
87713c5a93eSJosef Bacik 	WARN_ON(atomic_read(&cur_trans->num_writers) < 1);
87813c5a93eSJosef Bacik 	atomic_dec(&cur_trans->num_writers);
8790860adfdSMiao Xie 	extwriter_counter_dec(cur_trans, trans->type);
88089ce8a63SChris Mason 
881093258e6SDavid Sterba 	cond_wake_up(&cur_trans->writer_wait);
882724e2315SJosef Bacik 	btrfs_put_transaction(cur_trans);
8839ed74f2dSJosef Bacik 
8849ed74f2dSJosef Bacik 	if (current->journal_info == trans)
8859ed74f2dSJosef Bacik 		current->journal_info = NULL;
886ab78c84dSChris Mason 
88724bbcf04SYan, Zheng 	if (throttle)
8882ff7e61eSJeff Mahoney 		btrfs_run_delayed_iputs(info);
88924bbcf04SYan, Zheng 
89049b25e05SJeff Mahoney 	if (trans->aborted ||
8910b246afaSJeff Mahoney 	    test_bit(BTRFS_FS_STATE_ERROR, &info->fs_state)) {
8924e121c06SJosef Bacik 		wake_up_process(info->transaction_kthread);
8934edc2ca3SDave Jones 		err = -EIO;
8944e121c06SJosef Bacik 	}
89549b25e05SJeff Mahoney 
8964edc2ca3SDave Jones 	kmem_cache_free(btrfs_trans_handle_cachep, trans);
897a79b7d4bSChris Mason 	if (must_run_delayed_refs) {
8982ff7e61eSJeff Mahoney 		btrfs_async_run_delayed_refs(info, cur, transid,
899a79b7d4bSChris Mason 					     must_run_delayed_refs == 1);
900a79b7d4bSChris Mason 	}
9014edc2ca3SDave Jones 	return err;
90279154b1bSChris Mason }
90379154b1bSChris Mason 
9043a45bb20SJeff Mahoney int btrfs_end_transaction(struct btrfs_trans_handle *trans)
90589ce8a63SChris Mason {
9063a45bb20SJeff Mahoney 	return __btrfs_end_transaction(trans, 0);
90789ce8a63SChris Mason }
90889ce8a63SChris Mason 
9093a45bb20SJeff Mahoney int btrfs_end_transaction_throttle(struct btrfs_trans_handle *trans)
91089ce8a63SChris Mason {
9113a45bb20SJeff Mahoney 	return __btrfs_end_transaction(trans, 1);
91216cdcec7SMiao Xie }
91316cdcec7SMiao Xie 
914d352ac68SChris Mason /*
915d352ac68SChris Mason  * when btree blocks are allocated, they have some corresponding bits set for
916d352ac68SChris Mason  * them in one of two extent_io trees.  This is used to make sure all of
917690587d1SChris Mason  * those extents are sent to disk but does not wait on them
918d352ac68SChris Mason  */
9192ff7e61eSJeff Mahoney int btrfs_write_marked_extents(struct btrfs_fs_info *fs_info,
9208cef4e16SYan, Zheng 			       struct extent_io_tree *dirty_pages, int mark)
92179154b1bSChris Mason {
922777e6bd7SChris Mason 	int err = 0;
9237c4452b9SChris Mason 	int werr = 0;
9240b246afaSJeff Mahoney 	struct address_space *mapping = fs_info->btree_inode->i_mapping;
925e6138876SJosef Bacik 	struct extent_state *cached_state = NULL;
926777e6bd7SChris Mason 	u64 start = 0;
9275f39d397SChris Mason 	u64 end;
9287c4452b9SChris Mason 
9296300463bSLiu Bo 	atomic_inc(&BTRFS_I(fs_info->btree_inode)->sync_writers);
9301728366eSJosef Bacik 	while (!find_first_extent_bit(dirty_pages, start, &start, &end,
931e6138876SJosef Bacik 				      mark, &cached_state)) {
932663dfbb0SFilipe Manana 		bool wait_writeback = false;
933663dfbb0SFilipe Manana 
934663dfbb0SFilipe Manana 		err = convert_extent_bit(dirty_pages, start, end,
935663dfbb0SFilipe Manana 					 EXTENT_NEED_WAIT,
936210aa277SDavid Sterba 					 mark, &cached_state);
937663dfbb0SFilipe Manana 		/*
938663dfbb0SFilipe Manana 		 * convert_extent_bit can return -ENOMEM, which is most of the
939663dfbb0SFilipe Manana 		 * time a temporary error. So when it happens, ignore the error
940663dfbb0SFilipe Manana 		 * and wait for writeback of this range to finish - because we
941663dfbb0SFilipe Manana 		 * failed to set the bit EXTENT_NEED_WAIT for the range, a call
942bf89d38fSJeff Mahoney 		 * to __btrfs_wait_marked_extents() would not know that
943bf89d38fSJeff Mahoney 		 * writeback for this range started and therefore wouldn't
944bf89d38fSJeff Mahoney 		 * wait for it to finish - we don't want to commit a
945bf89d38fSJeff Mahoney 		 * superblock that points to btree nodes/leafs for which
946bf89d38fSJeff Mahoney 		 * writeback hasn't finished yet (and without errors).
947663dfbb0SFilipe Manana 		 * We cleanup any entries left in the io tree when committing
948663dfbb0SFilipe Manana 		 * the transaction (through clear_btree_io_tree()).
949663dfbb0SFilipe Manana 		 */
950663dfbb0SFilipe Manana 		if (err == -ENOMEM) {
951663dfbb0SFilipe Manana 			err = 0;
952663dfbb0SFilipe Manana 			wait_writeback = true;
953663dfbb0SFilipe Manana 		}
954663dfbb0SFilipe Manana 		if (!err)
9551728366eSJosef Bacik 			err = filemap_fdatawrite_range(mapping, start, end);
9567c4452b9SChris Mason 		if (err)
9577c4452b9SChris Mason 			werr = err;
958663dfbb0SFilipe Manana 		else if (wait_writeback)
959663dfbb0SFilipe Manana 			werr = filemap_fdatawait_range(mapping, start, end);
960e38e2ed7SFilipe Manana 		free_extent_state(cached_state);
961663dfbb0SFilipe Manana 		cached_state = NULL;
9621728366eSJosef Bacik 		cond_resched();
9631728366eSJosef Bacik 		start = end + 1;
9647c4452b9SChris Mason 	}
9656300463bSLiu Bo 	atomic_dec(&BTRFS_I(fs_info->btree_inode)->sync_writers);
966690587d1SChris Mason 	return werr;
967690587d1SChris Mason }
968690587d1SChris Mason 
969690587d1SChris Mason /*
970690587d1SChris Mason  * when btree blocks are allocated, they have some corresponding bits set for
971690587d1SChris Mason  * them in one of two extent_io trees.  This is used to make sure all of
972690587d1SChris Mason  * those extents are on disk for transaction or log commit.  We wait
973690587d1SChris Mason  * on all the pages and clear them from the dirty pages state tree
974690587d1SChris Mason  */
975bf89d38fSJeff Mahoney static int __btrfs_wait_marked_extents(struct btrfs_fs_info *fs_info,
976bf89d38fSJeff Mahoney 				       struct extent_io_tree *dirty_pages)
977690587d1SChris Mason {
978690587d1SChris Mason 	int err = 0;
979690587d1SChris Mason 	int werr = 0;
9800b246afaSJeff Mahoney 	struct address_space *mapping = fs_info->btree_inode->i_mapping;
981e6138876SJosef Bacik 	struct extent_state *cached_state = NULL;
982690587d1SChris Mason 	u64 start = 0;
983690587d1SChris Mason 	u64 end;
984690587d1SChris Mason 
9851728366eSJosef Bacik 	while (!find_first_extent_bit(dirty_pages, start, &start, &end,
986e6138876SJosef Bacik 				      EXTENT_NEED_WAIT, &cached_state)) {
987663dfbb0SFilipe Manana 		/*
988663dfbb0SFilipe Manana 		 * Ignore -ENOMEM errors returned by clear_extent_bit().
989663dfbb0SFilipe Manana 		 * When committing the transaction, we'll remove any entries
990663dfbb0SFilipe Manana 		 * left in the io tree. For a log commit, we don't remove them
991663dfbb0SFilipe Manana 		 * after committing the log because the tree can be accessed
992663dfbb0SFilipe Manana 		 * concurrently - we do it only at transaction commit time when
993663dfbb0SFilipe Manana 		 * it's safe to do it (through clear_btree_io_tree()).
994663dfbb0SFilipe Manana 		 */
995663dfbb0SFilipe Manana 		err = clear_extent_bit(dirty_pages, start, end,
996ae0f1625SDavid Sterba 				       EXTENT_NEED_WAIT, 0, 0, &cached_state);
997663dfbb0SFilipe Manana 		if (err == -ENOMEM)
998663dfbb0SFilipe Manana 			err = 0;
999663dfbb0SFilipe Manana 		if (!err)
10001728366eSJosef Bacik 			err = filemap_fdatawait_range(mapping, start, end);
1001777e6bd7SChris Mason 		if (err)
1002777e6bd7SChris Mason 			werr = err;
1003e38e2ed7SFilipe Manana 		free_extent_state(cached_state);
1004e38e2ed7SFilipe Manana 		cached_state = NULL;
1005777e6bd7SChris Mason 		cond_resched();
10061728366eSJosef Bacik 		start = end + 1;
1007777e6bd7SChris Mason 	}
10087c4452b9SChris Mason 	if (err)
10097c4452b9SChris Mason 		werr = err;
1010bf89d38fSJeff Mahoney 	return werr;
1011bf89d38fSJeff Mahoney }
1012656f30dbSFilipe Manana 
1013bf89d38fSJeff Mahoney int btrfs_wait_extents(struct btrfs_fs_info *fs_info,
1014bf89d38fSJeff Mahoney 		       struct extent_io_tree *dirty_pages)
1015bf89d38fSJeff Mahoney {
1016bf89d38fSJeff Mahoney 	bool errors = false;
1017bf89d38fSJeff Mahoney 	int err;
1018bf89d38fSJeff Mahoney 
1019bf89d38fSJeff Mahoney 	err = __btrfs_wait_marked_extents(fs_info, dirty_pages);
1020bf89d38fSJeff Mahoney 	if (test_and_clear_bit(BTRFS_FS_BTREE_ERR, &fs_info->flags))
1021bf89d38fSJeff Mahoney 		errors = true;
1022bf89d38fSJeff Mahoney 
1023bf89d38fSJeff Mahoney 	if (errors && !err)
1024bf89d38fSJeff Mahoney 		err = -EIO;
1025bf89d38fSJeff Mahoney 	return err;
1026bf89d38fSJeff Mahoney }
1027bf89d38fSJeff Mahoney 
1028bf89d38fSJeff Mahoney int btrfs_wait_tree_log_extents(struct btrfs_root *log_root, int mark)
1029bf89d38fSJeff Mahoney {
1030bf89d38fSJeff Mahoney 	struct btrfs_fs_info *fs_info = log_root->fs_info;
1031bf89d38fSJeff Mahoney 	struct extent_io_tree *dirty_pages = &log_root->dirty_log_pages;
1032bf89d38fSJeff Mahoney 	bool errors = false;
1033bf89d38fSJeff Mahoney 	int err;
1034bf89d38fSJeff Mahoney 
1035bf89d38fSJeff Mahoney 	ASSERT(log_root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID);
1036bf89d38fSJeff Mahoney 
1037bf89d38fSJeff Mahoney 	err = __btrfs_wait_marked_extents(fs_info, dirty_pages);
1038656f30dbSFilipe Manana 	if ((mark & EXTENT_DIRTY) &&
10390b246afaSJeff Mahoney 	    test_and_clear_bit(BTRFS_FS_LOG1_ERR, &fs_info->flags))
1040656f30dbSFilipe Manana 		errors = true;
1041656f30dbSFilipe Manana 
1042656f30dbSFilipe Manana 	if ((mark & EXTENT_NEW) &&
10430b246afaSJeff Mahoney 	    test_and_clear_bit(BTRFS_FS_LOG2_ERR, &fs_info->flags))
1044656f30dbSFilipe Manana 		errors = true;
1045656f30dbSFilipe Manana 
1046bf89d38fSJeff Mahoney 	if (errors && !err)
1047bf89d38fSJeff Mahoney 		err = -EIO;
1048bf89d38fSJeff Mahoney 	return err;
104979154b1bSChris Mason }
105079154b1bSChris Mason 
1051690587d1SChris Mason /*
1052c9b577c0SNikolay Borisov  * When btree blocks are allocated the corresponding extents are marked dirty.
1053c9b577c0SNikolay Borisov  * This function ensures such extents are persisted on disk for transaction or
1054c9b577c0SNikolay Borisov  * log commit.
1055c9b577c0SNikolay Borisov  *
1056c9b577c0SNikolay Borisov  * @trans: transaction whose dirty pages we'd like to write
1057690587d1SChris Mason  */
105870458a58SNikolay Borisov static int btrfs_write_and_wait_transaction(struct btrfs_trans_handle *trans)
1059d0c803c4SChris Mason {
1060663dfbb0SFilipe Manana 	int ret;
1061c9b577c0SNikolay Borisov 	int ret2;
1062c9b577c0SNikolay Borisov 	struct extent_io_tree *dirty_pages = &trans->transaction->dirty_pages;
106370458a58SNikolay Borisov 	struct btrfs_fs_info *fs_info = trans->fs_info;
1064c9b577c0SNikolay Borisov 	struct blk_plug plug;
1065663dfbb0SFilipe Manana 
1066c9b577c0SNikolay Borisov 	blk_start_plug(&plug);
1067c9b577c0SNikolay Borisov 	ret = btrfs_write_marked_extents(fs_info, dirty_pages, EXTENT_DIRTY);
1068c9b577c0SNikolay Borisov 	blk_finish_plug(&plug);
1069c9b577c0SNikolay Borisov 	ret2 = btrfs_wait_extents(fs_info, dirty_pages);
1070c9b577c0SNikolay Borisov 
1071663dfbb0SFilipe Manana 	clear_btree_io_tree(&trans->transaction->dirty_pages);
1072663dfbb0SFilipe Manana 
1073c9b577c0SNikolay Borisov 	if (ret)
1074663dfbb0SFilipe Manana 		return ret;
1075c9b577c0SNikolay Borisov 	else if (ret2)
1076c9b577c0SNikolay Borisov 		return ret2;
1077c9b577c0SNikolay Borisov 	else
1078c9b577c0SNikolay Borisov 		return 0;
1079d0c803c4SChris Mason }
1080d0c803c4SChris Mason 
1081d352ac68SChris Mason /*
1082d352ac68SChris Mason  * this is used to update the root pointer in the tree of tree roots.
1083d352ac68SChris Mason  *
1084d352ac68SChris Mason  * But, in the case of the extent allocation tree, updating the root
1085d352ac68SChris Mason  * pointer may allocate blocks which may change the root of the extent
1086d352ac68SChris Mason  * allocation tree.
1087d352ac68SChris Mason  *
1088d352ac68SChris Mason  * So, this loops and repeats and makes sure the cowonly root didn't
1089d352ac68SChris Mason  * change while the root pointer was being updated in the metadata.
1090d352ac68SChris Mason  */
10910b86a832SChris Mason static int update_cowonly_root(struct btrfs_trans_handle *trans,
109279154b1bSChris Mason 			       struct btrfs_root *root)
109379154b1bSChris Mason {
109479154b1bSChris Mason 	int ret;
10950b86a832SChris Mason 	u64 old_root_bytenr;
109686b9f2ecSYan, Zheng 	u64 old_root_used;
10970b246afaSJeff Mahoney 	struct btrfs_fs_info *fs_info = root->fs_info;
10980b246afaSJeff Mahoney 	struct btrfs_root *tree_root = fs_info->tree_root;
109979154b1bSChris Mason 
110086b9f2ecSYan, Zheng 	old_root_used = btrfs_root_used(&root->root_item);
110156bec294SChris Mason 
110279154b1bSChris Mason 	while (1) {
11030b86a832SChris Mason 		old_root_bytenr = btrfs_root_bytenr(&root->root_item);
110486b9f2ecSYan, Zheng 		if (old_root_bytenr == root->node->start &&
1105ea526d18SJosef Bacik 		    old_root_used == btrfs_root_used(&root->root_item))
110679154b1bSChris Mason 			break;
110787ef2bb4SChris Mason 
11085d4f98a2SYan Zheng 		btrfs_set_root_node(&root->root_item, root->node);
110979154b1bSChris Mason 		ret = btrfs_update_root(trans, tree_root,
11100b86a832SChris Mason 					&root->root_key,
11110b86a832SChris Mason 					&root->root_item);
111249b25e05SJeff Mahoney 		if (ret)
111349b25e05SJeff Mahoney 			return ret;
111456bec294SChris Mason 
111586b9f2ecSYan, Zheng 		old_root_used = btrfs_root_used(&root->root_item);
1116e7070be1SJosef Bacik 	}
1117276e680dSYan Zheng 
11180b86a832SChris Mason 	return 0;
11190b86a832SChris Mason }
11200b86a832SChris Mason 
1121d352ac68SChris Mason /*
1122d352ac68SChris Mason  * update all the cowonly tree roots on disk
112349b25e05SJeff Mahoney  *
112449b25e05SJeff Mahoney  * The error handling in this function may not be obvious. Any of the
112549b25e05SJeff Mahoney  * failures will cause the file system to go offline. We still need
112649b25e05SJeff Mahoney  * to clean up the delayed refs.
1127d352ac68SChris Mason  */
11289386d8bcSNikolay Borisov static noinline int commit_cowonly_roots(struct btrfs_trans_handle *trans)
11290b86a832SChris Mason {
11309386d8bcSNikolay Borisov 	struct btrfs_fs_info *fs_info = trans->fs_info;
1131ea526d18SJosef Bacik 	struct list_head *dirty_bgs = &trans->transaction->dirty_bgs;
11321bbc621eSChris Mason 	struct list_head *io_bgs = &trans->transaction->io_bgs;
11330b86a832SChris Mason 	struct list_head *next;
113484234f3aSYan Zheng 	struct extent_buffer *eb;
113556bec294SChris Mason 	int ret;
113684234f3aSYan Zheng 
113784234f3aSYan Zheng 	eb = btrfs_lock_root_node(fs_info->tree_root);
113849b25e05SJeff Mahoney 	ret = btrfs_cow_block(trans, fs_info->tree_root, eb, NULL,
113949b25e05SJeff Mahoney 			      0, &eb);
114084234f3aSYan Zheng 	btrfs_tree_unlock(eb);
114184234f3aSYan Zheng 	free_extent_buffer(eb);
11420b86a832SChris Mason 
114349b25e05SJeff Mahoney 	if (ret)
114449b25e05SJeff Mahoney 		return ret;
114549b25e05SJeff Mahoney 
1146c79a70b1SNikolay Borisov 	ret = btrfs_run_delayed_refs(trans, (unsigned long)-1);
114749b25e05SJeff Mahoney 	if (ret)
114849b25e05SJeff Mahoney 		return ret;
114987ef2bb4SChris Mason 
11500b246afaSJeff Mahoney 	ret = btrfs_run_dev_stats(trans, fs_info);
1151c16ce190SJosef Bacik 	if (ret)
1152c16ce190SJosef Bacik 		return ret;
11530b246afaSJeff Mahoney 	ret = btrfs_run_dev_replace(trans, fs_info);
1154c16ce190SJosef Bacik 	if (ret)
1155c16ce190SJosef Bacik 		return ret;
1156280f8bd2SLu Fengqi 	ret = btrfs_run_qgroups(trans);
1157c16ce190SJosef Bacik 	if (ret)
1158c16ce190SJosef Bacik 		return ret;
1159546adb0dSJan Schmidt 
11602ff7e61eSJeff Mahoney 	ret = btrfs_setup_space_cache(trans, fs_info);
1161dcdf7f6dSJosef Bacik 	if (ret)
1162dcdf7f6dSJosef Bacik 		return ret;
1163dcdf7f6dSJosef Bacik 
1164546adb0dSJan Schmidt 	/* run_qgroups might have added some more refs */
1165c79a70b1SNikolay Borisov 	ret = btrfs_run_delayed_refs(trans, (unsigned long)-1);
1166c16ce190SJosef Bacik 	if (ret)
1167c16ce190SJosef Bacik 		return ret;
1168ea526d18SJosef Bacik again:
11690b86a832SChris Mason 	while (!list_empty(&fs_info->dirty_cowonly_roots)) {
11702ff7e61eSJeff Mahoney 		struct btrfs_root *root;
11710b86a832SChris Mason 		next = fs_info->dirty_cowonly_roots.next;
11720b86a832SChris Mason 		list_del_init(next);
11730b86a832SChris Mason 		root = list_entry(next, struct btrfs_root, dirty_list);
1174e7070be1SJosef Bacik 		clear_bit(BTRFS_ROOT_DIRTY, &root->state);
117587ef2bb4SChris Mason 
11769e351cc8SJosef Bacik 		if (root != fs_info->extent_root)
11779e351cc8SJosef Bacik 			list_add_tail(&root->dirty_list,
11789e351cc8SJosef Bacik 				      &trans->transaction->switch_commits);
117949b25e05SJeff Mahoney 		ret = update_cowonly_root(trans, root);
118049b25e05SJeff Mahoney 		if (ret)
118149b25e05SJeff Mahoney 			return ret;
1182c79a70b1SNikolay Borisov 		ret = btrfs_run_delayed_refs(trans, (unsigned long)-1);
1183ea526d18SJosef Bacik 		if (ret)
1184ea526d18SJosef Bacik 			return ret;
118579154b1bSChris Mason 	}
1186276e680dSYan Zheng 
11871bbc621eSChris Mason 	while (!list_empty(dirty_bgs) || !list_empty(io_bgs)) {
11882ff7e61eSJeff Mahoney 		ret = btrfs_write_dirty_block_groups(trans, fs_info);
1189ea526d18SJosef Bacik 		if (ret)
1190ea526d18SJosef Bacik 			return ret;
1191c79a70b1SNikolay Borisov 		ret = btrfs_run_delayed_refs(trans, (unsigned long)-1);
1192ea526d18SJosef Bacik 		if (ret)
1193ea526d18SJosef Bacik 			return ret;
1194ea526d18SJosef Bacik 	}
1195ea526d18SJosef Bacik 
1196ea526d18SJosef Bacik 	if (!list_empty(&fs_info->dirty_cowonly_roots))
1197ea526d18SJosef Bacik 		goto again;
1198ea526d18SJosef Bacik 
11999e351cc8SJosef Bacik 	list_add_tail(&fs_info->extent_root->dirty_list,
12009e351cc8SJosef Bacik 		      &trans->transaction->switch_commits);
12019f6cbcbbSDavid Sterba 
12029f6cbcbbSDavid Sterba 	/* Update dev-replace pointer once everything is committed */
12039f6cbcbbSDavid Sterba 	fs_info->dev_replace.committed_cursor_left =
12049f6cbcbbSDavid Sterba 		fs_info->dev_replace.cursor_left_last_write_of_item;
12058dabb742SStefan Behrens 
120679154b1bSChris Mason 	return 0;
120779154b1bSChris Mason }
120879154b1bSChris Mason 
1209d352ac68SChris Mason /*
1210d352ac68SChris Mason  * dead roots are old snapshots that need to be deleted.  This allocates
1211d352ac68SChris Mason  * a dirty root struct and adds it into the list of dead roots that need to
1212d352ac68SChris Mason  * be deleted
1213d352ac68SChris Mason  */
1214cfad392bSJosef Bacik void btrfs_add_dead_root(struct btrfs_root *root)
12155eda7b5eSChris Mason {
12160b246afaSJeff Mahoney 	struct btrfs_fs_info *fs_info = root->fs_info;
12170b246afaSJeff Mahoney 
12180b246afaSJeff Mahoney 	spin_lock(&fs_info->trans_lock);
1219cfad392bSJosef Bacik 	if (list_empty(&root->root_list))
12200b246afaSJeff Mahoney 		list_add_tail(&root->root_list, &fs_info->dead_roots);
12210b246afaSJeff Mahoney 	spin_unlock(&fs_info->trans_lock);
12225eda7b5eSChris Mason }
12235eda7b5eSChris Mason 
1224d352ac68SChris Mason /*
12255d4f98a2SYan Zheng  * update all the cowonly tree roots on disk
1226d352ac68SChris Mason  */
12277e4443d9SNikolay Borisov static noinline int commit_fs_roots(struct btrfs_trans_handle *trans)
12280f7d52f4SChris Mason {
12297e4443d9SNikolay Borisov 	struct btrfs_fs_info *fs_info = trans->fs_info;
12300f7d52f4SChris Mason 	struct btrfs_root *gang[8];
12310f7d52f4SChris Mason 	int i;
12320f7d52f4SChris Mason 	int ret;
123354aa1f4dSChris Mason 	int err = 0;
123454aa1f4dSChris Mason 
1235a4abeea4SJosef Bacik 	spin_lock(&fs_info->fs_roots_radix_lock);
12360f7d52f4SChris Mason 	while (1) {
12375d4f98a2SYan Zheng 		ret = radix_tree_gang_lookup_tag(&fs_info->fs_roots_radix,
12385d4f98a2SYan Zheng 						 (void **)gang, 0,
12390f7d52f4SChris Mason 						 ARRAY_SIZE(gang),
12400f7d52f4SChris Mason 						 BTRFS_ROOT_TRANS_TAG);
12410f7d52f4SChris Mason 		if (ret == 0)
12420f7d52f4SChris Mason 			break;
12430f7d52f4SChris Mason 		for (i = 0; i < ret; i++) {
12445b4aacefSJeff Mahoney 			struct btrfs_root *root = gang[i];
12455d4f98a2SYan Zheng 			radix_tree_tag_clear(&fs_info->fs_roots_radix,
12462619ba1fSChris Mason 					(unsigned long)root->root_key.objectid,
12470f7d52f4SChris Mason 					BTRFS_ROOT_TRANS_TAG);
1248a4abeea4SJosef Bacik 			spin_unlock(&fs_info->fs_roots_radix_lock);
124931153d81SYan Zheng 
1250e02119d5SChris Mason 			btrfs_free_log(trans, root);
12515d4f98a2SYan Zheng 			btrfs_update_reloc_root(trans, root);
1252e02119d5SChris Mason 
125382d5902dSLi Zefan 			btrfs_save_ino_cache(root, trans);
125482d5902dSLi Zefan 
1255f1ebcc74SLiu Bo 			/* see comments in should_cow_block() */
125627cdeb70SMiao Xie 			clear_bit(BTRFS_ROOT_FORCE_COW, &root->state);
1257c7548af6SChris Mason 			smp_mb__after_atomic();
1258f1ebcc74SLiu Bo 
1259978d910dSYan Zheng 			if (root->commit_root != root->node) {
12609e351cc8SJosef Bacik 				list_add_tail(&root->dirty_list,
12619e351cc8SJosef Bacik 					&trans->transaction->switch_commits);
1262978d910dSYan Zheng 				btrfs_set_root_node(&root->root_item,
1263978d910dSYan Zheng 						    root->node);
1264978d910dSYan Zheng 			}
126531153d81SYan Zheng 
12665d4f98a2SYan Zheng 			err = btrfs_update_root(trans, fs_info->tree_root,
12670f7d52f4SChris Mason 						&root->root_key,
12680f7d52f4SChris Mason 						&root->root_item);
1269a4abeea4SJosef Bacik 			spin_lock(&fs_info->fs_roots_radix_lock);
127054aa1f4dSChris Mason 			if (err)
127154aa1f4dSChris Mason 				break;
1272733e03a0SQu Wenruo 			btrfs_qgroup_free_meta_all_pertrans(root);
12730f7d52f4SChris Mason 		}
12749f3a7427SChris Mason 	}
1275a4abeea4SJosef Bacik 	spin_unlock(&fs_info->fs_roots_radix_lock);
127654aa1f4dSChris Mason 	return err;
12770f7d52f4SChris Mason }
12780f7d52f4SChris Mason 
1279d352ac68SChris Mason /*
1280de78b51aSEric Sandeen  * defrag a given btree.
1281de78b51aSEric Sandeen  * Every leaf in the btree is read and defragged.
1282d352ac68SChris Mason  */
1283de78b51aSEric Sandeen int btrfs_defrag_root(struct btrfs_root *root)
1284e9d0b13bSChris Mason {
1285e9d0b13bSChris Mason 	struct btrfs_fs_info *info = root->fs_info;
1286e9d0b13bSChris Mason 	struct btrfs_trans_handle *trans;
12878929ecfaSYan, Zheng 	int ret;
1288e9d0b13bSChris Mason 
128927cdeb70SMiao Xie 	if (test_and_set_bit(BTRFS_ROOT_DEFRAG_RUNNING, &root->state))
1290e9d0b13bSChris Mason 		return 0;
12918929ecfaSYan, Zheng 
12926b80053dSChris Mason 	while (1) {
12938929ecfaSYan, Zheng 		trans = btrfs_start_transaction(root, 0);
12948929ecfaSYan, Zheng 		if (IS_ERR(trans))
12958929ecfaSYan, Zheng 			return PTR_ERR(trans);
12968929ecfaSYan, Zheng 
1297de78b51aSEric Sandeen 		ret = btrfs_defrag_leaves(trans, root);
12988929ecfaSYan, Zheng 
12993a45bb20SJeff Mahoney 		btrfs_end_transaction(trans);
13002ff7e61eSJeff Mahoney 		btrfs_btree_balance_dirty(info);
1301e9d0b13bSChris Mason 		cond_resched();
1302e9d0b13bSChris Mason 
1303ab8d0fc4SJeff Mahoney 		if (btrfs_fs_closing(info) || ret != -EAGAIN)
1304e9d0b13bSChris Mason 			break;
1305210549ebSDavid Sterba 
1306ab8d0fc4SJeff Mahoney 		if (btrfs_defrag_cancelled(info)) {
1307ab8d0fc4SJeff Mahoney 			btrfs_debug(info, "defrag_root cancelled");
1308210549ebSDavid Sterba 			ret = -EAGAIN;
1309210549ebSDavid Sterba 			break;
1310210549ebSDavid Sterba 		}
1311e9d0b13bSChris Mason 	}
131227cdeb70SMiao Xie 	clear_bit(BTRFS_ROOT_DEFRAG_RUNNING, &root->state);
13138929ecfaSYan, Zheng 	return ret;
1314e9d0b13bSChris Mason }
1315e9d0b13bSChris Mason 
1316d352ac68SChris Mason /*
13176426c7adSQu Wenruo  * Do all special snapshot related qgroup dirty hack.
13186426c7adSQu Wenruo  *
13196426c7adSQu Wenruo  * Will do all needed qgroup inherit and dirty hack like switch commit
13206426c7adSQu Wenruo  * roots inside one transaction and write all btree into disk, to make
13216426c7adSQu Wenruo  * qgroup works.
13226426c7adSQu Wenruo  */
13236426c7adSQu Wenruo static int qgroup_account_snapshot(struct btrfs_trans_handle *trans,
13246426c7adSQu Wenruo 				   struct btrfs_root *src,
13256426c7adSQu Wenruo 				   struct btrfs_root *parent,
13266426c7adSQu Wenruo 				   struct btrfs_qgroup_inherit *inherit,
13276426c7adSQu Wenruo 				   u64 dst_objectid)
13286426c7adSQu Wenruo {
13296426c7adSQu Wenruo 	struct btrfs_fs_info *fs_info = src->fs_info;
13306426c7adSQu Wenruo 	int ret;
13316426c7adSQu Wenruo 
13326426c7adSQu Wenruo 	/*
13336426c7adSQu Wenruo 	 * Save some performance in the case that qgroups are not
13346426c7adSQu Wenruo 	 * enabled. If this check races with the ioctl, rescan will
13356426c7adSQu Wenruo 	 * kick in anyway.
13366426c7adSQu Wenruo 	 */
13379ea6e2b5SDavid Sterba 	if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags))
13386426c7adSQu Wenruo 		return 0;
13396426c7adSQu Wenruo 
13406426c7adSQu Wenruo 	/*
13414d31778aSQu Wenruo 	 * Ensure dirty @src will be commited.  Or, after comming
13424d31778aSQu Wenruo 	 * commit_fs_roots() and switch_commit_roots(), any dirty but not
13434d31778aSQu Wenruo 	 * recorded root will never be updated again, causing an outdated root
13444d31778aSQu Wenruo 	 * item.
13454d31778aSQu Wenruo 	 */
13464d31778aSQu Wenruo 	record_root_in_trans(trans, src, 1);
13474d31778aSQu Wenruo 
13484d31778aSQu Wenruo 	/*
13496426c7adSQu Wenruo 	 * We are going to commit transaction, see btrfs_commit_transaction()
13506426c7adSQu Wenruo 	 * comment for reason locking tree_log_mutex
13516426c7adSQu Wenruo 	 */
13526426c7adSQu Wenruo 	mutex_lock(&fs_info->tree_log_mutex);
13536426c7adSQu Wenruo 
13547e4443d9SNikolay Borisov 	ret = commit_fs_roots(trans);
13556426c7adSQu Wenruo 	if (ret)
13566426c7adSQu Wenruo 		goto out;
1357460fb20aSNikolay Borisov 	ret = btrfs_qgroup_account_extents(trans);
13586426c7adSQu Wenruo 	if (ret < 0)
13596426c7adSQu Wenruo 		goto out;
13606426c7adSQu Wenruo 
13616426c7adSQu Wenruo 	/* Now qgroup are all updated, we can inherit it to new qgroups */
1362a9377422SLu Fengqi 	ret = btrfs_qgroup_inherit(trans, src->root_key.objectid, dst_objectid,
13636426c7adSQu Wenruo 				   inherit);
13646426c7adSQu Wenruo 	if (ret < 0)
13656426c7adSQu Wenruo 		goto out;
13666426c7adSQu Wenruo 
13676426c7adSQu Wenruo 	/*
13686426c7adSQu Wenruo 	 * Now we do a simplified commit transaction, which will:
13696426c7adSQu Wenruo 	 * 1) commit all subvolume and extent tree
13706426c7adSQu Wenruo 	 *    To ensure all subvolume and extent tree have a valid
13716426c7adSQu Wenruo 	 *    commit_root to accounting later insert_dir_item()
13726426c7adSQu Wenruo 	 * 2) write all btree blocks onto disk
13736426c7adSQu Wenruo 	 *    This is to make sure later btree modification will be cowed
13746426c7adSQu Wenruo 	 *    Or commit_root can be populated and cause wrong qgroup numbers
13756426c7adSQu Wenruo 	 * In this simplified commit, we don't really care about other trees
13766426c7adSQu Wenruo 	 * like chunk and root tree, as they won't affect qgroup.
13776426c7adSQu Wenruo 	 * And we don't write super to avoid half committed status.
13786426c7adSQu Wenruo 	 */
13799386d8bcSNikolay Borisov 	ret = commit_cowonly_roots(trans);
13806426c7adSQu Wenruo 	if (ret)
13816426c7adSQu Wenruo 		goto out;
138216916a88SNikolay Borisov 	switch_commit_roots(trans->transaction);
138370458a58SNikolay Borisov 	ret = btrfs_write_and_wait_transaction(trans);
13846426c7adSQu Wenruo 	if (ret)
1385f7af3934SDavid Sterba 		btrfs_handle_fs_error(fs_info, ret,
13866426c7adSQu Wenruo 			"Error while writing out transaction for qgroup");
13876426c7adSQu Wenruo 
13886426c7adSQu Wenruo out:
13896426c7adSQu Wenruo 	mutex_unlock(&fs_info->tree_log_mutex);
13906426c7adSQu Wenruo 
13916426c7adSQu Wenruo 	/*
13926426c7adSQu Wenruo 	 * Force parent root to be updated, as we recorded it before so its
13936426c7adSQu Wenruo 	 * last_trans == cur_transid.
13946426c7adSQu Wenruo 	 * Or it won't be committed again onto disk after later
13956426c7adSQu Wenruo 	 * insert_dir_item()
13966426c7adSQu Wenruo 	 */
13976426c7adSQu Wenruo 	if (!ret)
13986426c7adSQu Wenruo 		record_root_in_trans(trans, parent, 1);
13996426c7adSQu Wenruo 	return ret;
14006426c7adSQu Wenruo }
14016426c7adSQu Wenruo 
14026426c7adSQu Wenruo /*
1403d352ac68SChris Mason  * new snapshots need to be created at a very specific time in the
1404aec8030aSMiao Xie  * transaction commit.  This does the actual creation.
1405aec8030aSMiao Xie  *
1406aec8030aSMiao Xie  * Note:
1407aec8030aSMiao Xie  * If the error which may affect the commitment of the current transaction
1408aec8030aSMiao Xie  * happens, we should return the error number. If the error which just affect
1409aec8030aSMiao Xie  * the creation of the pending snapshots, just return 0.
1410d352ac68SChris Mason  */
141180b6794dSChris Mason static noinline int create_pending_snapshot(struct btrfs_trans_handle *trans,
14123063d29fSChris Mason 				   struct btrfs_pending_snapshot *pending)
14133063d29fSChris Mason {
141408d50ca3SNikolay Borisov 
141508d50ca3SNikolay Borisov 	struct btrfs_fs_info *fs_info = trans->fs_info;
14163063d29fSChris Mason 	struct btrfs_key key;
141780b6794dSChris Mason 	struct btrfs_root_item *new_root_item;
14183063d29fSChris Mason 	struct btrfs_root *tree_root = fs_info->tree_root;
14193063d29fSChris Mason 	struct btrfs_root *root = pending->root;
14206bdb72deSSage Weil 	struct btrfs_root *parent_root;
142198c9942aSLiu Bo 	struct btrfs_block_rsv *rsv;
14226bdb72deSSage Weil 	struct inode *parent_inode;
142342874b3dSMiao Xie 	struct btrfs_path *path;
142442874b3dSMiao Xie 	struct btrfs_dir_item *dir_item;
1425a22285a6SYan, Zheng 	struct dentry *dentry;
14263063d29fSChris Mason 	struct extent_buffer *tmp;
1427925baeddSChris Mason 	struct extent_buffer *old;
142895582b00SDeepa Dinamani 	struct timespec64 cur_time;
1429aec8030aSMiao Xie 	int ret = 0;
1430d68fc57bSYan, Zheng 	u64 to_reserve = 0;
14316bdb72deSSage Weil 	u64 index = 0;
1432a22285a6SYan, Zheng 	u64 objectid;
1433b83cc969SLi Zefan 	u64 root_flags;
14348ea05e3aSAlexander Block 	uuid_le new_uuid;
14353063d29fSChris Mason 
14368546b570SDavid Sterba 	ASSERT(pending->path);
14378546b570SDavid Sterba 	path = pending->path;
143842874b3dSMiao Xie 
1439b0c0ea63SDavid Sterba 	ASSERT(pending->root_item);
1440b0c0ea63SDavid Sterba 	new_root_item = pending->root_item;
1441a22285a6SYan, Zheng 
1442aec8030aSMiao Xie 	pending->error = btrfs_find_free_objectid(tree_root, &objectid);
1443aec8030aSMiao Xie 	if (pending->error)
14446fa9700eSMiao Xie 		goto no_free_objectid;
14453063d29fSChris Mason 
1446d6726335SQu Wenruo 	/*
1447d6726335SQu Wenruo 	 * Make qgroup to skip current new snapshot's qgroupid, as it is
1448d6726335SQu Wenruo 	 * accounted by later btrfs_qgroup_inherit().
1449d6726335SQu Wenruo 	 */
1450d6726335SQu Wenruo 	btrfs_set_skip_qgroup(trans, objectid);
1451d6726335SQu Wenruo 
1452147d256eSZhaolei 	btrfs_reloc_pre_snapshot(pending, &to_reserve);
1453d68fc57bSYan, Zheng 
1454d68fc57bSYan, Zheng 	if (to_reserve > 0) {
1455aec8030aSMiao Xie 		pending->error = btrfs_block_rsv_add(root,
1456aec8030aSMiao Xie 						     &pending->block_rsv,
145708e007d2SMiao Xie 						     to_reserve,
145808e007d2SMiao Xie 						     BTRFS_RESERVE_NO_FLUSH);
1459aec8030aSMiao Xie 		if (pending->error)
1460d6726335SQu Wenruo 			goto clear_skip_qgroup;
1461d68fc57bSYan, Zheng 	}
1462d68fc57bSYan, Zheng 
14633063d29fSChris Mason 	key.objectid = objectid;
1464a22285a6SYan, Zheng 	key.offset = (u64)-1;
1465a22285a6SYan, Zheng 	key.type = BTRFS_ROOT_ITEM_KEY;
14663063d29fSChris Mason 
14676fa9700eSMiao Xie 	rsv = trans->block_rsv;
1468a22285a6SYan, Zheng 	trans->block_rsv = &pending->block_rsv;
14692382c5ccSLiu Bo 	trans->bytes_reserved = trans->block_rsv->reserved;
14700b246afaSJeff Mahoney 	trace_btrfs_space_reservation(fs_info, "transaction",
147188d3a5aaSJosef Bacik 				      trans->transid,
147288d3a5aaSJosef Bacik 				      trans->bytes_reserved, 1);
1473a22285a6SYan, Zheng 	dentry = pending->dentry;
1474e9662f70SMiao Xie 	parent_inode = pending->dir;
1475a22285a6SYan, Zheng 	parent_root = BTRFS_I(parent_inode)->root;
14766426c7adSQu Wenruo 	record_root_in_trans(trans, parent_root, 0);
1477a22285a6SYan, Zheng 
1478c2050a45SDeepa Dinamani 	cur_time = current_time(parent_inode);
147904b285f3SDeepa Dinamani 
14806bdb72deSSage Weil 	/*
14816bdb72deSSage Weil 	 * insert the directory item
14826bdb72deSSage Weil 	 */
1483877574e2SNikolay Borisov 	ret = btrfs_set_inode_index(BTRFS_I(parent_inode), &index);
148449b25e05SJeff Mahoney 	BUG_ON(ret); /* -ENOMEM */
148542874b3dSMiao Xie 
148642874b3dSMiao Xie 	/* check if there is a file/dir which has the same name. */
148742874b3dSMiao Xie 	dir_item = btrfs_lookup_dir_item(NULL, parent_root, path,
14884a0cc7caSNikolay Borisov 					 btrfs_ino(BTRFS_I(parent_inode)),
148942874b3dSMiao Xie 					 dentry->d_name.name,
149042874b3dSMiao Xie 					 dentry->d_name.len, 0);
149142874b3dSMiao Xie 	if (dir_item != NULL && !IS_ERR(dir_item)) {
1492fe66a05aSChris Mason 		pending->error = -EEXIST;
1493aec8030aSMiao Xie 		goto dir_item_existed;
149442874b3dSMiao Xie 	} else if (IS_ERR(dir_item)) {
149542874b3dSMiao Xie 		ret = PTR_ERR(dir_item);
149666642832SJeff Mahoney 		btrfs_abort_transaction(trans, ret);
14978732d44fSMiao Xie 		goto fail;
149879787eaaSJeff Mahoney 	}
149942874b3dSMiao Xie 	btrfs_release_path(path);
15006bdb72deSSage Weil 
1501e999376fSChris Mason 	/*
1502e999376fSChris Mason 	 * pull in the delayed directory update
1503e999376fSChris Mason 	 * and the delayed inode item
1504e999376fSChris Mason 	 * otherwise we corrupt the FS during
1505e999376fSChris Mason 	 * snapshot
1506e999376fSChris Mason 	 */
1507e5c304e6SNikolay Borisov 	ret = btrfs_run_delayed_items(trans);
15088732d44fSMiao Xie 	if (ret) {	/* Transaction aborted */
150966642832SJeff Mahoney 		btrfs_abort_transaction(trans, ret);
15108732d44fSMiao Xie 		goto fail;
15118732d44fSMiao Xie 	}
1512e999376fSChris Mason 
15136426c7adSQu Wenruo 	record_root_in_trans(trans, root, 0);
15146bdb72deSSage Weil 	btrfs_set_root_last_snapshot(&root->root_item, trans->transid);
15156bdb72deSSage Weil 	memcpy(new_root_item, &root->root_item, sizeof(*new_root_item));
151608fe4db1SLi Zefan 	btrfs_check_and_init_root_item(new_root_item);
15176bdb72deSSage Weil 
1518b83cc969SLi Zefan 	root_flags = btrfs_root_flags(new_root_item);
1519b83cc969SLi Zefan 	if (pending->readonly)
1520b83cc969SLi Zefan 		root_flags |= BTRFS_ROOT_SUBVOL_RDONLY;
1521b83cc969SLi Zefan 	else
1522b83cc969SLi Zefan 		root_flags &= ~BTRFS_ROOT_SUBVOL_RDONLY;
1523b83cc969SLi Zefan 	btrfs_set_root_flags(new_root_item, root_flags);
1524b83cc969SLi Zefan 
15258ea05e3aSAlexander Block 	btrfs_set_root_generation_v2(new_root_item,
15268ea05e3aSAlexander Block 			trans->transid);
15278ea05e3aSAlexander Block 	uuid_le_gen(&new_uuid);
15288ea05e3aSAlexander Block 	memcpy(new_root_item->uuid, new_uuid.b, BTRFS_UUID_SIZE);
15298ea05e3aSAlexander Block 	memcpy(new_root_item->parent_uuid, root->root_item.uuid,
15308ea05e3aSAlexander Block 			BTRFS_UUID_SIZE);
153170023da2SStefan Behrens 	if (!(root_flags & BTRFS_ROOT_SUBVOL_RDONLY)) {
153270023da2SStefan Behrens 		memset(new_root_item->received_uuid, 0,
153370023da2SStefan Behrens 		       sizeof(new_root_item->received_uuid));
15348ea05e3aSAlexander Block 		memset(&new_root_item->stime, 0, sizeof(new_root_item->stime));
15358ea05e3aSAlexander Block 		memset(&new_root_item->rtime, 0, sizeof(new_root_item->rtime));
15368ea05e3aSAlexander Block 		btrfs_set_root_stransid(new_root_item, 0);
15378ea05e3aSAlexander Block 		btrfs_set_root_rtransid(new_root_item, 0);
153870023da2SStefan Behrens 	}
15393cae210fSQu Wenruo 	btrfs_set_stack_timespec_sec(&new_root_item->otime, cur_time.tv_sec);
15403cae210fSQu Wenruo 	btrfs_set_stack_timespec_nsec(&new_root_item->otime, cur_time.tv_nsec);
154170023da2SStefan Behrens 	btrfs_set_root_otransid(new_root_item, trans->transid);
15428ea05e3aSAlexander Block 
1543925baeddSChris Mason 	old = btrfs_lock_root_node(root);
154449b25e05SJeff Mahoney 	ret = btrfs_cow_block(trans, root, old, NULL, 0, &old);
154579787eaaSJeff Mahoney 	if (ret) {
154679787eaaSJeff Mahoney 		btrfs_tree_unlock(old);
154779787eaaSJeff Mahoney 		free_extent_buffer(old);
154866642832SJeff Mahoney 		btrfs_abort_transaction(trans, ret);
15498732d44fSMiao Xie 		goto fail;
155079787eaaSJeff Mahoney 	}
155149b25e05SJeff Mahoney 
15525d4f98a2SYan Zheng 	btrfs_set_lock_blocking(old);
15533063d29fSChris Mason 
155449b25e05SJeff Mahoney 	ret = btrfs_copy_root(trans, root, old, &tmp, objectid);
155579787eaaSJeff Mahoney 	/* clean up in any case */
1556925baeddSChris Mason 	btrfs_tree_unlock(old);
1557925baeddSChris Mason 	free_extent_buffer(old);
15588732d44fSMiao Xie 	if (ret) {
155966642832SJeff Mahoney 		btrfs_abort_transaction(trans, ret);
15608732d44fSMiao Xie 		goto fail;
15618732d44fSMiao Xie 	}
1562f1ebcc74SLiu Bo 	/* see comments in should_cow_block() */
156327cdeb70SMiao Xie 	set_bit(BTRFS_ROOT_FORCE_COW, &root->state);
1564f1ebcc74SLiu Bo 	smp_wmb();
1565f1ebcc74SLiu Bo 
15665d4f98a2SYan Zheng 	btrfs_set_root_node(new_root_item, tmp);
1567a22285a6SYan, Zheng 	/* record when the snapshot was created in key.offset */
1568a22285a6SYan, Zheng 	key.offset = trans->transid;
1569a22285a6SYan, Zheng 	ret = btrfs_insert_root(trans, tree_root, &key, new_root_item);
1570925baeddSChris Mason 	btrfs_tree_unlock(tmp);
15713063d29fSChris Mason 	free_extent_buffer(tmp);
15728732d44fSMiao Xie 	if (ret) {
157366642832SJeff Mahoney 		btrfs_abort_transaction(trans, ret);
15748732d44fSMiao Xie 		goto fail;
15758732d44fSMiao Xie 	}
15760660b5afSChris Mason 
1577a22285a6SYan, Zheng 	/*
1578a22285a6SYan, Zheng 	 * insert root back/forward references
1579a22285a6SYan, Zheng 	 */
15806025c19fSLu Fengqi 	ret = btrfs_add_root_ref(trans, objectid,
1581a22285a6SYan, Zheng 				 parent_root->root_key.objectid,
15824a0cc7caSNikolay Borisov 				 btrfs_ino(BTRFS_I(parent_inode)), index,
1583a22285a6SYan, Zheng 				 dentry->d_name.name, dentry->d_name.len);
15848732d44fSMiao Xie 	if (ret) {
158566642832SJeff Mahoney 		btrfs_abort_transaction(trans, ret);
15868732d44fSMiao Xie 		goto fail;
15878732d44fSMiao Xie 	}
1588a22285a6SYan, Zheng 
1589a22285a6SYan, Zheng 	key.offset = (u64)-1;
15900b246afaSJeff Mahoney 	pending->snap = btrfs_read_fs_root_no_name(fs_info, &key);
159179787eaaSJeff Mahoney 	if (IS_ERR(pending->snap)) {
159279787eaaSJeff Mahoney 		ret = PTR_ERR(pending->snap);
159366642832SJeff Mahoney 		btrfs_abort_transaction(trans, ret);
15948732d44fSMiao Xie 		goto fail;
159579787eaaSJeff Mahoney 	}
1596d68fc57bSYan, Zheng 
159749b25e05SJeff Mahoney 	ret = btrfs_reloc_post_snapshot(trans, pending);
15988732d44fSMiao Xie 	if (ret) {
159966642832SJeff Mahoney 		btrfs_abort_transaction(trans, ret);
16008732d44fSMiao Xie 		goto fail;
16018732d44fSMiao Xie 	}
1602361048f5SMiao Xie 
1603c79a70b1SNikolay Borisov 	ret = btrfs_run_delayed_refs(trans, (unsigned long)-1);
16048732d44fSMiao Xie 	if (ret) {
160566642832SJeff Mahoney 		btrfs_abort_transaction(trans, ret);
16068732d44fSMiao Xie 		goto fail;
16078732d44fSMiao Xie 	}
160842874b3dSMiao Xie 
16096426c7adSQu Wenruo 	/*
16106426c7adSQu Wenruo 	 * Do special qgroup accounting for snapshot, as we do some qgroup
16116426c7adSQu Wenruo 	 * snapshot hack to do fast snapshot.
16126426c7adSQu Wenruo 	 * To co-operate with that hack, we do hack again.
16136426c7adSQu Wenruo 	 * Or snapshot will be greatly slowed down by a subtree qgroup rescan
16146426c7adSQu Wenruo 	 */
16156426c7adSQu Wenruo 	ret = qgroup_account_snapshot(trans, root, parent_root,
16166426c7adSQu Wenruo 				      pending->inherit, objectid);
16176426c7adSQu Wenruo 	if (ret < 0)
16186426c7adSQu Wenruo 		goto fail;
16196426c7adSQu Wenruo 
1620684572dfSLu Fengqi 	ret = btrfs_insert_dir_item(trans, dentry->d_name.name,
1621684572dfSLu Fengqi 				    dentry->d_name.len, BTRFS_I(parent_inode),
1622684572dfSLu Fengqi 				    &key, BTRFS_FT_DIR, index);
162342874b3dSMiao Xie 	/* We have check then name at the beginning, so it is impossible. */
16249c52057cSChris Mason 	BUG_ON(ret == -EEXIST || ret == -EOVERFLOW);
16258732d44fSMiao Xie 	if (ret) {
162666642832SJeff Mahoney 		btrfs_abort_transaction(trans, ret);
16278732d44fSMiao Xie 		goto fail;
16288732d44fSMiao Xie 	}
162942874b3dSMiao Xie 
16306ef06d27SNikolay Borisov 	btrfs_i_size_write(BTRFS_I(parent_inode), parent_inode->i_size +
163142874b3dSMiao Xie 					 dentry->d_name.len * 2);
163204b285f3SDeepa Dinamani 	parent_inode->i_mtime = parent_inode->i_ctime =
1633c2050a45SDeepa Dinamani 		current_time(parent_inode);
1634be6aef60SJosef Bacik 	ret = btrfs_update_inode_fallback(trans, parent_root, parent_inode);
1635dd5f9615SStefan Behrens 	if (ret) {
163666642832SJeff Mahoney 		btrfs_abort_transaction(trans, ret);
1637dd5f9615SStefan Behrens 		goto fail;
1638dd5f9615SStefan Behrens 	}
1639cdb345a8SLu Fengqi 	ret = btrfs_uuid_tree_add(trans, new_uuid.b, BTRFS_UUID_KEY_SUBVOL,
1640cdb345a8SLu Fengqi 				  objectid);
1641dd5f9615SStefan Behrens 	if (ret) {
164266642832SJeff Mahoney 		btrfs_abort_transaction(trans, ret);
1643dd5f9615SStefan Behrens 		goto fail;
1644dd5f9615SStefan Behrens 	}
1645dd5f9615SStefan Behrens 	if (!btrfs_is_empty_uuid(new_root_item->received_uuid)) {
1646cdb345a8SLu Fengqi 		ret = btrfs_uuid_tree_add(trans, new_root_item->received_uuid,
1647dd5f9615SStefan Behrens 					  BTRFS_UUID_KEY_RECEIVED_SUBVOL,
1648dd5f9615SStefan Behrens 					  objectid);
1649dd5f9615SStefan Behrens 		if (ret && ret != -EEXIST) {
165066642832SJeff Mahoney 			btrfs_abort_transaction(trans, ret);
1651dd5f9615SStefan Behrens 			goto fail;
1652dd5f9615SStefan Behrens 		}
1653dd5f9615SStefan Behrens 	}
1654d6726335SQu Wenruo 
1655c79a70b1SNikolay Borisov 	ret = btrfs_run_delayed_refs(trans, (unsigned long)-1);
1656d6726335SQu Wenruo 	if (ret) {
165766642832SJeff Mahoney 		btrfs_abort_transaction(trans, ret);
1658d6726335SQu Wenruo 		goto fail;
1659d6726335SQu Wenruo 	}
1660d6726335SQu Wenruo 
16613063d29fSChris Mason fail:
1662aec8030aSMiao Xie 	pending->error = ret;
1663aec8030aSMiao Xie dir_item_existed:
166498c9942aSLiu Bo 	trans->block_rsv = rsv;
16652382c5ccSLiu Bo 	trans->bytes_reserved = 0;
1666d6726335SQu Wenruo clear_skip_qgroup:
1667d6726335SQu Wenruo 	btrfs_clear_skip_qgroup(trans);
16686fa9700eSMiao Xie no_free_objectid:
16696fa9700eSMiao Xie 	kfree(new_root_item);
1670b0c0ea63SDavid Sterba 	pending->root_item = NULL;
167142874b3dSMiao Xie 	btrfs_free_path(path);
16728546b570SDavid Sterba 	pending->path = NULL;
16738546b570SDavid Sterba 
167449b25e05SJeff Mahoney 	return ret;
16753063d29fSChris Mason }
16763063d29fSChris Mason 
1677d352ac68SChris Mason /*
1678d352ac68SChris Mason  * create all the snapshots we've scheduled for creation
1679d352ac68SChris Mason  */
168008d50ca3SNikolay Borisov static noinline int create_pending_snapshots(struct btrfs_trans_handle *trans)
16813063d29fSChris Mason {
1682aec8030aSMiao Xie 	struct btrfs_pending_snapshot *pending, *next;
16833063d29fSChris Mason 	struct list_head *head = &trans->transaction->pending_snapshots;
1684aec8030aSMiao Xie 	int ret = 0;
16853de4586cSChris Mason 
1686aec8030aSMiao Xie 	list_for_each_entry_safe(pending, next, head, list) {
1687aec8030aSMiao Xie 		list_del(&pending->list);
168808d50ca3SNikolay Borisov 		ret = create_pending_snapshot(trans, pending);
1689aec8030aSMiao Xie 		if (ret)
1690aec8030aSMiao Xie 			break;
1691aec8030aSMiao Xie 	}
1692aec8030aSMiao Xie 	return ret;
16933de4586cSChris Mason }
16943de4586cSChris Mason 
16952ff7e61eSJeff Mahoney static void update_super_roots(struct btrfs_fs_info *fs_info)
16965d4f98a2SYan Zheng {
16975d4f98a2SYan Zheng 	struct btrfs_root_item *root_item;
16985d4f98a2SYan Zheng 	struct btrfs_super_block *super;
16995d4f98a2SYan Zheng 
17000b246afaSJeff Mahoney 	super = fs_info->super_copy;
17015d4f98a2SYan Zheng 
17020b246afaSJeff Mahoney 	root_item = &fs_info->chunk_root->root_item;
1703093e037cSDavid Sterba 	super->chunk_root = root_item->bytenr;
1704093e037cSDavid Sterba 	super->chunk_root_generation = root_item->generation;
1705093e037cSDavid Sterba 	super->chunk_root_level = root_item->level;
17065d4f98a2SYan Zheng 
17070b246afaSJeff Mahoney 	root_item = &fs_info->tree_root->root_item;
1708093e037cSDavid Sterba 	super->root = root_item->bytenr;
1709093e037cSDavid Sterba 	super->generation = root_item->generation;
1710093e037cSDavid Sterba 	super->root_level = root_item->level;
17110b246afaSJeff Mahoney 	if (btrfs_test_opt(fs_info, SPACE_CACHE))
1712093e037cSDavid Sterba 		super->cache_generation = root_item->generation;
17130b246afaSJeff Mahoney 	if (test_bit(BTRFS_FS_UPDATE_UUID_TREE_GEN, &fs_info->flags))
1714093e037cSDavid Sterba 		super->uuid_tree_generation = root_item->generation;
17155d4f98a2SYan Zheng }
17165d4f98a2SYan Zheng 
1717f36f3042SChris Mason int btrfs_transaction_in_commit(struct btrfs_fs_info *info)
1718f36f3042SChris Mason {
17194a9d8bdeSMiao Xie 	struct btrfs_transaction *trans;
1720f36f3042SChris Mason 	int ret = 0;
17214a9d8bdeSMiao Xie 
1722a4abeea4SJosef Bacik 	spin_lock(&info->trans_lock);
17234a9d8bdeSMiao Xie 	trans = info->running_transaction;
17244a9d8bdeSMiao Xie 	if (trans)
17254a9d8bdeSMiao Xie 		ret = (trans->state >= TRANS_STATE_COMMIT_START);
1726a4abeea4SJosef Bacik 	spin_unlock(&info->trans_lock);
1727f36f3042SChris Mason 	return ret;
1728f36f3042SChris Mason }
1729f36f3042SChris Mason 
17308929ecfaSYan, Zheng int btrfs_transaction_blocked(struct btrfs_fs_info *info)
17318929ecfaSYan, Zheng {
17324a9d8bdeSMiao Xie 	struct btrfs_transaction *trans;
17338929ecfaSYan, Zheng 	int ret = 0;
17344a9d8bdeSMiao Xie 
1735a4abeea4SJosef Bacik 	spin_lock(&info->trans_lock);
17364a9d8bdeSMiao Xie 	trans = info->running_transaction;
17374a9d8bdeSMiao Xie 	if (trans)
17384a9d8bdeSMiao Xie 		ret = is_transaction_blocked(trans);
1739a4abeea4SJosef Bacik 	spin_unlock(&info->trans_lock);
17408929ecfaSYan, Zheng 	return ret;
17418929ecfaSYan, Zheng }
17428929ecfaSYan, Zheng 
1743bb9c12c9SSage Weil /*
1744bb9c12c9SSage Weil  * wait for the current transaction commit to start and block subsequent
1745bb9c12c9SSage Weil  * transaction joins
1746bb9c12c9SSage Weil  */
17472ff7e61eSJeff Mahoney static void wait_current_trans_commit_start(struct btrfs_fs_info *fs_info,
1748bb9c12c9SSage Weil 					    struct btrfs_transaction *trans)
1749bb9c12c9SSage Weil {
17502ff7e61eSJeff Mahoney 	wait_event(fs_info->transaction_blocked_wait,
17512ff7e61eSJeff Mahoney 		   trans->state >= TRANS_STATE_COMMIT_START || trans->aborted);
1752bb9c12c9SSage Weil }
1753bb9c12c9SSage Weil 
1754bb9c12c9SSage Weil /*
1755bb9c12c9SSage Weil  * wait for the current transaction to start and then become unblocked.
1756bb9c12c9SSage Weil  * caller holds ref.
1757bb9c12c9SSage Weil  */
17582ff7e61eSJeff Mahoney static void wait_current_trans_commit_start_and_unblock(
17592ff7e61eSJeff Mahoney 					struct btrfs_fs_info *fs_info,
1760bb9c12c9SSage Weil 					struct btrfs_transaction *trans)
1761bb9c12c9SSage Weil {
17622ff7e61eSJeff Mahoney 	wait_event(fs_info->transaction_wait,
17632ff7e61eSJeff Mahoney 		   trans->state >= TRANS_STATE_UNBLOCKED || trans->aborted);
1764bb9c12c9SSage Weil }
1765bb9c12c9SSage Weil 
1766bb9c12c9SSage Weil /*
1767bb9c12c9SSage Weil  * commit transactions asynchronously. once btrfs_commit_transaction_async
1768bb9c12c9SSage Weil  * returns, any subsequent transaction will not be allowed to join.
1769bb9c12c9SSage Weil  */
1770bb9c12c9SSage Weil struct btrfs_async_commit {
1771bb9c12c9SSage Weil 	struct btrfs_trans_handle *newtrans;
17727892b5afSMiao Xie 	struct work_struct work;
1773bb9c12c9SSage Weil };
1774bb9c12c9SSage Weil 
1775bb9c12c9SSage Weil static void do_async_commit(struct work_struct *work)
1776bb9c12c9SSage Weil {
1777bb9c12c9SSage Weil 	struct btrfs_async_commit *ac =
17787892b5afSMiao Xie 		container_of(work, struct btrfs_async_commit, work);
1779bb9c12c9SSage Weil 
17806fc4e354SSage Weil 	/*
17816fc4e354SSage Weil 	 * We've got freeze protection passed with the transaction.
17826fc4e354SSage Weil 	 * Tell lockdep about it.
17836fc4e354SSage Weil 	 */
1784b1a06a4bSLiu Bo 	if (ac->newtrans->type & __TRANS_FREEZABLE)
17853a45bb20SJeff Mahoney 		__sb_writers_acquired(ac->newtrans->fs_info->sb, SB_FREEZE_FS);
17866fc4e354SSage Weil 
1787e209db7aSSage Weil 	current->journal_info = ac->newtrans;
1788e209db7aSSage Weil 
17893a45bb20SJeff Mahoney 	btrfs_commit_transaction(ac->newtrans);
1790bb9c12c9SSage Weil 	kfree(ac);
1791bb9c12c9SSage Weil }
1792bb9c12c9SSage Weil 
1793bb9c12c9SSage Weil int btrfs_commit_transaction_async(struct btrfs_trans_handle *trans,
1794bb9c12c9SSage Weil 				   int wait_for_unblock)
1795bb9c12c9SSage Weil {
17963a45bb20SJeff Mahoney 	struct btrfs_fs_info *fs_info = trans->fs_info;
1797bb9c12c9SSage Weil 	struct btrfs_async_commit *ac;
1798bb9c12c9SSage Weil 	struct btrfs_transaction *cur_trans;
1799bb9c12c9SSage Weil 
1800bb9c12c9SSage Weil 	ac = kmalloc(sizeof(*ac), GFP_NOFS);
1801db5b493aSTsutomu Itoh 	if (!ac)
1802db5b493aSTsutomu Itoh 		return -ENOMEM;
1803bb9c12c9SSage Weil 
18047892b5afSMiao Xie 	INIT_WORK(&ac->work, do_async_commit);
18053a45bb20SJeff Mahoney 	ac->newtrans = btrfs_join_transaction(trans->root);
18063612b495STsutomu Itoh 	if (IS_ERR(ac->newtrans)) {
18073612b495STsutomu Itoh 		int err = PTR_ERR(ac->newtrans);
18083612b495STsutomu Itoh 		kfree(ac);
18093612b495STsutomu Itoh 		return err;
18103612b495STsutomu Itoh 	}
1811bb9c12c9SSage Weil 
1812bb9c12c9SSage Weil 	/* take transaction reference */
1813bb9c12c9SSage Weil 	cur_trans = trans->transaction;
18149b64f57dSElena Reshetova 	refcount_inc(&cur_trans->use_count);
1815bb9c12c9SSage Weil 
18163a45bb20SJeff Mahoney 	btrfs_end_transaction(trans);
18176fc4e354SSage Weil 
18186fc4e354SSage Weil 	/*
18196fc4e354SSage Weil 	 * Tell lockdep we've released the freeze rwsem, since the
18206fc4e354SSage Weil 	 * async commit thread will be the one to unlock it.
18216fc4e354SSage Weil 	 */
1822b1a06a4bSLiu Bo 	if (ac->newtrans->type & __TRANS_FREEZABLE)
18230b246afaSJeff Mahoney 		__sb_writers_release(fs_info->sb, SB_FREEZE_FS);
18246fc4e354SSage Weil 
18257892b5afSMiao Xie 	schedule_work(&ac->work);
1826bb9c12c9SSage Weil 
1827bb9c12c9SSage Weil 	/* wait for transaction to start and unblock */
1828bb9c12c9SSage Weil 	if (wait_for_unblock)
18292ff7e61eSJeff Mahoney 		wait_current_trans_commit_start_and_unblock(fs_info, cur_trans);
1830bb9c12c9SSage Weil 	else
18312ff7e61eSJeff Mahoney 		wait_current_trans_commit_start(fs_info, cur_trans);
1832bb9c12c9SSage Weil 
183338e88054SSage Weil 	if (current->journal_info == trans)
183438e88054SSage Weil 		current->journal_info = NULL;
183538e88054SSage Weil 
1836724e2315SJosef Bacik 	btrfs_put_transaction(cur_trans);
1837bb9c12c9SSage Weil 	return 0;
1838bb9c12c9SSage Weil }
1839bb9c12c9SSage Weil 
184049b25e05SJeff Mahoney 
184197cb39bbSNikolay Borisov static void cleanup_transaction(struct btrfs_trans_handle *trans, int err)
184249b25e05SJeff Mahoney {
184397cb39bbSNikolay Borisov 	struct btrfs_fs_info *fs_info = trans->fs_info;
184449b25e05SJeff Mahoney 	struct btrfs_transaction *cur_trans = trans->transaction;
1845f094ac32SLiu Bo 	DEFINE_WAIT(wait);
184649b25e05SJeff Mahoney 
1847b50fff81SDavid Sterba 	WARN_ON(refcount_read(&trans->use_count) > 1);
184849b25e05SJeff Mahoney 
184966642832SJeff Mahoney 	btrfs_abort_transaction(trans, err);
18507b8b92afSJosef Bacik 
18510b246afaSJeff Mahoney 	spin_lock(&fs_info->trans_lock);
185266b6135bSLiu Bo 
185325d8c284SMiao Xie 	/*
185425d8c284SMiao Xie 	 * If the transaction is removed from the list, it means this
185525d8c284SMiao Xie 	 * transaction has been committed successfully, so it is impossible
185625d8c284SMiao Xie 	 * to call the cleanup function.
185725d8c284SMiao Xie 	 */
185825d8c284SMiao Xie 	BUG_ON(list_empty(&cur_trans->list));
185966b6135bSLiu Bo 
186049b25e05SJeff Mahoney 	list_del_init(&cur_trans->list);
18610b246afaSJeff Mahoney 	if (cur_trans == fs_info->running_transaction) {
18624a9d8bdeSMiao Xie 		cur_trans->state = TRANS_STATE_COMMIT_DOING;
18630b246afaSJeff Mahoney 		spin_unlock(&fs_info->trans_lock);
1864f094ac32SLiu Bo 		wait_event(cur_trans->writer_wait,
1865f094ac32SLiu Bo 			   atomic_read(&cur_trans->num_writers) == 1);
1866f094ac32SLiu Bo 
18670b246afaSJeff Mahoney 		spin_lock(&fs_info->trans_lock);
1868d7096fc3SJosef Bacik 	}
18690b246afaSJeff Mahoney 	spin_unlock(&fs_info->trans_lock);
187049b25e05SJeff Mahoney 
18712ff7e61eSJeff Mahoney 	btrfs_cleanup_one_transaction(trans->transaction, fs_info);
187249b25e05SJeff Mahoney 
18730b246afaSJeff Mahoney 	spin_lock(&fs_info->trans_lock);
18740b246afaSJeff Mahoney 	if (cur_trans == fs_info->running_transaction)
18750b246afaSJeff Mahoney 		fs_info->running_transaction = NULL;
18760b246afaSJeff Mahoney 	spin_unlock(&fs_info->trans_lock);
18774a9d8bdeSMiao Xie 
1878e0228285SJosef Bacik 	if (trans->type & __TRANS_FREEZABLE)
18790b246afaSJeff Mahoney 		sb_end_intwrite(fs_info->sb);
1880724e2315SJosef Bacik 	btrfs_put_transaction(cur_trans);
1881724e2315SJosef Bacik 	btrfs_put_transaction(cur_trans);
188249b25e05SJeff Mahoney 
188397cb39bbSNikolay Borisov 	trace_btrfs_transaction_commit(trans->root);
188449b25e05SJeff Mahoney 
188549b25e05SJeff Mahoney 	if (current->journal_info == trans)
188649b25e05SJeff Mahoney 		current->journal_info = NULL;
18870b246afaSJeff Mahoney 	btrfs_scrub_cancel(fs_info);
188849b25e05SJeff Mahoney 
188949b25e05SJeff Mahoney 	kmem_cache_free(btrfs_trans_handle_cachep, trans);
189049b25e05SJeff Mahoney }
189149b25e05SJeff Mahoney 
189282436617SMiao Xie static inline int btrfs_start_delalloc_flush(struct btrfs_fs_info *fs_info)
189382436617SMiao Xie {
1894ce8ea7ccSJosef Bacik 	/*
1895ce8ea7ccSJosef Bacik 	 * We use writeback_inodes_sb here because if we used
1896ce8ea7ccSJosef Bacik 	 * btrfs_start_delalloc_roots we would deadlock with fs freeze.
1897ce8ea7ccSJosef Bacik 	 * Currently are holding the fs freeze lock, if we do an async flush
1898ce8ea7ccSJosef Bacik 	 * we'll do btrfs_join_transaction() and deadlock because we need to
1899ce8ea7ccSJosef Bacik 	 * wait for the fs freeze lock.  Using the direct flushing we benefit
1900ce8ea7ccSJosef Bacik 	 * from already being in a transaction and our join_transaction doesn't
1901ce8ea7ccSJosef Bacik 	 * have to re-take the fs freeze lock.
1902ce8ea7ccSJosef Bacik 	 */
19033cdde224SJeff Mahoney 	if (btrfs_test_opt(fs_info, FLUSHONCOMMIT))
1904ce8ea7ccSJosef Bacik 		writeback_inodes_sb(fs_info->sb, WB_REASON_SYNC);
190582436617SMiao Xie 	return 0;
190682436617SMiao Xie }
190782436617SMiao Xie 
190882436617SMiao Xie static inline void btrfs_wait_delalloc_flush(struct btrfs_fs_info *fs_info)
190982436617SMiao Xie {
19103cdde224SJeff Mahoney 	if (btrfs_test_opt(fs_info, FLUSHONCOMMIT))
19116374e57aSChris Mason 		btrfs_wait_ordered_roots(fs_info, U64_MAX, 0, (u64)-1);
191282436617SMiao Xie }
191382436617SMiao Xie 
191450d9aa99SJosef Bacik static inline void
1915161c3549SJosef Bacik btrfs_wait_pending_ordered(struct btrfs_transaction *cur_trans)
191650d9aa99SJosef Bacik {
1917161c3549SJosef Bacik 	wait_event(cur_trans->pending_wait,
1918161c3549SJosef Bacik 		   atomic_read(&cur_trans->pending_ordered) == 0);
191950d9aa99SJosef Bacik }
192050d9aa99SJosef Bacik 
19213a45bb20SJeff Mahoney int btrfs_commit_transaction(struct btrfs_trans_handle *trans)
192279154b1bSChris Mason {
19233a45bb20SJeff Mahoney 	struct btrfs_fs_info *fs_info = trans->fs_info;
192449b25e05SJeff Mahoney 	struct btrfs_transaction *cur_trans = trans->transaction;
19258fd17795SChris Mason 	struct btrfs_transaction *prev_trans = NULL;
192625287e0aSMiao Xie 	int ret;
192779154b1bSChris Mason 
19288d25a086SMiao Xie 	/* Stop the commit early if ->aborted is set */
192920c7bcecSSeraphime Kirkovski 	if (unlikely(READ_ONCE(cur_trans->aborted))) {
193025287e0aSMiao Xie 		ret = cur_trans->aborted;
19313a45bb20SJeff Mahoney 		btrfs_end_transaction(trans);
1932e4a2bcacSJosef Bacik 		return ret;
193325287e0aSMiao Xie 	}
193449b25e05SJeff Mahoney 
1935f45c752bSJosef Bacik 	btrfs_trans_release_metadata(trans);
1936f45c752bSJosef Bacik 	trans->block_rsv = NULL;
1937f45c752bSJosef Bacik 
193856bec294SChris Mason 	/* make a pass through all the delayed refs we have so far
193956bec294SChris Mason 	 * any runnings procs may add more while we are here
194056bec294SChris Mason 	 */
1941c79a70b1SNikolay Borisov 	ret = btrfs_run_delayed_refs(trans, 0);
1942e4a2bcacSJosef Bacik 	if (ret) {
19433a45bb20SJeff Mahoney 		btrfs_end_transaction(trans);
1944e4a2bcacSJosef Bacik 		return ret;
1945e4a2bcacSJosef Bacik 	}
194656bec294SChris Mason 
1947b7ec40d7SChris Mason 	cur_trans = trans->transaction;
194849b25e05SJeff Mahoney 
194956bec294SChris Mason 	/*
195056bec294SChris Mason 	 * set the flushing flag so procs in this transaction have to
195156bec294SChris Mason 	 * start sending their work down.
195256bec294SChris Mason 	 */
1953b7ec40d7SChris Mason 	cur_trans->delayed_refs.flushing = 1;
19541be41b78SJosef Bacik 	smp_wmb();
195556bec294SChris Mason 
1956ea658badSJosef Bacik 	if (!list_empty(&trans->new_bgs))
19576c686b35SNikolay Borisov 		btrfs_create_pending_block_groups(trans);
1958ea658badSJosef Bacik 
1959c79a70b1SNikolay Borisov 	ret = btrfs_run_delayed_refs(trans, 0);
1960e4a2bcacSJosef Bacik 	if (ret) {
19613a45bb20SJeff Mahoney 		btrfs_end_transaction(trans);
1962e4a2bcacSJosef Bacik 		return ret;
1963e4a2bcacSJosef Bacik 	}
196456bec294SChris Mason 
19653204d33cSJosef Bacik 	if (!test_bit(BTRFS_TRANS_DIRTY_BG_RUN, &cur_trans->flags)) {
19661bbc621eSChris Mason 		int run_it = 0;
19671bbc621eSChris Mason 
19681bbc621eSChris Mason 		/* this mutex is also taken before trying to set
19691bbc621eSChris Mason 		 * block groups readonly.  We need to make sure
19701bbc621eSChris Mason 		 * that nobody has set a block group readonly
19711bbc621eSChris Mason 		 * after a extents from that block group have been
19721bbc621eSChris Mason 		 * allocated for cache files.  btrfs_set_block_group_ro
19731bbc621eSChris Mason 		 * will wait for the transaction to commit if it
19743204d33cSJosef Bacik 		 * finds BTRFS_TRANS_DIRTY_BG_RUN set.
19751bbc621eSChris Mason 		 *
19763204d33cSJosef Bacik 		 * The BTRFS_TRANS_DIRTY_BG_RUN flag is also used to make sure
19773204d33cSJosef Bacik 		 * only one process starts all the block group IO.  It wouldn't
19781bbc621eSChris Mason 		 * hurt to have more than one go through, but there's no
19791bbc621eSChris Mason 		 * real advantage to it either.
19801bbc621eSChris Mason 		 */
19810b246afaSJeff Mahoney 		mutex_lock(&fs_info->ro_block_group_mutex);
19823204d33cSJosef Bacik 		if (!test_and_set_bit(BTRFS_TRANS_DIRTY_BG_RUN,
19833204d33cSJosef Bacik 				      &cur_trans->flags))
19841bbc621eSChris Mason 			run_it = 1;
19850b246afaSJeff Mahoney 		mutex_unlock(&fs_info->ro_block_group_mutex);
19861bbc621eSChris Mason 
1987f9cacae3SNikolay Borisov 		if (run_it) {
198821217054SNikolay Borisov 			ret = btrfs_start_dirty_block_groups(trans);
19891bbc621eSChris Mason 			if (ret) {
19903a45bb20SJeff Mahoney 				btrfs_end_transaction(trans);
19911bbc621eSChris Mason 				return ret;
19921bbc621eSChris Mason 			}
1993f9cacae3SNikolay Borisov 		}
1994f9cacae3SNikolay Borisov 	}
19951bbc621eSChris Mason 
19960b246afaSJeff Mahoney 	spin_lock(&fs_info->trans_lock);
19974a9d8bdeSMiao Xie 	if (cur_trans->state >= TRANS_STATE_COMMIT_START) {
19980b246afaSJeff Mahoney 		spin_unlock(&fs_info->trans_lock);
19999b64f57dSElena Reshetova 		refcount_inc(&cur_trans->use_count);
20003a45bb20SJeff Mahoney 		ret = btrfs_end_transaction(trans);
2001ccd467d6SChris Mason 
20022ff7e61eSJeff Mahoney 		wait_for_commit(cur_trans);
200315ee9bc7SJosef Bacik 
2004b4924a0fSLiu Bo 		if (unlikely(cur_trans->aborted))
2005b4924a0fSLiu Bo 			ret = cur_trans->aborted;
2006b4924a0fSLiu Bo 
2007724e2315SJosef Bacik 		btrfs_put_transaction(cur_trans);
200815ee9bc7SJosef Bacik 
200949b25e05SJeff Mahoney 		return ret;
201079154b1bSChris Mason 	}
20114313b399SChris Mason 
20124a9d8bdeSMiao Xie 	cur_trans->state = TRANS_STATE_COMMIT_START;
20130b246afaSJeff Mahoney 	wake_up(&fs_info->transaction_blocked_wait);
2014bb9c12c9SSage Weil 
20150b246afaSJeff Mahoney 	if (cur_trans->list.prev != &fs_info->trans_list) {
2016ccd467d6SChris Mason 		prev_trans = list_entry(cur_trans->list.prev,
2017ccd467d6SChris Mason 					struct btrfs_transaction, list);
20184a9d8bdeSMiao Xie 		if (prev_trans->state != TRANS_STATE_COMPLETED) {
20199b64f57dSElena Reshetova 			refcount_inc(&prev_trans->use_count);
20200b246afaSJeff Mahoney 			spin_unlock(&fs_info->trans_lock);
2021ccd467d6SChris Mason 
20222ff7e61eSJeff Mahoney 			wait_for_commit(prev_trans);
20231f9b8c8fSFilipe Manana 			ret = prev_trans->aborted;
2024ccd467d6SChris Mason 
2025724e2315SJosef Bacik 			btrfs_put_transaction(prev_trans);
20261f9b8c8fSFilipe Manana 			if (ret)
20271f9b8c8fSFilipe Manana 				goto cleanup_transaction;
2028a4abeea4SJosef Bacik 		} else {
20290b246afaSJeff Mahoney 			spin_unlock(&fs_info->trans_lock);
2030ccd467d6SChris Mason 		}
2031a4abeea4SJosef Bacik 	} else {
20320b246afaSJeff Mahoney 		spin_unlock(&fs_info->trans_lock);
2033ccd467d6SChris Mason 	}
203415ee9bc7SJosef Bacik 
20350860adfdSMiao Xie 	extwriter_counter_dec(cur_trans, trans->type);
20360860adfdSMiao Xie 
20370b246afaSJeff Mahoney 	ret = btrfs_start_delalloc_flush(fs_info);
203882436617SMiao Xie 	if (ret)
203982436617SMiao Xie 		goto cleanup_transaction;
204082436617SMiao Xie 
2041e5c304e6SNikolay Borisov 	ret = btrfs_run_delayed_items(trans);
204249b25e05SJeff Mahoney 	if (ret)
204349b25e05SJeff Mahoney 		goto cleanup_transaction;
204416cdcec7SMiao Xie 
2045581227d0SMiao Xie 	wait_event(cur_trans->writer_wait,
2046581227d0SMiao Xie 		   extwriter_counter_read(cur_trans) == 0);
2047ed3b3d31SChris Mason 
2048581227d0SMiao Xie 	/* some pending stuffs might be added after the previous flush. */
2049e5c304e6SNikolay Borisov 	ret = btrfs_run_delayed_items(trans);
2050ca469637SMiao Xie 	if (ret)
2051ca469637SMiao Xie 		goto cleanup_transaction;
2052ca469637SMiao Xie 
20530b246afaSJeff Mahoney 	btrfs_wait_delalloc_flush(fs_info);
2054cb7ab021SWang Shilong 
2055161c3549SJosef Bacik 	btrfs_wait_pending_ordered(cur_trans);
205650d9aa99SJosef Bacik 
20572ff7e61eSJeff Mahoney 	btrfs_scrub_pause(fs_info);
2058ed0ca140SJosef Bacik 	/*
2059ed0ca140SJosef Bacik 	 * Ok now we need to make sure to block out any other joins while we
2060ed0ca140SJosef Bacik 	 * commit the transaction.  We could have started a join before setting
20614a9d8bdeSMiao Xie 	 * COMMIT_DOING so make sure to wait for num_writers to == 1 again.
2062ed0ca140SJosef Bacik 	 */
20630b246afaSJeff Mahoney 	spin_lock(&fs_info->trans_lock);
20644a9d8bdeSMiao Xie 	cur_trans->state = TRANS_STATE_COMMIT_DOING;
20650b246afaSJeff Mahoney 	spin_unlock(&fs_info->trans_lock);
2066ed0ca140SJosef Bacik 	wait_event(cur_trans->writer_wait,
2067ed0ca140SJosef Bacik 		   atomic_read(&cur_trans->num_writers) == 1);
206815ee9bc7SJosef Bacik 
20692cba30f1SMiao Xie 	/* ->aborted might be set after the previous check, so check it */
207020c7bcecSSeraphime Kirkovski 	if (unlikely(READ_ONCE(cur_trans->aborted))) {
20712cba30f1SMiao Xie 		ret = cur_trans->aborted;
20726cf7f77eSWang Shilong 		goto scrub_continue;
20732cba30f1SMiao Xie 	}
20747585717fSChris Mason 	/*
20757585717fSChris Mason 	 * the reloc mutex makes sure that we stop
20767585717fSChris Mason 	 * the balancing code from coming in and moving
20777585717fSChris Mason 	 * extents around in the middle of the commit
20787585717fSChris Mason 	 */
20790b246afaSJeff Mahoney 	mutex_lock(&fs_info->reloc_mutex);
20807585717fSChris Mason 
208142874b3dSMiao Xie 	/*
208242874b3dSMiao Xie 	 * We needn't worry about the delayed items because we will
208342874b3dSMiao Xie 	 * deal with them in create_pending_snapshot(), which is the
208442874b3dSMiao Xie 	 * core function of the snapshot creation.
208542874b3dSMiao Xie 	 */
208608d50ca3SNikolay Borisov 	ret = create_pending_snapshots(trans);
208749b25e05SJeff Mahoney 	if (ret) {
20880b246afaSJeff Mahoney 		mutex_unlock(&fs_info->reloc_mutex);
20896cf7f77eSWang Shilong 		goto scrub_continue;
209049b25e05SJeff Mahoney 	}
20913063d29fSChris Mason 
209242874b3dSMiao Xie 	/*
209342874b3dSMiao Xie 	 * We insert the dir indexes of the snapshots and update the inode
209442874b3dSMiao Xie 	 * of the snapshots' parents after the snapshot creation, so there
209542874b3dSMiao Xie 	 * are some delayed items which are not dealt with. Now deal with
209642874b3dSMiao Xie 	 * them.
209742874b3dSMiao Xie 	 *
209842874b3dSMiao Xie 	 * We needn't worry that this operation will corrupt the snapshots,
209942874b3dSMiao Xie 	 * because all the tree which are snapshoted will be forced to COW
210042874b3dSMiao Xie 	 * the nodes and leaves.
210142874b3dSMiao Xie 	 */
2102e5c304e6SNikolay Borisov 	ret = btrfs_run_delayed_items(trans);
210349b25e05SJeff Mahoney 	if (ret) {
21040b246afaSJeff Mahoney 		mutex_unlock(&fs_info->reloc_mutex);
21056cf7f77eSWang Shilong 		goto scrub_continue;
210649b25e05SJeff Mahoney 	}
210716cdcec7SMiao Xie 
2108c79a70b1SNikolay Borisov 	ret = btrfs_run_delayed_refs(trans, (unsigned long)-1);
210949b25e05SJeff Mahoney 	if (ret) {
21100b246afaSJeff Mahoney 		mutex_unlock(&fs_info->reloc_mutex);
21116cf7f77eSWang Shilong 		goto scrub_continue;
211249b25e05SJeff Mahoney 	}
211356bec294SChris Mason 
2114e999376fSChris Mason 	/*
2115e999376fSChris Mason 	 * make sure none of the code above managed to slip in a
2116e999376fSChris Mason 	 * delayed item
2117e999376fSChris Mason 	 */
2118ccdf9b30SJeff Mahoney 	btrfs_assert_delayed_root_empty(fs_info);
2119e999376fSChris Mason 
21202c90e5d6SChris Mason 	WARN_ON(cur_trans != trans->transaction);
2121dc17ff8fSChris Mason 
2122e02119d5SChris Mason 	/* btrfs_commit_tree_roots is responsible for getting the
2123e02119d5SChris Mason 	 * various roots consistent with each other.  Every pointer
2124e02119d5SChris Mason 	 * in the tree of tree roots has to point to the most up to date
2125e02119d5SChris Mason 	 * root for every subvolume and other tree.  So, we have to keep
2126e02119d5SChris Mason 	 * the tree logging code from jumping in and changing any
2127e02119d5SChris Mason 	 * of the trees.
2128e02119d5SChris Mason 	 *
2129e02119d5SChris Mason 	 * At this point in the commit, there can't be any tree-log
2130e02119d5SChris Mason 	 * writers, but a little lower down we drop the trans mutex
2131e02119d5SChris Mason 	 * and let new people in.  By holding the tree_log_mutex
2132e02119d5SChris Mason 	 * from now until after the super is written, we avoid races
2133e02119d5SChris Mason 	 * with the tree-log code.
2134e02119d5SChris Mason 	 */
21350b246afaSJeff Mahoney 	mutex_lock(&fs_info->tree_log_mutex);
21361a40e23bSZheng Yan 
21377e4443d9SNikolay Borisov 	ret = commit_fs_roots(trans);
213849b25e05SJeff Mahoney 	if (ret) {
21390b246afaSJeff Mahoney 		mutex_unlock(&fs_info->tree_log_mutex);
21400b246afaSJeff Mahoney 		mutex_unlock(&fs_info->reloc_mutex);
21416cf7f77eSWang Shilong 		goto scrub_continue;
214249b25e05SJeff Mahoney 	}
214354aa1f4dSChris Mason 
21443818aea2SQu Wenruo 	/*
21457e1876acSDavid Sterba 	 * Since the transaction is done, we can apply the pending changes
21467e1876acSDavid Sterba 	 * before the next transaction.
21473818aea2SQu Wenruo 	 */
21480b246afaSJeff Mahoney 	btrfs_apply_pending_changes(fs_info);
21493818aea2SQu Wenruo 
21505d4f98a2SYan Zheng 	/* commit_fs_roots gets rid of all the tree log roots, it is now
2151e02119d5SChris Mason 	 * safe to free the root of tree log roots
2152e02119d5SChris Mason 	 */
21530b246afaSJeff Mahoney 	btrfs_free_log_root_tree(trans, fs_info);
2154e02119d5SChris Mason 
21550ed4792aSQu Wenruo 	/*
215682bafb38SQu Wenruo 	 * commit_fs_roots() can call btrfs_save_ino_cache(), which generates
215782bafb38SQu Wenruo 	 * new delayed refs. Must handle them or qgroup can be wrong.
215882bafb38SQu Wenruo 	 */
2159c79a70b1SNikolay Borisov 	ret = btrfs_run_delayed_refs(trans, (unsigned long)-1);
216082bafb38SQu Wenruo 	if (ret) {
216182bafb38SQu Wenruo 		mutex_unlock(&fs_info->tree_log_mutex);
216282bafb38SQu Wenruo 		mutex_unlock(&fs_info->reloc_mutex);
216382bafb38SQu Wenruo 		goto scrub_continue;
216482bafb38SQu Wenruo 	}
216582bafb38SQu Wenruo 
216682bafb38SQu Wenruo 	/*
21670ed4792aSQu Wenruo 	 * Since fs roots are all committed, we can get a quite accurate
21680ed4792aSQu Wenruo 	 * new_roots. So let's do quota accounting.
21690ed4792aSQu Wenruo 	 */
2170460fb20aSNikolay Borisov 	ret = btrfs_qgroup_account_extents(trans);
21710ed4792aSQu Wenruo 	if (ret < 0) {
21720b246afaSJeff Mahoney 		mutex_unlock(&fs_info->tree_log_mutex);
21730b246afaSJeff Mahoney 		mutex_unlock(&fs_info->reloc_mutex);
21740ed4792aSQu Wenruo 		goto scrub_continue;
21750ed4792aSQu Wenruo 	}
21760ed4792aSQu Wenruo 
21779386d8bcSNikolay Borisov 	ret = commit_cowonly_roots(trans);
217849b25e05SJeff Mahoney 	if (ret) {
21790b246afaSJeff Mahoney 		mutex_unlock(&fs_info->tree_log_mutex);
21800b246afaSJeff Mahoney 		mutex_unlock(&fs_info->reloc_mutex);
21816cf7f77eSWang Shilong 		goto scrub_continue;
218249b25e05SJeff Mahoney 	}
218354aa1f4dSChris Mason 
21842cba30f1SMiao Xie 	/*
21852cba30f1SMiao Xie 	 * The tasks which save the space cache and inode cache may also
21862cba30f1SMiao Xie 	 * update ->aborted, check it.
21872cba30f1SMiao Xie 	 */
218820c7bcecSSeraphime Kirkovski 	if (unlikely(READ_ONCE(cur_trans->aborted))) {
21892cba30f1SMiao Xie 		ret = cur_trans->aborted;
21900b246afaSJeff Mahoney 		mutex_unlock(&fs_info->tree_log_mutex);
21910b246afaSJeff Mahoney 		mutex_unlock(&fs_info->reloc_mutex);
21926cf7f77eSWang Shilong 		goto scrub_continue;
21932cba30f1SMiao Xie 	}
21942cba30f1SMiao Xie 
21958b74c03eSDavid Sterba 	btrfs_prepare_extent_commit(fs_info);
219611833d66SYan Zheng 
21970b246afaSJeff Mahoney 	cur_trans = fs_info->running_transaction;
21985f39d397SChris Mason 
21990b246afaSJeff Mahoney 	btrfs_set_root_node(&fs_info->tree_root->root_item,
22000b246afaSJeff Mahoney 			    fs_info->tree_root->node);
22010b246afaSJeff Mahoney 	list_add_tail(&fs_info->tree_root->dirty_list,
22029e351cc8SJosef Bacik 		      &cur_trans->switch_commits);
22035d4f98a2SYan Zheng 
22040b246afaSJeff Mahoney 	btrfs_set_root_node(&fs_info->chunk_root->root_item,
22050b246afaSJeff Mahoney 			    fs_info->chunk_root->node);
22060b246afaSJeff Mahoney 	list_add_tail(&fs_info->chunk_root->dirty_list,
22079e351cc8SJosef Bacik 		      &cur_trans->switch_commits);
22089e351cc8SJosef Bacik 
220916916a88SNikolay Borisov 	switch_commit_roots(cur_trans);
22105d4f98a2SYan Zheng 
2211ce93ec54SJosef Bacik 	ASSERT(list_empty(&cur_trans->dirty_bgs));
22121bbc621eSChris Mason 	ASSERT(list_empty(&cur_trans->io_bgs));
22132ff7e61eSJeff Mahoney 	update_super_roots(fs_info);
2214e02119d5SChris Mason 
22150b246afaSJeff Mahoney 	btrfs_set_super_log_root(fs_info->super_copy, 0);
22160b246afaSJeff Mahoney 	btrfs_set_super_log_root_level(fs_info->super_copy, 0);
22170b246afaSJeff Mahoney 	memcpy(fs_info->super_for_commit, fs_info->super_copy,
22180b246afaSJeff Mahoney 	       sizeof(*fs_info->super_copy));
2219ccd467d6SChris Mason 
22200b246afaSJeff Mahoney 	btrfs_update_commit_device_size(fs_info);
2221e9b919b1SNikolay Borisov 	btrfs_update_commit_device_bytes_used(cur_trans);
2222935e5cc9SMiao Xie 
22230b246afaSJeff Mahoney 	clear_bit(BTRFS_FS_LOG1_ERR, &fs_info->flags);
22240b246afaSJeff Mahoney 	clear_bit(BTRFS_FS_LOG2_ERR, &fs_info->flags);
2225656f30dbSFilipe Manana 
22264fbcdf66SFilipe Manana 	btrfs_trans_release_chunk_metadata(trans);
22274fbcdf66SFilipe Manana 
22280b246afaSJeff Mahoney 	spin_lock(&fs_info->trans_lock);
22294a9d8bdeSMiao Xie 	cur_trans->state = TRANS_STATE_UNBLOCKED;
22300b246afaSJeff Mahoney 	fs_info->running_transaction = NULL;
22310b246afaSJeff Mahoney 	spin_unlock(&fs_info->trans_lock);
22320b246afaSJeff Mahoney 	mutex_unlock(&fs_info->reloc_mutex);
2233b7ec40d7SChris Mason 
22340b246afaSJeff Mahoney 	wake_up(&fs_info->transaction_wait);
2235e6dcd2dcSChris Mason 
223670458a58SNikolay Borisov 	ret = btrfs_write_and_wait_transaction(trans);
223749b25e05SJeff Mahoney 	if (ret) {
22380b246afaSJeff Mahoney 		btrfs_handle_fs_error(fs_info, ret,
223908748810SDavid Sterba 				      "Error while writing out transaction");
22400b246afaSJeff Mahoney 		mutex_unlock(&fs_info->tree_log_mutex);
22416cf7f77eSWang Shilong 		goto scrub_continue;
224249b25e05SJeff Mahoney 	}
224349b25e05SJeff Mahoney 
2244eece6a9cSDavid Sterba 	ret = write_all_supers(fs_info, 0);
2245e02119d5SChris Mason 	/*
2246e02119d5SChris Mason 	 * the super is written, we can safely allow the tree-loggers
2247e02119d5SChris Mason 	 * to go about their business
2248e02119d5SChris Mason 	 */
22490b246afaSJeff Mahoney 	mutex_unlock(&fs_info->tree_log_mutex);
2250c1f32b7cSAnand Jain 	if (ret)
2251c1f32b7cSAnand Jain 		goto scrub_continue;
2252e02119d5SChris Mason 
22535ead2dd0SNikolay Borisov 	btrfs_finish_extent_commit(trans);
22544313b399SChris Mason 
22553204d33cSJosef Bacik 	if (test_bit(BTRFS_TRANS_HAVE_FREE_BGS, &cur_trans->flags))
22560b246afaSJeff Mahoney 		btrfs_clear_space_info_full(fs_info);
225713212b54SZhao Lei 
22580b246afaSJeff Mahoney 	fs_info->last_trans_committed = cur_trans->transid;
22594a9d8bdeSMiao Xie 	/*
22604a9d8bdeSMiao Xie 	 * We needn't acquire the lock here because there is no other task
22614a9d8bdeSMiao Xie 	 * which can change it.
22624a9d8bdeSMiao Xie 	 */
22634a9d8bdeSMiao Xie 	cur_trans->state = TRANS_STATE_COMPLETED;
22642c90e5d6SChris Mason 	wake_up(&cur_trans->commit_wait);
2265a514d638SQu Wenruo 	clear_bit(BTRFS_FS_NEED_ASYNC_COMMIT, &fs_info->flags);
22663de4586cSChris Mason 
22670b246afaSJeff Mahoney 	spin_lock(&fs_info->trans_lock);
226813c5a93eSJosef Bacik 	list_del_init(&cur_trans->list);
22690b246afaSJeff Mahoney 	spin_unlock(&fs_info->trans_lock);
2270a4abeea4SJosef Bacik 
2271724e2315SJosef Bacik 	btrfs_put_transaction(cur_trans);
2272724e2315SJosef Bacik 	btrfs_put_transaction(cur_trans);
227358176a96SJosef Bacik 
22740860adfdSMiao Xie 	if (trans->type & __TRANS_FREEZABLE)
22750b246afaSJeff Mahoney 		sb_end_intwrite(fs_info->sb);
2276b2b5ef5cSJan Kara 
22773a45bb20SJeff Mahoney 	trace_btrfs_transaction_commit(trans->root);
22781abe9b8aSliubo 
22792ff7e61eSJeff Mahoney 	btrfs_scrub_continue(fs_info);
2280a2de733cSArne Jansen 
22819ed74f2dSJosef Bacik 	if (current->journal_info == trans)
22829ed74f2dSJosef Bacik 		current->journal_info = NULL;
22839ed74f2dSJosef Bacik 
22842c90e5d6SChris Mason 	kmem_cache_free(btrfs_trans_handle_cachep, trans);
228524bbcf04SYan, Zheng 
22869e7cc91aSWang Xiaoguang 	/*
22879e7cc91aSWang Xiaoguang 	 * If fs has been frozen, we can not handle delayed iputs, otherwise
22889e7cc91aSWang Xiaoguang 	 * it'll result in deadlock about SB_FREEZE_FS.
22899e7cc91aSWang Xiaoguang 	 */
22900b246afaSJeff Mahoney 	if (current != fs_info->transaction_kthread &&
2291fac03c8dSDavid Sterba 	    current != fs_info->cleaner_kthread &&
2292fac03c8dSDavid Sterba 	    !test_bit(BTRFS_FS_FROZEN, &fs_info->flags))
22932ff7e61eSJeff Mahoney 		btrfs_run_delayed_iputs(fs_info);
229424bbcf04SYan, Zheng 
229579154b1bSChris Mason 	return ret;
229649b25e05SJeff Mahoney 
22976cf7f77eSWang Shilong scrub_continue:
22982ff7e61eSJeff Mahoney 	btrfs_scrub_continue(fs_info);
229949b25e05SJeff Mahoney cleanup_transaction:
2300dc60c525SNikolay Borisov 	btrfs_trans_release_metadata(trans);
23014fbcdf66SFilipe Manana 	btrfs_trans_release_chunk_metadata(trans);
23020e721106SJosef Bacik 	trans->block_rsv = NULL;
23030b246afaSJeff Mahoney 	btrfs_warn(fs_info, "Skipping commit of aborted transaction.");
230449b25e05SJeff Mahoney 	if (current->journal_info == trans)
230549b25e05SJeff Mahoney 		current->journal_info = NULL;
230697cb39bbSNikolay Borisov 	cleanup_transaction(trans, ret);
230749b25e05SJeff Mahoney 
230849b25e05SJeff Mahoney 	return ret;
230979154b1bSChris Mason }
231079154b1bSChris Mason 
2311d352ac68SChris Mason /*
23129d1a2a3aSDavid Sterba  * return < 0 if error
23139d1a2a3aSDavid Sterba  * 0 if there are no more dead_roots at the time of call
23149d1a2a3aSDavid Sterba  * 1 there are more to be processed, call me again
23159d1a2a3aSDavid Sterba  *
23169d1a2a3aSDavid Sterba  * The return value indicates there are certainly more snapshots to delete, but
23179d1a2a3aSDavid Sterba  * if there comes a new one during processing, it may return 0. We don't mind,
23189d1a2a3aSDavid Sterba  * because btrfs_commit_super will poke cleaner thread and it will process it a
23199d1a2a3aSDavid Sterba  * few seconds later.
2320d352ac68SChris Mason  */
23219d1a2a3aSDavid Sterba int btrfs_clean_one_deleted_snapshot(struct btrfs_root *root)
2322e9d0b13bSChris Mason {
23239d1a2a3aSDavid Sterba 	int ret;
23245d4f98a2SYan Zheng 	struct btrfs_fs_info *fs_info = root->fs_info;
2325e9d0b13bSChris Mason 
2326a4abeea4SJosef Bacik 	spin_lock(&fs_info->trans_lock);
23279d1a2a3aSDavid Sterba 	if (list_empty(&fs_info->dead_roots)) {
23289d1a2a3aSDavid Sterba 		spin_unlock(&fs_info->trans_lock);
23299d1a2a3aSDavid Sterba 		return 0;
23309d1a2a3aSDavid Sterba 	}
23319d1a2a3aSDavid Sterba 	root = list_first_entry(&fs_info->dead_roots,
23329d1a2a3aSDavid Sterba 			struct btrfs_root, root_list);
2333cfad392bSJosef Bacik 	list_del_init(&root->root_list);
2334a4abeea4SJosef Bacik 	spin_unlock(&fs_info->trans_lock);
23355d4f98a2SYan Zheng 
23364fd786e6SMisono Tomohiro 	btrfs_debug(fs_info, "cleaner removing %llu", root->root_key.objectid);
233776dda93cSYan, Zheng 
233816cdcec7SMiao Xie 	btrfs_kill_all_delayed_nodes(root);
233916cdcec7SMiao Xie 
234076dda93cSYan, Zheng 	if (btrfs_header_backref_rev(root->node) <
234176dda93cSYan, Zheng 			BTRFS_MIXED_BACKREF_REV)
23422c536799SJeff Mahoney 		ret = btrfs_drop_snapshot(root, NULL, 0, 0);
234376dda93cSYan, Zheng 	else
23442c536799SJeff Mahoney 		ret = btrfs_drop_snapshot(root, NULL, 1, 0);
234532471dc2SDavid Sterba 
23466596a928SJosef Bacik 	return (ret < 0) ? 0 : 1;
2347e9d0b13bSChris Mason }
2348572d9ab7SDavid Sterba 
2349572d9ab7SDavid Sterba void btrfs_apply_pending_changes(struct btrfs_fs_info *fs_info)
2350572d9ab7SDavid Sterba {
2351572d9ab7SDavid Sterba 	unsigned long prev;
2352572d9ab7SDavid Sterba 	unsigned long bit;
2353572d9ab7SDavid Sterba 
23546c9fe14fSQu Wenruo 	prev = xchg(&fs_info->pending_changes, 0);
2355572d9ab7SDavid Sterba 	if (!prev)
2356572d9ab7SDavid Sterba 		return;
2357572d9ab7SDavid Sterba 
23587e1876acSDavid Sterba 	bit = 1 << BTRFS_PENDING_SET_INODE_MAP_CACHE;
23597e1876acSDavid Sterba 	if (prev & bit)
23607e1876acSDavid Sterba 		btrfs_set_opt(fs_info->mount_opt, INODE_MAP_CACHE);
23617e1876acSDavid Sterba 	prev &= ~bit;
23627e1876acSDavid Sterba 
23637e1876acSDavid Sterba 	bit = 1 << BTRFS_PENDING_CLEAR_INODE_MAP_CACHE;
23647e1876acSDavid Sterba 	if (prev & bit)
23657e1876acSDavid Sterba 		btrfs_clear_opt(fs_info->mount_opt, INODE_MAP_CACHE);
23667e1876acSDavid Sterba 	prev &= ~bit;
23677e1876acSDavid Sterba 
2368d51033d0SDavid Sterba 	bit = 1 << BTRFS_PENDING_COMMIT;
2369d51033d0SDavid Sterba 	if (prev & bit)
2370d51033d0SDavid Sterba 		btrfs_debug(fs_info, "pending commit done");
2371d51033d0SDavid Sterba 	prev &= ~bit;
2372d51033d0SDavid Sterba 
2373572d9ab7SDavid Sterba 	if (prev)
2374572d9ab7SDavid Sterba 		btrfs_warn(fs_info,
2375572d9ab7SDavid Sterba 			"unknown pending changes left 0x%lx, ignoring", prev);
2376572d9ab7SDavid Sterba }
2377