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