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