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