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