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