uuid: Provide a GUID generator for raw buffer
[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>
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,
224 trans->chunk_bytes_reserved);
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,
843fcf35
MX
678 num_bytes);
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,
901 trans->bytes_reserved);
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;
8ea05e3a 1482 uuid_le new_uuid;
3063d29f 1483
8546b570
DS
1484 ASSERT(pending->path);
1485 path = pending->path;
42874b3d 1486
b0c0ea63
DS
1487 ASSERT(pending->root_item);
1488 new_root_item = pending->root_item;
a22285a6 1489
aec8030a
MX
1490 pending->error = btrfs_find_free_objectid(tree_root, &objectid);
1491 if (pending->error)
6fa9700e 1492 goto no_free_objectid;
3063d29f 1493
d6726335
QW
1494 /*
1495 * Make qgroup to skip current new snapshot's qgroupid, as it is
1496 * accounted by later btrfs_qgroup_inherit().
1497 */
1498 btrfs_set_skip_qgroup(trans, objectid);
1499
147d256e 1500 btrfs_reloc_pre_snapshot(pending, &to_reserve);
d68fc57b
YZ
1501
1502 if (to_reserve > 0) {
aec8030a
MX
1503 pending->error = btrfs_block_rsv_add(root,
1504 &pending->block_rsv,
1505 to_reserve,
1506 BTRFS_RESERVE_NO_FLUSH);
1507 if (pending->error)
d6726335 1508 goto clear_skip_qgroup;
d68fc57b
YZ
1509 }
1510
3063d29f 1511 key.objectid = objectid;
a22285a6
YZ
1512 key.offset = (u64)-1;
1513 key.type = BTRFS_ROOT_ITEM_KEY;
3063d29f 1514
6fa9700e 1515 rsv = trans->block_rsv;
a22285a6 1516 trans->block_rsv = &pending->block_rsv;
2382c5cc 1517 trans->bytes_reserved = trans->block_rsv->reserved;
0b246afa 1518 trace_btrfs_space_reservation(fs_info, "transaction",
88d3a5aa
JB
1519 trans->transid,
1520 trans->bytes_reserved, 1);
a22285a6 1521 dentry = pending->dentry;
e9662f70 1522 parent_inode = pending->dir;
a22285a6 1523 parent_root = BTRFS_I(parent_inode)->root;
6426c7ad 1524 record_root_in_trans(trans, parent_root, 0);
a22285a6 1525
c2050a45 1526 cur_time = current_time(parent_inode);
04b285f3 1527
3063d29f
CM
1528 /*
1529 * insert the directory item
1530 */
877574e2 1531 ret = btrfs_set_inode_index(BTRFS_I(parent_inode), &index);
49b25e05 1532 BUG_ON(ret); /* -ENOMEM */
42874b3d
MX
1533
1534 /* check if there is a file/dir which has the same name. */
1535 dir_item = btrfs_lookup_dir_item(NULL, parent_root, path,
4a0cc7ca 1536 btrfs_ino(BTRFS_I(parent_inode)),
42874b3d
MX
1537 dentry->d_name.name,
1538 dentry->d_name.len, 0);
1539 if (dir_item != NULL && !IS_ERR(dir_item)) {
fe66a05a 1540 pending->error = -EEXIST;
aec8030a 1541 goto dir_item_existed;
42874b3d
MX
1542 } else if (IS_ERR(dir_item)) {
1543 ret = PTR_ERR(dir_item);
66642832 1544 btrfs_abort_transaction(trans, ret);
8732d44f 1545 goto fail;
79787eaa 1546 }
42874b3d 1547 btrfs_release_path(path);
52c26179 1548
e999376f
CM
1549 /*
1550 * pull in the delayed directory update
1551 * and the delayed inode item
1552 * otherwise we corrupt the FS during
1553 * snapshot
1554 */
e5c304e6 1555 ret = btrfs_run_delayed_items(trans);
8732d44f 1556 if (ret) { /* Transaction aborted */
66642832 1557 btrfs_abort_transaction(trans, ret);
8732d44f
MX
1558 goto fail;
1559 }
e999376f 1560
6426c7ad 1561 record_root_in_trans(trans, root, 0);
6bdb72de
SW
1562 btrfs_set_root_last_snapshot(&root->root_item, trans->transid);
1563 memcpy(new_root_item, &root->root_item, sizeof(*new_root_item));
08fe4db1 1564 btrfs_check_and_init_root_item(new_root_item);
6bdb72de 1565
b83cc969
LZ
1566 root_flags = btrfs_root_flags(new_root_item);
1567 if (pending->readonly)
1568 root_flags |= BTRFS_ROOT_SUBVOL_RDONLY;
1569 else
1570 root_flags &= ~BTRFS_ROOT_SUBVOL_RDONLY;
1571 btrfs_set_root_flags(new_root_item, root_flags);
1572
8ea05e3a
AB
1573 btrfs_set_root_generation_v2(new_root_item,
1574 trans->transid);
1575 uuid_le_gen(&new_uuid);
1576 memcpy(new_root_item->uuid, new_uuid.b, BTRFS_UUID_SIZE);
1577 memcpy(new_root_item->parent_uuid, root->root_item.uuid,
1578 BTRFS_UUID_SIZE);
70023da2
SB
1579 if (!(root_flags & BTRFS_ROOT_SUBVOL_RDONLY)) {
1580 memset(new_root_item->received_uuid, 0,
1581 sizeof(new_root_item->received_uuid));
1582 memset(&new_root_item->stime, 0, sizeof(new_root_item->stime));
1583 memset(&new_root_item->rtime, 0, sizeof(new_root_item->rtime));
1584 btrfs_set_root_stransid(new_root_item, 0);
1585 btrfs_set_root_rtransid(new_root_item, 0);
1586 }
3cae210f
QW
1587 btrfs_set_stack_timespec_sec(&new_root_item->otime, cur_time.tv_sec);
1588 btrfs_set_stack_timespec_nsec(&new_root_item->otime, cur_time.tv_nsec);
8ea05e3a 1589 btrfs_set_root_otransid(new_root_item, trans->transid);
8ea05e3a 1590
6bdb72de 1591 old = btrfs_lock_root_node(root);
49b25e05 1592 ret = btrfs_cow_block(trans, root, old, NULL, 0, &old);
79787eaa
JM
1593 if (ret) {
1594 btrfs_tree_unlock(old);
1595 free_extent_buffer(old);
66642832 1596 btrfs_abort_transaction(trans, ret);
8732d44f 1597 goto fail;
79787eaa 1598 }
49b25e05 1599
8bead258 1600 btrfs_set_lock_blocking_write(old);
6bdb72de 1601
49b25e05 1602 ret = btrfs_copy_root(trans, root, old, &tmp, objectid);
79787eaa 1603 /* clean up in any case */
6bdb72de
SW
1604 btrfs_tree_unlock(old);
1605 free_extent_buffer(old);
8732d44f 1606 if (ret) {
66642832 1607 btrfs_abort_transaction(trans, ret);
8732d44f
MX
1608 goto fail;
1609 }
f1ebcc74 1610 /* see comments in should_cow_block() */
27cdeb70 1611 set_bit(BTRFS_ROOT_FORCE_COW, &root->state);
f1ebcc74
LB
1612 smp_wmb();
1613
6bdb72de 1614 btrfs_set_root_node(new_root_item, tmp);
a22285a6
YZ
1615 /* record when the snapshot was created in key.offset */
1616 key.offset = trans->transid;
1617 ret = btrfs_insert_root(trans, tree_root, &key, new_root_item);
6bdb72de
SW
1618 btrfs_tree_unlock(tmp);
1619 free_extent_buffer(tmp);
8732d44f 1620 if (ret) {
66642832 1621 btrfs_abort_transaction(trans, ret);
8732d44f
MX
1622 goto fail;
1623 }
6bdb72de 1624
a22285a6
YZ
1625 /*
1626 * insert root back/forward references
1627 */
6025c19f 1628 ret = btrfs_add_root_ref(trans, objectid,
0660b5af 1629 parent_root->root_key.objectid,
4a0cc7ca 1630 btrfs_ino(BTRFS_I(parent_inode)), index,
a22285a6 1631 dentry->d_name.name, dentry->d_name.len);
8732d44f 1632 if (ret) {
66642832 1633 btrfs_abort_transaction(trans, ret);
8732d44f
MX
1634 goto fail;
1635 }
0660b5af 1636
a22285a6 1637 key.offset = (u64)-1;
3619c94f 1638 pending->snap = btrfs_get_fs_root(fs_info, &key, true);
79787eaa
JM
1639 if (IS_ERR(pending->snap)) {
1640 ret = PTR_ERR(pending->snap);
66642832 1641 btrfs_abort_transaction(trans, ret);
8732d44f 1642 goto fail;
79787eaa 1643 }
d68fc57b 1644
49b25e05 1645 ret = btrfs_reloc_post_snapshot(trans, pending);
8732d44f 1646 if (ret) {
66642832 1647 btrfs_abort_transaction(trans, ret);
8732d44f
MX
1648 goto fail;
1649 }
361048f5 1650
c79a70b1 1651 ret = btrfs_run_delayed_refs(trans, (unsigned long)-1);
8732d44f 1652 if (ret) {
66642832 1653 btrfs_abort_transaction(trans, ret);
8732d44f
MX
1654 goto fail;
1655 }
42874b3d 1656
6426c7ad
QW
1657 /*
1658 * Do special qgroup accounting for snapshot, as we do some qgroup
1659 * snapshot hack to do fast snapshot.
1660 * To co-operate with that hack, we do hack again.
1661 * Or snapshot will be greatly slowed down by a subtree qgroup rescan
1662 */
1663 ret = qgroup_account_snapshot(trans, root, parent_root,
1664 pending->inherit, objectid);
1665 if (ret < 0)
1666 goto fail;
1667
684572df
LF
1668 ret = btrfs_insert_dir_item(trans, dentry->d_name.name,
1669 dentry->d_name.len, BTRFS_I(parent_inode),
1670 &key, BTRFS_FT_DIR, index);
42874b3d 1671 /* We have check then name at the beginning, so it is impossible. */
9c52057c 1672 BUG_ON(ret == -EEXIST || ret == -EOVERFLOW);
8732d44f 1673 if (ret) {
66642832 1674 btrfs_abort_transaction(trans, ret);
8732d44f
MX
1675 goto fail;
1676 }
42874b3d 1677
6ef06d27 1678 btrfs_i_size_write(BTRFS_I(parent_inode), parent_inode->i_size +
42874b3d 1679 dentry->d_name.len * 2);
04b285f3 1680 parent_inode->i_mtime = parent_inode->i_ctime =
c2050a45 1681 current_time(parent_inode);
be6aef60 1682 ret = btrfs_update_inode_fallback(trans, parent_root, parent_inode);
dd5f9615 1683 if (ret) {
66642832 1684 btrfs_abort_transaction(trans, ret);
dd5f9615
SB
1685 goto fail;
1686 }
cdb345a8
LF
1687 ret = btrfs_uuid_tree_add(trans, new_uuid.b, BTRFS_UUID_KEY_SUBVOL,
1688 objectid);
dd5f9615 1689 if (ret) {
66642832 1690 btrfs_abort_transaction(trans, ret);
dd5f9615
SB
1691 goto fail;
1692 }
1693 if (!btrfs_is_empty_uuid(new_root_item->received_uuid)) {
cdb345a8 1694 ret = btrfs_uuid_tree_add(trans, new_root_item->received_uuid,
dd5f9615
SB
1695 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
1696 objectid);
1697 if (ret && ret != -EEXIST) {
66642832 1698 btrfs_abort_transaction(trans, ret);
dd5f9615
SB
1699 goto fail;
1700 }
1701 }
d6726335 1702
c79a70b1 1703 ret = btrfs_run_delayed_refs(trans, (unsigned long)-1);
d6726335 1704 if (ret) {
66642832 1705 btrfs_abort_transaction(trans, ret);
d6726335
QW
1706 goto fail;
1707 }
1708
3063d29f 1709fail:
aec8030a
MX
1710 pending->error = ret;
1711dir_item_existed:
98c9942a 1712 trans->block_rsv = rsv;
2382c5cc 1713 trans->bytes_reserved = 0;
d6726335
QW
1714clear_skip_qgroup:
1715 btrfs_clear_skip_qgroup(trans);
6fa9700e
MX
1716no_free_objectid:
1717 kfree(new_root_item);
b0c0ea63 1718 pending->root_item = NULL;
42874b3d 1719 btrfs_free_path(path);
8546b570
DS
1720 pending->path = NULL;
1721
49b25e05 1722 return ret;
3063d29f
CM
1723}
1724
d352ac68
CM
1725/*
1726 * create all the snapshots we've scheduled for creation
1727 */
08d50ca3 1728static noinline int create_pending_snapshots(struct btrfs_trans_handle *trans)
3de4586c 1729{
aec8030a 1730 struct btrfs_pending_snapshot *pending, *next;
3de4586c 1731 struct list_head *head = &trans->transaction->pending_snapshots;
aec8030a 1732 int ret = 0;
3de4586c 1733
aec8030a
MX
1734 list_for_each_entry_safe(pending, next, head, list) {
1735 list_del(&pending->list);
08d50ca3 1736 ret = create_pending_snapshot(trans, pending);
aec8030a
MX
1737 if (ret)
1738 break;
1739 }
1740 return ret;
3de4586c
CM
1741}
1742
2ff7e61e 1743static void update_super_roots(struct btrfs_fs_info *fs_info)
5d4f98a2
YZ
1744{
1745 struct btrfs_root_item *root_item;
1746 struct btrfs_super_block *super;
1747
0b246afa 1748 super = fs_info->super_copy;
5d4f98a2 1749
0b246afa 1750 root_item = &fs_info->chunk_root->root_item;
093e037c
DS
1751 super->chunk_root = root_item->bytenr;
1752 super->chunk_root_generation = root_item->generation;
1753 super->chunk_root_level = root_item->level;
5d4f98a2 1754
0b246afa 1755 root_item = &fs_info->tree_root->root_item;
093e037c
DS
1756 super->root = root_item->bytenr;
1757 super->generation = root_item->generation;
1758 super->root_level = root_item->level;
0b246afa 1759 if (btrfs_test_opt(fs_info, SPACE_CACHE))
093e037c 1760 super->cache_generation = root_item->generation;
0b246afa 1761 if (test_bit(BTRFS_FS_UPDATE_UUID_TREE_GEN, &fs_info->flags))
093e037c 1762 super->uuid_tree_generation = root_item->generation;
5d4f98a2
YZ
1763}
1764
f36f3042
CM
1765int btrfs_transaction_in_commit(struct btrfs_fs_info *info)
1766{
4a9d8bde 1767 struct btrfs_transaction *trans;
f36f3042 1768 int ret = 0;
4a9d8bde 1769
a4abeea4 1770 spin_lock(&info->trans_lock);
4a9d8bde
MX
1771 trans = info->running_transaction;
1772 if (trans)
1773 ret = (trans->state >= TRANS_STATE_COMMIT_START);
a4abeea4 1774 spin_unlock(&info->trans_lock);
f36f3042
CM
1775 return ret;
1776}
1777
8929ecfa
YZ
1778int btrfs_transaction_blocked(struct btrfs_fs_info *info)
1779{
4a9d8bde 1780 struct btrfs_transaction *trans;
8929ecfa 1781 int ret = 0;
4a9d8bde 1782
a4abeea4 1783 spin_lock(&info->trans_lock);
4a9d8bde
MX
1784 trans = info->running_transaction;
1785 if (trans)
1786 ret = is_transaction_blocked(trans);
a4abeea4 1787 spin_unlock(&info->trans_lock);
8929ecfa
YZ
1788 return ret;
1789}
1790
bb9c12c9
SW
1791/*
1792 * wait for the current transaction commit to start and block subsequent
1793 * transaction joins
1794 */
2ff7e61e 1795static void wait_current_trans_commit_start(struct btrfs_fs_info *fs_info,
bb9c12c9
SW
1796 struct btrfs_transaction *trans)
1797{
2ff7e61e 1798 wait_event(fs_info->transaction_blocked_wait,
bf31f87f
DS
1799 trans->state >= TRANS_STATE_COMMIT_START ||
1800 TRANS_ABORTED(trans));
bb9c12c9
SW
1801}
1802
1803/*
1804 * wait for the current transaction to start and then become unblocked.
1805 * caller holds ref.
1806 */
2ff7e61e
JM
1807static void wait_current_trans_commit_start_and_unblock(
1808 struct btrfs_fs_info *fs_info,
1809 struct btrfs_transaction *trans)
bb9c12c9 1810{
2ff7e61e 1811 wait_event(fs_info->transaction_wait,
bf31f87f
DS
1812 trans->state >= TRANS_STATE_UNBLOCKED ||
1813 TRANS_ABORTED(trans));
bb9c12c9
SW
1814}
1815
1816/*
1817 * commit transactions asynchronously. once btrfs_commit_transaction_async
1818 * returns, any subsequent transaction will not be allowed to join.
1819 */
1820struct btrfs_async_commit {
1821 struct btrfs_trans_handle *newtrans;
7892b5af 1822 struct work_struct work;
bb9c12c9
SW
1823};
1824
1825static void do_async_commit(struct work_struct *work)
1826{
1827 struct btrfs_async_commit *ac =
7892b5af 1828 container_of(work, struct btrfs_async_commit, work);
bb9c12c9 1829
6fc4e354
SW
1830 /*
1831 * We've got freeze protection passed with the transaction.
1832 * Tell lockdep about it.
1833 */
b1a06a4b 1834 if (ac->newtrans->type & __TRANS_FREEZABLE)
3a45bb20 1835 __sb_writers_acquired(ac->newtrans->fs_info->sb, SB_FREEZE_FS);
6fc4e354 1836
e209db7a
SW
1837 current->journal_info = ac->newtrans;
1838
3a45bb20 1839 btrfs_commit_transaction(ac->newtrans);
bb9c12c9
SW
1840 kfree(ac);
1841}
1842
1843int btrfs_commit_transaction_async(struct btrfs_trans_handle *trans,
bb9c12c9
SW
1844 int wait_for_unblock)
1845{
3a45bb20 1846 struct btrfs_fs_info *fs_info = trans->fs_info;
bb9c12c9
SW
1847 struct btrfs_async_commit *ac;
1848 struct btrfs_transaction *cur_trans;
1849
1850 ac = kmalloc(sizeof(*ac), GFP_NOFS);
db5b493a
TI
1851 if (!ac)
1852 return -ENOMEM;
bb9c12c9 1853
7892b5af 1854 INIT_WORK(&ac->work, do_async_commit);
3a45bb20 1855 ac->newtrans = btrfs_join_transaction(trans->root);
3612b495
TI
1856 if (IS_ERR(ac->newtrans)) {
1857 int err = PTR_ERR(ac->newtrans);
1858 kfree(ac);
1859 return err;
1860 }
bb9c12c9
SW
1861
1862 /* take transaction reference */
bb9c12c9 1863 cur_trans = trans->transaction;
9b64f57d 1864 refcount_inc(&cur_trans->use_count);
bb9c12c9 1865
3a45bb20 1866 btrfs_end_transaction(trans);
6fc4e354
SW
1867
1868 /*
1869 * Tell lockdep we've released the freeze rwsem, since the
1870 * async commit thread will be the one to unlock it.
1871 */
b1a06a4b 1872 if (ac->newtrans->type & __TRANS_FREEZABLE)
0b246afa 1873 __sb_writers_release(fs_info->sb, SB_FREEZE_FS);
6fc4e354 1874
7892b5af 1875 schedule_work(&ac->work);
bb9c12c9
SW
1876
1877 /* wait for transaction to start and unblock */
bb9c12c9 1878 if (wait_for_unblock)
2ff7e61e 1879 wait_current_trans_commit_start_and_unblock(fs_info, cur_trans);
bb9c12c9 1880 else
2ff7e61e 1881 wait_current_trans_commit_start(fs_info, cur_trans);
bb9c12c9 1882
38e88054
SW
1883 if (current->journal_info == trans)
1884 current->journal_info = NULL;
1885
724e2315 1886 btrfs_put_transaction(cur_trans);
bb9c12c9
SW
1887 return 0;
1888}
1889
49b25e05 1890
97cb39bb 1891static void cleanup_transaction(struct btrfs_trans_handle *trans, int err)
49b25e05 1892{
97cb39bb 1893 struct btrfs_fs_info *fs_info = trans->fs_info;
49b25e05
JM
1894 struct btrfs_transaction *cur_trans = trans->transaction;
1895
b50fff81 1896 WARN_ON(refcount_read(&trans->use_count) > 1);
49b25e05 1897
66642832 1898 btrfs_abort_transaction(trans, err);
7b8b92af 1899
0b246afa 1900 spin_lock(&fs_info->trans_lock);
66b6135b 1901
25d8c284
MX
1902 /*
1903 * If the transaction is removed from the list, it means this
1904 * transaction has been committed successfully, so it is impossible
1905 * to call the cleanup function.
1906 */
1907 BUG_ON(list_empty(&cur_trans->list));
66b6135b 1908
49b25e05 1909 list_del_init(&cur_trans->list);
0b246afa 1910 if (cur_trans == fs_info->running_transaction) {
4a9d8bde 1911 cur_trans->state = TRANS_STATE_COMMIT_DOING;
0b246afa 1912 spin_unlock(&fs_info->trans_lock);
f094ac32
LB
1913 wait_event(cur_trans->writer_wait,
1914 atomic_read(&cur_trans->num_writers) == 1);
1915
0b246afa 1916 spin_lock(&fs_info->trans_lock);
d7096fc3 1917 }
0b246afa 1918 spin_unlock(&fs_info->trans_lock);
49b25e05 1919
2ff7e61e 1920 btrfs_cleanup_one_transaction(trans->transaction, fs_info);
49b25e05 1921
0b246afa
JM
1922 spin_lock(&fs_info->trans_lock);
1923 if (cur_trans == fs_info->running_transaction)
1924 fs_info->running_transaction = NULL;
1925 spin_unlock(&fs_info->trans_lock);
4a9d8bde 1926
e0228285 1927 if (trans->type & __TRANS_FREEZABLE)
0b246afa 1928 sb_end_intwrite(fs_info->sb);
724e2315
JB
1929 btrfs_put_transaction(cur_trans);
1930 btrfs_put_transaction(cur_trans);
49b25e05 1931
97cb39bb 1932 trace_btrfs_transaction_commit(trans->root);
49b25e05 1933
49b25e05
JM
1934 if (current->journal_info == trans)
1935 current->journal_info = NULL;
0b246afa 1936 btrfs_scrub_cancel(fs_info);
49b25e05
JM
1937
1938 kmem_cache_free(btrfs_trans_handle_cachep, trans);
1939}
1940
c7cc64a9
DS
1941/*
1942 * Release reserved delayed ref space of all pending block groups of the
1943 * transaction and remove them from the list
1944 */
1945static void btrfs_cleanup_pending_block_groups(struct btrfs_trans_handle *trans)
1946{
1947 struct btrfs_fs_info *fs_info = trans->fs_info;
32da5386 1948 struct btrfs_block_group *block_group, *tmp;
c7cc64a9
DS
1949
1950 list_for_each_entry_safe(block_group, tmp, &trans->new_bgs, bg_list) {
1951 btrfs_delayed_refs_rsv_release(fs_info, 1);
1952 list_del_init(&block_group->bg_list);
1953 }
1954}
1955
609e804d 1956static inline int btrfs_start_delalloc_flush(struct btrfs_trans_handle *trans)
82436617 1957{
609e804d
FM
1958 struct btrfs_fs_info *fs_info = trans->fs_info;
1959
ce8ea7cc
JB
1960 /*
1961 * We use writeback_inodes_sb here because if we used
1962 * btrfs_start_delalloc_roots we would deadlock with fs freeze.
1963 * Currently are holding the fs freeze lock, if we do an async flush
1964 * we'll do btrfs_join_transaction() and deadlock because we need to
1965 * wait for the fs freeze lock. Using the direct flushing we benefit
1966 * from already being in a transaction and our join_transaction doesn't
1967 * have to re-take the fs freeze lock.
1968 */
609e804d 1969 if (btrfs_test_opt(fs_info, FLUSHONCOMMIT)) {
ce8ea7cc 1970 writeback_inodes_sb(fs_info->sb, WB_REASON_SYNC);
609e804d
FM
1971 } else {
1972 struct btrfs_pending_snapshot *pending;
1973 struct list_head *head = &trans->transaction->pending_snapshots;
1974
1975 /*
1976 * Flush dellaloc for any root that is going to be snapshotted.
1977 * This is done to avoid a corrupted version of files, in the
1978 * snapshots, that had both buffered and direct IO writes (even
1979 * if they were done sequentially) due to an unordered update of
1980 * the inode's size on disk.
1981 */
1982 list_for_each_entry(pending, head, list) {
1983 int ret;
1984
1985 ret = btrfs_start_delalloc_snapshot(pending->root);
1986 if (ret)
1987 return ret;
1988 }
1989 }
82436617
MX
1990 return 0;
1991}
1992
609e804d 1993static inline void btrfs_wait_delalloc_flush(struct btrfs_trans_handle *trans)
82436617 1994{
609e804d
FM
1995 struct btrfs_fs_info *fs_info = trans->fs_info;
1996
1997 if (btrfs_test_opt(fs_info, FLUSHONCOMMIT)) {
6374e57a 1998 btrfs_wait_ordered_roots(fs_info, U64_MAX, 0, (u64)-1);
609e804d
FM
1999 } else {
2000 struct btrfs_pending_snapshot *pending;
2001 struct list_head *head = &trans->transaction->pending_snapshots;
2002
2003 /*
2004 * Wait for any dellaloc that we started previously for the roots
2005 * that are going to be snapshotted. This is to avoid a corrupted
2006 * version of files in the snapshots that had both buffered and
2007 * direct IO writes (even if they were done sequentially).
2008 */
2009 list_for_each_entry(pending, head, list)
2010 btrfs_wait_ordered_extents(pending->root,
2011 U64_MAX, 0, U64_MAX);
2012 }
82436617
MX
2013}
2014
3a45bb20 2015int btrfs_commit_transaction(struct btrfs_trans_handle *trans)
79154b1b 2016{
3a45bb20 2017 struct btrfs_fs_info *fs_info = trans->fs_info;
49b25e05 2018 struct btrfs_transaction *cur_trans = trans->transaction;
8fd17795 2019 struct btrfs_transaction *prev_trans = NULL;
25287e0a 2020 int ret;
79154b1b 2021
35b814f3
NB
2022 ASSERT(refcount_read(&trans->use_count) == 1);
2023
d62b23c9
JB
2024 /*
2025 * Some places just start a transaction to commit it. We need to make
2026 * sure that if this commit fails that the abort code actually marks the
2027 * transaction as failed, so set trans->dirty to make the abort code do
2028 * the right thing.
2029 */
2030 trans->dirty = true;
2031
8d25a086 2032 /* Stop the commit early if ->aborted is set */
bf31f87f 2033 if (TRANS_ABORTED(cur_trans)) {
25287e0a 2034 ret = cur_trans->aborted;
3a45bb20 2035 btrfs_end_transaction(trans);
e4a2bcac 2036 return ret;
25287e0a 2037 }
49b25e05 2038
f45c752b
JB
2039 btrfs_trans_release_metadata(trans);
2040 trans->block_rsv = NULL;
2041
56bec294
CM
2042 /* make a pass through all the delayed refs we have so far
2043 * any runnings procs may add more while we are here
2044 */
c79a70b1 2045 ret = btrfs_run_delayed_refs(trans, 0);
e4a2bcac 2046 if (ret) {
3a45bb20 2047 btrfs_end_transaction(trans);
e4a2bcac
JB
2048 return ret;
2049 }
56bec294 2050
b7ec40d7 2051 cur_trans = trans->transaction;
49b25e05 2052
56bec294
CM
2053 /*
2054 * set the flushing flag so procs in this transaction have to
2055 * start sending their work down.
2056 */
b7ec40d7 2057 cur_trans->delayed_refs.flushing = 1;
1be41b78 2058 smp_wmb();
56bec294 2059
119e80df 2060 btrfs_create_pending_block_groups(trans);
ea658bad 2061
c79a70b1 2062 ret = btrfs_run_delayed_refs(trans, 0);
e4a2bcac 2063 if (ret) {
3a45bb20 2064 btrfs_end_transaction(trans);
e4a2bcac
JB
2065 return ret;
2066 }
56bec294 2067
3204d33c 2068 if (!test_bit(BTRFS_TRANS_DIRTY_BG_RUN, &cur_trans->flags)) {
1bbc621e
CM
2069 int run_it = 0;
2070
2071 /* this mutex is also taken before trying to set
2072 * block groups readonly. We need to make sure
2073 * that nobody has set a block group readonly
2074 * after a extents from that block group have been
2075 * allocated for cache files. btrfs_set_block_group_ro
2076 * will wait for the transaction to commit if it
3204d33c 2077 * finds BTRFS_TRANS_DIRTY_BG_RUN set.
1bbc621e 2078 *
3204d33c
JB
2079 * The BTRFS_TRANS_DIRTY_BG_RUN flag is also used to make sure
2080 * only one process starts all the block group IO. It wouldn't
1bbc621e
CM
2081 * hurt to have more than one go through, but there's no
2082 * real advantage to it either.
2083 */
0b246afa 2084 mutex_lock(&fs_info->ro_block_group_mutex);
3204d33c
JB
2085 if (!test_and_set_bit(BTRFS_TRANS_DIRTY_BG_RUN,
2086 &cur_trans->flags))
1bbc621e 2087 run_it = 1;
0b246afa 2088 mutex_unlock(&fs_info->ro_block_group_mutex);
1bbc621e 2089
f9cacae3 2090 if (run_it) {
21217054 2091 ret = btrfs_start_dirty_block_groups(trans);
f9cacae3
NB
2092 if (ret) {
2093 btrfs_end_transaction(trans);
2094 return ret;
2095 }
2096 }
1bbc621e
CM
2097 }
2098
0b246afa 2099 spin_lock(&fs_info->trans_lock);
4a9d8bde 2100 if (cur_trans->state >= TRANS_STATE_COMMIT_START) {
0b246afa 2101 spin_unlock(&fs_info->trans_lock);
9b64f57d 2102 refcount_inc(&cur_trans->use_count);
3a45bb20 2103 ret = btrfs_end_transaction(trans);
ccd467d6 2104
2ff7e61e 2105 wait_for_commit(cur_trans);
15ee9bc7 2106
bf31f87f 2107 if (TRANS_ABORTED(cur_trans))
b4924a0f
LB
2108 ret = cur_trans->aborted;
2109
724e2315 2110 btrfs_put_transaction(cur_trans);
15ee9bc7 2111
49b25e05 2112 return ret;
79154b1b 2113 }
4313b399 2114
4a9d8bde 2115 cur_trans->state = TRANS_STATE_COMMIT_START;
0b246afa 2116 wake_up(&fs_info->transaction_blocked_wait);
bb9c12c9 2117
0b246afa 2118 if (cur_trans->list.prev != &fs_info->trans_list) {
ccd467d6
CM
2119 prev_trans = list_entry(cur_trans->list.prev,
2120 struct btrfs_transaction, list);
4a9d8bde 2121 if (prev_trans->state != TRANS_STATE_COMPLETED) {
9b64f57d 2122 refcount_inc(&prev_trans->use_count);
0b246afa 2123 spin_unlock(&fs_info->trans_lock);
ccd467d6 2124
2ff7e61e 2125 wait_for_commit(prev_trans);
bf31f87f 2126 ret = READ_ONCE(prev_trans->aborted);
ccd467d6 2127
724e2315 2128 btrfs_put_transaction(prev_trans);
1f9b8c8f
FM
2129 if (ret)
2130 goto cleanup_transaction;
a4abeea4 2131 } else {
0b246afa 2132 spin_unlock(&fs_info->trans_lock);
ccd467d6 2133 }
a4abeea4 2134 } else {
0b246afa 2135 spin_unlock(&fs_info->trans_lock);
cb2d3dad
FM
2136 /*
2137 * The previous transaction was aborted and was already removed
2138 * from the list of transactions at fs_info->trans_list. So we
2139 * abort to prevent writing a new superblock that reflects a
2140 * corrupt state (pointing to trees with unwritten nodes/leafs).
2141 */
2142 if (test_bit(BTRFS_FS_STATE_TRANS_ABORTED, &fs_info->fs_state)) {
2143 ret = -EROFS;
2144 goto cleanup_transaction;
2145 }
ccd467d6 2146 }
15ee9bc7 2147
0860adfd
MX
2148 extwriter_counter_dec(cur_trans, trans->type);
2149
609e804d 2150 ret = btrfs_start_delalloc_flush(trans);
82436617
MX
2151 if (ret)
2152 goto cleanup_transaction;
2153
e5c304e6 2154 ret = btrfs_run_delayed_items(trans);
581227d0
MX
2155 if (ret)
2156 goto cleanup_transaction;
15ee9bc7 2157
581227d0
MX
2158 wait_event(cur_trans->writer_wait,
2159 extwriter_counter_read(cur_trans) == 0);
15ee9bc7 2160
581227d0 2161 /* some pending stuffs might be added after the previous flush. */
e5c304e6 2162 ret = btrfs_run_delayed_items(trans);
ca469637
MX
2163 if (ret)
2164 goto cleanup_transaction;
2165
609e804d 2166 btrfs_wait_delalloc_flush(trans);
cb7ab021 2167
2ff7e61e 2168 btrfs_scrub_pause(fs_info);
ed0ca140
JB
2169 /*
2170 * Ok now we need to make sure to block out any other joins while we
2171 * commit the transaction. We could have started a join before setting
4a9d8bde 2172 * COMMIT_DOING so make sure to wait for num_writers to == 1 again.
ed0ca140 2173 */
0b246afa 2174 spin_lock(&fs_info->trans_lock);
4a9d8bde 2175 cur_trans->state = TRANS_STATE_COMMIT_DOING;
0b246afa 2176 spin_unlock(&fs_info->trans_lock);
ed0ca140
JB
2177 wait_event(cur_trans->writer_wait,
2178 atomic_read(&cur_trans->num_writers) == 1);
2179
bf31f87f 2180 if (TRANS_ABORTED(cur_trans)) {
2cba30f1 2181 ret = cur_trans->aborted;
6cf7f77e 2182 goto scrub_continue;
2cba30f1 2183 }
7585717f
CM
2184 /*
2185 * the reloc mutex makes sure that we stop
2186 * the balancing code from coming in and moving
2187 * extents around in the middle of the commit
2188 */
0b246afa 2189 mutex_lock(&fs_info->reloc_mutex);
7585717f 2190
42874b3d
MX
2191 /*
2192 * We needn't worry about the delayed items because we will
2193 * deal with them in create_pending_snapshot(), which is the
2194 * core function of the snapshot creation.
2195 */
08d50ca3 2196 ret = create_pending_snapshots(trans);
56e9f6ea
DS
2197 if (ret)
2198 goto unlock_reloc;
3063d29f 2199
42874b3d
MX
2200 /*
2201 * We insert the dir indexes of the snapshots and update the inode
2202 * of the snapshots' parents after the snapshot creation, so there
2203 * are some delayed items which are not dealt with. Now deal with
2204 * them.
2205 *
2206 * We needn't worry that this operation will corrupt the snapshots,
2207 * because all the tree which are snapshoted will be forced to COW
2208 * the nodes and leaves.
2209 */
e5c304e6 2210 ret = btrfs_run_delayed_items(trans);
56e9f6ea
DS
2211 if (ret)
2212 goto unlock_reloc;
16cdcec7 2213
c79a70b1 2214 ret = btrfs_run_delayed_refs(trans, (unsigned long)-1);
56e9f6ea
DS
2215 if (ret)
2216 goto unlock_reloc;
56bec294 2217
e999376f
CM
2218 /*
2219 * make sure none of the code above managed to slip in a
2220 * delayed item
2221 */
ccdf9b30 2222 btrfs_assert_delayed_root_empty(fs_info);
e999376f 2223
2c90e5d6 2224 WARN_ON(cur_trans != trans->transaction);
dc17ff8f 2225
e02119d5
CM
2226 /* btrfs_commit_tree_roots is responsible for getting the
2227 * various roots consistent with each other. Every pointer
2228 * in the tree of tree roots has to point to the most up to date
2229 * root for every subvolume and other tree. So, we have to keep
2230 * the tree logging code from jumping in and changing any
2231 * of the trees.
2232 *
2233 * At this point in the commit, there can't be any tree-log
2234 * writers, but a little lower down we drop the trans mutex
2235 * and let new people in. By holding the tree_log_mutex
2236 * from now until after the super is written, we avoid races
2237 * with the tree-log code.
2238 */
0b246afa 2239 mutex_lock(&fs_info->tree_log_mutex);
e02119d5 2240
7e4443d9 2241 ret = commit_fs_roots(trans);
56e9f6ea
DS
2242 if (ret)
2243 goto unlock_tree_log;
54aa1f4d 2244
3818aea2 2245 /*
7e1876ac
DS
2246 * Since the transaction is done, we can apply the pending changes
2247 * before the next transaction.
3818aea2 2248 */
0b246afa 2249 btrfs_apply_pending_changes(fs_info);
3818aea2 2250
5d4f98a2 2251 /* commit_fs_roots gets rid of all the tree log roots, it is now
e02119d5
CM
2252 * safe to free the root of tree log roots
2253 */
0b246afa 2254 btrfs_free_log_root_tree(trans, fs_info);
e02119d5 2255
82bafb38
QW
2256 /*
2257 * commit_fs_roots() can call btrfs_save_ino_cache(), which generates
2258 * new delayed refs. Must handle them or qgroup can be wrong.
2259 */
c79a70b1 2260 ret = btrfs_run_delayed_refs(trans, (unsigned long)-1);
56e9f6ea
DS
2261 if (ret)
2262 goto unlock_tree_log;
82bafb38 2263
0ed4792a
QW
2264 /*
2265 * Since fs roots are all committed, we can get a quite accurate
2266 * new_roots. So let's do quota accounting.
2267 */
460fb20a 2268 ret = btrfs_qgroup_account_extents(trans);
56e9f6ea
DS
2269 if (ret < 0)
2270 goto unlock_tree_log;
0ed4792a 2271
9386d8bc 2272 ret = commit_cowonly_roots(trans);
56e9f6ea
DS
2273 if (ret)
2274 goto unlock_tree_log;
54aa1f4d 2275
2cba30f1
MX
2276 /*
2277 * The tasks which save the space cache and inode cache may also
2278 * update ->aborted, check it.
2279 */
bf31f87f 2280 if (TRANS_ABORTED(cur_trans)) {
2cba30f1 2281 ret = cur_trans->aborted;
56e9f6ea 2282 goto unlock_tree_log;
2cba30f1
MX
2283 }
2284
8b74c03e 2285 btrfs_prepare_extent_commit(fs_info);
11833d66 2286
0b246afa 2287 cur_trans = fs_info->running_transaction;
5d4f98a2 2288
0b246afa
JM
2289 btrfs_set_root_node(&fs_info->tree_root->root_item,
2290 fs_info->tree_root->node);
2291 list_add_tail(&fs_info->tree_root->dirty_list,
9e351cc8 2292 &cur_trans->switch_commits);
5d4f98a2 2293
0b246afa
JM
2294 btrfs_set_root_node(&fs_info->chunk_root->root_item,
2295 fs_info->chunk_root->node);
2296 list_add_tail(&fs_info->chunk_root->dirty_list,
9e351cc8
JB
2297 &cur_trans->switch_commits);
2298
889bfa39 2299 switch_commit_roots(trans);
5d4f98a2 2300
ce93ec54 2301 ASSERT(list_empty(&cur_trans->dirty_bgs));
1bbc621e 2302 ASSERT(list_empty(&cur_trans->io_bgs));
2ff7e61e 2303 update_super_roots(fs_info);
e02119d5 2304
0b246afa
JM
2305 btrfs_set_super_log_root(fs_info->super_copy, 0);
2306 btrfs_set_super_log_root_level(fs_info->super_copy, 0);
2307 memcpy(fs_info->super_for_commit, fs_info->super_copy,
2308 sizeof(*fs_info->super_copy));
ccd467d6 2309
bbbf7243 2310 btrfs_commit_device_sizes(cur_trans);
935e5cc9 2311
0b246afa
JM
2312 clear_bit(BTRFS_FS_LOG1_ERR, &fs_info->flags);
2313 clear_bit(BTRFS_FS_LOG2_ERR, &fs_info->flags);
656f30db 2314
4fbcdf66
FM
2315 btrfs_trans_release_chunk_metadata(trans);
2316
0b246afa 2317 spin_lock(&fs_info->trans_lock);
4a9d8bde 2318 cur_trans->state = TRANS_STATE_UNBLOCKED;
0b246afa
JM
2319 fs_info->running_transaction = NULL;
2320 spin_unlock(&fs_info->trans_lock);
2321 mutex_unlock(&fs_info->reloc_mutex);
b7ec40d7 2322
0b246afa 2323 wake_up(&fs_info->transaction_wait);
e6dcd2dc 2324
70458a58 2325 ret = btrfs_write_and_wait_transaction(trans);
49b25e05 2326 if (ret) {
0b246afa
JM
2327 btrfs_handle_fs_error(fs_info, ret,
2328 "Error while writing out transaction");
56e9f6ea
DS
2329 /*
2330 * reloc_mutex has been unlocked, tree_log_mutex is still held
2331 * but we can't jump to unlock_tree_log causing double unlock
2332 */
0b246afa 2333 mutex_unlock(&fs_info->tree_log_mutex);
6cf7f77e 2334 goto scrub_continue;
49b25e05
JM
2335 }
2336
eece6a9c 2337 ret = write_all_supers(fs_info, 0);
e02119d5
CM
2338 /*
2339 * the super is written, we can safely allow the tree-loggers
2340 * to go about their business
2341 */
0b246afa 2342 mutex_unlock(&fs_info->tree_log_mutex);
c1f32b7c
AJ
2343 if (ret)
2344 goto scrub_continue;
e02119d5 2345
5ead2dd0 2346 btrfs_finish_extent_commit(trans);
4313b399 2347
3204d33c 2348 if (test_bit(BTRFS_TRANS_HAVE_FREE_BGS, &cur_trans->flags))
0b246afa 2349 btrfs_clear_space_info_full(fs_info);
13212b54 2350
0b246afa 2351 fs_info->last_trans_committed = cur_trans->transid;
4a9d8bde
MX
2352 /*
2353 * We needn't acquire the lock here because there is no other task
2354 * which can change it.
2355 */
2356 cur_trans->state = TRANS_STATE_COMPLETED;
2c90e5d6 2357 wake_up(&cur_trans->commit_wait);
a514d638 2358 clear_bit(BTRFS_FS_NEED_ASYNC_COMMIT, &fs_info->flags);
3de4586c 2359
0b246afa 2360 spin_lock(&fs_info->trans_lock);
13c5a93e 2361 list_del_init(&cur_trans->list);
0b246afa 2362 spin_unlock(&fs_info->trans_lock);
a4abeea4 2363
724e2315
JB
2364 btrfs_put_transaction(cur_trans);
2365 btrfs_put_transaction(cur_trans);
58176a96 2366
0860adfd 2367 if (trans->type & __TRANS_FREEZABLE)
0b246afa 2368 sb_end_intwrite(fs_info->sb);
b2b5ef5c 2369
3a45bb20 2370 trace_btrfs_transaction_commit(trans->root);
1abe9b8a 2371
2ff7e61e 2372 btrfs_scrub_continue(fs_info);
a2de733c 2373
9ed74f2d
JB
2374 if (current->journal_info == trans)
2375 current->journal_info = NULL;
2376
2c90e5d6 2377 kmem_cache_free(btrfs_trans_handle_cachep, trans);
24bbcf04 2378
79154b1b 2379 return ret;
49b25e05 2380
56e9f6ea
DS
2381unlock_tree_log:
2382 mutex_unlock(&fs_info->tree_log_mutex);
2383unlock_reloc:
2384 mutex_unlock(&fs_info->reloc_mutex);
6cf7f77e 2385scrub_continue:
2ff7e61e 2386 btrfs_scrub_continue(fs_info);
49b25e05 2387cleanup_transaction:
dc60c525 2388 btrfs_trans_release_metadata(trans);
c7cc64a9 2389 btrfs_cleanup_pending_block_groups(trans);
4fbcdf66 2390 btrfs_trans_release_chunk_metadata(trans);
0e721106 2391 trans->block_rsv = NULL;
0b246afa 2392 btrfs_warn(fs_info, "Skipping commit of aborted transaction.");
49b25e05
JM
2393 if (current->journal_info == trans)
2394 current->journal_info = NULL;
97cb39bb 2395 cleanup_transaction(trans, ret);
49b25e05
JM
2396
2397 return ret;
79154b1b
CM
2398}
2399
d352ac68 2400/*
9d1a2a3a
DS
2401 * return < 0 if error
2402 * 0 if there are no more dead_roots at the time of call
2403 * 1 there are more to be processed, call me again
2404 *
2405 * The return value indicates there are certainly more snapshots to delete, but
2406 * if there comes a new one during processing, it may return 0. We don't mind,
2407 * because btrfs_commit_super will poke cleaner thread and it will process it a
2408 * few seconds later.
d352ac68 2409 */
9d1a2a3a 2410int btrfs_clean_one_deleted_snapshot(struct btrfs_root *root)
e9d0b13b 2411{
9d1a2a3a 2412 int ret;
5d4f98a2
YZ
2413 struct btrfs_fs_info *fs_info = root->fs_info;
2414
a4abeea4 2415 spin_lock(&fs_info->trans_lock);
9d1a2a3a
DS
2416 if (list_empty(&fs_info->dead_roots)) {
2417 spin_unlock(&fs_info->trans_lock);
2418 return 0;
2419 }
2420 root = list_first_entry(&fs_info->dead_roots,
2421 struct btrfs_root, root_list);
cfad392b 2422 list_del_init(&root->root_list);
a4abeea4 2423 spin_unlock(&fs_info->trans_lock);
e9d0b13b 2424
4fd786e6 2425 btrfs_debug(fs_info, "cleaner removing %llu", root->root_key.objectid);
76dda93c 2426
9d1a2a3a 2427 btrfs_kill_all_delayed_nodes(root);
16cdcec7 2428
9d1a2a3a
DS
2429 if (btrfs_header_backref_rev(root->node) <
2430 BTRFS_MIXED_BACKREF_REV)
2431 ret = btrfs_drop_snapshot(root, NULL, 0, 0);
2432 else
2433 ret = btrfs_drop_snapshot(root, NULL, 1, 0);
32471dc2 2434
6596a928 2435 return (ret < 0) ? 0 : 1;
e9d0b13b 2436}
572d9ab7
DS
2437
2438void btrfs_apply_pending_changes(struct btrfs_fs_info *fs_info)
2439{
2440 unsigned long prev;
2441 unsigned long bit;
2442
6c9fe14f 2443 prev = xchg(&fs_info->pending_changes, 0);
572d9ab7
DS
2444 if (!prev)
2445 return;
2446
7e1876ac
DS
2447 bit = 1 << BTRFS_PENDING_SET_INODE_MAP_CACHE;
2448 if (prev & bit)
2449 btrfs_set_opt(fs_info->mount_opt, INODE_MAP_CACHE);
2450 prev &= ~bit;
2451
2452 bit = 1 << BTRFS_PENDING_CLEAR_INODE_MAP_CACHE;
2453 if (prev & bit)
2454 btrfs_clear_opt(fs_info->mount_opt, INODE_MAP_CACHE);
2455 prev &= ~bit;
2456
d51033d0
DS
2457 bit = 1 << BTRFS_PENDING_COMMIT;
2458 if (prev & bit)
2459 btrfs_debug(fs_info, "pending commit done");
2460 prev &= ~bit;
2461
572d9ab7
DS
2462 if (prev)
2463 btrfs_warn(fs_info,
2464 "unknown pending changes left 0x%lx, ignoring", prev);
2465}