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