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