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