• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/writeback.h>
10 #include <linux/pagemap.h>
11 #include <linux/blkdev.h>
12 #include <linux/uuid.h>
13 #include "ctree.h"
14 #include "disk-io.h"
15 #include "transaction.h"
16 #include "locking.h"
17 #include "tree-log.h"
18 #include "inode-map.h"
19 #include "volumes.h"
20 #include "dev-replace.h"
21 #include "qgroup.h"
22 
23 #define BTRFS_ROOT_TRANS_TAG 0
24 
25 static const unsigned int btrfs_blocked_trans_types[TRANS_STATE_MAX] = {
26 	[TRANS_STATE_RUNNING]		= 0U,
27 	[TRANS_STATE_BLOCKED]		=  __TRANS_START,
28 	[TRANS_STATE_COMMIT_START]	= (__TRANS_START | __TRANS_ATTACH),
29 	[TRANS_STATE_COMMIT_DOING]	= (__TRANS_START |
30 					   __TRANS_ATTACH |
31 					   __TRANS_JOIN |
32 					   __TRANS_JOIN_NOSTART),
33 	[TRANS_STATE_UNBLOCKED]		= (__TRANS_START |
34 					   __TRANS_ATTACH |
35 					   __TRANS_JOIN |
36 					   __TRANS_JOIN_NOLOCK |
37 					   __TRANS_JOIN_NOSTART),
38 	[TRANS_STATE_COMPLETED]		= (__TRANS_START |
39 					   __TRANS_ATTACH |
40 					   __TRANS_JOIN |
41 					   __TRANS_JOIN_NOLOCK |
42 					   __TRANS_JOIN_NOSTART),
43 };
44 
btrfs_put_transaction(struct btrfs_transaction * transaction)45 void btrfs_put_transaction(struct btrfs_transaction *transaction)
46 {
47 	WARN_ON(refcount_read(&transaction->use_count) == 0);
48 	if (refcount_dec_and_test(&transaction->use_count)) {
49 		BUG_ON(!list_empty(&transaction->list));
50 		WARN_ON(!RB_EMPTY_ROOT(&transaction->delayed_refs.href_root));
51 		if (transaction->delayed_refs.pending_csums)
52 			btrfs_err(transaction->fs_info,
53 				  "pending csums is %llu",
54 				  transaction->delayed_refs.pending_csums);
55 		while (!list_empty(&transaction->pending_chunks)) {
56 			struct extent_map *em;
57 
58 			em = list_first_entry(&transaction->pending_chunks,
59 					      struct extent_map, list);
60 			list_del_init(&em->list);
61 			free_extent_map(em);
62 		}
63 		/*
64 		 * If any block groups are found in ->deleted_bgs then it's
65 		 * because the transaction was aborted and a commit did not
66 		 * happen (things failed before writing the new superblock
67 		 * and calling btrfs_finish_extent_commit()), so we can not
68 		 * discard the physical locations of the block groups.
69 		 */
70 		while (!list_empty(&transaction->deleted_bgs)) {
71 			struct btrfs_block_group_cache *cache;
72 
73 			cache = list_first_entry(&transaction->deleted_bgs,
74 						 struct btrfs_block_group_cache,
75 						 bg_list);
76 			list_del_init(&cache->bg_list);
77 			btrfs_put_block_group_trimming(cache);
78 			btrfs_put_block_group(cache);
79 		}
80 		kfree(transaction);
81 	}
82 }
83 
clear_btree_io_tree(struct extent_io_tree * tree)84 static void clear_btree_io_tree(struct extent_io_tree *tree)
85 {
86 	spin_lock(&tree->lock);
87 	/*
88 	 * Do a single barrier for the waitqueue_active check here, the state
89 	 * of the waitqueue should not change once clear_btree_io_tree is
90 	 * called.
91 	 */
92 	smp_mb();
93 	while (!RB_EMPTY_ROOT(&tree->state)) {
94 		struct rb_node *node;
95 		struct extent_state *state;
96 
97 		node = rb_first(&tree->state);
98 		state = rb_entry(node, struct extent_state, rb_node);
99 		rb_erase(&state->rb_node, &tree->state);
100 		RB_CLEAR_NODE(&state->rb_node);
101 		/*
102 		 * btree io trees aren't supposed to have tasks waiting for
103 		 * changes in the flags of extent states ever.
104 		 */
105 		ASSERT(!waitqueue_active(&state->wq));
106 		free_extent_state(state);
107 
108 		cond_resched_lock(&tree->lock);
109 	}
110 	spin_unlock(&tree->lock);
111 }
112 
switch_commit_roots(struct btrfs_transaction * trans)113 static noinline void switch_commit_roots(struct btrfs_transaction *trans)
114 {
115 	struct btrfs_fs_info *fs_info = trans->fs_info;
116 	struct btrfs_root *root, *tmp;
117 
118 	down_write(&fs_info->commit_root_sem);
119 	list_for_each_entry_safe(root, tmp, &trans->switch_commits,
120 				 dirty_list) {
121 		list_del_init(&root->dirty_list);
122 		free_extent_buffer(root->commit_root);
123 		root->commit_root = btrfs_root_node(root);
124 		if (is_fstree(root->objectid))
125 			btrfs_unpin_free_ino(root);
126 		clear_btree_io_tree(&root->dirty_log_pages);
127 	}
128 
129 	/* We can free old roots now. */
130 	spin_lock(&trans->dropped_roots_lock);
131 	while (!list_empty(&trans->dropped_roots)) {
132 		root = list_first_entry(&trans->dropped_roots,
133 					struct btrfs_root, root_list);
134 		list_del_init(&root->root_list);
135 		spin_unlock(&trans->dropped_roots_lock);
136 		btrfs_drop_and_free_fs_root(fs_info, root);
137 		spin_lock(&trans->dropped_roots_lock);
138 	}
139 	spin_unlock(&trans->dropped_roots_lock);
140 	up_write(&fs_info->commit_root_sem);
141 }
142 
extwriter_counter_inc(struct btrfs_transaction * trans,unsigned int type)143 static inline void extwriter_counter_inc(struct btrfs_transaction *trans,
144 					 unsigned int type)
145 {
146 	if (type & TRANS_EXTWRITERS)
147 		atomic_inc(&trans->num_extwriters);
148 }
149 
extwriter_counter_dec(struct btrfs_transaction * trans,unsigned int type)150 static inline void extwriter_counter_dec(struct btrfs_transaction *trans,
151 					 unsigned int type)
152 {
153 	if (type & TRANS_EXTWRITERS)
154 		atomic_dec(&trans->num_extwriters);
155 }
156 
extwriter_counter_init(struct btrfs_transaction * trans,unsigned int type)157 static inline void extwriter_counter_init(struct btrfs_transaction *trans,
158 					  unsigned int type)
159 {
160 	atomic_set(&trans->num_extwriters, ((type & TRANS_EXTWRITERS) ? 1 : 0));
161 }
162 
extwriter_counter_read(struct btrfs_transaction * trans)163 static inline int extwriter_counter_read(struct btrfs_transaction *trans)
164 {
165 	return atomic_read(&trans->num_extwriters);
166 }
167 
168 /*
169  * either allocate a new transaction or hop into the existing one
170  */
join_transaction(struct btrfs_fs_info * fs_info,unsigned int type)171 static noinline int join_transaction(struct btrfs_fs_info *fs_info,
172 				     unsigned int type)
173 {
174 	struct btrfs_transaction *cur_trans;
175 
176 	spin_lock(&fs_info->trans_lock);
177 loop:
178 	/* The file system has been taken offline. No new transactions. */
179 	if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) {
180 		spin_unlock(&fs_info->trans_lock);
181 		return -EROFS;
182 	}
183 
184 	cur_trans = fs_info->running_transaction;
185 	if (cur_trans) {
186 		if (cur_trans->aborted) {
187 			spin_unlock(&fs_info->trans_lock);
188 			return cur_trans->aborted;
189 		}
190 		if (btrfs_blocked_trans_types[cur_trans->state] & type) {
191 			spin_unlock(&fs_info->trans_lock);
192 			return -EBUSY;
193 		}
194 		refcount_inc(&cur_trans->use_count);
195 		atomic_inc(&cur_trans->num_writers);
196 		extwriter_counter_inc(cur_trans, type);
197 		spin_unlock(&fs_info->trans_lock);
198 		return 0;
199 	}
200 	spin_unlock(&fs_info->trans_lock);
201 
202 	/*
203 	 * If we are ATTACH, we just want to catch the current transaction,
204 	 * and commit it. If there is no transaction, just return ENOENT.
205 	 */
206 	if (type == TRANS_ATTACH)
207 		return -ENOENT;
208 
209 	/*
210 	 * JOIN_NOLOCK only happens during the transaction commit, so
211 	 * it is impossible that ->running_transaction is NULL
212 	 */
213 	BUG_ON(type == TRANS_JOIN_NOLOCK);
214 
215 	cur_trans = kmalloc(sizeof(*cur_trans), GFP_NOFS);
216 	if (!cur_trans)
217 		return -ENOMEM;
218 
219 	spin_lock(&fs_info->trans_lock);
220 	if (fs_info->running_transaction) {
221 		/*
222 		 * someone started a transaction after we unlocked.  Make sure
223 		 * to redo the checks above
224 		 */
225 		kfree(cur_trans);
226 		goto loop;
227 	} else if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) {
228 		spin_unlock(&fs_info->trans_lock);
229 		kfree(cur_trans);
230 		return -EROFS;
231 	}
232 
233 	cur_trans->fs_info = fs_info;
234 	atomic_set(&cur_trans->num_writers, 1);
235 	extwriter_counter_init(cur_trans, type);
236 	init_waitqueue_head(&cur_trans->writer_wait);
237 	init_waitqueue_head(&cur_trans->commit_wait);
238 	init_waitqueue_head(&cur_trans->pending_wait);
239 	cur_trans->state = TRANS_STATE_RUNNING;
240 	/*
241 	 * One for this trans handle, one so it will live on until we
242 	 * commit the transaction.
243 	 */
244 	refcount_set(&cur_trans->use_count, 2);
245 	atomic_set(&cur_trans->pending_ordered, 0);
246 	cur_trans->flags = 0;
247 	cur_trans->start_time = ktime_get_seconds();
248 
249 	memset(&cur_trans->delayed_refs, 0, sizeof(cur_trans->delayed_refs));
250 
251 	cur_trans->delayed_refs.href_root = RB_ROOT;
252 	cur_trans->delayed_refs.dirty_extent_root = RB_ROOT;
253 	atomic_set(&cur_trans->delayed_refs.num_entries, 0);
254 
255 	/*
256 	 * although the tree mod log is per file system and not per transaction,
257 	 * the log must never go across transaction boundaries.
258 	 */
259 	smp_mb();
260 	if (!list_empty(&fs_info->tree_mod_seq_list))
261 		WARN(1, KERN_ERR "BTRFS: tree_mod_seq_list not empty when creating a fresh transaction\n");
262 	if (!RB_EMPTY_ROOT(&fs_info->tree_mod_log))
263 		WARN(1, KERN_ERR "BTRFS: tree_mod_log rb tree not empty when creating a fresh transaction\n");
264 	atomic64_set(&fs_info->tree_mod_seq, 0);
265 
266 	spin_lock_init(&cur_trans->delayed_refs.lock);
267 
268 	INIT_LIST_HEAD(&cur_trans->pending_snapshots);
269 	INIT_LIST_HEAD(&cur_trans->pending_chunks);
270 	INIT_LIST_HEAD(&cur_trans->switch_commits);
271 	INIT_LIST_HEAD(&cur_trans->dirty_bgs);
272 	INIT_LIST_HEAD(&cur_trans->io_bgs);
273 	INIT_LIST_HEAD(&cur_trans->dropped_roots);
274 	mutex_init(&cur_trans->cache_write_mutex);
275 	cur_trans->num_dirty_bgs = 0;
276 	spin_lock_init(&cur_trans->dirty_bgs_lock);
277 	INIT_LIST_HEAD(&cur_trans->deleted_bgs);
278 	spin_lock_init(&cur_trans->dropped_roots_lock);
279 	list_add_tail(&cur_trans->list, &fs_info->trans_list);
280 	extent_io_tree_init(&cur_trans->dirty_pages,
281 			     fs_info->btree_inode);
282 	fs_info->generation++;
283 	cur_trans->transid = fs_info->generation;
284 	fs_info->running_transaction = cur_trans;
285 	cur_trans->aborted = 0;
286 	spin_unlock(&fs_info->trans_lock);
287 
288 	return 0;
289 }
290 
291 /*
292  * this does all the record keeping required to make sure that a reference
293  * counted root is properly recorded in a given transaction.  This is required
294  * to make sure the old root from before we joined the transaction is deleted
295  * when the transaction commits
296  */
record_root_in_trans(struct btrfs_trans_handle * trans,struct btrfs_root * root,int force)297 static int record_root_in_trans(struct btrfs_trans_handle *trans,
298 			       struct btrfs_root *root,
299 			       int force)
300 {
301 	struct btrfs_fs_info *fs_info = root->fs_info;
302 
303 	if ((test_bit(BTRFS_ROOT_REF_COWS, &root->state) &&
304 	    root->last_trans < trans->transid) || force) {
305 		WARN_ON(root == fs_info->extent_root);
306 		WARN_ON(!force && root->commit_root != root->node);
307 
308 		/*
309 		 * see below for IN_TRANS_SETUP usage rules
310 		 * we have the reloc mutex held now, so there
311 		 * is only one writer in this function
312 		 */
313 		set_bit(BTRFS_ROOT_IN_TRANS_SETUP, &root->state);
314 
315 		/* make sure readers find IN_TRANS_SETUP before
316 		 * they find our root->last_trans update
317 		 */
318 		smp_wmb();
319 
320 		spin_lock(&fs_info->fs_roots_radix_lock);
321 		if (root->last_trans == trans->transid && !force) {
322 			spin_unlock(&fs_info->fs_roots_radix_lock);
323 			return 0;
324 		}
325 		radix_tree_tag_set(&fs_info->fs_roots_radix,
326 				   (unsigned long)root->root_key.objectid,
327 				   BTRFS_ROOT_TRANS_TAG);
328 		spin_unlock(&fs_info->fs_roots_radix_lock);
329 		root->last_trans = trans->transid;
330 
331 		/* this is pretty tricky.  We don't want to
332 		 * take the relocation lock in btrfs_record_root_in_trans
333 		 * unless we're really doing the first setup for this root in
334 		 * this transaction.
335 		 *
336 		 * Normally we'd use root->last_trans as a flag to decide
337 		 * if we want to take the expensive mutex.
338 		 *
339 		 * But, we have to set root->last_trans before we
340 		 * init the relocation root, otherwise, we trip over warnings
341 		 * in ctree.c.  The solution used here is to flag ourselves
342 		 * with root IN_TRANS_SETUP.  When this is 1, we're still
343 		 * fixing up the reloc trees and everyone must wait.
344 		 *
345 		 * When this is zero, they can trust root->last_trans and fly
346 		 * through btrfs_record_root_in_trans without having to take the
347 		 * lock.  smp_wmb() makes sure that all the writes above are
348 		 * done before we pop in the zero below
349 		 */
350 		btrfs_init_reloc_root(trans, root);
351 		smp_mb__before_atomic();
352 		clear_bit(BTRFS_ROOT_IN_TRANS_SETUP, &root->state);
353 	}
354 	return 0;
355 }
356 
357 
btrfs_add_dropped_root(struct btrfs_trans_handle * trans,struct btrfs_root * root)358 void btrfs_add_dropped_root(struct btrfs_trans_handle *trans,
359 			    struct btrfs_root *root)
360 {
361 	struct btrfs_fs_info *fs_info = root->fs_info;
362 	struct btrfs_transaction *cur_trans = trans->transaction;
363 
364 	/* Add ourselves to the transaction dropped list */
365 	spin_lock(&cur_trans->dropped_roots_lock);
366 	list_add_tail(&root->root_list, &cur_trans->dropped_roots);
367 	spin_unlock(&cur_trans->dropped_roots_lock);
368 
369 	/* Make sure we don't try to update the root at commit time */
370 	spin_lock(&fs_info->fs_roots_radix_lock);
371 	radix_tree_tag_clear(&fs_info->fs_roots_radix,
372 			     (unsigned long)root->root_key.objectid,
373 			     BTRFS_ROOT_TRANS_TAG);
374 	spin_unlock(&fs_info->fs_roots_radix_lock);
375 }
376 
btrfs_record_root_in_trans(struct btrfs_trans_handle * trans,struct btrfs_root * root)377 int btrfs_record_root_in_trans(struct btrfs_trans_handle *trans,
378 			       struct btrfs_root *root)
379 {
380 	struct btrfs_fs_info *fs_info = root->fs_info;
381 
382 	if (!test_bit(BTRFS_ROOT_REF_COWS, &root->state))
383 		return 0;
384 
385 	/*
386 	 * see record_root_in_trans for comments about IN_TRANS_SETUP usage
387 	 * and barriers
388 	 */
389 	smp_rmb();
390 	if (root->last_trans == trans->transid &&
391 	    !test_bit(BTRFS_ROOT_IN_TRANS_SETUP, &root->state))
392 		return 0;
393 
394 	mutex_lock(&fs_info->reloc_mutex);
395 	record_root_in_trans(trans, root, 0);
396 	mutex_unlock(&fs_info->reloc_mutex);
397 
398 	return 0;
399 }
400 
is_transaction_blocked(struct btrfs_transaction * trans)401 static inline int is_transaction_blocked(struct btrfs_transaction *trans)
402 {
403 	return (trans->state >= TRANS_STATE_BLOCKED &&
404 		trans->state < TRANS_STATE_UNBLOCKED &&
405 		!trans->aborted);
406 }
407 
408 /* wait for commit against the current transaction to become unblocked
409  * when this is done, it is safe to start a new transaction, but the current
410  * transaction might not be fully on disk.
411  */
wait_current_trans(struct btrfs_fs_info * fs_info)412 static void wait_current_trans(struct btrfs_fs_info *fs_info)
413 {
414 	struct btrfs_transaction *cur_trans;
415 
416 	spin_lock(&fs_info->trans_lock);
417 	cur_trans = fs_info->running_transaction;
418 	if (cur_trans && is_transaction_blocked(cur_trans)) {
419 		refcount_inc(&cur_trans->use_count);
420 		spin_unlock(&fs_info->trans_lock);
421 
422 		wait_event(fs_info->transaction_wait,
423 			   cur_trans->state >= TRANS_STATE_UNBLOCKED ||
424 			   cur_trans->aborted);
425 		btrfs_put_transaction(cur_trans);
426 	} else {
427 		spin_unlock(&fs_info->trans_lock);
428 	}
429 }
430 
may_wait_transaction(struct btrfs_fs_info * fs_info,int type)431 static int may_wait_transaction(struct btrfs_fs_info *fs_info, int type)
432 {
433 	if (test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags))
434 		return 0;
435 
436 	if (type == TRANS_START)
437 		return 1;
438 
439 	return 0;
440 }
441 
need_reserve_reloc_root(struct btrfs_root * root)442 static inline bool need_reserve_reloc_root(struct btrfs_root *root)
443 {
444 	struct btrfs_fs_info *fs_info = root->fs_info;
445 
446 	if (!fs_info->reloc_ctl ||
447 	    !test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
448 	    root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID ||
449 	    root->reloc_root)
450 		return false;
451 
452 	return true;
453 }
454 
455 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)456 start_transaction(struct btrfs_root *root, unsigned int num_items,
457 		  unsigned int type, enum btrfs_reserve_flush_enum flush,
458 		  bool enforce_qgroups)
459 {
460 	struct btrfs_fs_info *fs_info = root->fs_info;
461 
462 	struct btrfs_trans_handle *h;
463 	struct btrfs_transaction *cur_trans;
464 	u64 num_bytes = 0;
465 	u64 qgroup_reserved = 0;
466 	bool reloc_reserved = false;
467 	int ret;
468 
469 	/* Send isn't supposed to start transactions. */
470 	ASSERT(current->journal_info != BTRFS_SEND_TRANS_STUB);
471 
472 	if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
473 		return ERR_PTR(-EROFS);
474 
475 	if (current->journal_info) {
476 		WARN_ON(type & TRANS_EXTWRITERS);
477 		h = current->journal_info;
478 		refcount_inc(&h->use_count);
479 		WARN_ON(refcount_read(&h->use_count) > 2);
480 		h->orig_rsv = h->block_rsv;
481 		h->block_rsv = NULL;
482 		goto got_it;
483 	}
484 
485 	/*
486 	 * Do the reservation before we join the transaction so we can do all
487 	 * the appropriate flushing if need be.
488 	 */
489 	if (num_items && root != fs_info->chunk_root) {
490 		qgroup_reserved = num_items * fs_info->nodesize;
491 		ret = btrfs_qgroup_reserve_meta_pertrans(root, qgroup_reserved,
492 				enforce_qgroups);
493 		if (ret)
494 			return ERR_PTR(ret);
495 
496 		num_bytes = btrfs_calc_trans_metadata_size(fs_info, num_items);
497 		/*
498 		 * Do the reservation for the relocation root creation
499 		 */
500 		if (need_reserve_reloc_root(root)) {
501 			num_bytes += fs_info->nodesize;
502 			reloc_reserved = true;
503 		}
504 
505 		ret = btrfs_block_rsv_add(root, &fs_info->trans_block_rsv,
506 					  num_bytes, flush);
507 		if (ret)
508 			goto reserve_fail;
509 	}
510 again:
511 	h = kmem_cache_zalloc(btrfs_trans_handle_cachep, GFP_NOFS);
512 	if (!h) {
513 		ret = -ENOMEM;
514 		goto alloc_fail;
515 	}
516 
517 	/*
518 	 * If we are JOIN_NOLOCK we're already committing a transaction and
519 	 * waiting on this guy, so we don't need to do the sb_start_intwrite
520 	 * because we're already holding a ref.  We need this because we could
521 	 * have raced in and did an fsync() on a file which can kick a commit
522 	 * and then we deadlock with somebody doing a freeze.
523 	 *
524 	 * If we are ATTACH, it means we just want to catch the current
525 	 * transaction and commit it, so we needn't do sb_start_intwrite().
526 	 */
527 	if (type & __TRANS_FREEZABLE)
528 		sb_start_intwrite(fs_info->sb);
529 
530 	if (may_wait_transaction(fs_info, type))
531 		wait_current_trans(fs_info);
532 
533 	do {
534 		ret = join_transaction(fs_info, type);
535 		if (ret == -EBUSY) {
536 			wait_current_trans(fs_info);
537 			if (unlikely(type == TRANS_ATTACH ||
538 				     type == TRANS_JOIN_NOSTART))
539 				ret = -ENOENT;
540 		}
541 	} while (ret == -EBUSY);
542 
543 	if (ret < 0)
544 		goto join_fail;
545 
546 	cur_trans = fs_info->running_transaction;
547 
548 	h->transid = cur_trans->transid;
549 	h->transaction = cur_trans;
550 	h->root = root;
551 	refcount_set(&h->use_count, 1);
552 	h->fs_info = root->fs_info;
553 
554 	h->type = type;
555 	h->can_flush_pending_bgs = true;
556 	INIT_LIST_HEAD(&h->new_bgs);
557 
558 	smp_mb();
559 	if (cur_trans->state >= TRANS_STATE_BLOCKED &&
560 	    may_wait_transaction(fs_info, type)) {
561 		current->journal_info = h;
562 		btrfs_commit_transaction(h);
563 		goto again;
564 	}
565 
566 	if (num_bytes) {
567 		trace_btrfs_space_reservation(fs_info, "transaction",
568 					      h->transid, num_bytes, 1);
569 		h->block_rsv = &fs_info->trans_block_rsv;
570 		h->bytes_reserved = num_bytes;
571 		h->reloc_reserved = reloc_reserved;
572 	}
573 
574 got_it:
575 	if (!current->journal_info)
576 		current->journal_info = h;
577 
578 	/*
579 	 * btrfs_record_root_in_trans() needs to alloc new extents, and may
580 	 * call btrfs_join_transaction() while we're also starting a
581 	 * transaction.
582 	 *
583 	 * Thus it need to be called after current->journal_info initialized,
584 	 * or we can deadlock.
585 	 */
586 	btrfs_record_root_in_trans(h, root);
587 
588 	return h;
589 
590 join_fail:
591 	if (type & __TRANS_FREEZABLE)
592 		sb_end_intwrite(fs_info->sb);
593 	kmem_cache_free(btrfs_trans_handle_cachep, h);
594 alloc_fail:
595 	if (num_bytes)
596 		btrfs_block_rsv_release(fs_info, &fs_info->trans_block_rsv,
597 					num_bytes);
598 reserve_fail:
599 	btrfs_qgroup_free_meta_pertrans(root, qgroup_reserved);
600 	return ERR_PTR(ret);
601 }
602 
btrfs_start_transaction(struct btrfs_root * root,unsigned int num_items)603 struct btrfs_trans_handle *btrfs_start_transaction(struct btrfs_root *root,
604 						   unsigned int num_items)
605 {
606 	return start_transaction(root, num_items, TRANS_START,
607 				 BTRFS_RESERVE_FLUSH_ALL, true);
608 }
609 
btrfs_start_transaction_fallback_global_rsv(struct btrfs_root * root,unsigned int num_items,int min_factor)610 struct btrfs_trans_handle *btrfs_start_transaction_fallback_global_rsv(
611 					struct btrfs_root *root,
612 					unsigned int num_items,
613 					int min_factor)
614 {
615 	struct btrfs_fs_info *fs_info = root->fs_info;
616 	struct btrfs_trans_handle *trans;
617 	u64 num_bytes;
618 	int ret;
619 
620 	/*
621 	 * We have two callers: unlink and block group removal.  The
622 	 * former should succeed even if we will temporarily exceed
623 	 * quota and the latter operates on the extent root so
624 	 * qgroup enforcement is ignored anyway.
625 	 */
626 	trans = start_transaction(root, num_items, TRANS_START,
627 				  BTRFS_RESERVE_FLUSH_ALL, false);
628 	if (!IS_ERR(trans) || PTR_ERR(trans) != -ENOSPC)
629 		return trans;
630 
631 	trans = btrfs_start_transaction(root, 0);
632 	if (IS_ERR(trans))
633 		return trans;
634 
635 	num_bytes = btrfs_calc_trans_metadata_size(fs_info, num_items);
636 	ret = btrfs_cond_migrate_bytes(fs_info, &fs_info->trans_block_rsv,
637 				       num_bytes, min_factor);
638 	if (ret) {
639 		btrfs_end_transaction(trans);
640 		return ERR_PTR(ret);
641 	}
642 
643 	trans->block_rsv = &fs_info->trans_block_rsv;
644 	trans->bytes_reserved = num_bytes;
645 	trace_btrfs_space_reservation(fs_info, "transaction",
646 				      trans->transid, num_bytes, 1);
647 
648 	return trans;
649 }
650 
btrfs_join_transaction(struct btrfs_root * root)651 struct btrfs_trans_handle *btrfs_join_transaction(struct btrfs_root *root)
652 {
653 	return start_transaction(root, 0, TRANS_JOIN, BTRFS_RESERVE_NO_FLUSH,
654 				 true);
655 }
656 
btrfs_join_transaction_nolock(struct btrfs_root * root)657 struct btrfs_trans_handle *btrfs_join_transaction_nolock(struct btrfs_root *root)
658 {
659 	return start_transaction(root, 0, TRANS_JOIN_NOLOCK,
660 				 BTRFS_RESERVE_NO_FLUSH, true);
661 }
662 
663 /*
664  * Similar to regular join but it never starts a transaction when none is
665  * running or after waiting for the current one to finish.
666  */
btrfs_join_transaction_nostart(struct btrfs_root * root)667 struct btrfs_trans_handle *btrfs_join_transaction_nostart(struct btrfs_root *root)
668 {
669 	return start_transaction(root, 0, TRANS_JOIN_NOSTART,
670 				 BTRFS_RESERVE_NO_FLUSH, true);
671 }
672 
673 /*
674  * btrfs_attach_transaction() - catch the running transaction
675  *
676  * It is used when we want to commit the current the transaction, but
677  * don't want to start a new one.
678  *
679  * Note: If this function return -ENOENT, it just means there is no
680  * running transaction. But it is possible that the inactive transaction
681  * is still in the memory, not fully on disk. If you hope there is no
682  * inactive transaction in the fs when -ENOENT is returned, you should
683  * invoke
684  *     btrfs_attach_transaction_barrier()
685  */
btrfs_attach_transaction(struct btrfs_root * root)686 struct btrfs_trans_handle *btrfs_attach_transaction(struct btrfs_root *root)
687 {
688 	return start_transaction(root, 0, TRANS_ATTACH,
689 				 BTRFS_RESERVE_NO_FLUSH, true);
690 }
691 
692 /*
693  * btrfs_attach_transaction_barrier() - catch the running transaction
694  *
695  * It is similar to the above function, the differentia is this one
696  * will wait for all the inactive transactions until they fully
697  * complete.
698  */
699 struct btrfs_trans_handle *
btrfs_attach_transaction_barrier(struct btrfs_root * root)700 btrfs_attach_transaction_barrier(struct btrfs_root *root)
701 {
702 	struct btrfs_trans_handle *trans;
703 
704 	trans = start_transaction(root, 0, TRANS_ATTACH,
705 				  BTRFS_RESERVE_NO_FLUSH, true);
706 	if (trans == ERR_PTR(-ENOENT))
707 		btrfs_wait_for_commit(root->fs_info, 0);
708 
709 	return trans;
710 }
711 
712 /* wait for a transaction commit to be fully complete */
wait_for_commit(struct btrfs_transaction * commit)713 static noinline void wait_for_commit(struct btrfs_transaction *commit)
714 {
715 	wait_event(commit->commit_wait, commit->state == TRANS_STATE_COMPLETED);
716 }
717 
btrfs_wait_for_commit(struct btrfs_fs_info * fs_info,u64 transid)718 int btrfs_wait_for_commit(struct btrfs_fs_info *fs_info, u64 transid)
719 {
720 	struct btrfs_transaction *cur_trans = NULL, *t;
721 	int ret = 0;
722 
723 	if (transid) {
724 		if (transid <= fs_info->last_trans_committed)
725 			goto out;
726 
727 		/* find specified transaction */
728 		spin_lock(&fs_info->trans_lock);
729 		list_for_each_entry(t, &fs_info->trans_list, list) {
730 			if (t->transid == transid) {
731 				cur_trans = t;
732 				refcount_inc(&cur_trans->use_count);
733 				ret = 0;
734 				break;
735 			}
736 			if (t->transid > transid) {
737 				ret = 0;
738 				break;
739 			}
740 		}
741 		spin_unlock(&fs_info->trans_lock);
742 
743 		/*
744 		 * The specified transaction doesn't exist, or we
745 		 * raced with btrfs_commit_transaction
746 		 */
747 		if (!cur_trans) {
748 			if (transid > fs_info->last_trans_committed)
749 				ret = -EINVAL;
750 			goto out;
751 		}
752 	} else {
753 		/* find newest transaction that is committing | committed */
754 		spin_lock(&fs_info->trans_lock);
755 		list_for_each_entry_reverse(t, &fs_info->trans_list,
756 					    list) {
757 			if (t->state >= TRANS_STATE_COMMIT_START) {
758 				if (t->state == TRANS_STATE_COMPLETED)
759 					break;
760 				cur_trans = t;
761 				refcount_inc(&cur_trans->use_count);
762 				break;
763 			}
764 		}
765 		spin_unlock(&fs_info->trans_lock);
766 		if (!cur_trans)
767 			goto out;  /* nothing committing|committed */
768 	}
769 
770 	wait_for_commit(cur_trans);
771 	btrfs_put_transaction(cur_trans);
772 out:
773 	return ret;
774 }
775 
btrfs_throttle(struct btrfs_fs_info * fs_info)776 void btrfs_throttle(struct btrfs_fs_info *fs_info)
777 {
778 	wait_current_trans(fs_info);
779 }
780 
should_end_transaction(struct btrfs_trans_handle * trans)781 static int should_end_transaction(struct btrfs_trans_handle *trans)
782 {
783 	struct btrfs_fs_info *fs_info = trans->fs_info;
784 
785 	if (btrfs_check_space_for_delayed_refs(trans, fs_info))
786 		return 1;
787 
788 	return !!btrfs_block_rsv_check(&fs_info->global_block_rsv, 5);
789 }
790 
btrfs_should_end_transaction(struct btrfs_trans_handle * trans)791 int btrfs_should_end_transaction(struct btrfs_trans_handle *trans)
792 {
793 	struct btrfs_transaction *cur_trans = trans->transaction;
794 	int updates;
795 	int err;
796 
797 	smp_mb();
798 	if (cur_trans->state >= TRANS_STATE_BLOCKED ||
799 	    cur_trans->delayed_refs.flushing)
800 		return 1;
801 
802 	updates = trans->delayed_ref_updates;
803 	trans->delayed_ref_updates = 0;
804 	if (updates) {
805 		err = btrfs_run_delayed_refs(trans, updates * 2);
806 		if (err) /* Error code will also eval true */
807 			return err;
808 	}
809 
810 	return should_end_transaction(trans);
811 }
812 
btrfs_trans_release_metadata(struct btrfs_trans_handle * trans)813 static void btrfs_trans_release_metadata(struct btrfs_trans_handle *trans)
814 
815 {
816 	struct btrfs_fs_info *fs_info = trans->fs_info;
817 
818 	if (!trans->block_rsv) {
819 		ASSERT(!trans->bytes_reserved);
820 		return;
821 	}
822 
823 	if (!trans->bytes_reserved)
824 		return;
825 
826 	ASSERT(trans->block_rsv == &fs_info->trans_block_rsv);
827 	trace_btrfs_space_reservation(fs_info, "transaction",
828 				      trans->transid, trans->bytes_reserved, 0);
829 	btrfs_block_rsv_release(fs_info, trans->block_rsv,
830 				trans->bytes_reserved);
831 	trans->bytes_reserved = 0;
832 }
833 
__btrfs_end_transaction(struct btrfs_trans_handle * trans,int throttle)834 static int __btrfs_end_transaction(struct btrfs_trans_handle *trans,
835 				   int throttle)
836 {
837 	struct btrfs_fs_info *info = trans->fs_info;
838 	struct btrfs_transaction *cur_trans = trans->transaction;
839 	u64 transid = trans->transid;
840 	unsigned long cur = trans->delayed_ref_updates;
841 	int lock = (trans->type != TRANS_JOIN_NOLOCK);
842 	int err = 0;
843 	int must_run_delayed_refs = 0;
844 
845 	if (refcount_read(&trans->use_count) > 1) {
846 		refcount_dec(&trans->use_count);
847 		trans->block_rsv = trans->orig_rsv;
848 		return 0;
849 	}
850 
851 	btrfs_trans_release_metadata(trans);
852 	trans->block_rsv = NULL;
853 
854 	if (!list_empty(&trans->new_bgs))
855 		btrfs_create_pending_block_groups(trans);
856 
857 	trans->delayed_ref_updates = 0;
858 	if (!trans->sync) {
859 		must_run_delayed_refs =
860 			btrfs_should_throttle_delayed_refs(trans, info);
861 		cur = max_t(unsigned long, cur, 32);
862 
863 		/*
864 		 * don't make the caller wait if they are from a NOLOCK
865 		 * or ATTACH transaction, it will deadlock with commit
866 		 */
867 		if (must_run_delayed_refs == 1 &&
868 		    (trans->type & (__TRANS_JOIN_NOLOCK | __TRANS_ATTACH)))
869 			must_run_delayed_refs = 2;
870 	}
871 
872 	btrfs_trans_release_metadata(trans);
873 	trans->block_rsv = NULL;
874 
875 	if (!list_empty(&trans->new_bgs))
876 		btrfs_create_pending_block_groups(trans);
877 
878 	btrfs_trans_release_chunk_metadata(trans);
879 
880 	if (lock && should_end_transaction(trans) &&
881 	    READ_ONCE(cur_trans->state) == TRANS_STATE_RUNNING) {
882 		spin_lock(&info->trans_lock);
883 		if (cur_trans->state == TRANS_STATE_RUNNING)
884 			cur_trans->state = TRANS_STATE_BLOCKED;
885 		spin_unlock(&info->trans_lock);
886 	}
887 
888 	if (lock && READ_ONCE(cur_trans->state) == TRANS_STATE_BLOCKED) {
889 		if (throttle)
890 			return btrfs_commit_transaction(trans);
891 		else
892 			wake_up_process(info->transaction_kthread);
893 	}
894 
895 	if (trans->type & __TRANS_FREEZABLE)
896 		sb_end_intwrite(info->sb);
897 
898 	WARN_ON(cur_trans != info->running_transaction);
899 	WARN_ON(atomic_read(&cur_trans->num_writers) < 1);
900 	atomic_dec(&cur_trans->num_writers);
901 	extwriter_counter_dec(cur_trans, trans->type);
902 
903 	cond_wake_up(&cur_trans->writer_wait);
904 	btrfs_put_transaction(cur_trans);
905 
906 	if (current->journal_info == trans)
907 		current->journal_info = NULL;
908 
909 	if (throttle)
910 		btrfs_run_delayed_iputs(info);
911 
912 	if (trans->aborted ||
913 	    test_bit(BTRFS_FS_STATE_ERROR, &info->fs_state)) {
914 		wake_up_process(info->transaction_kthread);
915 		err = -EIO;
916 	}
917 
918 	kmem_cache_free(btrfs_trans_handle_cachep, trans);
919 	if (must_run_delayed_refs) {
920 		btrfs_async_run_delayed_refs(info, cur, transid,
921 					     must_run_delayed_refs == 1);
922 	}
923 	return err;
924 }
925 
btrfs_end_transaction(struct btrfs_trans_handle * trans)926 int btrfs_end_transaction(struct btrfs_trans_handle *trans)
927 {
928 	return __btrfs_end_transaction(trans, 0);
929 }
930 
btrfs_end_transaction_throttle(struct btrfs_trans_handle * trans)931 int btrfs_end_transaction_throttle(struct btrfs_trans_handle *trans)
932 {
933 	return __btrfs_end_transaction(trans, 1);
934 }
935 
936 /*
937  * when btree blocks are allocated, they have some corresponding bits set for
938  * them in one of two extent_io trees.  This is used to make sure all of
939  * those extents are sent to disk but does not wait on them
940  */
btrfs_write_marked_extents(struct btrfs_fs_info * fs_info,struct extent_io_tree * dirty_pages,int mark)941 int btrfs_write_marked_extents(struct btrfs_fs_info *fs_info,
942 			       struct extent_io_tree *dirty_pages, int mark)
943 {
944 	int err = 0;
945 	int werr = 0;
946 	struct address_space *mapping = fs_info->btree_inode->i_mapping;
947 	struct extent_state *cached_state = NULL;
948 	u64 start = 0;
949 	u64 end;
950 
951 	atomic_inc(&BTRFS_I(fs_info->btree_inode)->sync_writers);
952 	while (!find_first_extent_bit(dirty_pages, start, &start, &end,
953 				      mark, &cached_state)) {
954 		bool wait_writeback = false;
955 
956 		err = convert_extent_bit(dirty_pages, start, end,
957 					 EXTENT_NEED_WAIT,
958 					 mark, &cached_state);
959 		/*
960 		 * convert_extent_bit can return -ENOMEM, which is most of the
961 		 * time a temporary error. So when it happens, ignore the error
962 		 * and wait for writeback of this range to finish - because we
963 		 * failed to set the bit EXTENT_NEED_WAIT for the range, a call
964 		 * to __btrfs_wait_marked_extents() would not know that
965 		 * writeback for this range started and therefore wouldn't
966 		 * wait for it to finish - we don't want to commit a
967 		 * superblock that points to btree nodes/leafs for which
968 		 * writeback hasn't finished yet (and without errors).
969 		 * We cleanup any entries left in the io tree when committing
970 		 * the transaction (through clear_btree_io_tree()).
971 		 */
972 		if (err == -ENOMEM) {
973 			err = 0;
974 			wait_writeback = true;
975 		}
976 		if (!err)
977 			err = filemap_fdatawrite_range(mapping, start, end);
978 		if (err)
979 			werr = err;
980 		else if (wait_writeback)
981 			werr = filemap_fdatawait_range(mapping, start, end);
982 		free_extent_state(cached_state);
983 		cached_state = NULL;
984 		cond_resched();
985 		start = end + 1;
986 	}
987 	atomic_dec(&BTRFS_I(fs_info->btree_inode)->sync_writers);
988 	return werr;
989 }
990 
991 /*
992  * when btree blocks are allocated, they have some corresponding bits set for
993  * them in one of two extent_io trees.  This is used to make sure all of
994  * those extents are on disk for transaction or log commit.  We wait
995  * on all the pages and clear them from the dirty pages state tree
996  */
__btrfs_wait_marked_extents(struct btrfs_fs_info * fs_info,struct extent_io_tree * dirty_pages)997 static int __btrfs_wait_marked_extents(struct btrfs_fs_info *fs_info,
998 				       struct extent_io_tree *dirty_pages)
999 {
1000 	int err = 0;
1001 	int werr = 0;
1002 	struct address_space *mapping = fs_info->btree_inode->i_mapping;
1003 	struct extent_state *cached_state = NULL;
1004 	u64 start = 0;
1005 	u64 end;
1006 
1007 	while (!find_first_extent_bit(dirty_pages, start, &start, &end,
1008 				      EXTENT_NEED_WAIT, &cached_state)) {
1009 		/*
1010 		 * Ignore -ENOMEM errors returned by clear_extent_bit().
1011 		 * When committing the transaction, we'll remove any entries
1012 		 * left in the io tree. For a log commit, we don't remove them
1013 		 * after committing the log because the tree can be accessed
1014 		 * concurrently - we do it only at transaction commit time when
1015 		 * it's safe to do it (through clear_btree_io_tree()).
1016 		 */
1017 		err = clear_extent_bit(dirty_pages, start, end,
1018 				       EXTENT_NEED_WAIT, 0, 0, &cached_state);
1019 		if (err == -ENOMEM)
1020 			err = 0;
1021 		if (!err)
1022 			err = filemap_fdatawait_range(mapping, start, end);
1023 		if (err)
1024 			werr = err;
1025 		free_extent_state(cached_state);
1026 		cached_state = NULL;
1027 		cond_resched();
1028 		start = end + 1;
1029 	}
1030 	if (err)
1031 		werr = err;
1032 	return werr;
1033 }
1034 
btrfs_wait_extents(struct btrfs_fs_info * fs_info,struct extent_io_tree * dirty_pages)1035 int btrfs_wait_extents(struct btrfs_fs_info *fs_info,
1036 		       struct extent_io_tree *dirty_pages)
1037 {
1038 	bool errors = false;
1039 	int err;
1040 
1041 	err = __btrfs_wait_marked_extents(fs_info, dirty_pages);
1042 	if (test_and_clear_bit(BTRFS_FS_BTREE_ERR, &fs_info->flags))
1043 		errors = true;
1044 
1045 	if (errors && !err)
1046 		err = -EIO;
1047 	return err;
1048 }
1049 
btrfs_wait_tree_log_extents(struct btrfs_root * log_root,int mark)1050 int btrfs_wait_tree_log_extents(struct btrfs_root *log_root, int mark)
1051 {
1052 	struct btrfs_fs_info *fs_info = log_root->fs_info;
1053 	struct extent_io_tree *dirty_pages = &log_root->dirty_log_pages;
1054 	bool errors = false;
1055 	int err;
1056 
1057 	ASSERT(log_root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID);
1058 
1059 	err = __btrfs_wait_marked_extents(fs_info, dirty_pages);
1060 	if ((mark & EXTENT_DIRTY) &&
1061 	    test_and_clear_bit(BTRFS_FS_LOG1_ERR, &fs_info->flags))
1062 		errors = true;
1063 
1064 	if ((mark & EXTENT_NEW) &&
1065 	    test_and_clear_bit(BTRFS_FS_LOG2_ERR, &fs_info->flags))
1066 		errors = true;
1067 
1068 	if (errors && !err)
1069 		err = -EIO;
1070 	return err;
1071 }
1072 
1073 /*
1074  * When btree blocks are allocated the corresponding extents are marked dirty.
1075  * This function ensures such extents are persisted on disk for transaction or
1076  * log commit.
1077  *
1078  * @trans: transaction whose dirty pages we'd like to write
1079  */
btrfs_write_and_wait_transaction(struct btrfs_trans_handle * trans)1080 static int btrfs_write_and_wait_transaction(struct btrfs_trans_handle *trans)
1081 {
1082 	int ret;
1083 	int ret2;
1084 	struct extent_io_tree *dirty_pages = &trans->transaction->dirty_pages;
1085 	struct btrfs_fs_info *fs_info = trans->fs_info;
1086 	struct blk_plug plug;
1087 
1088 	blk_start_plug(&plug);
1089 	ret = btrfs_write_marked_extents(fs_info, dirty_pages, EXTENT_DIRTY);
1090 	blk_finish_plug(&plug);
1091 	ret2 = btrfs_wait_extents(fs_info, dirty_pages);
1092 
1093 	clear_btree_io_tree(&trans->transaction->dirty_pages);
1094 
1095 	if (ret)
1096 		return ret;
1097 	else if (ret2)
1098 		return ret2;
1099 	else
1100 		return 0;
1101 }
1102 
1103 /*
1104  * this is used to update the root pointer in the tree of tree roots.
1105  *
1106  * But, in the case of the extent allocation tree, updating the root
1107  * pointer may allocate blocks which may change the root of the extent
1108  * allocation tree.
1109  *
1110  * So, this loops and repeats and makes sure the cowonly root didn't
1111  * change while the root pointer was being updated in the metadata.
1112  */
update_cowonly_root(struct btrfs_trans_handle * trans,struct btrfs_root * root)1113 static int update_cowonly_root(struct btrfs_trans_handle *trans,
1114 			       struct btrfs_root *root)
1115 {
1116 	int ret;
1117 	u64 old_root_bytenr;
1118 	u64 old_root_used;
1119 	struct btrfs_fs_info *fs_info = root->fs_info;
1120 	struct btrfs_root *tree_root = fs_info->tree_root;
1121 
1122 	old_root_used = btrfs_root_used(&root->root_item);
1123 
1124 	while (1) {
1125 		old_root_bytenr = btrfs_root_bytenr(&root->root_item);
1126 		if (old_root_bytenr == root->node->start &&
1127 		    old_root_used == btrfs_root_used(&root->root_item))
1128 			break;
1129 
1130 		btrfs_set_root_node(&root->root_item, root->node);
1131 		ret = btrfs_update_root(trans, tree_root,
1132 					&root->root_key,
1133 					&root->root_item);
1134 		if (ret)
1135 			return ret;
1136 
1137 		old_root_used = btrfs_root_used(&root->root_item);
1138 	}
1139 
1140 	return 0;
1141 }
1142 
1143 /*
1144  * update all the cowonly tree roots on disk
1145  *
1146  * The error handling in this function may not be obvious. Any of the
1147  * failures will cause the file system to go offline. We still need
1148  * to clean up the delayed refs.
1149  */
commit_cowonly_roots(struct btrfs_trans_handle * trans)1150 static noinline int commit_cowonly_roots(struct btrfs_trans_handle *trans)
1151 {
1152 	struct btrfs_fs_info *fs_info = trans->fs_info;
1153 	struct list_head *dirty_bgs = &trans->transaction->dirty_bgs;
1154 	struct list_head *io_bgs = &trans->transaction->io_bgs;
1155 	struct list_head *next;
1156 	struct extent_buffer *eb;
1157 	int ret;
1158 
1159 	eb = btrfs_lock_root_node(fs_info->tree_root);
1160 	ret = btrfs_cow_block(trans, fs_info->tree_root, eb, NULL,
1161 			      0, &eb);
1162 	btrfs_tree_unlock(eb);
1163 	free_extent_buffer(eb);
1164 
1165 	if (ret)
1166 		return ret;
1167 
1168 	ret = btrfs_run_delayed_refs(trans, (unsigned long)-1);
1169 	if (ret)
1170 		return ret;
1171 
1172 	ret = btrfs_run_dev_stats(trans, fs_info);
1173 	if (ret)
1174 		return ret;
1175 	ret = btrfs_run_dev_replace(trans, fs_info);
1176 	if (ret)
1177 		return ret;
1178 	ret = btrfs_run_qgroups(trans);
1179 	if (ret)
1180 		return ret;
1181 
1182 	ret = btrfs_setup_space_cache(trans, fs_info);
1183 	if (ret)
1184 		return ret;
1185 
1186 	/* run_qgroups might have added some more refs */
1187 	ret = btrfs_run_delayed_refs(trans, (unsigned long)-1);
1188 	if (ret)
1189 		return ret;
1190 again:
1191 	while (!list_empty(&fs_info->dirty_cowonly_roots)) {
1192 		struct btrfs_root *root;
1193 		next = fs_info->dirty_cowonly_roots.next;
1194 		list_del_init(next);
1195 		root = list_entry(next, struct btrfs_root, dirty_list);
1196 		clear_bit(BTRFS_ROOT_DIRTY, &root->state);
1197 
1198 		if (root != fs_info->extent_root)
1199 			list_add_tail(&root->dirty_list,
1200 				      &trans->transaction->switch_commits);
1201 		ret = update_cowonly_root(trans, root);
1202 		if (ret)
1203 			return ret;
1204 		ret = btrfs_run_delayed_refs(trans, (unsigned long)-1);
1205 		if (ret)
1206 			return ret;
1207 	}
1208 
1209 	while (!list_empty(dirty_bgs) || !list_empty(io_bgs)) {
1210 		ret = btrfs_write_dirty_block_groups(trans, fs_info);
1211 		if (ret)
1212 			return ret;
1213 		ret = btrfs_run_delayed_refs(trans, (unsigned long)-1);
1214 		if (ret)
1215 			return ret;
1216 	}
1217 
1218 	if (!list_empty(&fs_info->dirty_cowonly_roots))
1219 		goto again;
1220 
1221 	list_add_tail(&fs_info->extent_root->dirty_list,
1222 		      &trans->transaction->switch_commits);
1223 	btrfs_after_dev_replace_commit(fs_info);
1224 
1225 	return 0;
1226 }
1227 
1228 /*
1229  * dead roots are old snapshots that need to be deleted.  This allocates
1230  * a dirty root struct and adds it into the list of dead roots that need to
1231  * be deleted
1232  */
btrfs_add_dead_root(struct btrfs_root * root)1233 void btrfs_add_dead_root(struct btrfs_root *root)
1234 {
1235 	struct btrfs_fs_info *fs_info = root->fs_info;
1236 
1237 	spin_lock(&fs_info->trans_lock);
1238 	if (list_empty(&root->root_list))
1239 		list_add_tail(&root->root_list, &fs_info->dead_roots);
1240 	spin_unlock(&fs_info->trans_lock);
1241 }
1242 
1243 /*
1244  * update all the cowonly tree roots on disk
1245  */
commit_fs_roots(struct btrfs_trans_handle * trans)1246 static noinline int commit_fs_roots(struct btrfs_trans_handle *trans)
1247 {
1248 	struct btrfs_fs_info *fs_info = trans->fs_info;
1249 	struct btrfs_root *gang[8];
1250 	int i;
1251 	int ret;
1252 	int err = 0;
1253 
1254 	spin_lock(&fs_info->fs_roots_radix_lock);
1255 	while (1) {
1256 		ret = radix_tree_gang_lookup_tag(&fs_info->fs_roots_radix,
1257 						 (void **)gang, 0,
1258 						 ARRAY_SIZE(gang),
1259 						 BTRFS_ROOT_TRANS_TAG);
1260 		if (ret == 0)
1261 			break;
1262 		for (i = 0; i < ret; i++) {
1263 			struct btrfs_root *root = gang[i];
1264 			radix_tree_tag_clear(&fs_info->fs_roots_radix,
1265 					(unsigned long)root->root_key.objectid,
1266 					BTRFS_ROOT_TRANS_TAG);
1267 			spin_unlock(&fs_info->fs_roots_radix_lock);
1268 
1269 			btrfs_free_log(trans, root);
1270 			btrfs_update_reloc_root(trans, root);
1271 
1272 			btrfs_save_ino_cache(root, trans);
1273 
1274 			/* see comments in should_cow_block() */
1275 			clear_bit(BTRFS_ROOT_FORCE_COW, &root->state);
1276 			smp_mb__after_atomic();
1277 
1278 			if (root->commit_root != root->node) {
1279 				list_add_tail(&root->dirty_list,
1280 					&trans->transaction->switch_commits);
1281 				btrfs_set_root_node(&root->root_item,
1282 						    root->node);
1283 			}
1284 
1285 			err = btrfs_update_root(trans, fs_info->tree_root,
1286 						&root->root_key,
1287 						&root->root_item);
1288 			spin_lock(&fs_info->fs_roots_radix_lock);
1289 			if (err)
1290 				break;
1291 			btrfs_qgroup_free_meta_all_pertrans(root);
1292 		}
1293 	}
1294 	spin_unlock(&fs_info->fs_roots_radix_lock);
1295 	return err;
1296 }
1297 
1298 /*
1299  * defrag a given btree.
1300  * Every leaf in the btree is read and defragged.
1301  */
btrfs_defrag_root(struct btrfs_root * root)1302 int btrfs_defrag_root(struct btrfs_root *root)
1303 {
1304 	struct btrfs_fs_info *info = root->fs_info;
1305 	struct btrfs_trans_handle *trans;
1306 	int ret;
1307 
1308 	if (test_and_set_bit(BTRFS_ROOT_DEFRAG_RUNNING, &root->state))
1309 		return 0;
1310 
1311 	while (1) {
1312 		trans = btrfs_start_transaction(root, 0);
1313 		if (IS_ERR(trans))
1314 			return PTR_ERR(trans);
1315 
1316 		ret = btrfs_defrag_leaves(trans, root);
1317 
1318 		btrfs_end_transaction(trans);
1319 		btrfs_btree_balance_dirty(info);
1320 		cond_resched();
1321 
1322 		if (btrfs_fs_closing(info) || ret != -EAGAIN)
1323 			break;
1324 
1325 		if (btrfs_defrag_cancelled(info)) {
1326 			btrfs_debug(info, "defrag_root cancelled");
1327 			ret = -EAGAIN;
1328 			break;
1329 		}
1330 	}
1331 	clear_bit(BTRFS_ROOT_DEFRAG_RUNNING, &root->state);
1332 	return ret;
1333 }
1334 
1335 /*
1336  * Do all special snapshot related qgroup dirty hack.
1337  *
1338  * Will do all needed qgroup inherit and dirty hack like switch commit
1339  * roots inside one transaction and write all btree into disk, to make
1340  * qgroup works.
1341  */
qgroup_account_snapshot(struct btrfs_trans_handle * trans,struct btrfs_root * src,struct btrfs_root * parent,struct btrfs_qgroup_inherit * inherit,u64 dst_objectid)1342 static int qgroup_account_snapshot(struct btrfs_trans_handle *trans,
1343 				   struct btrfs_root *src,
1344 				   struct btrfs_root *parent,
1345 				   struct btrfs_qgroup_inherit *inherit,
1346 				   u64 dst_objectid)
1347 {
1348 	struct btrfs_fs_info *fs_info = src->fs_info;
1349 	int ret;
1350 
1351 	/*
1352 	 * Save some performance in the case that qgroups are not
1353 	 * enabled. If this check races with the ioctl, rescan will
1354 	 * kick in anyway.
1355 	 */
1356 	if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags))
1357 		return 0;
1358 
1359 	/*
1360 	 * Ensure dirty @src will be commited.  Or, after comming
1361 	 * commit_fs_roots() and switch_commit_roots(), any dirty but not
1362 	 * recorded root will never be updated again, causing an outdated root
1363 	 * item.
1364 	 */
1365 	record_root_in_trans(trans, src, 1);
1366 
1367 	/*
1368 	 * We are going to commit transaction, see btrfs_commit_transaction()
1369 	 * comment for reason locking tree_log_mutex
1370 	 */
1371 	mutex_lock(&fs_info->tree_log_mutex);
1372 
1373 	ret = commit_fs_roots(trans);
1374 	if (ret)
1375 		goto out;
1376 	ret = btrfs_qgroup_account_extents(trans);
1377 	if (ret < 0)
1378 		goto out;
1379 
1380 	/* Now qgroup are all updated, we can inherit it to new qgroups */
1381 	ret = btrfs_qgroup_inherit(trans, src->root_key.objectid, dst_objectid,
1382 				   inherit);
1383 	if (ret < 0)
1384 		goto out;
1385 
1386 	/*
1387 	 * Now we do a simplified commit transaction, which will:
1388 	 * 1) commit all subvolume and extent tree
1389 	 *    To ensure all subvolume and extent tree have a valid
1390 	 *    commit_root to accounting later insert_dir_item()
1391 	 * 2) write all btree blocks onto disk
1392 	 *    This is to make sure later btree modification will be cowed
1393 	 *    Or commit_root can be populated and cause wrong qgroup numbers
1394 	 * In this simplified commit, we don't really care about other trees
1395 	 * like chunk and root tree, as they won't affect qgroup.
1396 	 * And we don't write super to avoid half committed status.
1397 	 */
1398 	ret = commit_cowonly_roots(trans);
1399 	if (ret)
1400 		goto out;
1401 	switch_commit_roots(trans->transaction);
1402 	ret = btrfs_write_and_wait_transaction(trans);
1403 	if (ret)
1404 		btrfs_handle_fs_error(fs_info, ret,
1405 			"Error while writing out transaction for qgroup");
1406 
1407 out:
1408 	mutex_unlock(&fs_info->tree_log_mutex);
1409 
1410 	/*
1411 	 * Force parent root to be updated, as we recorded it before so its
1412 	 * last_trans == cur_transid.
1413 	 * Or it won't be committed again onto disk after later
1414 	 * insert_dir_item()
1415 	 */
1416 	if (!ret)
1417 		record_root_in_trans(trans, parent, 1);
1418 	return ret;
1419 }
1420 
1421 /*
1422  * new snapshots need to be created at a very specific time in the
1423  * transaction commit.  This does the actual creation.
1424  *
1425  * Note:
1426  * If the error which may affect the commitment of the current transaction
1427  * happens, we should return the error number. If the error which just affect
1428  * the creation of the pending snapshots, just return 0.
1429  */
create_pending_snapshot(struct btrfs_trans_handle * trans,struct btrfs_pending_snapshot * pending)1430 static noinline int create_pending_snapshot(struct btrfs_trans_handle *trans,
1431 				   struct btrfs_pending_snapshot *pending)
1432 {
1433 
1434 	struct btrfs_fs_info *fs_info = trans->fs_info;
1435 	struct btrfs_key key;
1436 	struct btrfs_root_item *new_root_item;
1437 	struct btrfs_root *tree_root = fs_info->tree_root;
1438 	struct btrfs_root *root = pending->root;
1439 	struct btrfs_root *parent_root;
1440 	struct btrfs_block_rsv *rsv;
1441 	struct inode *parent_inode;
1442 	struct btrfs_path *path;
1443 	struct btrfs_dir_item *dir_item;
1444 	struct dentry *dentry;
1445 	struct extent_buffer *tmp;
1446 	struct extent_buffer *old;
1447 	struct timespec64 cur_time;
1448 	int ret = 0;
1449 	u64 to_reserve = 0;
1450 	u64 index = 0;
1451 	u64 objectid;
1452 	u64 root_flags;
1453 	uuid_le new_uuid;
1454 
1455 	ASSERT(pending->path);
1456 	path = pending->path;
1457 
1458 	ASSERT(pending->root_item);
1459 	new_root_item = pending->root_item;
1460 
1461 	pending->error = btrfs_find_free_objectid(tree_root, &objectid);
1462 	if (pending->error)
1463 		goto no_free_objectid;
1464 
1465 	/*
1466 	 * Make qgroup to skip current new snapshot's qgroupid, as it is
1467 	 * accounted by later btrfs_qgroup_inherit().
1468 	 */
1469 	btrfs_set_skip_qgroup(trans, objectid);
1470 
1471 	btrfs_reloc_pre_snapshot(pending, &to_reserve);
1472 
1473 	if (to_reserve > 0) {
1474 		pending->error = btrfs_block_rsv_add(root,
1475 						     &pending->block_rsv,
1476 						     to_reserve,
1477 						     BTRFS_RESERVE_NO_FLUSH);
1478 		if (pending->error)
1479 			goto clear_skip_qgroup;
1480 	}
1481 
1482 	key.objectid = objectid;
1483 	key.offset = (u64)-1;
1484 	key.type = BTRFS_ROOT_ITEM_KEY;
1485 
1486 	rsv = trans->block_rsv;
1487 	trans->block_rsv = &pending->block_rsv;
1488 	trans->bytes_reserved = trans->block_rsv->reserved;
1489 	trace_btrfs_space_reservation(fs_info, "transaction",
1490 				      trans->transid,
1491 				      trans->bytes_reserved, 1);
1492 	dentry = pending->dentry;
1493 	parent_inode = pending->dir;
1494 	parent_root = BTRFS_I(parent_inode)->root;
1495 	record_root_in_trans(trans, parent_root, 0);
1496 
1497 	cur_time = current_time(parent_inode);
1498 
1499 	/*
1500 	 * insert the directory item
1501 	 */
1502 	ret = btrfs_set_inode_index(BTRFS_I(parent_inode), &index);
1503 	BUG_ON(ret); /* -ENOMEM */
1504 
1505 	/* check if there is a file/dir which has the same name. */
1506 	dir_item = btrfs_lookup_dir_item(NULL, parent_root, path,
1507 					 btrfs_ino(BTRFS_I(parent_inode)),
1508 					 dentry->d_name.name,
1509 					 dentry->d_name.len, 0);
1510 	if (dir_item != NULL && !IS_ERR(dir_item)) {
1511 		pending->error = -EEXIST;
1512 		goto dir_item_existed;
1513 	} else if (IS_ERR(dir_item)) {
1514 		ret = PTR_ERR(dir_item);
1515 		btrfs_abort_transaction(trans, ret);
1516 		goto fail;
1517 	}
1518 	btrfs_release_path(path);
1519 
1520 	/*
1521 	 * pull in the delayed directory update
1522 	 * and the delayed inode item
1523 	 * otherwise we corrupt the FS during
1524 	 * snapshot
1525 	 */
1526 	ret = btrfs_run_delayed_items(trans);
1527 	if (ret) {	/* Transaction aborted */
1528 		btrfs_abort_transaction(trans, ret);
1529 		goto fail;
1530 	}
1531 
1532 	record_root_in_trans(trans, root, 0);
1533 	btrfs_set_root_last_snapshot(&root->root_item, trans->transid);
1534 	memcpy(new_root_item, &root->root_item, sizeof(*new_root_item));
1535 	btrfs_check_and_init_root_item(new_root_item);
1536 
1537 	root_flags = btrfs_root_flags(new_root_item);
1538 	if (pending->readonly)
1539 		root_flags |= BTRFS_ROOT_SUBVOL_RDONLY;
1540 	else
1541 		root_flags &= ~BTRFS_ROOT_SUBVOL_RDONLY;
1542 	btrfs_set_root_flags(new_root_item, root_flags);
1543 
1544 	btrfs_set_root_generation_v2(new_root_item,
1545 			trans->transid);
1546 	uuid_le_gen(&new_uuid);
1547 	memcpy(new_root_item->uuid, new_uuid.b, BTRFS_UUID_SIZE);
1548 	memcpy(new_root_item->parent_uuid, root->root_item.uuid,
1549 			BTRFS_UUID_SIZE);
1550 	if (!(root_flags & BTRFS_ROOT_SUBVOL_RDONLY)) {
1551 		memset(new_root_item->received_uuid, 0,
1552 		       sizeof(new_root_item->received_uuid));
1553 		memset(&new_root_item->stime, 0, sizeof(new_root_item->stime));
1554 		memset(&new_root_item->rtime, 0, sizeof(new_root_item->rtime));
1555 		btrfs_set_root_stransid(new_root_item, 0);
1556 		btrfs_set_root_rtransid(new_root_item, 0);
1557 	}
1558 	btrfs_set_stack_timespec_sec(&new_root_item->otime, cur_time.tv_sec);
1559 	btrfs_set_stack_timespec_nsec(&new_root_item->otime, cur_time.tv_nsec);
1560 	btrfs_set_root_otransid(new_root_item, trans->transid);
1561 
1562 	old = btrfs_lock_root_node(root);
1563 	ret = btrfs_cow_block(trans, root, old, NULL, 0, &old);
1564 	if (ret) {
1565 		btrfs_tree_unlock(old);
1566 		free_extent_buffer(old);
1567 		btrfs_abort_transaction(trans, ret);
1568 		goto fail;
1569 	}
1570 
1571 	btrfs_set_lock_blocking(old);
1572 
1573 	ret = btrfs_copy_root(trans, root, old, &tmp, objectid);
1574 	/* clean up in any case */
1575 	btrfs_tree_unlock(old);
1576 	free_extent_buffer(old);
1577 	if (ret) {
1578 		btrfs_abort_transaction(trans, ret);
1579 		goto fail;
1580 	}
1581 	/* see comments in should_cow_block() */
1582 	set_bit(BTRFS_ROOT_FORCE_COW, &root->state);
1583 	smp_wmb();
1584 
1585 	btrfs_set_root_node(new_root_item, tmp);
1586 	/* record when the snapshot was created in key.offset */
1587 	key.offset = trans->transid;
1588 	ret = btrfs_insert_root(trans, tree_root, &key, new_root_item);
1589 	btrfs_tree_unlock(tmp);
1590 	free_extent_buffer(tmp);
1591 	if (ret) {
1592 		btrfs_abort_transaction(trans, ret);
1593 		goto fail;
1594 	}
1595 
1596 	/*
1597 	 * insert root back/forward references
1598 	 */
1599 	ret = btrfs_add_root_ref(trans, objectid,
1600 				 parent_root->root_key.objectid,
1601 				 btrfs_ino(BTRFS_I(parent_inode)), index,
1602 				 dentry->d_name.name, dentry->d_name.len);
1603 	if (ret) {
1604 		btrfs_abort_transaction(trans, ret);
1605 		goto fail;
1606 	}
1607 
1608 	key.offset = (u64)-1;
1609 	pending->snap = btrfs_read_fs_root_no_name(fs_info, &key);
1610 	if (IS_ERR(pending->snap)) {
1611 		ret = PTR_ERR(pending->snap);
1612 		btrfs_abort_transaction(trans, ret);
1613 		goto fail;
1614 	}
1615 
1616 	ret = btrfs_reloc_post_snapshot(trans, pending);
1617 	if (ret) {
1618 		btrfs_abort_transaction(trans, ret);
1619 		goto fail;
1620 	}
1621 
1622 	ret = btrfs_run_delayed_refs(trans, (unsigned long)-1);
1623 	if (ret) {
1624 		btrfs_abort_transaction(trans, ret);
1625 		goto fail;
1626 	}
1627 
1628 	/*
1629 	 * Do special qgroup accounting for snapshot, as we do some qgroup
1630 	 * snapshot hack to do fast snapshot.
1631 	 * To co-operate with that hack, we do hack again.
1632 	 * Or snapshot will be greatly slowed down by a subtree qgroup rescan
1633 	 */
1634 	ret = qgroup_account_snapshot(trans, root, parent_root,
1635 				      pending->inherit, objectid);
1636 	if (ret < 0)
1637 		goto fail;
1638 
1639 	ret = btrfs_insert_dir_item(trans, parent_root,
1640 				    dentry->d_name.name, dentry->d_name.len,
1641 				    BTRFS_I(parent_inode), &key,
1642 				    BTRFS_FT_DIR, index);
1643 	/* We have check then name at the beginning, so it is impossible. */
1644 	BUG_ON(ret == -EEXIST || ret == -EOVERFLOW);
1645 	if (ret) {
1646 		btrfs_abort_transaction(trans, ret);
1647 		goto fail;
1648 	}
1649 
1650 	btrfs_i_size_write(BTRFS_I(parent_inode), parent_inode->i_size +
1651 					 dentry->d_name.len * 2);
1652 	parent_inode->i_mtime = parent_inode->i_ctime =
1653 		current_time(parent_inode);
1654 	ret = btrfs_update_inode_fallback(trans, parent_root, parent_inode);
1655 	if (ret) {
1656 		btrfs_abort_transaction(trans, ret);
1657 		goto fail;
1658 	}
1659 	ret = btrfs_uuid_tree_add(trans, new_uuid.b, BTRFS_UUID_KEY_SUBVOL,
1660 				  objectid);
1661 	if (ret) {
1662 		btrfs_abort_transaction(trans, ret);
1663 		goto fail;
1664 	}
1665 	if (!btrfs_is_empty_uuid(new_root_item->received_uuid)) {
1666 		ret = btrfs_uuid_tree_add(trans, new_root_item->received_uuid,
1667 					  BTRFS_UUID_KEY_RECEIVED_SUBVOL,
1668 					  objectid);
1669 		if (ret && ret != -EEXIST) {
1670 			btrfs_abort_transaction(trans, ret);
1671 			goto fail;
1672 		}
1673 	}
1674 
1675 	ret = btrfs_run_delayed_refs(trans, (unsigned long)-1);
1676 	if (ret) {
1677 		btrfs_abort_transaction(trans, ret);
1678 		goto fail;
1679 	}
1680 
1681 fail:
1682 	pending->error = ret;
1683 dir_item_existed:
1684 	trans->block_rsv = rsv;
1685 	trans->bytes_reserved = 0;
1686 clear_skip_qgroup:
1687 	btrfs_clear_skip_qgroup(trans);
1688 no_free_objectid:
1689 	kfree(new_root_item);
1690 	pending->root_item = NULL;
1691 	btrfs_free_path(path);
1692 	pending->path = NULL;
1693 
1694 	return ret;
1695 }
1696 
1697 /*
1698  * create all the snapshots we've scheduled for creation
1699  */
create_pending_snapshots(struct btrfs_trans_handle * trans)1700 static noinline int create_pending_snapshots(struct btrfs_trans_handle *trans)
1701 {
1702 	struct btrfs_pending_snapshot *pending, *next;
1703 	struct list_head *head = &trans->transaction->pending_snapshots;
1704 	int ret = 0;
1705 
1706 	list_for_each_entry_safe(pending, next, head, list) {
1707 		list_del(&pending->list);
1708 		ret = create_pending_snapshot(trans, pending);
1709 		if (ret)
1710 			break;
1711 	}
1712 	return ret;
1713 }
1714 
update_super_roots(struct btrfs_fs_info * fs_info)1715 static void update_super_roots(struct btrfs_fs_info *fs_info)
1716 {
1717 	struct btrfs_root_item *root_item;
1718 	struct btrfs_super_block *super;
1719 
1720 	super = fs_info->super_copy;
1721 
1722 	root_item = &fs_info->chunk_root->root_item;
1723 	super->chunk_root = root_item->bytenr;
1724 	super->chunk_root_generation = root_item->generation;
1725 	super->chunk_root_level = root_item->level;
1726 
1727 	root_item = &fs_info->tree_root->root_item;
1728 	super->root = root_item->bytenr;
1729 	super->generation = root_item->generation;
1730 	super->root_level = root_item->level;
1731 	if (btrfs_test_opt(fs_info, SPACE_CACHE))
1732 		super->cache_generation = root_item->generation;
1733 	if (test_bit(BTRFS_FS_UPDATE_UUID_TREE_GEN, &fs_info->flags))
1734 		super->uuid_tree_generation = root_item->generation;
1735 }
1736 
btrfs_transaction_in_commit(struct btrfs_fs_info * info)1737 int btrfs_transaction_in_commit(struct btrfs_fs_info *info)
1738 {
1739 	struct btrfs_transaction *trans;
1740 	int ret = 0;
1741 
1742 	spin_lock(&info->trans_lock);
1743 	trans = info->running_transaction;
1744 	if (trans)
1745 		ret = (trans->state >= TRANS_STATE_COMMIT_START);
1746 	spin_unlock(&info->trans_lock);
1747 	return ret;
1748 }
1749 
btrfs_transaction_blocked(struct btrfs_fs_info * info)1750 int btrfs_transaction_blocked(struct btrfs_fs_info *info)
1751 {
1752 	struct btrfs_transaction *trans;
1753 	int ret = 0;
1754 
1755 	spin_lock(&info->trans_lock);
1756 	trans = info->running_transaction;
1757 	if (trans)
1758 		ret = is_transaction_blocked(trans);
1759 	spin_unlock(&info->trans_lock);
1760 	return ret;
1761 }
1762 
1763 /*
1764  * wait for the current transaction commit to start and block subsequent
1765  * transaction joins
1766  */
wait_current_trans_commit_start(struct btrfs_fs_info * fs_info,struct btrfs_transaction * trans)1767 static void wait_current_trans_commit_start(struct btrfs_fs_info *fs_info,
1768 					    struct btrfs_transaction *trans)
1769 {
1770 	wait_event(fs_info->transaction_blocked_wait,
1771 		   trans->state >= TRANS_STATE_COMMIT_START || trans->aborted);
1772 }
1773 
1774 /*
1775  * wait for the current transaction to start and then become unblocked.
1776  * caller holds ref.
1777  */
wait_current_trans_commit_start_and_unblock(struct btrfs_fs_info * fs_info,struct btrfs_transaction * trans)1778 static void wait_current_trans_commit_start_and_unblock(
1779 					struct btrfs_fs_info *fs_info,
1780 					struct btrfs_transaction *trans)
1781 {
1782 	wait_event(fs_info->transaction_wait,
1783 		   trans->state >= TRANS_STATE_UNBLOCKED || trans->aborted);
1784 }
1785 
1786 /*
1787  * commit transactions asynchronously. once btrfs_commit_transaction_async
1788  * returns, any subsequent transaction will not be allowed to join.
1789  */
1790 struct btrfs_async_commit {
1791 	struct btrfs_trans_handle *newtrans;
1792 	struct work_struct work;
1793 };
1794 
do_async_commit(struct work_struct * work)1795 static void do_async_commit(struct work_struct *work)
1796 {
1797 	struct btrfs_async_commit *ac =
1798 		container_of(work, struct btrfs_async_commit, work);
1799 
1800 	/*
1801 	 * We've got freeze protection passed with the transaction.
1802 	 * Tell lockdep about it.
1803 	 */
1804 	if (ac->newtrans->type & __TRANS_FREEZABLE)
1805 		__sb_writers_acquired(ac->newtrans->fs_info->sb, SB_FREEZE_FS);
1806 
1807 	current->journal_info = ac->newtrans;
1808 
1809 	btrfs_commit_transaction(ac->newtrans);
1810 	kfree(ac);
1811 }
1812 
btrfs_commit_transaction_async(struct btrfs_trans_handle * trans,int wait_for_unblock)1813 int btrfs_commit_transaction_async(struct btrfs_trans_handle *trans,
1814 				   int wait_for_unblock)
1815 {
1816 	struct btrfs_fs_info *fs_info = trans->fs_info;
1817 	struct btrfs_async_commit *ac;
1818 	struct btrfs_transaction *cur_trans;
1819 
1820 	ac = kmalloc(sizeof(*ac), GFP_NOFS);
1821 	if (!ac)
1822 		return -ENOMEM;
1823 
1824 	INIT_WORK(&ac->work, do_async_commit);
1825 	ac->newtrans = btrfs_join_transaction(trans->root);
1826 	if (IS_ERR(ac->newtrans)) {
1827 		int err = PTR_ERR(ac->newtrans);
1828 		kfree(ac);
1829 		return err;
1830 	}
1831 
1832 	/* take transaction reference */
1833 	cur_trans = trans->transaction;
1834 	refcount_inc(&cur_trans->use_count);
1835 
1836 	btrfs_end_transaction(trans);
1837 
1838 	/*
1839 	 * Tell lockdep we've released the freeze rwsem, since the
1840 	 * async commit thread will be the one to unlock it.
1841 	 */
1842 	if (ac->newtrans->type & __TRANS_FREEZABLE)
1843 		__sb_writers_release(fs_info->sb, SB_FREEZE_FS);
1844 
1845 	schedule_work(&ac->work);
1846 
1847 	/* wait for transaction to start and unblock */
1848 	if (wait_for_unblock)
1849 		wait_current_trans_commit_start_and_unblock(fs_info, cur_trans);
1850 	else
1851 		wait_current_trans_commit_start(fs_info, cur_trans);
1852 
1853 	if (current->journal_info == trans)
1854 		current->journal_info = NULL;
1855 
1856 	btrfs_put_transaction(cur_trans);
1857 	return 0;
1858 }
1859 
1860 
cleanup_transaction(struct btrfs_trans_handle * trans,int err)1861 static void cleanup_transaction(struct btrfs_trans_handle *trans, int err)
1862 {
1863 	struct btrfs_fs_info *fs_info = trans->fs_info;
1864 	struct btrfs_transaction *cur_trans = trans->transaction;
1865 	DEFINE_WAIT(wait);
1866 
1867 	WARN_ON(refcount_read(&trans->use_count) > 1);
1868 
1869 	btrfs_abort_transaction(trans, err);
1870 
1871 	spin_lock(&fs_info->trans_lock);
1872 
1873 	/*
1874 	 * If the transaction is removed from the list, it means this
1875 	 * transaction has been committed successfully, so it is impossible
1876 	 * to call the cleanup function.
1877 	 */
1878 	BUG_ON(list_empty(&cur_trans->list));
1879 
1880 	list_del_init(&cur_trans->list);
1881 	if (cur_trans == fs_info->running_transaction) {
1882 		cur_trans->state = TRANS_STATE_COMMIT_DOING;
1883 		spin_unlock(&fs_info->trans_lock);
1884 		wait_event(cur_trans->writer_wait,
1885 			   atomic_read(&cur_trans->num_writers) == 1);
1886 
1887 		spin_lock(&fs_info->trans_lock);
1888 	}
1889 	spin_unlock(&fs_info->trans_lock);
1890 
1891 	btrfs_cleanup_one_transaction(trans->transaction, fs_info);
1892 
1893 	spin_lock(&fs_info->trans_lock);
1894 	if (cur_trans == fs_info->running_transaction)
1895 		fs_info->running_transaction = NULL;
1896 	spin_unlock(&fs_info->trans_lock);
1897 
1898 	if (trans->type & __TRANS_FREEZABLE)
1899 		sb_end_intwrite(fs_info->sb);
1900 	btrfs_put_transaction(cur_trans);
1901 	btrfs_put_transaction(cur_trans);
1902 
1903 	trace_btrfs_transaction_commit(trans->root);
1904 
1905 	if (current->journal_info == trans)
1906 		current->journal_info = NULL;
1907 	btrfs_scrub_cancel(fs_info);
1908 
1909 	kmem_cache_free(btrfs_trans_handle_cachep, trans);
1910 }
1911 
btrfs_start_delalloc_flush(struct btrfs_fs_info * fs_info)1912 static inline int btrfs_start_delalloc_flush(struct btrfs_fs_info *fs_info)
1913 {
1914 	/*
1915 	 * We use writeback_inodes_sb here because if we used
1916 	 * btrfs_start_delalloc_roots we would deadlock with fs freeze.
1917 	 * Currently are holding the fs freeze lock, if we do an async flush
1918 	 * we'll do btrfs_join_transaction() and deadlock because we need to
1919 	 * wait for the fs freeze lock.  Using the direct flushing we benefit
1920 	 * from already being in a transaction and our join_transaction doesn't
1921 	 * have to re-take the fs freeze lock.
1922 	 */
1923 	if (btrfs_test_opt(fs_info, FLUSHONCOMMIT))
1924 		writeback_inodes_sb(fs_info->sb, WB_REASON_SYNC);
1925 	return 0;
1926 }
1927 
btrfs_wait_delalloc_flush(struct btrfs_fs_info * fs_info)1928 static inline void btrfs_wait_delalloc_flush(struct btrfs_fs_info *fs_info)
1929 {
1930 	if (btrfs_test_opt(fs_info, FLUSHONCOMMIT))
1931 		btrfs_wait_ordered_roots(fs_info, U64_MAX, 0, (u64)-1);
1932 }
1933 
1934 static inline void
btrfs_wait_pending_ordered(struct btrfs_transaction * cur_trans)1935 btrfs_wait_pending_ordered(struct btrfs_transaction *cur_trans)
1936 {
1937 	wait_event(cur_trans->pending_wait,
1938 		   atomic_read(&cur_trans->pending_ordered) == 0);
1939 }
1940 
btrfs_commit_transaction(struct btrfs_trans_handle * trans)1941 int btrfs_commit_transaction(struct btrfs_trans_handle *trans)
1942 {
1943 	struct btrfs_fs_info *fs_info = trans->fs_info;
1944 	struct btrfs_transaction *cur_trans = trans->transaction;
1945 	struct btrfs_transaction *prev_trans = NULL;
1946 	int ret;
1947 
1948 	/*
1949 	 * Some places just start a transaction to commit it.  We need to make
1950 	 * sure that if this commit fails that the abort code actually marks the
1951 	 * transaction as failed, so set trans->dirty to make the abort code do
1952 	 * the right thing.
1953 	 */
1954 	trans->dirty = true;
1955 
1956 	/* Stop the commit early if ->aborted is set */
1957 	if (unlikely(READ_ONCE(cur_trans->aborted))) {
1958 		ret = cur_trans->aborted;
1959 		btrfs_end_transaction(trans);
1960 		return ret;
1961 	}
1962 
1963 	btrfs_trans_release_metadata(trans);
1964 	trans->block_rsv = NULL;
1965 
1966 	/* make a pass through all the delayed refs we have so far
1967 	 * any runnings procs may add more while we are here
1968 	 */
1969 	ret = btrfs_run_delayed_refs(trans, 0);
1970 	if (ret) {
1971 		btrfs_end_transaction(trans);
1972 		return ret;
1973 	}
1974 
1975 	cur_trans = trans->transaction;
1976 
1977 	/*
1978 	 * set the flushing flag so procs in this transaction have to
1979 	 * start sending their work down.
1980 	 */
1981 	cur_trans->delayed_refs.flushing = 1;
1982 	smp_wmb();
1983 
1984 	if (!list_empty(&trans->new_bgs))
1985 		btrfs_create_pending_block_groups(trans);
1986 
1987 	ret = btrfs_run_delayed_refs(trans, 0);
1988 	if (ret) {
1989 		btrfs_end_transaction(trans);
1990 		return ret;
1991 	}
1992 
1993 	if (!test_bit(BTRFS_TRANS_DIRTY_BG_RUN, &cur_trans->flags)) {
1994 		int run_it = 0;
1995 
1996 		/* this mutex is also taken before trying to set
1997 		 * block groups readonly.  We need to make sure
1998 		 * that nobody has set a block group readonly
1999 		 * after a extents from that block group have been
2000 		 * allocated for cache files.  btrfs_set_block_group_ro
2001 		 * will wait for the transaction to commit if it
2002 		 * finds BTRFS_TRANS_DIRTY_BG_RUN set.
2003 		 *
2004 		 * The BTRFS_TRANS_DIRTY_BG_RUN flag is also used to make sure
2005 		 * only one process starts all the block group IO.  It wouldn't
2006 		 * hurt to have more than one go through, but there's no
2007 		 * real advantage to it either.
2008 		 */
2009 		mutex_lock(&fs_info->ro_block_group_mutex);
2010 		if (!test_and_set_bit(BTRFS_TRANS_DIRTY_BG_RUN,
2011 				      &cur_trans->flags))
2012 			run_it = 1;
2013 		mutex_unlock(&fs_info->ro_block_group_mutex);
2014 
2015 		if (run_it) {
2016 			ret = btrfs_start_dirty_block_groups(trans);
2017 			if (ret) {
2018 				btrfs_end_transaction(trans);
2019 				return ret;
2020 			}
2021 		}
2022 	}
2023 
2024 	spin_lock(&fs_info->trans_lock);
2025 	if (cur_trans->state >= TRANS_STATE_COMMIT_START) {
2026 		spin_unlock(&fs_info->trans_lock);
2027 		refcount_inc(&cur_trans->use_count);
2028 		ret = btrfs_end_transaction(trans);
2029 
2030 		wait_for_commit(cur_trans);
2031 
2032 		if (unlikely(cur_trans->aborted))
2033 			ret = cur_trans->aborted;
2034 
2035 		btrfs_put_transaction(cur_trans);
2036 
2037 		return ret;
2038 	}
2039 
2040 	cur_trans->state = TRANS_STATE_COMMIT_START;
2041 	wake_up(&fs_info->transaction_blocked_wait);
2042 
2043 	if (cur_trans->list.prev != &fs_info->trans_list) {
2044 		prev_trans = list_entry(cur_trans->list.prev,
2045 					struct btrfs_transaction, list);
2046 		if (prev_trans->state != TRANS_STATE_COMPLETED) {
2047 			refcount_inc(&prev_trans->use_count);
2048 			spin_unlock(&fs_info->trans_lock);
2049 
2050 			wait_for_commit(prev_trans);
2051 			ret = prev_trans->aborted;
2052 
2053 			btrfs_put_transaction(prev_trans);
2054 			if (ret)
2055 				goto cleanup_transaction;
2056 		} else {
2057 			spin_unlock(&fs_info->trans_lock);
2058 		}
2059 	} else {
2060 		spin_unlock(&fs_info->trans_lock);
2061 		/*
2062 		 * The previous transaction was aborted and was already removed
2063 		 * from the list of transactions at fs_info->trans_list. So we
2064 		 * abort to prevent writing a new superblock that reflects a
2065 		 * corrupt state (pointing to trees with unwritten nodes/leafs).
2066 		 */
2067 		if (test_bit(BTRFS_FS_STATE_TRANS_ABORTED, &fs_info->fs_state)) {
2068 			ret = -EROFS;
2069 			goto cleanup_transaction;
2070 		}
2071 	}
2072 
2073 	extwriter_counter_dec(cur_trans, trans->type);
2074 
2075 	ret = btrfs_start_delalloc_flush(fs_info);
2076 	if (ret)
2077 		goto cleanup_transaction;
2078 
2079 	ret = btrfs_run_delayed_items(trans);
2080 	if (ret)
2081 		goto cleanup_transaction;
2082 
2083 	wait_event(cur_trans->writer_wait,
2084 		   extwriter_counter_read(cur_trans) == 0);
2085 
2086 	/* some pending stuffs might be added after the previous flush. */
2087 	ret = btrfs_run_delayed_items(trans);
2088 	if (ret)
2089 		goto cleanup_transaction;
2090 
2091 	btrfs_wait_delalloc_flush(fs_info);
2092 
2093 	btrfs_wait_pending_ordered(cur_trans);
2094 
2095 	btrfs_scrub_pause(fs_info);
2096 	/*
2097 	 * Ok now we need to make sure to block out any other joins while we
2098 	 * commit the transaction.  We could have started a join before setting
2099 	 * COMMIT_DOING so make sure to wait for num_writers to == 1 again.
2100 	 */
2101 	spin_lock(&fs_info->trans_lock);
2102 	cur_trans->state = TRANS_STATE_COMMIT_DOING;
2103 	spin_unlock(&fs_info->trans_lock);
2104 	wait_event(cur_trans->writer_wait,
2105 		   atomic_read(&cur_trans->num_writers) == 1);
2106 
2107 	/* ->aborted might be set after the previous check, so check it */
2108 	if (unlikely(READ_ONCE(cur_trans->aborted))) {
2109 		ret = cur_trans->aborted;
2110 		goto scrub_continue;
2111 	}
2112 	/*
2113 	 * the reloc mutex makes sure that we stop
2114 	 * the balancing code from coming in and moving
2115 	 * extents around in the middle of the commit
2116 	 */
2117 	mutex_lock(&fs_info->reloc_mutex);
2118 
2119 	/*
2120 	 * We needn't worry about the delayed items because we will
2121 	 * deal with them in create_pending_snapshot(), which is the
2122 	 * core function of the snapshot creation.
2123 	 */
2124 	ret = create_pending_snapshots(trans);
2125 	if (ret) {
2126 		mutex_unlock(&fs_info->reloc_mutex);
2127 		goto scrub_continue;
2128 	}
2129 
2130 	/*
2131 	 * We insert the dir indexes of the snapshots and update the inode
2132 	 * of the snapshots' parents after the snapshot creation, so there
2133 	 * are some delayed items which are not dealt with. Now deal with
2134 	 * them.
2135 	 *
2136 	 * We needn't worry that this operation will corrupt the snapshots,
2137 	 * because all the tree which are snapshoted will be forced to COW
2138 	 * the nodes and leaves.
2139 	 */
2140 	ret = btrfs_run_delayed_items(trans);
2141 	if (ret) {
2142 		mutex_unlock(&fs_info->reloc_mutex);
2143 		goto scrub_continue;
2144 	}
2145 
2146 	ret = btrfs_run_delayed_refs(trans, (unsigned long)-1);
2147 	if (ret) {
2148 		mutex_unlock(&fs_info->reloc_mutex);
2149 		goto scrub_continue;
2150 	}
2151 
2152 	/*
2153 	 * make sure none of the code above managed to slip in a
2154 	 * delayed item
2155 	 */
2156 	btrfs_assert_delayed_root_empty(fs_info);
2157 
2158 	WARN_ON(cur_trans != trans->transaction);
2159 
2160 	/* btrfs_commit_tree_roots is responsible for getting the
2161 	 * various roots consistent with each other.  Every pointer
2162 	 * in the tree of tree roots has to point to the most up to date
2163 	 * root for every subvolume and other tree.  So, we have to keep
2164 	 * the tree logging code from jumping in and changing any
2165 	 * of the trees.
2166 	 *
2167 	 * At this point in the commit, there can't be any tree-log
2168 	 * writers, but a little lower down we drop the trans mutex
2169 	 * and let new people in.  By holding the tree_log_mutex
2170 	 * from now until after the super is written, we avoid races
2171 	 * with the tree-log code.
2172 	 */
2173 	mutex_lock(&fs_info->tree_log_mutex);
2174 
2175 	ret = commit_fs_roots(trans);
2176 	if (ret) {
2177 		mutex_unlock(&fs_info->tree_log_mutex);
2178 		mutex_unlock(&fs_info->reloc_mutex);
2179 		goto scrub_continue;
2180 	}
2181 
2182 	/*
2183 	 * Since the transaction is done, we can apply the pending changes
2184 	 * before the next transaction.
2185 	 */
2186 	btrfs_apply_pending_changes(fs_info);
2187 
2188 	/* commit_fs_roots gets rid of all the tree log roots, it is now
2189 	 * safe to free the root of tree log roots
2190 	 */
2191 	btrfs_free_log_root_tree(trans, fs_info);
2192 
2193 	/*
2194 	 * commit_fs_roots() can call btrfs_save_ino_cache(), which generates
2195 	 * new delayed refs. Must handle them or qgroup can be wrong.
2196 	 */
2197 	ret = btrfs_run_delayed_refs(trans, (unsigned long)-1);
2198 	if (ret) {
2199 		mutex_unlock(&fs_info->tree_log_mutex);
2200 		mutex_unlock(&fs_info->reloc_mutex);
2201 		goto scrub_continue;
2202 	}
2203 
2204 	/*
2205 	 * Since fs roots are all committed, we can get a quite accurate
2206 	 * new_roots. So let's do quota accounting.
2207 	 */
2208 	ret = btrfs_qgroup_account_extents(trans);
2209 	if (ret < 0) {
2210 		mutex_unlock(&fs_info->tree_log_mutex);
2211 		mutex_unlock(&fs_info->reloc_mutex);
2212 		goto scrub_continue;
2213 	}
2214 
2215 	ret = commit_cowonly_roots(trans);
2216 	if (ret) {
2217 		mutex_unlock(&fs_info->tree_log_mutex);
2218 		mutex_unlock(&fs_info->reloc_mutex);
2219 		goto scrub_continue;
2220 	}
2221 
2222 	/*
2223 	 * The tasks which save the space cache and inode cache may also
2224 	 * update ->aborted, check it.
2225 	 */
2226 	if (unlikely(READ_ONCE(cur_trans->aborted))) {
2227 		ret = cur_trans->aborted;
2228 		mutex_unlock(&fs_info->tree_log_mutex);
2229 		mutex_unlock(&fs_info->reloc_mutex);
2230 		goto scrub_continue;
2231 	}
2232 
2233 	btrfs_prepare_extent_commit(fs_info);
2234 
2235 	cur_trans = fs_info->running_transaction;
2236 
2237 	btrfs_set_root_node(&fs_info->tree_root->root_item,
2238 			    fs_info->tree_root->node);
2239 	list_add_tail(&fs_info->tree_root->dirty_list,
2240 		      &cur_trans->switch_commits);
2241 
2242 	btrfs_set_root_node(&fs_info->chunk_root->root_item,
2243 			    fs_info->chunk_root->node);
2244 	list_add_tail(&fs_info->chunk_root->dirty_list,
2245 		      &cur_trans->switch_commits);
2246 
2247 	switch_commit_roots(cur_trans);
2248 
2249 	ASSERT(list_empty(&cur_trans->dirty_bgs));
2250 	ASSERT(list_empty(&cur_trans->io_bgs));
2251 	update_super_roots(fs_info);
2252 
2253 	btrfs_set_super_log_root(fs_info->super_copy, 0);
2254 	btrfs_set_super_log_root_level(fs_info->super_copy, 0);
2255 	memcpy(fs_info->super_for_commit, fs_info->super_copy,
2256 	       sizeof(*fs_info->super_copy));
2257 
2258 	btrfs_update_commit_device_size(fs_info);
2259 	btrfs_update_commit_device_bytes_used(cur_trans);
2260 
2261 	clear_bit(BTRFS_FS_LOG1_ERR, &fs_info->flags);
2262 	clear_bit(BTRFS_FS_LOG2_ERR, &fs_info->flags);
2263 
2264 	btrfs_trans_release_chunk_metadata(trans);
2265 
2266 	spin_lock(&fs_info->trans_lock);
2267 	cur_trans->state = TRANS_STATE_UNBLOCKED;
2268 	fs_info->running_transaction = NULL;
2269 	spin_unlock(&fs_info->trans_lock);
2270 	mutex_unlock(&fs_info->reloc_mutex);
2271 
2272 	wake_up(&fs_info->transaction_wait);
2273 
2274 	ret = btrfs_write_and_wait_transaction(trans);
2275 	if (ret) {
2276 		btrfs_handle_fs_error(fs_info, ret,
2277 				      "Error while writing out transaction");
2278 		mutex_unlock(&fs_info->tree_log_mutex);
2279 		goto scrub_continue;
2280 	}
2281 
2282 	ret = write_all_supers(fs_info, 0);
2283 	/*
2284 	 * the super is written, we can safely allow the tree-loggers
2285 	 * to go about their business
2286 	 */
2287 	mutex_unlock(&fs_info->tree_log_mutex);
2288 	if (ret)
2289 		goto scrub_continue;
2290 
2291 	btrfs_finish_extent_commit(trans);
2292 
2293 	if (test_bit(BTRFS_TRANS_HAVE_FREE_BGS, &cur_trans->flags))
2294 		btrfs_clear_space_info_full(fs_info);
2295 
2296 	fs_info->last_trans_committed = cur_trans->transid;
2297 	/*
2298 	 * We needn't acquire the lock here because there is no other task
2299 	 * which can change it.
2300 	 */
2301 	cur_trans->state = TRANS_STATE_COMPLETED;
2302 	wake_up(&cur_trans->commit_wait);
2303 	clear_bit(BTRFS_FS_NEED_ASYNC_COMMIT, &fs_info->flags);
2304 
2305 	spin_lock(&fs_info->trans_lock);
2306 	list_del_init(&cur_trans->list);
2307 	spin_unlock(&fs_info->trans_lock);
2308 
2309 	btrfs_put_transaction(cur_trans);
2310 	btrfs_put_transaction(cur_trans);
2311 
2312 	if (trans->type & __TRANS_FREEZABLE)
2313 		sb_end_intwrite(fs_info->sb);
2314 
2315 	trace_btrfs_transaction_commit(trans->root);
2316 
2317 	btrfs_scrub_continue(fs_info);
2318 
2319 	if (current->journal_info == trans)
2320 		current->journal_info = NULL;
2321 
2322 	kmem_cache_free(btrfs_trans_handle_cachep, trans);
2323 
2324 	return ret;
2325 
2326 scrub_continue:
2327 	btrfs_scrub_continue(fs_info);
2328 cleanup_transaction:
2329 	btrfs_trans_release_metadata(trans);
2330 	btrfs_trans_release_chunk_metadata(trans);
2331 	trans->block_rsv = NULL;
2332 	btrfs_warn(fs_info, "Skipping commit of aborted transaction.");
2333 	if (current->journal_info == trans)
2334 		current->journal_info = NULL;
2335 	cleanup_transaction(trans, ret);
2336 
2337 	return ret;
2338 }
2339 
2340 /*
2341  * return < 0 if error
2342  * 0 if there are no more dead_roots at the time of call
2343  * 1 there are more to be processed, call me again
2344  *
2345  * The return value indicates there are certainly more snapshots to delete, but
2346  * if there comes a new one during processing, it may return 0. We don't mind,
2347  * because btrfs_commit_super will poke cleaner thread and it will process it a
2348  * few seconds later.
2349  */
btrfs_clean_one_deleted_snapshot(struct btrfs_root * root)2350 int btrfs_clean_one_deleted_snapshot(struct btrfs_root *root)
2351 {
2352 	int ret;
2353 	struct btrfs_fs_info *fs_info = root->fs_info;
2354 
2355 	spin_lock(&fs_info->trans_lock);
2356 	if (list_empty(&fs_info->dead_roots)) {
2357 		spin_unlock(&fs_info->trans_lock);
2358 		return 0;
2359 	}
2360 	root = list_first_entry(&fs_info->dead_roots,
2361 			struct btrfs_root, root_list);
2362 	list_del_init(&root->root_list);
2363 	spin_unlock(&fs_info->trans_lock);
2364 
2365 	btrfs_debug(fs_info, "cleaner removing %llu", root->objectid);
2366 
2367 	btrfs_kill_all_delayed_nodes(root);
2368 
2369 	if (btrfs_header_backref_rev(root->node) <
2370 			BTRFS_MIXED_BACKREF_REV)
2371 		ret = btrfs_drop_snapshot(root, NULL, 0, 0);
2372 	else
2373 		ret = btrfs_drop_snapshot(root, NULL, 1, 0);
2374 
2375 	return (ret < 0) ? 0 : 1;
2376 }
2377 
btrfs_apply_pending_changes(struct btrfs_fs_info * fs_info)2378 void btrfs_apply_pending_changes(struct btrfs_fs_info *fs_info)
2379 {
2380 	unsigned long prev;
2381 	unsigned long bit;
2382 
2383 	prev = xchg(&fs_info->pending_changes, 0);
2384 	if (!prev)
2385 		return;
2386 
2387 	bit = 1 << BTRFS_PENDING_SET_INODE_MAP_CACHE;
2388 	if (prev & bit)
2389 		btrfs_set_opt(fs_info->mount_opt, INODE_MAP_CACHE);
2390 	prev &= ~bit;
2391 
2392 	bit = 1 << BTRFS_PENDING_CLEAR_INODE_MAP_CACHE;
2393 	if (prev & bit)
2394 		btrfs_clear_opt(fs_info->mount_opt, INODE_MAP_CACHE);
2395 	prev &= ~bit;
2396 
2397 	bit = 1 << BTRFS_PENDING_COMMIT;
2398 	if (prev & bit)
2399 		btrfs_debug(fs_info, "pending commit done");
2400 	prev &= ~bit;
2401 
2402 	if (prev)
2403 		btrfs_warn(fs_info,
2404 			"unknown pending changes left 0x%lx, ignoring", prev);
2405 }
2406