hc
2024-05-10 9999e48639b3cecb08ffb37358bcba3b48161b29
kernel/fs/btrfs/transaction.c
....@@ -10,6 +10,7 @@
1010 #include <linux/pagemap.h>
1111 #include <linux/blkdev.h>
1212 #include <linux/uuid.h>
13
+#include "misc.h"
1314 #include "ctree.h"
1415 #include "disk-io.h"
1516 #include "transaction.h"
....@@ -19,12 +20,84 @@
1920 #include "volumes.h"
2021 #include "dev-replace.h"
2122 #include "qgroup.h"
23
+#include "block-group.h"
24
+#include "space-info.h"
2225
2326 #define BTRFS_ROOT_TRANS_TAG 0
2427
28
+/*
29
+ * Transaction states and transitions
30
+ *
31
+ * No running transaction (fs tree blocks are not modified)
32
+ * |
33
+ * | To next stage:
34
+ * | Call start_transaction() variants. Except btrfs_join_transaction_nostart().
35
+ * V
36
+ * Transaction N [[TRANS_STATE_RUNNING]]
37
+ * |
38
+ * | New trans handles can be attached to transaction N by calling all
39
+ * | start_transaction() variants.
40
+ * |
41
+ * | To next stage:
42
+ * | Call btrfs_commit_transaction() on any trans handle attached to
43
+ * | transaction N
44
+ * V
45
+ * Transaction N [[TRANS_STATE_COMMIT_START]]
46
+ * |
47
+ * | Will wait for previous running transaction to completely finish if there
48
+ * | is one
49
+ * |
50
+ * | Then one of the following happes:
51
+ * | - Wait for all other trans handle holders to release.
52
+ * | The btrfs_commit_transaction() caller will do the commit work.
53
+ * | - Wait for current transaction to be committed by others.
54
+ * | Other btrfs_commit_transaction() caller will do the commit work.
55
+ * |
56
+ * | At this stage, only btrfs_join_transaction*() variants can attach
57
+ * | to this running transaction.
58
+ * | All other variants will wait for current one to finish and attach to
59
+ * | transaction N+1.
60
+ * |
61
+ * | To next stage:
62
+ * | Caller is chosen to commit transaction N, and all other trans handle
63
+ * | haven been released.
64
+ * V
65
+ * Transaction N [[TRANS_STATE_COMMIT_DOING]]
66
+ * |
67
+ * | The heavy lifting transaction work is started.
68
+ * | From running delayed refs (modifying extent tree) to creating pending
69
+ * | snapshots, running qgroups.
70
+ * | In short, modify supporting trees to reflect modifications of subvolume
71
+ * | trees.
72
+ * |
73
+ * | At this stage, all start_transaction() calls will wait for this
74
+ * | transaction to finish and attach to transaction N+1.
75
+ * |
76
+ * | To next stage:
77
+ * | Until all supporting trees are updated.
78
+ * V
79
+ * Transaction N [[TRANS_STATE_UNBLOCKED]]
80
+ * | Transaction N+1
81
+ * | All needed trees are modified, thus we only [[TRANS_STATE_RUNNING]]
82
+ * | need to write them back to disk and update |
83
+ * | super blocks. |
84
+ * | |
85
+ * | At this stage, new transaction is allowed to |
86
+ * | start. |
87
+ * | All new start_transaction() calls will be |
88
+ * | attached to transid N+1. |
89
+ * | |
90
+ * | To next stage: |
91
+ * | Until all tree blocks are super blocks are |
92
+ * | written to block devices |
93
+ * V |
94
+ * Transaction N [[TRANS_STATE_COMPLETED]] V
95
+ * All tree blocks and super blocks are written. Transaction N+1
96
+ * This transaction is finished and all its [[TRANS_STATE_COMMIT_START]]
97
+ * data structures will be cleaned up. | Life goes on
98
+ */
2599 static const unsigned int btrfs_blocked_trans_types[TRANS_STATE_MAX] = {
26100 [TRANS_STATE_RUNNING] = 0U,
27
- [TRANS_STATE_BLOCKED] = __TRANS_START,
28101 [TRANS_STATE_COMMIT_START] = (__TRANS_START | __TRANS_ATTACH),
29102 [TRANS_STATE_COMMIT_DOING] = (__TRANS_START |
30103 __TRANS_ATTACH |
....@@ -47,19 +120,14 @@
47120 WARN_ON(refcount_read(&transaction->use_count) == 0);
48121 if (refcount_dec_and_test(&transaction->use_count)) {
49122 BUG_ON(!list_empty(&transaction->list));
50
- WARN_ON(!RB_EMPTY_ROOT(&transaction->delayed_refs.href_root));
123
+ WARN_ON(!RB_EMPTY_ROOT(
124
+ &transaction->delayed_refs.href_root.rb_root));
125
+ WARN_ON(!RB_EMPTY_ROOT(
126
+ &transaction->delayed_refs.dirty_extent_root));
51127 if (transaction->delayed_refs.pending_csums)
52128 btrfs_err(transaction->fs_info,
53129 "pending csums is %llu",
54130 transaction->delayed_refs.pending_csums);
55
- while (!list_empty(&transaction->pending_chunks)) {
56
- struct extent_map *em;
57
-
58
- em = list_first_entry(&transaction->pending_chunks,
59
- struct extent_map, list);
60
- list_del_init(&em->list);
61
- free_extent_map(em);
62
- }
63131 /*
64132 * If any block groups are found in ->deleted_bgs then it's
65133 * because the transaction was aborted and a commit did not
....@@ -68,75 +136,90 @@
68136 * discard the physical locations of the block groups.
69137 */
70138 while (!list_empty(&transaction->deleted_bgs)) {
71
- struct btrfs_block_group_cache *cache;
139
+ struct btrfs_block_group *cache;
72140
73141 cache = list_first_entry(&transaction->deleted_bgs,
74
- struct btrfs_block_group_cache,
142
+ struct btrfs_block_group,
75143 bg_list);
76144 list_del_init(&cache->bg_list);
77
- btrfs_put_block_group_trimming(cache);
145
+ btrfs_unfreeze_block_group(cache);
78146 btrfs_put_block_group(cache);
79147 }
148
+ WARN_ON(!list_empty(&transaction->dev_update_list));
80149 kfree(transaction);
81150 }
82151 }
83152
84
-static void clear_btree_io_tree(struct extent_io_tree *tree)
153
+static noinline void switch_commit_roots(struct btrfs_trans_handle *trans)
85154 {
86
- spin_lock(&tree->lock);
87
- /*
88
- * Do a single barrier for the waitqueue_active check here, the state
89
- * of the waitqueue should not change once clear_btree_io_tree is
90
- * called.
91
- */
92
- smp_mb();
93
- while (!RB_EMPTY_ROOT(&tree->state)) {
94
- struct rb_node *node;
95
- struct extent_state *state;
96
-
97
- node = rb_first(&tree->state);
98
- state = rb_entry(node, struct extent_state, rb_node);
99
- rb_erase(&state->rb_node, &tree->state);
100
- RB_CLEAR_NODE(&state->rb_node);
101
- /*
102
- * btree io trees aren't supposed to have tasks waiting for
103
- * changes in the flags of extent states ever.
104
- */
105
- ASSERT(!waitqueue_active(&state->wq));
106
- free_extent_state(state);
107
-
108
- cond_resched_lock(&tree->lock);
109
- }
110
- spin_unlock(&tree->lock);
111
-}
112
-
113
-static noinline void switch_commit_roots(struct btrfs_transaction *trans)
114
-{
155
+ struct btrfs_transaction *cur_trans = trans->transaction;
115156 struct btrfs_fs_info *fs_info = trans->fs_info;
116157 struct btrfs_root *root, *tmp;
158
+ struct btrfs_caching_control *caching_ctl, *next;
117159
118160 down_write(&fs_info->commit_root_sem);
119
- list_for_each_entry_safe(root, tmp, &trans->switch_commits,
161
+ list_for_each_entry_safe(root, tmp, &cur_trans->switch_commits,
120162 dirty_list) {
121163 list_del_init(&root->dirty_list);
122164 free_extent_buffer(root->commit_root);
123165 root->commit_root = btrfs_root_node(root);
124
- if (is_fstree(root->objectid))
166
+ if (is_fstree(root->root_key.objectid))
125167 btrfs_unpin_free_ino(root);
126
- clear_btree_io_tree(&root->dirty_log_pages);
168
+ extent_io_tree_release(&root->dirty_log_pages);
169
+ btrfs_qgroup_clean_swapped_blocks(root);
127170 }
128171
129172 /* We can free old roots now. */
130
- spin_lock(&trans->dropped_roots_lock);
131
- while (!list_empty(&trans->dropped_roots)) {
132
- root = list_first_entry(&trans->dropped_roots,
173
+ spin_lock(&cur_trans->dropped_roots_lock);
174
+ while (!list_empty(&cur_trans->dropped_roots)) {
175
+ root = list_first_entry(&cur_trans->dropped_roots,
133176 struct btrfs_root, root_list);
134177 list_del_init(&root->root_list);
135
- spin_unlock(&trans->dropped_roots_lock);
178
+ spin_unlock(&cur_trans->dropped_roots_lock);
179
+ btrfs_free_log(trans, root);
136180 btrfs_drop_and_free_fs_root(fs_info, root);
137
- spin_lock(&trans->dropped_roots_lock);
181
+ spin_lock(&cur_trans->dropped_roots_lock);
138182 }
139
- spin_unlock(&trans->dropped_roots_lock);
183
+ spin_unlock(&cur_trans->dropped_roots_lock);
184
+
185
+ /*
186
+ * We have to update the last_byte_to_unpin under the commit_root_sem,
187
+ * at the same time we swap out the commit roots.
188
+ *
189
+ * This is because we must have a real view of the last spot the caching
190
+ * kthreads were while caching. Consider the following views of the
191
+ * extent tree for a block group
192
+ *
193
+ * commit root
194
+ * +----+----+----+----+----+----+----+
195
+ * |\\\\| |\\\\|\\\\| |\\\\|\\\\|
196
+ * +----+----+----+----+----+----+----+
197
+ * 0 1 2 3 4 5 6 7
198
+ *
199
+ * new commit root
200
+ * +----+----+----+----+----+----+----+
201
+ * | | | |\\\\| | |\\\\|
202
+ * +----+----+----+----+----+----+----+
203
+ * 0 1 2 3 4 5 6 7
204
+ *
205
+ * If the cache_ctl->progress was at 3, then we are only allowed to
206
+ * unpin [0,1) and [2,3], because the caching thread has already
207
+ * processed those extents. We are not allowed to unpin [5,6), because
208
+ * the caching thread will re-start it's search from 3, and thus find
209
+ * the hole from [4,6) to add to the free space cache.
210
+ */
211
+ list_for_each_entry_safe(caching_ctl, next,
212
+ &fs_info->caching_block_groups, list) {
213
+ struct btrfs_block_group *cache = caching_ctl->block_group;
214
+
215
+ if (btrfs_block_group_done(cache)) {
216
+ cache->last_byte_to_unpin = (u64)-1;
217
+ list_del_init(&caching_ctl->list);
218
+ btrfs_put_caching_control(caching_ctl);
219
+ } else {
220
+ cache->last_byte_to_unpin = caching_ctl->progress;
221
+ }
222
+ }
140223 up_write(&fs_info->commit_root_sem);
141224 }
142225
....@@ -166,6 +249,24 @@
166249 }
167250
168251 /*
252
+ * To be called after all the new block groups attached to the transaction
253
+ * handle have been created (btrfs_create_pending_block_groups()).
254
+ */
255
+void btrfs_trans_release_chunk_metadata(struct btrfs_trans_handle *trans)
256
+{
257
+ struct btrfs_fs_info *fs_info = trans->fs_info;
258
+
259
+ if (!trans->chunk_bytes_reserved)
260
+ return;
261
+
262
+ WARN_ON_ONCE(!list_empty(&trans->new_bgs));
263
+
264
+ btrfs_block_rsv_release(fs_info, &fs_info->chunk_block_rsv,
265
+ trans->chunk_bytes_reserved, NULL);
266
+ trans->chunk_bytes_reserved = 0;
267
+}
268
+
269
+/*
169270 * either allocate a new transaction or hop into the existing one
170271 */
171272 static noinline int join_transaction(struct btrfs_fs_info *fs_info,
....@@ -183,7 +284,7 @@
183284
184285 cur_trans = fs_info->running_transaction;
185286 if (cur_trans) {
186
- if (cur_trans->aborted) {
287
+ if (TRANS_ABORTED(cur_trans)) {
187288 spin_unlock(&fs_info->trans_lock);
188289 return cur_trans->aborted;
189290 }
....@@ -200,10 +301,11 @@
200301 spin_unlock(&fs_info->trans_lock);
201302
202303 /*
203
- * If we are ATTACH, we just want to catch the current transaction,
204
- * and commit it. If there is no transaction, just return ENOENT.
304
+ * If we are ATTACH or TRANS_JOIN_NOSTART, we just want to catch the
305
+ * current transaction, and commit it. If there is no transaction, just
306
+ * return ENOENT.
205307 */
206
- if (type == TRANS_ATTACH)
308
+ if (type == TRANS_ATTACH || type == TRANS_JOIN_NOSTART)
207309 return -ENOENT;
208310
209311 /*
....@@ -231,24 +333,24 @@
231333 }
232334
233335 cur_trans->fs_info = fs_info;
336
+ atomic_set(&cur_trans->pending_ordered, 0);
337
+ init_waitqueue_head(&cur_trans->pending_wait);
234338 atomic_set(&cur_trans->num_writers, 1);
235339 extwriter_counter_init(cur_trans, type);
236340 init_waitqueue_head(&cur_trans->writer_wait);
237341 init_waitqueue_head(&cur_trans->commit_wait);
238
- init_waitqueue_head(&cur_trans->pending_wait);
239342 cur_trans->state = TRANS_STATE_RUNNING;
240343 /*
241344 * One for this trans handle, one so it will live on until we
242345 * commit the transaction.
243346 */
244347 refcount_set(&cur_trans->use_count, 2);
245
- atomic_set(&cur_trans->pending_ordered, 0);
246348 cur_trans->flags = 0;
247349 cur_trans->start_time = ktime_get_seconds();
248350
249351 memset(&cur_trans->delayed_refs, 0, sizeof(cur_trans->delayed_refs));
250352
251
- cur_trans->delayed_refs.href_root = RB_ROOT;
353
+ cur_trans->delayed_refs.href_root = RB_ROOT_CACHED;
252354 cur_trans->delayed_refs.dirty_extent_root = RB_ROOT;
253355 atomic_set(&cur_trans->delayed_refs.num_entries, 0);
254356
....@@ -266,19 +368,20 @@
266368 spin_lock_init(&cur_trans->delayed_refs.lock);
267369
268370 INIT_LIST_HEAD(&cur_trans->pending_snapshots);
269
- INIT_LIST_HEAD(&cur_trans->pending_chunks);
371
+ INIT_LIST_HEAD(&cur_trans->dev_update_list);
270372 INIT_LIST_HEAD(&cur_trans->switch_commits);
271373 INIT_LIST_HEAD(&cur_trans->dirty_bgs);
272374 INIT_LIST_HEAD(&cur_trans->io_bgs);
273375 INIT_LIST_HEAD(&cur_trans->dropped_roots);
274376 mutex_init(&cur_trans->cache_write_mutex);
275
- cur_trans->num_dirty_bgs = 0;
276377 spin_lock_init(&cur_trans->dirty_bgs_lock);
277378 INIT_LIST_HEAD(&cur_trans->deleted_bgs);
278379 spin_lock_init(&cur_trans->dropped_roots_lock);
279380 list_add_tail(&cur_trans->list, &fs_info->trans_list);
280
- extent_io_tree_init(&cur_trans->dirty_pages,
281
- fs_info->btree_inode);
381
+ extent_io_tree_init(fs_info, &cur_trans->dirty_pages,
382
+ IO_TREE_TRANS_DIRTY_PAGES, fs_info->btree_inode);
383
+ extent_io_tree_init(fs_info, &cur_trans->pinned_extents,
384
+ IO_TREE_FS_PINNED_EXTENTS, NULL);
282385 fs_info->generation++;
283386 cur_trans->transid = fs_info->generation;
284387 fs_info->running_transaction = cur_trans;
....@@ -289,10 +392,10 @@
289392 }
290393
291394 /*
292
- * this does all the record keeping required to make sure that a reference
293
- * counted root is properly recorded in a given transaction. This is required
294
- * to make sure the old root from before we joined the transaction is deleted
295
- * when the transaction commits
395
+ * This does all the record keeping required to make sure that a shareable root
396
+ * is properly recorded in a given transaction. This is required to make sure
397
+ * the old root from before we joined the transaction is deleted when the
398
+ * transaction commits.
296399 */
297400 static int record_root_in_trans(struct btrfs_trans_handle *trans,
298401 struct btrfs_root *root,
....@@ -300,7 +403,7 @@
300403 {
301404 struct btrfs_fs_info *fs_info = root->fs_info;
302405
303
- if ((test_bit(BTRFS_ROOT_REF_COWS, &root->state) &&
406
+ if ((test_bit(BTRFS_ROOT_SHAREABLE, &root->state) &&
304407 root->last_trans < trans->transid) || force) {
305408 WARN_ON(root == fs_info->extent_root);
306409 WARN_ON(!force && root->commit_root != root->node);
....@@ -379,7 +482,7 @@
379482 {
380483 struct btrfs_fs_info *fs_info = root->fs_info;
381484
382
- if (!test_bit(BTRFS_ROOT_REF_COWS, &root->state))
485
+ if (!test_bit(BTRFS_ROOT_SHAREABLE, &root->state))
383486 return 0;
384487
385488 /*
....@@ -400,9 +503,9 @@
400503
401504 static inline int is_transaction_blocked(struct btrfs_transaction *trans)
402505 {
403
- return (trans->state >= TRANS_STATE_BLOCKED &&
506
+ return (trans->state >= TRANS_STATE_COMMIT_START &&
404507 trans->state < TRANS_STATE_UNBLOCKED &&
405
- !trans->aborted);
508
+ !TRANS_ABORTED(trans));
406509 }
407510
408511 /* wait for commit against the current transaction to become unblocked
....@@ -421,7 +524,7 @@
421524
422525 wait_event(fs_info->transaction_wait,
423526 cur_trans->state >= TRANS_STATE_UNBLOCKED ||
424
- cur_trans->aborted);
527
+ TRANS_ABORTED(cur_trans));
425528 btrfs_put_transaction(cur_trans);
426529 } else {
427530 spin_unlock(&fs_info->trans_lock);
....@@ -444,7 +547,7 @@
444547 struct btrfs_fs_info *fs_info = root->fs_info;
445548
446549 if (!fs_info->reloc_ctl ||
447
- !test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
550
+ !test_bit(BTRFS_ROOT_SHAREABLE, &root->state) ||
448551 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID ||
449552 root->reloc_root)
450553 return false;
....@@ -458,12 +561,13 @@
458561 bool enforce_qgroups)
459562 {
460563 struct btrfs_fs_info *fs_info = root->fs_info;
461
-
564
+ struct btrfs_block_rsv *delayed_refs_rsv = &fs_info->delayed_refs_rsv;
462565 struct btrfs_trans_handle *h;
463566 struct btrfs_transaction *cur_trans;
464567 u64 num_bytes = 0;
465568 u64 qgroup_reserved = 0;
466569 bool reloc_reserved = false;
570
+ bool do_chunk_alloc = false;
467571 int ret;
468572
469573 /* Send isn't supposed to start transactions. */
....@@ -487,13 +591,29 @@
487591 * the appropriate flushing if need be.
488592 */
489593 if (num_items && root != fs_info->chunk_root) {
594
+ struct btrfs_block_rsv *rsv = &fs_info->trans_block_rsv;
595
+ u64 delayed_refs_bytes = 0;
596
+
490597 qgroup_reserved = num_items * fs_info->nodesize;
491598 ret = btrfs_qgroup_reserve_meta_pertrans(root, qgroup_reserved,
492599 enforce_qgroups);
493600 if (ret)
494601 return ERR_PTR(ret);
495602
496
- num_bytes = btrfs_calc_trans_metadata_size(fs_info, num_items);
603
+ /*
604
+ * We want to reserve all the bytes we may need all at once, so
605
+ * we only do 1 enospc flushing cycle per transaction start. We
606
+ * accomplish this by simply assuming we'll do 2 x num_items
607
+ * worth of delayed refs updates in this trans handle, and
608
+ * refill that amount for whatever is missing in the reserve.
609
+ */
610
+ num_bytes = btrfs_calc_insert_metadata_size(fs_info, num_items);
611
+ if (flush == BTRFS_RESERVE_FLUSH_ALL &&
612
+ delayed_refs_rsv->full == 0) {
613
+ delayed_refs_bytes = num_bytes;
614
+ num_bytes <<= 1;
615
+ }
616
+
497617 /*
498618 * Do the reservation for the relocation root creation
499619 */
....@@ -502,8 +622,27 @@
502622 reloc_reserved = true;
503623 }
504624
505
- ret = btrfs_block_rsv_add(root, &fs_info->trans_block_rsv,
506
- num_bytes, flush);
625
+ ret = btrfs_block_rsv_add(root, rsv, num_bytes, flush);
626
+ if (ret)
627
+ goto reserve_fail;
628
+ if (delayed_refs_bytes) {
629
+ btrfs_migrate_to_delayed_refs_rsv(fs_info, rsv,
630
+ delayed_refs_bytes);
631
+ num_bytes -= delayed_refs_bytes;
632
+ }
633
+
634
+ if (rsv->space_info->force_alloc)
635
+ do_chunk_alloc = true;
636
+ } else if (num_items == 0 && flush == BTRFS_RESERVE_FLUSH_ALL &&
637
+ !delayed_refs_rsv->full) {
638
+ /*
639
+ * Some people call with btrfs_start_transaction(root, 0)
640
+ * because they can be throttled, but have some other mechanism
641
+ * for reserving space. We still want these guys to refill the
642
+ * delayed block_rsv so just add 1 items worth of reservation
643
+ * here.
644
+ */
645
+ ret = btrfs_delayed_refs_rsv_refill(fs_info, flush);
507646 if (ret)
508647 goto reserve_fail;
509648 }
....@@ -556,7 +695,7 @@
556695 INIT_LIST_HEAD(&h->new_bgs);
557696
558697 smp_mb();
559
- if (cur_trans->state >= TRANS_STATE_BLOCKED &&
698
+ if (cur_trans->state >= TRANS_STATE_COMMIT_START &&
560699 may_wait_transaction(fs_info, type)) {
561700 current->journal_info = h;
562701 btrfs_commit_transaction(h);
....@@ -574,6 +713,19 @@
574713 got_it:
575714 if (!current->journal_info)
576715 current->journal_info = h;
716
+
717
+ /*
718
+ * If the space_info is marked ALLOC_FORCE then we'll get upgraded to
719
+ * ALLOC_FORCE the first run through, and then we won't allocate for
720
+ * anybody else who races in later. We don't care about the return
721
+ * value here.
722
+ */
723
+ if (do_chunk_alloc && num_bytes) {
724
+ u64 flags = h->block_rsv->space_info->flags;
725
+
726
+ btrfs_chunk_alloc(h, btrfs_get_alloc_profile(fs_info, flags),
727
+ CHUNK_ALLOC_NO_FORCE);
728
+ }
577729
578730 /*
579731 * btrfs_record_root_in_trans() needs to alloc new extents, and may
....@@ -594,7 +746,7 @@
594746 alloc_fail:
595747 if (num_bytes)
596748 btrfs_block_rsv_release(fs_info, &fs_info->trans_block_rsv,
597
- num_bytes);
749
+ num_bytes, NULL);
598750 reserve_fail:
599751 btrfs_qgroup_free_meta_pertrans(root, qgroup_reserved);
600752 return ERR_PTR(ret);
....@@ -609,43 +761,10 @@
609761
610762 struct btrfs_trans_handle *btrfs_start_transaction_fallback_global_rsv(
611763 struct btrfs_root *root,
612
- unsigned int num_items,
613
- int min_factor)
764
+ unsigned int num_items)
614765 {
615
- struct btrfs_fs_info *fs_info = root->fs_info;
616
- struct btrfs_trans_handle *trans;
617
- u64 num_bytes;
618
- int ret;
619
-
620
- /*
621
- * We have two callers: unlink and block group removal. The
622
- * former should succeed even if we will temporarily exceed
623
- * quota and the latter operates on the extent root so
624
- * qgroup enforcement is ignored anyway.
625
- */
626
- trans = start_transaction(root, num_items, TRANS_START,
627
- BTRFS_RESERVE_FLUSH_ALL, false);
628
- if (!IS_ERR(trans) || PTR_ERR(trans) != -ENOSPC)
629
- return trans;
630
-
631
- trans = btrfs_start_transaction(root, 0);
632
- if (IS_ERR(trans))
633
- return trans;
634
-
635
- num_bytes = btrfs_calc_trans_metadata_size(fs_info, num_items);
636
- ret = btrfs_cond_migrate_bytes(fs_info, &fs_info->trans_block_rsv,
637
- num_bytes, min_factor);
638
- if (ret) {
639
- btrfs_end_transaction(trans);
640
- return ERR_PTR(ret);
641
- }
642
-
643
- trans->block_rsv = &fs_info->trans_block_rsv;
644
- trans->bytes_reserved = num_bytes;
645
- trace_btrfs_space_reservation(fs_info, "transaction",
646
- trans->transid, num_bytes, 1);
647
-
648
- return trans;
766
+ return start_transaction(root, num_items, TRANS_START,
767
+ BTRFS_RESERVE_FLUSH_ALL_STEAL, false);
649768 }
650769
651770 struct btrfs_trans_handle *btrfs_join_transaction(struct btrfs_root *root)
....@@ -654,7 +773,7 @@
654773 true);
655774 }
656775
657
-struct btrfs_trans_handle *btrfs_join_transaction_nolock(struct btrfs_root *root)
776
+struct btrfs_trans_handle *btrfs_join_transaction_spacecache(struct btrfs_root *root)
658777 {
659778 return start_transaction(root, 0, TRANS_JOIN_NOLOCK,
660779 BTRFS_RESERVE_NO_FLUSH, true);
....@@ -692,7 +811,7 @@
692811 /*
693812 * btrfs_attach_transaction_barrier() - catch the running transaction
694813 *
695
- * It is similar to the above function, the differentia is this one
814
+ * It is similar to the above function, the difference is this one
696815 * will wait for all the inactive transactions until they fully
697816 * complete.
698817 */
....@@ -703,8 +822,13 @@
703822
704823 trans = start_transaction(root, 0, TRANS_ATTACH,
705824 BTRFS_RESERVE_NO_FLUSH, true);
706
- if (trans == ERR_PTR(-ENOENT))
707
- btrfs_wait_for_commit(root->fs_info, 0);
825
+ if (trans == ERR_PTR(-ENOENT)) {
826
+ int ret;
827
+
828
+ ret = btrfs_wait_for_commit(root->fs_info, 0);
829
+ if (ret)
830
+ return ERR_PTR(ret);
831
+ }
708832
709833 return trans;
710834 }
....@@ -768,6 +892,7 @@
768892 }
769893
770894 wait_for_commit(cur_trans);
895
+ ret = cur_trans->aborted;
771896 btrfs_put_transaction(cur_trans);
772897 out:
773898 return ret;
....@@ -782,7 +907,7 @@
782907 {
783908 struct btrfs_fs_info *fs_info = trans->fs_info;
784909
785
- if (btrfs_check_space_for_delayed_refs(trans, fs_info))
910
+ if (btrfs_check_space_for_delayed_refs(fs_info))
786911 return 1;
787912
788913 return !!btrfs_block_rsv_check(&fs_info->global_block_rsv, 5);
....@@ -791,21 +916,11 @@
791916 int btrfs_should_end_transaction(struct btrfs_trans_handle *trans)
792917 {
793918 struct btrfs_transaction *cur_trans = trans->transaction;
794
- int updates;
795
- int err;
796919
797920 smp_mb();
798
- if (cur_trans->state >= TRANS_STATE_BLOCKED ||
921
+ if (cur_trans->state >= TRANS_STATE_COMMIT_START ||
799922 cur_trans->delayed_refs.flushing)
800923 return 1;
801
-
802
- updates = trans->delayed_ref_updates;
803
- trans->delayed_ref_updates = 0;
804
- if (updates) {
805
- err = btrfs_run_delayed_refs(trans, updates * 2);
806
- if (err) /* Error code will also eval true */
807
- return err;
808
- }
809924
810925 return should_end_transaction(trans);
811926 }
....@@ -827,7 +942,7 @@
827942 trace_btrfs_space_reservation(fs_info, "transaction",
828943 trans->transid, trans->bytes_reserved, 0);
829944 btrfs_block_rsv_release(fs_info, trans->block_rsv,
830
- trans->bytes_reserved);
945
+ trans->bytes_reserved, NULL);
831946 trans->bytes_reserved = 0;
832947 }
833948
....@@ -836,11 +951,7 @@
836951 {
837952 struct btrfs_fs_info *info = trans->fs_info;
838953 struct btrfs_transaction *cur_trans = trans->transaction;
839
- u64 transid = trans->transid;
840
- unsigned long cur = trans->delayed_ref_updates;
841
- int lock = (trans->type != TRANS_JOIN_NOLOCK);
842954 int err = 0;
843
- int must_run_delayed_refs = 0;
844955
845956 if (refcount_read(&trans->use_count) > 1) {
846957 refcount_dec(&trans->use_count);
....@@ -851,46 +962,9 @@
851962 btrfs_trans_release_metadata(trans);
852963 trans->block_rsv = NULL;
853964
854
- if (!list_empty(&trans->new_bgs))
855
- btrfs_create_pending_block_groups(trans);
856
-
857
- trans->delayed_ref_updates = 0;
858
- if (!trans->sync) {
859
- must_run_delayed_refs =
860
- btrfs_should_throttle_delayed_refs(trans, info);
861
- cur = max_t(unsigned long, cur, 32);
862
-
863
- /*
864
- * don't make the caller wait if they are from a NOLOCK
865
- * or ATTACH transaction, it will deadlock with commit
866
- */
867
- if (must_run_delayed_refs == 1 &&
868
- (trans->type & (__TRANS_JOIN_NOLOCK | __TRANS_ATTACH)))
869
- must_run_delayed_refs = 2;
870
- }
871
-
872
- btrfs_trans_release_metadata(trans);
873
- trans->block_rsv = NULL;
874
-
875
- if (!list_empty(&trans->new_bgs))
876
- btrfs_create_pending_block_groups(trans);
965
+ btrfs_create_pending_block_groups(trans);
877966
878967 btrfs_trans_release_chunk_metadata(trans);
879
-
880
- if (lock && should_end_transaction(trans) &&
881
- READ_ONCE(cur_trans->state) == TRANS_STATE_RUNNING) {
882
- spin_lock(&info->trans_lock);
883
- if (cur_trans->state == TRANS_STATE_RUNNING)
884
- cur_trans->state = TRANS_STATE_BLOCKED;
885
- spin_unlock(&info->trans_lock);
886
- }
887
-
888
- if (lock && READ_ONCE(cur_trans->state) == TRANS_STATE_BLOCKED) {
889
- if (throttle)
890
- return btrfs_commit_transaction(trans);
891
- else
892
- wake_up_process(info->transaction_kthread);
893
- }
894968
895969 if (trans->type & __TRANS_FREEZABLE)
896970 sb_end_intwrite(info->sb);
....@@ -909,17 +983,16 @@
909983 if (throttle)
910984 btrfs_run_delayed_iputs(info);
911985
912
- if (trans->aborted ||
986
+ if (TRANS_ABORTED(trans) ||
913987 test_bit(BTRFS_FS_STATE_ERROR, &info->fs_state)) {
914988 wake_up_process(info->transaction_kthread);
915
- err = -EIO;
989
+ if (TRANS_ABORTED(trans))
990
+ err = trans->aborted;
991
+ else
992
+ err = -EROFS;
916993 }
917994
918995 kmem_cache_free(btrfs_trans_handle_cachep, trans);
919
- if (must_run_delayed_refs) {
920
- btrfs_async_run_delayed_refs(info, cur, transid,
921
- must_run_delayed_refs == 1);
922
- }
923996 return err;
924997 }
925998
....@@ -967,7 +1040,7 @@
9671040 * superblock that points to btree nodes/leafs for which
9681041 * writeback hasn't finished yet (and without errors).
9691042 * We cleanup any entries left in the io tree when committing
970
- * the transaction (through clear_btree_io_tree()).
1043
+ * the transaction (through extent_io_tree_release()).
9711044 */
9721045 if (err == -ENOMEM) {
9731046 err = 0;
....@@ -1012,7 +1085,7 @@
10121085 * left in the io tree. For a log commit, we don't remove them
10131086 * after committing the log because the tree can be accessed
10141087 * concurrently - we do it only at transaction commit time when
1015
- * it's safe to do it (through clear_btree_io_tree()).
1088
+ * it's safe to do it (through extent_io_tree_release()).
10161089 */
10171090 err = clear_extent_bit(dirty_pages, start, end,
10181091 EXTENT_NEED_WAIT, 0, 0, &cached_state);
....@@ -1032,7 +1105,7 @@
10321105 return werr;
10331106 }
10341107
1035
-int btrfs_wait_extents(struct btrfs_fs_info *fs_info,
1108
+static int btrfs_wait_extents(struct btrfs_fs_info *fs_info,
10361109 struct extent_io_tree *dirty_pages)
10371110 {
10381111 bool errors = false;
....@@ -1090,7 +1163,7 @@
10901163 blk_finish_plug(&plug);
10911164 ret2 = btrfs_wait_extents(fs_info, dirty_pages);
10921165
1093
- clear_btree_io_tree(&trans->transaction->dirty_pages);
1166
+ extent_io_tree_release(&trans->transaction->dirty_pages);
10941167
10951168 if (ret)
10961169 return ret;
....@@ -1158,7 +1231,7 @@
11581231
11591232 eb = btrfs_lock_root_node(fs_info->tree_root);
11601233 ret = btrfs_cow_block(trans, fs_info->tree_root, eb, NULL,
1161
- 0, &eb);
1234
+ 0, &eb, BTRFS_NESTING_COW);
11621235 btrfs_tree_unlock(eb);
11631236 free_extent_buffer(eb);
11641237
....@@ -1169,17 +1242,17 @@
11691242 if (ret)
11701243 return ret;
11711244
1172
- ret = btrfs_run_dev_stats(trans, fs_info);
1245
+ ret = btrfs_run_dev_stats(trans);
11731246 if (ret)
11741247 return ret;
1175
- ret = btrfs_run_dev_replace(trans, fs_info);
1248
+ ret = btrfs_run_dev_replace(trans);
11761249 if (ret)
11771250 return ret;
11781251 ret = btrfs_run_qgroups(trans);
11791252 if (ret)
11801253 return ret;
11811254
1182
- ret = btrfs_setup_space_cache(trans, fs_info);
1255
+ ret = btrfs_setup_space_cache(trans);
11831256 if (ret)
11841257 return ret;
11851258
....@@ -1207,7 +1280,7 @@
12071280 }
12081281
12091282 while (!list_empty(dirty_bgs) || !list_empty(io_bgs)) {
1210
- ret = btrfs_write_dirty_block_groups(trans, fs_info);
1283
+ ret = btrfs_write_dirty_block_groups(trans);
12111284 if (ret)
12121285 return ret;
12131286 ret = btrfs_run_delayed_refs(trans, (unsigned long)-1);
....@@ -1220,7 +1293,10 @@
12201293
12211294 list_add_tail(&fs_info->extent_root->dirty_list,
12221295 &trans->transaction->switch_commits);
1223
- btrfs_after_dev_replace_commit(fs_info);
1296
+
1297
+ /* Update dev-replace pointer once everything is committed */
1298
+ fs_info->dev_replace.committed_cursor_left =
1299
+ fs_info->dev_replace.cursor_left_last_write_of_item;
12241300
12251301 return 0;
12261302 }
....@@ -1235,8 +1311,10 @@
12351311 struct btrfs_fs_info *fs_info = root->fs_info;
12361312
12371313 spin_lock(&fs_info->trans_lock);
1238
- if (list_empty(&root->root_list))
1314
+ if (list_empty(&root->root_list)) {
1315
+ btrfs_grab_root(root);
12391316 list_add_tail(&root->root_list, &fs_info->dead_roots);
1317
+ }
12401318 spin_unlock(&fs_info->trans_lock);
12411319 }
12421320
....@@ -1360,7 +1438,7 @@
13601438 return 0;
13611439
13621440 /*
1363
- * Ensure dirty @src will be commited. Or, after comming
1441
+ * Ensure dirty @src will be committed. Or, after coming
13641442 * commit_fs_roots() and switch_commit_roots(), any dirty but not
13651443 * recorded root will never be updated again, causing an outdated root
13661444 * item.
....@@ -1401,7 +1479,7 @@
14011479 ret = commit_cowonly_roots(trans);
14021480 if (ret)
14031481 goto out;
1404
- switch_commit_roots(trans->transaction);
1482
+ switch_commit_roots(trans);
14051483 ret = btrfs_write_and_wait_transaction(trans);
14061484 if (ret)
14071485 btrfs_handle_fs_error(fs_info, ret,
....@@ -1453,7 +1531,6 @@
14531531 u64 index = 0;
14541532 u64 objectid;
14551533 u64 root_flags;
1456
- uuid_le new_uuid;
14571534
14581535 ASSERT(pending->path);
14591536 path = pending->path;
....@@ -1546,8 +1623,7 @@
15461623
15471624 btrfs_set_root_generation_v2(new_root_item,
15481625 trans->transid);
1549
- uuid_le_gen(&new_uuid);
1550
- memcpy(new_root_item->uuid, new_uuid.b, BTRFS_UUID_SIZE);
1626
+ generate_random_guid(new_root_item->uuid);
15511627 memcpy(new_root_item->parent_uuid, root->root_item.uuid,
15521628 BTRFS_UUID_SIZE);
15531629 if (!(root_flags & BTRFS_ROOT_SUBVOL_RDONLY)) {
....@@ -1563,7 +1639,8 @@
15631639 btrfs_set_root_otransid(new_root_item, trans->transid);
15641640
15651641 old = btrfs_lock_root_node(root);
1566
- ret = btrfs_cow_block(trans, root, old, NULL, 0, &old);
1642
+ ret = btrfs_cow_block(trans, root, old, NULL, 0, &old,
1643
+ BTRFS_NESTING_COW);
15671644 if (ret) {
15681645 btrfs_tree_unlock(old);
15691646 free_extent_buffer(old);
....@@ -1571,7 +1648,7 @@
15711648 goto fail;
15721649 }
15731650
1574
- btrfs_set_lock_blocking(old);
1651
+ btrfs_set_lock_blocking_write(old);
15751652
15761653 ret = btrfs_copy_root(trans, root, old, &tmp, objectid);
15771654 /* clean up in any case */
....@@ -1609,9 +1686,10 @@
16091686 }
16101687
16111688 key.offset = (u64)-1;
1612
- pending->snap = btrfs_read_fs_root_no_name(fs_info, &key);
1689
+ pending->snap = btrfs_get_new_fs_root(fs_info, objectid, pending->anon_dev);
16131690 if (IS_ERR(pending->snap)) {
16141691 ret = PTR_ERR(pending->snap);
1692
+ pending->snap = NULL;
16151693 btrfs_abort_transaction(trans, ret);
16161694 goto fail;
16171695 }
....@@ -1639,10 +1717,9 @@
16391717 if (ret < 0)
16401718 goto fail;
16411719
1642
- ret = btrfs_insert_dir_item(trans, parent_root,
1643
- dentry->d_name.name, dentry->d_name.len,
1644
- BTRFS_I(parent_inode), &key,
1645
- BTRFS_FT_DIR, index);
1720
+ ret = btrfs_insert_dir_item(trans, dentry->d_name.name,
1721
+ dentry->d_name.len, BTRFS_I(parent_inode),
1722
+ &key, BTRFS_FT_DIR, index);
16461723 /* We have check then name at the beginning, so it is impossible. */
16471724 BUG_ON(ret == -EEXIST || ret == -EOVERFLOW);
16481725 if (ret) {
....@@ -1659,7 +1736,8 @@
16591736 btrfs_abort_transaction(trans, ret);
16601737 goto fail;
16611738 }
1662
- ret = btrfs_uuid_tree_add(trans, new_uuid.b, BTRFS_UUID_KEY_SUBVOL,
1739
+ ret = btrfs_uuid_tree_add(trans, new_root_item->uuid,
1740
+ BTRFS_UUID_KEY_SUBVOL,
16631741 objectid);
16641742 if (ret) {
16651743 btrfs_abort_transaction(trans, ret);
....@@ -1771,7 +1849,8 @@
17711849 struct btrfs_transaction *trans)
17721850 {
17731851 wait_event(fs_info->transaction_blocked_wait,
1774
- trans->state >= TRANS_STATE_COMMIT_START || trans->aborted);
1852
+ trans->state >= TRANS_STATE_COMMIT_START ||
1853
+ TRANS_ABORTED(trans));
17751854 }
17761855
17771856 /*
....@@ -1783,7 +1862,8 @@
17831862 struct btrfs_transaction *trans)
17841863 {
17851864 wait_event(fs_info->transaction_wait,
1786
- trans->state >= TRANS_STATE_UNBLOCKED || trans->aborted);
1865
+ trans->state >= TRANS_STATE_UNBLOCKED ||
1866
+ TRANS_ABORTED(trans));
17871867 }
17881868
17891869 /*
....@@ -1865,7 +1945,6 @@
18651945 {
18661946 struct btrfs_fs_info *fs_info = trans->fs_info;
18671947 struct btrfs_transaction *cur_trans = trans->transaction;
1868
- DEFINE_WAIT(wait);
18691948
18701949 WARN_ON(refcount_read(&trans->use_count) > 1);
18711950
....@@ -1880,7 +1959,6 @@
18801959 */
18811960 BUG_ON(list_empty(&cur_trans->list));
18821961
1883
- list_del_init(&cur_trans->list);
18841962 if (cur_trans == fs_info->running_transaction) {
18851963 cur_trans->state = TRANS_STATE_COMMIT_DOING;
18861964 spin_unlock(&fs_info->trans_lock);
....@@ -1889,6 +1967,17 @@
18891967
18901968 spin_lock(&fs_info->trans_lock);
18911969 }
1970
+
1971
+ /*
1972
+ * Now that we know no one else is still using the transaction we can
1973
+ * remove the transaction from the list of transactions. This avoids
1974
+ * the transaction kthread from cleaning up the transaction while some
1975
+ * other task is still using it, which could result in a use-after-free
1976
+ * on things like log trees, as it forces the transaction kthread to
1977
+ * wait for this transaction to be cleaned up by us.
1978
+ */
1979
+ list_del_init(&cur_trans->list);
1980
+
18921981 spin_unlock(&fs_info->trans_lock);
18931982
18941983 btrfs_cleanup_one_transaction(trans->transaction, fs_info);
....@@ -1912,8 +2001,25 @@
19122001 kmem_cache_free(btrfs_trans_handle_cachep, trans);
19132002 }
19142003
1915
-static inline int btrfs_start_delalloc_flush(struct btrfs_fs_info *fs_info)
2004
+/*
2005
+ * Release reserved delayed ref space of all pending block groups of the
2006
+ * transaction and remove them from the list
2007
+ */
2008
+static void btrfs_cleanup_pending_block_groups(struct btrfs_trans_handle *trans)
19162009 {
2010
+ struct btrfs_fs_info *fs_info = trans->fs_info;
2011
+ struct btrfs_block_group *block_group, *tmp;
2012
+
2013
+ list_for_each_entry_safe(block_group, tmp, &trans->new_bgs, bg_list) {
2014
+ btrfs_delayed_refs_rsv_release(fs_info, 1);
2015
+ list_del_init(&block_group->bg_list);
2016
+ }
2017
+}
2018
+
2019
+static inline int btrfs_start_delalloc_flush(struct btrfs_trans_handle *trans)
2020
+{
2021
+ struct btrfs_fs_info *fs_info = trans->fs_info;
2022
+
19172023 /*
19182024 * We use writeback_inodes_sb here because if we used
19192025 * btrfs_start_delalloc_roots we would deadlock with fs freeze.
....@@ -1923,22 +2029,50 @@
19232029 * from already being in a transaction and our join_transaction doesn't
19242030 * have to re-take the fs freeze lock.
19252031 */
1926
- if (btrfs_test_opt(fs_info, FLUSHONCOMMIT))
2032
+ if (btrfs_test_opt(fs_info, FLUSHONCOMMIT)) {
19272033 writeback_inodes_sb(fs_info->sb, WB_REASON_SYNC);
2034
+ } else {
2035
+ struct btrfs_pending_snapshot *pending;
2036
+ struct list_head *head = &trans->transaction->pending_snapshots;
2037
+
2038
+ /*
2039
+ * Flush dellaloc for any root that is going to be snapshotted.
2040
+ * This is done to avoid a corrupted version of files, in the
2041
+ * snapshots, that had both buffered and direct IO writes (even
2042
+ * if they were done sequentially) due to an unordered update of
2043
+ * the inode's size on disk.
2044
+ */
2045
+ list_for_each_entry(pending, head, list) {
2046
+ int ret;
2047
+
2048
+ ret = btrfs_start_delalloc_snapshot(pending->root);
2049
+ if (ret)
2050
+ return ret;
2051
+ }
2052
+ }
19282053 return 0;
19292054 }
19302055
1931
-static inline void btrfs_wait_delalloc_flush(struct btrfs_fs_info *fs_info)
2056
+static inline void btrfs_wait_delalloc_flush(struct btrfs_trans_handle *trans)
19322057 {
1933
- if (btrfs_test_opt(fs_info, FLUSHONCOMMIT))
1934
- btrfs_wait_ordered_roots(fs_info, U64_MAX, 0, (u64)-1);
1935
-}
2058
+ struct btrfs_fs_info *fs_info = trans->fs_info;
19362059
1937
-static inline void
1938
-btrfs_wait_pending_ordered(struct btrfs_transaction *cur_trans)
1939
-{
1940
- wait_event(cur_trans->pending_wait,
1941
- atomic_read(&cur_trans->pending_ordered) == 0);
2060
+ if (btrfs_test_opt(fs_info, FLUSHONCOMMIT)) {
2061
+ btrfs_wait_ordered_roots(fs_info, U64_MAX, 0, (u64)-1);
2062
+ } else {
2063
+ struct btrfs_pending_snapshot *pending;
2064
+ struct list_head *head = &trans->transaction->pending_snapshots;
2065
+
2066
+ /*
2067
+ * Wait for any dellaloc that we started previously for the roots
2068
+ * that are going to be snapshotted. This is to avoid a corrupted
2069
+ * version of files in the snapshots that had both buffered and
2070
+ * direct IO writes (even if they were done sequentially).
2071
+ */
2072
+ list_for_each_entry(pending, head, list)
2073
+ btrfs_wait_ordered_extents(pending->root,
2074
+ U64_MAX, 0, U64_MAX);
2075
+ }
19422076 }
19432077
19442078 int btrfs_commit_transaction(struct btrfs_trans_handle *trans)
....@@ -1947,6 +2081,8 @@
19472081 struct btrfs_transaction *cur_trans = trans->transaction;
19482082 struct btrfs_transaction *prev_trans = NULL;
19492083 int ret;
2084
+
2085
+ ASSERT(refcount_read(&trans->use_count) == 1);
19502086
19512087 /*
19522088 * Some places just start a transaction to commit it. We need to make
....@@ -1957,7 +2093,7 @@
19572093 trans->dirty = true;
19582094
19592095 /* Stop the commit early if ->aborted is set */
1960
- if (unlikely(READ_ONCE(cur_trans->aborted))) {
2096
+ if (TRANS_ABORTED(cur_trans)) {
19612097 ret = cur_trans->aborted;
19622098 btrfs_end_transaction(trans);
19632099 return ret;
....@@ -1984,8 +2120,7 @@
19842120 cur_trans->delayed_refs.flushing = 1;
19852121 smp_wmb();
19862122
1987
- if (!list_empty(&trans->new_bgs))
1988
- btrfs_create_pending_block_groups(trans);
2123
+ btrfs_create_pending_block_groups(trans);
19892124
19902125 ret = btrfs_run_delayed_refs(trans, 0);
19912126 if (ret) {
....@@ -2032,7 +2167,7 @@
20322167
20332168 wait_for_commit(cur_trans);
20342169
2035
- if (unlikely(cur_trans->aborted))
2170
+ if (TRANS_ABORTED(cur_trans))
20362171 ret = cur_trans->aborted;
20372172
20382173 btrfs_put_transaction(cur_trans);
....@@ -2051,7 +2186,7 @@
20512186 spin_unlock(&fs_info->trans_lock);
20522187
20532188 wait_for_commit(prev_trans);
2054
- ret = prev_trans->aborted;
2189
+ ret = READ_ONCE(prev_trans->aborted);
20552190
20562191 btrfs_put_transaction(prev_trans);
20572192 if (ret)
....@@ -2075,7 +2210,7 @@
20752210
20762211 extwriter_counter_dec(cur_trans, trans->type);
20772212
2078
- ret = btrfs_start_delalloc_flush(fs_info);
2213
+ ret = btrfs_start_delalloc_flush(trans);
20792214 if (ret)
20802215 goto cleanup_transaction;
20812216
....@@ -2091,9 +2226,15 @@
20912226 if (ret)
20922227 goto cleanup_transaction;
20932228
2094
- btrfs_wait_delalloc_flush(fs_info);
2229
+ btrfs_wait_delalloc_flush(trans);
20952230
2096
- btrfs_wait_pending_ordered(cur_trans);
2231
+ /*
2232
+ * Wait for all ordered extents started by a fast fsync that joined this
2233
+ * transaction. Otherwise if this transaction commits before the ordered
2234
+ * extents complete we lose logged data after a power failure.
2235
+ */
2236
+ wait_event(cur_trans->pending_wait,
2237
+ atomic_read(&cur_trans->pending_ordered) == 0);
20972238
20982239 btrfs_scrub_pause(fs_info);
20992240 /*
....@@ -2107,8 +2248,7 @@
21072248 wait_event(cur_trans->writer_wait,
21082249 atomic_read(&cur_trans->num_writers) == 1);
21092250
2110
- /* ->aborted might be set after the previous check, so check it */
2111
- if (unlikely(READ_ONCE(cur_trans->aborted))) {
2251
+ if (TRANS_ABORTED(cur_trans)) {
21122252 ret = cur_trans->aborted;
21132253 goto scrub_continue;
21142254 }
....@@ -2125,10 +2265,8 @@
21252265 * core function of the snapshot creation.
21262266 */
21272267 ret = create_pending_snapshots(trans);
2128
- if (ret) {
2129
- mutex_unlock(&fs_info->reloc_mutex);
2130
- goto scrub_continue;
2131
- }
2268
+ if (ret)
2269
+ goto unlock_reloc;
21322270
21332271 /*
21342272 * We insert the dir indexes of the snapshots and update the inode
....@@ -2141,16 +2279,12 @@
21412279 * the nodes and leaves.
21422280 */
21432281 ret = btrfs_run_delayed_items(trans);
2144
- if (ret) {
2145
- mutex_unlock(&fs_info->reloc_mutex);
2146
- goto scrub_continue;
2147
- }
2282
+ if (ret)
2283
+ goto unlock_reloc;
21482284
21492285 ret = btrfs_run_delayed_refs(trans, (unsigned long)-1);
2150
- if (ret) {
2151
- mutex_unlock(&fs_info->reloc_mutex);
2152
- goto scrub_continue;
2153
- }
2286
+ if (ret)
2287
+ goto unlock_reloc;
21542288
21552289 /*
21562290 * make sure none of the code above managed to slip in a
....@@ -2176,11 +2310,8 @@
21762310 mutex_lock(&fs_info->tree_log_mutex);
21772311
21782312 ret = commit_fs_roots(trans);
2179
- if (ret) {
2180
- mutex_unlock(&fs_info->tree_log_mutex);
2181
- mutex_unlock(&fs_info->reloc_mutex);
2182
- goto scrub_continue;
2183
- }
2313
+ if (ret)
2314
+ goto unlock_tree_log;
21842315
21852316 /*
21862317 * Since the transaction is done, we can apply the pending changes
....@@ -2198,42 +2329,29 @@
21982329 * new delayed refs. Must handle them or qgroup can be wrong.
21992330 */
22002331 ret = btrfs_run_delayed_refs(trans, (unsigned long)-1);
2201
- if (ret) {
2202
- mutex_unlock(&fs_info->tree_log_mutex);
2203
- mutex_unlock(&fs_info->reloc_mutex);
2204
- goto scrub_continue;
2205
- }
2332
+ if (ret)
2333
+ goto unlock_tree_log;
22062334
22072335 /*
22082336 * Since fs roots are all committed, we can get a quite accurate
22092337 * new_roots. So let's do quota accounting.
22102338 */
22112339 ret = btrfs_qgroup_account_extents(trans);
2212
- if (ret < 0) {
2213
- mutex_unlock(&fs_info->tree_log_mutex);
2214
- mutex_unlock(&fs_info->reloc_mutex);
2215
- goto scrub_continue;
2216
- }
2340
+ if (ret < 0)
2341
+ goto unlock_tree_log;
22172342
22182343 ret = commit_cowonly_roots(trans);
2219
- if (ret) {
2220
- mutex_unlock(&fs_info->tree_log_mutex);
2221
- mutex_unlock(&fs_info->reloc_mutex);
2222
- goto scrub_continue;
2223
- }
2344
+ if (ret)
2345
+ goto unlock_tree_log;
22242346
22252347 /*
22262348 * The tasks which save the space cache and inode cache may also
22272349 * update ->aborted, check it.
22282350 */
2229
- if (unlikely(READ_ONCE(cur_trans->aborted))) {
2351
+ if (TRANS_ABORTED(cur_trans)) {
22302352 ret = cur_trans->aborted;
2231
- mutex_unlock(&fs_info->tree_log_mutex);
2232
- mutex_unlock(&fs_info->reloc_mutex);
2233
- goto scrub_continue;
2353
+ goto unlock_tree_log;
22342354 }
2235
-
2236
- btrfs_prepare_extent_commit(fs_info);
22372355
22382356 cur_trans = fs_info->running_transaction;
22392357
....@@ -2247,7 +2365,7 @@
22472365 list_add_tail(&fs_info->chunk_root->dirty_list,
22482366 &cur_trans->switch_commits);
22492367
2250
- switch_commit_roots(cur_trans);
2368
+ switch_commit_roots(trans);
22512369
22522370 ASSERT(list_empty(&cur_trans->dirty_bgs));
22532371 ASSERT(list_empty(&cur_trans->io_bgs));
....@@ -2258,8 +2376,7 @@
22582376 memcpy(fs_info->super_for_commit, fs_info->super_copy,
22592377 sizeof(*fs_info->super_copy));
22602378
2261
- btrfs_update_commit_device_size(fs_info);
2262
- btrfs_update_commit_device_bytes_used(cur_trans);
2379
+ btrfs_commit_device_sizes(cur_trans);
22632380
22642381 clear_bit(BTRFS_FS_LOG1_ERR, &fs_info->flags);
22652382 clear_bit(BTRFS_FS_LOG2_ERR, &fs_info->flags);
....@@ -2278,6 +2395,10 @@
22782395 if (ret) {
22792396 btrfs_handle_fs_error(fs_info, ret,
22802397 "Error while writing out transaction");
2398
+ /*
2399
+ * reloc_mutex has been unlocked, tree_log_mutex is still held
2400
+ * but we can't jump to unlock_tree_log causing double unlock
2401
+ */
22812402 mutex_unlock(&fs_info->tree_log_mutex);
22822403 goto scrub_continue;
22832404 }
....@@ -2303,7 +2424,6 @@
23032424 */
23042425 cur_trans->state = TRANS_STATE_COMPLETED;
23052426 wake_up(&cur_trans->commit_wait);
2306
- clear_bit(BTRFS_FS_NEED_ASYNC_COMMIT, &fs_info->flags);
23072427
23082428 spin_lock(&fs_info->trans_lock);
23092429 list_del_init(&cur_trans->list);
....@@ -2326,10 +2446,15 @@
23262446
23272447 return ret;
23282448
2449
+unlock_tree_log:
2450
+ mutex_unlock(&fs_info->tree_log_mutex);
2451
+unlock_reloc:
2452
+ mutex_unlock(&fs_info->reloc_mutex);
23292453 scrub_continue:
23302454 btrfs_scrub_continue(fs_info);
23312455 cleanup_transaction:
23322456 btrfs_trans_release_metadata(trans);
2457
+ btrfs_cleanup_pending_block_groups(trans);
23332458 btrfs_trans_release_chunk_metadata(trans);
23342459 trans->block_rsv = NULL;
23352460 btrfs_warn(fs_info, "Skipping commit of aborted transaction.");
....@@ -2365,16 +2490,21 @@
23652490 list_del_init(&root->root_list);
23662491 spin_unlock(&fs_info->trans_lock);
23672492
2368
- btrfs_debug(fs_info, "cleaner removing %llu", root->objectid);
2493
+ btrfs_debug(fs_info, "cleaner removing %llu", root->root_key.objectid);
23692494
23702495 btrfs_kill_all_delayed_nodes(root);
2496
+ if (root->ino_cache_inode) {
2497
+ iput(root->ino_cache_inode);
2498
+ root->ino_cache_inode = NULL;
2499
+ }
23712500
23722501 if (btrfs_header_backref_rev(root->node) <
23732502 BTRFS_MIXED_BACKREF_REV)
2374
- ret = btrfs_drop_snapshot(root, NULL, 0, 0);
2503
+ ret = btrfs_drop_snapshot(root, 0, 0);
23752504 else
2376
- ret = btrfs_drop_snapshot(root, NULL, 1, 0);
2505
+ ret = btrfs_drop_snapshot(root, 1, 0);
23772506
2507
+ btrfs_put_root(root);
23782508 return (ret < 0) ? 0 : 1;
23792509 }
23802510