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