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