| .. | .. |
|---|
| 10 | 10 | #include <linux/pagemap.h> |
|---|
| 11 | 11 | #include <linux/blkdev.h> |
|---|
| 12 | 12 | #include <linux/uuid.h> |
|---|
| 13 | +#include "misc.h" |
|---|
| 13 | 14 | #include "ctree.h" |
|---|
| 14 | 15 | #include "disk-io.h" |
|---|
| 15 | 16 | #include "transaction.h" |
|---|
| .. | .. |
|---|
| 19 | 20 | #include "volumes.h" |
|---|
| 20 | 21 | #include "dev-replace.h" |
|---|
| 21 | 22 | #include "qgroup.h" |
|---|
| 23 | +#include "block-group.h" |
|---|
| 24 | +#include "space-info.h" |
|---|
| 22 | 25 | |
|---|
| 23 | 26 | #define BTRFS_ROOT_TRANS_TAG 0 |
|---|
| 24 | 27 | |
|---|
| 28 | +/* |
|---|
| 29 | + * Transaction states and transitions |
|---|
| 30 | + * |
|---|
| 31 | + * No running transaction (fs tree blocks are not modified) |
|---|
| 32 | + * | |
|---|
| 33 | + * | To next stage: |
|---|
| 34 | + * | Call start_transaction() variants. Except btrfs_join_transaction_nostart(). |
|---|
| 35 | + * V |
|---|
| 36 | + * Transaction N [[TRANS_STATE_RUNNING]] |
|---|
| 37 | + * | |
|---|
| 38 | + * | New trans handles can be attached to transaction N by calling all |
|---|
| 39 | + * | start_transaction() variants. |
|---|
| 40 | + * | |
|---|
| 41 | + * | To next stage: |
|---|
| 42 | + * | Call btrfs_commit_transaction() on any trans handle attached to |
|---|
| 43 | + * | transaction N |
|---|
| 44 | + * V |
|---|
| 45 | + * Transaction N [[TRANS_STATE_COMMIT_START]] |
|---|
| 46 | + * | |
|---|
| 47 | + * | Will wait for previous running transaction to completely finish if there |
|---|
| 48 | + * | is one |
|---|
| 49 | + * | |
|---|
| 50 | + * | Then one of the following happes: |
|---|
| 51 | + * | - Wait for all other trans handle holders to release. |
|---|
| 52 | + * | The btrfs_commit_transaction() caller will do the commit work. |
|---|
| 53 | + * | - Wait for current transaction to be committed by others. |
|---|
| 54 | + * | Other btrfs_commit_transaction() caller will do the commit work. |
|---|
| 55 | + * | |
|---|
| 56 | + * | At this stage, only btrfs_join_transaction*() variants can attach |
|---|
| 57 | + * | to this running transaction. |
|---|
| 58 | + * | All other variants will wait for current one to finish and attach to |
|---|
| 59 | + * | transaction N+1. |
|---|
| 60 | + * | |
|---|
| 61 | + * | To next stage: |
|---|
| 62 | + * | Caller is chosen to commit transaction N, and all other trans handle |
|---|
| 63 | + * | haven been released. |
|---|
| 64 | + * V |
|---|
| 65 | + * Transaction N [[TRANS_STATE_COMMIT_DOING]] |
|---|
| 66 | + * | |
|---|
| 67 | + * | The heavy lifting transaction work is started. |
|---|
| 68 | + * | From running delayed refs (modifying extent tree) to creating pending |
|---|
| 69 | + * | snapshots, running qgroups. |
|---|
| 70 | + * | In short, modify supporting trees to reflect modifications of subvolume |
|---|
| 71 | + * | trees. |
|---|
| 72 | + * | |
|---|
| 73 | + * | At this stage, all start_transaction() calls will wait for this |
|---|
| 74 | + * | transaction to finish and attach to transaction N+1. |
|---|
| 75 | + * | |
|---|
| 76 | + * | To next stage: |
|---|
| 77 | + * | Until all supporting trees are updated. |
|---|
| 78 | + * V |
|---|
| 79 | + * Transaction N [[TRANS_STATE_UNBLOCKED]] |
|---|
| 80 | + * | Transaction N+1 |
|---|
| 81 | + * | All needed trees are modified, thus we only [[TRANS_STATE_RUNNING]] |
|---|
| 82 | + * | need to write them back to disk and update | |
|---|
| 83 | + * | super blocks. | |
|---|
| 84 | + * | | |
|---|
| 85 | + * | At this stage, new transaction is allowed to | |
|---|
| 86 | + * | start. | |
|---|
| 87 | + * | All new start_transaction() calls will be | |
|---|
| 88 | + * | attached to transid N+1. | |
|---|
| 89 | + * | | |
|---|
| 90 | + * | To next stage: | |
|---|
| 91 | + * | Until all tree blocks are super blocks are | |
|---|
| 92 | + * | written to block devices | |
|---|
| 93 | + * V | |
|---|
| 94 | + * Transaction N [[TRANS_STATE_COMPLETED]] V |
|---|
| 95 | + * All tree blocks and super blocks are written. Transaction N+1 |
|---|
| 96 | + * This transaction is finished and all its [[TRANS_STATE_COMMIT_START]] |
|---|
| 97 | + * data structures will be cleaned up. | Life goes on |
|---|
| 98 | + */ |
|---|
| 25 | 99 | static const unsigned int btrfs_blocked_trans_types[TRANS_STATE_MAX] = { |
|---|
| 26 | 100 | [TRANS_STATE_RUNNING] = 0U, |
|---|
| 27 | | - [TRANS_STATE_BLOCKED] = __TRANS_START, |
|---|
| 28 | 101 | [TRANS_STATE_COMMIT_START] = (__TRANS_START | __TRANS_ATTACH), |
|---|
| 29 | 102 | [TRANS_STATE_COMMIT_DOING] = (__TRANS_START | |
|---|
| 30 | 103 | __TRANS_ATTACH | |
|---|
| .. | .. |
|---|
| 47 | 120 | WARN_ON(refcount_read(&transaction->use_count) == 0); |
|---|
| 48 | 121 | if (refcount_dec_and_test(&transaction->use_count)) { |
|---|
| 49 | 122 | BUG_ON(!list_empty(&transaction->list)); |
|---|
| 50 | | - WARN_ON(!RB_EMPTY_ROOT(&transaction->delayed_refs.href_root)); |
|---|
| 123 | + WARN_ON(!RB_EMPTY_ROOT( |
|---|
| 124 | + &transaction->delayed_refs.href_root.rb_root)); |
|---|
| 125 | + WARN_ON(!RB_EMPTY_ROOT( |
|---|
| 126 | + &transaction->delayed_refs.dirty_extent_root)); |
|---|
| 51 | 127 | if (transaction->delayed_refs.pending_csums) |
|---|
| 52 | 128 | btrfs_err(transaction->fs_info, |
|---|
| 53 | 129 | "pending csums is %llu", |
|---|
| 54 | 130 | transaction->delayed_refs.pending_csums); |
|---|
| 55 | | - while (!list_empty(&transaction->pending_chunks)) { |
|---|
| 56 | | - struct extent_map *em; |
|---|
| 57 | | - |
|---|
| 58 | | - em = list_first_entry(&transaction->pending_chunks, |
|---|
| 59 | | - struct extent_map, list); |
|---|
| 60 | | - list_del_init(&em->list); |
|---|
| 61 | | - free_extent_map(em); |
|---|
| 62 | | - } |
|---|
| 63 | 131 | /* |
|---|
| 64 | 132 | * If any block groups are found in ->deleted_bgs then it's |
|---|
| 65 | 133 | * because the transaction was aborted and a commit did not |
|---|
| .. | .. |
|---|
| 68 | 136 | * discard the physical locations of the block groups. |
|---|
| 69 | 137 | */ |
|---|
| 70 | 138 | while (!list_empty(&transaction->deleted_bgs)) { |
|---|
| 71 | | - struct btrfs_block_group_cache *cache; |
|---|
| 139 | + struct btrfs_block_group *cache; |
|---|
| 72 | 140 | |
|---|
| 73 | 141 | cache = list_first_entry(&transaction->deleted_bgs, |
|---|
| 74 | | - struct btrfs_block_group_cache, |
|---|
| 142 | + struct btrfs_block_group, |
|---|
| 75 | 143 | bg_list); |
|---|
| 76 | 144 | list_del_init(&cache->bg_list); |
|---|
| 77 | | - btrfs_put_block_group_trimming(cache); |
|---|
| 145 | + btrfs_unfreeze_block_group(cache); |
|---|
| 78 | 146 | btrfs_put_block_group(cache); |
|---|
| 79 | 147 | } |
|---|
| 148 | + WARN_ON(!list_empty(&transaction->dev_update_list)); |
|---|
| 80 | 149 | kfree(transaction); |
|---|
| 81 | 150 | } |
|---|
| 82 | 151 | } |
|---|
| 83 | 152 | |
|---|
| 84 | | -static void clear_btree_io_tree(struct extent_io_tree *tree) |
|---|
| 153 | +static noinline void switch_commit_roots(struct btrfs_trans_handle *trans) |
|---|
| 85 | 154 | { |
|---|
| 86 | | - spin_lock(&tree->lock); |
|---|
| 87 | | - /* |
|---|
| 88 | | - * Do a single barrier for the waitqueue_active check here, the state |
|---|
| 89 | | - * of the waitqueue should not change once clear_btree_io_tree is |
|---|
| 90 | | - * called. |
|---|
| 91 | | - */ |
|---|
| 92 | | - smp_mb(); |
|---|
| 93 | | - while (!RB_EMPTY_ROOT(&tree->state)) { |
|---|
| 94 | | - struct rb_node *node; |
|---|
| 95 | | - struct extent_state *state; |
|---|
| 96 | | - |
|---|
| 97 | | - node = rb_first(&tree->state); |
|---|
| 98 | | - state = rb_entry(node, struct extent_state, rb_node); |
|---|
| 99 | | - rb_erase(&state->rb_node, &tree->state); |
|---|
| 100 | | - RB_CLEAR_NODE(&state->rb_node); |
|---|
| 101 | | - /* |
|---|
| 102 | | - * btree io trees aren't supposed to have tasks waiting for |
|---|
| 103 | | - * changes in the flags of extent states ever. |
|---|
| 104 | | - */ |
|---|
| 105 | | - ASSERT(!waitqueue_active(&state->wq)); |
|---|
| 106 | | - free_extent_state(state); |
|---|
| 107 | | - |
|---|
| 108 | | - cond_resched_lock(&tree->lock); |
|---|
| 109 | | - } |
|---|
| 110 | | - spin_unlock(&tree->lock); |
|---|
| 111 | | -} |
|---|
| 112 | | - |
|---|
| 113 | | -static noinline void switch_commit_roots(struct btrfs_transaction *trans) |
|---|
| 114 | | -{ |
|---|
| 155 | + struct btrfs_transaction *cur_trans = trans->transaction; |
|---|
| 115 | 156 | struct btrfs_fs_info *fs_info = trans->fs_info; |
|---|
| 116 | 157 | struct btrfs_root *root, *tmp; |
|---|
| 158 | + struct btrfs_caching_control *caching_ctl, *next; |
|---|
| 117 | 159 | |
|---|
| 118 | 160 | down_write(&fs_info->commit_root_sem); |
|---|
| 119 | | - list_for_each_entry_safe(root, tmp, &trans->switch_commits, |
|---|
| 161 | + list_for_each_entry_safe(root, tmp, &cur_trans->switch_commits, |
|---|
| 120 | 162 | dirty_list) { |
|---|
| 121 | 163 | list_del_init(&root->dirty_list); |
|---|
| 122 | 164 | free_extent_buffer(root->commit_root); |
|---|
| 123 | 165 | root->commit_root = btrfs_root_node(root); |
|---|
| 124 | | - if (is_fstree(root->objectid)) |
|---|
| 166 | + if (is_fstree(root->root_key.objectid)) |
|---|
| 125 | 167 | btrfs_unpin_free_ino(root); |
|---|
| 126 | | - clear_btree_io_tree(&root->dirty_log_pages); |
|---|
| 168 | + extent_io_tree_release(&root->dirty_log_pages); |
|---|
| 169 | + btrfs_qgroup_clean_swapped_blocks(root); |
|---|
| 127 | 170 | } |
|---|
| 128 | 171 | |
|---|
| 129 | 172 | /* We can free old roots now. */ |
|---|
| 130 | | - spin_lock(&trans->dropped_roots_lock); |
|---|
| 131 | | - while (!list_empty(&trans->dropped_roots)) { |
|---|
| 132 | | - root = list_first_entry(&trans->dropped_roots, |
|---|
| 173 | + spin_lock(&cur_trans->dropped_roots_lock); |
|---|
| 174 | + while (!list_empty(&cur_trans->dropped_roots)) { |
|---|
| 175 | + root = list_first_entry(&cur_trans->dropped_roots, |
|---|
| 133 | 176 | struct btrfs_root, root_list); |
|---|
| 134 | 177 | list_del_init(&root->root_list); |
|---|
| 135 | | - spin_unlock(&trans->dropped_roots_lock); |
|---|
| 178 | + spin_unlock(&cur_trans->dropped_roots_lock); |
|---|
| 179 | + btrfs_free_log(trans, root); |
|---|
| 136 | 180 | btrfs_drop_and_free_fs_root(fs_info, root); |
|---|
| 137 | | - spin_lock(&trans->dropped_roots_lock); |
|---|
| 181 | + spin_lock(&cur_trans->dropped_roots_lock); |
|---|
| 138 | 182 | } |
|---|
| 139 | | - spin_unlock(&trans->dropped_roots_lock); |
|---|
| 183 | + spin_unlock(&cur_trans->dropped_roots_lock); |
|---|
| 184 | + |
|---|
| 185 | + /* |
|---|
| 186 | + * We have to update the last_byte_to_unpin under the commit_root_sem, |
|---|
| 187 | + * at the same time we swap out the commit roots. |
|---|
| 188 | + * |
|---|
| 189 | + * This is because we must have a real view of the last spot the caching |
|---|
| 190 | + * kthreads were while caching. Consider the following views of the |
|---|
| 191 | + * extent tree for a block group |
|---|
| 192 | + * |
|---|
| 193 | + * commit root |
|---|
| 194 | + * +----+----+----+----+----+----+----+ |
|---|
| 195 | + * |\\\\| |\\\\|\\\\| |\\\\|\\\\| |
|---|
| 196 | + * +----+----+----+----+----+----+----+ |
|---|
| 197 | + * 0 1 2 3 4 5 6 7 |
|---|
| 198 | + * |
|---|
| 199 | + * new commit root |
|---|
| 200 | + * +----+----+----+----+----+----+----+ |
|---|
| 201 | + * | | | |\\\\| | |\\\\| |
|---|
| 202 | + * +----+----+----+----+----+----+----+ |
|---|
| 203 | + * 0 1 2 3 4 5 6 7 |
|---|
| 204 | + * |
|---|
| 205 | + * If the cache_ctl->progress was at 3, then we are only allowed to |
|---|
| 206 | + * unpin [0,1) and [2,3], because the caching thread has already |
|---|
| 207 | + * processed those extents. We are not allowed to unpin [5,6), because |
|---|
| 208 | + * the caching thread will re-start it's search from 3, and thus find |
|---|
| 209 | + * the hole from [4,6) to add to the free space cache. |
|---|
| 210 | + */ |
|---|
| 211 | + list_for_each_entry_safe(caching_ctl, next, |
|---|
| 212 | + &fs_info->caching_block_groups, list) { |
|---|
| 213 | + struct btrfs_block_group *cache = caching_ctl->block_group; |
|---|
| 214 | + |
|---|
| 215 | + if (btrfs_block_group_done(cache)) { |
|---|
| 216 | + cache->last_byte_to_unpin = (u64)-1; |
|---|
| 217 | + list_del_init(&caching_ctl->list); |
|---|
| 218 | + btrfs_put_caching_control(caching_ctl); |
|---|
| 219 | + } else { |
|---|
| 220 | + cache->last_byte_to_unpin = caching_ctl->progress; |
|---|
| 221 | + } |
|---|
| 222 | + } |
|---|
| 140 | 223 | up_write(&fs_info->commit_root_sem); |
|---|
| 141 | 224 | } |
|---|
| 142 | 225 | |
|---|
| .. | .. |
|---|
| 166 | 249 | } |
|---|
| 167 | 250 | |
|---|
| 168 | 251 | /* |
|---|
| 252 | + * To be called after all the new block groups attached to the transaction |
|---|
| 253 | + * handle have been created (btrfs_create_pending_block_groups()). |
|---|
| 254 | + */ |
|---|
| 255 | +void btrfs_trans_release_chunk_metadata(struct btrfs_trans_handle *trans) |
|---|
| 256 | +{ |
|---|
| 257 | + struct btrfs_fs_info *fs_info = trans->fs_info; |
|---|
| 258 | + |
|---|
| 259 | + if (!trans->chunk_bytes_reserved) |
|---|
| 260 | + return; |
|---|
| 261 | + |
|---|
| 262 | + WARN_ON_ONCE(!list_empty(&trans->new_bgs)); |
|---|
| 263 | + |
|---|
| 264 | + btrfs_block_rsv_release(fs_info, &fs_info->chunk_block_rsv, |
|---|
| 265 | + trans->chunk_bytes_reserved, NULL); |
|---|
| 266 | + trans->chunk_bytes_reserved = 0; |
|---|
| 267 | +} |
|---|
| 268 | + |
|---|
| 269 | +/* |
|---|
| 169 | 270 | * either allocate a new transaction or hop into the existing one |
|---|
| 170 | 271 | */ |
|---|
| 171 | 272 | static noinline int join_transaction(struct btrfs_fs_info *fs_info, |
|---|
| .. | .. |
|---|
| 183 | 284 | |
|---|
| 184 | 285 | cur_trans = fs_info->running_transaction; |
|---|
| 185 | 286 | if (cur_trans) { |
|---|
| 186 | | - if (cur_trans->aborted) { |
|---|
| 287 | + if (TRANS_ABORTED(cur_trans)) { |
|---|
| 187 | 288 | spin_unlock(&fs_info->trans_lock); |
|---|
| 188 | 289 | return cur_trans->aborted; |
|---|
| 189 | 290 | } |
|---|
| .. | .. |
|---|
| 231 | 332 | } |
|---|
| 232 | 333 | |
|---|
| 233 | 334 | cur_trans->fs_info = fs_info; |
|---|
| 335 | + atomic_set(&cur_trans->pending_ordered, 0); |
|---|
| 336 | + init_waitqueue_head(&cur_trans->pending_wait); |
|---|
| 234 | 337 | atomic_set(&cur_trans->num_writers, 1); |
|---|
| 235 | 338 | extwriter_counter_init(cur_trans, type); |
|---|
| 236 | 339 | init_waitqueue_head(&cur_trans->writer_wait); |
|---|
| 237 | 340 | init_waitqueue_head(&cur_trans->commit_wait); |
|---|
| 238 | | - init_waitqueue_head(&cur_trans->pending_wait); |
|---|
| 239 | 341 | cur_trans->state = TRANS_STATE_RUNNING; |
|---|
| 240 | 342 | /* |
|---|
| 241 | 343 | * One for this trans handle, one so it will live on until we |
|---|
| 242 | 344 | * commit the transaction. |
|---|
| 243 | 345 | */ |
|---|
| 244 | 346 | refcount_set(&cur_trans->use_count, 2); |
|---|
| 245 | | - atomic_set(&cur_trans->pending_ordered, 0); |
|---|
| 246 | 347 | cur_trans->flags = 0; |
|---|
| 247 | 348 | cur_trans->start_time = ktime_get_seconds(); |
|---|
| 248 | 349 | |
|---|
| 249 | 350 | memset(&cur_trans->delayed_refs, 0, sizeof(cur_trans->delayed_refs)); |
|---|
| 250 | 351 | |
|---|
| 251 | | - cur_trans->delayed_refs.href_root = RB_ROOT; |
|---|
| 352 | + cur_trans->delayed_refs.href_root = RB_ROOT_CACHED; |
|---|
| 252 | 353 | cur_trans->delayed_refs.dirty_extent_root = RB_ROOT; |
|---|
| 253 | 354 | atomic_set(&cur_trans->delayed_refs.num_entries, 0); |
|---|
| 254 | 355 | |
|---|
| .. | .. |
|---|
| 266 | 367 | spin_lock_init(&cur_trans->delayed_refs.lock); |
|---|
| 267 | 368 | |
|---|
| 268 | 369 | INIT_LIST_HEAD(&cur_trans->pending_snapshots); |
|---|
| 269 | | - INIT_LIST_HEAD(&cur_trans->pending_chunks); |
|---|
| 370 | + INIT_LIST_HEAD(&cur_trans->dev_update_list); |
|---|
| 270 | 371 | INIT_LIST_HEAD(&cur_trans->switch_commits); |
|---|
| 271 | 372 | INIT_LIST_HEAD(&cur_trans->dirty_bgs); |
|---|
| 272 | 373 | INIT_LIST_HEAD(&cur_trans->io_bgs); |
|---|
| 273 | 374 | INIT_LIST_HEAD(&cur_trans->dropped_roots); |
|---|
| 274 | 375 | mutex_init(&cur_trans->cache_write_mutex); |
|---|
| 275 | | - cur_trans->num_dirty_bgs = 0; |
|---|
| 276 | 376 | spin_lock_init(&cur_trans->dirty_bgs_lock); |
|---|
| 277 | 377 | INIT_LIST_HEAD(&cur_trans->deleted_bgs); |
|---|
| 278 | 378 | spin_lock_init(&cur_trans->dropped_roots_lock); |
|---|
| 279 | 379 | list_add_tail(&cur_trans->list, &fs_info->trans_list); |
|---|
| 280 | | - extent_io_tree_init(&cur_trans->dirty_pages, |
|---|
| 281 | | - fs_info->btree_inode); |
|---|
| 380 | + extent_io_tree_init(fs_info, &cur_trans->dirty_pages, |
|---|
| 381 | + IO_TREE_TRANS_DIRTY_PAGES, fs_info->btree_inode); |
|---|
| 382 | + extent_io_tree_init(fs_info, &cur_trans->pinned_extents, |
|---|
| 383 | + IO_TREE_FS_PINNED_EXTENTS, NULL); |
|---|
| 282 | 384 | fs_info->generation++; |
|---|
| 283 | 385 | cur_trans->transid = fs_info->generation; |
|---|
| 284 | 386 | fs_info->running_transaction = cur_trans; |
|---|
| .. | .. |
|---|
| 289 | 391 | } |
|---|
| 290 | 392 | |
|---|
| 291 | 393 | /* |
|---|
| 292 | | - * this does all the record keeping required to make sure that a reference |
|---|
| 293 | | - * counted root is properly recorded in a given transaction. This is required |
|---|
| 294 | | - * to make sure the old root from before we joined the transaction is deleted |
|---|
| 295 | | - * when the transaction commits |
|---|
| 394 | + * This does all the record keeping required to make sure that a shareable root |
|---|
| 395 | + * is properly recorded in a given transaction. This is required to make sure |
|---|
| 396 | + * the old root from before we joined the transaction is deleted when the |
|---|
| 397 | + * transaction commits. |
|---|
| 296 | 398 | */ |
|---|
| 297 | 399 | static int record_root_in_trans(struct btrfs_trans_handle *trans, |
|---|
| 298 | 400 | struct btrfs_root *root, |
|---|
| .. | .. |
|---|
| 300 | 402 | { |
|---|
| 301 | 403 | struct btrfs_fs_info *fs_info = root->fs_info; |
|---|
| 302 | 404 | |
|---|
| 303 | | - if ((test_bit(BTRFS_ROOT_REF_COWS, &root->state) && |
|---|
| 405 | + if ((test_bit(BTRFS_ROOT_SHAREABLE, &root->state) && |
|---|
| 304 | 406 | root->last_trans < trans->transid) || force) { |
|---|
| 305 | 407 | WARN_ON(root == fs_info->extent_root); |
|---|
| 306 | 408 | WARN_ON(!force && root->commit_root != root->node); |
|---|
| .. | .. |
|---|
| 379 | 481 | { |
|---|
| 380 | 482 | struct btrfs_fs_info *fs_info = root->fs_info; |
|---|
| 381 | 483 | |
|---|
| 382 | | - if (!test_bit(BTRFS_ROOT_REF_COWS, &root->state)) |
|---|
| 484 | + if (!test_bit(BTRFS_ROOT_SHAREABLE, &root->state)) |
|---|
| 383 | 485 | return 0; |
|---|
| 384 | 486 | |
|---|
| 385 | 487 | /* |
|---|
| .. | .. |
|---|
| 400 | 502 | |
|---|
| 401 | 503 | static inline int is_transaction_blocked(struct btrfs_transaction *trans) |
|---|
| 402 | 504 | { |
|---|
| 403 | | - return (trans->state >= TRANS_STATE_BLOCKED && |
|---|
| 505 | + return (trans->state >= TRANS_STATE_COMMIT_START && |
|---|
| 404 | 506 | trans->state < TRANS_STATE_UNBLOCKED && |
|---|
| 405 | | - !trans->aborted); |
|---|
| 507 | + !TRANS_ABORTED(trans)); |
|---|
| 406 | 508 | } |
|---|
| 407 | 509 | |
|---|
| 408 | 510 | /* wait for commit against the current transaction to become unblocked |
|---|
| .. | .. |
|---|
| 421 | 523 | |
|---|
| 422 | 524 | wait_event(fs_info->transaction_wait, |
|---|
| 423 | 525 | cur_trans->state >= TRANS_STATE_UNBLOCKED || |
|---|
| 424 | | - cur_trans->aborted); |
|---|
| 526 | + TRANS_ABORTED(cur_trans)); |
|---|
| 425 | 527 | btrfs_put_transaction(cur_trans); |
|---|
| 426 | 528 | } else { |
|---|
| 427 | 529 | spin_unlock(&fs_info->trans_lock); |
|---|
| .. | .. |
|---|
| 444 | 546 | struct btrfs_fs_info *fs_info = root->fs_info; |
|---|
| 445 | 547 | |
|---|
| 446 | 548 | if (!fs_info->reloc_ctl || |
|---|
| 447 | | - !test_bit(BTRFS_ROOT_REF_COWS, &root->state) || |
|---|
| 549 | + !test_bit(BTRFS_ROOT_SHAREABLE, &root->state) || |
|---|
| 448 | 550 | root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID || |
|---|
| 449 | 551 | root->reloc_root) |
|---|
| 450 | 552 | return false; |
|---|
| .. | .. |
|---|
| 458 | 560 | bool enforce_qgroups) |
|---|
| 459 | 561 | { |
|---|
| 460 | 562 | struct btrfs_fs_info *fs_info = root->fs_info; |
|---|
| 461 | | - |
|---|
| 563 | + struct btrfs_block_rsv *delayed_refs_rsv = &fs_info->delayed_refs_rsv; |
|---|
| 462 | 564 | struct btrfs_trans_handle *h; |
|---|
| 463 | 565 | struct btrfs_transaction *cur_trans; |
|---|
| 464 | 566 | u64 num_bytes = 0; |
|---|
| 465 | 567 | u64 qgroup_reserved = 0; |
|---|
| 466 | 568 | bool reloc_reserved = false; |
|---|
| 569 | + bool do_chunk_alloc = false; |
|---|
| 467 | 570 | int ret; |
|---|
| 468 | 571 | |
|---|
| 469 | 572 | /* Send isn't supposed to start transactions. */ |
|---|
| .. | .. |
|---|
| 487 | 590 | * the appropriate flushing if need be. |
|---|
| 488 | 591 | */ |
|---|
| 489 | 592 | if (num_items && root != fs_info->chunk_root) { |
|---|
| 593 | + struct btrfs_block_rsv *rsv = &fs_info->trans_block_rsv; |
|---|
| 594 | + u64 delayed_refs_bytes = 0; |
|---|
| 595 | + |
|---|
| 490 | 596 | qgroup_reserved = num_items * fs_info->nodesize; |
|---|
| 491 | 597 | ret = btrfs_qgroup_reserve_meta_pertrans(root, qgroup_reserved, |
|---|
| 492 | 598 | enforce_qgroups); |
|---|
| 493 | 599 | if (ret) |
|---|
| 494 | 600 | return ERR_PTR(ret); |
|---|
| 495 | 601 | |
|---|
| 496 | | - num_bytes = btrfs_calc_trans_metadata_size(fs_info, num_items); |
|---|
| 602 | + /* |
|---|
| 603 | + * We want to reserve all the bytes we may need all at once, so |
|---|
| 604 | + * we only do 1 enospc flushing cycle per transaction start. We |
|---|
| 605 | + * accomplish this by simply assuming we'll do 2 x num_items |
|---|
| 606 | + * worth of delayed refs updates in this trans handle, and |
|---|
| 607 | + * refill that amount for whatever is missing in the reserve. |
|---|
| 608 | + */ |
|---|
| 609 | + num_bytes = btrfs_calc_insert_metadata_size(fs_info, num_items); |
|---|
| 610 | + if (flush == BTRFS_RESERVE_FLUSH_ALL && |
|---|
| 611 | + delayed_refs_rsv->full == 0) { |
|---|
| 612 | + delayed_refs_bytes = num_bytes; |
|---|
| 613 | + num_bytes <<= 1; |
|---|
| 614 | + } |
|---|
| 615 | + |
|---|
| 497 | 616 | /* |
|---|
| 498 | 617 | * Do the reservation for the relocation root creation |
|---|
| 499 | 618 | */ |
|---|
| .. | .. |
|---|
| 502 | 621 | reloc_reserved = true; |
|---|
| 503 | 622 | } |
|---|
| 504 | 623 | |
|---|
| 505 | | - ret = btrfs_block_rsv_add(root, &fs_info->trans_block_rsv, |
|---|
| 506 | | - num_bytes, flush); |
|---|
| 624 | + ret = btrfs_block_rsv_add(root, rsv, num_bytes, flush); |
|---|
| 625 | + if (ret) |
|---|
| 626 | + goto reserve_fail; |
|---|
| 627 | + if (delayed_refs_bytes) { |
|---|
| 628 | + btrfs_migrate_to_delayed_refs_rsv(fs_info, rsv, |
|---|
| 629 | + delayed_refs_bytes); |
|---|
| 630 | + num_bytes -= delayed_refs_bytes; |
|---|
| 631 | + } |
|---|
| 632 | + |
|---|
| 633 | + if (rsv->space_info->force_alloc) |
|---|
| 634 | + do_chunk_alloc = true; |
|---|
| 635 | + } else if (num_items == 0 && flush == BTRFS_RESERVE_FLUSH_ALL && |
|---|
| 636 | + !delayed_refs_rsv->full) { |
|---|
| 637 | + /* |
|---|
| 638 | + * Some people call with btrfs_start_transaction(root, 0) |
|---|
| 639 | + * because they can be throttled, but have some other mechanism |
|---|
| 640 | + * for reserving space. We still want these guys to refill the |
|---|
| 641 | + * delayed block_rsv so just add 1 items worth of reservation |
|---|
| 642 | + * here. |
|---|
| 643 | + */ |
|---|
| 644 | + ret = btrfs_delayed_refs_rsv_refill(fs_info, flush); |
|---|
| 507 | 645 | if (ret) |
|---|
| 508 | 646 | goto reserve_fail; |
|---|
| 509 | 647 | } |
|---|
| .. | .. |
|---|
| 556 | 694 | INIT_LIST_HEAD(&h->new_bgs); |
|---|
| 557 | 695 | |
|---|
| 558 | 696 | smp_mb(); |
|---|
| 559 | | - if (cur_trans->state >= TRANS_STATE_BLOCKED && |
|---|
| 697 | + if (cur_trans->state >= TRANS_STATE_COMMIT_START && |
|---|
| 560 | 698 | may_wait_transaction(fs_info, type)) { |
|---|
| 561 | 699 | current->journal_info = h; |
|---|
| 562 | 700 | btrfs_commit_transaction(h); |
|---|
| .. | .. |
|---|
| 574 | 712 | got_it: |
|---|
| 575 | 713 | if (!current->journal_info) |
|---|
| 576 | 714 | current->journal_info = h; |
|---|
| 715 | + |
|---|
| 716 | + /* |
|---|
| 717 | + * If the space_info is marked ALLOC_FORCE then we'll get upgraded to |
|---|
| 718 | + * ALLOC_FORCE the first run through, and then we won't allocate for |
|---|
| 719 | + * anybody else who races in later. We don't care about the return |
|---|
| 720 | + * value here. |
|---|
| 721 | + */ |
|---|
| 722 | + if (do_chunk_alloc && num_bytes) { |
|---|
| 723 | + u64 flags = h->block_rsv->space_info->flags; |
|---|
| 724 | + |
|---|
| 725 | + btrfs_chunk_alloc(h, btrfs_get_alloc_profile(fs_info, flags), |
|---|
| 726 | + CHUNK_ALLOC_NO_FORCE); |
|---|
| 727 | + } |
|---|
| 577 | 728 | |
|---|
| 578 | 729 | /* |
|---|
| 579 | 730 | * btrfs_record_root_in_trans() needs to alloc new extents, and may |
|---|
| .. | .. |
|---|
| 594 | 745 | alloc_fail: |
|---|
| 595 | 746 | if (num_bytes) |
|---|
| 596 | 747 | btrfs_block_rsv_release(fs_info, &fs_info->trans_block_rsv, |
|---|
| 597 | | - num_bytes); |
|---|
| 748 | + num_bytes, NULL); |
|---|
| 598 | 749 | reserve_fail: |
|---|
| 599 | 750 | btrfs_qgroup_free_meta_pertrans(root, qgroup_reserved); |
|---|
| 600 | 751 | return ERR_PTR(ret); |
|---|
| .. | .. |
|---|
| 609 | 760 | |
|---|
| 610 | 761 | struct btrfs_trans_handle *btrfs_start_transaction_fallback_global_rsv( |
|---|
| 611 | 762 | struct btrfs_root *root, |
|---|
| 612 | | - unsigned int num_items, |
|---|
| 613 | | - int min_factor) |
|---|
| 763 | + unsigned int num_items) |
|---|
| 614 | 764 | { |
|---|
| 615 | | - struct btrfs_fs_info *fs_info = root->fs_info; |
|---|
| 616 | | - struct btrfs_trans_handle *trans; |
|---|
| 617 | | - u64 num_bytes; |
|---|
| 618 | | - int ret; |
|---|
| 619 | | - |
|---|
| 620 | | - /* |
|---|
| 621 | | - * We have two callers: unlink and block group removal. The |
|---|
| 622 | | - * former should succeed even if we will temporarily exceed |
|---|
| 623 | | - * quota and the latter operates on the extent root so |
|---|
| 624 | | - * qgroup enforcement is ignored anyway. |
|---|
| 625 | | - */ |
|---|
| 626 | | - trans = start_transaction(root, num_items, TRANS_START, |
|---|
| 627 | | - BTRFS_RESERVE_FLUSH_ALL, false); |
|---|
| 628 | | - if (!IS_ERR(trans) || PTR_ERR(trans) != -ENOSPC) |
|---|
| 629 | | - return trans; |
|---|
| 630 | | - |
|---|
| 631 | | - trans = btrfs_start_transaction(root, 0); |
|---|
| 632 | | - if (IS_ERR(trans)) |
|---|
| 633 | | - return trans; |
|---|
| 634 | | - |
|---|
| 635 | | - num_bytes = btrfs_calc_trans_metadata_size(fs_info, num_items); |
|---|
| 636 | | - ret = btrfs_cond_migrate_bytes(fs_info, &fs_info->trans_block_rsv, |
|---|
| 637 | | - num_bytes, min_factor); |
|---|
| 638 | | - if (ret) { |
|---|
| 639 | | - btrfs_end_transaction(trans); |
|---|
| 640 | | - return ERR_PTR(ret); |
|---|
| 641 | | - } |
|---|
| 642 | | - |
|---|
| 643 | | - trans->block_rsv = &fs_info->trans_block_rsv; |
|---|
| 644 | | - trans->bytes_reserved = num_bytes; |
|---|
| 645 | | - trace_btrfs_space_reservation(fs_info, "transaction", |
|---|
| 646 | | - trans->transid, num_bytes, 1); |
|---|
| 647 | | - |
|---|
| 648 | | - return trans; |
|---|
| 765 | + return start_transaction(root, num_items, TRANS_START, |
|---|
| 766 | + BTRFS_RESERVE_FLUSH_ALL_STEAL, false); |
|---|
| 649 | 767 | } |
|---|
| 650 | 768 | |
|---|
| 651 | 769 | struct btrfs_trans_handle *btrfs_join_transaction(struct btrfs_root *root) |
|---|
| .. | .. |
|---|
| 654 | 772 | true); |
|---|
| 655 | 773 | } |
|---|
| 656 | 774 | |
|---|
| 657 | | -struct btrfs_trans_handle *btrfs_join_transaction_nolock(struct btrfs_root *root) |
|---|
| 775 | +struct btrfs_trans_handle *btrfs_join_transaction_spacecache(struct btrfs_root *root) |
|---|
| 658 | 776 | { |
|---|
| 659 | 777 | return start_transaction(root, 0, TRANS_JOIN_NOLOCK, |
|---|
| 660 | 778 | BTRFS_RESERVE_NO_FLUSH, true); |
|---|
| .. | .. |
|---|
| 692 | 810 | /* |
|---|
| 693 | 811 | * btrfs_attach_transaction_barrier() - catch the running transaction |
|---|
| 694 | 812 | * |
|---|
| 695 | | - * It is similar to the above function, the differentia is this one |
|---|
| 813 | + * It is similar to the above function, the difference is this one |
|---|
| 696 | 814 | * will wait for all the inactive transactions until they fully |
|---|
| 697 | 815 | * complete. |
|---|
| 698 | 816 | */ |
|---|
| .. | .. |
|---|
| 782 | 900 | { |
|---|
| 783 | 901 | struct btrfs_fs_info *fs_info = trans->fs_info; |
|---|
| 784 | 902 | |
|---|
| 785 | | - if (btrfs_check_space_for_delayed_refs(trans, fs_info)) |
|---|
| 903 | + if (btrfs_check_space_for_delayed_refs(fs_info)) |
|---|
| 786 | 904 | return 1; |
|---|
| 787 | 905 | |
|---|
| 788 | 906 | return !!btrfs_block_rsv_check(&fs_info->global_block_rsv, 5); |
|---|
| .. | .. |
|---|
| 791 | 909 | int btrfs_should_end_transaction(struct btrfs_trans_handle *trans) |
|---|
| 792 | 910 | { |
|---|
| 793 | 911 | struct btrfs_transaction *cur_trans = trans->transaction; |
|---|
| 794 | | - int updates; |
|---|
| 795 | | - int err; |
|---|
| 796 | 912 | |
|---|
| 797 | 913 | smp_mb(); |
|---|
| 798 | | - if (cur_trans->state >= TRANS_STATE_BLOCKED || |
|---|
| 914 | + if (cur_trans->state >= TRANS_STATE_COMMIT_START || |
|---|
| 799 | 915 | cur_trans->delayed_refs.flushing) |
|---|
| 800 | 916 | return 1; |
|---|
| 801 | | - |
|---|
| 802 | | - updates = trans->delayed_ref_updates; |
|---|
| 803 | | - trans->delayed_ref_updates = 0; |
|---|
| 804 | | - if (updates) { |
|---|
| 805 | | - err = btrfs_run_delayed_refs(trans, updates * 2); |
|---|
| 806 | | - if (err) /* Error code will also eval true */ |
|---|
| 807 | | - return err; |
|---|
| 808 | | - } |
|---|
| 809 | 917 | |
|---|
| 810 | 918 | return should_end_transaction(trans); |
|---|
| 811 | 919 | } |
|---|
| .. | .. |
|---|
| 827 | 935 | trace_btrfs_space_reservation(fs_info, "transaction", |
|---|
| 828 | 936 | trans->transid, trans->bytes_reserved, 0); |
|---|
| 829 | 937 | btrfs_block_rsv_release(fs_info, trans->block_rsv, |
|---|
| 830 | | - trans->bytes_reserved); |
|---|
| 938 | + trans->bytes_reserved, NULL); |
|---|
| 831 | 939 | trans->bytes_reserved = 0; |
|---|
| 832 | 940 | } |
|---|
| 833 | 941 | |
|---|
| .. | .. |
|---|
| 836 | 944 | { |
|---|
| 837 | 945 | struct btrfs_fs_info *info = trans->fs_info; |
|---|
| 838 | 946 | struct btrfs_transaction *cur_trans = trans->transaction; |
|---|
| 839 | | - u64 transid = trans->transid; |
|---|
| 840 | | - unsigned long cur = trans->delayed_ref_updates; |
|---|
| 841 | | - int lock = (trans->type != TRANS_JOIN_NOLOCK); |
|---|
| 842 | 947 | int err = 0; |
|---|
| 843 | | - int must_run_delayed_refs = 0; |
|---|
| 844 | 948 | |
|---|
| 845 | 949 | if (refcount_read(&trans->use_count) > 1) { |
|---|
| 846 | 950 | refcount_dec(&trans->use_count); |
|---|
| .. | .. |
|---|
| 851 | 955 | btrfs_trans_release_metadata(trans); |
|---|
| 852 | 956 | trans->block_rsv = NULL; |
|---|
| 853 | 957 | |
|---|
| 854 | | - if (!list_empty(&trans->new_bgs)) |
|---|
| 855 | | - btrfs_create_pending_block_groups(trans); |
|---|
| 856 | | - |
|---|
| 857 | | - trans->delayed_ref_updates = 0; |
|---|
| 858 | | - if (!trans->sync) { |
|---|
| 859 | | - must_run_delayed_refs = |
|---|
| 860 | | - btrfs_should_throttle_delayed_refs(trans, info); |
|---|
| 861 | | - cur = max_t(unsigned long, cur, 32); |
|---|
| 862 | | - |
|---|
| 863 | | - /* |
|---|
| 864 | | - * don't make the caller wait if they are from a NOLOCK |
|---|
| 865 | | - * or ATTACH transaction, it will deadlock with commit |
|---|
| 866 | | - */ |
|---|
| 867 | | - if (must_run_delayed_refs == 1 && |
|---|
| 868 | | - (trans->type & (__TRANS_JOIN_NOLOCK | __TRANS_ATTACH))) |
|---|
| 869 | | - must_run_delayed_refs = 2; |
|---|
| 870 | | - } |
|---|
| 871 | | - |
|---|
| 872 | | - btrfs_trans_release_metadata(trans); |
|---|
| 873 | | - trans->block_rsv = NULL; |
|---|
| 874 | | - |
|---|
| 875 | | - if (!list_empty(&trans->new_bgs)) |
|---|
| 876 | | - btrfs_create_pending_block_groups(trans); |
|---|
| 958 | + btrfs_create_pending_block_groups(trans); |
|---|
| 877 | 959 | |
|---|
| 878 | 960 | btrfs_trans_release_chunk_metadata(trans); |
|---|
| 879 | | - |
|---|
| 880 | | - if (lock && should_end_transaction(trans) && |
|---|
| 881 | | - READ_ONCE(cur_trans->state) == TRANS_STATE_RUNNING) { |
|---|
| 882 | | - spin_lock(&info->trans_lock); |
|---|
| 883 | | - if (cur_trans->state == TRANS_STATE_RUNNING) |
|---|
| 884 | | - cur_trans->state = TRANS_STATE_BLOCKED; |
|---|
| 885 | | - spin_unlock(&info->trans_lock); |
|---|
| 886 | | - } |
|---|
| 887 | | - |
|---|
| 888 | | - if (lock && READ_ONCE(cur_trans->state) == TRANS_STATE_BLOCKED) { |
|---|
| 889 | | - if (throttle) |
|---|
| 890 | | - return btrfs_commit_transaction(trans); |
|---|
| 891 | | - else |
|---|
| 892 | | - wake_up_process(info->transaction_kthread); |
|---|
| 893 | | - } |
|---|
| 894 | 961 | |
|---|
| 895 | 962 | if (trans->type & __TRANS_FREEZABLE) |
|---|
| 896 | 963 | sb_end_intwrite(info->sb); |
|---|
| .. | .. |
|---|
| 909 | 976 | if (throttle) |
|---|
| 910 | 977 | btrfs_run_delayed_iputs(info); |
|---|
| 911 | 978 | |
|---|
| 912 | | - if (trans->aborted || |
|---|
| 979 | + if (TRANS_ABORTED(trans) || |
|---|
| 913 | 980 | test_bit(BTRFS_FS_STATE_ERROR, &info->fs_state)) { |
|---|
| 914 | 981 | wake_up_process(info->transaction_kthread); |
|---|
| 915 | | - err = -EIO; |
|---|
| 982 | + if (TRANS_ABORTED(trans)) |
|---|
| 983 | + err = trans->aborted; |
|---|
| 984 | + else |
|---|
| 985 | + err = -EROFS; |
|---|
| 916 | 986 | } |
|---|
| 917 | 987 | |
|---|
| 918 | 988 | kmem_cache_free(btrfs_trans_handle_cachep, trans); |
|---|
| 919 | | - if (must_run_delayed_refs) { |
|---|
| 920 | | - btrfs_async_run_delayed_refs(info, cur, transid, |
|---|
| 921 | | - must_run_delayed_refs == 1); |
|---|
| 922 | | - } |
|---|
| 923 | 989 | return err; |
|---|
| 924 | 990 | } |
|---|
| 925 | 991 | |
|---|
| .. | .. |
|---|
| 967 | 1033 | * superblock that points to btree nodes/leafs for which |
|---|
| 968 | 1034 | * writeback hasn't finished yet (and without errors). |
|---|
| 969 | 1035 | * We cleanup any entries left in the io tree when committing |
|---|
| 970 | | - * the transaction (through clear_btree_io_tree()). |
|---|
| 1036 | + * the transaction (through extent_io_tree_release()). |
|---|
| 971 | 1037 | */ |
|---|
| 972 | 1038 | if (err == -ENOMEM) { |
|---|
| 973 | 1039 | err = 0; |
|---|
| .. | .. |
|---|
| 1012 | 1078 | * left in the io tree. For a log commit, we don't remove them |
|---|
| 1013 | 1079 | * after committing the log because the tree can be accessed |
|---|
| 1014 | 1080 | * concurrently - we do it only at transaction commit time when |
|---|
| 1015 | | - * it's safe to do it (through clear_btree_io_tree()). |
|---|
| 1081 | + * it's safe to do it (through extent_io_tree_release()). |
|---|
| 1016 | 1082 | */ |
|---|
| 1017 | 1083 | err = clear_extent_bit(dirty_pages, start, end, |
|---|
| 1018 | 1084 | EXTENT_NEED_WAIT, 0, 0, &cached_state); |
|---|
| .. | .. |
|---|
| 1032 | 1098 | return werr; |
|---|
| 1033 | 1099 | } |
|---|
| 1034 | 1100 | |
|---|
| 1035 | | -int btrfs_wait_extents(struct btrfs_fs_info *fs_info, |
|---|
| 1101 | +static int btrfs_wait_extents(struct btrfs_fs_info *fs_info, |
|---|
| 1036 | 1102 | struct extent_io_tree *dirty_pages) |
|---|
| 1037 | 1103 | { |
|---|
| 1038 | 1104 | bool errors = false; |
|---|
| .. | .. |
|---|
| 1090 | 1156 | blk_finish_plug(&plug); |
|---|
| 1091 | 1157 | ret2 = btrfs_wait_extents(fs_info, dirty_pages); |
|---|
| 1092 | 1158 | |
|---|
| 1093 | | - clear_btree_io_tree(&trans->transaction->dirty_pages); |
|---|
| 1159 | + extent_io_tree_release(&trans->transaction->dirty_pages); |
|---|
| 1094 | 1160 | |
|---|
| 1095 | 1161 | if (ret) |
|---|
| 1096 | 1162 | return ret; |
|---|
| .. | .. |
|---|
| 1158 | 1224 | |
|---|
| 1159 | 1225 | eb = btrfs_lock_root_node(fs_info->tree_root); |
|---|
| 1160 | 1226 | ret = btrfs_cow_block(trans, fs_info->tree_root, eb, NULL, |
|---|
| 1161 | | - 0, &eb); |
|---|
| 1227 | + 0, &eb, BTRFS_NESTING_COW); |
|---|
| 1162 | 1228 | btrfs_tree_unlock(eb); |
|---|
| 1163 | 1229 | free_extent_buffer(eb); |
|---|
| 1164 | 1230 | |
|---|
| .. | .. |
|---|
| 1169 | 1235 | if (ret) |
|---|
| 1170 | 1236 | return ret; |
|---|
| 1171 | 1237 | |
|---|
| 1172 | | - ret = btrfs_run_dev_stats(trans, fs_info); |
|---|
| 1238 | + ret = btrfs_run_dev_stats(trans); |
|---|
| 1173 | 1239 | if (ret) |
|---|
| 1174 | 1240 | return ret; |
|---|
| 1175 | | - ret = btrfs_run_dev_replace(trans, fs_info); |
|---|
| 1241 | + ret = btrfs_run_dev_replace(trans); |
|---|
| 1176 | 1242 | if (ret) |
|---|
| 1177 | 1243 | return ret; |
|---|
| 1178 | 1244 | ret = btrfs_run_qgroups(trans); |
|---|
| 1179 | 1245 | if (ret) |
|---|
| 1180 | 1246 | return ret; |
|---|
| 1181 | 1247 | |
|---|
| 1182 | | - ret = btrfs_setup_space_cache(trans, fs_info); |
|---|
| 1248 | + ret = btrfs_setup_space_cache(trans); |
|---|
| 1183 | 1249 | if (ret) |
|---|
| 1184 | 1250 | return ret; |
|---|
| 1185 | 1251 | |
|---|
| .. | .. |
|---|
| 1207 | 1273 | } |
|---|
| 1208 | 1274 | |
|---|
| 1209 | 1275 | while (!list_empty(dirty_bgs) || !list_empty(io_bgs)) { |
|---|
| 1210 | | - ret = btrfs_write_dirty_block_groups(trans, fs_info); |
|---|
| 1276 | + ret = btrfs_write_dirty_block_groups(trans); |
|---|
| 1211 | 1277 | if (ret) |
|---|
| 1212 | 1278 | return ret; |
|---|
| 1213 | 1279 | ret = btrfs_run_delayed_refs(trans, (unsigned long)-1); |
|---|
| .. | .. |
|---|
| 1220 | 1286 | |
|---|
| 1221 | 1287 | list_add_tail(&fs_info->extent_root->dirty_list, |
|---|
| 1222 | 1288 | &trans->transaction->switch_commits); |
|---|
| 1223 | | - btrfs_after_dev_replace_commit(fs_info); |
|---|
| 1289 | + |
|---|
| 1290 | + /* Update dev-replace pointer once everything is committed */ |
|---|
| 1291 | + fs_info->dev_replace.committed_cursor_left = |
|---|
| 1292 | + fs_info->dev_replace.cursor_left_last_write_of_item; |
|---|
| 1224 | 1293 | |
|---|
| 1225 | 1294 | return 0; |
|---|
| 1226 | 1295 | } |
|---|
| .. | .. |
|---|
| 1235 | 1304 | struct btrfs_fs_info *fs_info = root->fs_info; |
|---|
| 1236 | 1305 | |
|---|
| 1237 | 1306 | spin_lock(&fs_info->trans_lock); |
|---|
| 1238 | | - if (list_empty(&root->root_list)) |
|---|
| 1307 | + if (list_empty(&root->root_list)) { |
|---|
| 1308 | + btrfs_grab_root(root); |
|---|
| 1239 | 1309 | list_add_tail(&root->root_list, &fs_info->dead_roots); |
|---|
| 1310 | + } |
|---|
| 1240 | 1311 | spin_unlock(&fs_info->trans_lock); |
|---|
| 1241 | 1312 | } |
|---|
| 1242 | 1313 | |
|---|
| .. | .. |
|---|
| 1360 | 1431 | return 0; |
|---|
| 1361 | 1432 | |
|---|
| 1362 | 1433 | /* |
|---|
| 1363 | | - * Ensure dirty @src will be commited. Or, after comming |
|---|
| 1434 | + * Ensure dirty @src will be committed. Or, after coming |
|---|
| 1364 | 1435 | * commit_fs_roots() and switch_commit_roots(), any dirty but not |
|---|
| 1365 | 1436 | * recorded root will never be updated again, causing an outdated root |
|---|
| 1366 | 1437 | * item. |
|---|
| .. | .. |
|---|
| 1401 | 1472 | ret = commit_cowonly_roots(trans); |
|---|
| 1402 | 1473 | if (ret) |
|---|
| 1403 | 1474 | goto out; |
|---|
| 1404 | | - switch_commit_roots(trans->transaction); |
|---|
| 1475 | + switch_commit_roots(trans); |
|---|
| 1405 | 1476 | ret = btrfs_write_and_wait_transaction(trans); |
|---|
| 1406 | 1477 | if (ret) |
|---|
| 1407 | 1478 | btrfs_handle_fs_error(fs_info, ret, |
|---|
| .. | .. |
|---|
| 1453 | 1524 | u64 index = 0; |
|---|
| 1454 | 1525 | u64 objectid; |
|---|
| 1455 | 1526 | u64 root_flags; |
|---|
| 1456 | | - uuid_le new_uuid; |
|---|
| 1457 | 1527 | |
|---|
| 1458 | 1528 | ASSERT(pending->path); |
|---|
| 1459 | 1529 | path = pending->path; |
|---|
| .. | .. |
|---|
| 1546 | 1616 | |
|---|
| 1547 | 1617 | btrfs_set_root_generation_v2(new_root_item, |
|---|
| 1548 | 1618 | trans->transid); |
|---|
| 1549 | | - uuid_le_gen(&new_uuid); |
|---|
| 1550 | | - memcpy(new_root_item->uuid, new_uuid.b, BTRFS_UUID_SIZE); |
|---|
| 1619 | + generate_random_guid(new_root_item->uuid); |
|---|
| 1551 | 1620 | memcpy(new_root_item->parent_uuid, root->root_item.uuid, |
|---|
| 1552 | 1621 | BTRFS_UUID_SIZE); |
|---|
| 1553 | 1622 | if (!(root_flags & BTRFS_ROOT_SUBVOL_RDONLY)) { |
|---|
| .. | .. |
|---|
| 1563 | 1632 | btrfs_set_root_otransid(new_root_item, trans->transid); |
|---|
| 1564 | 1633 | |
|---|
| 1565 | 1634 | old = btrfs_lock_root_node(root); |
|---|
| 1566 | | - ret = btrfs_cow_block(trans, root, old, NULL, 0, &old); |
|---|
| 1635 | + ret = btrfs_cow_block(trans, root, old, NULL, 0, &old, |
|---|
| 1636 | + BTRFS_NESTING_COW); |
|---|
| 1567 | 1637 | if (ret) { |
|---|
| 1568 | 1638 | btrfs_tree_unlock(old); |
|---|
| 1569 | 1639 | free_extent_buffer(old); |
|---|
| .. | .. |
|---|
| 1571 | 1641 | goto fail; |
|---|
| 1572 | 1642 | } |
|---|
| 1573 | 1643 | |
|---|
| 1574 | | - btrfs_set_lock_blocking(old); |
|---|
| 1644 | + btrfs_set_lock_blocking_write(old); |
|---|
| 1575 | 1645 | |
|---|
| 1576 | 1646 | ret = btrfs_copy_root(trans, root, old, &tmp, objectid); |
|---|
| 1577 | 1647 | /* clean up in any case */ |
|---|
| .. | .. |
|---|
| 1609 | 1679 | } |
|---|
| 1610 | 1680 | |
|---|
| 1611 | 1681 | key.offset = (u64)-1; |
|---|
| 1612 | | - pending->snap = btrfs_read_fs_root_no_name(fs_info, &key); |
|---|
| 1682 | + pending->snap = btrfs_get_new_fs_root(fs_info, objectid, pending->anon_dev); |
|---|
| 1613 | 1683 | if (IS_ERR(pending->snap)) { |
|---|
| 1614 | 1684 | ret = PTR_ERR(pending->snap); |
|---|
| 1685 | + pending->snap = NULL; |
|---|
| 1615 | 1686 | btrfs_abort_transaction(trans, ret); |
|---|
| 1616 | 1687 | goto fail; |
|---|
| 1617 | 1688 | } |
|---|
| .. | .. |
|---|
| 1639 | 1710 | if (ret < 0) |
|---|
| 1640 | 1711 | goto fail; |
|---|
| 1641 | 1712 | |
|---|
| 1642 | | - ret = btrfs_insert_dir_item(trans, parent_root, |
|---|
| 1643 | | - dentry->d_name.name, dentry->d_name.len, |
|---|
| 1644 | | - BTRFS_I(parent_inode), &key, |
|---|
| 1645 | | - BTRFS_FT_DIR, index); |
|---|
| 1713 | + ret = btrfs_insert_dir_item(trans, dentry->d_name.name, |
|---|
| 1714 | + dentry->d_name.len, BTRFS_I(parent_inode), |
|---|
| 1715 | + &key, BTRFS_FT_DIR, index); |
|---|
| 1646 | 1716 | /* We have check then name at the beginning, so it is impossible. */ |
|---|
| 1647 | 1717 | BUG_ON(ret == -EEXIST || ret == -EOVERFLOW); |
|---|
| 1648 | 1718 | if (ret) { |
|---|
| .. | .. |
|---|
| 1659 | 1729 | btrfs_abort_transaction(trans, ret); |
|---|
| 1660 | 1730 | goto fail; |
|---|
| 1661 | 1731 | } |
|---|
| 1662 | | - ret = btrfs_uuid_tree_add(trans, new_uuid.b, BTRFS_UUID_KEY_SUBVOL, |
|---|
| 1732 | + ret = btrfs_uuid_tree_add(trans, new_root_item->uuid, |
|---|
| 1733 | + BTRFS_UUID_KEY_SUBVOL, |
|---|
| 1663 | 1734 | objectid); |
|---|
| 1664 | 1735 | if (ret) { |
|---|
| 1665 | 1736 | btrfs_abort_transaction(trans, ret); |
|---|
| .. | .. |
|---|
| 1771 | 1842 | struct btrfs_transaction *trans) |
|---|
| 1772 | 1843 | { |
|---|
| 1773 | 1844 | wait_event(fs_info->transaction_blocked_wait, |
|---|
| 1774 | | - trans->state >= TRANS_STATE_COMMIT_START || trans->aborted); |
|---|
| 1845 | + trans->state >= TRANS_STATE_COMMIT_START || |
|---|
| 1846 | + TRANS_ABORTED(trans)); |
|---|
| 1775 | 1847 | } |
|---|
| 1776 | 1848 | |
|---|
| 1777 | 1849 | /* |
|---|
| .. | .. |
|---|
| 1783 | 1855 | struct btrfs_transaction *trans) |
|---|
| 1784 | 1856 | { |
|---|
| 1785 | 1857 | wait_event(fs_info->transaction_wait, |
|---|
| 1786 | | - trans->state >= TRANS_STATE_UNBLOCKED || trans->aborted); |
|---|
| 1858 | + trans->state >= TRANS_STATE_UNBLOCKED || |
|---|
| 1859 | + TRANS_ABORTED(trans)); |
|---|
| 1787 | 1860 | } |
|---|
| 1788 | 1861 | |
|---|
| 1789 | 1862 | /* |
|---|
| .. | .. |
|---|
| 1865 | 1938 | { |
|---|
| 1866 | 1939 | struct btrfs_fs_info *fs_info = trans->fs_info; |
|---|
| 1867 | 1940 | struct btrfs_transaction *cur_trans = trans->transaction; |
|---|
| 1868 | | - DEFINE_WAIT(wait); |
|---|
| 1869 | 1941 | |
|---|
| 1870 | 1942 | WARN_ON(refcount_read(&trans->use_count) > 1); |
|---|
| 1871 | 1943 | |
|---|
| .. | .. |
|---|
| 1880 | 1952 | */ |
|---|
| 1881 | 1953 | BUG_ON(list_empty(&cur_trans->list)); |
|---|
| 1882 | 1954 | |
|---|
| 1883 | | - list_del_init(&cur_trans->list); |
|---|
| 1884 | 1955 | if (cur_trans == fs_info->running_transaction) { |
|---|
| 1885 | 1956 | cur_trans->state = TRANS_STATE_COMMIT_DOING; |
|---|
| 1886 | 1957 | spin_unlock(&fs_info->trans_lock); |
|---|
| .. | .. |
|---|
| 1889 | 1960 | |
|---|
| 1890 | 1961 | spin_lock(&fs_info->trans_lock); |
|---|
| 1891 | 1962 | } |
|---|
| 1963 | + |
|---|
| 1964 | + /* |
|---|
| 1965 | + * Now that we know no one else is still using the transaction we can |
|---|
| 1966 | + * remove the transaction from the list of transactions. This avoids |
|---|
| 1967 | + * the transaction kthread from cleaning up the transaction while some |
|---|
| 1968 | + * other task is still using it, which could result in a use-after-free |
|---|
| 1969 | + * on things like log trees, as it forces the transaction kthread to |
|---|
| 1970 | + * wait for this transaction to be cleaned up by us. |
|---|
| 1971 | + */ |
|---|
| 1972 | + list_del_init(&cur_trans->list); |
|---|
| 1973 | + |
|---|
| 1892 | 1974 | spin_unlock(&fs_info->trans_lock); |
|---|
| 1893 | 1975 | |
|---|
| 1894 | 1976 | btrfs_cleanup_one_transaction(trans->transaction, fs_info); |
|---|
| .. | .. |
|---|
| 1912 | 1994 | kmem_cache_free(btrfs_trans_handle_cachep, trans); |
|---|
| 1913 | 1995 | } |
|---|
| 1914 | 1996 | |
|---|
| 1915 | | -static inline int btrfs_start_delalloc_flush(struct btrfs_fs_info *fs_info) |
|---|
| 1997 | +/* |
|---|
| 1998 | + * Release reserved delayed ref space of all pending block groups of the |
|---|
| 1999 | + * transaction and remove them from the list |
|---|
| 2000 | + */ |
|---|
| 2001 | +static void btrfs_cleanup_pending_block_groups(struct btrfs_trans_handle *trans) |
|---|
| 1916 | 2002 | { |
|---|
| 2003 | + struct btrfs_fs_info *fs_info = trans->fs_info; |
|---|
| 2004 | + struct btrfs_block_group *block_group, *tmp; |
|---|
| 2005 | + |
|---|
| 2006 | + list_for_each_entry_safe(block_group, tmp, &trans->new_bgs, bg_list) { |
|---|
| 2007 | + btrfs_delayed_refs_rsv_release(fs_info, 1); |
|---|
| 2008 | + list_del_init(&block_group->bg_list); |
|---|
| 2009 | + } |
|---|
| 2010 | +} |
|---|
| 2011 | + |
|---|
| 2012 | +static inline int btrfs_start_delalloc_flush(struct btrfs_trans_handle *trans) |
|---|
| 2013 | +{ |
|---|
| 2014 | + struct btrfs_fs_info *fs_info = trans->fs_info; |
|---|
| 2015 | + |
|---|
| 1917 | 2016 | /* |
|---|
| 1918 | 2017 | * We use writeback_inodes_sb here because if we used |
|---|
| 1919 | 2018 | * btrfs_start_delalloc_roots we would deadlock with fs freeze. |
|---|
| .. | .. |
|---|
| 1923 | 2022 | * from already being in a transaction and our join_transaction doesn't |
|---|
| 1924 | 2023 | * have to re-take the fs freeze lock. |
|---|
| 1925 | 2024 | */ |
|---|
| 1926 | | - if (btrfs_test_opt(fs_info, FLUSHONCOMMIT)) |
|---|
| 2025 | + if (btrfs_test_opt(fs_info, FLUSHONCOMMIT)) { |
|---|
| 1927 | 2026 | writeback_inodes_sb(fs_info->sb, WB_REASON_SYNC); |
|---|
| 2027 | + } else { |
|---|
| 2028 | + struct btrfs_pending_snapshot *pending; |
|---|
| 2029 | + struct list_head *head = &trans->transaction->pending_snapshots; |
|---|
| 2030 | + |
|---|
| 2031 | + /* |
|---|
| 2032 | + * Flush dellaloc for any root that is going to be snapshotted. |
|---|
| 2033 | + * This is done to avoid a corrupted version of files, in the |
|---|
| 2034 | + * snapshots, that had both buffered and direct IO writes (even |
|---|
| 2035 | + * if they were done sequentially) due to an unordered update of |
|---|
| 2036 | + * the inode's size on disk. |
|---|
| 2037 | + */ |
|---|
| 2038 | + list_for_each_entry(pending, head, list) { |
|---|
| 2039 | + int ret; |
|---|
| 2040 | + |
|---|
| 2041 | + ret = btrfs_start_delalloc_snapshot(pending->root); |
|---|
| 2042 | + if (ret) |
|---|
| 2043 | + return ret; |
|---|
| 2044 | + } |
|---|
| 2045 | + } |
|---|
| 1928 | 2046 | return 0; |
|---|
| 1929 | 2047 | } |
|---|
| 1930 | 2048 | |
|---|
| 1931 | | -static inline void btrfs_wait_delalloc_flush(struct btrfs_fs_info *fs_info) |
|---|
| 2049 | +static inline void btrfs_wait_delalloc_flush(struct btrfs_trans_handle *trans) |
|---|
| 1932 | 2050 | { |
|---|
| 1933 | | - if (btrfs_test_opt(fs_info, FLUSHONCOMMIT)) |
|---|
| 1934 | | - btrfs_wait_ordered_roots(fs_info, U64_MAX, 0, (u64)-1); |
|---|
| 1935 | | -} |
|---|
| 2051 | + struct btrfs_fs_info *fs_info = trans->fs_info; |
|---|
| 1936 | 2052 | |
|---|
| 1937 | | -static inline void |
|---|
| 1938 | | -btrfs_wait_pending_ordered(struct btrfs_transaction *cur_trans) |
|---|
| 1939 | | -{ |
|---|
| 1940 | | - wait_event(cur_trans->pending_wait, |
|---|
| 1941 | | - atomic_read(&cur_trans->pending_ordered) == 0); |
|---|
| 2053 | + if (btrfs_test_opt(fs_info, FLUSHONCOMMIT)) { |
|---|
| 2054 | + btrfs_wait_ordered_roots(fs_info, U64_MAX, 0, (u64)-1); |
|---|
| 2055 | + } else { |
|---|
| 2056 | + struct btrfs_pending_snapshot *pending; |
|---|
| 2057 | + struct list_head *head = &trans->transaction->pending_snapshots; |
|---|
| 2058 | + |
|---|
| 2059 | + /* |
|---|
| 2060 | + * Wait for any dellaloc that we started previously for the roots |
|---|
| 2061 | + * that are going to be snapshotted. This is to avoid a corrupted |
|---|
| 2062 | + * version of files in the snapshots that had both buffered and |
|---|
| 2063 | + * direct IO writes (even if they were done sequentially). |
|---|
| 2064 | + */ |
|---|
| 2065 | + list_for_each_entry(pending, head, list) |
|---|
| 2066 | + btrfs_wait_ordered_extents(pending->root, |
|---|
| 2067 | + U64_MAX, 0, U64_MAX); |
|---|
| 2068 | + } |
|---|
| 1942 | 2069 | } |
|---|
| 1943 | 2070 | |
|---|
| 1944 | 2071 | int btrfs_commit_transaction(struct btrfs_trans_handle *trans) |
|---|
| .. | .. |
|---|
| 1947 | 2074 | struct btrfs_transaction *cur_trans = trans->transaction; |
|---|
| 1948 | 2075 | struct btrfs_transaction *prev_trans = NULL; |
|---|
| 1949 | 2076 | int ret; |
|---|
| 2077 | + |
|---|
| 2078 | + ASSERT(refcount_read(&trans->use_count) == 1); |
|---|
| 1950 | 2079 | |
|---|
| 1951 | 2080 | /* |
|---|
| 1952 | 2081 | * Some places just start a transaction to commit it. We need to make |
|---|
| .. | .. |
|---|
| 1957 | 2086 | trans->dirty = true; |
|---|
| 1958 | 2087 | |
|---|
| 1959 | 2088 | /* Stop the commit early if ->aborted is set */ |
|---|
| 1960 | | - if (unlikely(READ_ONCE(cur_trans->aborted))) { |
|---|
| 2089 | + if (TRANS_ABORTED(cur_trans)) { |
|---|
| 1961 | 2090 | ret = cur_trans->aborted; |
|---|
| 1962 | 2091 | btrfs_end_transaction(trans); |
|---|
| 1963 | 2092 | return ret; |
|---|
| .. | .. |
|---|
| 1984 | 2113 | cur_trans->delayed_refs.flushing = 1; |
|---|
| 1985 | 2114 | smp_wmb(); |
|---|
| 1986 | 2115 | |
|---|
| 1987 | | - if (!list_empty(&trans->new_bgs)) |
|---|
| 1988 | | - btrfs_create_pending_block_groups(trans); |
|---|
| 2116 | + btrfs_create_pending_block_groups(trans); |
|---|
| 1989 | 2117 | |
|---|
| 1990 | 2118 | ret = btrfs_run_delayed_refs(trans, 0); |
|---|
| 1991 | 2119 | if (ret) { |
|---|
| .. | .. |
|---|
| 2032 | 2160 | |
|---|
| 2033 | 2161 | wait_for_commit(cur_trans); |
|---|
| 2034 | 2162 | |
|---|
| 2035 | | - if (unlikely(cur_trans->aborted)) |
|---|
| 2163 | + if (TRANS_ABORTED(cur_trans)) |
|---|
| 2036 | 2164 | ret = cur_trans->aborted; |
|---|
| 2037 | 2165 | |
|---|
| 2038 | 2166 | btrfs_put_transaction(cur_trans); |
|---|
| .. | .. |
|---|
| 2051 | 2179 | spin_unlock(&fs_info->trans_lock); |
|---|
| 2052 | 2180 | |
|---|
| 2053 | 2181 | wait_for_commit(prev_trans); |
|---|
| 2054 | | - ret = prev_trans->aborted; |
|---|
| 2182 | + ret = READ_ONCE(prev_trans->aborted); |
|---|
| 2055 | 2183 | |
|---|
| 2056 | 2184 | btrfs_put_transaction(prev_trans); |
|---|
| 2057 | 2185 | if (ret) |
|---|
| .. | .. |
|---|
| 2075 | 2203 | |
|---|
| 2076 | 2204 | extwriter_counter_dec(cur_trans, trans->type); |
|---|
| 2077 | 2205 | |
|---|
| 2078 | | - ret = btrfs_start_delalloc_flush(fs_info); |
|---|
| 2206 | + ret = btrfs_start_delalloc_flush(trans); |
|---|
| 2079 | 2207 | if (ret) |
|---|
| 2080 | 2208 | goto cleanup_transaction; |
|---|
| 2081 | 2209 | |
|---|
| .. | .. |
|---|
| 2091 | 2219 | if (ret) |
|---|
| 2092 | 2220 | goto cleanup_transaction; |
|---|
| 2093 | 2221 | |
|---|
| 2094 | | - btrfs_wait_delalloc_flush(fs_info); |
|---|
| 2222 | + btrfs_wait_delalloc_flush(trans); |
|---|
| 2095 | 2223 | |
|---|
| 2096 | | - btrfs_wait_pending_ordered(cur_trans); |
|---|
| 2224 | + /* |
|---|
| 2225 | + * Wait for all ordered extents started by a fast fsync that joined this |
|---|
| 2226 | + * transaction. Otherwise if this transaction commits before the ordered |
|---|
| 2227 | + * extents complete we lose logged data after a power failure. |
|---|
| 2228 | + */ |
|---|
| 2229 | + wait_event(cur_trans->pending_wait, |
|---|
| 2230 | + atomic_read(&cur_trans->pending_ordered) == 0); |
|---|
| 2097 | 2231 | |
|---|
| 2098 | 2232 | btrfs_scrub_pause(fs_info); |
|---|
| 2099 | 2233 | /* |
|---|
| .. | .. |
|---|
| 2107 | 2241 | wait_event(cur_trans->writer_wait, |
|---|
| 2108 | 2242 | atomic_read(&cur_trans->num_writers) == 1); |
|---|
| 2109 | 2243 | |
|---|
| 2110 | | - /* ->aborted might be set after the previous check, so check it */ |
|---|
| 2111 | | - if (unlikely(READ_ONCE(cur_trans->aborted))) { |
|---|
| 2244 | + if (TRANS_ABORTED(cur_trans)) { |
|---|
| 2112 | 2245 | ret = cur_trans->aborted; |
|---|
| 2113 | 2246 | goto scrub_continue; |
|---|
| 2114 | 2247 | } |
|---|
| .. | .. |
|---|
| 2125 | 2258 | * core function of the snapshot creation. |
|---|
| 2126 | 2259 | */ |
|---|
| 2127 | 2260 | ret = create_pending_snapshots(trans); |
|---|
| 2128 | | - if (ret) { |
|---|
| 2129 | | - mutex_unlock(&fs_info->reloc_mutex); |
|---|
| 2130 | | - goto scrub_continue; |
|---|
| 2131 | | - } |
|---|
| 2261 | + if (ret) |
|---|
| 2262 | + goto unlock_reloc; |
|---|
| 2132 | 2263 | |
|---|
| 2133 | 2264 | /* |
|---|
| 2134 | 2265 | * We insert the dir indexes of the snapshots and update the inode |
|---|
| .. | .. |
|---|
| 2141 | 2272 | * the nodes and leaves. |
|---|
| 2142 | 2273 | */ |
|---|
| 2143 | 2274 | ret = btrfs_run_delayed_items(trans); |
|---|
| 2144 | | - if (ret) { |
|---|
| 2145 | | - mutex_unlock(&fs_info->reloc_mutex); |
|---|
| 2146 | | - goto scrub_continue; |
|---|
| 2147 | | - } |
|---|
| 2275 | + if (ret) |
|---|
| 2276 | + goto unlock_reloc; |
|---|
| 2148 | 2277 | |
|---|
| 2149 | 2278 | ret = btrfs_run_delayed_refs(trans, (unsigned long)-1); |
|---|
| 2150 | | - if (ret) { |
|---|
| 2151 | | - mutex_unlock(&fs_info->reloc_mutex); |
|---|
| 2152 | | - goto scrub_continue; |
|---|
| 2153 | | - } |
|---|
| 2279 | + if (ret) |
|---|
| 2280 | + goto unlock_reloc; |
|---|
| 2154 | 2281 | |
|---|
| 2155 | 2282 | /* |
|---|
| 2156 | 2283 | * make sure none of the code above managed to slip in a |
|---|
| .. | .. |
|---|
| 2176 | 2303 | mutex_lock(&fs_info->tree_log_mutex); |
|---|
| 2177 | 2304 | |
|---|
| 2178 | 2305 | ret = commit_fs_roots(trans); |
|---|
| 2179 | | - if (ret) { |
|---|
| 2180 | | - mutex_unlock(&fs_info->tree_log_mutex); |
|---|
| 2181 | | - mutex_unlock(&fs_info->reloc_mutex); |
|---|
| 2182 | | - goto scrub_continue; |
|---|
| 2183 | | - } |
|---|
| 2306 | + if (ret) |
|---|
| 2307 | + goto unlock_tree_log; |
|---|
| 2184 | 2308 | |
|---|
| 2185 | 2309 | /* |
|---|
| 2186 | 2310 | * Since the transaction is done, we can apply the pending changes |
|---|
| .. | .. |
|---|
| 2198 | 2322 | * new delayed refs. Must handle them or qgroup can be wrong. |
|---|
| 2199 | 2323 | */ |
|---|
| 2200 | 2324 | ret = btrfs_run_delayed_refs(trans, (unsigned long)-1); |
|---|
| 2201 | | - if (ret) { |
|---|
| 2202 | | - mutex_unlock(&fs_info->tree_log_mutex); |
|---|
| 2203 | | - mutex_unlock(&fs_info->reloc_mutex); |
|---|
| 2204 | | - goto scrub_continue; |
|---|
| 2205 | | - } |
|---|
| 2325 | + if (ret) |
|---|
| 2326 | + goto unlock_tree_log; |
|---|
| 2206 | 2327 | |
|---|
| 2207 | 2328 | /* |
|---|
| 2208 | 2329 | * Since fs roots are all committed, we can get a quite accurate |
|---|
| 2209 | 2330 | * new_roots. So let's do quota accounting. |
|---|
| 2210 | 2331 | */ |
|---|
| 2211 | 2332 | ret = btrfs_qgroup_account_extents(trans); |
|---|
| 2212 | | - if (ret < 0) { |
|---|
| 2213 | | - mutex_unlock(&fs_info->tree_log_mutex); |
|---|
| 2214 | | - mutex_unlock(&fs_info->reloc_mutex); |
|---|
| 2215 | | - goto scrub_continue; |
|---|
| 2216 | | - } |
|---|
| 2333 | + if (ret < 0) |
|---|
| 2334 | + goto unlock_tree_log; |
|---|
| 2217 | 2335 | |
|---|
| 2218 | 2336 | ret = commit_cowonly_roots(trans); |
|---|
| 2219 | | - if (ret) { |
|---|
| 2220 | | - mutex_unlock(&fs_info->tree_log_mutex); |
|---|
| 2221 | | - mutex_unlock(&fs_info->reloc_mutex); |
|---|
| 2222 | | - goto scrub_continue; |
|---|
| 2223 | | - } |
|---|
| 2337 | + if (ret) |
|---|
| 2338 | + goto unlock_tree_log; |
|---|
| 2224 | 2339 | |
|---|
| 2225 | 2340 | /* |
|---|
| 2226 | 2341 | * The tasks which save the space cache and inode cache may also |
|---|
| 2227 | 2342 | * update ->aborted, check it. |
|---|
| 2228 | 2343 | */ |
|---|
| 2229 | | - if (unlikely(READ_ONCE(cur_trans->aborted))) { |
|---|
| 2344 | + if (TRANS_ABORTED(cur_trans)) { |
|---|
| 2230 | 2345 | ret = cur_trans->aborted; |
|---|
| 2231 | | - mutex_unlock(&fs_info->tree_log_mutex); |
|---|
| 2232 | | - mutex_unlock(&fs_info->reloc_mutex); |
|---|
| 2233 | | - goto scrub_continue; |
|---|
| 2346 | + goto unlock_tree_log; |
|---|
| 2234 | 2347 | } |
|---|
| 2235 | | - |
|---|
| 2236 | | - btrfs_prepare_extent_commit(fs_info); |
|---|
| 2237 | 2348 | |
|---|
| 2238 | 2349 | cur_trans = fs_info->running_transaction; |
|---|
| 2239 | 2350 | |
|---|
| .. | .. |
|---|
| 2247 | 2358 | list_add_tail(&fs_info->chunk_root->dirty_list, |
|---|
| 2248 | 2359 | &cur_trans->switch_commits); |
|---|
| 2249 | 2360 | |
|---|
| 2250 | | - switch_commit_roots(cur_trans); |
|---|
| 2361 | + switch_commit_roots(trans); |
|---|
| 2251 | 2362 | |
|---|
| 2252 | 2363 | ASSERT(list_empty(&cur_trans->dirty_bgs)); |
|---|
| 2253 | 2364 | ASSERT(list_empty(&cur_trans->io_bgs)); |
|---|
| .. | .. |
|---|
| 2258 | 2369 | memcpy(fs_info->super_for_commit, fs_info->super_copy, |
|---|
| 2259 | 2370 | sizeof(*fs_info->super_copy)); |
|---|
| 2260 | 2371 | |
|---|
| 2261 | | - btrfs_update_commit_device_size(fs_info); |
|---|
| 2262 | | - btrfs_update_commit_device_bytes_used(cur_trans); |
|---|
| 2372 | + btrfs_commit_device_sizes(cur_trans); |
|---|
| 2263 | 2373 | |
|---|
| 2264 | 2374 | clear_bit(BTRFS_FS_LOG1_ERR, &fs_info->flags); |
|---|
| 2265 | 2375 | clear_bit(BTRFS_FS_LOG2_ERR, &fs_info->flags); |
|---|
| .. | .. |
|---|
| 2278 | 2388 | if (ret) { |
|---|
| 2279 | 2389 | btrfs_handle_fs_error(fs_info, ret, |
|---|
| 2280 | 2390 | "Error while writing out transaction"); |
|---|
| 2391 | + /* |
|---|
| 2392 | + * reloc_mutex has been unlocked, tree_log_mutex is still held |
|---|
| 2393 | + * but we can't jump to unlock_tree_log causing double unlock |
|---|
| 2394 | + */ |
|---|
| 2281 | 2395 | mutex_unlock(&fs_info->tree_log_mutex); |
|---|
| 2282 | 2396 | goto scrub_continue; |
|---|
| 2283 | 2397 | } |
|---|
| .. | .. |
|---|
| 2303 | 2417 | */ |
|---|
| 2304 | 2418 | cur_trans->state = TRANS_STATE_COMPLETED; |
|---|
| 2305 | 2419 | wake_up(&cur_trans->commit_wait); |
|---|
| 2306 | | - clear_bit(BTRFS_FS_NEED_ASYNC_COMMIT, &fs_info->flags); |
|---|
| 2307 | 2420 | |
|---|
| 2308 | 2421 | spin_lock(&fs_info->trans_lock); |
|---|
| 2309 | 2422 | list_del_init(&cur_trans->list); |
|---|
| .. | .. |
|---|
| 2326 | 2439 | |
|---|
| 2327 | 2440 | return ret; |
|---|
| 2328 | 2441 | |
|---|
| 2442 | +unlock_tree_log: |
|---|
| 2443 | + mutex_unlock(&fs_info->tree_log_mutex); |
|---|
| 2444 | +unlock_reloc: |
|---|
| 2445 | + mutex_unlock(&fs_info->reloc_mutex); |
|---|
| 2329 | 2446 | scrub_continue: |
|---|
| 2330 | 2447 | btrfs_scrub_continue(fs_info); |
|---|
| 2331 | 2448 | cleanup_transaction: |
|---|
| 2332 | 2449 | btrfs_trans_release_metadata(trans); |
|---|
| 2450 | + btrfs_cleanup_pending_block_groups(trans); |
|---|
| 2333 | 2451 | btrfs_trans_release_chunk_metadata(trans); |
|---|
| 2334 | 2452 | trans->block_rsv = NULL; |
|---|
| 2335 | 2453 | btrfs_warn(fs_info, "Skipping commit of aborted transaction."); |
|---|
| .. | .. |
|---|
| 2365 | 2483 | list_del_init(&root->root_list); |
|---|
| 2366 | 2484 | spin_unlock(&fs_info->trans_lock); |
|---|
| 2367 | 2485 | |
|---|
| 2368 | | - btrfs_debug(fs_info, "cleaner removing %llu", root->objectid); |
|---|
| 2486 | + btrfs_debug(fs_info, "cleaner removing %llu", root->root_key.objectid); |
|---|
| 2369 | 2487 | |
|---|
| 2370 | 2488 | btrfs_kill_all_delayed_nodes(root); |
|---|
| 2489 | + if (root->ino_cache_inode) { |
|---|
| 2490 | + iput(root->ino_cache_inode); |
|---|
| 2491 | + root->ino_cache_inode = NULL; |
|---|
| 2492 | + } |
|---|
| 2371 | 2493 | |
|---|
| 2372 | 2494 | if (btrfs_header_backref_rev(root->node) < |
|---|
| 2373 | 2495 | BTRFS_MIXED_BACKREF_REV) |
|---|
| 2374 | | - ret = btrfs_drop_snapshot(root, NULL, 0, 0); |
|---|
| 2496 | + ret = btrfs_drop_snapshot(root, 0, 0); |
|---|
| 2375 | 2497 | else |
|---|
| 2376 | | - ret = btrfs_drop_snapshot(root, NULL, 1, 0); |
|---|
| 2498 | + ret = btrfs_drop_snapshot(root, 1, 0); |
|---|
| 2377 | 2499 | |
|---|
| 2500 | + btrfs_put_root(root); |
|---|
| 2378 | 2501 | return (ret < 0) ? 0 : 1; |
|---|
| 2379 | 2502 | } |
|---|
| 2380 | 2503 | |
|---|