forked from ~ljy/RK356X_SDK_RELEASE

hc
2023-12-11 072de836f53be56a70cecf70b43ae43b7ce17376
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 }
....@@ -231,24 +332,24 @@
231332 }
232333
233334 cur_trans->fs_info = fs_info;
335
+ atomic_set(&cur_trans->pending_ordered, 0);
336
+ init_waitqueue_head(&cur_trans->pending_wait);
234337 atomic_set(&cur_trans->num_writers, 1);
235338 extwriter_counter_init(cur_trans, type);
236339 init_waitqueue_head(&cur_trans->writer_wait);
237340 init_waitqueue_head(&cur_trans->commit_wait);
238
- init_waitqueue_head(&cur_trans->pending_wait);
239341 cur_trans->state = TRANS_STATE_RUNNING;
240342 /*
241343 * One for this trans handle, one so it will live on until we
242344 * commit the transaction.
243345 */
244346 refcount_set(&cur_trans->use_count, 2);
245
- atomic_set(&cur_trans->pending_ordered, 0);
246347 cur_trans->flags = 0;
247348 cur_trans->start_time = ktime_get_seconds();
248349
249350 memset(&cur_trans->delayed_refs, 0, sizeof(cur_trans->delayed_refs));
250351
251
- cur_trans->delayed_refs.href_root = RB_ROOT;
352
+ cur_trans->delayed_refs.href_root = RB_ROOT_CACHED;
252353 cur_trans->delayed_refs.dirty_extent_root = RB_ROOT;
253354 atomic_set(&cur_trans->delayed_refs.num_entries, 0);
254355
....@@ -266,19 +367,20 @@
266367 spin_lock_init(&cur_trans->delayed_refs.lock);
267368
268369 INIT_LIST_HEAD(&cur_trans->pending_snapshots);
269
- INIT_LIST_HEAD(&cur_trans->pending_chunks);
370
+ INIT_LIST_HEAD(&cur_trans->dev_update_list);
270371 INIT_LIST_HEAD(&cur_trans->switch_commits);
271372 INIT_LIST_HEAD(&cur_trans->dirty_bgs);
272373 INIT_LIST_HEAD(&cur_trans->io_bgs);
273374 INIT_LIST_HEAD(&cur_trans->dropped_roots);
274375 mutex_init(&cur_trans->cache_write_mutex);
275
- cur_trans->num_dirty_bgs = 0;
276376 spin_lock_init(&cur_trans->dirty_bgs_lock);
277377 INIT_LIST_HEAD(&cur_trans->deleted_bgs);
278378 spin_lock_init(&cur_trans->dropped_roots_lock);
279379 list_add_tail(&cur_trans->list, &fs_info->trans_list);
280
- extent_io_tree_init(&cur_trans->dirty_pages,
281
- fs_info->btree_inode);
380
+ extent_io_tree_init(fs_info, &cur_trans->dirty_pages,
381
+ IO_TREE_TRANS_DIRTY_PAGES, fs_info->btree_inode);
382
+ extent_io_tree_init(fs_info, &cur_trans->pinned_extents,
383
+ IO_TREE_FS_PINNED_EXTENTS, NULL);
282384 fs_info->generation++;
283385 cur_trans->transid = fs_info->generation;
284386 fs_info->running_transaction = cur_trans;
....@@ -289,10 +391,10 @@
289391 }
290392
291393 /*
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
394
+ * This does all the record keeping required to make sure that a shareable root
395
+ * is properly recorded in a given transaction. This is required to make sure
396
+ * the old root from before we joined the transaction is deleted when the
397
+ * transaction commits.
296398 */
297399 static int record_root_in_trans(struct btrfs_trans_handle *trans,
298400 struct btrfs_root *root,
....@@ -300,7 +402,7 @@
300402 {
301403 struct btrfs_fs_info *fs_info = root->fs_info;
302404
303
- if ((test_bit(BTRFS_ROOT_REF_COWS, &root->state) &&
405
+ if ((test_bit(BTRFS_ROOT_SHAREABLE, &root->state) &&
304406 root->last_trans < trans->transid) || force) {
305407 WARN_ON(root == fs_info->extent_root);
306408 WARN_ON(!force && root->commit_root != root->node);
....@@ -379,7 +481,7 @@
379481 {
380482 struct btrfs_fs_info *fs_info = root->fs_info;
381483
382
- if (!test_bit(BTRFS_ROOT_REF_COWS, &root->state))
484
+ if (!test_bit(BTRFS_ROOT_SHAREABLE, &root->state))
383485 return 0;
384486
385487 /*
....@@ -400,9 +502,9 @@
400502
401503 static inline int is_transaction_blocked(struct btrfs_transaction *trans)
402504 {
403
- return (trans->state >= TRANS_STATE_BLOCKED &&
505
+ return (trans->state >= TRANS_STATE_COMMIT_START &&
404506 trans->state < TRANS_STATE_UNBLOCKED &&
405
- !trans->aborted);
507
+ !TRANS_ABORTED(trans));
406508 }
407509
408510 /* wait for commit against the current transaction to become unblocked
....@@ -421,7 +523,7 @@
421523
422524 wait_event(fs_info->transaction_wait,
423525 cur_trans->state >= TRANS_STATE_UNBLOCKED ||
424
- cur_trans->aborted);
526
+ TRANS_ABORTED(cur_trans));
425527 btrfs_put_transaction(cur_trans);
426528 } else {
427529 spin_unlock(&fs_info->trans_lock);
....@@ -444,7 +546,7 @@
444546 struct btrfs_fs_info *fs_info = root->fs_info;
445547
446548 if (!fs_info->reloc_ctl ||
447
- !test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
549
+ !test_bit(BTRFS_ROOT_SHAREABLE, &root->state) ||
448550 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID ||
449551 root->reloc_root)
450552 return false;
....@@ -458,12 +560,13 @@
458560 bool enforce_qgroups)
459561 {
460562 struct btrfs_fs_info *fs_info = root->fs_info;
461
-
563
+ struct btrfs_block_rsv *delayed_refs_rsv = &fs_info->delayed_refs_rsv;
462564 struct btrfs_trans_handle *h;
463565 struct btrfs_transaction *cur_trans;
464566 u64 num_bytes = 0;
465567 u64 qgroup_reserved = 0;
466568 bool reloc_reserved = false;
569
+ bool do_chunk_alloc = false;
467570 int ret;
468571
469572 /* Send isn't supposed to start transactions. */
....@@ -487,13 +590,29 @@
487590 * the appropriate flushing if need be.
488591 */
489592 if (num_items && root != fs_info->chunk_root) {
593
+ struct btrfs_block_rsv *rsv = &fs_info->trans_block_rsv;
594
+ u64 delayed_refs_bytes = 0;
595
+
490596 qgroup_reserved = num_items * fs_info->nodesize;
491597 ret = btrfs_qgroup_reserve_meta_pertrans(root, qgroup_reserved,
492598 enforce_qgroups);
493599 if (ret)
494600 return ERR_PTR(ret);
495601
496
- num_bytes = btrfs_calc_trans_metadata_size(fs_info, num_items);
602
+ /*
603
+ * We want to reserve all the bytes we may need all at once, so
604
+ * we only do 1 enospc flushing cycle per transaction start. We
605
+ * accomplish this by simply assuming we'll do 2 x num_items
606
+ * worth of delayed refs updates in this trans handle, and
607
+ * refill that amount for whatever is missing in the reserve.
608
+ */
609
+ num_bytes = btrfs_calc_insert_metadata_size(fs_info, num_items);
610
+ if (flush == BTRFS_RESERVE_FLUSH_ALL &&
611
+ delayed_refs_rsv->full == 0) {
612
+ delayed_refs_bytes = num_bytes;
613
+ num_bytes <<= 1;
614
+ }
615
+
497616 /*
498617 * Do the reservation for the relocation root creation
499618 */
....@@ -502,8 +621,27 @@
502621 reloc_reserved = true;
503622 }
504623
505
- ret = btrfs_block_rsv_add(root, &fs_info->trans_block_rsv,
506
- num_bytes, flush);
624
+ ret = btrfs_block_rsv_add(root, rsv, num_bytes, flush);
625
+ if (ret)
626
+ goto reserve_fail;
627
+ if (delayed_refs_bytes) {
628
+ btrfs_migrate_to_delayed_refs_rsv(fs_info, rsv,
629
+ delayed_refs_bytes);
630
+ num_bytes -= delayed_refs_bytes;
631
+ }
632
+
633
+ if (rsv->space_info->force_alloc)
634
+ do_chunk_alloc = true;
635
+ } else if (num_items == 0 && flush == BTRFS_RESERVE_FLUSH_ALL &&
636
+ !delayed_refs_rsv->full) {
637
+ /*
638
+ * Some people call with btrfs_start_transaction(root, 0)
639
+ * because they can be throttled, but have some other mechanism
640
+ * for reserving space. We still want these guys to refill the
641
+ * delayed block_rsv so just add 1 items worth of reservation
642
+ * here.
643
+ */
644
+ ret = btrfs_delayed_refs_rsv_refill(fs_info, flush);
507645 if (ret)
508646 goto reserve_fail;
509647 }
....@@ -556,7 +694,7 @@
556694 INIT_LIST_HEAD(&h->new_bgs);
557695
558696 smp_mb();
559
- if (cur_trans->state >= TRANS_STATE_BLOCKED &&
697
+ if (cur_trans->state >= TRANS_STATE_COMMIT_START &&
560698 may_wait_transaction(fs_info, type)) {
561699 current->journal_info = h;
562700 btrfs_commit_transaction(h);
....@@ -574,6 +712,19 @@
574712 got_it:
575713 if (!current->journal_info)
576714 current->journal_info = h;
715
+
716
+ /*
717
+ * If the space_info is marked ALLOC_FORCE then we'll get upgraded to
718
+ * ALLOC_FORCE the first run through, and then we won't allocate for
719
+ * anybody else who races in later. We don't care about the return
720
+ * value here.
721
+ */
722
+ if (do_chunk_alloc && num_bytes) {
723
+ u64 flags = h->block_rsv->space_info->flags;
724
+
725
+ btrfs_chunk_alloc(h, btrfs_get_alloc_profile(fs_info, flags),
726
+ CHUNK_ALLOC_NO_FORCE);
727
+ }
577728
578729 /*
579730 * btrfs_record_root_in_trans() needs to alloc new extents, and may
....@@ -594,7 +745,7 @@
594745 alloc_fail:
595746 if (num_bytes)
596747 btrfs_block_rsv_release(fs_info, &fs_info->trans_block_rsv,
597
- num_bytes);
748
+ num_bytes, NULL);
598749 reserve_fail:
599750 btrfs_qgroup_free_meta_pertrans(root, qgroup_reserved);
600751 return ERR_PTR(ret);
....@@ -609,43 +760,10 @@
609760
610761 struct btrfs_trans_handle *btrfs_start_transaction_fallback_global_rsv(
611762 struct btrfs_root *root,
612
- unsigned int num_items,
613
- int min_factor)
763
+ unsigned int num_items)
614764 {
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;
765
+ return start_transaction(root, num_items, TRANS_START,
766
+ BTRFS_RESERVE_FLUSH_ALL_STEAL, false);
649767 }
650768
651769 struct btrfs_trans_handle *btrfs_join_transaction(struct btrfs_root *root)
....@@ -654,7 +772,7 @@
654772 true);
655773 }
656774
657
-struct btrfs_trans_handle *btrfs_join_transaction_nolock(struct btrfs_root *root)
775
+struct btrfs_trans_handle *btrfs_join_transaction_spacecache(struct btrfs_root *root)
658776 {
659777 return start_transaction(root, 0, TRANS_JOIN_NOLOCK,
660778 BTRFS_RESERVE_NO_FLUSH, true);
....@@ -692,7 +810,7 @@
692810 /*
693811 * btrfs_attach_transaction_barrier() - catch the running transaction
694812 *
695
- * It is similar to the above function, the differentia is this one
813
+ * It is similar to the above function, the difference is this one
696814 * will wait for all the inactive transactions until they fully
697815 * complete.
698816 */
....@@ -782,7 +900,7 @@
782900 {
783901 struct btrfs_fs_info *fs_info = trans->fs_info;
784902
785
- if (btrfs_check_space_for_delayed_refs(trans, fs_info))
903
+ if (btrfs_check_space_for_delayed_refs(fs_info))
786904 return 1;
787905
788906 return !!btrfs_block_rsv_check(&fs_info->global_block_rsv, 5);
....@@ -791,21 +909,11 @@
791909 int btrfs_should_end_transaction(struct btrfs_trans_handle *trans)
792910 {
793911 struct btrfs_transaction *cur_trans = trans->transaction;
794
- int updates;
795
- int err;
796912
797913 smp_mb();
798
- if (cur_trans->state >= TRANS_STATE_BLOCKED ||
914
+ if (cur_trans->state >= TRANS_STATE_COMMIT_START ||
799915 cur_trans->delayed_refs.flushing)
800916 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
- }
809917
810918 return should_end_transaction(trans);
811919 }
....@@ -827,7 +935,7 @@
827935 trace_btrfs_space_reservation(fs_info, "transaction",
828936 trans->transid, trans->bytes_reserved, 0);
829937 btrfs_block_rsv_release(fs_info, trans->block_rsv,
830
- trans->bytes_reserved);
938
+ trans->bytes_reserved, NULL);
831939 trans->bytes_reserved = 0;
832940 }
833941
....@@ -836,11 +944,7 @@
836944 {
837945 struct btrfs_fs_info *info = trans->fs_info;
838946 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);
842947 int err = 0;
843
- int must_run_delayed_refs = 0;
844948
845949 if (refcount_read(&trans->use_count) > 1) {
846950 refcount_dec(&trans->use_count);
....@@ -851,46 +955,9 @@
851955 btrfs_trans_release_metadata(trans);
852956 trans->block_rsv = NULL;
853957
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);
958
+ btrfs_create_pending_block_groups(trans);
877959
878960 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
- }
894961
895962 if (trans->type & __TRANS_FREEZABLE)
896963 sb_end_intwrite(info->sb);
....@@ -909,17 +976,16 @@
909976 if (throttle)
910977 btrfs_run_delayed_iputs(info);
911978
912
- if (trans->aborted ||
979
+ if (TRANS_ABORTED(trans) ||
913980 test_bit(BTRFS_FS_STATE_ERROR, &info->fs_state)) {
914981 wake_up_process(info->transaction_kthread);
915
- err = -EIO;
982
+ if (TRANS_ABORTED(trans))
983
+ err = trans->aborted;
984
+ else
985
+ err = -EROFS;
916986 }
917987
918988 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
- }
923989 return err;
924990 }
925991
....@@ -967,7 +1033,7 @@
9671033 * superblock that points to btree nodes/leafs for which
9681034 * writeback hasn't finished yet (and without errors).
9691035 * We cleanup any entries left in the io tree when committing
970
- * the transaction (through clear_btree_io_tree()).
1036
+ * the transaction (through extent_io_tree_release()).
9711037 */
9721038 if (err == -ENOMEM) {
9731039 err = 0;
....@@ -1012,7 +1078,7 @@
10121078 * left in the io tree. For a log commit, we don't remove them
10131079 * after committing the log because the tree can be accessed
10141080 * concurrently - we do it only at transaction commit time when
1015
- * it's safe to do it (through clear_btree_io_tree()).
1081
+ * it's safe to do it (through extent_io_tree_release()).
10161082 */
10171083 err = clear_extent_bit(dirty_pages, start, end,
10181084 EXTENT_NEED_WAIT, 0, 0, &cached_state);
....@@ -1032,7 +1098,7 @@
10321098 return werr;
10331099 }
10341100
1035
-int btrfs_wait_extents(struct btrfs_fs_info *fs_info,
1101
+static int btrfs_wait_extents(struct btrfs_fs_info *fs_info,
10361102 struct extent_io_tree *dirty_pages)
10371103 {
10381104 bool errors = false;
....@@ -1090,7 +1156,7 @@
10901156 blk_finish_plug(&plug);
10911157 ret2 = btrfs_wait_extents(fs_info, dirty_pages);
10921158
1093
- clear_btree_io_tree(&trans->transaction->dirty_pages);
1159
+ extent_io_tree_release(&trans->transaction->dirty_pages);
10941160
10951161 if (ret)
10961162 return ret;
....@@ -1158,7 +1224,7 @@
11581224
11591225 eb = btrfs_lock_root_node(fs_info->tree_root);
11601226 ret = btrfs_cow_block(trans, fs_info->tree_root, eb, NULL,
1161
- 0, &eb);
1227
+ 0, &eb, BTRFS_NESTING_COW);
11621228 btrfs_tree_unlock(eb);
11631229 free_extent_buffer(eb);
11641230
....@@ -1169,17 +1235,17 @@
11691235 if (ret)
11701236 return ret;
11711237
1172
- ret = btrfs_run_dev_stats(trans, fs_info);
1238
+ ret = btrfs_run_dev_stats(trans);
11731239 if (ret)
11741240 return ret;
1175
- ret = btrfs_run_dev_replace(trans, fs_info);
1241
+ ret = btrfs_run_dev_replace(trans);
11761242 if (ret)
11771243 return ret;
11781244 ret = btrfs_run_qgroups(trans);
11791245 if (ret)
11801246 return ret;
11811247
1182
- ret = btrfs_setup_space_cache(trans, fs_info);
1248
+ ret = btrfs_setup_space_cache(trans);
11831249 if (ret)
11841250 return ret;
11851251
....@@ -1207,7 +1273,7 @@
12071273 }
12081274
12091275 while (!list_empty(dirty_bgs) || !list_empty(io_bgs)) {
1210
- ret = btrfs_write_dirty_block_groups(trans, fs_info);
1276
+ ret = btrfs_write_dirty_block_groups(trans);
12111277 if (ret)
12121278 return ret;
12131279 ret = btrfs_run_delayed_refs(trans, (unsigned long)-1);
....@@ -1220,7 +1286,10 @@
12201286
12211287 list_add_tail(&fs_info->extent_root->dirty_list,
12221288 &trans->transaction->switch_commits);
1223
- btrfs_after_dev_replace_commit(fs_info);
1289
+
1290
+ /* Update dev-replace pointer once everything is committed */
1291
+ fs_info->dev_replace.committed_cursor_left =
1292
+ fs_info->dev_replace.cursor_left_last_write_of_item;
12241293
12251294 return 0;
12261295 }
....@@ -1235,8 +1304,10 @@
12351304 struct btrfs_fs_info *fs_info = root->fs_info;
12361305
12371306 spin_lock(&fs_info->trans_lock);
1238
- if (list_empty(&root->root_list))
1307
+ if (list_empty(&root->root_list)) {
1308
+ btrfs_grab_root(root);
12391309 list_add_tail(&root->root_list, &fs_info->dead_roots);
1310
+ }
12401311 spin_unlock(&fs_info->trans_lock);
12411312 }
12421313
....@@ -1360,7 +1431,7 @@
13601431 return 0;
13611432
13621433 /*
1363
- * Ensure dirty @src will be commited. Or, after comming
1434
+ * Ensure dirty @src will be committed. Or, after coming
13641435 * commit_fs_roots() and switch_commit_roots(), any dirty but not
13651436 * recorded root will never be updated again, causing an outdated root
13661437 * item.
....@@ -1401,7 +1472,7 @@
14011472 ret = commit_cowonly_roots(trans);
14021473 if (ret)
14031474 goto out;
1404
- switch_commit_roots(trans->transaction);
1475
+ switch_commit_roots(trans);
14051476 ret = btrfs_write_and_wait_transaction(trans);
14061477 if (ret)
14071478 btrfs_handle_fs_error(fs_info, ret,
....@@ -1453,7 +1524,6 @@
14531524 u64 index = 0;
14541525 u64 objectid;
14551526 u64 root_flags;
1456
- uuid_le new_uuid;
14571527
14581528 ASSERT(pending->path);
14591529 path = pending->path;
....@@ -1546,8 +1616,7 @@
15461616
15471617 btrfs_set_root_generation_v2(new_root_item,
15481618 trans->transid);
1549
- uuid_le_gen(&new_uuid);
1550
- memcpy(new_root_item->uuid, new_uuid.b, BTRFS_UUID_SIZE);
1619
+ generate_random_guid(new_root_item->uuid);
15511620 memcpy(new_root_item->parent_uuid, root->root_item.uuid,
15521621 BTRFS_UUID_SIZE);
15531622 if (!(root_flags & BTRFS_ROOT_SUBVOL_RDONLY)) {
....@@ -1563,7 +1632,8 @@
15631632 btrfs_set_root_otransid(new_root_item, trans->transid);
15641633
15651634 old = btrfs_lock_root_node(root);
1566
- ret = btrfs_cow_block(trans, root, old, NULL, 0, &old);
1635
+ ret = btrfs_cow_block(trans, root, old, NULL, 0, &old,
1636
+ BTRFS_NESTING_COW);
15671637 if (ret) {
15681638 btrfs_tree_unlock(old);
15691639 free_extent_buffer(old);
....@@ -1571,7 +1641,7 @@
15711641 goto fail;
15721642 }
15731643
1574
- btrfs_set_lock_blocking(old);
1644
+ btrfs_set_lock_blocking_write(old);
15751645
15761646 ret = btrfs_copy_root(trans, root, old, &tmp, objectid);
15771647 /* clean up in any case */
....@@ -1609,9 +1679,10 @@
16091679 }
16101680
16111681 key.offset = (u64)-1;
1612
- pending->snap = btrfs_read_fs_root_no_name(fs_info, &key);
1682
+ pending->snap = btrfs_get_new_fs_root(fs_info, objectid, pending->anon_dev);
16131683 if (IS_ERR(pending->snap)) {
16141684 ret = PTR_ERR(pending->snap);
1685
+ pending->snap = NULL;
16151686 btrfs_abort_transaction(trans, ret);
16161687 goto fail;
16171688 }
....@@ -1639,10 +1710,9 @@
16391710 if (ret < 0)
16401711 goto fail;
16411712
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);
1713
+ ret = btrfs_insert_dir_item(trans, dentry->d_name.name,
1714
+ dentry->d_name.len, BTRFS_I(parent_inode),
1715
+ &key, BTRFS_FT_DIR, index);
16461716 /* We have check then name at the beginning, so it is impossible. */
16471717 BUG_ON(ret == -EEXIST || ret == -EOVERFLOW);
16481718 if (ret) {
....@@ -1659,7 +1729,8 @@
16591729 btrfs_abort_transaction(trans, ret);
16601730 goto fail;
16611731 }
1662
- ret = btrfs_uuid_tree_add(trans, new_uuid.b, BTRFS_UUID_KEY_SUBVOL,
1732
+ ret = btrfs_uuid_tree_add(trans, new_root_item->uuid,
1733
+ BTRFS_UUID_KEY_SUBVOL,
16631734 objectid);
16641735 if (ret) {
16651736 btrfs_abort_transaction(trans, ret);
....@@ -1771,7 +1842,8 @@
17711842 struct btrfs_transaction *trans)
17721843 {
17731844 wait_event(fs_info->transaction_blocked_wait,
1774
- trans->state >= TRANS_STATE_COMMIT_START || trans->aborted);
1845
+ trans->state >= TRANS_STATE_COMMIT_START ||
1846
+ TRANS_ABORTED(trans));
17751847 }
17761848
17771849 /*
....@@ -1783,7 +1855,8 @@
17831855 struct btrfs_transaction *trans)
17841856 {
17851857 wait_event(fs_info->transaction_wait,
1786
- trans->state >= TRANS_STATE_UNBLOCKED || trans->aborted);
1858
+ trans->state >= TRANS_STATE_UNBLOCKED ||
1859
+ TRANS_ABORTED(trans));
17871860 }
17881861
17891862 /*
....@@ -1865,7 +1938,6 @@
18651938 {
18661939 struct btrfs_fs_info *fs_info = trans->fs_info;
18671940 struct btrfs_transaction *cur_trans = trans->transaction;
1868
- DEFINE_WAIT(wait);
18691941
18701942 WARN_ON(refcount_read(&trans->use_count) > 1);
18711943
....@@ -1880,7 +1952,6 @@
18801952 */
18811953 BUG_ON(list_empty(&cur_trans->list));
18821954
1883
- list_del_init(&cur_trans->list);
18841955 if (cur_trans == fs_info->running_transaction) {
18851956 cur_trans->state = TRANS_STATE_COMMIT_DOING;
18861957 spin_unlock(&fs_info->trans_lock);
....@@ -1889,6 +1960,17 @@
18891960
18901961 spin_lock(&fs_info->trans_lock);
18911962 }
1963
+
1964
+ /*
1965
+ * Now that we know no one else is still using the transaction we can
1966
+ * remove the transaction from the list of transactions. This avoids
1967
+ * the transaction kthread from cleaning up the transaction while some
1968
+ * other task is still using it, which could result in a use-after-free
1969
+ * on things like log trees, as it forces the transaction kthread to
1970
+ * wait for this transaction to be cleaned up by us.
1971
+ */
1972
+ list_del_init(&cur_trans->list);
1973
+
18921974 spin_unlock(&fs_info->trans_lock);
18931975
18941976 btrfs_cleanup_one_transaction(trans->transaction, fs_info);
....@@ -1912,8 +1994,25 @@
19121994 kmem_cache_free(btrfs_trans_handle_cachep, trans);
19131995 }
19141996
1915
-static inline int btrfs_start_delalloc_flush(struct btrfs_fs_info *fs_info)
1997
+/*
1998
+ * Release reserved delayed ref space of all pending block groups of the
1999
+ * transaction and remove them from the list
2000
+ */
2001
+static void btrfs_cleanup_pending_block_groups(struct btrfs_trans_handle *trans)
19162002 {
2003
+ struct btrfs_fs_info *fs_info = trans->fs_info;
2004
+ struct btrfs_block_group *block_group, *tmp;
2005
+
2006
+ list_for_each_entry_safe(block_group, tmp, &trans->new_bgs, bg_list) {
2007
+ btrfs_delayed_refs_rsv_release(fs_info, 1);
2008
+ list_del_init(&block_group->bg_list);
2009
+ }
2010
+}
2011
+
2012
+static inline int btrfs_start_delalloc_flush(struct btrfs_trans_handle *trans)
2013
+{
2014
+ struct btrfs_fs_info *fs_info = trans->fs_info;
2015
+
19172016 /*
19182017 * We use writeback_inodes_sb here because if we used
19192018 * btrfs_start_delalloc_roots we would deadlock with fs freeze.
....@@ -1923,22 +2022,50 @@
19232022 * from already being in a transaction and our join_transaction doesn't
19242023 * have to re-take the fs freeze lock.
19252024 */
1926
- if (btrfs_test_opt(fs_info, FLUSHONCOMMIT))
2025
+ if (btrfs_test_opt(fs_info, FLUSHONCOMMIT)) {
19272026 writeback_inodes_sb(fs_info->sb, WB_REASON_SYNC);
2027
+ } else {
2028
+ struct btrfs_pending_snapshot *pending;
2029
+ struct list_head *head = &trans->transaction->pending_snapshots;
2030
+
2031
+ /*
2032
+ * Flush dellaloc for any root that is going to be snapshotted.
2033
+ * This is done to avoid a corrupted version of files, in the
2034
+ * snapshots, that had both buffered and direct IO writes (even
2035
+ * if they were done sequentially) due to an unordered update of
2036
+ * the inode's size on disk.
2037
+ */
2038
+ list_for_each_entry(pending, head, list) {
2039
+ int ret;
2040
+
2041
+ ret = btrfs_start_delalloc_snapshot(pending->root);
2042
+ if (ret)
2043
+ return ret;
2044
+ }
2045
+ }
19282046 return 0;
19292047 }
19302048
1931
-static inline void btrfs_wait_delalloc_flush(struct btrfs_fs_info *fs_info)
2049
+static inline void btrfs_wait_delalloc_flush(struct btrfs_trans_handle *trans)
19322050 {
1933
- if (btrfs_test_opt(fs_info, FLUSHONCOMMIT))
1934
- btrfs_wait_ordered_roots(fs_info, U64_MAX, 0, (u64)-1);
1935
-}
2051
+ struct btrfs_fs_info *fs_info = trans->fs_info;
19362052
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);
2053
+ if (btrfs_test_opt(fs_info, FLUSHONCOMMIT)) {
2054
+ btrfs_wait_ordered_roots(fs_info, U64_MAX, 0, (u64)-1);
2055
+ } else {
2056
+ struct btrfs_pending_snapshot *pending;
2057
+ struct list_head *head = &trans->transaction->pending_snapshots;
2058
+
2059
+ /*
2060
+ * Wait for any dellaloc that we started previously for the roots
2061
+ * that are going to be snapshotted. This is to avoid a corrupted
2062
+ * version of files in the snapshots that had both buffered and
2063
+ * direct IO writes (even if they were done sequentially).
2064
+ */
2065
+ list_for_each_entry(pending, head, list)
2066
+ btrfs_wait_ordered_extents(pending->root,
2067
+ U64_MAX, 0, U64_MAX);
2068
+ }
19422069 }
19432070
19442071 int btrfs_commit_transaction(struct btrfs_trans_handle *trans)
....@@ -1947,6 +2074,8 @@
19472074 struct btrfs_transaction *cur_trans = trans->transaction;
19482075 struct btrfs_transaction *prev_trans = NULL;
19492076 int ret;
2077
+
2078
+ ASSERT(refcount_read(&trans->use_count) == 1);
19502079
19512080 /*
19522081 * Some places just start a transaction to commit it. We need to make
....@@ -1957,7 +2086,7 @@
19572086 trans->dirty = true;
19582087
19592088 /* Stop the commit early if ->aborted is set */
1960
- if (unlikely(READ_ONCE(cur_trans->aborted))) {
2089
+ if (TRANS_ABORTED(cur_trans)) {
19612090 ret = cur_trans->aborted;
19622091 btrfs_end_transaction(trans);
19632092 return ret;
....@@ -1984,8 +2113,7 @@
19842113 cur_trans->delayed_refs.flushing = 1;
19852114 smp_wmb();
19862115
1987
- if (!list_empty(&trans->new_bgs))
1988
- btrfs_create_pending_block_groups(trans);
2116
+ btrfs_create_pending_block_groups(trans);
19892117
19902118 ret = btrfs_run_delayed_refs(trans, 0);
19912119 if (ret) {
....@@ -2032,7 +2160,7 @@
20322160
20332161 wait_for_commit(cur_trans);
20342162
2035
- if (unlikely(cur_trans->aborted))
2163
+ if (TRANS_ABORTED(cur_trans))
20362164 ret = cur_trans->aborted;
20372165
20382166 btrfs_put_transaction(cur_trans);
....@@ -2051,7 +2179,7 @@
20512179 spin_unlock(&fs_info->trans_lock);
20522180
20532181 wait_for_commit(prev_trans);
2054
- ret = prev_trans->aborted;
2182
+ ret = READ_ONCE(prev_trans->aborted);
20552183
20562184 btrfs_put_transaction(prev_trans);
20572185 if (ret)
....@@ -2075,7 +2203,7 @@
20752203
20762204 extwriter_counter_dec(cur_trans, trans->type);
20772205
2078
- ret = btrfs_start_delalloc_flush(fs_info);
2206
+ ret = btrfs_start_delalloc_flush(trans);
20792207 if (ret)
20802208 goto cleanup_transaction;
20812209
....@@ -2091,9 +2219,15 @@
20912219 if (ret)
20922220 goto cleanup_transaction;
20932221
2094
- btrfs_wait_delalloc_flush(fs_info);
2222
+ btrfs_wait_delalloc_flush(trans);
20952223
2096
- btrfs_wait_pending_ordered(cur_trans);
2224
+ /*
2225
+ * Wait for all ordered extents started by a fast fsync that joined this
2226
+ * transaction. Otherwise if this transaction commits before the ordered
2227
+ * extents complete we lose logged data after a power failure.
2228
+ */
2229
+ wait_event(cur_trans->pending_wait,
2230
+ atomic_read(&cur_trans->pending_ordered) == 0);
20972231
20982232 btrfs_scrub_pause(fs_info);
20992233 /*
....@@ -2107,8 +2241,7 @@
21072241 wait_event(cur_trans->writer_wait,
21082242 atomic_read(&cur_trans->num_writers) == 1);
21092243
2110
- /* ->aborted might be set after the previous check, so check it */
2111
- if (unlikely(READ_ONCE(cur_trans->aborted))) {
2244
+ if (TRANS_ABORTED(cur_trans)) {
21122245 ret = cur_trans->aborted;
21132246 goto scrub_continue;
21142247 }
....@@ -2125,10 +2258,8 @@
21252258 * core function of the snapshot creation.
21262259 */
21272260 ret = create_pending_snapshots(trans);
2128
- if (ret) {
2129
- mutex_unlock(&fs_info->reloc_mutex);
2130
- goto scrub_continue;
2131
- }
2261
+ if (ret)
2262
+ goto unlock_reloc;
21322263
21332264 /*
21342265 * We insert the dir indexes of the snapshots and update the inode
....@@ -2141,16 +2272,12 @@
21412272 * the nodes and leaves.
21422273 */
21432274 ret = btrfs_run_delayed_items(trans);
2144
- if (ret) {
2145
- mutex_unlock(&fs_info->reloc_mutex);
2146
- goto scrub_continue;
2147
- }
2275
+ if (ret)
2276
+ goto unlock_reloc;
21482277
21492278 ret = btrfs_run_delayed_refs(trans, (unsigned long)-1);
2150
- if (ret) {
2151
- mutex_unlock(&fs_info->reloc_mutex);
2152
- goto scrub_continue;
2153
- }
2279
+ if (ret)
2280
+ goto unlock_reloc;
21542281
21552282 /*
21562283 * make sure none of the code above managed to slip in a
....@@ -2176,11 +2303,8 @@
21762303 mutex_lock(&fs_info->tree_log_mutex);
21772304
21782305 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
- }
2306
+ if (ret)
2307
+ goto unlock_tree_log;
21842308
21852309 /*
21862310 * Since the transaction is done, we can apply the pending changes
....@@ -2198,42 +2322,29 @@
21982322 * new delayed refs. Must handle them or qgroup can be wrong.
21992323 */
22002324 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
- }
2325
+ if (ret)
2326
+ goto unlock_tree_log;
22062327
22072328 /*
22082329 * Since fs roots are all committed, we can get a quite accurate
22092330 * new_roots. So let's do quota accounting.
22102331 */
22112332 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
- }
2333
+ if (ret < 0)
2334
+ goto unlock_tree_log;
22172335
22182336 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
- }
2337
+ if (ret)
2338
+ goto unlock_tree_log;
22242339
22252340 /*
22262341 * The tasks which save the space cache and inode cache may also
22272342 * update ->aborted, check it.
22282343 */
2229
- if (unlikely(READ_ONCE(cur_trans->aborted))) {
2344
+ if (TRANS_ABORTED(cur_trans)) {
22302345 ret = cur_trans->aborted;
2231
- mutex_unlock(&fs_info->tree_log_mutex);
2232
- mutex_unlock(&fs_info->reloc_mutex);
2233
- goto scrub_continue;
2346
+ goto unlock_tree_log;
22342347 }
2235
-
2236
- btrfs_prepare_extent_commit(fs_info);
22372348
22382349 cur_trans = fs_info->running_transaction;
22392350
....@@ -2247,7 +2358,7 @@
22472358 list_add_tail(&fs_info->chunk_root->dirty_list,
22482359 &cur_trans->switch_commits);
22492360
2250
- switch_commit_roots(cur_trans);
2361
+ switch_commit_roots(trans);
22512362
22522363 ASSERT(list_empty(&cur_trans->dirty_bgs));
22532364 ASSERT(list_empty(&cur_trans->io_bgs));
....@@ -2258,8 +2369,7 @@
22582369 memcpy(fs_info->super_for_commit, fs_info->super_copy,
22592370 sizeof(*fs_info->super_copy));
22602371
2261
- btrfs_update_commit_device_size(fs_info);
2262
- btrfs_update_commit_device_bytes_used(cur_trans);
2372
+ btrfs_commit_device_sizes(cur_trans);
22632373
22642374 clear_bit(BTRFS_FS_LOG1_ERR, &fs_info->flags);
22652375 clear_bit(BTRFS_FS_LOG2_ERR, &fs_info->flags);
....@@ -2278,6 +2388,10 @@
22782388 if (ret) {
22792389 btrfs_handle_fs_error(fs_info, ret,
22802390 "Error while writing out transaction");
2391
+ /*
2392
+ * reloc_mutex has been unlocked, tree_log_mutex is still held
2393
+ * but we can't jump to unlock_tree_log causing double unlock
2394
+ */
22812395 mutex_unlock(&fs_info->tree_log_mutex);
22822396 goto scrub_continue;
22832397 }
....@@ -2303,7 +2417,6 @@
23032417 */
23042418 cur_trans->state = TRANS_STATE_COMPLETED;
23052419 wake_up(&cur_trans->commit_wait);
2306
- clear_bit(BTRFS_FS_NEED_ASYNC_COMMIT, &fs_info->flags);
23072420
23082421 spin_lock(&fs_info->trans_lock);
23092422 list_del_init(&cur_trans->list);
....@@ -2326,10 +2439,15 @@
23262439
23272440 return ret;
23282441
2442
+unlock_tree_log:
2443
+ mutex_unlock(&fs_info->tree_log_mutex);
2444
+unlock_reloc:
2445
+ mutex_unlock(&fs_info->reloc_mutex);
23292446 scrub_continue:
23302447 btrfs_scrub_continue(fs_info);
23312448 cleanup_transaction:
23322449 btrfs_trans_release_metadata(trans);
2450
+ btrfs_cleanup_pending_block_groups(trans);
23332451 btrfs_trans_release_chunk_metadata(trans);
23342452 trans->block_rsv = NULL;
23352453 btrfs_warn(fs_info, "Skipping commit of aborted transaction.");
....@@ -2365,16 +2483,21 @@
23652483 list_del_init(&root->root_list);
23662484 spin_unlock(&fs_info->trans_lock);
23672485
2368
- btrfs_debug(fs_info, "cleaner removing %llu", root->objectid);
2486
+ btrfs_debug(fs_info, "cleaner removing %llu", root->root_key.objectid);
23692487
23702488 btrfs_kill_all_delayed_nodes(root);
2489
+ if (root->ino_cache_inode) {
2490
+ iput(root->ino_cache_inode);
2491
+ root->ino_cache_inode = NULL;
2492
+ }
23712493
23722494 if (btrfs_header_backref_rev(root->node) <
23732495 BTRFS_MIXED_BACKREF_REV)
2374
- ret = btrfs_drop_snapshot(root, NULL, 0, 0);
2496
+ ret = btrfs_drop_snapshot(root, 0, 0);
23752497 else
2376
- ret = btrfs_drop_snapshot(root, NULL, 1, 0);
2498
+ ret = btrfs_drop_snapshot(root, 1, 0);
23772499
2500
+ btrfs_put_root(root);
23782501 return (ret < 0) ? 0 : 1;
23792502 }
23802503