xref: /linux/fs/btrfs/transaction.c (revision 7f98ab9da046865d57c102fd3ca9669a29845f67)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (C) 2007 Oracle.  All rights reserved.
4  */
5 
6 #include <linux/fs.h>
7 #include <linux/slab.h>
8 #include <linux/sched.h>
9 #include <linux/sched/mm.h>
10 #include <linux/writeback.h>
11 #include <linux/pagemap.h>
12 #include <linux/blkdev.h>
13 #include <linux/uuid.h>
14 #include <linux/timekeeping.h>
15 #include "misc.h"
16 #include "ctree.h"
17 #include "disk-io.h"
18 #include "transaction.h"
19 #include "locking.h"
20 #include "tree-log.h"
21 #include "volumes.h"
22 #include "dev-replace.h"
23 #include "qgroup.h"
24 #include "block-group.h"
25 #include "space-info.h"
26 #include "fs.h"
27 #include "accessors.h"
28 #include "extent-tree.h"
29 #include "root-tree.h"
30 #include "dir-item.h"
31 #include "uuid-tree.h"
32 #include "ioctl.h"
33 #include "relocation.h"
34 #include "scrub.h"
35 #include "ordered-data.h"
36 #include "delayed-inode.h"
37 
38 static struct kmem_cache *btrfs_trans_handle_cachep;
39 
40 /*
41  * Transaction states and transitions
42  *
43  * No running transaction (fs tree blocks are not modified)
44  * |
45  * | To next stage:
46  * |  Call start_transaction() variants. Except btrfs_join_transaction_nostart().
47  * V
48  * Transaction N [[TRANS_STATE_RUNNING]]
49  * |
50  * | New trans handles can be attached to transaction N by calling all
51  * | start_transaction() variants.
52  * |
53  * | To next stage:
54  * |  Call btrfs_commit_transaction() on any trans handle attached to
55  * |  transaction N
56  * V
57  * Transaction N [[TRANS_STATE_COMMIT_PREP]]
58  * |
59  * | If there are simultaneous calls to btrfs_commit_transaction() one will win
60  * | the race and the rest will wait for the winner to commit the transaction.
61  * |
62  * | The winner will wait for previous running transaction to completely finish
63  * | if there is one.
64  * |
65  * Transaction N [[TRANS_STATE_COMMIT_START]]
66  * |
67  * | Then one of the following happens:
68  * | - Wait for all other trans handle holders to release.
69  * |   The btrfs_commit_transaction() caller will do the commit work.
70  * | - Wait for current transaction to be committed by others.
71  * |   Other btrfs_commit_transaction() caller will do the commit work.
72  * |
73  * | At this stage, only btrfs_join_transaction*() variants can attach
74  * | to this running transaction.
75  * | All other variants will wait for current one to finish and attach to
76  * | transaction N+1.
77  * |
78  * | To next stage:
79  * |  Caller is chosen to commit transaction N, and all other trans handle
80  * |  haven been released.
81  * V
82  * Transaction N [[TRANS_STATE_COMMIT_DOING]]
83  * |
84  * | The heavy lifting transaction work is started.
85  * | From running delayed refs (modifying extent tree) to creating pending
86  * | snapshots, running qgroups.
87  * | In short, modify supporting trees to reflect modifications of subvolume
88  * | trees.
89  * |
90  * | At this stage, all start_transaction() calls will wait for this
91  * | transaction to finish and attach to transaction N+1.
92  * |
93  * | To next stage:
94  * |  Until all supporting trees are updated.
95  * V
96  * Transaction N [[TRANS_STATE_UNBLOCKED]]
97  * |						    Transaction N+1
98  * | All needed trees are modified, thus we only    [[TRANS_STATE_RUNNING]]
99  * | need to write them back to disk and update	    |
100  * | super blocks.				    |
101  * |						    |
102  * | At this stage, new transaction is allowed to   |
103  * | start.					    |
104  * | All new start_transaction() calls will be	    |
105  * | attached to transid N+1.			    |
106  * |						    |
107  * | To next stage:				    |
108  * |  Until all tree blocks and super blocks are    |
109  * |  written to block devices			    |
110  * V						    |
111  * Transaction N [[TRANS_STATE_COMPLETED]]	    V
112  *   All tree blocks and super blocks are written.  Transaction N+1
113  *   This transaction is finished and all its	    [[TRANS_STATE_COMMIT_START]]
114  *   data structures will be cleaned up.	    | Life goes on
115  */
116 static const unsigned int btrfs_blocked_trans_types[TRANS_STATE_MAX] = {
117 	[TRANS_STATE_RUNNING]		= 0U,
118 	[TRANS_STATE_COMMIT_PREP]	= 0U,
119 	[TRANS_STATE_COMMIT_START]	= (__TRANS_START | __TRANS_ATTACH),
120 	[TRANS_STATE_COMMIT_DOING]	= (__TRANS_START |
121 					   __TRANS_ATTACH |
122 					   __TRANS_JOIN |
123 					   __TRANS_JOIN_NOSTART),
124 	[TRANS_STATE_UNBLOCKED]		= (__TRANS_START |
125 					   __TRANS_ATTACH |
126 					   __TRANS_JOIN |
127 					   __TRANS_JOIN_NOLOCK |
128 					   __TRANS_JOIN_NOSTART),
129 	[TRANS_STATE_SUPER_COMMITTED]	= (__TRANS_START |
130 					   __TRANS_ATTACH |
131 					   __TRANS_JOIN |
132 					   __TRANS_JOIN_NOLOCK |
133 					   __TRANS_JOIN_NOSTART),
134 	[TRANS_STATE_COMPLETED]		= (__TRANS_START |
135 					   __TRANS_ATTACH |
136 					   __TRANS_JOIN |
137 					   __TRANS_JOIN_NOLOCK |
138 					   __TRANS_JOIN_NOSTART),
139 };
140 
btrfs_put_transaction(struct btrfs_transaction * transaction)141 void btrfs_put_transaction(struct btrfs_transaction *transaction)
142 {
143 	if (refcount_dec_and_test(&transaction->use_count)) {
144 		BUG_ON(!list_empty(&transaction->list));
145 		WARN_ON(!xa_empty(&transaction->delayed_refs.head_refs));
146 		WARN_ON(!xa_empty(&transaction->delayed_refs.dirty_extents));
147 		if (transaction->delayed_refs.pending_csums)
148 			btrfs_err(transaction->fs_info,
149 				  "pending csums is %llu",
150 				  transaction->delayed_refs.pending_csums);
151 		/*
152 		 * If any block groups are found in ->deleted_bgs then it's
153 		 * because the transaction was aborted and a commit did not
154 		 * happen (things failed before writing the new superblock
155 		 * and calling btrfs_finish_extent_commit()), so we can not
156 		 * discard the physical locations of the block groups.
157 		 */
158 		while (!list_empty(&transaction->deleted_bgs)) {
159 			struct btrfs_block_group *cache;
160 
161 			cache = list_first_entry(&transaction->deleted_bgs,
162 						 struct btrfs_block_group,
163 						 bg_list);
164 			/*
165 			 * Not strictly necessary to lock, as no other task will be using a
166 			 * block_group on the deleted_bgs list during a transaction abort.
167 			 */
168 			spin_lock(&transaction->fs_info->unused_bgs_lock);
169 			list_del_init(&cache->bg_list);
170 			spin_unlock(&transaction->fs_info->unused_bgs_lock);
171 			btrfs_unfreeze_block_group(cache);
172 			btrfs_put_block_group(cache);
173 		}
174 		WARN_ON(!list_empty(&transaction->dev_update_list));
175 		kfree(transaction);
176 	}
177 }
178 
switch_commit_roots(struct btrfs_trans_handle * trans)179 static noinline void switch_commit_roots(struct btrfs_trans_handle *trans)
180 {
181 	struct btrfs_transaction *cur_trans = trans->transaction;
182 	struct btrfs_fs_info *fs_info = trans->fs_info;
183 	struct btrfs_root *root, *tmp;
184 
185 	/*
186 	 * At this point no one can be using this transaction to modify any tree
187 	 * and no one can start another transaction to modify any tree either.
188 	 */
189 	ASSERT(cur_trans->state == TRANS_STATE_COMMIT_DOING,
190 	       "cur_trans->state=%d", cur_trans->state);
191 
192 	down_write(&fs_info->commit_root_sem);
193 
194 	if (test_bit(BTRFS_FS_RELOC_RUNNING, &fs_info->flags))
195 		fs_info->last_reloc_trans = trans->transid;
196 
197 	list_for_each_entry_safe(root, tmp, &cur_trans->switch_commits,
198 				 dirty_list) {
199 		list_del_init(&root->dirty_list);
200 		free_extent_buffer(root->commit_root);
201 		root->commit_root = btrfs_root_node(root);
202 		btrfs_extent_io_tree_release(&root->dirty_log_pages);
203 		btrfs_qgroup_clean_swapped_blocks(root);
204 	}
205 
206 	/* We can free old roots now. */
207 	spin_lock(&cur_trans->dropped_roots_lock);
208 	while (!list_empty(&cur_trans->dropped_roots)) {
209 		root = list_first_entry(&cur_trans->dropped_roots,
210 					struct btrfs_root, root_list);
211 		list_del_init(&root->root_list);
212 		spin_unlock(&cur_trans->dropped_roots_lock);
213 		btrfs_free_log(trans, root);
214 		btrfs_drop_and_free_fs_root(fs_info, root);
215 		spin_lock(&cur_trans->dropped_roots_lock);
216 	}
217 	spin_unlock(&cur_trans->dropped_roots_lock);
218 
219 	up_write(&fs_info->commit_root_sem);
220 }
221 
extwriter_counter_inc(struct btrfs_transaction * trans,unsigned int type)222 static inline void extwriter_counter_inc(struct btrfs_transaction *trans,
223 					 unsigned int type)
224 {
225 	if (type & TRANS_EXTWRITERS)
226 		atomic_inc(&trans->num_extwriters);
227 }
228 
extwriter_counter_dec(struct btrfs_transaction * trans,unsigned int type)229 static inline void extwriter_counter_dec(struct btrfs_transaction *trans,
230 					 unsigned int type)
231 {
232 	if (type & TRANS_EXTWRITERS)
233 		atomic_dec(&trans->num_extwriters);
234 }
235 
extwriter_counter_init(struct btrfs_transaction * trans,unsigned int type)236 static inline void extwriter_counter_init(struct btrfs_transaction *trans,
237 					  unsigned int type)
238 {
239 	atomic_set(&trans->num_extwriters, ((type & TRANS_EXTWRITERS) ? 1 : 0));
240 }
241 
extwriter_counter_read(struct btrfs_transaction * trans)242 static inline int extwriter_counter_read(struct btrfs_transaction *trans)
243 {
244 	return atomic_read(&trans->num_extwriters);
245 }
246 
247 /*
248  * To be called after doing the chunk btree updates right after allocating a new
249  * chunk (after btrfs_chunk_alloc_add_chunk_item() is called), when removing a
250  * chunk after all chunk btree updates and after finishing the second phase of
251  * chunk allocation (btrfs_create_pending_block_groups()) in case some block
252  * group had its chunk item insertion delayed to the second phase.
253  */
btrfs_trans_release_chunk_metadata(struct btrfs_trans_handle * trans)254 void btrfs_trans_release_chunk_metadata(struct btrfs_trans_handle *trans)
255 {
256 	struct btrfs_fs_info *fs_info = trans->fs_info;
257 
258 	if (!trans->chunk_bytes_reserved)
259 		return;
260 
261 	btrfs_block_rsv_release(fs_info, &fs_info->chunk_block_rsv,
262 				trans->chunk_bytes_reserved, NULL);
263 	trans->chunk_bytes_reserved = 0;
264 }
265 
266 /*
267  * either allocate a new transaction or hop into the existing one
268  */
join_transaction(struct btrfs_fs_info * fs_info,unsigned int type)269 static noinline int join_transaction(struct btrfs_fs_info *fs_info,
270 				     unsigned int type)
271 {
272 	struct btrfs_transaction *cur_trans;
273 
274 	spin_lock(&fs_info->trans_lock);
275 loop:
276 	/* The file system has been taken offline. No new transactions. */
277 	if (BTRFS_FS_ERROR(fs_info)) {
278 		spin_unlock(&fs_info->trans_lock);
279 		return -EROFS;
280 	}
281 
282 	cur_trans = fs_info->running_transaction;
283 	if (cur_trans) {
284 		if (TRANS_ABORTED(cur_trans)) {
285 			const int abort_error = cur_trans->aborted;
286 
287 			spin_unlock(&fs_info->trans_lock);
288 			return abort_error;
289 		}
290 		if (btrfs_blocked_trans_types[cur_trans->state] & type) {
291 			spin_unlock(&fs_info->trans_lock);
292 			return -EBUSY;
293 		}
294 		refcount_inc(&cur_trans->use_count);
295 		atomic_inc(&cur_trans->num_writers);
296 		extwriter_counter_inc(cur_trans, type);
297 		spin_unlock(&fs_info->trans_lock);
298 		btrfs_lockdep_acquire(fs_info, btrfs_trans_num_writers);
299 		btrfs_lockdep_acquire(fs_info, btrfs_trans_num_extwriters);
300 		return 0;
301 	}
302 	spin_unlock(&fs_info->trans_lock);
303 
304 	/*
305 	 * If we are ATTACH or TRANS_JOIN_NOSTART, we just want to catch the
306 	 * current transaction, and commit it. If there is no transaction, just
307 	 * return ENOENT.
308 	 */
309 	if (type == TRANS_ATTACH || type == TRANS_JOIN_NOSTART)
310 		return -ENOENT;
311 
312 	/*
313 	 * JOIN_NOLOCK only happens during the transaction commit, so
314 	 * it is impossible that ->running_transaction is NULL
315 	 */
316 	BUG_ON(type == TRANS_JOIN_NOLOCK);
317 
318 	cur_trans = kmalloc(sizeof(*cur_trans), GFP_NOFS);
319 	if (!cur_trans)
320 		return -ENOMEM;
321 
322 	btrfs_lockdep_acquire(fs_info, btrfs_trans_num_writers);
323 	btrfs_lockdep_acquire(fs_info, btrfs_trans_num_extwriters);
324 
325 	spin_lock(&fs_info->trans_lock);
326 	if (fs_info->running_transaction) {
327 		/*
328 		 * someone started a transaction after we unlocked.  Make sure
329 		 * to redo the checks above
330 		 */
331 		btrfs_lockdep_release(fs_info, btrfs_trans_num_extwriters);
332 		btrfs_lockdep_release(fs_info, btrfs_trans_num_writers);
333 		kfree(cur_trans);
334 		goto loop;
335 	} else if (BTRFS_FS_ERROR(fs_info)) {
336 		spin_unlock(&fs_info->trans_lock);
337 		btrfs_lockdep_release(fs_info, btrfs_trans_num_extwriters);
338 		btrfs_lockdep_release(fs_info, btrfs_trans_num_writers);
339 		kfree(cur_trans);
340 		return -EROFS;
341 	}
342 
343 	cur_trans->fs_info = fs_info;
344 	atomic_set(&cur_trans->pending_ordered, 0);
345 	init_waitqueue_head(&cur_trans->pending_wait);
346 	atomic_set(&cur_trans->num_writers, 1);
347 	extwriter_counter_init(cur_trans, type);
348 	init_waitqueue_head(&cur_trans->writer_wait);
349 	init_waitqueue_head(&cur_trans->commit_wait);
350 	cur_trans->state = TRANS_STATE_RUNNING;
351 	/*
352 	 * One for this trans handle, one so it will live on until we
353 	 * commit the transaction.
354 	 */
355 	refcount_set(&cur_trans->use_count, 2);
356 	cur_trans->flags = 0;
357 	cur_trans->start_time = ktime_get_seconds();
358 
359 	memset(&cur_trans->delayed_refs, 0, sizeof(cur_trans->delayed_refs));
360 
361 	xa_init(&cur_trans->delayed_refs.head_refs);
362 	xa_init(&cur_trans->delayed_refs.dirty_extents);
363 
364 	/*
365 	 * although the tree mod log is per file system and not per transaction,
366 	 * the log must never go across transaction boundaries.
367 	 */
368 	smp_mb();
369 	if (!list_empty(&fs_info->tree_mod_seq_list))
370 		WARN(1, KERN_ERR "BTRFS: tree_mod_seq_list not empty when creating a fresh transaction\n");
371 	if (!RB_EMPTY_ROOT(&fs_info->tree_mod_log))
372 		WARN(1, KERN_ERR "BTRFS: tree_mod_log rb tree not empty when creating a fresh transaction\n");
373 	atomic64_set(&fs_info->tree_mod_seq, 0);
374 
375 	spin_lock_init(&cur_trans->delayed_refs.lock);
376 
377 	INIT_LIST_HEAD(&cur_trans->pending_snapshots);
378 	INIT_LIST_HEAD(&cur_trans->dev_update_list);
379 	INIT_LIST_HEAD(&cur_trans->switch_commits);
380 	INIT_LIST_HEAD(&cur_trans->dirty_bgs);
381 	INIT_LIST_HEAD(&cur_trans->io_bgs);
382 	INIT_LIST_HEAD(&cur_trans->dropped_roots);
383 	mutex_init(&cur_trans->cache_write_mutex);
384 	spin_lock_init(&cur_trans->dirty_bgs_lock);
385 	INIT_LIST_HEAD(&cur_trans->deleted_bgs);
386 	spin_lock_init(&cur_trans->dropped_roots_lock);
387 	list_add_tail(&cur_trans->list, &fs_info->trans_list);
388 	btrfs_extent_io_tree_init(fs_info, &cur_trans->dirty_pages,
389 				  IO_TREE_TRANS_DIRTY_PAGES);
390 	btrfs_extent_io_tree_init(fs_info, &cur_trans->pinned_extents,
391 				  IO_TREE_FS_PINNED_EXTENTS);
392 	btrfs_set_fs_generation(fs_info, fs_info->generation + 1);
393 	cur_trans->transid = fs_info->generation;
394 	fs_info->running_transaction = cur_trans;
395 	cur_trans->aborted = 0;
396 	spin_unlock(&fs_info->trans_lock);
397 
398 	return 0;
399 }
400 
401 /*
402  * This does all the record keeping required to make sure that a shareable root
403  * is properly recorded in a given transaction.  This is required to make sure
404  * the old root from before we joined the transaction is deleted when the
405  * transaction commits.
406  */
record_root_in_trans(struct btrfs_trans_handle * trans,struct btrfs_root * root,bool force)407 static int record_root_in_trans(struct btrfs_trans_handle *trans,
408 			       struct btrfs_root *root,
409 			       bool force)
410 {
411 	struct btrfs_fs_info *fs_info = root->fs_info;
412 	int ret = 0;
413 
414 	if ((test_bit(BTRFS_ROOT_SHAREABLE, &root->state) &&
415 	    btrfs_get_root_last_trans(root) < trans->transid) || force) {
416 		WARN_ON(!force && root->commit_root != root->node);
417 
418 		/*
419 		 * see below for IN_TRANS_SETUP usage rules
420 		 * we have the reloc mutex held now, so there
421 		 * is only one writer in this function
422 		 */
423 		set_bit(BTRFS_ROOT_IN_TRANS_SETUP, &root->state);
424 
425 		/* make sure readers find IN_TRANS_SETUP before
426 		 * they find our root->last_trans update
427 		 */
428 		smp_wmb();
429 
430 		spin_lock(&fs_info->fs_roots_radix_lock);
431 		if (btrfs_get_root_last_trans(root) == trans->transid && !force) {
432 			spin_unlock(&fs_info->fs_roots_radix_lock);
433 			return 0;
434 		}
435 		radix_tree_tag_set(&fs_info->fs_roots_radix,
436 				   (unsigned long)btrfs_root_id(root),
437 				   BTRFS_ROOT_TRANS_TAG);
438 		spin_unlock(&fs_info->fs_roots_radix_lock);
439 		btrfs_set_root_last_trans(root, trans->transid);
440 
441 		/* this is pretty tricky.  We don't want to
442 		 * take the relocation lock in btrfs_record_root_in_trans
443 		 * unless we're really doing the first setup for this root in
444 		 * this transaction.
445 		 *
446 		 * Normally we'd use root->last_trans as a flag to decide
447 		 * if we want to take the expensive mutex.
448 		 *
449 		 * But, we have to set root->last_trans before we
450 		 * init the relocation root, otherwise, we trip over warnings
451 		 * in ctree.c.  The solution used here is to flag ourselves
452 		 * with root IN_TRANS_SETUP.  When this is 1, we're still
453 		 * fixing up the reloc trees and everyone must wait.
454 		 *
455 		 * When this is zero, they can trust root->last_trans and fly
456 		 * through btrfs_record_root_in_trans without having to take the
457 		 * lock.  smp_wmb() makes sure that all the writes above are
458 		 * done before we pop in the zero below
459 		 */
460 		ret = btrfs_init_reloc_root(trans, root);
461 		smp_mb__before_atomic();
462 		clear_bit(BTRFS_ROOT_IN_TRANS_SETUP, &root->state);
463 	}
464 	return ret;
465 }
466 
467 
btrfs_add_dropped_root(struct btrfs_trans_handle * trans,struct btrfs_root * root)468 void btrfs_add_dropped_root(struct btrfs_trans_handle *trans,
469 			    struct btrfs_root *root)
470 {
471 	struct btrfs_fs_info *fs_info = root->fs_info;
472 	struct btrfs_transaction *cur_trans = trans->transaction;
473 
474 	/* Add ourselves to the transaction dropped list */
475 	spin_lock(&cur_trans->dropped_roots_lock);
476 	list_add_tail(&root->root_list, &cur_trans->dropped_roots);
477 	spin_unlock(&cur_trans->dropped_roots_lock);
478 
479 	/* Make sure we don't try to update the root at commit time */
480 	spin_lock(&fs_info->fs_roots_radix_lock);
481 	radix_tree_tag_clear(&fs_info->fs_roots_radix,
482 			     (unsigned long)btrfs_root_id(root),
483 			     BTRFS_ROOT_TRANS_TAG);
484 	spin_unlock(&fs_info->fs_roots_radix_lock);
485 }
486 
btrfs_record_root_in_trans(struct btrfs_trans_handle * trans,struct btrfs_root * root)487 int btrfs_record_root_in_trans(struct btrfs_trans_handle *trans,
488 			       struct btrfs_root *root)
489 {
490 	struct btrfs_fs_info *fs_info = root->fs_info;
491 	int ret;
492 
493 	if (!test_bit(BTRFS_ROOT_SHAREABLE, &root->state))
494 		return 0;
495 
496 	/*
497 	 * see record_root_in_trans for comments about IN_TRANS_SETUP usage
498 	 * and barriers
499 	 */
500 	smp_rmb();
501 	if (btrfs_get_root_last_trans(root) == trans->transid &&
502 	    !test_bit(BTRFS_ROOT_IN_TRANS_SETUP, &root->state))
503 		return 0;
504 
505 	mutex_lock(&fs_info->reloc_mutex);
506 	ret = record_root_in_trans(trans, root, 0);
507 	mutex_unlock(&fs_info->reloc_mutex);
508 
509 	return ret;
510 }
511 
is_transaction_blocked(struct btrfs_transaction * trans)512 static inline int is_transaction_blocked(struct btrfs_transaction *trans)
513 {
514 	return (trans->state >= TRANS_STATE_COMMIT_START &&
515 		trans->state < TRANS_STATE_UNBLOCKED &&
516 		!TRANS_ABORTED(trans));
517 }
518 
519 /* wait for commit against the current transaction to become unblocked
520  * when this is done, it is safe to start a new transaction, but the current
521  * transaction might not be fully on disk.
522  */
wait_current_trans(struct btrfs_fs_info * fs_info,unsigned int type)523 static void wait_current_trans(struct btrfs_fs_info *fs_info, unsigned int type)
524 {
525 	struct btrfs_transaction *cur_trans;
526 
527 	spin_lock(&fs_info->trans_lock);
528 	cur_trans = fs_info->running_transaction;
529 	if (cur_trans && is_transaction_blocked(cur_trans) &&
530 	    (btrfs_blocked_trans_types[cur_trans->state] & type)) {
531 		refcount_inc(&cur_trans->use_count);
532 		spin_unlock(&fs_info->trans_lock);
533 
534 		btrfs_might_wait_for_state(fs_info, BTRFS_LOCKDEP_TRANS_UNBLOCKED);
535 		wait_event(fs_info->transaction_wait,
536 			   cur_trans->state >= TRANS_STATE_UNBLOCKED ||
537 			   TRANS_ABORTED(cur_trans));
538 		btrfs_put_transaction(cur_trans);
539 	} else {
540 		spin_unlock(&fs_info->trans_lock);
541 	}
542 }
543 
may_wait_transaction(struct btrfs_fs_info * fs_info,int type)544 static bool may_wait_transaction(struct btrfs_fs_info *fs_info, int type)
545 {
546 	if (test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags))
547 		return false;
548 
549 	if (type == TRANS_START)
550 		return true;
551 
552 	return false;
553 }
554 
need_reserve_reloc_root(struct btrfs_root * root)555 static inline bool need_reserve_reloc_root(struct btrfs_root *root)
556 {
557 	struct btrfs_fs_info *fs_info = root->fs_info;
558 
559 	if (!fs_info->reloc_ctl ||
560 	    !test_bit(BTRFS_ROOT_SHAREABLE, &root->state) ||
561 	    btrfs_root_id(root) == BTRFS_TREE_RELOC_OBJECTID ||
562 	    root->reloc_root)
563 		return false;
564 
565 	return true;
566 }
567 
btrfs_reserve_trans_metadata(struct btrfs_fs_info * fs_info,enum btrfs_reserve_flush_enum flush,u64 num_bytes,u64 * delayed_refs_bytes)568 static int btrfs_reserve_trans_metadata(struct btrfs_fs_info *fs_info,
569 					enum btrfs_reserve_flush_enum flush,
570 					u64 num_bytes,
571 					u64 *delayed_refs_bytes)
572 {
573 	struct btrfs_space_info *si = fs_info->trans_block_rsv.space_info;
574 	u64 bytes = num_bytes + *delayed_refs_bytes;
575 	int ret;
576 
577 	/*
578 	 * We want to reserve all the bytes we may need all at once, so we only
579 	 * do 1 enospc flushing cycle per transaction start.
580 	 */
581 	ret = btrfs_reserve_metadata_bytes(si, bytes, flush);
582 
583 	/*
584 	 * If we are an emergency flush, which can steal from the global block
585 	 * reserve, then attempt to not reserve space for the delayed refs, as
586 	 * we will consume space for them from the global block reserve.
587 	 */
588 	if (ret && flush == BTRFS_RESERVE_FLUSH_ALL_STEAL) {
589 		bytes -= *delayed_refs_bytes;
590 		*delayed_refs_bytes = 0;
591 		ret = btrfs_reserve_metadata_bytes(si, bytes, flush);
592 	}
593 
594 	return ret;
595 }
596 
597 static struct btrfs_trans_handle *
start_transaction(struct btrfs_root * root,unsigned int num_items,unsigned int type,enum btrfs_reserve_flush_enum flush,bool enforce_qgroups)598 start_transaction(struct btrfs_root *root, unsigned int num_items,
599 		  unsigned int type, enum btrfs_reserve_flush_enum flush,
600 		  bool enforce_qgroups)
601 {
602 	struct btrfs_fs_info *fs_info = root->fs_info;
603 	struct btrfs_block_rsv *delayed_refs_rsv = &fs_info->delayed_refs_rsv;
604 	struct btrfs_block_rsv *trans_rsv = &fs_info->trans_block_rsv;
605 	struct btrfs_trans_handle *h;
606 	struct btrfs_transaction *cur_trans;
607 	u64 num_bytes = 0;
608 	u64 qgroup_reserved = 0;
609 	u64 delayed_refs_bytes = 0;
610 	bool reloc_reserved = false;
611 	bool do_chunk_alloc = false;
612 	int ret;
613 
614 	if (BTRFS_FS_ERROR(fs_info))
615 		return ERR_PTR(-EROFS);
616 
617 	if (current->journal_info) {
618 		WARN_ON(type & TRANS_EXTWRITERS);
619 		h = current->journal_info;
620 		refcount_inc(&h->use_count);
621 		WARN_ON(refcount_read(&h->use_count) > 2);
622 		h->orig_rsv = h->block_rsv;
623 		h->block_rsv = NULL;
624 		goto got_it;
625 	}
626 
627 	/*
628 	 * Do the reservation before we join the transaction so we can do all
629 	 * the appropriate flushing if need be.
630 	 */
631 	if (num_items && root != fs_info->chunk_root) {
632 		qgroup_reserved = num_items * fs_info->nodesize;
633 		/*
634 		 * Use prealloc for now, as there might be a currently running
635 		 * transaction that could free this reserved space prematurely
636 		 * by committing.
637 		 */
638 		ret = btrfs_qgroup_reserve_meta_prealloc(root, qgroup_reserved,
639 							 enforce_qgroups, false);
640 		if (ret)
641 			return ERR_PTR(ret);
642 
643 		num_bytes = btrfs_calc_insert_metadata_size(fs_info, num_items);
644 		/*
645 		 * If we plan to insert/update/delete "num_items" from a btree,
646 		 * we will also generate delayed refs for extent buffers in the
647 		 * respective btree paths, so reserve space for the delayed refs
648 		 * that will be generated by the caller as it modifies btrees.
649 		 * Try to reserve them to avoid excessive use of the global
650 		 * block reserve.
651 		 */
652 		delayed_refs_bytes = btrfs_calc_delayed_ref_bytes(fs_info, num_items);
653 
654 		/*
655 		 * Do the reservation for the relocation root creation
656 		 */
657 		if (need_reserve_reloc_root(root)) {
658 			num_bytes += fs_info->nodesize;
659 			reloc_reserved = true;
660 		}
661 
662 		ret = btrfs_reserve_trans_metadata(fs_info, flush, num_bytes,
663 						   &delayed_refs_bytes);
664 		if (ret)
665 			goto reserve_fail;
666 
667 		btrfs_block_rsv_add_bytes(trans_rsv, num_bytes, true);
668 
669 		if (trans_rsv->space_info->force_alloc)
670 			do_chunk_alloc = true;
671 	} else if (num_items == 0 && flush == BTRFS_RESERVE_FLUSH_ALL &&
672 		   !btrfs_block_rsv_full(delayed_refs_rsv)) {
673 		/*
674 		 * Some people call with btrfs_start_transaction(root, 0)
675 		 * because they can be throttled, but have some other mechanism
676 		 * for reserving space.  We still want these guys to refill the
677 		 * delayed block_rsv so just add 1 items worth of reservation
678 		 * here.
679 		 */
680 		ret = btrfs_delayed_refs_rsv_refill(fs_info, flush);
681 		if (ret)
682 			goto reserve_fail;
683 	}
684 again:
685 	h = kmem_cache_zalloc(btrfs_trans_handle_cachep, GFP_NOFS);
686 	if (!h) {
687 		ret = -ENOMEM;
688 		goto alloc_fail;
689 	}
690 
691 	/*
692 	 * If we are JOIN_NOLOCK we're already committing a transaction and
693 	 * waiting on this guy, so we don't need to do the sb_start_intwrite
694 	 * because we're already holding a ref.  We need this because we could
695 	 * have raced in and did an fsync() on a file which can kick a commit
696 	 * and then we deadlock with somebody doing a freeze.
697 	 *
698 	 * If we are ATTACH, it means we just want to catch the current
699 	 * transaction and commit it, so we needn't do sb_start_intwrite().
700 	 */
701 	if (type & __TRANS_FREEZABLE)
702 		sb_start_intwrite(fs_info->sb);
703 
704 	if (may_wait_transaction(fs_info, type))
705 		wait_current_trans(fs_info, type);
706 
707 	do {
708 		ret = join_transaction(fs_info, type);
709 		if (ret == -EBUSY) {
710 			wait_current_trans(fs_info, type);
711 			if (unlikely(type == TRANS_ATTACH ||
712 				     type == TRANS_JOIN_NOSTART))
713 				ret = -ENOENT;
714 		}
715 	} while (ret == -EBUSY);
716 
717 	if (ret < 0)
718 		goto join_fail;
719 
720 	cur_trans = fs_info->running_transaction;
721 
722 	h->transid = cur_trans->transid;
723 	h->transaction = cur_trans;
724 	refcount_set(&h->use_count, 1);
725 	h->fs_info = root->fs_info;
726 
727 	h->type = type;
728 	INIT_LIST_HEAD(&h->new_bgs);
729 	btrfs_init_metadata_block_rsv(fs_info, &h->delayed_rsv, BTRFS_BLOCK_RSV_DELOPS);
730 
731 	smp_mb();
732 	if (cur_trans->state >= TRANS_STATE_COMMIT_START &&
733 	    may_wait_transaction(fs_info, type)) {
734 		current->journal_info = h;
735 		btrfs_commit_transaction(h);
736 		goto again;
737 	}
738 
739 	if (num_bytes) {
740 		trace_btrfs_space_reservation(fs_info, "transaction",
741 					      h->transid, num_bytes, 1);
742 		h->block_rsv = trans_rsv;
743 		h->bytes_reserved = num_bytes;
744 		if (delayed_refs_bytes > 0) {
745 			trace_btrfs_space_reservation(fs_info,
746 						      "local_delayed_refs_rsv",
747 						      h->transid,
748 						      delayed_refs_bytes, 1);
749 			h->delayed_refs_bytes_reserved = delayed_refs_bytes;
750 			btrfs_block_rsv_add_bytes(&h->delayed_rsv, delayed_refs_bytes, true);
751 			delayed_refs_bytes = 0;
752 		}
753 		h->reloc_reserved = reloc_reserved;
754 	}
755 
756 got_it:
757 	if (!current->journal_info)
758 		current->journal_info = h;
759 
760 	/*
761 	 * If the space_info is marked ALLOC_FORCE then we'll get upgraded to
762 	 * ALLOC_FORCE the first run through, and then we won't allocate for
763 	 * anybody else who races in later.  We don't care about the return
764 	 * value here.
765 	 */
766 	if (do_chunk_alloc && num_bytes) {
767 		struct btrfs_space_info *space_info = h->block_rsv->space_info;
768 		u64 flags = space_info->flags;
769 
770 		btrfs_chunk_alloc(h, space_info, btrfs_get_alloc_profile(fs_info, flags),
771 				  CHUNK_ALLOC_NO_FORCE);
772 	}
773 
774 	/*
775 	 * btrfs_record_root_in_trans() needs to alloc new extents, and may
776 	 * call btrfs_join_transaction() while we're also starting a
777 	 * transaction.
778 	 *
779 	 * Thus it need to be called after current->journal_info initialized,
780 	 * or we can deadlock.
781 	 */
782 	ret = btrfs_record_root_in_trans(h, root);
783 	if (ret) {
784 		/*
785 		 * The transaction handle is fully initialized and linked with
786 		 * other structures so it needs to be ended in case of errors,
787 		 * not just freed.
788 		 */
789 		btrfs_end_transaction(h);
790 		goto reserve_fail;
791 	}
792 	/*
793 	 * Now that we have found a transaction to be a part of, convert the
794 	 * qgroup reservation from prealloc to pertrans. A different transaction
795 	 * can't race in and free our pertrans out from under us.
796 	 */
797 	if (qgroup_reserved)
798 		btrfs_qgroup_convert_reserved_meta(root, qgroup_reserved);
799 
800 	return h;
801 
802 join_fail:
803 	if (type & __TRANS_FREEZABLE)
804 		sb_end_intwrite(fs_info->sb);
805 	kmem_cache_free(btrfs_trans_handle_cachep, h);
806 alloc_fail:
807 	if (num_bytes)
808 		btrfs_block_rsv_release(fs_info, trans_rsv, num_bytes, NULL);
809 	if (delayed_refs_bytes)
810 		btrfs_space_info_free_bytes_may_use(trans_rsv->space_info, delayed_refs_bytes);
811 reserve_fail:
812 	btrfs_qgroup_free_meta_prealloc(root, qgroup_reserved);
813 	return ERR_PTR(ret);
814 }
815 
btrfs_start_transaction(struct btrfs_root * root,unsigned int num_items)816 struct btrfs_trans_handle *btrfs_start_transaction(struct btrfs_root *root,
817 						   unsigned int num_items)
818 {
819 	return start_transaction(root, num_items, TRANS_START,
820 				 BTRFS_RESERVE_FLUSH_ALL, true);
821 }
822 
btrfs_start_transaction_fallback_global_rsv(struct btrfs_root * root,unsigned int num_items)823 struct btrfs_trans_handle *btrfs_start_transaction_fallback_global_rsv(
824 					struct btrfs_root *root,
825 					unsigned int num_items)
826 {
827 	return start_transaction(root, num_items, TRANS_START,
828 				 BTRFS_RESERVE_FLUSH_ALL_STEAL, false);
829 }
830 
btrfs_join_transaction(struct btrfs_root * root)831 struct btrfs_trans_handle *btrfs_join_transaction(struct btrfs_root *root)
832 {
833 	return start_transaction(root, 0, TRANS_JOIN, BTRFS_RESERVE_NO_FLUSH,
834 				 true);
835 }
836 
btrfs_join_transaction_spacecache(struct btrfs_root * root)837 struct btrfs_trans_handle *btrfs_join_transaction_spacecache(struct btrfs_root *root)
838 {
839 	return start_transaction(root, 0, TRANS_JOIN_NOLOCK,
840 				 BTRFS_RESERVE_NO_FLUSH, true);
841 }
842 
843 /*
844  * Similar to regular join but it never starts a transaction when none is
845  * running or when there's a running one at a state >= TRANS_STATE_UNBLOCKED.
846  * This is similar to btrfs_attach_transaction() but it allows the join to
847  * happen if the transaction commit already started but it's not yet in the
848  * "doing" phase (the state is < TRANS_STATE_COMMIT_DOING).
849  */
btrfs_join_transaction_nostart(struct btrfs_root * root)850 struct btrfs_trans_handle *btrfs_join_transaction_nostart(struct btrfs_root *root)
851 {
852 	return start_transaction(root, 0, TRANS_JOIN_NOSTART,
853 				 BTRFS_RESERVE_NO_FLUSH, true);
854 }
855 
856 /*
857  * Catch the running transaction.
858  *
859  * It is used when we want to commit the current the transaction, but
860  * don't want to start a new one.
861  *
862  * Note: If this function return -ENOENT, it just means there is no
863  * running transaction. But it is possible that the inactive transaction
864  * is still in the memory, not fully on disk. If you hope there is no
865  * inactive transaction in the fs when -ENOENT is returned, you should
866  * invoke
867  *     btrfs_attach_transaction_barrier()
868  */
btrfs_attach_transaction(struct btrfs_root * root)869 struct btrfs_trans_handle *btrfs_attach_transaction(struct btrfs_root *root)
870 {
871 	return start_transaction(root, 0, TRANS_ATTACH,
872 				 BTRFS_RESERVE_NO_FLUSH, true);
873 }
874 
875 /*
876  * Catch the running transaction.
877  *
878  * It is similar to the above function, the difference is this one
879  * will wait for all the inactive transactions until they fully
880  * complete.
881  */
882 struct btrfs_trans_handle *
btrfs_attach_transaction_barrier(struct btrfs_root * root)883 btrfs_attach_transaction_barrier(struct btrfs_root *root)
884 {
885 	struct btrfs_trans_handle *trans;
886 
887 	trans = start_transaction(root, 0, TRANS_ATTACH,
888 				  BTRFS_RESERVE_NO_FLUSH, true);
889 	if (trans == ERR_PTR(-ENOENT)) {
890 		int ret;
891 
892 		ret = btrfs_wait_for_commit(root->fs_info, 0);
893 		if (ret)
894 			return ERR_PTR(ret);
895 	}
896 
897 	return trans;
898 }
899 
900 /* Wait for a transaction commit to reach at least the given state. */
wait_for_commit(struct btrfs_transaction * commit,const enum btrfs_trans_state min_state)901 static noinline void wait_for_commit(struct btrfs_transaction *commit,
902 				     const enum btrfs_trans_state min_state)
903 {
904 	struct btrfs_fs_info *fs_info = commit->fs_info;
905 	u64 transid = commit->transid;
906 	bool put = false;
907 
908 	/*
909 	 * At the moment this function is called with min_state either being
910 	 * TRANS_STATE_COMPLETED or TRANS_STATE_SUPER_COMMITTED.
911 	 */
912 	if (min_state == TRANS_STATE_COMPLETED)
913 		btrfs_might_wait_for_state(fs_info, BTRFS_LOCKDEP_TRANS_COMPLETED);
914 	else
915 		btrfs_might_wait_for_state(fs_info, BTRFS_LOCKDEP_TRANS_SUPER_COMMITTED);
916 
917 	while (1) {
918 		wait_event(commit->commit_wait, commit->state >= min_state);
919 		if (put)
920 			btrfs_put_transaction(commit);
921 
922 		if (min_state < TRANS_STATE_COMPLETED)
923 			break;
924 
925 		/*
926 		 * A transaction isn't really completed until all of the
927 		 * previous transactions are completed, but with fsync we can
928 		 * end up with SUPER_COMMITTED transactions before a COMPLETED
929 		 * transaction. Wait for those.
930 		 */
931 
932 		spin_lock(&fs_info->trans_lock);
933 		commit = list_first_entry_or_null(&fs_info->trans_list,
934 						  struct btrfs_transaction,
935 						  list);
936 		if (!commit || commit->transid > transid) {
937 			spin_unlock(&fs_info->trans_lock);
938 			break;
939 		}
940 		refcount_inc(&commit->use_count);
941 		put = true;
942 		spin_unlock(&fs_info->trans_lock);
943 	}
944 }
945 
btrfs_wait_for_commit(struct btrfs_fs_info * fs_info,u64 transid)946 int btrfs_wait_for_commit(struct btrfs_fs_info *fs_info, u64 transid)
947 {
948 	struct btrfs_transaction *cur_trans = NULL, *t;
949 	int ret = 0;
950 
951 	if (transid) {
952 		if (transid <= btrfs_get_last_trans_committed(fs_info))
953 			goto out;
954 
955 		/* find specified transaction */
956 		spin_lock(&fs_info->trans_lock);
957 		list_for_each_entry(t, &fs_info->trans_list, list) {
958 			if (t->transid == transid) {
959 				cur_trans = t;
960 				refcount_inc(&cur_trans->use_count);
961 				ret = 0;
962 				break;
963 			}
964 			if (t->transid > transid) {
965 				ret = 0;
966 				break;
967 			}
968 		}
969 		spin_unlock(&fs_info->trans_lock);
970 
971 		/*
972 		 * The specified transaction doesn't exist, or we
973 		 * raced with btrfs_commit_transaction
974 		 */
975 		if (!cur_trans) {
976 			if (transid > btrfs_get_last_trans_committed(fs_info))
977 				ret = -EINVAL;
978 			goto out;
979 		}
980 	} else {
981 		/* find newest transaction that is committing | committed */
982 		spin_lock(&fs_info->trans_lock);
983 		list_for_each_entry_reverse(t, &fs_info->trans_list,
984 					    list) {
985 			if (t->state >= TRANS_STATE_COMMIT_START) {
986 				if (t->state == TRANS_STATE_COMPLETED)
987 					break;
988 				cur_trans = t;
989 				refcount_inc(&cur_trans->use_count);
990 				break;
991 			}
992 		}
993 		spin_unlock(&fs_info->trans_lock);
994 		if (!cur_trans)
995 			goto out;  /* nothing committing|committed */
996 	}
997 
998 	wait_for_commit(cur_trans, TRANS_STATE_COMPLETED);
999 	ret = cur_trans->aborted;
1000 	btrfs_put_transaction(cur_trans);
1001 out:
1002 	return ret;
1003 }
1004 
btrfs_throttle(struct btrfs_fs_info * fs_info)1005 void btrfs_throttle(struct btrfs_fs_info *fs_info)
1006 {
1007 	wait_current_trans(fs_info, TRANS_START);
1008 }
1009 
btrfs_should_end_transaction(struct btrfs_trans_handle * trans)1010 bool btrfs_should_end_transaction(struct btrfs_trans_handle *trans)
1011 {
1012 	struct btrfs_transaction *cur_trans = trans->transaction;
1013 
1014 	if (cur_trans->state >= TRANS_STATE_COMMIT_START ||
1015 	    test_bit(BTRFS_DELAYED_REFS_FLUSHING, &cur_trans->delayed_refs.flags))
1016 		return true;
1017 
1018 	if (btrfs_check_space_for_delayed_refs(trans->fs_info))
1019 		return true;
1020 
1021 	return !!btrfs_block_rsv_check(&trans->fs_info->global_block_rsv, 50);
1022 }
1023 
btrfs_trans_release_metadata(struct btrfs_trans_handle * trans)1024 static void btrfs_trans_release_metadata(struct btrfs_trans_handle *trans)
1025 
1026 {
1027 	struct btrfs_fs_info *fs_info = trans->fs_info;
1028 
1029 	if (!trans->block_rsv) {
1030 		ASSERT(trans->bytes_reserved == 0,
1031 		       "trans->bytes_reserved=%llu", trans->bytes_reserved);
1032 		ASSERT(trans->delayed_refs_bytes_reserved == 0,
1033 		       "trans->delayed_refs_bytes_reserved=%llu",
1034 		       trans->delayed_refs_bytes_reserved);
1035 		return;
1036 	}
1037 
1038 	if (!trans->bytes_reserved) {
1039 		ASSERT(trans->delayed_refs_bytes_reserved == 0,
1040 		       "trans->delayed_refs_bytes_reserved=%llu",
1041 		       trans->delayed_refs_bytes_reserved);
1042 		return;
1043 	}
1044 
1045 	ASSERT(trans->block_rsv == &fs_info->trans_block_rsv);
1046 	trace_btrfs_space_reservation(fs_info, "transaction",
1047 				      trans->transid, trans->bytes_reserved, 0);
1048 	btrfs_block_rsv_release(fs_info, trans->block_rsv,
1049 				trans->bytes_reserved, NULL);
1050 	trans->bytes_reserved = 0;
1051 
1052 	if (!trans->delayed_refs_bytes_reserved)
1053 		return;
1054 
1055 	trace_btrfs_space_reservation(fs_info, "local_delayed_refs_rsv",
1056 				      trans->transid,
1057 				      trans->delayed_refs_bytes_reserved, 0);
1058 	btrfs_block_rsv_release(fs_info, &trans->delayed_rsv,
1059 				trans->delayed_refs_bytes_reserved, NULL);
1060 	trans->delayed_refs_bytes_reserved = 0;
1061 }
1062 
__btrfs_end_transaction(struct btrfs_trans_handle * trans,int throttle)1063 static int __btrfs_end_transaction(struct btrfs_trans_handle *trans,
1064 				   int throttle)
1065 {
1066 	struct btrfs_fs_info *info = trans->fs_info;
1067 	struct btrfs_transaction *cur_trans = trans->transaction;
1068 	int ret = 0;
1069 
1070 	if (refcount_read(&trans->use_count) > 1) {
1071 		refcount_dec(&trans->use_count);
1072 		trans->block_rsv = trans->orig_rsv;
1073 		return 0;
1074 	}
1075 
1076 	btrfs_trans_release_metadata(trans);
1077 	trans->block_rsv = NULL;
1078 
1079 	btrfs_create_pending_block_groups(trans);
1080 
1081 	btrfs_trans_release_chunk_metadata(trans);
1082 
1083 	if (trans->type & __TRANS_FREEZABLE)
1084 		sb_end_intwrite(info->sb);
1085 
1086 	WARN_ON(cur_trans != info->running_transaction);
1087 	WARN_ON(atomic_read(&cur_trans->num_writers) < 1);
1088 	atomic_dec(&cur_trans->num_writers);
1089 	extwriter_counter_dec(cur_trans, trans->type);
1090 
1091 	cond_wake_up(&cur_trans->writer_wait);
1092 
1093 	btrfs_lockdep_release(info, btrfs_trans_num_extwriters);
1094 	btrfs_lockdep_release(info, btrfs_trans_num_writers);
1095 
1096 	btrfs_put_transaction(cur_trans);
1097 
1098 	if (current->journal_info == trans)
1099 		current->journal_info = NULL;
1100 
1101 	if (throttle)
1102 		btrfs_run_delayed_iputs(info);
1103 
1104 	if (TRANS_ABORTED(trans) || BTRFS_FS_ERROR(info)) {
1105 		wake_up_process(info->transaction_kthread);
1106 		if (TRANS_ABORTED(trans))
1107 			ret = trans->aborted;
1108 		else
1109 			ret = -EROFS;
1110 	}
1111 
1112 	kmem_cache_free(btrfs_trans_handle_cachep, trans);
1113 	return ret;
1114 }
1115 
btrfs_end_transaction(struct btrfs_trans_handle * trans)1116 int btrfs_end_transaction(struct btrfs_trans_handle *trans)
1117 {
1118 	return __btrfs_end_transaction(trans, 0);
1119 }
1120 
btrfs_end_transaction_throttle(struct btrfs_trans_handle * trans)1121 int btrfs_end_transaction_throttle(struct btrfs_trans_handle *trans)
1122 {
1123 	return __btrfs_end_transaction(trans, 1);
1124 }
1125 
1126 /*
1127  * when btree blocks are allocated, they have some corresponding bits set for
1128  * them in one of two extent_io trees.  This is used to make sure all of
1129  * those extents are sent to disk but does not wait on them
1130  */
btrfs_write_marked_extents(struct btrfs_fs_info * fs_info,struct extent_io_tree * dirty_pages,int mark)1131 int btrfs_write_marked_extents(struct btrfs_fs_info *fs_info,
1132 			       struct extent_io_tree *dirty_pages, int mark)
1133 {
1134 	int ret = 0;
1135 	struct address_space *mapping = fs_info->btree_inode->i_mapping;
1136 	struct extent_state *cached_state = NULL;
1137 	u64 start = 0;
1138 	u64 end;
1139 
1140 	while (btrfs_find_first_extent_bit(dirty_pages, start, &start, &end,
1141 					   mark, &cached_state)) {
1142 		bool wait_writeback = false;
1143 
1144 		ret = btrfs_convert_extent_bit(dirty_pages, start, end,
1145 					       EXTENT_NEED_WAIT,
1146 					       mark, &cached_state);
1147 		/*
1148 		 * convert_extent_bit can return -ENOMEM, which is most of the
1149 		 * time a temporary error. So when it happens, ignore the error
1150 		 * and wait for writeback of this range to finish - because we
1151 		 * failed to set the bit EXTENT_NEED_WAIT for the range, a call
1152 		 * to __btrfs_wait_marked_extents() would not know that
1153 		 * writeback for this range started and therefore wouldn't
1154 		 * wait for it to finish - we don't want to commit a
1155 		 * superblock that points to btree nodes/leafs for which
1156 		 * writeback hasn't finished yet (and without errors).
1157 		 * We cleanup any entries left in the io tree when committing
1158 		 * the transaction (through extent_io_tree_release()).
1159 		 */
1160 		if (ret == -ENOMEM) {
1161 			ret = 0;
1162 			wait_writeback = true;
1163 		}
1164 		if (!ret)
1165 			ret = filemap_fdatawrite_range(mapping, start, end);
1166 		if (!ret && wait_writeback)
1167 			btrfs_btree_wait_writeback_range(fs_info, start, end);
1168 		btrfs_free_extent_state(cached_state);
1169 		if (ret)
1170 			break;
1171 		cached_state = NULL;
1172 		cond_resched();
1173 		start = end + 1;
1174 	}
1175 	return ret;
1176 }
1177 
1178 /*
1179  * when btree blocks are allocated, they have some corresponding bits set for
1180  * them in one of two extent_io trees.  This is used to make sure all of
1181  * those extents are on disk for transaction or log commit.  We wait
1182  * on all the pages and clear them from the dirty pages state tree
1183  */
__btrfs_wait_marked_extents(struct btrfs_fs_info * fs_info,struct extent_io_tree * dirty_pages)1184 static int __btrfs_wait_marked_extents(struct btrfs_fs_info *fs_info,
1185 				       struct extent_io_tree *dirty_pages)
1186 {
1187 	struct extent_state *cached_state = NULL;
1188 	u64 start = 0;
1189 	u64 end;
1190 	int ret = 0;
1191 
1192 	while (btrfs_find_first_extent_bit(dirty_pages, start, &start, &end,
1193 					   EXTENT_NEED_WAIT, &cached_state)) {
1194 		/*
1195 		 * Ignore -ENOMEM errors returned by clear_extent_bit().
1196 		 * When committing the transaction, we'll remove any entries
1197 		 * left in the io tree. For a log commit, we don't remove them
1198 		 * after committing the log because the tree can be accessed
1199 		 * concurrently - we do it only at transaction commit time when
1200 		 * it's safe to do it (through extent_io_tree_release()).
1201 		 */
1202 		ret = btrfs_clear_extent_bit(dirty_pages, start, end,
1203 					     EXTENT_NEED_WAIT, &cached_state);
1204 		if (ret == -ENOMEM)
1205 			ret = 0;
1206 		if (!ret)
1207 			btrfs_btree_wait_writeback_range(fs_info, start, end);
1208 		btrfs_free_extent_state(cached_state);
1209 		if (ret)
1210 			break;
1211 		cached_state = NULL;
1212 		cond_resched();
1213 		start = end + 1;
1214 	}
1215 	return ret;
1216 }
1217 
btrfs_wait_extents(struct btrfs_fs_info * fs_info,struct extent_io_tree * dirty_pages)1218 static int btrfs_wait_extents(struct btrfs_fs_info *fs_info,
1219 		       struct extent_io_tree *dirty_pages)
1220 {
1221 	bool errors = false;
1222 	int ret;
1223 
1224 	ret = __btrfs_wait_marked_extents(fs_info, dirty_pages);
1225 	if (test_and_clear_bit(BTRFS_FS_BTREE_ERR, &fs_info->flags))
1226 		errors = true;
1227 
1228 	if (errors && !ret)
1229 		ret = -EIO;
1230 	return ret;
1231 }
1232 
btrfs_wait_tree_log_extents(struct btrfs_root * log_root,int mark)1233 int btrfs_wait_tree_log_extents(struct btrfs_root *log_root, int mark)
1234 {
1235 	struct btrfs_fs_info *fs_info = log_root->fs_info;
1236 	struct extent_io_tree *dirty_pages = &log_root->dirty_log_pages;
1237 	bool errors = false;
1238 	int ret;
1239 
1240 	ASSERT(btrfs_root_id(log_root) == BTRFS_TREE_LOG_OBJECTID,
1241 	       "root_id(log_root)=%llu", btrfs_root_id(log_root));
1242 
1243 	ret = __btrfs_wait_marked_extents(fs_info, dirty_pages);
1244 	if ((mark & EXTENT_DIRTY_LOG1) &&
1245 	    test_and_clear_bit(BTRFS_FS_LOG1_ERR, &fs_info->flags))
1246 		errors = true;
1247 
1248 	if ((mark & EXTENT_DIRTY_LOG2) &&
1249 	    test_and_clear_bit(BTRFS_FS_LOG2_ERR, &fs_info->flags))
1250 		errors = true;
1251 
1252 	if (errors && !ret)
1253 		ret = -EIO;
1254 	return ret;
1255 }
1256 
1257 /*
1258  * When btree blocks are allocated the corresponding extents are marked dirty.
1259  * This function ensures such extents are persisted on disk for transaction or
1260  * log commit.
1261  *
1262  * @trans: transaction whose dirty pages we'd like to write
1263  */
btrfs_write_and_wait_transaction(struct btrfs_trans_handle * trans)1264 static int btrfs_write_and_wait_transaction(struct btrfs_trans_handle *trans)
1265 {
1266 	int ret;
1267 	int ret2;
1268 	struct extent_io_tree *dirty_pages = &trans->transaction->dirty_pages;
1269 	struct btrfs_fs_info *fs_info = trans->fs_info;
1270 	struct blk_plug plug;
1271 
1272 	blk_start_plug(&plug);
1273 	ret = btrfs_write_marked_extents(fs_info, dirty_pages, EXTENT_DIRTY);
1274 	blk_finish_plug(&plug);
1275 	ret2 = btrfs_wait_extents(fs_info, dirty_pages);
1276 
1277 	btrfs_extent_io_tree_release(&trans->transaction->dirty_pages);
1278 
1279 	if (ret)
1280 		return ret;
1281 	else if (ret2)
1282 		return ret2;
1283 	else
1284 		return 0;
1285 }
1286 
1287 /*
1288  * this is used to update the root pointer in the tree of tree roots.
1289  *
1290  * But, in the case of the extent allocation tree, updating the root
1291  * pointer may allocate blocks which may change the root of the extent
1292  * allocation tree.
1293  *
1294  * So, this loops and repeats and makes sure the cowonly root didn't
1295  * change while the root pointer was being updated in the metadata.
1296  */
update_cowonly_root(struct btrfs_trans_handle * trans,struct btrfs_root * root)1297 static int update_cowonly_root(struct btrfs_trans_handle *trans,
1298 			       struct btrfs_root *root)
1299 {
1300 	int ret;
1301 	u64 old_root_bytenr;
1302 	u64 old_root_used;
1303 	struct btrfs_fs_info *fs_info = root->fs_info;
1304 	struct btrfs_root *tree_root = fs_info->tree_root;
1305 
1306 	old_root_used = btrfs_root_used(&root->root_item);
1307 
1308 	while (1) {
1309 		old_root_bytenr = btrfs_root_bytenr(&root->root_item);
1310 		if (old_root_bytenr == root->node->start &&
1311 		    old_root_used == btrfs_root_used(&root->root_item))
1312 			break;
1313 
1314 		btrfs_set_root_node(&root->root_item, root->node);
1315 		ret = btrfs_update_root(trans, tree_root,
1316 					&root->root_key,
1317 					&root->root_item);
1318 		if (ret)
1319 			return ret;
1320 
1321 		old_root_used = btrfs_root_used(&root->root_item);
1322 	}
1323 
1324 	return 0;
1325 }
1326 
1327 /*
1328  * update all the cowonly tree roots on disk
1329  *
1330  * The error handling in this function may not be obvious. Any of the
1331  * failures will cause the file system to go offline. We still need
1332  * to clean up the delayed refs.
1333  */
commit_cowonly_roots(struct btrfs_trans_handle * trans)1334 static noinline int commit_cowonly_roots(struct btrfs_trans_handle *trans)
1335 {
1336 	struct btrfs_fs_info *fs_info = trans->fs_info;
1337 	struct list_head *dirty_bgs = &trans->transaction->dirty_bgs;
1338 	struct list_head *io_bgs = &trans->transaction->io_bgs;
1339 	struct extent_buffer *eb;
1340 	int ret;
1341 
1342 	/*
1343 	 * At this point no one can be using this transaction to modify any tree
1344 	 * and no one can start another transaction to modify any tree either.
1345 	 */
1346 	ASSERT(trans->transaction->state == TRANS_STATE_COMMIT_DOING,
1347 	       "trans->transaction->state=%d", trans->transaction->state);
1348 
1349 	eb = btrfs_lock_root_node(fs_info->tree_root);
1350 	ret = btrfs_cow_block(trans, fs_info->tree_root, eb, NULL,
1351 			      0, &eb, BTRFS_NESTING_COW);
1352 	btrfs_tree_unlock(eb);
1353 	free_extent_buffer(eb);
1354 
1355 	if (ret)
1356 		return ret;
1357 
1358 	ret = btrfs_run_dev_stats(trans);
1359 	if (ret)
1360 		return ret;
1361 	ret = btrfs_run_dev_replace(trans);
1362 	if (ret)
1363 		return ret;
1364 	ret = btrfs_run_qgroups(trans);
1365 	if (ret)
1366 		return ret;
1367 
1368 	ret = btrfs_setup_space_cache(trans);
1369 	if (ret)
1370 		return ret;
1371 
1372 again:
1373 	while (!list_empty(&fs_info->dirty_cowonly_roots)) {
1374 		struct btrfs_root *root;
1375 
1376 		root = list_first_entry(&fs_info->dirty_cowonly_roots,
1377 					struct btrfs_root, dirty_list);
1378 		clear_bit(BTRFS_ROOT_DIRTY, &root->state);
1379 		list_move_tail(&root->dirty_list,
1380 			       &trans->transaction->switch_commits);
1381 
1382 		ret = update_cowonly_root(trans, root);
1383 		if (ret)
1384 			return ret;
1385 	}
1386 
1387 	/* Now flush any delayed refs generated by updating all of the roots */
1388 	ret = btrfs_run_delayed_refs(trans, U64_MAX);
1389 	if (ret)
1390 		return ret;
1391 
1392 	while (!list_empty(dirty_bgs) || !list_empty(io_bgs)) {
1393 		ret = btrfs_write_dirty_block_groups(trans);
1394 		if (ret)
1395 			return ret;
1396 
1397 		/*
1398 		 * We're writing the dirty block groups, which could generate
1399 		 * delayed refs, which could generate more dirty block groups,
1400 		 * so we want to keep this flushing in this loop to make sure
1401 		 * everything gets run.
1402 		 */
1403 		ret = btrfs_run_delayed_refs(trans, U64_MAX);
1404 		if (ret)
1405 			return ret;
1406 	}
1407 
1408 	if (!list_empty(&fs_info->dirty_cowonly_roots))
1409 		goto again;
1410 
1411 	/* Update dev-replace pointer once everything is committed */
1412 	fs_info->dev_replace.committed_cursor_left =
1413 		fs_info->dev_replace.cursor_left_last_write_of_item;
1414 
1415 	return 0;
1416 }
1417 
1418 /*
1419  * If we had a pending drop we need to see if there are any others left in our
1420  * dead roots list, and if not clear our bit and wake any waiters.
1421  */
btrfs_maybe_wake_unfinished_drop(struct btrfs_fs_info * fs_info)1422 void btrfs_maybe_wake_unfinished_drop(struct btrfs_fs_info *fs_info)
1423 {
1424 	/*
1425 	 * We put the drop in progress roots at the front of the list, so if the
1426 	 * first entry doesn't have UNFINISHED_DROP set we can wake everybody
1427 	 * up.
1428 	 */
1429 	spin_lock(&fs_info->trans_lock);
1430 	if (!list_empty(&fs_info->dead_roots)) {
1431 		struct btrfs_root *root = list_first_entry(&fs_info->dead_roots,
1432 							   struct btrfs_root,
1433 							   root_list);
1434 		if (test_bit(BTRFS_ROOT_UNFINISHED_DROP, &root->state)) {
1435 			spin_unlock(&fs_info->trans_lock);
1436 			return;
1437 		}
1438 	}
1439 	spin_unlock(&fs_info->trans_lock);
1440 
1441 	btrfs_wake_unfinished_drop(fs_info);
1442 }
1443 
1444 /*
1445  * dead roots are old snapshots that need to be deleted.  This allocates
1446  * a dirty root struct and adds it into the list of dead roots that need to
1447  * be deleted
1448  */
btrfs_add_dead_root(struct btrfs_root * root)1449 void btrfs_add_dead_root(struct btrfs_root *root)
1450 {
1451 	struct btrfs_fs_info *fs_info = root->fs_info;
1452 
1453 	spin_lock(&fs_info->trans_lock);
1454 	if (list_empty(&root->root_list)) {
1455 		btrfs_grab_root(root);
1456 
1457 		/* We want to process the partially complete drops first. */
1458 		if (test_bit(BTRFS_ROOT_UNFINISHED_DROP, &root->state))
1459 			list_add(&root->root_list, &fs_info->dead_roots);
1460 		else
1461 			list_add_tail(&root->root_list, &fs_info->dead_roots);
1462 	}
1463 	spin_unlock(&fs_info->trans_lock);
1464 }
1465 
1466 /*
1467  * Update each subvolume root and its relocation root, if it exists, in the tree
1468  * of tree roots. Also free log roots if they exist.
1469  */
commit_fs_roots(struct btrfs_trans_handle * trans)1470 static noinline int commit_fs_roots(struct btrfs_trans_handle *trans)
1471 {
1472 	struct btrfs_fs_info *fs_info = trans->fs_info;
1473 	struct btrfs_root *gang[8];
1474 	int i;
1475 	int ret;
1476 
1477 	/*
1478 	 * At this point no one can be using this transaction to modify any tree
1479 	 * and no one can start another transaction to modify any tree either.
1480 	 */
1481 	ASSERT(trans->transaction->state == TRANS_STATE_COMMIT_DOING,
1482 	       "trans->transaction->state=%d", trans->transaction->state);
1483 
1484 	spin_lock(&fs_info->fs_roots_radix_lock);
1485 	while (1) {
1486 		ret = radix_tree_gang_lookup_tag(&fs_info->fs_roots_radix,
1487 						 (void **)gang, 0,
1488 						 ARRAY_SIZE(gang),
1489 						 BTRFS_ROOT_TRANS_TAG);
1490 		if (ret == 0)
1491 			break;
1492 		for (i = 0; i < ret; i++) {
1493 			struct btrfs_root *root = gang[i];
1494 			int ret2;
1495 
1496 			/*
1497 			 * At this point we can neither have tasks logging inodes
1498 			 * from a root nor trying to commit a log tree.
1499 			 */
1500 			ASSERT(atomic_read(&root->log_writers) == 0,
1501 			       "atomic_read(&root->log_writers)=%d",
1502 			       atomic_read(&root->log_writers));
1503 			ASSERT(atomic_read(&root->log_commit[0]) == 0,
1504 			       "atomic_read(&root->log_commit[0])=%d",
1505 			       atomic_read(&root->log_commit[0]));
1506 			ASSERT(atomic_read(&root->log_commit[1]) == 0,
1507 			       "atomic_read(&root->log_commit[1])=%d",
1508 			       atomic_read(&root->log_commit[1]));
1509 
1510 			radix_tree_tag_clear(&fs_info->fs_roots_radix,
1511 					(unsigned long)btrfs_root_id(root),
1512 					BTRFS_ROOT_TRANS_TAG);
1513 			btrfs_qgroup_free_meta_all_pertrans(root);
1514 			spin_unlock(&fs_info->fs_roots_radix_lock);
1515 
1516 			btrfs_free_log(trans, root);
1517 			ret2 = btrfs_update_reloc_root(trans, root);
1518 			if (ret2)
1519 				return ret2;
1520 
1521 			/* see comments in should_cow_block() */
1522 			clear_bit(BTRFS_ROOT_FORCE_COW, &root->state);
1523 			smp_mb__after_atomic();
1524 
1525 			if (root->commit_root != root->node) {
1526 				list_add_tail(&root->dirty_list,
1527 					&trans->transaction->switch_commits);
1528 				btrfs_set_root_node(&root->root_item,
1529 						    root->node);
1530 			}
1531 
1532 			ret2 = btrfs_update_root(trans, fs_info->tree_root,
1533 						&root->root_key,
1534 						&root->root_item);
1535 			if (ret2)
1536 				return ret2;
1537 			spin_lock(&fs_info->fs_roots_radix_lock);
1538 		}
1539 	}
1540 	spin_unlock(&fs_info->fs_roots_radix_lock);
1541 	return 0;
1542 }
1543 
1544 /*
1545  * Do all special snapshot related qgroup dirty hack.
1546  *
1547  * Will do all needed qgroup inherit and dirty hack like switch commit
1548  * roots inside one transaction and write all btree into disk, to make
1549  * qgroup works.
1550  */
qgroup_account_snapshot(struct btrfs_trans_handle * trans,struct btrfs_root * src,struct btrfs_root * parent,struct btrfs_qgroup_inherit * inherit,u64 dst_objectid)1551 static int qgroup_account_snapshot(struct btrfs_trans_handle *trans,
1552 				   struct btrfs_root *src,
1553 				   struct btrfs_root *parent,
1554 				   struct btrfs_qgroup_inherit *inherit,
1555 				   u64 dst_objectid)
1556 {
1557 	struct btrfs_fs_info *fs_info = src->fs_info;
1558 	int ret;
1559 
1560 	/*
1561 	 * Save some performance in the case that qgroups are not enabled. If
1562 	 * this check races with the ioctl, rescan will kick in anyway.
1563 	 */
1564 	if (!btrfs_qgroup_full_accounting(fs_info))
1565 		return 0;
1566 
1567 	/*
1568 	 * Ensure dirty @src will be committed.  Or, after coming
1569 	 * commit_fs_roots() and switch_commit_roots(), any dirty but not
1570 	 * recorded root will never be updated again, causing an outdated root
1571 	 * item.
1572 	 */
1573 	ret = record_root_in_trans(trans, src, 1);
1574 	if (ret)
1575 		return ret;
1576 
1577 	/*
1578 	 * btrfs_qgroup_inherit relies on a consistent view of the usage for the
1579 	 * src root, so we must run the delayed refs here.
1580 	 *
1581 	 * However this isn't particularly fool proof, because there's no
1582 	 * synchronization keeping us from changing the tree after this point
1583 	 * before we do the qgroup_inherit, or even from making changes while
1584 	 * we're doing the qgroup_inherit.  But that's a problem for the future,
1585 	 * for now flush the delayed refs to narrow the race window where the
1586 	 * qgroup counters could end up wrong.
1587 	 */
1588 	ret = btrfs_run_delayed_refs(trans, U64_MAX);
1589 	if (unlikely(ret)) {
1590 		btrfs_abort_transaction(trans, ret);
1591 		return ret;
1592 	}
1593 
1594 	ret = commit_fs_roots(trans);
1595 	if (ret)
1596 		goto out;
1597 	ret = btrfs_qgroup_account_extents(trans);
1598 	if (ret < 0)
1599 		goto out;
1600 
1601 	/* Now qgroup are all updated, we can inherit it to new qgroups */
1602 	ret = btrfs_qgroup_inherit(trans, btrfs_root_id(src), dst_objectid,
1603 				   btrfs_root_id(parent), inherit);
1604 	if (ret < 0)
1605 		goto out;
1606 
1607 	/*
1608 	 * Now we do a simplified commit transaction, which will:
1609 	 * 1) commit all subvolume and extent tree
1610 	 *    To ensure all subvolume and extent tree have a valid
1611 	 *    commit_root to accounting later insert_dir_item()
1612 	 * 2) write all btree blocks onto disk
1613 	 *    This is to make sure later btree modification will be cowed
1614 	 *    Or commit_root can be populated and cause wrong qgroup numbers
1615 	 * In this simplified commit, we don't really care about other trees
1616 	 * like chunk and root tree, as they won't affect qgroup.
1617 	 * And we don't write super to avoid half committed status.
1618 	 */
1619 	ret = commit_cowonly_roots(trans);
1620 	if (ret)
1621 		goto out;
1622 	switch_commit_roots(trans);
1623 	ret = btrfs_write_and_wait_transaction(trans);
1624 	if (ret)
1625 		btrfs_handle_fs_error(fs_info, ret,
1626 			"Error while writing out transaction for qgroup");
1627 
1628 out:
1629 	/*
1630 	 * Force parent root to be updated, as we recorded it before so its
1631 	 * last_trans == cur_transid.
1632 	 * Or it won't be committed again onto disk after later
1633 	 * insert_dir_item()
1634 	 */
1635 	if (!ret)
1636 		ret = record_root_in_trans(trans, parent, 1);
1637 	return ret;
1638 }
1639 
1640 /*
1641  * new snapshots need to be created at a very specific time in the
1642  * transaction commit.  This does the actual creation.
1643  *
1644  * Note:
1645  * If the error which may affect the commitment of the current transaction
1646  * happens, we should return the error number. If the error which just affect
1647  * the creation of the pending snapshots, just return 0.
1648  */
create_pending_snapshot(struct btrfs_trans_handle * trans,struct btrfs_pending_snapshot * pending)1649 static noinline int create_pending_snapshot(struct btrfs_trans_handle *trans,
1650 				   struct btrfs_pending_snapshot *pending)
1651 {
1652 
1653 	struct btrfs_fs_info *fs_info = trans->fs_info;
1654 	struct btrfs_key key;
1655 	struct btrfs_root_item *new_root_item;
1656 	struct btrfs_root *tree_root = fs_info->tree_root;
1657 	struct btrfs_root *root = pending->root;
1658 	struct btrfs_root *parent_root;
1659 	struct btrfs_block_rsv *rsv;
1660 	struct btrfs_inode *parent_inode = pending->dir;
1661 	BTRFS_PATH_AUTO_FREE(path);
1662 	struct btrfs_dir_item *dir_item;
1663 	struct extent_buffer *tmp;
1664 	struct extent_buffer *old;
1665 	struct timespec64 cur_time;
1666 	int ret = 0;
1667 	u64 to_reserve = 0;
1668 	u64 index = 0;
1669 	u64 objectid;
1670 	u64 root_flags;
1671 	unsigned int nofs_flags;
1672 	struct fscrypt_name fname;
1673 
1674 	ASSERT(pending->path);
1675 	path = pending->path;
1676 
1677 	ASSERT(pending->root_item);
1678 	new_root_item = pending->root_item;
1679 
1680 	/*
1681 	 * We're inside a transaction and must make sure that any potential
1682 	 * allocations with GFP_KERNEL in fscrypt won't recurse back to
1683 	 * filesystem.
1684 	 */
1685 	nofs_flags = memalloc_nofs_save();
1686 	pending->error = fscrypt_setup_filename(&parent_inode->vfs_inode,
1687 						&pending->dentry->d_name, 0,
1688 						&fname);
1689 	memalloc_nofs_restore(nofs_flags);
1690 	if (pending->error)
1691 		goto free_pending;
1692 
1693 	pending->error = btrfs_get_free_objectid(tree_root, &objectid);
1694 	if (pending->error)
1695 		goto free_fname;
1696 
1697 	/*
1698 	 * Make qgroup to skip current new snapshot's qgroupid, as it is
1699 	 * accounted by later btrfs_qgroup_inherit().
1700 	 */
1701 	btrfs_set_skip_qgroup(trans, objectid);
1702 
1703 	btrfs_reloc_pre_snapshot(pending, &to_reserve);
1704 
1705 	if (to_reserve > 0) {
1706 		pending->error = btrfs_block_rsv_add(fs_info,
1707 						     &pending->block_rsv,
1708 						     to_reserve,
1709 						     BTRFS_RESERVE_NO_FLUSH);
1710 		if (pending->error)
1711 			goto clear_skip_qgroup;
1712 	}
1713 
1714 	rsv = trans->block_rsv;
1715 	trans->block_rsv = &pending->block_rsv;
1716 	trans->bytes_reserved = trans->block_rsv->reserved;
1717 	trace_btrfs_space_reservation(fs_info, "transaction",
1718 				      trans->transid,
1719 				      trans->bytes_reserved, 1);
1720 	parent_root = parent_inode->root;
1721 	ret = record_root_in_trans(trans, parent_root, 0);
1722 	if (ret)
1723 		goto fail;
1724 	cur_time = current_time(&parent_inode->vfs_inode);
1725 
1726 	/*
1727 	 * insert the directory item
1728 	 */
1729 	ret = btrfs_set_inode_index(parent_inode, &index);
1730 	if (unlikely(ret)) {
1731 		btrfs_abort_transaction(trans, ret);
1732 		goto fail;
1733 	}
1734 
1735 	/* check if there is a file/dir which has the same name. */
1736 	dir_item = btrfs_lookup_dir_item(NULL, parent_root, path,
1737 					 btrfs_ino(parent_inode),
1738 					 &fname.disk_name, 0);
1739 	if (dir_item != NULL && !IS_ERR(dir_item)) {
1740 		pending->error = -EEXIST;
1741 		goto dir_item_existed;
1742 	} else if (IS_ERR(dir_item)) {
1743 		ret = PTR_ERR(dir_item);
1744 		btrfs_abort_transaction(trans, ret);
1745 		goto fail;
1746 	}
1747 	btrfs_release_path(path);
1748 
1749 	ret = btrfs_create_qgroup(trans, objectid);
1750 	if (ret && ret != -EEXIST) {
1751 		if (unlikely(ret != -ENOTCONN || btrfs_qgroup_enabled(fs_info))) {
1752 			btrfs_abort_transaction(trans, ret);
1753 			goto fail;
1754 		}
1755 	}
1756 
1757 	/*
1758 	 * pull in the delayed directory update
1759 	 * and the delayed inode item
1760 	 * otherwise we corrupt the FS during
1761 	 * snapshot
1762 	 */
1763 	ret = btrfs_run_delayed_items(trans);
1764 	if (unlikely(ret)) {
1765 		btrfs_abort_transaction(trans, ret);
1766 		goto fail;
1767 	}
1768 
1769 	ret = record_root_in_trans(trans, root, 0);
1770 	if (unlikely(ret)) {
1771 		btrfs_abort_transaction(trans, ret);
1772 		goto fail;
1773 	}
1774 	btrfs_set_root_last_snapshot(&root->root_item, trans->transid);
1775 	memcpy(new_root_item, &root->root_item, sizeof(*new_root_item));
1776 	btrfs_check_and_init_root_item(new_root_item);
1777 
1778 	root_flags = btrfs_root_flags(new_root_item);
1779 	if (pending->readonly)
1780 		root_flags |= BTRFS_ROOT_SUBVOL_RDONLY;
1781 	else
1782 		root_flags &= ~BTRFS_ROOT_SUBVOL_RDONLY;
1783 	btrfs_set_root_flags(new_root_item, root_flags);
1784 
1785 	btrfs_set_root_generation_v2(new_root_item,
1786 			trans->transid);
1787 	generate_random_guid(new_root_item->uuid);
1788 	memcpy(new_root_item->parent_uuid, root->root_item.uuid,
1789 			BTRFS_UUID_SIZE);
1790 	if (!(root_flags & BTRFS_ROOT_SUBVOL_RDONLY)) {
1791 		memset(new_root_item->received_uuid, 0,
1792 		       sizeof(new_root_item->received_uuid));
1793 		memset(&new_root_item->stime, 0, sizeof(new_root_item->stime));
1794 		memset(&new_root_item->rtime, 0, sizeof(new_root_item->rtime));
1795 		btrfs_set_root_stransid(new_root_item, 0);
1796 		btrfs_set_root_rtransid(new_root_item, 0);
1797 	}
1798 	btrfs_set_stack_timespec_sec(&new_root_item->otime, cur_time.tv_sec);
1799 	btrfs_set_stack_timespec_nsec(&new_root_item->otime, cur_time.tv_nsec);
1800 	btrfs_set_root_otransid(new_root_item, trans->transid);
1801 
1802 	old = btrfs_lock_root_node(root);
1803 	ret = btrfs_cow_block(trans, root, old, NULL, 0, &old,
1804 			      BTRFS_NESTING_COW);
1805 	if (unlikely(ret)) {
1806 		btrfs_tree_unlock(old);
1807 		free_extent_buffer(old);
1808 		btrfs_abort_transaction(trans, ret);
1809 		goto fail;
1810 	}
1811 
1812 	ret = btrfs_copy_root(trans, root, old, &tmp, objectid);
1813 	/* clean up in any case */
1814 	btrfs_tree_unlock(old);
1815 	free_extent_buffer(old);
1816 	if (unlikely(ret)) {
1817 		btrfs_abort_transaction(trans, ret);
1818 		goto fail;
1819 	}
1820 	/* see comments in should_cow_block() */
1821 	set_bit(BTRFS_ROOT_FORCE_COW, &root->state);
1822 	smp_mb__after_atomic();
1823 
1824 	btrfs_set_root_node(new_root_item, tmp);
1825 	/* record when the snapshot was created in key.offset */
1826 	key.objectid = objectid;
1827 	key.type = BTRFS_ROOT_ITEM_KEY;
1828 	key.offset = trans->transid;
1829 	ret = btrfs_insert_root(trans, tree_root, &key, new_root_item);
1830 	btrfs_tree_unlock(tmp);
1831 	free_extent_buffer(tmp);
1832 	if (unlikely(ret)) {
1833 		btrfs_abort_transaction(trans, ret);
1834 		goto fail;
1835 	}
1836 
1837 	/*
1838 	 * insert root back/forward references
1839 	 */
1840 	ret = btrfs_add_root_ref(trans, objectid,
1841 				 btrfs_root_id(parent_root),
1842 				 btrfs_ino(parent_inode), index,
1843 				 &fname.disk_name);
1844 	if (unlikely(ret)) {
1845 		btrfs_abort_transaction(trans, ret);
1846 		goto fail;
1847 	}
1848 
1849 	key.offset = (u64)-1;
1850 	pending->snap = btrfs_get_new_fs_root(fs_info, objectid, &pending->anon_dev);
1851 	if (IS_ERR(pending->snap)) {
1852 		ret = PTR_ERR(pending->snap);
1853 		pending->snap = NULL;
1854 		btrfs_abort_transaction(trans, ret);
1855 		goto fail;
1856 	}
1857 
1858 	ret = btrfs_reloc_post_snapshot(trans, pending);
1859 	if (unlikely(ret)) {
1860 		btrfs_abort_transaction(trans, ret);
1861 		goto fail;
1862 	}
1863 
1864 	/*
1865 	 * Do special qgroup accounting for snapshot, as we do some qgroup
1866 	 * snapshot hack to do fast snapshot.
1867 	 * To co-operate with that hack, we do hack again.
1868 	 * Or snapshot will be greatly slowed down by a subtree qgroup rescan
1869 	 */
1870 	if (btrfs_qgroup_mode(fs_info) == BTRFS_QGROUP_MODE_FULL)
1871 		ret = qgroup_account_snapshot(trans, root, parent_root,
1872 					      pending->inherit, objectid);
1873 	else if (btrfs_qgroup_mode(fs_info) == BTRFS_QGROUP_MODE_SIMPLE)
1874 		ret = btrfs_qgroup_inherit(trans, btrfs_root_id(root), objectid,
1875 					   btrfs_root_id(parent_root), pending->inherit);
1876 	if (ret < 0)
1877 		goto fail;
1878 
1879 	ret = btrfs_insert_dir_item(trans, &fname.disk_name,
1880 				    parent_inode, &key, BTRFS_FT_DIR,
1881 				    index);
1882 	if (unlikely(ret)) {
1883 		btrfs_abort_transaction(trans, ret);
1884 		goto fail;
1885 	}
1886 
1887 	btrfs_i_size_write(parent_inode, parent_inode->vfs_inode.i_size +
1888 						  fname.disk_name.len * 2);
1889 	inode_set_mtime_to_ts(&parent_inode->vfs_inode,
1890 			      inode_set_ctime_current(&parent_inode->vfs_inode));
1891 	ret = btrfs_update_inode_fallback(trans, parent_inode);
1892 	if (unlikely(ret)) {
1893 		btrfs_abort_transaction(trans, ret);
1894 		goto fail;
1895 	}
1896 	ret = btrfs_uuid_tree_add(trans, new_root_item->uuid,
1897 				  BTRFS_UUID_KEY_SUBVOL,
1898 				  objectid);
1899 	if (unlikely(ret)) {
1900 		btrfs_abort_transaction(trans, ret);
1901 		goto fail;
1902 	}
1903 	if (!btrfs_is_empty_uuid(new_root_item->received_uuid)) {
1904 		ret = btrfs_uuid_tree_add(trans, new_root_item->received_uuid,
1905 					  BTRFS_UUID_KEY_RECEIVED_SUBVOL,
1906 					  objectid);
1907 		if (unlikely(ret && ret != -EEXIST)) {
1908 			btrfs_abort_transaction(trans, ret);
1909 			goto fail;
1910 		}
1911 	}
1912 
1913 fail:
1914 	pending->error = ret;
1915 dir_item_existed:
1916 	trans->block_rsv = rsv;
1917 	trans->bytes_reserved = 0;
1918 clear_skip_qgroup:
1919 	btrfs_clear_skip_qgroup(trans);
1920 free_fname:
1921 	fscrypt_free_filename(&fname);
1922 free_pending:
1923 	kfree(new_root_item);
1924 	pending->root_item = NULL;
1925 	pending->path = NULL;
1926 
1927 	return ret;
1928 }
1929 
1930 /*
1931  * create all the snapshots we've scheduled for creation
1932  */
create_pending_snapshots(struct btrfs_trans_handle * trans)1933 static noinline int create_pending_snapshots(struct btrfs_trans_handle *trans)
1934 {
1935 	struct btrfs_pending_snapshot *pending, *next;
1936 	struct list_head *head = &trans->transaction->pending_snapshots;
1937 	int ret = 0;
1938 
1939 	list_for_each_entry_safe(pending, next, head, list) {
1940 		list_del(&pending->list);
1941 		ret = create_pending_snapshot(trans, pending);
1942 		if (ret)
1943 			break;
1944 	}
1945 	return ret;
1946 }
1947 
update_super_roots(struct btrfs_fs_info * fs_info)1948 static void update_super_roots(struct btrfs_fs_info *fs_info)
1949 {
1950 	struct btrfs_root_item *root_item;
1951 	struct btrfs_super_block *super;
1952 
1953 	super = fs_info->super_copy;
1954 
1955 	root_item = &fs_info->chunk_root->root_item;
1956 	super->chunk_root = root_item->bytenr;
1957 	super->chunk_root_generation = root_item->generation;
1958 	super->chunk_root_level = root_item->level;
1959 
1960 	root_item = &fs_info->tree_root->root_item;
1961 	super->root = root_item->bytenr;
1962 	super->generation = root_item->generation;
1963 	super->root_level = root_item->level;
1964 	if (btrfs_test_opt(fs_info, SPACE_CACHE))
1965 		super->cache_generation = root_item->generation;
1966 	else if (test_bit(BTRFS_FS_CLEANUP_SPACE_CACHE_V1, &fs_info->flags))
1967 		super->cache_generation = 0;
1968 	if (test_bit(BTRFS_FS_UPDATE_UUID_TREE_GEN, &fs_info->flags))
1969 		super->uuid_tree_generation = root_item->generation;
1970 }
1971 
btrfs_transaction_blocked(struct btrfs_fs_info * info)1972 int btrfs_transaction_blocked(struct btrfs_fs_info *info)
1973 {
1974 	struct btrfs_transaction *trans;
1975 	int ret = 0;
1976 
1977 	spin_lock(&info->trans_lock);
1978 	trans = info->running_transaction;
1979 	if (trans)
1980 		ret = is_transaction_blocked(trans);
1981 	spin_unlock(&info->trans_lock);
1982 	return ret;
1983 }
1984 
btrfs_commit_transaction_async(struct btrfs_trans_handle * trans)1985 void btrfs_commit_transaction_async(struct btrfs_trans_handle *trans)
1986 {
1987 	struct btrfs_fs_info *fs_info = trans->fs_info;
1988 	struct btrfs_transaction *cur_trans;
1989 
1990 	/* Kick the transaction kthread. */
1991 	set_bit(BTRFS_FS_COMMIT_TRANS, &fs_info->flags);
1992 	wake_up_process(fs_info->transaction_kthread);
1993 
1994 	/* take transaction reference */
1995 	cur_trans = trans->transaction;
1996 	refcount_inc(&cur_trans->use_count);
1997 
1998 	btrfs_end_transaction(trans);
1999 
2000 	/*
2001 	 * Wait for the current transaction commit to start and block
2002 	 * subsequent transaction joins
2003 	 */
2004 	btrfs_might_wait_for_state(fs_info, BTRFS_LOCKDEP_TRANS_COMMIT_PREP);
2005 	wait_event(fs_info->transaction_blocked_wait,
2006 		   cur_trans->state >= TRANS_STATE_COMMIT_START ||
2007 		   TRANS_ABORTED(cur_trans));
2008 	btrfs_put_transaction(cur_trans);
2009 }
2010 
2011 /*
2012  * If there is a running transaction commit it or if it's already committing,
2013  * wait for its commit to complete. Does not start and commit a new transaction
2014  * if there isn't any running.
2015  */
btrfs_commit_current_transaction(struct btrfs_root * root)2016 int btrfs_commit_current_transaction(struct btrfs_root *root)
2017 {
2018 	struct btrfs_trans_handle *trans;
2019 
2020 	trans = btrfs_attach_transaction_barrier(root);
2021 	if (IS_ERR(trans)) {
2022 		int ret = PTR_ERR(trans);
2023 
2024 		return (ret == -ENOENT) ? 0 : ret;
2025 	}
2026 
2027 	return btrfs_commit_transaction(trans);
2028 }
2029 
cleanup_transaction(struct btrfs_trans_handle * trans,int err)2030 static void cleanup_transaction(struct btrfs_trans_handle *trans, int err)
2031 {
2032 	struct btrfs_fs_info *fs_info = trans->fs_info;
2033 	struct btrfs_transaction *cur_trans = trans->transaction;
2034 
2035 	WARN_ON(refcount_read(&trans->use_count) > 1);
2036 
2037 	btrfs_abort_transaction(trans, err);
2038 
2039 	spin_lock(&fs_info->trans_lock);
2040 
2041 	/*
2042 	 * If the transaction is removed from the list, it means this
2043 	 * transaction has been committed successfully, so it is impossible
2044 	 * to call the cleanup function.
2045 	 */
2046 	BUG_ON(list_empty(&cur_trans->list));
2047 
2048 	if (cur_trans == fs_info->running_transaction) {
2049 		cur_trans->state = TRANS_STATE_COMMIT_DOING;
2050 		spin_unlock(&fs_info->trans_lock);
2051 
2052 		/*
2053 		 * The thread has already released the lockdep map as reader
2054 		 * already in btrfs_commit_transaction().
2055 		 */
2056 		btrfs_might_wait_for_event(fs_info, btrfs_trans_num_writers);
2057 		wait_event(cur_trans->writer_wait,
2058 			   atomic_read(&cur_trans->num_writers) == 1);
2059 
2060 		spin_lock(&fs_info->trans_lock);
2061 	}
2062 
2063 	/*
2064 	 * Now that we know no one else is still using the transaction we can
2065 	 * remove the transaction from the list of transactions. This avoids
2066 	 * the transaction kthread from cleaning up the transaction while some
2067 	 * other task is still using it, which could result in a use-after-free
2068 	 * on things like log trees, as it forces the transaction kthread to
2069 	 * wait for this transaction to be cleaned up by us.
2070 	 */
2071 	list_del_init(&cur_trans->list);
2072 
2073 	spin_unlock(&fs_info->trans_lock);
2074 
2075 	btrfs_cleanup_one_transaction(trans->transaction);
2076 
2077 	spin_lock(&fs_info->trans_lock);
2078 	if (cur_trans == fs_info->running_transaction)
2079 		fs_info->running_transaction = NULL;
2080 	spin_unlock(&fs_info->trans_lock);
2081 
2082 	if (trans->type & __TRANS_FREEZABLE)
2083 		sb_end_intwrite(fs_info->sb);
2084 	btrfs_put_transaction(cur_trans);
2085 	btrfs_put_transaction(cur_trans);
2086 
2087 	trace_btrfs_transaction_commit(fs_info);
2088 
2089 	if (current->journal_info == trans)
2090 		current->journal_info = NULL;
2091 
2092 	/*
2093 	 * If relocation is running, we can't cancel scrub because that will
2094 	 * result in a deadlock. Before relocating a block group, relocation
2095 	 * pauses scrub, then starts and commits a transaction before unpausing
2096 	 * scrub. If the transaction commit is being done by the relocation
2097 	 * task or triggered by another task and the relocation task is waiting
2098 	 * for the commit, and we end up here due to an error in the commit
2099 	 * path, then calling btrfs_scrub_cancel() will deadlock, as we are
2100 	 * asking for scrub to stop while having it asked to be paused higher
2101 	 * above in relocation code.
2102 	 */
2103 	if (!test_bit(BTRFS_FS_RELOC_RUNNING, &fs_info->flags))
2104 		btrfs_scrub_cancel(fs_info);
2105 
2106 	kmem_cache_free(btrfs_trans_handle_cachep, trans);
2107 }
2108 
2109 /*
2110  * Release reserved delayed ref space of all pending block groups of the
2111  * transaction and remove them from the list
2112  */
btrfs_cleanup_pending_block_groups(struct btrfs_trans_handle * trans)2113 static void btrfs_cleanup_pending_block_groups(struct btrfs_trans_handle *trans)
2114 {
2115        struct btrfs_fs_info *fs_info = trans->fs_info;
2116        struct btrfs_block_group *block_group, *tmp;
2117 
2118        list_for_each_entry_safe(block_group, tmp, &trans->new_bgs, bg_list) {
2119                btrfs_dec_delayed_refs_rsv_bg_inserts(fs_info);
2120 		/*
2121 		* Not strictly necessary to lock, as no other task will be using a
2122 		* block_group on the new_bgs list during a transaction abort.
2123 		*/
2124 	       spin_lock(&fs_info->unused_bgs_lock);
2125                list_del_init(&block_group->bg_list);
2126 	       btrfs_put_block_group(block_group);
2127 	       spin_unlock(&fs_info->unused_bgs_lock);
2128        }
2129 }
2130 
btrfs_start_delalloc_flush(struct btrfs_fs_info * fs_info)2131 static inline int btrfs_start_delalloc_flush(struct btrfs_fs_info *fs_info)
2132 {
2133 	/*
2134 	 * We use try_to_writeback_inodes_sb() here because if we used
2135 	 * btrfs_start_delalloc_roots we would deadlock with fs freeze.
2136 	 * Currently are holding the fs freeze lock, if we do an async flush
2137 	 * we'll do btrfs_join_transaction() and deadlock because we need to
2138 	 * wait for the fs freeze lock.  Using the direct flushing we benefit
2139 	 * from already being in a transaction and our join_transaction doesn't
2140 	 * have to re-take the fs freeze lock.
2141 	 *
2142 	 * Note that try_to_writeback_inodes_sb() will only trigger writeback
2143 	 * if it can read lock sb->s_umount. It will always be able to lock it,
2144 	 * except when the filesystem is being unmounted or being frozen, but in
2145 	 * those cases sync_filesystem() is called, which results in calling
2146 	 * writeback_inodes_sb() while holding a write lock on sb->s_umount.
2147 	 * Note that we don't call writeback_inodes_sb() directly, because it
2148 	 * will emit a warning if sb->s_umount is not locked.
2149 	 */
2150 	if (btrfs_test_opt(fs_info, FLUSHONCOMMIT))
2151 		try_to_writeback_inodes_sb(fs_info->sb, WB_REASON_SYNC);
2152 	return 0;
2153 }
2154 
btrfs_wait_delalloc_flush(struct btrfs_fs_info * fs_info)2155 static inline void btrfs_wait_delalloc_flush(struct btrfs_fs_info *fs_info)
2156 {
2157 	if (btrfs_test_opt(fs_info, FLUSHONCOMMIT))
2158 		btrfs_wait_ordered_roots(fs_info, U64_MAX, NULL);
2159 }
2160 
2161 /*
2162  * Add a pending snapshot associated with the given transaction handle to the
2163  * respective handle. This must be called after the transaction commit started
2164  * and while holding fs_info->trans_lock.
2165  * This serves to guarantee a caller of btrfs_commit_transaction() that it can
2166  * safely free the pending snapshot pointer in case btrfs_commit_transaction()
2167  * returns an error.
2168  */
add_pending_snapshot(struct btrfs_trans_handle * trans)2169 static void add_pending_snapshot(struct btrfs_trans_handle *trans)
2170 {
2171 	struct btrfs_transaction *cur_trans = trans->transaction;
2172 
2173 	if (!trans->pending_snapshot)
2174 		return;
2175 
2176 	lockdep_assert_held(&trans->fs_info->trans_lock);
2177 	ASSERT(cur_trans->state >= TRANS_STATE_COMMIT_PREP,
2178 	       "cur_trans->state=%d", cur_trans->state);
2179 
2180 	list_add(&trans->pending_snapshot->list, &cur_trans->pending_snapshots);
2181 }
2182 
update_commit_stats(struct btrfs_fs_info * fs_info)2183 static void update_commit_stats(struct btrfs_fs_info *fs_info)
2184 {
2185 	ktime_t now = ktime_get_ns();
2186 	ktime_t interval = now - fs_info->commit_stats.critical_section_start_time;
2187 
2188 	ASSERT(fs_info->commit_stats.critical_section_start_time);
2189 
2190 	fs_info->commit_stats.commit_count++;
2191 	fs_info->commit_stats.last_commit_dur = interval;
2192 	fs_info->commit_stats.max_commit_dur =
2193 			max_t(u64, fs_info->commit_stats.max_commit_dur, interval);
2194 	fs_info->commit_stats.total_commit_dur += interval;
2195 	fs_info->commit_stats.critical_section_start_time = 0;
2196 }
2197 
btrfs_commit_transaction(struct btrfs_trans_handle * trans)2198 int btrfs_commit_transaction(struct btrfs_trans_handle *trans)
2199 {
2200 	struct btrfs_fs_info *fs_info = trans->fs_info;
2201 	struct btrfs_transaction *cur_trans = trans->transaction;
2202 	struct btrfs_transaction *prev_trans = NULL;
2203 	int ret;
2204 
2205 	ASSERT(refcount_read(&trans->use_count) == 1,
2206 	       "refcount_read(&trans->use_count)=%d", refcount_read(&trans->use_count));
2207 	btrfs_trans_state_lockdep_acquire(fs_info, BTRFS_LOCKDEP_TRANS_COMMIT_PREP);
2208 
2209 	clear_bit(BTRFS_FS_NEED_TRANS_COMMIT, &fs_info->flags);
2210 
2211 	/* Stop the commit early if ->aborted is set */
2212 	if (TRANS_ABORTED(cur_trans)) {
2213 		ret = cur_trans->aborted;
2214 		goto lockdep_trans_commit_start_release;
2215 	}
2216 
2217 	btrfs_trans_release_metadata(trans);
2218 	trans->block_rsv = NULL;
2219 
2220 	/*
2221 	 * We only want one transaction commit doing the flushing so we do not
2222 	 * waste a bunch of time on lock contention on the extent root node.
2223 	 */
2224 	if (!test_and_set_bit(BTRFS_DELAYED_REFS_FLUSHING,
2225 			      &cur_trans->delayed_refs.flags)) {
2226 		/*
2227 		 * Make a pass through all the delayed refs we have so far.
2228 		 * Any running threads may add more while we are here.
2229 		 */
2230 		ret = btrfs_run_delayed_refs(trans, 0);
2231 		if (ret)
2232 			goto lockdep_trans_commit_start_release;
2233 	}
2234 
2235 	btrfs_create_pending_block_groups(trans);
2236 
2237 	if (!test_bit(BTRFS_TRANS_DIRTY_BG_RUN, &cur_trans->flags)) {
2238 		int run_it = 0;
2239 
2240 		/* this mutex is also taken before trying to set
2241 		 * block groups readonly.  We need to make sure
2242 		 * that nobody has set a block group readonly
2243 		 * after a extents from that block group have been
2244 		 * allocated for cache files.  btrfs_set_block_group_ro
2245 		 * will wait for the transaction to commit if it
2246 		 * finds BTRFS_TRANS_DIRTY_BG_RUN set.
2247 		 *
2248 		 * The BTRFS_TRANS_DIRTY_BG_RUN flag is also used to make sure
2249 		 * only one process starts all the block group IO.  It wouldn't
2250 		 * hurt to have more than one go through, but there's no
2251 		 * real advantage to it either.
2252 		 */
2253 		mutex_lock(&fs_info->ro_block_group_mutex);
2254 		if (!test_and_set_bit(BTRFS_TRANS_DIRTY_BG_RUN,
2255 				      &cur_trans->flags))
2256 			run_it = 1;
2257 		mutex_unlock(&fs_info->ro_block_group_mutex);
2258 
2259 		if (run_it) {
2260 			ret = btrfs_start_dirty_block_groups(trans);
2261 			if (ret)
2262 				goto lockdep_trans_commit_start_release;
2263 		}
2264 	}
2265 
2266 	spin_lock(&fs_info->trans_lock);
2267 	if (cur_trans->state >= TRANS_STATE_COMMIT_PREP) {
2268 		enum btrfs_trans_state want_state = TRANS_STATE_COMPLETED;
2269 
2270 		add_pending_snapshot(trans);
2271 
2272 		spin_unlock(&fs_info->trans_lock);
2273 		refcount_inc(&cur_trans->use_count);
2274 
2275 		if (trans->in_fsync)
2276 			want_state = TRANS_STATE_SUPER_COMMITTED;
2277 
2278 		btrfs_trans_state_lockdep_release(fs_info,
2279 						  BTRFS_LOCKDEP_TRANS_COMMIT_PREP);
2280 		ret = btrfs_end_transaction(trans);
2281 		wait_for_commit(cur_trans, want_state);
2282 
2283 		if (TRANS_ABORTED(cur_trans))
2284 			ret = cur_trans->aborted;
2285 
2286 		btrfs_put_transaction(cur_trans);
2287 
2288 		return ret;
2289 	}
2290 
2291 	cur_trans->state = TRANS_STATE_COMMIT_PREP;
2292 	wake_up(&fs_info->transaction_blocked_wait);
2293 	btrfs_trans_state_lockdep_release(fs_info, BTRFS_LOCKDEP_TRANS_COMMIT_PREP);
2294 
2295 	if (!list_is_first(&cur_trans->list, &fs_info->trans_list)) {
2296 		enum btrfs_trans_state want_state = TRANS_STATE_COMPLETED;
2297 
2298 		if (trans->in_fsync)
2299 			want_state = TRANS_STATE_SUPER_COMMITTED;
2300 
2301 		prev_trans = list_prev_entry(cur_trans, list);
2302 		if (prev_trans->state < want_state) {
2303 			refcount_inc(&prev_trans->use_count);
2304 			spin_unlock(&fs_info->trans_lock);
2305 
2306 			wait_for_commit(prev_trans, want_state);
2307 
2308 			ret = READ_ONCE(prev_trans->aborted);
2309 
2310 			btrfs_put_transaction(prev_trans);
2311 			if (ret)
2312 				goto lockdep_release;
2313 			spin_lock(&fs_info->trans_lock);
2314 		}
2315 	} else {
2316 		/*
2317 		 * The previous transaction was aborted and was already removed
2318 		 * from the list of transactions at fs_info->trans_list. So we
2319 		 * abort to prevent writing a new superblock that reflects a
2320 		 * corrupt state (pointing to trees with unwritten nodes/leafs).
2321 		 */
2322 		if (BTRFS_FS_ERROR(fs_info)) {
2323 			spin_unlock(&fs_info->trans_lock);
2324 			ret = -EROFS;
2325 			goto lockdep_release;
2326 		}
2327 	}
2328 
2329 	cur_trans->state = TRANS_STATE_COMMIT_START;
2330 	wake_up(&fs_info->transaction_blocked_wait);
2331 	spin_unlock(&fs_info->trans_lock);
2332 
2333 	/*
2334 	 * Get the time spent on the work done by the commit thread and not
2335 	 * the time spent waiting on a previous commit
2336 	 */
2337 	fs_info->commit_stats.critical_section_start_time = ktime_get_ns();
2338 	extwriter_counter_dec(cur_trans, trans->type);
2339 
2340 	ret = btrfs_start_delalloc_flush(fs_info);
2341 	if (ret)
2342 		goto lockdep_release;
2343 
2344 	ret = btrfs_run_delayed_items(trans);
2345 	if (ret)
2346 		goto lockdep_release;
2347 
2348 	/*
2349 	 * The thread has started/joined the transaction thus it holds the
2350 	 * lockdep map as a reader. It has to release it before acquiring the
2351 	 * lockdep map as a writer.
2352 	 */
2353 	btrfs_lockdep_release(fs_info, btrfs_trans_num_extwriters);
2354 	btrfs_might_wait_for_event(fs_info, btrfs_trans_num_extwriters);
2355 	wait_event(cur_trans->writer_wait,
2356 		   extwriter_counter_read(cur_trans) == 0);
2357 
2358 	/* some pending stuffs might be added after the previous flush. */
2359 	ret = btrfs_run_delayed_items(trans);
2360 	if (ret) {
2361 		btrfs_lockdep_release(fs_info, btrfs_trans_num_writers);
2362 		goto cleanup_transaction;
2363 	}
2364 
2365 	btrfs_wait_delalloc_flush(fs_info);
2366 
2367 	/*
2368 	 * Wait for all ordered extents started by a fast fsync that joined this
2369 	 * transaction. Otherwise if this transaction commits before the ordered
2370 	 * extents complete we lose logged data after a power failure.
2371 	 */
2372 	btrfs_might_wait_for_event(fs_info, btrfs_trans_pending_ordered);
2373 	wait_event(cur_trans->pending_wait,
2374 		   atomic_read(&cur_trans->pending_ordered) == 0);
2375 
2376 	btrfs_scrub_pause(fs_info);
2377 	/*
2378 	 * Ok now we need to make sure to block out any other joins while we
2379 	 * commit the transaction.  We could have started a join before setting
2380 	 * COMMIT_DOING so make sure to wait for num_writers to == 1 again.
2381 	 */
2382 	spin_lock(&fs_info->trans_lock);
2383 	add_pending_snapshot(trans);
2384 	cur_trans->state = TRANS_STATE_COMMIT_DOING;
2385 	spin_unlock(&fs_info->trans_lock);
2386 
2387 	/*
2388 	 * The thread has started/joined the transaction thus it holds the
2389 	 * lockdep map as a reader. It has to release it before acquiring the
2390 	 * lockdep map as a writer.
2391 	 */
2392 	btrfs_lockdep_release(fs_info, btrfs_trans_num_writers);
2393 	btrfs_might_wait_for_event(fs_info, btrfs_trans_num_writers);
2394 	wait_event(cur_trans->writer_wait,
2395 		   atomic_read(&cur_trans->num_writers) == 1);
2396 
2397 	/*
2398 	 * Make lockdep happy by acquiring the state locks after
2399 	 * btrfs_trans_num_writers is released. If we acquired the state locks
2400 	 * before releasing the btrfs_trans_num_writers lock then lockdep would
2401 	 * complain because we did not follow the reverse order unlocking rule.
2402 	 */
2403 	btrfs_trans_state_lockdep_acquire(fs_info, BTRFS_LOCKDEP_TRANS_COMPLETED);
2404 	btrfs_trans_state_lockdep_acquire(fs_info, BTRFS_LOCKDEP_TRANS_SUPER_COMMITTED);
2405 	btrfs_trans_state_lockdep_acquire(fs_info, BTRFS_LOCKDEP_TRANS_UNBLOCKED);
2406 
2407 	/*
2408 	 * We've started the commit, clear the flag in case we were triggered to
2409 	 * do an async commit but somebody else started before the transaction
2410 	 * kthread could do the work.
2411 	 */
2412 	clear_bit(BTRFS_FS_COMMIT_TRANS, &fs_info->flags);
2413 
2414 	if (TRANS_ABORTED(cur_trans)) {
2415 		ret = cur_trans->aborted;
2416 		btrfs_trans_state_lockdep_release(fs_info, BTRFS_LOCKDEP_TRANS_UNBLOCKED);
2417 		goto scrub_continue;
2418 	}
2419 	/*
2420 	 * the reloc mutex makes sure that we stop
2421 	 * the balancing code from coming in and moving
2422 	 * extents around in the middle of the commit
2423 	 */
2424 	mutex_lock(&fs_info->reloc_mutex);
2425 
2426 	/*
2427 	 * We needn't worry about the delayed items because we will
2428 	 * deal with them in create_pending_snapshot(), which is the
2429 	 * core function of the snapshot creation.
2430 	 */
2431 	ret = create_pending_snapshots(trans);
2432 	if (ret)
2433 		goto unlock_reloc;
2434 
2435 	/*
2436 	 * We insert the dir indexes of the snapshots and update the inode
2437 	 * of the snapshots' parents after the snapshot creation, so there
2438 	 * are some delayed items which are not dealt with. Now deal with
2439 	 * them.
2440 	 *
2441 	 * We needn't worry that this operation will corrupt the snapshots,
2442 	 * because all the tree which are snapshotted will be forced to COW
2443 	 * the nodes and leaves.
2444 	 */
2445 	ret = btrfs_run_delayed_items(trans);
2446 	if (ret)
2447 		goto unlock_reloc;
2448 
2449 	ret = btrfs_run_delayed_refs(trans, U64_MAX);
2450 	if (ret)
2451 		goto unlock_reloc;
2452 
2453 	/*
2454 	 * make sure none of the code above managed to slip in a
2455 	 * delayed item
2456 	 */
2457 	btrfs_assert_delayed_root_empty(fs_info);
2458 
2459 	WARN_ON(cur_trans != trans->transaction);
2460 
2461 	ret = commit_fs_roots(trans);
2462 	if (ret)
2463 		goto unlock_reloc;
2464 
2465 	/* commit_fs_roots gets rid of all the tree log roots, it is now
2466 	 * safe to free the root of tree log roots
2467 	 */
2468 	btrfs_free_log_root_tree(trans, fs_info);
2469 
2470 	/*
2471 	 * Since fs roots are all committed, we can get a quite accurate
2472 	 * new_roots. So let's do quota accounting.
2473 	 */
2474 	ret = btrfs_qgroup_account_extents(trans);
2475 	if (ret < 0)
2476 		goto unlock_reloc;
2477 
2478 	ret = commit_cowonly_roots(trans);
2479 	if (ret)
2480 		goto unlock_reloc;
2481 
2482 	/*
2483 	 * The tasks which save the space cache and inode cache may also
2484 	 * update ->aborted, check it.
2485 	 */
2486 	if (TRANS_ABORTED(cur_trans)) {
2487 		ret = cur_trans->aborted;
2488 		goto unlock_reloc;
2489 	}
2490 
2491 	cur_trans = fs_info->running_transaction;
2492 
2493 	btrfs_set_root_node(&fs_info->tree_root->root_item,
2494 			    fs_info->tree_root->node);
2495 	list_add_tail(&fs_info->tree_root->dirty_list,
2496 		      &cur_trans->switch_commits);
2497 
2498 	btrfs_set_root_node(&fs_info->chunk_root->root_item,
2499 			    fs_info->chunk_root->node);
2500 	list_add_tail(&fs_info->chunk_root->dirty_list,
2501 		      &cur_trans->switch_commits);
2502 
2503 	if (btrfs_fs_incompat(fs_info, EXTENT_TREE_V2)) {
2504 		btrfs_set_root_node(&fs_info->block_group_root->root_item,
2505 				    fs_info->block_group_root->node);
2506 		list_add_tail(&fs_info->block_group_root->dirty_list,
2507 			      &cur_trans->switch_commits);
2508 	}
2509 
2510 	switch_commit_roots(trans);
2511 
2512 	ASSERT(list_empty(&cur_trans->dirty_bgs));
2513 	ASSERT(list_empty(&cur_trans->io_bgs));
2514 	update_super_roots(fs_info);
2515 
2516 	btrfs_set_super_log_root(fs_info->super_copy, 0);
2517 	btrfs_set_super_log_root_level(fs_info->super_copy, 0);
2518 	memcpy(fs_info->super_for_commit, fs_info->super_copy,
2519 	       sizeof(*fs_info->super_copy));
2520 
2521 	btrfs_commit_device_sizes(cur_trans);
2522 
2523 	clear_bit(BTRFS_FS_LOG1_ERR, &fs_info->flags);
2524 	clear_bit(BTRFS_FS_LOG2_ERR, &fs_info->flags);
2525 
2526 	btrfs_trans_release_chunk_metadata(trans);
2527 
2528 	/*
2529 	 * Before changing the transaction state to TRANS_STATE_UNBLOCKED and
2530 	 * setting fs_info->running_transaction to NULL, lock tree_log_mutex to
2531 	 * make sure that before we commit our superblock, no other task can
2532 	 * start a new transaction and commit a log tree before we commit our
2533 	 * superblock. Anyone trying to commit a log tree locks this mutex before
2534 	 * writing its superblock.
2535 	 */
2536 	mutex_lock(&fs_info->tree_log_mutex);
2537 
2538 	spin_lock(&fs_info->trans_lock);
2539 	cur_trans->state = TRANS_STATE_UNBLOCKED;
2540 	fs_info->running_transaction = NULL;
2541 	spin_unlock(&fs_info->trans_lock);
2542 	mutex_unlock(&fs_info->reloc_mutex);
2543 
2544 	wake_up(&fs_info->transaction_wait);
2545 	btrfs_trans_state_lockdep_release(fs_info, BTRFS_LOCKDEP_TRANS_UNBLOCKED);
2546 
2547 	/* If we have features changed, wake up the cleaner to update sysfs. */
2548 	if (test_bit(BTRFS_FS_FEATURE_CHANGED, &fs_info->flags) &&
2549 	    fs_info->cleaner_kthread)
2550 		wake_up_process(fs_info->cleaner_kthread);
2551 
2552 	ret = btrfs_write_and_wait_transaction(trans);
2553 	if (ret) {
2554 		btrfs_handle_fs_error(fs_info, ret,
2555 				      "Error while writing out transaction");
2556 		mutex_unlock(&fs_info->tree_log_mutex);
2557 		goto scrub_continue;
2558 	}
2559 
2560 	ret = write_all_supers(fs_info, 0);
2561 	/*
2562 	 * the super is written, we can safely allow the tree-loggers
2563 	 * to go about their business
2564 	 */
2565 	mutex_unlock(&fs_info->tree_log_mutex);
2566 	if (ret)
2567 		goto scrub_continue;
2568 
2569 	update_commit_stats(fs_info);
2570 	/*
2571 	 * We needn't acquire the lock here because there is no other task
2572 	 * which can change it.
2573 	 */
2574 	cur_trans->state = TRANS_STATE_SUPER_COMMITTED;
2575 	wake_up(&cur_trans->commit_wait);
2576 	btrfs_trans_state_lockdep_release(fs_info, BTRFS_LOCKDEP_TRANS_SUPER_COMMITTED);
2577 
2578 	ret = btrfs_finish_extent_commit(trans);
2579 	if (ret)
2580 		goto scrub_continue;
2581 
2582 	if (test_bit(BTRFS_TRANS_HAVE_FREE_BGS, &cur_trans->flags))
2583 		btrfs_clear_space_info_full(fs_info);
2584 
2585 	btrfs_set_last_trans_committed(fs_info, cur_trans->transid);
2586 	/*
2587 	 * We needn't acquire the lock here because there is no other task
2588 	 * which can change it.
2589 	 */
2590 	cur_trans->state = TRANS_STATE_COMPLETED;
2591 	wake_up(&cur_trans->commit_wait);
2592 	btrfs_trans_state_lockdep_release(fs_info, BTRFS_LOCKDEP_TRANS_COMPLETED);
2593 
2594 	spin_lock(&fs_info->trans_lock);
2595 	list_del_init(&cur_trans->list);
2596 	spin_unlock(&fs_info->trans_lock);
2597 
2598 	btrfs_put_transaction(cur_trans);
2599 	btrfs_put_transaction(cur_trans);
2600 
2601 	if (trans->type & __TRANS_FREEZABLE)
2602 		sb_end_intwrite(fs_info->sb);
2603 
2604 	trace_btrfs_transaction_commit(fs_info);
2605 
2606 	btrfs_scrub_continue(fs_info);
2607 
2608 	if (current->journal_info == trans)
2609 		current->journal_info = NULL;
2610 
2611 	kmem_cache_free(btrfs_trans_handle_cachep, trans);
2612 
2613 	return ret;
2614 
2615 unlock_reloc:
2616 	mutex_unlock(&fs_info->reloc_mutex);
2617 	btrfs_trans_state_lockdep_release(fs_info, BTRFS_LOCKDEP_TRANS_UNBLOCKED);
2618 scrub_continue:
2619 	btrfs_trans_state_lockdep_release(fs_info, BTRFS_LOCKDEP_TRANS_SUPER_COMMITTED);
2620 	btrfs_trans_state_lockdep_release(fs_info, BTRFS_LOCKDEP_TRANS_COMPLETED);
2621 	btrfs_scrub_continue(fs_info);
2622 cleanup_transaction:
2623 	btrfs_trans_release_metadata(trans);
2624 	btrfs_cleanup_pending_block_groups(trans);
2625 	btrfs_trans_release_chunk_metadata(trans);
2626 	trans->block_rsv = NULL;
2627 	btrfs_warn(fs_info, "Skipping commit of aborted transaction.");
2628 	if (current->journal_info == trans)
2629 		current->journal_info = NULL;
2630 	cleanup_transaction(trans, ret);
2631 
2632 	return ret;
2633 
2634 lockdep_release:
2635 	btrfs_lockdep_release(fs_info, btrfs_trans_num_extwriters);
2636 	btrfs_lockdep_release(fs_info, btrfs_trans_num_writers);
2637 	goto cleanup_transaction;
2638 
2639 lockdep_trans_commit_start_release:
2640 	btrfs_trans_state_lockdep_release(fs_info, BTRFS_LOCKDEP_TRANS_COMMIT_PREP);
2641 	btrfs_end_transaction(trans);
2642 	return ret;
2643 }
2644 
2645 /*
2646  * return < 0 if error
2647  * 0 if there are no more dead_roots at the time of call
2648  * 1 there are more to be processed, call me again
2649  *
2650  * The return value indicates there are certainly more snapshots to delete, but
2651  * if there comes a new one during processing, it may return 0. We don't mind,
2652  * because btrfs_commit_super will poke cleaner thread and it will process it a
2653  * few seconds later.
2654  */
btrfs_clean_one_deleted_snapshot(struct btrfs_fs_info * fs_info)2655 int btrfs_clean_one_deleted_snapshot(struct btrfs_fs_info *fs_info)
2656 {
2657 	struct btrfs_root *root;
2658 	int ret;
2659 
2660 	spin_lock(&fs_info->trans_lock);
2661 	if (list_empty(&fs_info->dead_roots)) {
2662 		spin_unlock(&fs_info->trans_lock);
2663 		return 0;
2664 	}
2665 	root = list_first_entry(&fs_info->dead_roots,
2666 			struct btrfs_root, root_list);
2667 	list_del_init(&root->root_list);
2668 	spin_unlock(&fs_info->trans_lock);
2669 
2670 	btrfs_debug(fs_info, "cleaner removing %llu", btrfs_root_id(root));
2671 
2672 	btrfs_kill_all_delayed_nodes(root);
2673 
2674 	if (btrfs_header_backref_rev(root->node) <
2675 			BTRFS_MIXED_BACKREF_REV)
2676 		ret = btrfs_drop_snapshot(root, false, false);
2677 	else
2678 		ret = btrfs_drop_snapshot(root, true, false);
2679 
2680 	btrfs_put_root(root);
2681 	return (ret < 0) ? 0 : 1;
2682 }
2683 
2684 /*
2685  * We only mark the transaction aborted and then set the file system read-only.
2686  * This will prevent new transactions from starting or trying to join this
2687  * one.
2688  *
2689  * This means that error recovery at the call site is limited to freeing
2690  * any local memory allocations and passing the error code up without
2691  * further cleanup. The transaction should complete as it normally would
2692  * in the call path but will return -EIO.
2693  *
2694  * We'll complete the cleanup in btrfs_end_transaction and
2695  * btrfs_commit_transaction.
2696  */
__btrfs_abort_transaction(struct btrfs_trans_handle * trans,const char * function,unsigned int line,int error,bool first_hit)2697 void __cold __btrfs_abort_transaction(struct btrfs_trans_handle *trans,
2698 				      const char *function,
2699 				      unsigned int line, int error, bool first_hit)
2700 {
2701 	struct btrfs_fs_info *fs_info = trans->fs_info;
2702 
2703 	WRITE_ONCE(trans->aborted, error);
2704 	WRITE_ONCE(trans->transaction->aborted, error);
2705 	if (first_hit && error == -ENOSPC)
2706 		btrfs_dump_space_info_for_trans_abort(fs_info);
2707 	/* Wake up anybody who may be waiting on this transaction */
2708 	wake_up(&fs_info->transaction_wait);
2709 	wake_up(&fs_info->transaction_blocked_wait);
2710 	__btrfs_handle_fs_error(fs_info, function, line, error, NULL);
2711 }
2712 
btrfs_transaction_init(void)2713 int __init btrfs_transaction_init(void)
2714 {
2715 	btrfs_trans_handle_cachep = KMEM_CACHE(btrfs_trans_handle, SLAB_TEMPORARY);
2716 	if (!btrfs_trans_handle_cachep)
2717 		return -ENOMEM;
2718 	return 0;
2719 }
2720 
btrfs_transaction_exit(void)2721 void __cold btrfs_transaction_exit(void)
2722 {
2723 	kmem_cache_destroy(btrfs_trans_handle_cachep);
2724 }
2725