Blame view

fs/btrfs/transaction.c 65.6 KB
6cbd55707   Chris Mason   Btrfs: add GPLv2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
  /*
   * Copyright (C) 2007 Oracle.  All rights reserved.
   *
   * This program is free software; you can redistribute it and/or
   * modify it under the terms of the GNU General Public
   * License v2 as published by the Free Software Foundation.
   *
   * This program is distributed in the hope that it will be useful,
   * but WITHOUT ANY WARRANTY; without even the implied warranty of
   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   * General Public License for more details.
   *
   * You should have received a copy of the GNU General Public
   * License along with this program; if not, write to the
   * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
   * Boston, MA 021110-1307, USA.
   */
79154b1b5   Chris Mason   Btrfs: transactio...
18
  #include <linux/fs.h>
5a0e3ad6a   Tejun Heo   include cleanup: ...
19
  #include <linux/slab.h>
340887809   Chris Mason   Btrfs: i386 fixes...
20
  #include <linux/sched.h>
d3c2fdcf7   Chris Mason   Btrfs: Use balanc...
21
  #include <linux/writeback.h>
5f39d397d   Chris Mason   Btrfs: Create ext...
22
  #include <linux/pagemap.h>
5f2cc086c   Chris Mason   Btrfs: Avoid unpl...
23
  #include <linux/blkdev.h>
8ea05e3a4   Alexander Block   Btrfs: introduce ...
24
  #include <linux/uuid.h>
79154b1b5   Chris Mason   Btrfs: transactio...
25
26
27
  #include "ctree.h"
  #include "disk-io.h"
  #include "transaction.h"
925baeddc   Chris Mason   Btrfs: Start btre...
28
  #include "locking.h"
e02119d5a   Chris Mason   Btrfs: Add a writ...
29
  #include "tree-log.h"
581bb0509   Li Zefan   Btrfs: Cache free...
30
  #include "inode-map.h"
733f4fbbc   Stefan Behrens   Btrfs: read devic...
31
  #include "volumes.h"
8dabb7420   Stefan Behrens   Btrfs: change cor...
32
  #include "dev-replace.h"
fcebe4562   Josef Bacik   Btrfs: rework qgr...
33
  #include "qgroup.h"
79154b1b5   Chris Mason   Btrfs: transactio...
34

0f7d52f44   Chris Mason   Btrfs: groundwork...
35
  #define BTRFS_ROOT_TRANS_TAG 0
e8c9f1860   David Sterba   btrfs: constify s...
36
  static const unsigned int btrfs_blocked_trans_types[TRANS_STATE_MAX] = {
4a9d8bdee   Miao Xie   Btrfs: make the s...
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
  	[TRANS_STATE_RUNNING]		= 0U,
  	[TRANS_STATE_BLOCKED]		= (__TRANS_USERSPACE |
  					   __TRANS_START),
  	[TRANS_STATE_COMMIT_START]	= (__TRANS_USERSPACE |
  					   __TRANS_START |
  					   __TRANS_ATTACH),
  	[TRANS_STATE_COMMIT_DOING]	= (__TRANS_USERSPACE |
  					   __TRANS_START |
  					   __TRANS_ATTACH |
  					   __TRANS_JOIN),
  	[TRANS_STATE_UNBLOCKED]		= (__TRANS_USERSPACE |
  					   __TRANS_START |
  					   __TRANS_ATTACH |
  					   __TRANS_JOIN |
  					   __TRANS_JOIN_NOLOCK),
  	[TRANS_STATE_COMPLETED]		= (__TRANS_USERSPACE |
  					   __TRANS_START |
  					   __TRANS_ATTACH |
  					   __TRANS_JOIN |
  					   __TRANS_JOIN_NOLOCK),
  };
724e2315d   Josef Bacik   Btrfs: fix two us...
58
  void btrfs_put_transaction(struct btrfs_transaction *transaction)
79154b1b5   Chris Mason   Btrfs: transactio...
59
  {
13c5a93e7   Josef Bacik   Btrfs: avoid taki...
60
61
  	WARN_ON(atomic_read(&transaction->use_count) == 0);
  	if (atomic_dec_and_test(&transaction->use_count)) {
a4abeea41   Josef Bacik   Btrfs: kill trans...
62
  		BUG_ON(!list_empty(&transaction->list));
c46effa60   Liu Bo   Btrfs: introduce ...
63
  		WARN_ON(!RB_EMPTY_ROOT(&transaction->delayed_refs.href_root));
1262133b8   Josef Bacik   Btrfs: account fo...
64
  		if (transaction->delayed_refs.pending_csums)
ab8d0fc48   Jeff Mahoney   btrfs: convert pr...
65
66
67
  			btrfs_err(transaction->fs_info,
  				  "pending csums is %llu",
  				  transaction->delayed_refs.pending_csums);
6df9a95e6   Josef Bacik   Btrfs: make the c...
68
69
70
71
72
73
74
75
  		while (!list_empty(&transaction->pending_chunks)) {
  			struct extent_map *em;
  
  			em = list_first_entry(&transaction->pending_chunks,
  					      struct extent_map, list);
  			list_del_init(&em->list);
  			free_extent_map(em);
  		}
7785a663c   Filipe Manana   Btrfs: fix memory...
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
  		/*
  		 * If any block groups are found in ->deleted_bgs then it's
  		 * because the transaction was aborted and a commit did not
  		 * happen (things failed before writing the new superblock
  		 * and calling btrfs_finish_extent_commit()), so we can not
  		 * discard the physical locations of the block groups.
  		 */
  		while (!list_empty(&transaction->deleted_bgs)) {
  			struct btrfs_block_group_cache *cache;
  
  			cache = list_first_entry(&transaction->deleted_bgs,
  						 struct btrfs_block_group_cache,
  						 bg_list);
  			list_del_init(&cache->bg_list);
  			btrfs_put_block_group_trimming(cache);
  			btrfs_put_block_group(cache);
  		}
2c90e5d65   Chris Mason   Btrfs: still corr...
93
  		kmem_cache_free(btrfs_transaction_cachep, transaction);
78fae27eb   Chris Mason   Btrfs: leak fixes...
94
  	}
79154b1b5   Chris Mason   Btrfs: transactio...
95
  }
663dfbb07   Filipe Manana   Btrfs: deal with ...
96
97
98
  static void clear_btree_io_tree(struct extent_io_tree *tree)
  {
  	spin_lock(&tree->lock);
b666a9cd9   David Sterba   btrfs: add barrie...
99
100
101
102
103
104
  	/*
  	 * Do a single barrier for the waitqueue_active check here, the state
  	 * of the waitqueue should not change once clear_btree_io_tree is
  	 * called.
  	 */
  	smp_mb();
663dfbb07   Filipe Manana   Btrfs: deal with ...
105
106
107
108
109
110
111
112
113
114
115
116
117
118
  	while (!RB_EMPTY_ROOT(&tree->state)) {
  		struct rb_node *node;
  		struct extent_state *state;
  
  		node = rb_first(&tree->state);
  		state = rb_entry(node, struct extent_state, rb_node);
  		rb_erase(&state->rb_node, &tree->state);
  		RB_CLEAR_NODE(&state->rb_node);
  		/*
  		 * btree io trees aren't supposed to have tasks waiting for
  		 * changes in the flags of extent states ever.
  		 */
  		ASSERT(!waitqueue_active(&state->wq));
  		free_extent_state(state);
351810c1d   David Sterba   btrfs: use cond_r...
119
120
  
  		cond_resched_lock(&tree->lock);
663dfbb07   Filipe Manana   Btrfs: deal with ...
121
122
123
  	}
  	spin_unlock(&tree->lock);
  }
9e351cc86   Josef Bacik   Btrfs: remove tra...
124
125
  static noinline void switch_commit_roots(struct btrfs_transaction *trans,
  					 struct btrfs_fs_info *fs_info)
817d52f8d   Josef Bacik   Btrfs: async bloc...
126
  {
9e351cc86   Josef Bacik   Btrfs: remove tra...
127
128
129
130
131
132
133
134
135
136
  	struct btrfs_root *root, *tmp;
  
  	down_write(&fs_info->commit_root_sem);
  	list_for_each_entry_safe(root, tmp, &trans->switch_commits,
  				 dirty_list) {
  		list_del_init(&root->dirty_list);
  		free_extent_buffer(root->commit_root);
  		root->commit_root = btrfs_root_node(root);
  		if (is_fstree(root->objectid))
  			btrfs_unpin_free_ino(root);
663dfbb07   Filipe Manana   Btrfs: deal with ...
137
  		clear_btree_io_tree(&root->dirty_log_pages);
9e351cc86   Josef Bacik   Btrfs: remove tra...
138
  	}
2b9dbef27   Josef Bacik   Btrfs: keep dropp...
139
140
141
142
143
144
145
146
147
148
149
150
  
  	/* We can free old roots now. */
  	spin_lock(&trans->dropped_roots_lock);
  	while (!list_empty(&trans->dropped_roots)) {
  		root = list_first_entry(&trans->dropped_roots,
  					struct btrfs_root, root_list);
  		list_del_init(&root->root_list);
  		spin_unlock(&trans->dropped_roots_lock);
  		btrfs_drop_and_free_fs_root(fs_info, root);
  		spin_lock(&trans->dropped_roots_lock);
  	}
  	spin_unlock(&trans->dropped_roots_lock);
9e351cc86   Josef Bacik   Btrfs: remove tra...
151
  	up_write(&fs_info->commit_root_sem);
817d52f8d   Josef Bacik   Btrfs: async bloc...
152
  }
0860adfdb   Miao Xie   Btrfs: don't wait...
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
  static inline void extwriter_counter_inc(struct btrfs_transaction *trans,
  					 unsigned int type)
  {
  	if (type & TRANS_EXTWRITERS)
  		atomic_inc(&trans->num_extwriters);
  }
  
  static inline void extwriter_counter_dec(struct btrfs_transaction *trans,
  					 unsigned int type)
  {
  	if (type & TRANS_EXTWRITERS)
  		atomic_dec(&trans->num_extwriters);
  }
  
  static inline void extwriter_counter_init(struct btrfs_transaction *trans,
  					  unsigned int type)
  {
  	atomic_set(&trans->num_extwriters, ((type & TRANS_EXTWRITERS) ? 1 : 0));
  }
  
  static inline int extwriter_counter_read(struct btrfs_transaction *trans)
  {
  	return atomic_read(&trans->num_extwriters);
178260b2c   Miao Xie   Btrfs: fix the de...
176
  }
d352ac681   Chris Mason   Btrfs: add and im...
177
178
179
  /*
   * either allocate a new transaction or hop into the existing one
   */
0860adfdb   Miao Xie   Btrfs: don't wait...
180
  static noinline int join_transaction(struct btrfs_root *root, unsigned int type)
79154b1b5   Chris Mason   Btrfs: transactio...
181
182
  {
  	struct btrfs_transaction *cur_trans;
19ae4e813   Jan Schmidt   Btrfs: fs_info va...
183
  	struct btrfs_fs_info *fs_info = root->fs_info;
a4abeea41   Josef Bacik   Btrfs: kill trans...
184

19ae4e813   Jan Schmidt   Btrfs: fs_info va...
185
  	spin_lock(&fs_info->trans_lock);
d43317dcd   Chris Mason   Btrfs: fix race d...
186
  loop:
49b25e054   Jeff Mahoney   btrfs: enhance tr...
187
  	/* The file system has been taken offline. No new transactions. */
87533c475   Miao Xie   Btrfs: use bit op...
188
  	if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) {
19ae4e813   Jan Schmidt   Btrfs: fs_info va...
189
  		spin_unlock(&fs_info->trans_lock);
49b25e054   Jeff Mahoney   btrfs: enhance tr...
190
191
  		return -EROFS;
  	}
19ae4e813   Jan Schmidt   Btrfs: fs_info va...
192
  	cur_trans = fs_info->running_transaction;
a4abeea41   Josef Bacik   Btrfs: kill trans...
193
  	if (cur_trans) {
871383be5   David Sterba   btrfs: add missin...
194
  		if (cur_trans->aborted) {
19ae4e813   Jan Schmidt   Btrfs: fs_info va...
195
  			spin_unlock(&fs_info->trans_lock);
49b25e054   Jeff Mahoney   btrfs: enhance tr...
196
  			return cur_trans->aborted;
871383be5   David Sterba   btrfs: add missin...
197
  		}
4a9d8bdee   Miao Xie   Btrfs: make the s...
198
  		if (btrfs_blocked_trans_types[cur_trans->state] & type) {
178260b2c   Miao Xie   Btrfs: fix the de...
199
200
201
  			spin_unlock(&fs_info->trans_lock);
  			return -EBUSY;
  		}
a4abeea41   Josef Bacik   Btrfs: kill trans...
202
  		atomic_inc(&cur_trans->use_count);
13c5a93e7   Josef Bacik   Btrfs: avoid taki...
203
  		atomic_inc(&cur_trans->num_writers);
0860adfdb   Miao Xie   Btrfs: don't wait...
204
  		extwriter_counter_inc(cur_trans, type);
19ae4e813   Jan Schmidt   Btrfs: fs_info va...
205
  		spin_unlock(&fs_info->trans_lock);
a4abeea41   Josef Bacik   Btrfs: kill trans...
206
  		return 0;
79154b1b5   Chris Mason   Btrfs: transactio...
207
  	}
19ae4e813   Jan Schmidt   Btrfs: fs_info va...
208
  	spin_unlock(&fs_info->trans_lock);
a4abeea41   Josef Bacik   Btrfs: kill trans...
209

354aa0fb6   Miao Xie   Btrfs: fix orphan...
210
211
212
213
214
215
  	/*
  	 * If we are ATTACH, we just want to catch the current transaction,
  	 * and commit it. If there is no transaction, just return ENOENT.
  	 */
  	if (type == TRANS_ATTACH)
  		return -ENOENT;
4a9d8bdee   Miao Xie   Btrfs: make the s...
216
217
218
219
220
  	/*
  	 * JOIN_NOLOCK only happens during the transaction commit, so
  	 * it is impossible that ->running_transaction is NULL
  	 */
  	BUG_ON(type == TRANS_JOIN_NOLOCK);
a4abeea41   Josef Bacik   Btrfs: kill trans...
221
222
223
  	cur_trans = kmem_cache_alloc(btrfs_transaction_cachep, GFP_NOFS);
  	if (!cur_trans)
  		return -ENOMEM;
d43317dcd   Chris Mason   Btrfs: fix race d...
224

19ae4e813   Jan Schmidt   Btrfs: fs_info va...
225
226
  	spin_lock(&fs_info->trans_lock);
  	if (fs_info->running_transaction) {
d43317dcd   Chris Mason   Btrfs: fix race d...
227
228
  		/*
  		 * someone started a transaction after we unlocked.  Make sure
4a9d8bdee   Miao Xie   Btrfs: make the s...
229
  		 * to redo the checks above
d43317dcd   Chris Mason   Btrfs: fix race d...
230
  		 */
a4abeea41   Josef Bacik   Btrfs: kill trans...
231
  		kmem_cache_free(btrfs_transaction_cachep, cur_trans);
d43317dcd   Chris Mason   Btrfs: fix race d...
232
  		goto loop;
87533c475   Miao Xie   Btrfs: use bit op...
233
  	} else if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) {
e4b50e14c   Dan Carpenter   Btrfs: small nami...
234
  		spin_unlock(&fs_info->trans_lock);
7b8b92af5   Josef Bacik   Btrfs: abort the ...
235
236
  		kmem_cache_free(btrfs_transaction_cachep, cur_trans);
  		return -EROFS;
79154b1b5   Chris Mason   Btrfs: transactio...
237
  	}
d43317dcd   Chris Mason   Btrfs: fix race d...
238

ab8d0fc48   Jeff Mahoney   btrfs: convert pr...
239
  	cur_trans->fs_info = fs_info;
a4abeea41   Josef Bacik   Btrfs: kill trans...
240
  	atomic_set(&cur_trans->num_writers, 1);
0860adfdb   Miao Xie   Btrfs: don't wait...
241
  	extwriter_counter_init(cur_trans, type);
a4abeea41   Josef Bacik   Btrfs: kill trans...
242
243
  	init_waitqueue_head(&cur_trans->writer_wait);
  	init_waitqueue_head(&cur_trans->commit_wait);
161c3549b   Josef Bacik   Btrfs: change how...
244
  	init_waitqueue_head(&cur_trans->pending_wait);
4a9d8bdee   Miao Xie   Btrfs: make the s...
245
  	cur_trans->state = TRANS_STATE_RUNNING;
a4abeea41   Josef Bacik   Btrfs: kill trans...
246
247
248
249
250
  	/*
  	 * One for this trans handle, one so it will live on until we
  	 * commit the transaction.
  	 */
  	atomic_set(&cur_trans->use_count, 2);
161c3549b   Josef Bacik   Btrfs: change how...
251
  	atomic_set(&cur_trans->pending_ordered, 0);
3204d33cd   Josef Bacik   Btrfs: add a flag...
252
  	cur_trans->flags = 0;
a4abeea41   Josef Bacik   Btrfs: kill trans...
253
  	cur_trans->start_time = get_seconds();
a099d0fdb   Alexandru Moise   btrfs: memset cur...
254
  	memset(&cur_trans->delayed_refs, 0, sizeof(cur_trans->delayed_refs));
c46effa60   Liu Bo   Btrfs: introduce ...
255
  	cur_trans->delayed_refs.href_root = RB_ROOT;
3368d001b   Qu Wenruo   btrfs: qgroup: Re...
256
  	cur_trans->delayed_refs.dirty_extent_root = RB_ROOT;
d7df2c796   Josef Bacik   Btrfs: attach del...
257
  	atomic_set(&cur_trans->delayed_refs.num_entries, 0);
20b297d62   Jan Schmidt   Btrfs: tree mod l...
258
259
260
261
262
263
  
  	/*
  	 * although the tree mod log is per file system and not per transaction,
  	 * the log must never go across transaction boundaries.
  	 */
  	smp_mb();
31b1a2bd7   Julia Lawall   fs/btrfs: use WARN
264
  	if (!list_empty(&fs_info->tree_mod_seq_list))
5d163e0e6   Jeff Mahoney   btrfs: unsplit pr...
265
266
  		WARN(1, KERN_ERR "BTRFS: tree_mod_seq_list not empty when creating a fresh transaction
  ");
31b1a2bd7   Julia Lawall   fs/btrfs: use WARN
267
  	if (!RB_EMPTY_ROOT(&fs_info->tree_mod_log))
5d163e0e6   Jeff Mahoney   btrfs: unsplit pr...
268
269
  		WARN(1, KERN_ERR "BTRFS: tree_mod_log rb tree not empty when creating a fresh transaction
  ");
fc36ed7e0   Jan Schmidt   Btrfs: separate s...
270
  	atomic64_set(&fs_info->tree_mod_seq, 0);
20b297d62   Jan Schmidt   Btrfs: tree mod l...
271

a4abeea41   Josef Bacik   Btrfs: kill trans...
272
273
274
  	spin_lock_init(&cur_trans->delayed_refs.lock);
  
  	INIT_LIST_HEAD(&cur_trans->pending_snapshots);
6df9a95e6   Josef Bacik   Btrfs: make the c...
275
  	INIT_LIST_HEAD(&cur_trans->pending_chunks);
9e351cc86   Josef Bacik   Btrfs: remove tra...
276
  	INIT_LIST_HEAD(&cur_trans->switch_commits);
ce93ec548   Josef Bacik   Btrfs: track dirt...
277
  	INIT_LIST_HEAD(&cur_trans->dirty_bgs);
1bbc621ef   Chris Mason   Btrfs: allow bloc...
278
  	INIT_LIST_HEAD(&cur_trans->io_bgs);
2b9dbef27   Josef Bacik   Btrfs: keep dropp...
279
  	INIT_LIST_HEAD(&cur_trans->dropped_roots);
1bbc621ef   Chris Mason   Btrfs: allow bloc...
280
  	mutex_init(&cur_trans->cache_write_mutex);
cb723e491   Josef Bacik   Btrfs: reserve sp...
281
  	cur_trans->num_dirty_bgs = 0;
ce93ec548   Josef Bacik   Btrfs: track dirt...
282
  	spin_lock_init(&cur_trans->dirty_bgs_lock);
e33e17ee1   Jeff Mahoney   btrfs: add missin...
283
  	INIT_LIST_HEAD(&cur_trans->deleted_bgs);
2b9dbef27   Josef Bacik   Btrfs: keep dropp...
284
  	spin_lock_init(&cur_trans->dropped_roots_lock);
19ae4e813   Jan Schmidt   Btrfs: fs_info va...
285
  	list_add_tail(&cur_trans->list, &fs_info->trans_list);
a4abeea41   Josef Bacik   Btrfs: kill trans...
286
  	extent_io_tree_init(&cur_trans->dirty_pages,
19ae4e813   Jan Schmidt   Btrfs: fs_info va...
287
288
289
290
  			     fs_info->btree_inode->i_mapping);
  	fs_info->generation++;
  	cur_trans->transid = fs_info->generation;
  	fs_info->running_transaction = cur_trans;
49b25e054   Jeff Mahoney   btrfs: enhance tr...
291
  	cur_trans->aborted = 0;
19ae4e813   Jan Schmidt   Btrfs: fs_info va...
292
  	spin_unlock(&fs_info->trans_lock);
15ee9bc7e   Josef Bacik   Btrfs: delay comm...
293

79154b1b5   Chris Mason   Btrfs: transactio...
294
295
  	return 0;
  }
d352ac681   Chris Mason   Btrfs: add and im...
296
  /*
d397712bc   Chris Mason   Btrfs: Fix checkp...
297
298
299
300
   * this does all the record keeping required to make sure that a reference
   * counted root is properly recorded in a given transaction.  This is required
   * to make sure the old root from before we joined the transaction is deleted
   * when the transaction commits
d352ac681   Chris Mason   Btrfs: add and im...
301
   */
7585717f3   Chris Mason   Btrfs: fix reloca...
302
  static int record_root_in_trans(struct btrfs_trans_handle *trans,
6426c7ad6   Qu Wenruo   btrfs: qgroup: Fi...
303
304
  			       struct btrfs_root *root,
  			       int force)
6702ed490   Chris Mason   Btrfs: Add run ti...
305
  {
6426c7ad6   Qu Wenruo   btrfs: qgroup: Fi...
306
307
  	if ((test_bit(BTRFS_ROOT_REF_COWS, &root->state) &&
  	    root->last_trans < trans->transid) || force) {
6702ed490   Chris Mason   Btrfs: Add run ti...
308
  		WARN_ON(root == root->fs_info->extent_root);
5d4f98a28   Yan Zheng   Btrfs: Mixed back...
309
  		WARN_ON(root->commit_root != root->node);
7585717f3   Chris Mason   Btrfs: fix reloca...
310
  		/*
27cdeb709   Miao Xie   Btrfs: use bitfie...
311
  		 * see below for IN_TRANS_SETUP usage rules
7585717f3   Chris Mason   Btrfs: fix reloca...
312
313
314
  		 * we have the reloc mutex held now, so there
  		 * is only one writer in this function
  		 */
27cdeb709   Miao Xie   Btrfs: use bitfie...
315
  		set_bit(BTRFS_ROOT_IN_TRANS_SETUP, &root->state);
7585717f3   Chris Mason   Btrfs: fix reloca...
316

27cdeb709   Miao Xie   Btrfs: use bitfie...
317
  		/* make sure readers find IN_TRANS_SETUP before
7585717f3   Chris Mason   Btrfs: fix reloca...
318
319
320
  		 * they find our root->last_trans update
  		 */
  		smp_wmb();
a4abeea41   Josef Bacik   Btrfs: kill trans...
321
  		spin_lock(&root->fs_info->fs_roots_radix_lock);
6426c7ad6   Qu Wenruo   btrfs: qgroup: Fi...
322
  		if (root->last_trans == trans->transid && !force) {
a4abeea41   Josef Bacik   Btrfs: kill trans...
323
324
325
  			spin_unlock(&root->fs_info->fs_roots_radix_lock);
  			return 0;
  		}
5d4f98a28   Yan Zheng   Btrfs: Mixed back...
326
327
328
  		radix_tree_tag_set(&root->fs_info->fs_roots_radix,
  			   (unsigned long)root->root_key.objectid,
  			   BTRFS_ROOT_TRANS_TAG);
a4abeea41   Josef Bacik   Btrfs: kill trans...
329
  		spin_unlock(&root->fs_info->fs_roots_radix_lock);
7585717f3   Chris Mason   Btrfs: fix reloca...
330
331
332
333
334
335
336
337
338
339
340
341
342
  		root->last_trans = trans->transid;
  
  		/* this is pretty tricky.  We don't want to
  		 * take the relocation lock in btrfs_record_root_in_trans
  		 * unless we're really doing the first setup for this root in
  		 * this transaction.
  		 *
  		 * Normally we'd use root->last_trans as a flag to decide
  		 * if we want to take the expensive mutex.
  		 *
  		 * But, we have to set root->last_trans before we
  		 * init the relocation root, otherwise, we trip over warnings
  		 * in ctree.c.  The solution used here is to flag ourselves
27cdeb709   Miao Xie   Btrfs: use bitfie...
343
  		 * with root IN_TRANS_SETUP.  When this is 1, we're still
7585717f3   Chris Mason   Btrfs: fix reloca...
344
345
346
347
348
349
350
  		 * fixing up the reloc trees and everyone must wait.
  		 *
  		 * When this is zero, they can trust root->last_trans and fly
  		 * through btrfs_record_root_in_trans without having to take the
  		 * lock.  smp_wmb() makes sure that all the writes above are
  		 * done before we pop in the zero below
  		 */
5d4f98a28   Yan Zheng   Btrfs: Mixed back...
351
  		btrfs_init_reloc_root(trans, root);
c7548af69   Chris Mason   Btrfs: convert sm...
352
  		smp_mb__before_atomic();
27cdeb709   Miao Xie   Btrfs: use bitfie...
353
  		clear_bit(BTRFS_ROOT_IN_TRANS_SETUP, &root->state);
5d4f98a28   Yan Zheng   Btrfs: Mixed back...
354
355
356
  	}
  	return 0;
  }
bcc63abbf   Yan   Btrfs: implement ...
357

7585717f3   Chris Mason   Btrfs: fix reloca...
358

2b9dbef27   Josef Bacik   Btrfs: keep dropp...
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
  void btrfs_add_dropped_root(struct btrfs_trans_handle *trans,
  			    struct btrfs_root *root)
  {
  	struct btrfs_transaction *cur_trans = trans->transaction;
  
  	/* Add ourselves to the transaction dropped list */
  	spin_lock(&cur_trans->dropped_roots_lock);
  	list_add_tail(&root->root_list, &cur_trans->dropped_roots);
  	spin_unlock(&cur_trans->dropped_roots_lock);
  
  	/* Make sure we don't try to update the root at commit time */
  	spin_lock(&root->fs_info->fs_roots_radix_lock);
  	radix_tree_tag_clear(&root->fs_info->fs_roots_radix,
  			     (unsigned long)root->root_key.objectid,
  			     BTRFS_ROOT_TRANS_TAG);
  	spin_unlock(&root->fs_info->fs_roots_radix_lock);
  }
7585717f3   Chris Mason   Btrfs: fix reloca...
376
377
378
  int btrfs_record_root_in_trans(struct btrfs_trans_handle *trans,
  			       struct btrfs_root *root)
  {
27cdeb709   Miao Xie   Btrfs: use bitfie...
379
  	if (!test_bit(BTRFS_ROOT_REF_COWS, &root->state))
7585717f3   Chris Mason   Btrfs: fix reloca...
380
381
382
  		return 0;
  
  	/*
27cdeb709   Miao Xie   Btrfs: use bitfie...
383
  	 * see record_root_in_trans for comments about IN_TRANS_SETUP usage
7585717f3   Chris Mason   Btrfs: fix reloca...
384
385
386
387
  	 * and barriers
  	 */
  	smp_rmb();
  	if (root->last_trans == trans->transid &&
27cdeb709   Miao Xie   Btrfs: use bitfie...
388
  	    !test_bit(BTRFS_ROOT_IN_TRANS_SETUP, &root->state))
7585717f3   Chris Mason   Btrfs: fix reloca...
389
390
391
  		return 0;
  
  	mutex_lock(&root->fs_info->reloc_mutex);
6426c7ad6   Qu Wenruo   btrfs: qgroup: Fi...
392
  	record_root_in_trans(trans, root, 0);
7585717f3   Chris Mason   Btrfs: fix reloca...
393
394
395
396
  	mutex_unlock(&root->fs_info->reloc_mutex);
  
  	return 0;
  }
4a9d8bdee   Miao Xie   Btrfs: make the s...
397
398
399
  static inline int is_transaction_blocked(struct btrfs_transaction *trans)
  {
  	return (trans->state >= TRANS_STATE_BLOCKED &&
501407aab   Josef Bacik   Btrfs: stop waiti...
400
401
  		trans->state < TRANS_STATE_UNBLOCKED &&
  		!trans->aborted);
4a9d8bdee   Miao Xie   Btrfs: make the s...
402
  }
d352ac681   Chris Mason   Btrfs: add and im...
403
404
405
406
  /* wait for commit against the current transaction to become unblocked
   * when this is done, it is safe to start a new transaction, but the current
   * transaction might not be fully on disk.
   */
37d1aeee3   Chris Mason   Btrfs: Throttle t...
407
  static void wait_current_trans(struct btrfs_root *root)
79154b1b5   Chris Mason   Btrfs: transactio...
408
  {
f92957493   Chris Mason   btrfs_start_trans...
409
  	struct btrfs_transaction *cur_trans;
79154b1b5   Chris Mason   Btrfs: transactio...
410

a4abeea41   Josef Bacik   Btrfs: kill trans...
411
  	spin_lock(&root->fs_info->trans_lock);
f92957493   Chris Mason   btrfs_start_trans...
412
  	cur_trans = root->fs_info->running_transaction;
4a9d8bdee   Miao Xie   Btrfs: make the s...
413
  	if (cur_trans && is_transaction_blocked(cur_trans)) {
13c5a93e7   Josef Bacik   Btrfs: avoid taki...
414
  		atomic_inc(&cur_trans->use_count);
a4abeea41   Josef Bacik   Btrfs: kill trans...
415
  		spin_unlock(&root->fs_info->trans_lock);
72d63ed64   Li Zefan   Btrfs: use wait_e...
416
417
  
  		wait_event(root->fs_info->transaction_wait,
501407aab   Josef Bacik   Btrfs: stop waiti...
418
419
  			   cur_trans->state >= TRANS_STATE_UNBLOCKED ||
  			   cur_trans->aborted);
724e2315d   Josef Bacik   Btrfs: fix two us...
420
  		btrfs_put_transaction(cur_trans);
a4abeea41   Josef Bacik   Btrfs: kill trans...
421
422
  	} else {
  		spin_unlock(&root->fs_info->trans_lock);
f92957493   Chris Mason   btrfs_start_trans...
423
  	}
37d1aeee3   Chris Mason   Btrfs: Throttle t...
424
  }
a22285a6a   Yan, Zheng   Btrfs: Integrate ...
425
426
  static int may_wait_transaction(struct btrfs_root *root, int type)
  {
afcdd129e   Josef Bacik   Btrfs: add a flag...
427
  	if (test_bit(BTRFS_FS_LOG_RECOVERING, &root->fs_info->flags))
a4abeea41   Josef Bacik   Btrfs: kill trans...
428
429
430
431
432
433
434
  		return 0;
  
  	if (type == TRANS_USERSPACE)
  		return 1;
  
  	if (type == TRANS_START &&
  	    !atomic_read(&root->fs_info->open_ioctl_trans))
a22285a6a   Yan, Zheng   Btrfs: Integrate ...
435
  		return 1;
a4abeea41   Josef Bacik   Btrfs: kill trans...
436

a22285a6a   Yan, Zheng   Btrfs: Integrate ...
437
438
  	return 0;
  }
20dd2cbf0   Miao Xie   Btrfs: fix BUG_ON...
439
440
441
  static inline bool need_reserve_reloc_root(struct btrfs_root *root)
  {
  	if (!root->fs_info->reloc_ctl ||
27cdeb709   Miao Xie   Btrfs: use bitfie...
442
  	    !test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
20dd2cbf0   Miao Xie   Btrfs: fix BUG_ON...
443
444
445
446
447
448
  	    root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID ||
  	    root->reloc_root)
  		return false;
  
  	return true;
  }
08e007d2e   Miao Xie   Btrfs: improve th...
449
  static struct btrfs_trans_handle *
5aed1dd8b   Alexandru Moise   btrfs: change num...
450
451
  start_transaction(struct btrfs_root *root, unsigned int num_items,
  		  unsigned int type, enum btrfs_reserve_flush_enum flush)
37d1aeee3   Chris Mason   Btrfs: Throttle t...
452
  {
a22285a6a   Yan, Zheng   Btrfs: Integrate ...
453
454
  	struct btrfs_trans_handle *h;
  	struct btrfs_transaction *cur_trans;
b5009945b   Josef Bacik   Btrfs: do transac...
455
  	u64 num_bytes = 0;
c55672379   Arne Jansen   Btrfs: hooks to r...
456
  	u64 qgroup_reserved = 0;
20dd2cbf0   Miao Xie   Btrfs: fix BUG_ON...
457
458
  	bool reloc_reserved = false;
  	int ret;
acce952b0   liubo   Btrfs: forced rea...
459

46c4e71e9   Filipe Manana   Btrfs: assert sen...
460
  	/* Send isn't supposed to start transactions. */
2755a0de6   David Sterba   btrfs: hide typec...
461
  	ASSERT(current->journal_info != BTRFS_SEND_TRANS_STUB);
46c4e71e9   Filipe Manana   Btrfs: assert sen...
462

87533c475   Miao Xie   Btrfs: use bit op...
463
  	if (test_bit(BTRFS_FS_STATE_ERROR, &root->fs_info->fs_state))
acce952b0   liubo   Btrfs: forced rea...
464
  		return ERR_PTR(-EROFS);
2a1eb4614   Josef Bacik   Btrfs: if we've a...
465

46c4e71e9   Filipe Manana   Btrfs: assert sen...
466
  	if (current->journal_info) {
0860adfdb   Miao Xie   Btrfs: don't wait...
467
  		WARN_ON(type & TRANS_EXTWRITERS);
2a1eb4614   Josef Bacik   Btrfs: if we've a...
468
469
  		h = current->journal_info;
  		h->use_count++;
b7d5b0a81   Miao Xie   Btrfs: fix joinin...
470
  		WARN_ON(h->use_count > 2);
2a1eb4614   Josef Bacik   Btrfs: if we've a...
471
472
473
474
  		h->orig_rsv = h->block_rsv;
  		h->block_rsv = NULL;
  		goto got_it;
  	}
b5009945b   Josef Bacik   Btrfs: do transac...
475
476
477
478
479
480
  
  	/*
  	 * Do the reservation before we join the transaction so we can do all
  	 * the appropriate flushing if need be.
  	 */
  	if (num_items > 0 && root != root->fs_info->chunk_root) {
7174109c6   Qu Wenruo   btrfs: qgroup: Us...
481
482
483
484
  		qgroup_reserved = num_items * root->nodesize;
  		ret = btrfs_qgroup_reserve_meta(root, qgroup_reserved);
  		if (ret)
  			return ERR_PTR(ret);
c55672379   Arne Jansen   Btrfs: hooks to r...
485

b5009945b   Josef Bacik   Btrfs: do transac...
486
  		num_bytes = btrfs_calc_trans_metadata_size(root, num_items);
20dd2cbf0   Miao Xie   Btrfs: fix BUG_ON...
487
488
489
  		/*
  		 * Do the reservation for the relocation root creation
  		 */
ee39b432b   David Sterba   btrfs: remove unl...
490
  		if (need_reserve_reloc_root(root)) {
20dd2cbf0   Miao Xie   Btrfs: fix BUG_ON...
491
492
493
  			num_bytes += root->nodesize;
  			reloc_reserved = true;
  		}
08e007d2e   Miao Xie   Btrfs: improve th...
494
495
496
  		ret = btrfs_block_rsv_add(root,
  					  &root->fs_info->trans_block_rsv,
  					  num_bytes, flush);
b5009945b   Josef Bacik   Btrfs: do transac...
497
  		if (ret)
843fcf357   Miao Xie   Btrfs: fix missin...
498
  			goto reserve_fail;
b5009945b   Josef Bacik   Btrfs: do transac...
499
  	}
a22285a6a   Yan, Zheng   Btrfs: Integrate ...
500
  again:
f2f767e73   Alexandru Moise   btrfs: trimming s...
501
  	h = kmem_cache_zalloc(btrfs_trans_handle_cachep, GFP_NOFS);
843fcf357   Miao Xie   Btrfs: fix missin...
502
503
504
505
  	if (!h) {
  		ret = -ENOMEM;
  		goto alloc_fail;
  	}
37d1aeee3   Chris Mason   Btrfs: Throttle t...
506

98114659e   Josef Bacik   Btrfs: fix race w...
507
508
509
510
511
512
  	/*
  	 * If we are JOIN_NOLOCK we're already committing a transaction and
  	 * waiting on this guy, so we don't need to do the sb_start_intwrite
  	 * because we're already holding a ref.  We need this because we could
  	 * have raced in and did an fsync() on a file which can kick a commit
  	 * and then we deadlock with somebody doing a freeze.
354aa0fb6   Miao Xie   Btrfs: fix orphan...
513
514
515
  	 *
  	 * If we are ATTACH, it means we just want to catch the current
  	 * transaction and commit it, so we needn't do sb_start_intwrite(). 
98114659e   Josef Bacik   Btrfs: fix race w...
516
  	 */
0860adfdb   Miao Xie   Btrfs: don't wait...
517
  	if (type & __TRANS_FREEZABLE)
60376ce4a   Josef Bacik   Btrfs: fix race i...
518
  		sb_start_intwrite(root->fs_info->sb);
b2b5ef5c8   Jan Kara   btrfs: Convert to...
519

a22285a6a   Yan, Zheng   Btrfs: Integrate ...
520
  	if (may_wait_transaction(root, type))
37d1aeee3   Chris Mason   Btrfs: Throttle t...
521
  		wait_current_trans(root);
a22285a6a   Yan, Zheng   Btrfs: Integrate ...
522

a4abeea41   Josef Bacik   Btrfs: kill trans...
523
  	do {
354aa0fb6   Miao Xie   Btrfs: fix orphan...
524
  		ret = join_transaction(root, type);
178260b2c   Miao Xie   Btrfs: fix the de...
525
  		if (ret == -EBUSY) {
a4abeea41   Josef Bacik   Btrfs: kill trans...
526
  			wait_current_trans(root);
178260b2c   Miao Xie   Btrfs: fix the de...
527
528
529
  			if (unlikely(type == TRANS_ATTACH))
  				ret = -ENOENT;
  		}
a4abeea41   Josef Bacik   Btrfs: kill trans...
530
  	} while (ret == -EBUSY);
a43f7f820   Liu Bo   Btrfs: remove BUG...
531
  	if (ret < 0)
843fcf357   Miao Xie   Btrfs: fix missin...
532
  		goto join_fail;
0f7d52f44   Chris Mason   Btrfs: groundwork...
533

a22285a6a   Yan, Zheng   Btrfs: Integrate ...
534
  	cur_trans = root->fs_info->running_transaction;
a22285a6a   Yan, Zheng   Btrfs: Integrate ...
535
536
537
  
  	h->transid = cur_trans->transid;
  	h->transaction = cur_trans;
d13603ef6   Arne Jansen   Btrfs: check the ...
538
  	h->root = root;
2a1eb4614   Josef Bacik   Btrfs: if we've a...
539
  	h->use_count = 1;
64b635807   Jeff Mahoney   btrfs: add btrfs_...
540
  	h->fs_info = root->fs_info;
7174109c6   Qu Wenruo   btrfs: qgroup: Us...
541

a698d0755   Miao Xie   Btrfs: add a type...
542
  	h->type = type;
d9a0540a7   Filipe Manana   Btrfs: fix deadlo...
543
  	h->can_flush_pending_bgs = true;
bed92eae2   Arne Jansen   Btrfs: qgroup imp...
544
  	INIT_LIST_HEAD(&h->qgroup_ref_list);
ea658badc   Josef Bacik   Btrfs: delay bloc...
545
  	INIT_LIST_HEAD(&h->new_bgs);
b7ec40d78   Chris Mason   Btrfs: reduce sta...
546

a22285a6a   Yan, Zheng   Btrfs: Integrate ...
547
  	smp_mb();
4a9d8bdee   Miao Xie   Btrfs: make the s...
548
549
  	if (cur_trans->state >= TRANS_STATE_BLOCKED &&
  	    may_wait_transaction(root, type)) {
abdd2e80a   Filipe Manana   Btrfs: fix crash ...
550
  		current->journal_info = h;
a22285a6a   Yan, Zheng   Btrfs: Integrate ...
551
552
553
  		btrfs_commit_transaction(h, root);
  		goto again;
  	}
b5009945b   Josef Bacik   Btrfs: do transac...
554
  	if (num_bytes) {
8c2a3ca20   Josef Bacik   Btrfs: space leak...
555
  		trace_btrfs_space_reservation(root->fs_info, "transaction",
2bcc0328c   Liu Bo   Btrfs: show usefu...
556
  					      h->transid, num_bytes, 1);
b5009945b   Josef Bacik   Btrfs: do transac...
557
558
  		h->block_rsv = &root->fs_info->trans_block_rsv;
  		h->bytes_reserved = num_bytes;
20dd2cbf0   Miao Xie   Btrfs: fix BUG_ON...
559
  		h->reloc_reserved = reloc_reserved;
a22285a6a   Yan, Zheng   Btrfs: Integrate ...
560
  	}
9ed74f2db   Josef Bacik   Btrfs: proper -EN...
561

2a1eb4614   Josef Bacik   Btrfs: if we've a...
562
  got_it:
a4abeea41   Josef Bacik   Btrfs: kill trans...
563
  	btrfs_record_root_in_trans(h, root);
a22285a6a   Yan, Zheng   Btrfs: Integrate ...
564
565
566
  
  	if (!current->journal_info && type != TRANS_USERSPACE)
  		current->journal_info = h;
79154b1b5   Chris Mason   Btrfs: transactio...
567
  	return h;
843fcf357   Miao Xie   Btrfs: fix missin...
568
569
  
  join_fail:
0860adfdb   Miao Xie   Btrfs: don't wait...
570
  	if (type & __TRANS_FREEZABLE)
843fcf357   Miao Xie   Btrfs: fix missin...
571
572
573
574
575
576
577
  		sb_end_intwrite(root->fs_info->sb);
  	kmem_cache_free(btrfs_trans_handle_cachep, h);
  alloc_fail:
  	if (num_bytes)
  		btrfs_block_rsv_release(root, &root->fs_info->trans_block_rsv,
  					num_bytes);
  reserve_fail:
7174109c6   Qu Wenruo   btrfs: qgroup: Us...
578
  	btrfs_qgroup_free_meta(root, qgroup_reserved);
843fcf357   Miao Xie   Btrfs: fix missin...
579
  	return ERR_PTR(ret);
79154b1b5   Chris Mason   Btrfs: transactio...
580
  }
f92957493   Chris Mason   btrfs_start_trans...
581
  struct btrfs_trans_handle *btrfs_start_transaction(struct btrfs_root *root,
5aed1dd8b   Alexandru Moise   btrfs: change num...
582
  						   unsigned int num_items)
f92957493   Chris Mason   btrfs_start_trans...
583
  {
08e007d2e   Miao Xie   Btrfs: improve th...
584
585
  	return start_transaction(root, num_items, TRANS_START,
  				 BTRFS_RESERVE_FLUSH_ALL);
f92957493   Chris Mason   btrfs_start_trans...
586
  }
8eab77ff1   Filipe Manana   Btrfs: use global...
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
  struct btrfs_trans_handle *btrfs_start_transaction_fallback_global_rsv(
  					struct btrfs_root *root,
  					unsigned int num_items,
  					int min_factor)
  {
  	struct btrfs_trans_handle *trans;
  	u64 num_bytes;
  	int ret;
  
  	trans = btrfs_start_transaction(root, num_items);
  	if (!IS_ERR(trans) || PTR_ERR(trans) != -ENOSPC)
  		return trans;
  
  	trans = btrfs_start_transaction(root, 0);
  	if (IS_ERR(trans))
  		return trans;
  
  	num_bytes = btrfs_calc_trans_metadata_size(root, num_items);
  	ret = btrfs_cond_migrate_bytes(root->fs_info,
  				       &root->fs_info->trans_block_rsv,
  				       num_bytes,
  				       min_factor);
  	if (ret) {
  		btrfs_end_transaction(trans, root);
  		return ERR_PTR(ret);
  	}
  
  	trans->block_rsv = &root->fs_info->trans_block_rsv;
  	trans->bytes_reserved = num_bytes;
88d3a5aaf   Josef Bacik   Btrfs: add transa...
616
617
  	trace_btrfs_space_reservation(root->fs_info, "transaction",
  				      trans->transid, num_bytes, 1);
8eab77ff1   Filipe Manana   Btrfs: use global...
618
619
620
  
  	return trans;
  }
8407aa464   Miao Xie   Btrfs: fix corrup...
621

08e007d2e   Miao Xie   Btrfs: improve th...
622
  struct btrfs_trans_handle *btrfs_start_transaction_lflush(
5aed1dd8b   Alexandru Moise   btrfs: change num...
623
624
  					struct btrfs_root *root,
  					unsigned int num_items)
8407aa464   Miao Xie   Btrfs: fix corrup...
625
  {
08e007d2e   Miao Xie   Btrfs: improve th...
626
627
  	return start_transaction(root, num_items, TRANS_START,
  				 BTRFS_RESERVE_FLUSH_LIMIT);
8407aa464   Miao Xie   Btrfs: fix corrup...
628
  }
7a7eaa40a   Josef Bacik   Btrfs: take away ...
629
  struct btrfs_trans_handle *btrfs_join_transaction(struct btrfs_root *root)
f92957493   Chris Mason   btrfs_start_trans...
630
  {
575a75d6f   Alexandru Moise   btrfs: pass prope...
631
632
  	return start_transaction(root, 0, TRANS_JOIN,
  				 BTRFS_RESERVE_NO_FLUSH);
f92957493   Chris Mason   btrfs_start_trans...
633
  }
7a7eaa40a   Josef Bacik   Btrfs: take away ...
634
  struct btrfs_trans_handle *btrfs_join_transaction_nolock(struct btrfs_root *root)
0af3d00ba   Josef Bacik   Btrfs: create spe...
635
  {
575a75d6f   Alexandru Moise   btrfs: pass prope...
636
637
  	return start_transaction(root, 0, TRANS_JOIN_NOLOCK,
  				 BTRFS_RESERVE_NO_FLUSH);
0af3d00ba   Josef Bacik   Btrfs: create spe...
638
  }
7a7eaa40a   Josef Bacik   Btrfs: take away ...
639
  struct btrfs_trans_handle *btrfs_start_ioctl_transaction(struct btrfs_root *root)
9ca9ee09c   Sage Weil   Btrfs: fix ioctl-...
640
  {
575a75d6f   Alexandru Moise   btrfs: pass prope...
641
642
  	return start_transaction(root, 0, TRANS_USERSPACE,
  				 BTRFS_RESERVE_NO_FLUSH);
9ca9ee09c   Sage Weil   Btrfs: fix ioctl-...
643
  }
d4edf39bd   Miao Xie   Btrfs: fix uncomp...
644
645
646
647
648
649
650
651
652
653
654
655
656
  /*
   * btrfs_attach_transaction() - catch the running transaction
   *
   * It is used when we want to commit the current the transaction, but
   * don't want to start a new one.
   *
   * Note: If this function return -ENOENT, it just means there is no
   * running transaction. But it is possible that the inactive transaction
   * is still in the memory, not fully on disk. If you hope there is no
   * inactive transaction in the fs when -ENOENT is returned, you should
   * invoke
   *     btrfs_attach_transaction_barrier()
   */
354aa0fb6   Miao Xie   Btrfs: fix orphan...
657
  struct btrfs_trans_handle *btrfs_attach_transaction(struct btrfs_root *root)
60376ce4a   Josef Bacik   Btrfs: fix race i...
658
  {
575a75d6f   Alexandru Moise   btrfs: pass prope...
659
660
  	return start_transaction(root, 0, TRANS_ATTACH,
  				 BTRFS_RESERVE_NO_FLUSH);
60376ce4a   Josef Bacik   Btrfs: fix race i...
661
  }
d4edf39bd   Miao Xie   Btrfs: fix uncomp...
662
  /*
90b6d2830   Wang Sheng-Hui   Btrfs: fix the co...
663
   * btrfs_attach_transaction_barrier() - catch the running transaction
d4edf39bd   Miao Xie   Btrfs: fix uncomp...
664
665
666
667
668
669
670
671
672
   *
   * It is similar to the above function, the differentia is this one
   * will wait for all the inactive transactions until they fully
   * complete.
   */
  struct btrfs_trans_handle *
  btrfs_attach_transaction_barrier(struct btrfs_root *root)
  {
  	struct btrfs_trans_handle *trans;
575a75d6f   Alexandru Moise   btrfs: pass prope...
673
674
  	trans = start_transaction(root, 0, TRANS_ATTACH,
  				  BTRFS_RESERVE_NO_FLUSH);
d4edf39bd   Miao Xie   Btrfs: fix uncomp...
675
676
677
678
679
  	if (IS_ERR(trans) && PTR_ERR(trans) == -ENOENT)
  		btrfs_wait_for_commit(root, 0);
  
  	return trans;
  }
d352ac681   Chris Mason   Btrfs: add and im...
680
  /* wait for a transaction commit to be fully complete */
b9c8300c2   Li Zefan   Btrfs: remove a B...
681
  static noinline void wait_for_commit(struct btrfs_root *root,
89ce8a63d   Chris Mason   Add btrfs_end_tra...
682
683
  				    struct btrfs_transaction *commit)
  {
4a9d8bdee   Miao Xie   Btrfs: make the s...
684
  	wait_event(commit->commit_wait, commit->state == TRANS_STATE_COMPLETED);
89ce8a63d   Chris Mason   Add btrfs_end_tra...
685
  }
462045928   Sage Weil   Btrfs: add START_...
686
687
688
  int btrfs_wait_for_commit(struct btrfs_root *root, u64 transid)
  {
  	struct btrfs_transaction *cur_trans = NULL, *t;
8cd2807f7   Miao Xie   Btrfs: fix wrong ...
689
  	int ret = 0;
462045928   Sage Weil   Btrfs: add START_...
690

462045928   Sage Weil   Btrfs: add START_...
691
692
  	if (transid) {
  		if (transid <= root->fs_info->last_trans_committed)
a4abeea41   Josef Bacik   Btrfs: kill trans...
693
  			goto out;
462045928   Sage Weil   Btrfs: add START_...
694
695
  
  		/* find specified transaction */
a4abeea41   Josef Bacik   Btrfs: kill trans...
696
  		spin_lock(&root->fs_info->trans_lock);
462045928   Sage Weil   Btrfs: add START_...
697
698
699
  		list_for_each_entry(t, &root->fs_info->trans_list, list) {
  			if (t->transid == transid) {
  				cur_trans = t;
a4abeea41   Josef Bacik   Btrfs: kill trans...
700
  				atomic_inc(&cur_trans->use_count);
8cd2807f7   Miao Xie   Btrfs: fix wrong ...
701
  				ret = 0;
462045928   Sage Weil   Btrfs: add START_...
702
703
  				break;
  			}
8cd2807f7   Miao Xie   Btrfs: fix wrong ...
704
705
  			if (t->transid > transid) {
  				ret = 0;
462045928   Sage Weil   Btrfs: add START_...
706
  				break;
8cd2807f7   Miao Xie   Btrfs: fix wrong ...
707
  			}
462045928   Sage Weil   Btrfs: add START_...
708
  		}
a4abeea41   Josef Bacik   Btrfs: kill trans...
709
  		spin_unlock(&root->fs_info->trans_lock);
42383020b   Sage Weil   Btrfs: fix race i...
710
711
712
713
714
715
716
717
  
  		/*
  		 * The specified transaction doesn't exist, or we
  		 * raced with btrfs_commit_transaction
  		 */
  		if (!cur_trans) {
  			if (transid > root->fs_info->last_trans_committed)
  				ret = -EINVAL;
8cd2807f7   Miao Xie   Btrfs: fix wrong ...
718
  			goto out;
42383020b   Sage Weil   Btrfs: fix race i...
719
  		}
462045928   Sage Weil   Btrfs: add START_...
720
721
  	} else {
  		/* find newest transaction that is committing | committed */
a4abeea41   Josef Bacik   Btrfs: kill trans...
722
  		spin_lock(&root->fs_info->trans_lock);
462045928   Sage Weil   Btrfs: add START_...
723
724
  		list_for_each_entry_reverse(t, &root->fs_info->trans_list,
  					    list) {
4a9d8bdee   Miao Xie   Btrfs: make the s...
725
726
  			if (t->state >= TRANS_STATE_COMMIT_START) {
  				if (t->state == TRANS_STATE_COMPLETED)
3473f3c06   Josef Bacik   Btrfs: unlock the...
727
  					break;
462045928   Sage Weil   Btrfs: add START_...
728
  				cur_trans = t;
a4abeea41   Josef Bacik   Btrfs: kill trans...
729
  				atomic_inc(&cur_trans->use_count);
462045928   Sage Weil   Btrfs: add START_...
730
731
732
  				break;
  			}
  		}
a4abeea41   Josef Bacik   Btrfs: kill trans...
733
  		spin_unlock(&root->fs_info->trans_lock);
462045928   Sage Weil   Btrfs: add START_...
734
  		if (!cur_trans)
a4abeea41   Josef Bacik   Btrfs: kill trans...
735
  			goto out;  /* nothing committing|committed */
462045928   Sage Weil   Btrfs: add START_...
736
  	}
462045928   Sage Weil   Btrfs: add START_...
737
  	wait_for_commit(root, cur_trans);
724e2315d   Josef Bacik   Btrfs: fix two us...
738
  	btrfs_put_transaction(cur_trans);
a4abeea41   Josef Bacik   Btrfs: kill trans...
739
  out:
462045928   Sage Weil   Btrfs: add START_...
740
741
  	return ret;
  }
37d1aeee3   Chris Mason   Btrfs: Throttle t...
742
743
  void btrfs_throttle(struct btrfs_root *root)
  {
a4abeea41   Josef Bacik   Btrfs: kill trans...
744
  	if (!atomic_read(&root->fs_info->open_ioctl_trans))
9ca9ee09c   Sage Weil   Btrfs: fix ioctl-...
745
  		wait_current_trans(root);
37d1aeee3   Chris Mason   Btrfs: Throttle t...
746
  }
8929ecfa5   Yan, Zheng   Btrfs: Introduce ...
747
748
749
  static int should_end_transaction(struct btrfs_trans_handle *trans,
  				  struct btrfs_root *root)
  {
1be41b78b   Josef Bacik   Btrfs: fix transa...
750
  	if (root->fs_info->global_block_rsv.space_info->full &&
0a2b2a844   Josef Bacik   Btrfs: throttle d...
751
  	    btrfs_check_space_for_delayed_refs(trans, root))
1be41b78b   Josef Bacik   Btrfs: fix transa...
752
  		return 1;
36ba022ac   Josef Bacik   Btrfs: seperate o...
753

1be41b78b   Josef Bacik   Btrfs: fix transa...
754
  	return !!btrfs_block_rsv_check(root, &root->fs_info->global_block_rsv, 5);
8929ecfa5   Yan, Zheng   Btrfs: Introduce ...
755
756
757
758
759
760
761
  }
  
  int btrfs_should_end_transaction(struct btrfs_trans_handle *trans,
  				 struct btrfs_root *root)
  {
  	struct btrfs_transaction *cur_trans = trans->transaction;
  	int updates;
49b25e054   Jeff Mahoney   btrfs: enhance tr...
762
  	int err;
8929ecfa5   Yan, Zheng   Btrfs: Introduce ...
763

a4abeea41   Josef Bacik   Btrfs: kill trans...
764
  	smp_mb();
4a9d8bdee   Miao Xie   Btrfs: make the s...
765
766
  	if (cur_trans->state >= TRANS_STATE_BLOCKED ||
  	    cur_trans->delayed_refs.flushing)
8929ecfa5   Yan, Zheng   Btrfs: Introduce ...
767
768
769
770
  		return 1;
  
  	updates = trans->delayed_ref_updates;
  	trans->delayed_ref_updates = 0;
49b25e054   Jeff Mahoney   btrfs: enhance tr...
771
  	if (updates) {
28ed1345a   Chris Mason   btrfs: actively r...
772
  		err = btrfs_run_delayed_refs(trans, root, updates * 2);
49b25e054   Jeff Mahoney   btrfs: enhance tr...
773
774
775
  		if (err) /* Error code will also eval true */
  			return err;
  	}
8929ecfa5   Yan, Zheng   Btrfs: Introduce ...
776
777
778
  
  	return should_end_transaction(trans, root);
  }
89ce8a63d   Chris Mason   Add btrfs_end_tra...
779
  static int __btrfs_end_transaction(struct btrfs_trans_handle *trans,
a698d0755   Miao Xie   Btrfs: add a type...
780
  			  struct btrfs_root *root, int throttle)
79154b1b5   Chris Mason   Btrfs: transactio...
781
  {
8929ecfa5   Yan, Zheng   Btrfs: Introduce ...
782
  	struct btrfs_transaction *cur_trans = trans->transaction;
ab78c84de   Chris Mason   Btrfs: Throttle o...
783
  	struct btrfs_fs_info *info = root->fs_info;
31b9655f4   Josef Bacik   Btrfs: track tran...
784
  	u64 transid = trans->transid;
1be41b78b   Josef Bacik   Btrfs: fix transa...
785
  	unsigned long cur = trans->delayed_ref_updates;
a698d0755   Miao Xie   Btrfs: add a type...
786
  	int lock = (trans->type != TRANS_JOIN_NOLOCK);
4edc2ca38   Dave Jones   Btrfs: fix use-af...
787
  	int err = 0;
a79b7d4b3   Chris Mason   Btrfs: async dela...
788
  	int must_run_delayed_refs = 0;
c3e69d58e   Chris Mason   Btrfs: process th...
789

3bbb24b20   Josef Bacik   Btrfs: fix deadlo...
790
791
  	if (trans->use_count > 1) {
  		trans->use_count--;
2a1eb4614   Josef Bacik   Btrfs: if we've a...
792
793
794
  		trans->block_rsv = trans->orig_rsv;
  		return 0;
  	}
b24e03db0   Josef Bacik   Btrfs: release tr...
795
  	btrfs_trans_release_metadata(trans, root);
4c13d758b   Josef Bacik   Btrfs: use the tr...
796
  	trans->block_rsv = NULL;
c55672379   Arne Jansen   Btrfs: hooks to r...
797

ea658badc   Josef Bacik   Btrfs: delay bloc...
798
799
  	if (!list_empty(&trans->new_bgs))
  		btrfs_create_pending_block_groups(trans, root);
1be41b78b   Josef Bacik   Btrfs: fix transa...
800
  	trans->delayed_ref_updates = 0;
a79b7d4b3   Chris Mason   Btrfs: async dela...
801
802
803
  	if (!trans->sync) {
  		must_run_delayed_refs =
  			btrfs_should_throttle_delayed_refs(trans, root);
0a2b2a844   Josef Bacik   Btrfs: throttle d...
804
  		cur = max_t(unsigned long, cur, 32);
a79b7d4b3   Chris Mason   Btrfs: async dela...
805
806
807
808
809
810
811
812
  
  		/*
  		 * don't make the caller wait if they are from a NOLOCK
  		 * or ATTACH transaction, it will deadlock with commit
  		 */
  		if (must_run_delayed_refs == 1 &&
  		    (trans->type & (__TRANS_JOIN_NOLOCK | __TRANS_ATTACH)))
  			must_run_delayed_refs = 2;
56bec294d   Chris Mason   Btrfs: do extent ...
813
  	}
bb721703a   Chris Mason   Btrfs: reduce CPU...
814

0e7211069   Josef Bacik   Btrfs: change how...
815
816
  	btrfs_trans_release_metadata(trans, root);
  	trans->block_rsv = NULL;
56bec294d   Chris Mason   Btrfs: do extent ...
817

ea658badc   Josef Bacik   Btrfs: delay bloc...
818
819
  	if (!list_empty(&trans->new_bgs))
  		btrfs_create_pending_block_groups(trans, root);
4fbcdf669   Filipe Manana   Btrfs: fix -ENOSP...
820
  	btrfs_trans_release_chunk_metadata(trans);
a4abeea41   Josef Bacik   Btrfs: kill trans...
821
  	if (lock && !atomic_read(&root->fs_info->open_ioctl_trans) &&
4a9d8bdee   Miao Xie   Btrfs: make the s...
822
823
824
825
826
827
  	    should_end_transaction(trans, root) &&
  	    ACCESS_ONCE(cur_trans->state) == TRANS_STATE_RUNNING) {
  		spin_lock(&info->trans_lock);
  		if (cur_trans->state == TRANS_STATE_RUNNING)
  			cur_trans->state = TRANS_STATE_BLOCKED;
  		spin_unlock(&info->trans_lock);
a4abeea41   Josef Bacik   Btrfs: kill trans...
828
  	}
8929ecfa5   Yan, Zheng   Btrfs: Introduce ...
829

4a9d8bdee   Miao Xie   Btrfs: make the s...
830
  	if (lock && ACCESS_ONCE(cur_trans->state) == TRANS_STATE_BLOCKED) {
3bbb24b20   Josef Bacik   Btrfs: fix deadlo...
831
  		if (throttle)
8929ecfa5   Yan, Zheng   Btrfs: Introduce ...
832
  			return btrfs_commit_transaction(trans, root);
3bbb24b20   Josef Bacik   Btrfs: fix deadlo...
833
  		else
8929ecfa5   Yan, Zheng   Btrfs: Introduce ...
834
835
  			wake_up_process(info->transaction_kthread);
  	}
0860adfdb   Miao Xie   Btrfs: don't wait...
836
  	if (trans->type & __TRANS_FREEZABLE)
98114659e   Josef Bacik   Btrfs: fix race w...
837
  		sb_end_intwrite(root->fs_info->sb);
6df7881a8   Josef Bacik   Btrfs: move the s...
838

8929ecfa5   Yan, Zheng   Btrfs: Introduce ...
839
  	WARN_ON(cur_trans != info->running_transaction);
13c5a93e7   Josef Bacik   Btrfs: avoid taki...
840
841
  	WARN_ON(atomic_read(&cur_trans->num_writers) < 1);
  	atomic_dec(&cur_trans->num_writers);
0860adfdb   Miao Xie   Btrfs: don't wait...
842
  	extwriter_counter_dec(cur_trans, trans->type);
89ce8a63d   Chris Mason   Add btrfs_end_tra...
843

a83342aa0   David Sterba   btrfs: add commen...
844
845
846
  	/*
  	 * Make sure counter is updated before we wake up waiters.
  	 */
99d16cbca   Sage Weil   Btrfs: fix deadlo...
847
  	smp_mb();
79154b1b5   Chris Mason   Btrfs: transactio...
848
849
  	if (waitqueue_active(&cur_trans->writer_wait))
  		wake_up(&cur_trans->writer_wait);
724e2315d   Josef Bacik   Btrfs: fix two us...
850
  	btrfs_put_transaction(cur_trans);
9ed74f2db   Josef Bacik   Btrfs: proper -EN...
851
852
853
  
  	if (current->journal_info == trans)
  		current->journal_info = NULL;
ab78c84de   Chris Mason   Btrfs: Throttle o...
854

24bbcf044   Yan, Zheng   Btrfs: Add delaye...
855
856
  	if (throttle)
  		btrfs_run_delayed_iputs(root);
49b25e054   Jeff Mahoney   btrfs: enhance tr...
857
  	if (trans->aborted ||
4e121c06a   Josef Bacik   Btrfs: cleanup tr...
858
859
  	    test_bit(BTRFS_FS_STATE_ERROR, &root->fs_info->fs_state)) {
  		wake_up_process(info->transaction_kthread);
4edc2ca38   Dave Jones   Btrfs: fix use-af...
860
  		err = -EIO;
4e121c06a   Josef Bacik   Btrfs: cleanup tr...
861
  	}
edf39272d   Jan Schmidt   Btrfs: call the q...
862
  	assert_qgroups_uptodate(trans);
49b25e054   Jeff Mahoney   btrfs: enhance tr...
863

4edc2ca38   Dave Jones   Btrfs: fix use-af...
864
  	kmem_cache_free(btrfs_trans_handle_cachep, trans);
a79b7d4b3   Chris Mason   Btrfs: async dela...
865
  	if (must_run_delayed_refs) {
31b9655f4   Josef Bacik   Btrfs: track tran...
866
  		btrfs_async_run_delayed_refs(root, cur, transid,
a79b7d4b3   Chris Mason   Btrfs: async dela...
867
868
  					     must_run_delayed_refs == 1);
  	}
4edc2ca38   Dave Jones   Btrfs: fix use-af...
869
  	return err;
79154b1b5   Chris Mason   Btrfs: transactio...
870
  }
89ce8a63d   Chris Mason   Add btrfs_end_tra...
871
872
873
  int btrfs_end_transaction(struct btrfs_trans_handle *trans,
  			  struct btrfs_root *root)
  {
98ad43be0   Wang Shilong   Btrfs: cleanup to...
874
  	return __btrfs_end_transaction(trans, root, 0);
89ce8a63d   Chris Mason   Add btrfs_end_tra...
875
876
877
878
879
  }
  
  int btrfs_end_transaction_throttle(struct btrfs_trans_handle *trans,
  				   struct btrfs_root *root)
  {
98ad43be0   Wang Shilong   Btrfs: cleanup to...
880
  	return __btrfs_end_transaction(trans, root, 1);
16cdcec73   Miao Xie   btrfs: implement ...
881
  }
d352ac681   Chris Mason   Btrfs: add and im...
882
883
884
  /*
   * when btree blocks are allocated, they have some corresponding bits set for
   * them in one of two extent_io trees.  This is used to make sure all of
690587d10   Chris Mason   Btrfs: streamline...
885
   * those extents are sent to disk but does not wait on them
d352ac681   Chris Mason   Btrfs: add and im...
886
   */
690587d10   Chris Mason   Btrfs: streamline...
887
  int btrfs_write_marked_extents(struct btrfs_root *root,
8cef4e160   Yan, Zheng   Btrfs: Avoid supe...
888
  			       struct extent_io_tree *dirty_pages, int mark)
79154b1b5   Chris Mason   Btrfs: transactio...
889
  {
777e6bd70   Chris Mason   Btrfs: Transactio...
890
  	int err = 0;
7c4452b9a   Chris Mason   Btrfs: smarter tr...
891
  	int werr = 0;
1728366ef   Josef Bacik   Btrfs: stop using...
892
  	struct address_space *mapping = root->fs_info->btree_inode->i_mapping;
e6138876a   Josef Bacik   Btrfs: cache exte...
893
  	struct extent_state *cached_state = NULL;
777e6bd70   Chris Mason   Btrfs: Transactio...
894
  	u64 start = 0;
5f39d397d   Chris Mason   Btrfs: Create ext...
895
  	u64 end;
7c4452b9a   Chris Mason   Btrfs: smarter tr...
896

1728366ef   Josef Bacik   Btrfs: stop using...
897
  	while (!find_first_extent_bit(dirty_pages, start, &start, &end,
e6138876a   Josef Bacik   Btrfs: cache exte...
898
  				      mark, &cached_state)) {
663dfbb07   Filipe Manana   Btrfs: deal with ...
899
900
901
902
  		bool wait_writeback = false;
  
  		err = convert_extent_bit(dirty_pages, start, end,
  					 EXTENT_NEED_WAIT,
210aa2776   David Sterba   btrfs: sink gfp p...
903
  					 mark, &cached_state);
663dfbb07   Filipe Manana   Btrfs: deal with ...
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
  		/*
  		 * convert_extent_bit can return -ENOMEM, which is most of the
  		 * time a temporary error. So when it happens, ignore the error
  		 * and wait for writeback of this range to finish - because we
  		 * failed to set the bit EXTENT_NEED_WAIT for the range, a call
  		 * to btrfs_wait_marked_extents() would not know that writeback
  		 * for this range started and therefore wouldn't wait for it to
  		 * finish - we don't want to commit a superblock that points to
  		 * btree nodes/leafs for which writeback hasn't finished yet
  		 * (and without errors).
  		 * We cleanup any entries left in the io tree when committing
  		 * the transaction (through clear_btree_io_tree()).
  		 */
  		if (err == -ENOMEM) {
  			err = 0;
  			wait_writeback = true;
  		}
  		if (!err)
  			err = filemap_fdatawrite_range(mapping, start, end);
1728366ef   Josef Bacik   Btrfs: stop using...
923
924
  		if (err)
  			werr = err;
663dfbb07   Filipe Manana   Btrfs: deal with ...
925
926
  		else if (wait_writeback)
  			werr = filemap_fdatawait_range(mapping, start, end);
e38e2ed70   Filipe Manana   Btrfs: make find_...
927
  		free_extent_state(cached_state);
663dfbb07   Filipe Manana   Btrfs: deal with ...
928
  		cached_state = NULL;
1728366ef   Josef Bacik   Btrfs: stop using...
929
930
  		cond_resched();
  		start = end + 1;
7c4452b9a   Chris Mason   Btrfs: smarter tr...
931
  	}
690587d10   Chris Mason   Btrfs: streamline...
932
933
934
935
936
937
938
939
940
941
  	return werr;
  }
  
  /*
   * when btree blocks are allocated, they have some corresponding bits set for
   * them in one of two extent_io trees.  This is used to make sure all of
   * those extents are on disk for transaction or log commit.  We wait
   * on all the pages and clear them from the dirty pages state tree
   */
  int btrfs_wait_marked_extents(struct btrfs_root *root,
8cef4e160   Yan, Zheng   Btrfs: Avoid supe...
942
  			      struct extent_io_tree *dirty_pages, int mark)
690587d10   Chris Mason   Btrfs: streamline...
943
  {
690587d10   Chris Mason   Btrfs: streamline...
944
945
  	int err = 0;
  	int werr = 0;
1728366ef   Josef Bacik   Btrfs: stop using...
946
  	struct address_space *mapping = root->fs_info->btree_inode->i_mapping;
e6138876a   Josef Bacik   Btrfs: cache exte...
947
  	struct extent_state *cached_state = NULL;
690587d10   Chris Mason   Btrfs: streamline...
948
949
  	u64 start = 0;
  	u64 end;
656f30dba   Filipe Manana   Btrfs: be aware o...
950
  	bool errors = false;
777e6bd70   Chris Mason   Btrfs: Transactio...
951

1728366ef   Josef Bacik   Btrfs: stop using...
952
  	while (!find_first_extent_bit(dirty_pages, start, &start, &end,
e6138876a   Josef Bacik   Btrfs: cache exte...
953
  				      EXTENT_NEED_WAIT, &cached_state)) {
663dfbb07   Filipe Manana   Btrfs: deal with ...
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
  		/*
  		 * Ignore -ENOMEM errors returned by clear_extent_bit().
  		 * When committing the transaction, we'll remove any entries
  		 * left in the io tree. For a log commit, we don't remove them
  		 * after committing the log because the tree can be accessed
  		 * concurrently - we do it only at transaction commit time when
  		 * it's safe to do it (through clear_btree_io_tree()).
  		 */
  		err = clear_extent_bit(dirty_pages, start, end,
  				       EXTENT_NEED_WAIT,
  				       0, 0, &cached_state, GFP_NOFS);
  		if (err == -ENOMEM)
  			err = 0;
  		if (!err)
  			err = filemap_fdatawait_range(mapping, start, end);
1728366ef   Josef Bacik   Btrfs: stop using...
969
970
  		if (err)
  			werr = err;
e38e2ed70   Filipe Manana   Btrfs: make find_...
971
972
  		free_extent_state(cached_state);
  		cached_state = NULL;
1728366ef   Josef Bacik   Btrfs: stop using...
973
974
  		cond_resched();
  		start = end + 1;
777e6bd70   Chris Mason   Btrfs: Transactio...
975
  	}
7c4452b9a   Chris Mason   Btrfs: smarter tr...
976
977
  	if (err)
  		werr = err;
656f30dba   Filipe Manana   Btrfs: be aware o...
978
979
980
  
  	if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) {
  		if ((mark & EXTENT_DIRTY) &&
afcdd129e   Josef Bacik   Btrfs: add a flag...
981
982
  		    test_and_clear_bit(BTRFS_FS_LOG1_ERR,
  				       &root->fs_info->flags))
656f30dba   Filipe Manana   Btrfs: be aware o...
983
984
985
  			errors = true;
  
  		if ((mark & EXTENT_NEW) &&
afcdd129e   Josef Bacik   Btrfs: add a flag...
986
987
  		    test_and_clear_bit(BTRFS_FS_LOG2_ERR,
  				       &root->fs_info->flags))
656f30dba   Filipe Manana   Btrfs: be aware o...
988
989
  			errors = true;
  	} else {
afcdd129e   Josef Bacik   Btrfs: add a flag...
990
991
  		if (test_and_clear_bit(BTRFS_FS_BTREE_ERR,
  				       &root->fs_info->flags))
656f30dba   Filipe Manana   Btrfs: be aware o...
992
993
994
995
996
  			errors = true;
  	}
  
  	if (errors && !werr)
  		werr = -EIO;
7c4452b9a   Chris Mason   Btrfs: smarter tr...
997
  	return werr;
79154b1b5   Chris Mason   Btrfs: transactio...
998
  }
690587d10   Chris Mason   Btrfs: streamline...
999
1000
1001
1002
1003
  /*
   * when btree blocks are allocated, they have some corresponding bits set for
   * them in one of two extent_io trees.  This is used to make sure all of
   * those extents are on disk for transaction or log commit
   */
171170c1c   Sergei Trofimovich   btrfs: mark some ...
1004
  static int btrfs_write_and_wait_marked_extents(struct btrfs_root *root,
8cef4e160   Yan, Zheng   Btrfs: Avoid supe...
1005
  				struct extent_io_tree *dirty_pages, int mark)
690587d10   Chris Mason   Btrfs: streamline...
1006
1007
1008
  {
  	int ret;
  	int ret2;
c6adc9cc0   Miao Xie   Btrfs: merge pend...
1009
  	struct blk_plug plug;
690587d10   Chris Mason   Btrfs: streamline...
1010

c6adc9cc0   Miao Xie   Btrfs: merge pend...
1011
  	blk_start_plug(&plug);
8cef4e160   Yan, Zheng   Btrfs: Avoid supe...
1012
  	ret = btrfs_write_marked_extents(root, dirty_pages, mark);
c6adc9cc0   Miao Xie   Btrfs: merge pend...
1013
  	blk_finish_plug(&plug);
8cef4e160   Yan, Zheng   Btrfs: Avoid supe...
1014
  	ret2 = btrfs_wait_marked_extents(root, dirty_pages, mark);
bf0da8c18   Chris Mason   Btrfs: ClearPageE...
1015
1016
1017
1018
1019
1020
  
  	if (ret)
  		return ret;
  	if (ret2)
  		return ret2;
  	return 0;
690587d10   Chris Mason   Btrfs: streamline...
1021
  }
663dfbb07   Filipe Manana   Btrfs: deal with ...
1022
  static int btrfs_write_and_wait_transaction(struct btrfs_trans_handle *trans,
d0c803c40   Chris Mason   Btrfs: Record dir...
1023
1024
  				     struct btrfs_root *root)
  {
663dfbb07   Filipe Manana   Btrfs: deal with ...
1025
1026
1027
  	int ret;
  
  	ret = btrfs_write_and_wait_marked_extents(root,
8cef4e160   Yan, Zheng   Btrfs: Avoid supe...
1028
1029
  					   &trans->transaction->dirty_pages,
  					   EXTENT_DIRTY);
663dfbb07   Filipe Manana   Btrfs: deal with ...
1030
1031
1032
  	clear_btree_io_tree(&trans->transaction->dirty_pages);
  
  	return ret;
d0c803c40   Chris Mason   Btrfs: Record dir...
1033
  }
d352ac681   Chris Mason   Btrfs: add and im...
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
  /*
   * this is used to update the root pointer in the tree of tree roots.
   *
   * But, in the case of the extent allocation tree, updating the root
   * pointer may allocate blocks which may change the root of the extent
   * allocation tree.
   *
   * So, this loops and repeats and makes sure the cowonly root didn't
   * change while the root pointer was being updated in the metadata.
   */
0b86a832a   Chris Mason   Btrfs: Add suppor...
1044
1045
  static int update_cowonly_root(struct btrfs_trans_handle *trans,
  			       struct btrfs_root *root)
79154b1b5   Chris Mason   Btrfs: transactio...
1046
1047
  {
  	int ret;
0b86a832a   Chris Mason   Btrfs: Add suppor...
1048
  	u64 old_root_bytenr;
86b9f2eca   Yan, Zheng   Btrfs: Fix per ro...
1049
  	u64 old_root_used;
0b86a832a   Chris Mason   Btrfs: Add suppor...
1050
  	struct btrfs_root *tree_root = root->fs_info->tree_root;
79154b1b5   Chris Mason   Btrfs: transactio...
1051

86b9f2eca   Yan, Zheng   Btrfs: Fix per ro...
1052
  	old_root_used = btrfs_root_used(&root->root_item);
56bec294d   Chris Mason   Btrfs: do extent ...
1053

d397712bc   Chris Mason   Btrfs: Fix checkp...
1054
  	while (1) {
0b86a832a   Chris Mason   Btrfs: Add suppor...
1055
  		old_root_bytenr = btrfs_root_bytenr(&root->root_item);
86b9f2eca   Yan, Zheng   Btrfs: Fix per ro...
1056
  		if (old_root_bytenr == root->node->start &&
ea526d189   Josef Bacik   Btrfs: fix ASSERT...
1057
  		    old_root_used == btrfs_root_used(&root->root_item))
79154b1b5   Chris Mason   Btrfs: transactio...
1058
  			break;
87ef2bb46   Chris Mason   Btrfs: prevent lo...
1059

5d4f98a28   Yan Zheng   Btrfs: Mixed back...
1060
  		btrfs_set_root_node(&root->root_item, root->node);
79154b1b5   Chris Mason   Btrfs: transactio...
1061
  		ret = btrfs_update_root(trans, tree_root,
0b86a832a   Chris Mason   Btrfs: Add suppor...
1062
1063
  					&root->root_key,
  					&root->root_item);
49b25e054   Jeff Mahoney   btrfs: enhance tr...
1064
1065
  		if (ret)
  			return ret;
56bec294d   Chris Mason   Btrfs: do extent ...
1066

86b9f2eca   Yan, Zheng   Btrfs: Fix per ro...
1067
  		old_root_used = btrfs_root_used(&root->root_item);
0b86a832a   Chris Mason   Btrfs: Add suppor...
1068
  	}
276e680d1   Yan Zheng   Btrfs: preserve c...
1069

0b86a832a   Chris Mason   Btrfs: Add suppor...
1070
1071
  	return 0;
  }
d352ac681   Chris Mason   Btrfs: add and im...
1072
1073
  /*
   * update all the cowonly tree roots on disk
49b25e054   Jeff Mahoney   btrfs: enhance tr...
1074
1075
1076
1077
   *
   * The error handling in this function may not be obvious. Any of the
   * failures will cause the file system to go offline. We still need
   * to clean up the delayed refs.
d352ac681   Chris Mason   Btrfs: add and im...
1078
   */
5d4f98a28   Yan Zheng   Btrfs: Mixed back...
1079
1080
  static noinline int commit_cowonly_roots(struct btrfs_trans_handle *trans,
  					 struct btrfs_root *root)
0b86a832a   Chris Mason   Btrfs: Add suppor...
1081
1082
  {
  	struct btrfs_fs_info *fs_info = root->fs_info;
ea526d189   Josef Bacik   Btrfs: fix ASSERT...
1083
  	struct list_head *dirty_bgs = &trans->transaction->dirty_bgs;
1bbc621ef   Chris Mason   Btrfs: allow bloc...
1084
  	struct list_head *io_bgs = &trans->transaction->io_bgs;
0b86a832a   Chris Mason   Btrfs: Add suppor...
1085
  	struct list_head *next;
84234f3a1   Yan Zheng   Btrfs: Add root t...
1086
  	struct extent_buffer *eb;
56bec294d   Chris Mason   Btrfs: do extent ...
1087
  	int ret;
84234f3a1   Yan Zheng   Btrfs: Add root t...
1088
1089
  
  	eb = btrfs_lock_root_node(fs_info->tree_root);
49b25e054   Jeff Mahoney   btrfs: enhance tr...
1090
1091
  	ret = btrfs_cow_block(trans, fs_info->tree_root, eb, NULL,
  			      0, &eb);
84234f3a1   Yan Zheng   Btrfs: Add root t...
1092
1093
  	btrfs_tree_unlock(eb);
  	free_extent_buffer(eb);
0b86a832a   Chris Mason   Btrfs: Add suppor...
1094

49b25e054   Jeff Mahoney   btrfs: enhance tr...
1095
1096
  	if (ret)
  		return ret;
56bec294d   Chris Mason   Btrfs: do extent ...
1097
  	ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
49b25e054   Jeff Mahoney   btrfs: enhance tr...
1098
1099
  	if (ret)
  		return ret;
87ef2bb46   Chris Mason   Btrfs: prevent lo...
1100

733f4fbbc   Stefan Behrens   Btrfs: read devic...
1101
  	ret = btrfs_run_dev_stats(trans, root->fs_info);
c16ce1901   Josef Bacik   Btrfs: remove all...
1102
1103
  	if (ret)
  		return ret;
8dabb7420   Stefan Behrens   Btrfs: change cor...
1104
  	ret = btrfs_run_dev_replace(trans, root->fs_info);
c16ce1901   Josef Bacik   Btrfs: remove all...
1105
1106
  	if (ret)
  		return ret;
546adb0d8   Jan Schmidt   Btrfs: hooks for ...
1107
  	ret = btrfs_run_qgroups(trans, root->fs_info);
c16ce1901   Josef Bacik   Btrfs: remove all...
1108
1109
  	if (ret)
  		return ret;
546adb0d8   Jan Schmidt   Btrfs: hooks for ...
1110

dcdf7f6dd   Josef Bacik   Btrfs: prepare bl...
1111
1112
1113
  	ret = btrfs_setup_space_cache(trans, root);
  	if (ret)
  		return ret;
546adb0d8   Jan Schmidt   Btrfs: hooks for ...
1114
1115
  	/* run_qgroups might have added some more refs */
  	ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
c16ce1901   Josef Bacik   Btrfs: remove all...
1116
1117
  	if (ret)
  		return ret;
ea526d189   Josef Bacik   Btrfs: fix ASSERT...
1118
  again:
d397712bc   Chris Mason   Btrfs: Fix checkp...
1119
  	while (!list_empty(&fs_info->dirty_cowonly_roots)) {
0b86a832a   Chris Mason   Btrfs: Add suppor...
1120
1121
1122
  		next = fs_info->dirty_cowonly_roots.next;
  		list_del_init(next);
  		root = list_entry(next, struct btrfs_root, dirty_list);
e7070be19   Josef Bacik   Btrfs: change how...
1123
  		clear_bit(BTRFS_ROOT_DIRTY, &root->state);
87ef2bb46   Chris Mason   Btrfs: prevent lo...
1124

9e351cc86   Josef Bacik   Btrfs: remove tra...
1125
1126
1127
  		if (root != fs_info->extent_root)
  			list_add_tail(&root->dirty_list,
  				      &trans->transaction->switch_commits);
49b25e054   Jeff Mahoney   btrfs: enhance tr...
1128
1129
1130
  		ret = update_cowonly_root(trans, root);
  		if (ret)
  			return ret;
ea526d189   Josef Bacik   Btrfs: fix ASSERT...
1131
1132
1133
  		ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
  		if (ret)
  			return ret;
79154b1b5   Chris Mason   Btrfs: transactio...
1134
  	}
276e680d1   Yan Zheng   Btrfs: preserve c...
1135

1bbc621ef   Chris Mason   Btrfs: allow bloc...
1136
  	while (!list_empty(dirty_bgs) || !list_empty(io_bgs)) {
ea526d189   Josef Bacik   Btrfs: fix ASSERT...
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
  		ret = btrfs_write_dirty_block_groups(trans, root);
  		if (ret)
  			return ret;
  		ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
  		if (ret)
  			return ret;
  	}
  
  	if (!list_empty(&fs_info->dirty_cowonly_roots))
  		goto again;
9e351cc86   Josef Bacik   Btrfs: remove tra...
1147
1148
  	list_add_tail(&fs_info->extent_root->dirty_list,
  		      &trans->transaction->switch_commits);
8dabb7420   Stefan Behrens   Btrfs: change cor...
1149
  	btrfs_after_dev_replace_commit(fs_info);
79154b1b5   Chris Mason   Btrfs: transactio...
1150
1151
  	return 0;
  }
d352ac681   Chris Mason   Btrfs: add and im...
1152
1153
1154
1155
1156
  /*
   * dead roots are old snapshots that need to be deleted.  This allocates
   * a dirty root struct and adds it into the list of dead roots that need to
   * be deleted
   */
cfad392b2   Josef Bacik   Btrfs: check to s...
1157
  void btrfs_add_dead_root(struct btrfs_root *root)
5eda7b5e9   Chris Mason   Btrfs: Add the ab...
1158
  {
a4abeea41   Josef Bacik   Btrfs: kill trans...
1159
  	spin_lock(&root->fs_info->trans_lock);
cfad392b2   Josef Bacik   Btrfs: check to s...
1160
1161
  	if (list_empty(&root->root_list))
  		list_add_tail(&root->root_list, &root->fs_info->dead_roots);
a4abeea41   Josef Bacik   Btrfs: kill trans...
1162
  	spin_unlock(&root->fs_info->trans_lock);
5eda7b5e9   Chris Mason   Btrfs: Add the ab...
1163
  }
d352ac681   Chris Mason   Btrfs: add and im...
1164
  /*
5d4f98a28   Yan Zheng   Btrfs: Mixed back...
1165
   * update all the cowonly tree roots on disk
d352ac681   Chris Mason   Btrfs: add and im...
1166
   */
5d4f98a28   Yan Zheng   Btrfs: Mixed back...
1167
1168
  static noinline int commit_fs_roots(struct btrfs_trans_handle *trans,
  				    struct btrfs_root *root)
0f7d52f44   Chris Mason   Btrfs: groundwork...
1169
  {
0f7d52f44   Chris Mason   Btrfs: groundwork...
1170
  	struct btrfs_root *gang[8];
5d4f98a28   Yan Zheng   Btrfs: Mixed back...
1171
  	struct btrfs_fs_info *fs_info = root->fs_info;
0f7d52f44   Chris Mason   Btrfs: groundwork...
1172
1173
  	int i;
  	int ret;
54aa1f4df   Chris Mason   Btrfs: Audit call...
1174
  	int err = 0;
a4abeea41   Josef Bacik   Btrfs: kill trans...
1175
  	spin_lock(&fs_info->fs_roots_radix_lock);
d397712bc   Chris Mason   Btrfs: Fix checkp...
1176
  	while (1) {
5d4f98a28   Yan Zheng   Btrfs: Mixed back...
1177
1178
  		ret = radix_tree_gang_lookup_tag(&fs_info->fs_roots_radix,
  						 (void **)gang, 0,
0f7d52f44   Chris Mason   Btrfs: groundwork...
1179
1180
1181
1182
1183
1184
  						 ARRAY_SIZE(gang),
  						 BTRFS_ROOT_TRANS_TAG);
  		if (ret == 0)
  			break;
  		for (i = 0; i < ret; i++) {
  			root = gang[i];
5d4f98a28   Yan Zheng   Btrfs: Mixed back...
1185
1186
1187
  			radix_tree_tag_clear(&fs_info->fs_roots_radix,
  					(unsigned long)root->root_key.objectid,
  					BTRFS_ROOT_TRANS_TAG);
a4abeea41   Josef Bacik   Btrfs: kill trans...
1188
  			spin_unlock(&fs_info->fs_roots_radix_lock);
31153d812   Yan Zheng   Btrfs: Add a leaf...
1189

e02119d5a   Chris Mason   Btrfs: Add a writ...
1190
  			btrfs_free_log(trans, root);
5d4f98a28   Yan Zheng   Btrfs: Mixed back...
1191
  			btrfs_update_reloc_root(trans, root);
d68fc57b7   Yan, Zheng   Btrfs: Metadata r...
1192
  			btrfs_orphan_commit_root(trans, root);
bcc63abbf   Yan   Btrfs: implement ...
1193

82d5902d9   Li Zefan   Btrfs: Support re...
1194
  			btrfs_save_ino_cache(root, trans);
f1ebcc74d   Liu Bo   Btrfs: fix tree c...
1195
  			/* see comments in should_cow_block() */
27cdeb709   Miao Xie   Btrfs: use bitfie...
1196
  			clear_bit(BTRFS_ROOT_FORCE_COW, &root->state);
c7548af69   Chris Mason   Btrfs: convert sm...
1197
  			smp_mb__after_atomic();
f1ebcc74d   Liu Bo   Btrfs: fix tree c...
1198

978d910d3   Yan Zheng   Btrfs: always upd...
1199
  			if (root->commit_root != root->node) {
9e351cc86   Josef Bacik   Btrfs: remove tra...
1200
1201
  				list_add_tail(&root->dirty_list,
  					&trans->transaction->switch_commits);
978d910d3   Yan Zheng   Btrfs: always upd...
1202
1203
1204
  				btrfs_set_root_node(&root->root_item,
  						    root->node);
  			}
5d4f98a28   Yan Zheng   Btrfs: Mixed back...
1205

5d4f98a28   Yan Zheng   Btrfs: Mixed back...
1206
  			err = btrfs_update_root(trans, fs_info->tree_root,
0f7d52f44   Chris Mason   Btrfs: groundwork...
1207
1208
  						&root->root_key,
  						&root->root_item);
a4abeea41   Josef Bacik   Btrfs: kill trans...
1209
  			spin_lock(&fs_info->fs_roots_radix_lock);
54aa1f4df   Chris Mason   Btrfs: Audit call...
1210
1211
  			if (err)
  				break;
7174109c6   Qu Wenruo   btrfs: qgroup: Us...
1212
  			btrfs_qgroup_free_meta_all(root);
0f7d52f44   Chris Mason   Btrfs: groundwork...
1213
1214
  		}
  	}
a4abeea41   Josef Bacik   Btrfs: kill trans...
1215
  	spin_unlock(&fs_info->fs_roots_radix_lock);
54aa1f4df   Chris Mason   Btrfs: Audit call...
1216
  	return err;
0f7d52f44   Chris Mason   Btrfs: groundwork...
1217
  }
d352ac681   Chris Mason   Btrfs: add and im...
1218
  /*
de78b51a2   Eric Sandeen   btrfs: remove cac...
1219
1220
   * defrag a given btree.
   * Every leaf in the btree is read and defragged.
d352ac681   Chris Mason   Btrfs: add and im...
1221
   */
de78b51a2   Eric Sandeen   btrfs: remove cac...
1222
  int btrfs_defrag_root(struct btrfs_root *root)
e9d0b13b5   Chris Mason   Btrfs: Btree defr...
1223
1224
  {
  	struct btrfs_fs_info *info = root->fs_info;
e9d0b13b5   Chris Mason   Btrfs: Btree defr...
1225
  	struct btrfs_trans_handle *trans;
8929ecfa5   Yan, Zheng   Btrfs: Introduce ...
1226
  	int ret;
e9d0b13b5   Chris Mason   Btrfs: Btree defr...
1227

27cdeb709   Miao Xie   Btrfs: use bitfie...
1228
  	if (test_and_set_bit(BTRFS_ROOT_DEFRAG_RUNNING, &root->state))
e9d0b13b5   Chris Mason   Btrfs: Btree defr...
1229
  		return 0;
8929ecfa5   Yan, Zheng   Btrfs: Introduce ...
1230

6b80053d0   Chris Mason   Btrfs: Add back t...
1231
  	while (1) {
8929ecfa5   Yan, Zheng   Btrfs: Introduce ...
1232
1233
1234
  		trans = btrfs_start_transaction(root, 0);
  		if (IS_ERR(trans))
  			return PTR_ERR(trans);
de78b51a2   Eric Sandeen   btrfs: remove cac...
1235
  		ret = btrfs_defrag_leaves(trans, root);
8929ecfa5   Yan, Zheng   Btrfs: Introduce ...
1236

e9d0b13b5   Chris Mason   Btrfs: Btree defr...
1237
  		btrfs_end_transaction(trans, root);
b53d3f5db   Liu Bo   Btrfs: cleanup fo...
1238
  		btrfs_btree_balance_dirty(info->tree_root);
e9d0b13b5   Chris Mason   Btrfs: Btree defr...
1239
  		cond_resched();
ab8d0fc48   Jeff Mahoney   btrfs: convert pr...
1240
  		if (btrfs_fs_closing(info) || ret != -EAGAIN)
e9d0b13b5   Chris Mason   Btrfs: Btree defr...
1241
  			break;
210549ebe   David Sterba   btrfs: add cancel...
1242

ab8d0fc48   Jeff Mahoney   btrfs: convert pr...
1243
1244
  		if (btrfs_defrag_cancelled(info)) {
  			btrfs_debug(info, "defrag_root cancelled");
210549ebe   David Sterba   btrfs: add cancel...
1245
1246
1247
  			ret = -EAGAIN;
  			break;
  		}
e9d0b13b5   Chris Mason   Btrfs: Btree defr...
1248
  	}
27cdeb709   Miao Xie   Btrfs: use bitfie...
1249
  	clear_bit(BTRFS_ROOT_DEFRAG_RUNNING, &root->state);
8929ecfa5   Yan, Zheng   Btrfs: Introduce ...
1250
  	return ret;
e9d0b13b5   Chris Mason   Btrfs: Btree defr...
1251
  }
d352ac681   Chris Mason   Btrfs: add and im...
1252
  /*
6426c7ad6   Qu Wenruo   btrfs: qgroup: Fi...
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
   * Do all special snapshot related qgroup dirty hack.
   *
   * Will do all needed qgroup inherit and dirty hack like switch commit
   * roots inside one transaction and write all btree into disk, to make
   * qgroup works.
   */
  static int qgroup_account_snapshot(struct btrfs_trans_handle *trans,
  				   struct btrfs_root *src,
  				   struct btrfs_root *parent,
  				   struct btrfs_qgroup_inherit *inherit,
  				   u64 dst_objectid)
  {
  	struct btrfs_fs_info *fs_info = src->fs_info;
  	int ret;
  
  	/*
  	 * Save some performance in the case that qgroups are not
  	 * enabled. If this check races with the ioctl, rescan will
  	 * kick in anyway.
  	 */
  	mutex_lock(&fs_info->qgroup_ioctl_lock);
afcdd129e   Josef Bacik   Btrfs: add a flag...
1274
  	if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags)) {
6426c7ad6   Qu Wenruo   btrfs: qgroup: Fi...
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
  		mutex_unlock(&fs_info->qgroup_ioctl_lock);
  		return 0;
  	}
  	mutex_unlock(&fs_info->qgroup_ioctl_lock);
  
  	/*
  	 * We are going to commit transaction, see btrfs_commit_transaction()
  	 * comment for reason locking tree_log_mutex
  	 */
  	mutex_lock(&fs_info->tree_log_mutex);
  
  	ret = commit_fs_roots(trans, src);
  	if (ret)
  		goto out;
  	ret = btrfs_qgroup_prepare_account_extents(trans, fs_info);
  	if (ret < 0)
  		goto out;
  	ret = btrfs_qgroup_account_extents(trans, fs_info);
  	if (ret < 0)
  		goto out;
  
  	/* Now qgroup are all updated, we can inherit it to new qgroups */
  	ret = btrfs_qgroup_inherit(trans, fs_info,
  				   src->root_key.objectid, dst_objectid,
  				   inherit);
  	if (ret < 0)
  		goto out;
  
  	/*
  	 * Now we do a simplified commit transaction, which will:
  	 * 1) commit all subvolume and extent tree
  	 *    To ensure all subvolume and extent tree have a valid
  	 *    commit_root to accounting later insert_dir_item()
  	 * 2) write all btree blocks onto disk
  	 *    This is to make sure later btree modification will be cowed
  	 *    Or commit_root can be populated and cause wrong qgroup numbers
  	 * In this simplified commit, we don't really care about other trees
  	 * like chunk and root tree, as they won't affect qgroup.
  	 * And we don't write super to avoid half committed status.
  	 */
  	ret = commit_cowonly_roots(trans, src);
  	if (ret)
  		goto out;
  	switch_commit_roots(trans->transaction, fs_info);
  	ret = btrfs_write_and_wait_transaction(trans, src);
  	if (ret)
f7af3934c   David Sterba   btrfs: use new er...
1321
  		btrfs_handle_fs_error(fs_info, ret,
6426c7ad6   Qu Wenruo   btrfs: qgroup: Fi...
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
  			"Error while writing out transaction for qgroup");
  
  out:
  	mutex_unlock(&fs_info->tree_log_mutex);
  
  	/*
  	 * Force parent root to be updated, as we recorded it before so its
  	 * last_trans == cur_transid.
  	 * Or it won't be committed again onto disk after later
  	 * insert_dir_item()
  	 */
  	if (!ret)
  		record_root_in_trans(trans, parent, 1);
  	return ret;
  }
  
  /*
d352ac681   Chris Mason   Btrfs: add and im...
1339
   * new snapshots need to be created at a very specific time in the
aec8030a8   Miao Xie   Btrfs: fix wrong ...
1340
1341
1342
1343
1344
1345
   * transaction commit.  This does the actual creation.
   *
   * Note:
   * If the error which may affect the commitment of the current transaction
   * happens, we should return the error number. If the error which just affect
   * the creation of the pending snapshots, just return 0.
d352ac681   Chris Mason   Btrfs: add and im...
1346
   */
80b6794d1   Chris Mason   Btrfs: Lower stac...
1347
  static noinline int create_pending_snapshot(struct btrfs_trans_handle *trans,
3063d29f2   Chris Mason   Btrfs: Move snaps...
1348
1349
1350
1351
  				   struct btrfs_fs_info *fs_info,
  				   struct btrfs_pending_snapshot *pending)
  {
  	struct btrfs_key key;
80b6794d1   Chris Mason   Btrfs: Lower stac...
1352
  	struct btrfs_root_item *new_root_item;
3063d29f2   Chris Mason   Btrfs: Move snaps...
1353
1354
  	struct btrfs_root *tree_root = fs_info->tree_root;
  	struct btrfs_root *root = pending->root;
6bdb72ded   Sage Weil   Btrfs: create sna...
1355
  	struct btrfs_root *parent_root;
98c9942ac   Liu Bo   Btrfs: fix misuse...
1356
  	struct btrfs_block_rsv *rsv;
6bdb72ded   Sage Weil   Btrfs: create sna...
1357
  	struct inode *parent_inode;
42874b3db   Miao Xie   Btrfs: fix the sn...
1358
1359
  	struct btrfs_path *path;
  	struct btrfs_dir_item *dir_item;
a22285a6a   Yan, Zheng   Btrfs: Integrate ...
1360
  	struct dentry *dentry;
3063d29f2   Chris Mason   Btrfs: Move snaps...
1361
  	struct extent_buffer *tmp;
925baeddc   Chris Mason   Btrfs: Start btre...
1362
  	struct extent_buffer *old;
04b285f35   Deepa Dinamani   btrfs: Replace CU...
1363
  	struct timespec cur_time;
aec8030a8   Miao Xie   Btrfs: fix wrong ...
1364
  	int ret = 0;
d68fc57b7   Yan, Zheng   Btrfs: Metadata r...
1365
  	u64 to_reserve = 0;
6bdb72ded   Sage Weil   Btrfs: create sna...
1366
  	u64 index = 0;
a22285a6a   Yan, Zheng   Btrfs: Integrate ...
1367
  	u64 objectid;
b83cc9693   Li Zefan   Btrfs: Add readon...
1368
  	u64 root_flags;
8ea05e3a4   Alexander Block   Btrfs: introduce ...
1369
  	uuid_le new_uuid;
3063d29f2   Chris Mason   Btrfs: Move snaps...
1370

8546b5705   David Sterba   btrfs: preallocat...
1371
1372
  	ASSERT(pending->path);
  	path = pending->path;
42874b3db   Miao Xie   Btrfs: fix the sn...
1373

b0c0ea633   David Sterba   btrfs: allocate r...
1374
1375
  	ASSERT(pending->root_item);
  	new_root_item = pending->root_item;
a22285a6a   Yan, Zheng   Btrfs: Integrate ...
1376

aec8030a8   Miao Xie   Btrfs: fix wrong ...
1377
1378
  	pending->error = btrfs_find_free_objectid(tree_root, &objectid);
  	if (pending->error)
6fa9700e7   Miao Xie   Btrfs: fix error ...
1379
  		goto no_free_objectid;
3063d29f2   Chris Mason   Btrfs: Move snaps...
1380

d67263354   Qu Wenruo   btrfs: qgroup: Ma...
1381
1382
1383
1384
1385
  	/*
  	 * Make qgroup to skip current new snapshot's qgroupid, as it is
  	 * accounted by later btrfs_qgroup_inherit().
  	 */
  	btrfs_set_skip_qgroup(trans, objectid);
147d256e0   Zhaolei   btrfs: Remove unn...
1386
  	btrfs_reloc_pre_snapshot(pending, &to_reserve);
d68fc57b7   Yan, Zheng   Btrfs: Metadata r...
1387
1388
  
  	if (to_reserve > 0) {
aec8030a8   Miao Xie   Btrfs: fix wrong ...
1389
1390
1391
1392
1393
  		pending->error = btrfs_block_rsv_add(root,
  						     &pending->block_rsv,
  						     to_reserve,
  						     BTRFS_RESERVE_NO_FLUSH);
  		if (pending->error)
d67263354   Qu Wenruo   btrfs: qgroup: Ma...
1394
  			goto clear_skip_qgroup;
d68fc57b7   Yan, Zheng   Btrfs: Metadata r...
1395
  	}
3063d29f2   Chris Mason   Btrfs: Move snaps...
1396
  	key.objectid = objectid;
a22285a6a   Yan, Zheng   Btrfs: Integrate ...
1397
1398
  	key.offset = (u64)-1;
  	key.type = BTRFS_ROOT_ITEM_KEY;
3063d29f2   Chris Mason   Btrfs: Move snaps...
1399

6fa9700e7   Miao Xie   Btrfs: fix error ...
1400
  	rsv = trans->block_rsv;
a22285a6a   Yan, Zheng   Btrfs: Integrate ...
1401
  	trans->block_rsv = &pending->block_rsv;
2382c5cc7   Liu Bo   Btrfs: use reserv...
1402
  	trans->bytes_reserved = trans->block_rsv->reserved;
88d3a5aaf   Josef Bacik   Btrfs: add transa...
1403
1404
1405
  	trace_btrfs_space_reservation(root->fs_info, "transaction",
  				      trans->transid,
  				      trans->bytes_reserved, 1);
a22285a6a   Yan, Zheng   Btrfs: Integrate ...
1406
  	dentry = pending->dentry;
e9662f701   Miao Xie   Btrfs: remove unn...
1407
  	parent_inode = pending->dir;
a22285a6a   Yan, Zheng   Btrfs: Integrate ...
1408
  	parent_root = BTRFS_I(parent_inode)->root;
6426c7ad6   Qu Wenruo   btrfs: qgroup: Fi...
1409
  	record_root_in_trans(trans, parent_root, 0);
a22285a6a   Yan, Zheng   Btrfs: Integrate ...
1410

c2050a454   Deepa Dinamani   fs: Replace curre...
1411
  	cur_time = current_time(parent_inode);
04b285f35   Deepa Dinamani   btrfs: Replace CU...
1412

3063d29f2   Chris Mason   Btrfs: Move snaps...
1413
1414
1415
  	/*
  	 * insert the directory item
  	 */
3de4586c5   Chris Mason   Btrfs: Allow subv...
1416
  	ret = btrfs_set_inode_index(parent_inode, &index);
49b25e054   Jeff Mahoney   btrfs: enhance tr...
1417
  	BUG_ON(ret); /* -ENOMEM */
42874b3db   Miao Xie   Btrfs: fix the sn...
1418
1419
1420
1421
1422
1423
1424
  
  	/* check if there is a file/dir which has the same name. */
  	dir_item = btrfs_lookup_dir_item(NULL, parent_root, path,
  					 btrfs_ino(parent_inode),
  					 dentry->d_name.name,
  					 dentry->d_name.len, 0);
  	if (dir_item != NULL && !IS_ERR(dir_item)) {
fe66a05a0   Chris Mason   Btrfs: improve er...
1425
  		pending->error = -EEXIST;
aec8030a8   Miao Xie   Btrfs: fix wrong ...
1426
  		goto dir_item_existed;
42874b3db   Miao Xie   Btrfs: fix the sn...
1427
1428
  	} else if (IS_ERR(dir_item)) {
  		ret = PTR_ERR(dir_item);
66642832f   Jeff Mahoney   btrfs: btrfs_abor...
1429
  		btrfs_abort_transaction(trans, ret);
8732d44f8   Miao Xie   Btrfs: fix the mi...
1430
  		goto fail;
79787eaab   Jeff Mahoney   btrfs: replace ma...
1431
  	}
42874b3db   Miao Xie   Btrfs: fix the sn...
1432
  	btrfs_release_path(path);
52c261799   Yan Zheng   Btrfs: update dir...
1433

e999376f0   Chris Mason   Btrfs: avoid dela...
1434
1435
1436
1437
1438
1439
1440
  	/*
  	 * pull in the delayed directory update
  	 * and the delayed inode item
  	 * otherwise we corrupt the FS during
  	 * snapshot
  	 */
  	ret = btrfs_run_delayed_items(trans, root);
8732d44f8   Miao Xie   Btrfs: fix the mi...
1441
  	if (ret) {	/* Transaction aborted */
66642832f   Jeff Mahoney   btrfs: btrfs_abor...
1442
  		btrfs_abort_transaction(trans, ret);
8732d44f8   Miao Xie   Btrfs: fix the mi...
1443
1444
  		goto fail;
  	}
e999376f0   Chris Mason   Btrfs: avoid dela...
1445

6426c7ad6   Qu Wenruo   btrfs: qgroup: Fi...
1446
  	record_root_in_trans(trans, root, 0);
6bdb72ded   Sage Weil   Btrfs: create sna...
1447
1448
  	btrfs_set_root_last_snapshot(&root->root_item, trans->transid);
  	memcpy(new_root_item, &root->root_item, sizeof(*new_root_item));
08fe4db17   Li Zefan   Btrfs: Fix uninit...
1449
  	btrfs_check_and_init_root_item(new_root_item);
6bdb72ded   Sage Weil   Btrfs: create sna...
1450

b83cc9693   Li Zefan   Btrfs: Add readon...
1451
1452
1453
1454
1455
1456
  	root_flags = btrfs_root_flags(new_root_item);
  	if (pending->readonly)
  		root_flags |= BTRFS_ROOT_SUBVOL_RDONLY;
  	else
  		root_flags &= ~BTRFS_ROOT_SUBVOL_RDONLY;
  	btrfs_set_root_flags(new_root_item, root_flags);
8ea05e3a4   Alexander Block   Btrfs: introduce ...
1457
1458
1459
1460
1461
1462
  	btrfs_set_root_generation_v2(new_root_item,
  			trans->transid);
  	uuid_le_gen(&new_uuid);
  	memcpy(new_root_item->uuid, new_uuid.b, BTRFS_UUID_SIZE);
  	memcpy(new_root_item->parent_uuid, root->root_item.uuid,
  			BTRFS_UUID_SIZE);
70023da27   Stefan Behrens   Btrfs: clear rece...
1463
1464
1465
1466
1467
1468
1469
1470
  	if (!(root_flags & BTRFS_ROOT_SUBVOL_RDONLY)) {
  		memset(new_root_item->received_uuid, 0,
  		       sizeof(new_root_item->received_uuid));
  		memset(&new_root_item->stime, 0, sizeof(new_root_item->stime));
  		memset(&new_root_item->rtime, 0, sizeof(new_root_item->rtime));
  		btrfs_set_root_stransid(new_root_item, 0);
  		btrfs_set_root_rtransid(new_root_item, 0);
  	}
3cae210fa   Qu Wenruo   btrfs: Cleanup fo...
1471
1472
  	btrfs_set_stack_timespec_sec(&new_root_item->otime, cur_time.tv_sec);
  	btrfs_set_stack_timespec_nsec(&new_root_item->otime, cur_time.tv_nsec);
8ea05e3a4   Alexander Block   Btrfs: introduce ...
1473
  	btrfs_set_root_otransid(new_root_item, trans->transid);
8ea05e3a4   Alexander Block   Btrfs: introduce ...
1474

6bdb72ded   Sage Weil   Btrfs: create sna...
1475
  	old = btrfs_lock_root_node(root);
49b25e054   Jeff Mahoney   btrfs: enhance tr...
1476
  	ret = btrfs_cow_block(trans, root, old, NULL, 0, &old);
79787eaab   Jeff Mahoney   btrfs: replace ma...
1477
1478
1479
  	if (ret) {
  		btrfs_tree_unlock(old);
  		free_extent_buffer(old);
66642832f   Jeff Mahoney   btrfs: btrfs_abor...
1480
  		btrfs_abort_transaction(trans, ret);
8732d44f8   Miao Xie   Btrfs: fix the mi...
1481
  		goto fail;
79787eaab   Jeff Mahoney   btrfs: replace ma...
1482
  	}
49b25e054   Jeff Mahoney   btrfs: enhance tr...
1483

6bdb72ded   Sage Weil   Btrfs: create sna...
1484
  	btrfs_set_lock_blocking(old);
49b25e054   Jeff Mahoney   btrfs: enhance tr...
1485
  	ret = btrfs_copy_root(trans, root, old, &tmp, objectid);
79787eaab   Jeff Mahoney   btrfs: replace ma...
1486
  	/* clean up in any case */
6bdb72ded   Sage Weil   Btrfs: create sna...
1487
1488
  	btrfs_tree_unlock(old);
  	free_extent_buffer(old);
8732d44f8   Miao Xie   Btrfs: fix the mi...
1489
  	if (ret) {
66642832f   Jeff Mahoney   btrfs: btrfs_abor...
1490
  		btrfs_abort_transaction(trans, ret);
8732d44f8   Miao Xie   Btrfs: fix the mi...
1491
1492
  		goto fail;
  	}
f1ebcc74d   Liu Bo   Btrfs: fix tree c...
1493
  	/* see comments in should_cow_block() */
27cdeb709   Miao Xie   Btrfs: use bitfie...
1494
  	set_bit(BTRFS_ROOT_FORCE_COW, &root->state);
f1ebcc74d   Liu Bo   Btrfs: fix tree c...
1495
  	smp_wmb();
6bdb72ded   Sage Weil   Btrfs: create sna...
1496
  	btrfs_set_root_node(new_root_item, tmp);
a22285a6a   Yan, Zheng   Btrfs: Integrate ...
1497
1498
1499
  	/* record when the snapshot was created in key.offset */
  	key.offset = trans->transid;
  	ret = btrfs_insert_root(trans, tree_root, &key, new_root_item);
6bdb72ded   Sage Weil   Btrfs: create sna...
1500
1501
  	btrfs_tree_unlock(tmp);
  	free_extent_buffer(tmp);
8732d44f8   Miao Xie   Btrfs: fix the mi...
1502
  	if (ret) {
66642832f   Jeff Mahoney   btrfs: btrfs_abor...
1503
  		btrfs_abort_transaction(trans, ret);
8732d44f8   Miao Xie   Btrfs: fix the mi...
1504
1505
  		goto fail;
  	}
6bdb72ded   Sage Weil   Btrfs: create sna...
1506

a22285a6a   Yan, Zheng   Btrfs: Integrate ...
1507
1508
1509
1510
  	/*
  	 * insert root back/forward references
  	 */
  	ret = btrfs_add_root_ref(trans, tree_root, objectid,
0660b5af3   Chris Mason   Btrfs: Add backre...
1511
  				 parent_root->root_key.objectid,
33345d015   Li Zefan   Btrfs: Always use...
1512
  				 btrfs_ino(parent_inode), index,
a22285a6a   Yan, Zheng   Btrfs: Integrate ...
1513
  				 dentry->d_name.name, dentry->d_name.len);
8732d44f8   Miao Xie   Btrfs: fix the mi...
1514
  	if (ret) {
66642832f   Jeff Mahoney   btrfs: btrfs_abor...
1515
  		btrfs_abort_transaction(trans, ret);
8732d44f8   Miao Xie   Btrfs: fix the mi...
1516
1517
  		goto fail;
  	}
0660b5af3   Chris Mason   Btrfs: Add backre...
1518

a22285a6a   Yan, Zheng   Btrfs: Integrate ...
1519
1520
  	key.offset = (u64)-1;
  	pending->snap = btrfs_read_fs_root_no_name(root->fs_info, &key);
79787eaab   Jeff Mahoney   btrfs: replace ma...
1521
1522
  	if (IS_ERR(pending->snap)) {
  		ret = PTR_ERR(pending->snap);
66642832f   Jeff Mahoney   btrfs: btrfs_abor...
1523
  		btrfs_abort_transaction(trans, ret);
8732d44f8   Miao Xie   Btrfs: fix the mi...
1524
  		goto fail;
79787eaab   Jeff Mahoney   btrfs: replace ma...
1525
  	}
d68fc57b7   Yan, Zheng   Btrfs: Metadata r...
1526

49b25e054   Jeff Mahoney   btrfs: enhance tr...
1527
  	ret = btrfs_reloc_post_snapshot(trans, pending);
8732d44f8   Miao Xie   Btrfs: fix the mi...
1528
  	if (ret) {
66642832f   Jeff Mahoney   btrfs: btrfs_abor...
1529
  		btrfs_abort_transaction(trans, ret);
8732d44f8   Miao Xie   Btrfs: fix the mi...
1530
1531
  		goto fail;
  	}
361048f58   Miao Xie   Btrfs: fix full b...
1532
1533
  
  	ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
8732d44f8   Miao Xie   Btrfs: fix the mi...
1534
  	if (ret) {
66642832f   Jeff Mahoney   btrfs: btrfs_abor...
1535
  		btrfs_abort_transaction(trans, ret);
8732d44f8   Miao Xie   Btrfs: fix the mi...
1536
1537
  		goto fail;
  	}
42874b3db   Miao Xie   Btrfs: fix the sn...
1538

6426c7ad6   Qu Wenruo   btrfs: qgroup: Fi...
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
  	/*
  	 * Do special qgroup accounting for snapshot, as we do some qgroup
  	 * snapshot hack to do fast snapshot.
  	 * To co-operate with that hack, we do hack again.
  	 * Or snapshot will be greatly slowed down by a subtree qgroup rescan
  	 */
  	ret = qgroup_account_snapshot(trans, root, parent_root,
  				      pending->inherit, objectid);
  	if (ret < 0)
  		goto fail;
42874b3db   Miao Xie   Btrfs: fix the sn...
1549
1550
1551
1552
1553
  	ret = btrfs_insert_dir_item(trans, parent_root,
  				    dentry->d_name.name, dentry->d_name.len,
  				    parent_inode, &key,
  				    BTRFS_FT_DIR, index);
  	/* We have check then name at the beginning, so it is impossible. */
9c52057c6   Chris Mason   Btrfs: fix hash o...
1554
  	BUG_ON(ret == -EEXIST || ret == -EOVERFLOW);
8732d44f8   Miao Xie   Btrfs: fix the mi...
1555
  	if (ret) {
66642832f   Jeff Mahoney   btrfs: btrfs_abor...
1556
  		btrfs_abort_transaction(trans, ret);
8732d44f8   Miao Xie   Btrfs: fix the mi...
1557
1558
  		goto fail;
  	}
42874b3db   Miao Xie   Btrfs: fix the sn...
1559
1560
1561
  
  	btrfs_i_size_write(parent_inode, parent_inode->i_size +
  					 dentry->d_name.len * 2);
04b285f35   Deepa Dinamani   btrfs: Replace CU...
1562
  	parent_inode->i_mtime = parent_inode->i_ctime =
c2050a454   Deepa Dinamani   fs: Replace curre...
1563
  		current_time(parent_inode);
be6aef604   Josef Bacik   Btrfs: Use btrfs_...
1564
  	ret = btrfs_update_inode_fallback(trans, parent_root, parent_inode);
dd5f9615f   Stefan Behrens   Btrfs: maintain s...
1565
  	if (ret) {
66642832f   Jeff Mahoney   btrfs: btrfs_abor...
1566
  		btrfs_abort_transaction(trans, ret);
dd5f9615f   Stefan Behrens   Btrfs: maintain s...
1567
1568
1569
1570
1571
  		goto fail;
  	}
  	ret = btrfs_uuid_tree_add(trans, fs_info->uuid_root, new_uuid.b,
  				  BTRFS_UUID_KEY_SUBVOL, objectid);
  	if (ret) {
66642832f   Jeff Mahoney   btrfs: btrfs_abor...
1572
  		btrfs_abort_transaction(trans, ret);
dd5f9615f   Stefan Behrens   Btrfs: maintain s...
1573
1574
1575
1576
1577
1578
1579
1580
  		goto fail;
  	}
  	if (!btrfs_is_empty_uuid(new_root_item->received_uuid)) {
  		ret = btrfs_uuid_tree_add(trans, fs_info->uuid_root,
  					  new_root_item->received_uuid,
  					  BTRFS_UUID_KEY_RECEIVED_SUBVOL,
  					  objectid);
  		if (ret && ret != -EEXIST) {
66642832f   Jeff Mahoney   btrfs: btrfs_abor...
1581
  			btrfs_abort_transaction(trans, ret);
dd5f9615f   Stefan Behrens   Btrfs: maintain s...
1582
1583
1584
  			goto fail;
  		}
  	}
d67263354   Qu Wenruo   btrfs: qgroup: Ma...
1585
1586
1587
  
  	ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
  	if (ret) {
66642832f   Jeff Mahoney   btrfs: btrfs_abor...
1588
  		btrfs_abort_transaction(trans, ret);
d67263354   Qu Wenruo   btrfs: qgroup: Ma...
1589
1590
  		goto fail;
  	}
3063d29f2   Chris Mason   Btrfs: Move snaps...
1591
  fail:
aec8030a8   Miao Xie   Btrfs: fix wrong ...
1592
1593
  	pending->error = ret;
  dir_item_existed:
98c9942ac   Liu Bo   Btrfs: fix misuse...
1594
  	trans->block_rsv = rsv;
2382c5cc7   Liu Bo   Btrfs: use reserv...
1595
  	trans->bytes_reserved = 0;
d67263354   Qu Wenruo   btrfs: qgroup: Ma...
1596
1597
  clear_skip_qgroup:
  	btrfs_clear_skip_qgroup(trans);
6fa9700e7   Miao Xie   Btrfs: fix error ...
1598
1599
  no_free_objectid:
  	kfree(new_root_item);
b0c0ea633   David Sterba   btrfs: allocate r...
1600
  	pending->root_item = NULL;
42874b3db   Miao Xie   Btrfs: fix the sn...
1601
  	btrfs_free_path(path);
8546b5705   David Sterba   btrfs: preallocat...
1602
  	pending->path = NULL;
49b25e054   Jeff Mahoney   btrfs: enhance tr...
1603
  	return ret;
3063d29f2   Chris Mason   Btrfs: Move snaps...
1604
  }
d352ac681   Chris Mason   Btrfs: add and im...
1605
1606
1607
  /*
   * create all the snapshots we've scheduled for creation
   */
80b6794d1   Chris Mason   Btrfs: Lower stac...
1608
1609
  static noinline int create_pending_snapshots(struct btrfs_trans_handle *trans,
  					     struct btrfs_fs_info *fs_info)
3063d29f2   Chris Mason   Btrfs: Move snaps...
1610
  {
aec8030a8   Miao Xie   Btrfs: fix wrong ...
1611
  	struct btrfs_pending_snapshot *pending, *next;
3063d29f2   Chris Mason   Btrfs: Move snaps...
1612
  	struct list_head *head = &trans->transaction->pending_snapshots;
aec8030a8   Miao Xie   Btrfs: fix wrong ...
1613
  	int ret = 0;
3de4586c5   Chris Mason   Btrfs: Allow subv...
1614

aec8030a8   Miao Xie   Btrfs: fix wrong ...
1615
1616
1617
1618
1619
1620
1621
  	list_for_each_entry_safe(pending, next, head, list) {
  		list_del(&pending->list);
  		ret = create_pending_snapshot(trans, fs_info, pending);
  		if (ret)
  			break;
  	}
  	return ret;
3de4586c5   Chris Mason   Btrfs: Allow subv...
1622
  }
5d4f98a28   Yan Zheng   Btrfs: Mixed back...
1623
1624
1625
1626
  static void update_super_roots(struct btrfs_root *root)
  {
  	struct btrfs_root_item *root_item;
  	struct btrfs_super_block *super;
6c41761fc   David Sterba   btrfs: separate s...
1627
  	super = root->fs_info->super_copy;
5d4f98a28   Yan Zheng   Btrfs: Mixed back...
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
  
  	root_item = &root->fs_info->chunk_root->root_item;
  	super->chunk_root = root_item->bytenr;
  	super->chunk_root_generation = root_item->generation;
  	super->chunk_root_level = root_item->level;
  
  	root_item = &root->fs_info->tree_root->root_item;
  	super->root = root_item->bytenr;
  	super->generation = root_item->generation;
  	super->root_level = root_item->level;
3cdde2240   Jeff Mahoney   btrfs: btrfs_test...
1638
  	if (btrfs_test_opt(root->fs_info, SPACE_CACHE))
0af3d00ba   Josef Bacik   Btrfs: create spe...
1639
  		super->cache_generation = root_item->generation;
afcdd129e   Josef Bacik   Btrfs: add a flag...
1640
  	if (test_bit(BTRFS_FS_UPDATE_UUID_TREE_GEN, &root->fs_info->flags))
70f801754   Stefan Behrens   Btrfs: check UUID...
1641
  		super->uuid_tree_generation = root_item->generation;
5d4f98a28   Yan Zheng   Btrfs: Mixed back...
1642
  }
f36f3042e   Chris Mason   Btrfs: be more po...
1643
1644
  int btrfs_transaction_in_commit(struct btrfs_fs_info *info)
  {
4a9d8bdee   Miao Xie   Btrfs: make the s...
1645
  	struct btrfs_transaction *trans;
f36f3042e   Chris Mason   Btrfs: be more po...
1646
  	int ret = 0;
4a9d8bdee   Miao Xie   Btrfs: make the s...
1647

a4abeea41   Josef Bacik   Btrfs: kill trans...
1648
  	spin_lock(&info->trans_lock);
4a9d8bdee   Miao Xie   Btrfs: make the s...
1649
1650
1651
  	trans = info->running_transaction;
  	if (trans)
  		ret = (trans->state >= TRANS_STATE_COMMIT_START);
a4abeea41   Josef Bacik   Btrfs: kill trans...
1652
  	spin_unlock(&info->trans_lock);
f36f3042e   Chris Mason   Btrfs: be more po...
1653
1654
  	return ret;
  }
8929ecfa5   Yan, Zheng   Btrfs: Introduce ...
1655
1656
  int btrfs_transaction_blocked(struct btrfs_fs_info *info)
  {
4a9d8bdee   Miao Xie   Btrfs: make the s...
1657
  	struct btrfs_transaction *trans;
8929ecfa5   Yan, Zheng   Btrfs: Introduce ...
1658
  	int ret = 0;
4a9d8bdee   Miao Xie   Btrfs: make the s...
1659

a4abeea41   Josef Bacik   Btrfs: kill trans...
1660
  	spin_lock(&info->trans_lock);
4a9d8bdee   Miao Xie   Btrfs: make the s...
1661
1662
1663
  	trans = info->running_transaction;
  	if (trans)
  		ret = is_transaction_blocked(trans);
a4abeea41   Josef Bacik   Btrfs: kill trans...
1664
  	spin_unlock(&info->trans_lock);
8929ecfa5   Yan, Zheng   Btrfs: Introduce ...
1665
1666
  	return ret;
  }
bb9c12c94   Sage Weil   Btrfs: async tran...
1667
1668
1669
1670
1671
1672
1673
  /*
   * wait for the current transaction commit to start and block subsequent
   * transaction joins
   */
  static void wait_current_trans_commit_start(struct btrfs_root *root,
  					    struct btrfs_transaction *trans)
  {
4a9d8bdee   Miao Xie   Btrfs: make the s...
1674
  	wait_event(root->fs_info->transaction_blocked_wait,
501407aab   Josef Bacik   Btrfs: stop waiti...
1675
1676
  		   trans->state >= TRANS_STATE_COMMIT_START ||
  		   trans->aborted);
bb9c12c94   Sage Weil   Btrfs: async tran...
1677
1678
1679
1680
1681
1682
1683
1684
1685
  }
  
  /*
   * wait for the current transaction to start and then become unblocked.
   * caller holds ref.
   */
  static void wait_current_trans_commit_start_and_unblock(struct btrfs_root *root,
  					 struct btrfs_transaction *trans)
  {
72d63ed64   Li Zefan   Btrfs: use wait_e...
1686
  	wait_event(root->fs_info->transaction_wait,
501407aab   Josef Bacik   Btrfs: stop waiti...
1687
1688
  		   trans->state >= TRANS_STATE_UNBLOCKED ||
  		   trans->aborted);
bb9c12c94   Sage Weil   Btrfs: async tran...
1689
1690
1691
1692
1693
1694
1695
1696
1697
  }
  
  /*
   * commit transactions asynchronously. once btrfs_commit_transaction_async
   * returns, any subsequent transaction will not be allowed to join.
   */
  struct btrfs_async_commit {
  	struct btrfs_trans_handle *newtrans;
  	struct btrfs_root *root;
7892b5afe   Miao Xie   Btrfs: use common...
1698
  	struct work_struct work;
bb9c12c94   Sage Weil   Btrfs: async tran...
1699
1700
1701
1702
1703
  };
  
  static void do_async_commit(struct work_struct *work)
  {
  	struct btrfs_async_commit *ac =
7892b5afe   Miao Xie   Btrfs: use common...
1704
  		container_of(work, struct btrfs_async_commit, work);
bb9c12c94   Sage Weil   Btrfs: async tran...
1705

6fc4e3548   Sage Weil   Btrfs: pass lockd...
1706
1707
1708
1709
  	/*
  	 * We've got freeze protection passed with the transaction.
  	 * Tell lockdep about it.
  	 */
b1a06a4b5   Liu Bo   Btrfs: fix lockde...
1710
  	if (ac->newtrans->type & __TRANS_FREEZABLE)
bee9182d9   Oleg Nesterov   introduce __sb_wr...
1711
  		__sb_writers_acquired(ac->root->fs_info->sb, SB_FREEZE_FS);
6fc4e3548   Sage Weil   Btrfs: pass lockd...
1712

e209db7ac   Sage Weil   Btrfs: set journa...
1713
  	current->journal_info = ac->newtrans;
bb9c12c94   Sage Weil   Btrfs: async tran...
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
  	btrfs_commit_transaction(ac->newtrans, ac->root);
  	kfree(ac);
  }
  
  int btrfs_commit_transaction_async(struct btrfs_trans_handle *trans,
  				   struct btrfs_root *root,
  				   int wait_for_unblock)
  {
  	struct btrfs_async_commit *ac;
  	struct btrfs_transaction *cur_trans;
  
  	ac = kmalloc(sizeof(*ac), GFP_NOFS);
db5b493ac   Tsutomu Itoh   Btrfs: cleanup so...
1726
1727
  	if (!ac)
  		return -ENOMEM;
bb9c12c94   Sage Weil   Btrfs: async tran...
1728

7892b5afe   Miao Xie   Btrfs: use common...
1729
  	INIT_WORK(&ac->work, do_async_commit);
bb9c12c94   Sage Weil   Btrfs: async tran...
1730
  	ac->root = root;
7a7eaa40a   Josef Bacik   Btrfs: take away ...
1731
  	ac->newtrans = btrfs_join_transaction(root);
3612b4959   Tsutomu Itoh   btrfs: fix return...
1732
1733
1734
1735
1736
  	if (IS_ERR(ac->newtrans)) {
  		int err = PTR_ERR(ac->newtrans);
  		kfree(ac);
  		return err;
  	}
bb9c12c94   Sage Weil   Btrfs: async tran...
1737
1738
  
  	/* take transaction reference */
bb9c12c94   Sage Weil   Btrfs: async tran...
1739
  	cur_trans = trans->transaction;
13c5a93e7   Josef Bacik   Btrfs: avoid taki...
1740
  	atomic_inc(&cur_trans->use_count);
bb9c12c94   Sage Weil   Btrfs: async tran...
1741
1742
  
  	btrfs_end_transaction(trans, root);
6fc4e3548   Sage Weil   Btrfs: pass lockd...
1743
1744
1745
1746
1747
  
  	/*
  	 * Tell lockdep we've released the freeze rwsem, since the
  	 * async commit thread will be the one to unlock it.
  	 */
b1a06a4b5   Liu Bo   Btrfs: fix lockde...
1748
  	if (ac->newtrans->type & __TRANS_FREEZABLE)
bee9182d9   Oleg Nesterov   introduce __sb_wr...
1749
  		__sb_writers_release(root->fs_info->sb, SB_FREEZE_FS);
6fc4e3548   Sage Weil   Btrfs: pass lockd...
1750

7892b5afe   Miao Xie   Btrfs: use common...
1751
  	schedule_work(&ac->work);
bb9c12c94   Sage Weil   Btrfs: async tran...
1752
1753
  
  	/* wait for transaction to start and unblock */
bb9c12c94   Sage Weil   Btrfs: async tran...
1754
1755
1756
1757
  	if (wait_for_unblock)
  		wait_current_trans_commit_start_and_unblock(root, cur_trans);
  	else
  		wait_current_trans_commit_start(root, cur_trans);
bb9c12c94   Sage Weil   Btrfs: async tran...
1758

38e880540   Sage Weil   Btrfs: clear curr...
1759
1760
  	if (current->journal_info == trans)
  		current->journal_info = NULL;
724e2315d   Josef Bacik   Btrfs: fix two us...
1761
  	btrfs_put_transaction(cur_trans);
bb9c12c94   Sage Weil   Btrfs: async tran...
1762
1763
  	return 0;
  }
49b25e054   Jeff Mahoney   btrfs: enhance tr...
1764
1765
  
  static void cleanup_transaction(struct btrfs_trans_handle *trans,
7b8b92af5   Josef Bacik   Btrfs: abort the ...
1766
  				struct btrfs_root *root, int err)
49b25e054   Jeff Mahoney   btrfs: enhance tr...
1767
1768
  {
  	struct btrfs_transaction *cur_trans = trans->transaction;
f094ac32a   Liu Bo   Btrfs: fix NULL p...
1769
  	DEFINE_WAIT(wait);
49b25e054   Jeff Mahoney   btrfs: enhance tr...
1770
1771
  
  	WARN_ON(trans->use_count > 1);
66642832f   Jeff Mahoney   btrfs: btrfs_abor...
1772
  	btrfs_abort_transaction(trans, err);
7b8b92af5   Josef Bacik   Btrfs: abort the ...
1773

49b25e054   Jeff Mahoney   btrfs: enhance tr...
1774
  	spin_lock(&root->fs_info->trans_lock);
66b6135b7   Liu Bo   Btrfs: avoid dead...
1775

25d8c284c   Miao Xie   Btrfs: remove the...
1776
1777
1778
1779
1780
1781
  	/*
  	 * If the transaction is removed from the list, it means this
  	 * transaction has been committed successfully, so it is impossible
  	 * to call the cleanup function.
  	 */
  	BUG_ON(list_empty(&cur_trans->list));
66b6135b7   Liu Bo   Btrfs: avoid dead...
1782

49b25e054   Jeff Mahoney   btrfs: enhance tr...
1783
  	list_del_init(&cur_trans->list);
d7096fc3e   Josef Bacik   Btrfs: wake up tr...
1784
  	if (cur_trans == root->fs_info->running_transaction) {
4a9d8bdee   Miao Xie   Btrfs: make the s...
1785
  		cur_trans->state = TRANS_STATE_COMMIT_DOING;
f094ac32a   Liu Bo   Btrfs: fix NULL p...
1786
1787
1788
1789
1790
  		spin_unlock(&root->fs_info->trans_lock);
  		wait_event(cur_trans->writer_wait,
  			   atomic_read(&cur_trans->num_writers) == 1);
  
  		spin_lock(&root->fs_info->trans_lock);
d7096fc3e   Josef Bacik   Btrfs: wake up tr...
1791
  	}
49b25e054   Jeff Mahoney   btrfs: enhance tr...
1792
1793
1794
  	spin_unlock(&root->fs_info->trans_lock);
  
  	btrfs_cleanup_one_transaction(trans->transaction, root);
4a9d8bdee   Miao Xie   Btrfs: make the s...
1795
1796
1797
1798
  	spin_lock(&root->fs_info->trans_lock);
  	if (cur_trans == root->fs_info->running_transaction)
  		root->fs_info->running_transaction = NULL;
  	spin_unlock(&root->fs_info->trans_lock);
e0228285a   Josef Bacik   Btrfs: reset intw...
1799
1800
  	if (trans->type & __TRANS_FREEZABLE)
  		sb_end_intwrite(root->fs_info->sb);
724e2315d   Josef Bacik   Btrfs: fix two us...
1801
1802
  	btrfs_put_transaction(cur_trans);
  	btrfs_put_transaction(cur_trans);
49b25e054   Jeff Mahoney   btrfs: enhance tr...
1803
1804
  
  	trace_btrfs_transaction_commit(root);
49b25e054   Jeff Mahoney   btrfs: enhance tr...
1805
1806
  	if (current->journal_info == trans)
  		current->journal_info = NULL;
c0af8f0b1   Wang Shilong   Btrfs: cancel scr...
1807
  	btrfs_scrub_cancel(root->fs_info);
49b25e054   Jeff Mahoney   btrfs: enhance tr...
1808
1809
1810
  
  	kmem_cache_free(btrfs_trans_handle_cachep, trans);
  }
824366177   Miao Xie   Btrfs: don't flus...
1811
1812
  static inline int btrfs_start_delalloc_flush(struct btrfs_fs_info *fs_info)
  {
3cdde2240   Jeff Mahoney   btrfs: btrfs_test...
1813
  	if (btrfs_test_opt(fs_info, FLUSHONCOMMIT))
6c255e67c   Miao Xie   Btrfs: don't flus...
1814
  		return btrfs_start_delalloc_roots(fs_info, 1, -1);
824366177   Miao Xie   Btrfs: don't flus...
1815
1816
1817
1818
1819
  	return 0;
  }
  
  static inline void btrfs_wait_delalloc_flush(struct btrfs_fs_info *fs_info)
  {
3cdde2240   Jeff Mahoney   btrfs: btrfs_test...
1820
  	if (btrfs_test_opt(fs_info, FLUSHONCOMMIT))
578def7c5   Filipe Manana   Btrfs: don't wait...
1821
  		btrfs_wait_ordered_roots(fs_info, -1, 0, (u64)-1);
824366177   Miao Xie   Btrfs: don't flus...
1822
  }
50d9aa99b   Josef Bacik   Btrfs: make sure ...
1823
  static inline void
161c3549b   Josef Bacik   Btrfs: change how...
1824
  btrfs_wait_pending_ordered(struct btrfs_transaction *cur_trans)
50d9aa99b   Josef Bacik   Btrfs: make sure ...
1825
  {
161c3549b   Josef Bacik   Btrfs: change how...
1826
1827
  	wait_event(cur_trans->pending_wait,
  		   atomic_read(&cur_trans->pending_ordered) == 0);
50d9aa99b   Josef Bacik   Btrfs: make sure ...
1828
  }
79154b1b5   Chris Mason   Btrfs: transactio...
1829
1830
1831
  int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
  			     struct btrfs_root *root)
  {
49b25e054   Jeff Mahoney   btrfs: enhance tr...
1832
  	struct btrfs_transaction *cur_trans = trans->transaction;
8fd17795b   Chris Mason   Btrfs: early fsyn...
1833
  	struct btrfs_transaction *prev_trans = NULL;
25287e0a1   Miao Xie   Btrfs: make order...
1834
  	int ret;
79154b1b5   Chris Mason   Btrfs: transactio...
1835

8d25a086e   Miao Xie   Btrfs: Add ACCESS...
1836
1837
  	/* Stop the commit early if ->aborted is set */
  	if (unlikely(ACCESS_ONCE(cur_trans->aborted))) {
25287e0a1   Miao Xie   Btrfs: make order...
1838
  		ret = cur_trans->aborted;
e4a2bcaca   Josef Bacik   Btrfs: if we aren...
1839
1840
  		btrfs_end_transaction(trans, root);
  		return ret;
25287e0a1   Miao Xie   Btrfs: make order...
1841
  	}
49b25e054   Jeff Mahoney   btrfs: enhance tr...
1842

56bec294d   Chris Mason   Btrfs: do extent ...
1843
1844
1845
1846
  	/* make a pass through all the delayed refs we have so far
  	 * any runnings procs may add more while we are here
  	 */
  	ret = btrfs_run_delayed_refs(trans, root, 0);
e4a2bcaca   Josef Bacik   Btrfs: if we aren...
1847
1848
1849
1850
  	if (ret) {
  		btrfs_end_transaction(trans, root);
  		return ret;
  	}
56bec294d   Chris Mason   Btrfs: do extent ...
1851

0e7211069   Josef Bacik   Btrfs: change how...
1852
1853
  	btrfs_trans_release_metadata(trans, root);
  	trans->block_rsv = NULL;
b7ec40d78   Chris Mason   Btrfs: reduce sta...
1854
  	cur_trans = trans->transaction;
49b25e054   Jeff Mahoney   btrfs: enhance tr...
1855

56bec294d   Chris Mason   Btrfs: do extent ...
1856
1857
1858
1859
  	/*
  	 * set the flushing flag so procs in this transaction have to
  	 * start sending their work down.
  	 */
b7ec40d78   Chris Mason   Btrfs: reduce sta...
1860
  	cur_trans->delayed_refs.flushing = 1;
1be41b78b   Josef Bacik   Btrfs: fix transa...
1861
  	smp_wmb();
56bec294d   Chris Mason   Btrfs: do extent ...
1862

ea658badc   Josef Bacik   Btrfs: delay bloc...
1863
1864
  	if (!list_empty(&trans->new_bgs))
  		btrfs_create_pending_block_groups(trans, root);
c3e69d58e   Chris Mason   Btrfs: process th...
1865
  	ret = btrfs_run_delayed_refs(trans, root, 0);
e4a2bcaca   Josef Bacik   Btrfs: if we aren...
1866
1867
1868
1869
  	if (ret) {
  		btrfs_end_transaction(trans, root);
  		return ret;
  	}
56bec294d   Chris Mason   Btrfs: do extent ...
1870

3204d33cd   Josef Bacik   Btrfs: add a flag...
1871
  	if (!test_bit(BTRFS_TRANS_DIRTY_BG_RUN, &cur_trans->flags)) {
1bbc621ef   Chris Mason   Btrfs: allow bloc...
1872
1873
1874
1875
1876
1877
1878
1879
  		int run_it = 0;
  
  		/* this mutex is also taken before trying to set
  		 * block groups readonly.  We need to make sure
  		 * that nobody has set a block group readonly
  		 * after a extents from that block group have been
  		 * allocated for cache files.  btrfs_set_block_group_ro
  		 * will wait for the transaction to commit if it
3204d33cd   Josef Bacik   Btrfs: add a flag...
1880
  		 * finds BTRFS_TRANS_DIRTY_BG_RUN set.
1bbc621ef   Chris Mason   Btrfs: allow bloc...
1881
  		 *
3204d33cd   Josef Bacik   Btrfs: add a flag...
1882
1883
  		 * The BTRFS_TRANS_DIRTY_BG_RUN flag is also used to make sure
  		 * only one process starts all the block group IO.  It wouldn't
1bbc621ef   Chris Mason   Btrfs: allow bloc...
1884
1885
1886
1887
  		 * hurt to have more than one go through, but there's no
  		 * real advantage to it either.
  		 */
  		mutex_lock(&root->fs_info->ro_block_group_mutex);
3204d33cd   Josef Bacik   Btrfs: add a flag...
1888
1889
  		if (!test_and_set_bit(BTRFS_TRANS_DIRTY_BG_RUN,
  				      &cur_trans->flags))
1bbc621ef   Chris Mason   Btrfs: allow bloc...
1890
  			run_it = 1;
1bbc621ef   Chris Mason   Btrfs: allow bloc...
1891
1892
1893
1894
1895
1896
1897
1898
1899
  		mutex_unlock(&root->fs_info->ro_block_group_mutex);
  
  		if (run_it)
  			ret = btrfs_start_dirty_block_groups(trans, root);
  	}
  	if (ret) {
  		btrfs_end_transaction(trans, root);
  		return ret;
  	}
4a9d8bdee   Miao Xie   Btrfs: make the s...
1900
1901
1902
  	spin_lock(&root->fs_info->trans_lock);
  	if (cur_trans->state >= TRANS_STATE_COMMIT_START) {
  		spin_unlock(&root->fs_info->trans_lock);
13c5a93e7   Josef Bacik   Btrfs: avoid taki...
1903
  		atomic_inc(&cur_trans->use_count);
49b25e054   Jeff Mahoney   btrfs: enhance tr...
1904
  		ret = btrfs_end_transaction(trans, root);
ccd467d60   Chris Mason   Btrfs: crash reco...
1905

b9c8300c2   Li Zefan   Btrfs: remove a B...
1906
  		wait_for_commit(root, cur_trans);
15ee9bc7e   Josef Bacik   Btrfs: delay comm...
1907

b4924a0fa   Liu Bo   Btrfs: catch tran...
1908
1909
  		if (unlikely(cur_trans->aborted))
  			ret = cur_trans->aborted;
724e2315d   Josef Bacik   Btrfs: fix two us...
1910
  		btrfs_put_transaction(cur_trans);
15ee9bc7e   Josef Bacik   Btrfs: delay comm...
1911

49b25e054   Jeff Mahoney   btrfs: enhance tr...
1912
  		return ret;
79154b1b5   Chris Mason   Btrfs: transactio...
1913
  	}
4313b3994   Chris Mason   Btrfs: Reduce sta...
1914

4a9d8bdee   Miao Xie   Btrfs: make the s...
1915
  	cur_trans->state = TRANS_STATE_COMMIT_START;
bb9c12c94   Sage Weil   Btrfs: async tran...
1916
  	wake_up(&root->fs_info->transaction_blocked_wait);
ccd467d60   Chris Mason   Btrfs: crash reco...
1917
1918
1919
  	if (cur_trans->list.prev != &root->fs_info->trans_list) {
  		prev_trans = list_entry(cur_trans->list.prev,
  					struct btrfs_transaction, list);
4a9d8bdee   Miao Xie   Btrfs: make the s...
1920
  		if (prev_trans->state != TRANS_STATE_COMPLETED) {
13c5a93e7   Josef Bacik   Btrfs: avoid taki...
1921
  			atomic_inc(&prev_trans->use_count);
a4abeea41   Josef Bacik   Btrfs: kill trans...
1922
  			spin_unlock(&root->fs_info->trans_lock);
ccd467d60   Chris Mason   Btrfs: crash reco...
1923
1924
  
  			wait_for_commit(root, prev_trans);
1f9b8c8fb   Filipe Manana   Btrfs: check if p...
1925
  			ret = prev_trans->aborted;
ccd467d60   Chris Mason   Btrfs: crash reco...
1926

724e2315d   Josef Bacik   Btrfs: fix two us...
1927
  			btrfs_put_transaction(prev_trans);
1f9b8c8fb   Filipe Manana   Btrfs: check if p...
1928
1929
  			if (ret)
  				goto cleanup_transaction;
a4abeea41   Josef Bacik   Btrfs: kill trans...
1930
1931
  		} else {
  			spin_unlock(&root->fs_info->trans_lock);
ccd467d60   Chris Mason   Btrfs: crash reco...
1932
  		}
a4abeea41   Josef Bacik   Btrfs: kill trans...
1933
1934
  	} else {
  		spin_unlock(&root->fs_info->trans_lock);
ccd467d60   Chris Mason   Btrfs: crash reco...
1935
  	}
15ee9bc7e   Josef Bacik   Btrfs: delay comm...
1936

0860adfdb   Miao Xie   Btrfs: don't wait...
1937
  	extwriter_counter_dec(cur_trans, trans->type);
824366177   Miao Xie   Btrfs: don't flus...
1938
1939
1940
  	ret = btrfs_start_delalloc_flush(root->fs_info);
  	if (ret)
  		goto cleanup_transaction;
8d875f95d   Chris Mason   btrfs: disable st...
1941
  	ret = btrfs_run_delayed_items(trans, root);
581227d0d   Miao Xie   Btrfs: remove the...
1942
1943
  	if (ret)
  		goto cleanup_transaction;
15ee9bc7e   Josef Bacik   Btrfs: delay comm...
1944

581227d0d   Miao Xie   Btrfs: remove the...
1945
1946
  	wait_event(cur_trans->writer_wait,
  		   extwriter_counter_read(cur_trans) == 0);
15ee9bc7e   Josef Bacik   Btrfs: delay comm...
1947

581227d0d   Miao Xie   Btrfs: remove the...
1948
  	/* some pending stuffs might be added after the previous flush. */
8d875f95d   Chris Mason   btrfs: disable st...
1949
  	ret = btrfs_run_delayed_items(trans, root);
ca4696371   Miao Xie   Btrfs: fix missin...
1950
1951
  	if (ret)
  		goto cleanup_transaction;
824366177   Miao Xie   Btrfs: don't flus...
1952
  	btrfs_wait_delalloc_flush(root->fs_info);
cb7ab0215   Wang Shilong   Btrfs: wrap repea...
1953

161c3549b   Josef Bacik   Btrfs: change how...
1954
  	btrfs_wait_pending_ordered(cur_trans);
50d9aa99b   Josef Bacik   Btrfs: make sure ...
1955

cb7ab0215   Wang Shilong   Btrfs: wrap repea...
1956
  	btrfs_scrub_pause(root);
7585717f3   Chris Mason   Btrfs: fix reloca...
1957
  	/*
ed0ca1402   Josef Bacik   Btrfs: set no_tra...
1958
1959
  	 * Ok now we need to make sure to block out any other joins while we
  	 * commit the transaction.  We could have started a join before setting
4a9d8bdee   Miao Xie   Btrfs: make the s...
1960
  	 * COMMIT_DOING so make sure to wait for num_writers to == 1 again.
ed0ca1402   Josef Bacik   Btrfs: set no_tra...
1961
1962
  	 */
  	spin_lock(&root->fs_info->trans_lock);
4a9d8bdee   Miao Xie   Btrfs: make the s...
1963
  	cur_trans->state = TRANS_STATE_COMMIT_DOING;
ed0ca1402   Josef Bacik   Btrfs: set no_tra...
1964
1965
1966
  	spin_unlock(&root->fs_info->trans_lock);
  	wait_event(cur_trans->writer_wait,
  		   atomic_read(&cur_trans->num_writers) == 1);
2cba30f17   Miao Xie   Btrfs: fix missed...
1967
1968
1969
  	/* ->aborted might be set after the previous check, so check it */
  	if (unlikely(ACCESS_ONCE(cur_trans->aborted))) {
  		ret = cur_trans->aborted;
6cf7f77e6   Wang Shilong   Btrfs: fix a poss...
1970
  		goto scrub_continue;
2cba30f17   Miao Xie   Btrfs: fix missed...
1971
  	}
e038dca80   Chris Mason   Merge branch 'for...
1972
  	/*
7585717f3   Chris Mason   Btrfs: fix reloca...
1973
1974
1975
1976
1977
  	 * the reloc mutex makes sure that we stop
  	 * the balancing code from coming in and moving
  	 * extents around in the middle of the commit
  	 */
  	mutex_lock(&root->fs_info->reloc_mutex);
42874b3db   Miao Xie   Btrfs: fix the sn...
1978
1979
1980
1981
1982
1983
  	/*
  	 * We needn't worry about the delayed items because we will
  	 * deal with them in create_pending_snapshot(), which is the
  	 * core function of the snapshot creation.
  	 */
  	ret = create_pending_snapshots(trans, root->fs_info);
49b25e054   Jeff Mahoney   btrfs: enhance tr...
1984
1985
  	if (ret) {
  		mutex_unlock(&root->fs_info->reloc_mutex);
6cf7f77e6   Wang Shilong   Btrfs: fix a poss...
1986
  		goto scrub_continue;
49b25e054   Jeff Mahoney   btrfs: enhance tr...
1987
  	}
3063d29f2   Chris Mason   Btrfs: Move snaps...
1988

42874b3db   Miao Xie   Btrfs: fix the sn...
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
  	/*
  	 * We insert the dir indexes of the snapshots and update the inode
  	 * of the snapshots' parents after the snapshot creation, so there
  	 * are some delayed items which are not dealt with. Now deal with
  	 * them.
  	 *
  	 * We needn't worry that this operation will corrupt the snapshots,
  	 * because all the tree which are snapshoted will be forced to COW
  	 * the nodes and leaves.
  	 */
  	ret = btrfs_run_delayed_items(trans, root);
49b25e054   Jeff Mahoney   btrfs: enhance tr...
2000
2001
  	if (ret) {
  		mutex_unlock(&root->fs_info->reloc_mutex);
6cf7f77e6   Wang Shilong   Btrfs: fix a poss...
2002
  		goto scrub_continue;
49b25e054   Jeff Mahoney   btrfs: enhance tr...
2003
  	}
16cdcec73   Miao Xie   btrfs: implement ...
2004

56bec294d   Chris Mason   Btrfs: do extent ...
2005
  	ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
49b25e054   Jeff Mahoney   btrfs: enhance tr...
2006
2007
  	if (ret) {
  		mutex_unlock(&root->fs_info->reloc_mutex);
6cf7f77e6   Wang Shilong   Btrfs: fix a poss...
2008
  		goto scrub_continue;
49b25e054   Jeff Mahoney   btrfs: enhance tr...
2009
  	}
56bec294d   Chris Mason   Btrfs: do extent ...
2010

0ed4792af   Qu Wenruo   btrfs: qgroup: Sw...
2011
2012
2013
2014
2015
2016
  	/* Reocrd old roots for later qgroup accounting */
  	ret = btrfs_qgroup_prepare_account_extents(trans, root->fs_info);
  	if (ret) {
  		mutex_unlock(&root->fs_info->reloc_mutex);
  		goto scrub_continue;
  	}
e999376f0   Chris Mason   Btrfs: avoid dela...
2017
2018
2019
2020
2021
  	/*
  	 * make sure none of the code above managed to slip in a
  	 * delayed item
  	 */
  	btrfs_assert_delayed_root_empty(root);
2c90e5d65   Chris Mason   Btrfs: still corr...
2022
  	WARN_ON(cur_trans != trans->transaction);
dc17ff8f1   Chris Mason   Btrfs: Add data=o...
2023

e02119d5a   Chris Mason   Btrfs: Add a writ...
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
  	/* btrfs_commit_tree_roots is responsible for getting the
  	 * various roots consistent with each other.  Every pointer
  	 * in the tree of tree roots has to point to the most up to date
  	 * root for every subvolume and other tree.  So, we have to keep
  	 * the tree logging code from jumping in and changing any
  	 * of the trees.
  	 *
  	 * At this point in the commit, there can't be any tree-log
  	 * writers, but a little lower down we drop the trans mutex
  	 * and let new people in.  By holding the tree_log_mutex
  	 * from now until after the super is written, we avoid races
  	 * with the tree-log code.
  	 */
  	mutex_lock(&root->fs_info->tree_log_mutex);
5d4f98a28   Yan Zheng   Btrfs: Mixed back...
2038
  	ret = commit_fs_roots(trans, root);
49b25e054   Jeff Mahoney   btrfs: enhance tr...
2039
2040
  	if (ret) {
  		mutex_unlock(&root->fs_info->tree_log_mutex);
871383be5   David Sterba   btrfs: add missin...
2041
  		mutex_unlock(&root->fs_info->reloc_mutex);
6cf7f77e6   Wang Shilong   Btrfs: fix a poss...
2042
  		goto scrub_continue;
49b25e054   Jeff Mahoney   btrfs: enhance tr...
2043
  	}
54aa1f4df   Chris Mason   Btrfs: Audit call...
2044

3818aea27   Qu Wenruo   btrfs: Add noinod...
2045
  	/*
7e1876aca   David Sterba   btrfs: switch ino...
2046
2047
  	 * Since the transaction is done, we can apply the pending changes
  	 * before the next transaction.
3818aea27   Qu Wenruo   btrfs: Add noinod...
2048
  	 */
572d9ab78   David Sterba   btrfs: add suppor...
2049
  	btrfs_apply_pending_changes(root->fs_info);
3818aea27   Qu Wenruo   btrfs: Add noinod...
2050

5d4f98a28   Yan Zheng   Btrfs: Mixed back...
2051
  	/* commit_fs_roots gets rid of all the tree log roots, it is now
e02119d5a   Chris Mason   Btrfs: Add a writ...
2052
2053
2054
  	 * safe to free the root of tree log roots
  	 */
  	btrfs_free_log_root_tree(trans, root->fs_info);
0ed4792af   Qu Wenruo   btrfs: qgroup: Sw...
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
  	/*
  	 * Since fs roots are all committed, we can get a quite accurate
  	 * new_roots. So let's do quota accounting.
  	 */
  	ret = btrfs_qgroup_account_extents(trans, root->fs_info);
  	if (ret < 0) {
  		mutex_unlock(&root->fs_info->tree_log_mutex);
  		mutex_unlock(&root->fs_info->reloc_mutex);
  		goto scrub_continue;
  	}
5d4f98a28   Yan Zheng   Btrfs: Mixed back...
2065
  	ret = commit_cowonly_roots(trans, root);
49b25e054   Jeff Mahoney   btrfs: enhance tr...
2066
2067
  	if (ret) {
  		mutex_unlock(&root->fs_info->tree_log_mutex);
871383be5   David Sterba   btrfs: add missin...
2068
  		mutex_unlock(&root->fs_info->reloc_mutex);
6cf7f77e6   Wang Shilong   Btrfs: fix a poss...
2069
  		goto scrub_continue;
49b25e054   Jeff Mahoney   btrfs: enhance tr...
2070
  	}
54aa1f4df   Chris Mason   Btrfs: Audit call...
2071

2cba30f17   Miao Xie   Btrfs: fix missed...
2072
2073
2074
2075
2076
2077
2078
2079
  	/*
  	 * The tasks which save the space cache and inode cache may also
  	 * update ->aborted, check it.
  	 */
  	if (unlikely(ACCESS_ONCE(cur_trans->aborted))) {
  		ret = cur_trans->aborted;
  		mutex_unlock(&root->fs_info->tree_log_mutex);
  		mutex_unlock(&root->fs_info->reloc_mutex);
6cf7f77e6   Wang Shilong   Btrfs: fix a poss...
2080
  		goto scrub_continue;
2cba30f17   Miao Xie   Btrfs: fix missed...
2081
  	}
11833d66b   Yan Zheng   Btrfs: improve as...
2082
  	btrfs_prepare_extent_commit(trans, root);
78fae27eb   Chris Mason   Btrfs: leak fixes...
2083
  	cur_trans = root->fs_info->running_transaction;
5d4f98a28   Yan Zheng   Btrfs: Mixed back...
2084
2085
2086
  
  	btrfs_set_root_node(&root->fs_info->tree_root->root_item,
  			    root->fs_info->tree_root->node);
9e351cc86   Josef Bacik   Btrfs: remove tra...
2087
2088
  	list_add_tail(&root->fs_info->tree_root->dirty_list,
  		      &cur_trans->switch_commits);
5d4f98a28   Yan Zheng   Btrfs: Mixed back...
2089
2090
2091
  
  	btrfs_set_root_node(&root->fs_info->chunk_root->root_item,
  			    root->fs_info->chunk_root->node);
9e351cc86   Josef Bacik   Btrfs: remove tra...
2092
2093
2094
2095
  	list_add_tail(&root->fs_info->chunk_root->dirty_list,
  		      &cur_trans->switch_commits);
  
  	switch_commit_roots(cur_trans, root->fs_info);
5d4f98a28   Yan Zheng   Btrfs: Mixed back...
2096

edf39272d   Jan Schmidt   Btrfs: call the q...
2097
  	assert_qgroups_uptodate(trans);
ce93ec548   Josef Bacik   Btrfs: track dirt...
2098
  	ASSERT(list_empty(&cur_trans->dirty_bgs));
1bbc621ef   Chris Mason   Btrfs: allow bloc...
2099
  	ASSERT(list_empty(&cur_trans->io_bgs));
5d4f98a28   Yan Zheng   Btrfs: Mixed back...
2100
  	update_super_roots(root);
e02119d5a   Chris Mason   Btrfs: Add a writ...
2101

60e7cd3a4   Josef Bacik   Btrfs: fix transi...
2102
2103
  	btrfs_set_super_log_root(root->fs_info->super_copy, 0);
  	btrfs_set_super_log_root_level(root->fs_info->super_copy, 0);
6c41761fc   David Sterba   btrfs: separate s...
2104
2105
  	memcpy(root->fs_info->super_for_commit, root->fs_info->super_copy,
  	       sizeof(*root->fs_info->super_copy));
ccd467d60   Chris Mason   Btrfs: crash reco...
2106

935e5cc93   Miao Xie   Btrfs: fix wrong ...
2107
  	btrfs_update_commit_device_size(root->fs_info);
ce7213c70   Miao Xie   Btrfs: fix wrong ...
2108
  	btrfs_update_commit_device_bytes_used(root, cur_trans);
935e5cc93   Miao Xie   Btrfs: fix wrong ...
2109

afcdd129e   Josef Bacik   Btrfs: add a flag...
2110
2111
  	clear_bit(BTRFS_FS_LOG1_ERR, &root->fs_info->flags);
  	clear_bit(BTRFS_FS_LOG2_ERR, &root->fs_info->flags);
656f30dba   Filipe Manana   Btrfs: be aware o...
2112

4fbcdf669   Filipe Manana   Btrfs: fix -ENOSP...
2113
  	btrfs_trans_release_chunk_metadata(trans);
a4abeea41   Josef Bacik   Btrfs: kill trans...
2114
  	spin_lock(&root->fs_info->trans_lock);
4a9d8bdee   Miao Xie   Btrfs: make the s...
2115
  	cur_trans->state = TRANS_STATE_UNBLOCKED;
a4abeea41   Josef Bacik   Btrfs: kill trans...
2116
  	root->fs_info->running_transaction = NULL;
a4abeea41   Josef Bacik   Btrfs: kill trans...
2117
  	spin_unlock(&root->fs_info->trans_lock);
7585717f3   Chris Mason   Btrfs: fix reloca...
2118
  	mutex_unlock(&root->fs_info->reloc_mutex);
b7ec40d78   Chris Mason   Btrfs: reduce sta...
2119

f92957493   Chris Mason   btrfs_start_trans...
2120
  	wake_up(&root->fs_info->transaction_wait);
e6dcd2dc9   Chris Mason   Btrfs: New data=o...
2121

79154b1b5   Chris Mason   Btrfs: transactio...
2122
  	ret = btrfs_write_and_wait_transaction(trans, root);
49b25e054   Jeff Mahoney   btrfs: enhance tr...
2123
  	if (ret) {
34d970070   Anand Jain   btrfs: rename btr...
2124
  		btrfs_handle_fs_error(root->fs_info, ret,
087488109   David Sterba   btrfs: clean up t...
2125
  			    "Error while writing out transaction");
49b25e054   Jeff Mahoney   btrfs: enhance tr...
2126
  		mutex_unlock(&root->fs_info->tree_log_mutex);
6cf7f77e6   Wang Shilong   Btrfs: fix a poss...
2127
  		goto scrub_continue;
49b25e054   Jeff Mahoney   btrfs: enhance tr...
2128
2129
2130
2131
2132
  	}
  
  	ret = write_ctree_super(trans, root, 0);
  	if (ret) {
  		mutex_unlock(&root->fs_info->tree_log_mutex);
6cf7f77e6   Wang Shilong   Btrfs: fix a poss...
2133
  		goto scrub_continue;
49b25e054   Jeff Mahoney   btrfs: enhance tr...
2134
  	}
4313b3994   Chris Mason   Btrfs: Reduce sta...
2135

e02119d5a   Chris Mason   Btrfs: Add a writ...
2136
2137
2138
2139
2140
  	/*
  	 * the super is written, we can safely allow the tree-loggers
  	 * to go about their business
  	 */
  	mutex_unlock(&root->fs_info->tree_log_mutex);
11833d66b   Yan Zheng   Btrfs: improve as...
2141
  	btrfs_finish_extent_commit(trans, root);
4313b3994   Chris Mason   Btrfs: Reduce sta...
2142

3204d33cd   Josef Bacik   Btrfs: add a flag...
2143
  	if (test_bit(BTRFS_TRANS_HAVE_FREE_BGS, &cur_trans->flags))
13212b54d   Zhao Lei   btrfs: Fix out-of...
2144
  		btrfs_clear_space_info_full(root->fs_info);
15ee9bc7e   Josef Bacik   Btrfs: delay comm...
2145
  	root->fs_info->last_trans_committed = cur_trans->transid;
4a9d8bdee   Miao Xie   Btrfs: make the s...
2146
2147
2148
2149
2150
  	/*
  	 * We needn't acquire the lock here because there is no other task
  	 * which can change it.
  	 */
  	cur_trans->state = TRANS_STATE_COMPLETED;
2c90e5d65   Chris Mason   Btrfs: still corr...
2151
  	wake_up(&cur_trans->commit_wait);
3de4586c5   Chris Mason   Btrfs: Allow subv...
2152

a4abeea41   Josef Bacik   Btrfs: kill trans...
2153
  	spin_lock(&root->fs_info->trans_lock);
13c5a93e7   Josef Bacik   Btrfs: avoid taki...
2154
  	list_del_init(&cur_trans->list);
a4abeea41   Josef Bacik   Btrfs: kill trans...
2155
  	spin_unlock(&root->fs_info->trans_lock);
724e2315d   Josef Bacik   Btrfs: fix two us...
2156
2157
  	btrfs_put_transaction(cur_trans);
  	btrfs_put_transaction(cur_trans);
58176a960   Josef Bacik   Btrfs: Add per-ro...
2158

0860adfdb   Miao Xie   Btrfs: don't wait...
2159
  	if (trans->type & __TRANS_FREEZABLE)
354aa0fb6   Miao Xie   Btrfs: fix orphan...
2160
  		sb_end_intwrite(root->fs_info->sb);
b2b5ef5c8   Jan Kara   btrfs: Convert to...
2161

1abe9b8a1   liubo   Btrfs: add initia...
2162
  	trace_btrfs_transaction_commit(root);
a2de733c7   Arne Jansen   btrfs: scrub
2163
  	btrfs_scrub_continue(root);
9ed74f2db   Josef Bacik   Btrfs: proper -EN...
2164
2165
  	if (current->journal_info == trans)
  		current->journal_info = NULL;
2c90e5d65   Chris Mason   Btrfs: still corr...
2166
  	kmem_cache_free(btrfs_trans_handle_cachep, trans);
24bbcf044   Yan, Zheng   Btrfs: Add delaye...
2167

9e7cc91a6   Wang Xiaoguang   btrfs: fix fsfree...
2168
2169
2170
2171
  	/*
  	 * If fs has been frozen, we can not handle delayed iputs, otherwise
  	 * it'll result in deadlock about SB_FREEZE_FS.
  	 */
8a7330130   Zhao Lei   btrfs: Fix lockde...
2172
  	if (current != root->fs_info->transaction_kthread &&
9e7cc91a6   Wang Xiaoguang   btrfs: fix fsfree...
2173
2174
  	    current != root->fs_info->cleaner_kthread &&
  	    !root->fs_info->fs_frozen)
24bbcf044   Yan, Zheng   Btrfs: Add delaye...
2175
  		btrfs_run_delayed_iputs(root);
79154b1b5   Chris Mason   Btrfs: transactio...
2176
  	return ret;
49b25e054   Jeff Mahoney   btrfs: enhance tr...
2177

6cf7f77e6   Wang Shilong   Btrfs: fix a poss...
2178
2179
  scrub_continue:
  	btrfs_scrub_continue(root);
49b25e054   Jeff Mahoney   btrfs: enhance tr...
2180
  cleanup_transaction:
0e7211069   Josef Bacik   Btrfs: change how...
2181
  	btrfs_trans_release_metadata(trans, root);
4fbcdf669   Filipe Manana   Btrfs: fix -ENOSP...
2182
  	btrfs_trans_release_chunk_metadata(trans);
0e7211069   Josef Bacik   Btrfs: change how...
2183
  	trans->block_rsv = NULL;
c2cf52eb7   Simon Kirby   Btrfs: Include th...
2184
  	btrfs_warn(root->fs_info, "Skipping commit of aborted transaction.");
49b25e054   Jeff Mahoney   btrfs: enhance tr...
2185
2186
  	if (current->journal_info == trans)
  		current->journal_info = NULL;
7b8b92af5   Josef Bacik   Btrfs: abort the ...
2187
  	cleanup_transaction(trans, root, ret);
49b25e054   Jeff Mahoney   btrfs: enhance tr...
2188
2189
  
  	return ret;
79154b1b5   Chris Mason   Btrfs: transactio...
2190
  }
d352ac681   Chris Mason   Btrfs: add and im...
2191
  /*
9d1a2a3ad   David Sterba   btrfs: clean snap...
2192
2193
2194
2195
2196
2197
2198
2199
   * return < 0 if error
   * 0 if there are no more dead_roots at the time of call
   * 1 there are more to be processed, call me again
   *
   * The return value indicates there are certainly more snapshots to delete, but
   * if there comes a new one during processing, it may return 0. We don't mind,
   * because btrfs_commit_super will poke cleaner thread and it will process it a
   * few seconds later.
d352ac681   Chris Mason   Btrfs: add and im...
2200
   */
9d1a2a3ad   David Sterba   btrfs: clean snap...
2201
  int btrfs_clean_one_deleted_snapshot(struct btrfs_root *root)
e9d0b13b5   Chris Mason   Btrfs: Btree defr...
2202
  {
9d1a2a3ad   David Sterba   btrfs: clean snap...
2203
  	int ret;
5d4f98a28   Yan Zheng   Btrfs: Mixed back...
2204
  	struct btrfs_fs_info *fs_info = root->fs_info;
a4abeea41   Josef Bacik   Btrfs: kill trans...
2205
  	spin_lock(&fs_info->trans_lock);
9d1a2a3ad   David Sterba   btrfs: clean snap...
2206
2207
2208
2209
2210
2211
  	if (list_empty(&fs_info->dead_roots)) {
  		spin_unlock(&fs_info->trans_lock);
  		return 0;
  	}
  	root = list_first_entry(&fs_info->dead_roots,
  			struct btrfs_root, root_list);
cfad392b2   Josef Bacik   Btrfs: check to s...
2212
  	list_del_init(&root->root_list);
a4abeea41   Josef Bacik   Btrfs: kill trans...
2213
  	spin_unlock(&fs_info->trans_lock);
e9d0b13b5   Chris Mason   Btrfs: Btree defr...
2214

ab8d0fc48   Jeff Mahoney   btrfs: convert pr...
2215
  	btrfs_debug(fs_info, "cleaner removing %llu", root->objectid);
76dda93c6   Yan, Zheng   Btrfs: add snapsh...
2216

9d1a2a3ad   David Sterba   btrfs: clean snap...
2217
  	btrfs_kill_all_delayed_nodes(root);
16cdcec73   Miao Xie   btrfs: implement ...
2218

9d1a2a3ad   David Sterba   btrfs: clean snap...
2219
2220
2221
2222
2223
  	if (btrfs_header_backref_rev(root->node) <
  			BTRFS_MIXED_BACKREF_REV)
  		ret = btrfs_drop_snapshot(root, NULL, 0, 0);
  	else
  		ret = btrfs_drop_snapshot(root, NULL, 1, 0);
32471dc2b   David Sterba   btrfs: remove obs...
2224

6596a9281   Josef Bacik   Btrfs: don't bug_...
2225
  	return (ret < 0) ? 0 : 1;
e9d0b13b5   Chris Mason   Btrfs: Btree defr...
2226
  }
572d9ab78   David Sterba   btrfs: add suppor...
2227
2228
2229
2230
2231
  
  void btrfs_apply_pending_changes(struct btrfs_fs_info *fs_info)
  {
  	unsigned long prev;
  	unsigned long bit;
6c9fe14f9   Qu Wenruo   btrfs: Fix the bu...
2232
  	prev = xchg(&fs_info->pending_changes, 0);
572d9ab78   David Sterba   btrfs: add suppor...
2233
2234
  	if (!prev)
  		return;
7e1876aca   David Sterba   btrfs: switch ino...
2235
2236
2237
2238
2239
2240
2241
2242
2243
  	bit = 1 << BTRFS_PENDING_SET_INODE_MAP_CACHE;
  	if (prev & bit)
  		btrfs_set_opt(fs_info->mount_opt, INODE_MAP_CACHE);
  	prev &= ~bit;
  
  	bit = 1 << BTRFS_PENDING_CLEAR_INODE_MAP_CACHE;
  	if (prev & bit)
  		btrfs_clear_opt(fs_info->mount_opt, INODE_MAP_CACHE);
  	prev &= ~bit;
d51033d05   David Sterba   btrfs: introduce ...
2244
2245
2246
2247
  	bit = 1 << BTRFS_PENDING_COMMIT;
  	if (prev & bit)
  		btrfs_debug(fs_info, "pending commit done");
  	prev &= ~bit;
572d9ab78   David Sterba   btrfs: add suppor...
2248
2249
2250
2251
  	if (prev)
  		btrfs_warn(fs_info,
  			"unknown pending changes left 0x%lx, ignoring", prev);
  }