| .. | .. |
|---|
| 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 | } |
|---|
| .. | .. |
|---|
| 200 | 301 | spin_unlock(&fs_info->trans_lock); |
|---|
| 201 | 302 | |
|---|
| 202 | 303 | /* |
|---|
| 203 | | - * If we are ATTACH, we just want to catch the current transaction, |
|---|
| 204 | | - * and commit it. If there is no transaction, just return ENOENT. |
|---|
| 304 | + * If we are ATTACH or TRANS_JOIN_NOSTART, we just want to catch the |
|---|
| 305 | + * current transaction, and commit it. If there is no transaction, just |
|---|
| 306 | + * return ENOENT. |
|---|
| 205 | 307 | */ |
|---|
| 206 | | - if (type == TRANS_ATTACH) |
|---|
| 308 | + if (type == TRANS_ATTACH || type == TRANS_JOIN_NOSTART) |
|---|
| 207 | 309 | return -ENOENT; |
|---|
| 208 | 310 | |
|---|
| 209 | 311 | /* |
|---|
| .. | .. |
|---|
| 231 | 333 | } |
|---|
| 232 | 334 | |
|---|
| 233 | 335 | cur_trans->fs_info = fs_info; |
|---|
| 336 | + atomic_set(&cur_trans->pending_ordered, 0); |
|---|
| 337 | + init_waitqueue_head(&cur_trans->pending_wait); |
|---|
| 234 | 338 | atomic_set(&cur_trans->num_writers, 1); |
|---|
| 235 | 339 | extwriter_counter_init(cur_trans, type); |
|---|
| 236 | 340 | init_waitqueue_head(&cur_trans->writer_wait); |
|---|
| 237 | 341 | init_waitqueue_head(&cur_trans->commit_wait); |
|---|
| 238 | | - init_waitqueue_head(&cur_trans->pending_wait); |
|---|
| 239 | 342 | cur_trans->state = TRANS_STATE_RUNNING; |
|---|
| 240 | 343 | /* |
|---|
| 241 | 344 | * One for this trans handle, one so it will live on until we |
|---|
| 242 | 345 | * commit the transaction. |
|---|
| 243 | 346 | */ |
|---|
| 244 | 347 | refcount_set(&cur_trans->use_count, 2); |
|---|
| 245 | | - atomic_set(&cur_trans->pending_ordered, 0); |
|---|
| 246 | 348 | cur_trans->flags = 0; |
|---|
| 247 | 349 | cur_trans->start_time = ktime_get_seconds(); |
|---|
| 248 | 350 | |
|---|
| 249 | 351 | memset(&cur_trans->delayed_refs, 0, sizeof(cur_trans->delayed_refs)); |
|---|
| 250 | 352 | |
|---|
| 251 | | - cur_trans->delayed_refs.href_root = RB_ROOT; |
|---|
| 353 | + cur_trans->delayed_refs.href_root = RB_ROOT_CACHED; |
|---|
| 252 | 354 | cur_trans->delayed_refs.dirty_extent_root = RB_ROOT; |
|---|
| 253 | 355 | atomic_set(&cur_trans->delayed_refs.num_entries, 0); |
|---|
| 254 | 356 | |
|---|
| .. | .. |
|---|
| 266 | 368 | spin_lock_init(&cur_trans->delayed_refs.lock); |
|---|
| 267 | 369 | |
|---|
| 268 | 370 | INIT_LIST_HEAD(&cur_trans->pending_snapshots); |
|---|
| 269 | | - INIT_LIST_HEAD(&cur_trans->pending_chunks); |
|---|
| 371 | + INIT_LIST_HEAD(&cur_trans->dev_update_list); |
|---|
| 270 | 372 | INIT_LIST_HEAD(&cur_trans->switch_commits); |
|---|
| 271 | 373 | INIT_LIST_HEAD(&cur_trans->dirty_bgs); |
|---|
| 272 | 374 | INIT_LIST_HEAD(&cur_trans->io_bgs); |
|---|
| 273 | 375 | INIT_LIST_HEAD(&cur_trans->dropped_roots); |
|---|
| 274 | 376 | mutex_init(&cur_trans->cache_write_mutex); |
|---|
| 275 | | - cur_trans->num_dirty_bgs = 0; |
|---|
| 276 | 377 | spin_lock_init(&cur_trans->dirty_bgs_lock); |
|---|
| 277 | 378 | INIT_LIST_HEAD(&cur_trans->deleted_bgs); |
|---|
| 278 | 379 | spin_lock_init(&cur_trans->dropped_roots_lock); |
|---|
| 279 | 380 | list_add_tail(&cur_trans->list, &fs_info->trans_list); |
|---|
| 280 | | - extent_io_tree_init(&cur_trans->dirty_pages, |
|---|
| 281 | | - fs_info->btree_inode); |
|---|
| 381 | + extent_io_tree_init(fs_info, &cur_trans->dirty_pages, |
|---|
| 382 | + IO_TREE_TRANS_DIRTY_PAGES, fs_info->btree_inode); |
|---|
| 383 | + extent_io_tree_init(fs_info, &cur_trans->pinned_extents, |
|---|
| 384 | + IO_TREE_FS_PINNED_EXTENTS, NULL); |
|---|
| 282 | 385 | fs_info->generation++; |
|---|
| 283 | 386 | cur_trans->transid = fs_info->generation; |
|---|
| 284 | 387 | fs_info->running_transaction = cur_trans; |
|---|
| .. | .. |
|---|
| 289 | 392 | } |
|---|
| 290 | 393 | |
|---|
| 291 | 394 | /* |
|---|
| 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 |
|---|
| 395 | + * This does all the record keeping required to make sure that a shareable root |
|---|
| 396 | + * is properly recorded in a given transaction. This is required to make sure |
|---|
| 397 | + * the old root from before we joined the transaction is deleted when the |
|---|
| 398 | + * transaction commits. |
|---|
| 296 | 399 | */ |
|---|
| 297 | 400 | static int record_root_in_trans(struct btrfs_trans_handle *trans, |
|---|
| 298 | 401 | struct btrfs_root *root, |
|---|
| .. | .. |
|---|
| 300 | 403 | { |
|---|
| 301 | 404 | struct btrfs_fs_info *fs_info = root->fs_info; |
|---|
| 302 | 405 | |
|---|
| 303 | | - if ((test_bit(BTRFS_ROOT_REF_COWS, &root->state) && |
|---|
| 406 | + if ((test_bit(BTRFS_ROOT_SHAREABLE, &root->state) && |
|---|
| 304 | 407 | root->last_trans < trans->transid) || force) { |
|---|
| 305 | 408 | WARN_ON(root == fs_info->extent_root); |
|---|
| 306 | 409 | WARN_ON(!force && root->commit_root != root->node); |
|---|
| .. | .. |
|---|
| 379 | 482 | { |
|---|
| 380 | 483 | struct btrfs_fs_info *fs_info = root->fs_info; |
|---|
| 381 | 484 | |
|---|
| 382 | | - if (!test_bit(BTRFS_ROOT_REF_COWS, &root->state)) |
|---|
| 485 | + if (!test_bit(BTRFS_ROOT_SHAREABLE, &root->state)) |
|---|
| 383 | 486 | return 0; |
|---|
| 384 | 487 | |
|---|
| 385 | 488 | /* |
|---|
| .. | .. |
|---|
| 400 | 503 | |
|---|
| 401 | 504 | static inline int is_transaction_blocked(struct btrfs_transaction *trans) |
|---|
| 402 | 505 | { |
|---|
| 403 | | - return (trans->state >= TRANS_STATE_BLOCKED && |
|---|
| 506 | + return (trans->state >= TRANS_STATE_COMMIT_START && |
|---|
| 404 | 507 | trans->state < TRANS_STATE_UNBLOCKED && |
|---|
| 405 | | - !trans->aborted); |
|---|
| 508 | + !TRANS_ABORTED(trans)); |
|---|
| 406 | 509 | } |
|---|
| 407 | 510 | |
|---|
| 408 | 511 | /* wait for commit against the current transaction to become unblocked |
|---|
| .. | .. |
|---|
| 421 | 524 | |
|---|
| 422 | 525 | wait_event(fs_info->transaction_wait, |
|---|
| 423 | 526 | cur_trans->state >= TRANS_STATE_UNBLOCKED || |
|---|
| 424 | | - cur_trans->aborted); |
|---|
| 527 | + TRANS_ABORTED(cur_trans)); |
|---|
| 425 | 528 | btrfs_put_transaction(cur_trans); |
|---|
| 426 | 529 | } else { |
|---|
| 427 | 530 | spin_unlock(&fs_info->trans_lock); |
|---|
| .. | .. |
|---|
| 444 | 547 | struct btrfs_fs_info *fs_info = root->fs_info; |
|---|
| 445 | 548 | |
|---|
| 446 | 549 | if (!fs_info->reloc_ctl || |
|---|
| 447 | | - !test_bit(BTRFS_ROOT_REF_COWS, &root->state) || |
|---|
| 550 | + !test_bit(BTRFS_ROOT_SHAREABLE, &root->state) || |
|---|
| 448 | 551 | root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID || |
|---|
| 449 | 552 | root->reloc_root) |
|---|
| 450 | 553 | return false; |
|---|
| .. | .. |
|---|
| 458 | 561 | bool enforce_qgroups) |
|---|
| 459 | 562 | { |
|---|
| 460 | 563 | struct btrfs_fs_info *fs_info = root->fs_info; |
|---|
| 461 | | - |
|---|
| 564 | + struct btrfs_block_rsv *delayed_refs_rsv = &fs_info->delayed_refs_rsv; |
|---|
| 462 | 565 | struct btrfs_trans_handle *h; |
|---|
| 463 | 566 | struct btrfs_transaction *cur_trans; |
|---|
| 464 | 567 | u64 num_bytes = 0; |
|---|
| 465 | 568 | u64 qgroup_reserved = 0; |
|---|
| 466 | 569 | bool reloc_reserved = false; |
|---|
| 570 | + bool do_chunk_alloc = false; |
|---|
| 467 | 571 | int ret; |
|---|
| 468 | 572 | |
|---|
| 469 | 573 | /* Send isn't supposed to start transactions. */ |
|---|
| .. | .. |
|---|
| 487 | 591 | * the appropriate flushing if need be. |
|---|
| 488 | 592 | */ |
|---|
| 489 | 593 | if (num_items && root != fs_info->chunk_root) { |
|---|
| 594 | + struct btrfs_block_rsv *rsv = &fs_info->trans_block_rsv; |
|---|
| 595 | + u64 delayed_refs_bytes = 0; |
|---|
| 596 | + |
|---|
| 490 | 597 | qgroup_reserved = num_items * fs_info->nodesize; |
|---|
| 491 | 598 | ret = btrfs_qgroup_reserve_meta_pertrans(root, qgroup_reserved, |
|---|
| 492 | 599 | enforce_qgroups); |
|---|
| 493 | 600 | if (ret) |
|---|
| 494 | 601 | return ERR_PTR(ret); |
|---|
| 495 | 602 | |
|---|
| 496 | | - num_bytes = btrfs_calc_trans_metadata_size(fs_info, num_items); |
|---|
| 603 | + /* |
|---|
| 604 | + * We want to reserve all the bytes we may need all at once, so |
|---|
| 605 | + * we only do 1 enospc flushing cycle per transaction start. We |
|---|
| 606 | + * accomplish this by simply assuming we'll do 2 x num_items |
|---|
| 607 | + * worth of delayed refs updates in this trans handle, and |
|---|
| 608 | + * refill that amount for whatever is missing in the reserve. |
|---|
| 609 | + */ |
|---|
| 610 | + num_bytes = btrfs_calc_insert_metadata_size(fs_info, num_items); |
|---|
| 611 | + if (flush == BTRFS_RESERVE_FLUSH_ALL && |
|---|
| 612 | + delayed_refs_rsv->full == 0) { |
|---|
| 613 | + delayed_refs_bytes = num_bytes; |
|---|
| 614 | + num_bytes <<= 1; |
|---|
| 615 | + } |
|---|
| 616 | + |
|---|
| 497 | 617 | /* |
|---|
| 498 | 618 | * Do the reservation for the relocation root creation |
|---|
| 499 | 619 | */ |
|---|
| .. | .. |
|---|
| 502 | 622 | reloc_reserved = true; |
|---|
| 503 | 623 | } |
|---|
| 504 | 624 | |
|---|
| 505 | | - ret = btrfs_block_rsv_add(root, &fs_info->trans_block_rsv, |
|---|
| 506 | | - num_bytes, flush); |
|---|
| 625 | + ret = btrfs_block_rsv_add(root, rsv, num_bytes, flush); |
|---|
| 626 | + if (ret) |
|---|
| 627 | + goto reserve_fail; |
|---|
| 628 | + if (delayed_refs_bytes) { |
|---|
| 629 | + btrfs_migrate_to_delayed_refs_rsv(fs_info, rsv, |
|---|
| 630 | + delayed_refs_bytes); |
|---|
| 631 | + num_bytes -= delayed_refs_bytes; |
|---|
| 632 | + } |
|---|
| 633 | + |
|---|
| 634 | + if (rsv->space_info->force_alloc) |
|---|
| 635 | + do_chunk_alloc = true; |
|---|
| 636 | + } else if (num_items == 0 && flush == BTRFS_RESERVE_FLUSH_ALL && |
|---|
| 637 | + !delayed_refs_rsv->full) { |
|---|
| 638 | + /* |
|---|
| 639 | + * Some people call with btrfs_start_transaction(root, 0) |
|---|
| 640 | + * because they can be throttled, but have some other mechanism |
|---|
| 641 | + * for reserving space. We still want these guys to refill the |
|---|
| 642 | + * delayed block_rsv so just add 1 items worth of reservation |
|---|
| 643 | + * here. |
|---|
| 644 | + */ |
|---|
| 645 | + ret = btrfs_delayed_refs_rsv_refill(fs_info, flush); |
|---|
| 507 | 646 | if (ret) |
|---|
| 508 | 647 | goto reserve_fail; |
|---|
| 509 | 648 | } |
|---|
| .. | .. |
|---|
| 556 | 695 | INIT_LIST_HEAD(&h->new_bgs); |
|---|
| 557 | 696 | |
|---|
| 558 | 697 | smp_mb(); |
|---|
| 559 | | - if (cur_trans->state >= TRANS_STATE_BLOCKED && |
|---|
| 698 | + if (cur_trans->state >= TRANS_STATE_COMMIT_START && |
|---|
| 560 | 699 | may_wait_transaction(fs_info, type)) { |
|---|
| 561 | 700 | current->journal_info = h; |
|---|
| 562 | 701 | btrfs_commit_transaction(h); |
|---|
| .. | .. |
|---|
| 574 | 713 | got_it: |
|---|
| 575 | 714 | if (!current->journal_info) |
|---|
| 576 | 715 | current->journal_info = h; |
|---|
| 716 | + |
|---|
| 717 | + /* |
|---|
| 718 | + * If the space_info is marked ALLOC_FORCE then we'll get upgraded to |
|---|
| 719 | + * ALLOC_FORCE the first run through, and then we won't allocate for |
|---|
| 720 | + * anybody else who races in later. We don't care about the return |
|---|
| 721 | + * value here. |
|---|
| 722 | + */ |
|---|
| 723 | + if (do_chunk_alloc && num_bytes) { |
|---|
| 724 | + u64 flags = h->block_rsv->space_info->flags; |
|---|
| 725 | + |
|---|
| 726 | + btrfs_chunk_alloc(h, btrfs_get_alloc_profile(fs_info, flags), |
|---|
| 727 | + CHUNK_ALLOC_NO_FORCE); |
|---|
| 728 | + } |
|---|
| 577 | 729 | |
|---|
| 578 | 730 | /* |
|---|
| 579 | 731 | * btrfs_record_root_in_trans() needs to alloc new extents, and may |
|---|
| .. | .. |
|---|
| 594 | 746 | alloc_fail: |
|---|
| 595 | 747 | if (num_bytes) |
|---|
| 596 | 748 | btrfs_block_rsv_release(fs_info, &fs_info->trans_block_rsv, |
|---|
| 597 | | - num_bytes); |
|---|
| 749 | + num_bytes, NULL); |
|---|
| 598 | 750 | reserve_fail: |
|---|
| 599 | 751 | btrfs_qgroup_free_meta_pertrans(root, qgroup_reserved); |
|---|
| 600 | 752 | return ERR_PTR(ret); |
|---|
| .. | .. |
|---|
| 609 | 761 | |
|---|
| 610 | 762 | struct btrfs_trans_handle *btrfs_start_transaction_fallback_global_rsv( |
|---|
| 611 | 763 | struct btrfs_root *root, |
|---|
| 612 | | - unsigned int num_items, |
|---|
| 613 | | - int min_factor) |
|---|
| 764 | + unsigned int num_items) |
|---|
| 614 | 765 | { |
|---|
| 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; |
|---|
| 766 | + return start_transaction(root, num_items, TRANS_START, |
|---|
| 767 | + BTRFS_RESERVE_FLUSH_ALL_STEAL, false); |
|---|
| 649 | 768 | } |
|---|
| 650 | 769 | |
|---|
| 651 | 770 | struct btrfs_trans_handle *btrfs_join_transaction(struct btrfs_root *root) |
|---|
| .. | .. |
|---|
| 654 | 773 | true); |
|---|
| 655 | 774 | } |
|---|
| 656 | 775 | |
|---|
| 657 | | -struct btrfs_trans_handle *btrfs_join_transaction_nolock(struct btrfs_root *root) |
|---|
| 776 | +struct btrfs_trans_handle *btrfs_join_transaction_spacecache(struct btrfs_root *root) |
|---|
| 658 | 777 | { |
|---|
| 659 | 778 | return start_transaction(root, 0, TRANS_JOIN_NOLOCK, |
|---|
| 660 | 779 | BTRFS_RESERVE_NO_FLUSH, true); |
|---|
| .. | .. |
|---|
| 692 | 811 | /* |
|---|
| 693 | 812 | * btrfs_attach_transaction_barrier() - catch the running transaction |
|---|
| 694 | 813 | * |
|---|
| 695 | | - * It is similar to the above function, the differentia is this one |
|---|
| 814 | + * It is similar to the above function, the difference is this one |
|---|
| 696 | 815 | * will wait for all the inactive transactions until they fully |
|---|
| 697 | 816 | * complete. |
|---|
| 698 | 817 | */ |
|---|
| .. | .. |
|---|
| 703 | 822 | |
|---|
| 704 | 823 | trans = start_transaction(root, 0, TRANS_ATTACH, |
|---|
| 705 | 824 | BTRFS_RESERVE_NO_FLUSH, true); |
|---|
| 706 | | - if (trans == ERR_PTR(-ENOENT)) |
|---|
| 707 | | - btrfs_wait_for_commit(root->fs_info, 0); |
|---|
| 825 | + if (trans == ERR_PTR(-ENOENT)) { |
|---|
| 826 | + int ret; |
|---|
| 827 | + |
|---|
| 828 | + ret = btrfs_wait_for_commit(root->fs_info, 0); |
|---|
| 829 | + if (ret) |
|---|
| 830 | + return ERR_PTR(ret); |
|---|
| 831 | + } |
|---|
| 708 | 832 | |
|---|
| 709 | 833 | return trans; |
|---|
| 710 | 834 | } |
|---|
| .. | .. |
|---|
| 768 | 892 | } |
|---|
| 769 | 893 | |
|---|
| 770 | 894 | wait_for_commit(cur_trans); |
|---|
| 895 | + ret = cur_trans->aborted; |
|---|
| 771 | 896 | btrfs_put_transaction(cur_trans); |
|---|
| 772 | 897 | out: |
|---|
| 773 | 898 | return ret; |
|---|
| .. | .. |
|---|
| 782 | 907 | { |
|---|
| 783 | 908 | struct btrfs_fs_info *fs_info = trans->fs_info; |
|---|
| 784 | 909 | |
|---|
| 785 | | - if (btrfs_check_space_for_delayed_refs(trans, fs_info)) |
|---|
| 910 | + if (btrfs_check_space_for_delayed_refs(fs_info)) |
|---|
| 786 | 911 | return 1; |
|---|
| 787 | 912 | |
|---|
| 788 | 913 | return !!btrfs_block_rsv_check(&fs_info->global_block_rsv, 5); |
|---|
| .. | .. |
|---|
| 791 | 916 | int btrfs_should_end_transaction(struct btrfs_trans_handle *trans) |
|---|
| 792 | 917 | { |
|---|
| 793 | 918 | struct btrfs_transaction *cur_trans = trans->transaction; |
|---|
| 794 | | - int updates; |
|---|
| 795 | | - int err; |
|---|
| 796 | 919 | |
|---|
| 797 | 920 | smp_mb(); |
|---|
| 798 | | - if (cur_trans->state >= TRANS_STATE_BLOCKED || |
|---|
| 921 | + if (cur_trans->state >= TRANS_STATE_COMMIT_START || |
|---|
| 799 | 922 | cur_trans->delayed_refs.flushing) |
|---|
| 800 | 923 | 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 | 924 | |
|---|
| 810 | 925 | return should_end_transaction(trans); |
|---|
| 811 | 926 | } |
|---|
| .. | .. |
|---|
| 827 | 942 | trace_btrfs_space_reservation(fs_info, "transaction", |
|---|
| 828 | 943 | trans->transid, trans->bytes_reserved, 0); |
|---|
| 829 | 944 | btrfs_block_rsv_release(fs_info, trans->block_rsv, |
|---|
| 830 | | - trans->bytes_reserved); |
|---|
| 945 | + trans->bytes_reserved, NULL); |
|---|
| 831 | 946 | trans->bytes_reserved = 0; |
|---|
| 832 | 947 | } |
|---|
| 833 | 948 | |
|---|
| .. | .. |
|---|
| 836 | 951 | { |
|---|
| 837 | 952 | struct btrfs_fs_info *info = trans->fs_info; |
|---|
| 838 | 953 | 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 | 954 | int err = 0; |
|---|
| 843 | | - int must_run_delayed_refs = 0; |
|---|
| 844 | 955 | |
|---|
| 845 | 956 | if (refcount_read(&trans->use_count) > 1) { |
|---|
| 846 | 957 | refcount_dec(&trans->use_count); |
|---|
| .. | .. |
|---|
| 851 | 962 | btrfs_trans_release_metadata(trans); |
|---|
| 852 | 963 | trans->block_rsv = NULL; |
|---|
| 853 | 964 | |
|---|
| 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); |
|---|
| 965 | + btrfs_create_pending_block_groups(trans); |
|---|
| 877 | 966 | |
|---|
| 878 | 967 | 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 | 968 | |
|---|
| 895 | 969 | if (trans->type & __TRANS_FREEZABLE) |
|---|
| 896 | 970 | sb_end_intwrite(info->sb); |
|---|
| .. | .. |
|---|
| 909 | 983 | if (throttle) |
|---|
| 910 | 984 | btrfs_run_delayed_iputs(info); |
|---|
| 911 | 985 | |
|---|
| 912 | | - if (trans->aborted || |
|---|
| 986 | + if (TRANS_ABORTED(trans) || |
|---|
| 913 | 987 | test_bit(BTRFS_FS_STATE_ERROR, &info->fs_state)) { |
|---|
| 914 | 988 | wake_up_process(info->transaction_kthread); |
|---|
| 915 | | - err = -EIO; |
|---|
| 989 | + if (TRANS_ABORTED(trans)) |
|---|
| 990 | + err = trans->aborted; |
|---|
| 991 | + else |
|---|
| 992 | + err = -EROFS; |
|---|
| 916 | 993 | } |
|---|
| 917 | 994 | |
|---|
| 918 | 995 | 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 | 996 | return err; |
|---|
| 924 | 997 | } |
|---|
| 925 | 998 | |
|---|
| .. | .. |
|---|
| 967 | 1040 | * superblock that points to btree nodes/leafs for which |
|---|
| 968 | 1041 | * writeback hasn't finished yet (and without errors). |
|---|
| 969 | 1042 | * We cleanup any entries left in the io tree when committing |
|---|
| 970 | | - * the transaction (through clear_btree_io_tree()). |
|---|
| 1043 | + * the transaction (through extent_io_tree_release()). |
|---|
| 971 | 1044 | */ |
|---|
| 972 | 1045 | if (err == -ENOMEM) { |
|---|
| 973 | 1046 | err = 0; |
|---|
| .. | .. |
|---|
| 1012 | 1085 | * left in the io tree. For a log commit, we don't remove them |
|---|
| 1013 | 1086 | * after committing the log because the tree can be accessed |
|---|
| 1014 | 1087 | * concurrently - we do it only at transaction commit time when |
|---|
| 1015 | | - * it's safe to do it (through clear_btree_io_tree()). |
|---|
| 1088 | + * it's safe to do it (through extent_io_tree_release()). |
|---|
| 1016 | 1089 | */ |
|---|
| 1017 | 1090 | err = clear_extent_bit(dirty_pages, start, end, |
|---|
| 1018 | 1091 | EXTENT_NEED_WAIT, 0, 0, &cached_state); |
|---|
| .. | .. |
|---|
| 1032 | 1105 | return werr; |
|---|
| 1033 | 1106 | } |
|---|
| 1034 | 1107 | |
|---|
| 1035 | | -int btrfs_wait_extents(struct btrfs_fs_info *fs_info, |
|---|
| 1108 | +static int btrfs_wait_extents(struct btrfs_fs_info *fs_info, |
|---|
| 1036 | 1109 | struct extent_io_tree *dirty_pages) |
|---|
| 1037 | 1110 | { |
|---|
| 1038 | 1111 | bool errors = false; |
|---|
| .. | .. |
|---|
| 1090 | 1163 | blk_finish_plug(&plug); |
|---|
| 1091 | 1164 | ret2 = btrfs_wait_extents(fs_info, dirty_pages); |
|---|
| 1092 | 1165 | |
|---|
| 1093 | | - clear_btree_io_tree(&trans->transaction->dirty_pages); |
|---|
| 1166 | + extent_io_tree_release(&trans->transaction->dirty_pages); |
|---|
| 1094 | 1167 | |
|---|
| 1095 | 1168 | if (ret) |
|---|
| 1096 | 1169 | return ret; |
|---|
| .. | .. |
|---|
| 1158 | 1231 | |
|---|
| 1159 | 1232 | eb = btrfs_lock_root_node(fs_info->tree_root); |
|---|
| 1160 | 1233 | ret = btrfs_cow_block(trans, fs_info->tree_root, eb, NULL, |
|---|
| 1161 | | - 0, &eb); |
|---|
| 1234 | + 0, &eb, BTRFS_NESTING_COW); |
|---|
| 1162 | 1235 | btrfs_tree_unlock(eb); |
|---|
| 1163 | 1236 | free_extent_buffer(eb); |
|---|
| 1164 | 1237 | |
|---|
| .. | .. |
|---|
| 1169 | 1242 | if (ret) |
|---|
| 1170 | 1243 | return ret; |
|---|
| 1171 | 1244 | |
|---|
| 1172 | | - ret = btrfs_run_dev_stats(trans, fs_info); |
|---|
| 1245 | + ret = btrfs_run_dev_stats(trans); |
|---|
| 1173 | 1246 | if (ret) |
|---|
| 1174 | 1247 | return ret; |
|---|
| 1175 | | - ret = btrfs_run_dev_replace(trans, fs_info); |
|---|
| 1248 | + ret = btrfs_run_dev_replace(trans); |
|---|
| 1176 | 1249 | if (ret) |
|---|
| 1177 | 1250 | return ret; |
|---|
| 1178 | 1251 | ret = btrfs_run_qgroups(trans); |
|---|
| 1179 | 1252 | if (ret) |
|---|
| 1180 | 1253 | return ret; |
|---|
| 1181 | 1254 | |
|---|
| 1182 | | - ret = btrfs_setup_space_cache(trans, fs_info); |
|---|
| 1255 | + ret = btrfs_setup_space_cache(trans); |
|---|
| 1183 | 1256 | if (ret) |
|---|
| 1184 | 1257 | return ret; |
|---|
| 1185 | 1258 | |
|---|
| .. | .. |
|---|
| 1207 | 1280 | } |
|---|
| 1208 | 1281 | |
|---|
| 1209 | 1282 | while (!list_empty(dirty_bgs) || !list_empty(io_bgs)) { |
|---|
| 1210 | | - ret = btrfs_write_dirty_block_groups(trans, fs_info); |
|---|
| 1283 | + ret = btrfs_write_dirty_block_groups(trans); |
|---|
| 1211 | 1284 | if (ret) |
|---|
| 1212 | 1285 | return ret; |
|---|
| 1213 | 1286 | ret = btrfs_run_delayed_refs(trans, (unsigned long)-1); |
|---|
| .. | .. |
|---|
| 1220 | 1293 | |
|---|
| 1221 | 1294 | list_add_tail(&fs_info->extent_root->dirty_list, |
|---|
| 1222 | 1295 | &trans->transaction->switch_commits); |
|---|
| 1223 | | - btrfs_after_dev_replace_commit(fs_info); |
|---|
| 1296 | + |
|---|
| 1297 | + /* Update dev-replace pointer once everything is committed */ |
|---|
| 1298 | + fs_info->dev_replace.committed_cursor_left = |
|---|
| 1299 | + fs_info->dev_replace.cursor_left_last_write_of_item; |
|---|
| 1224 | 1300 | |
|---|
| 1225 | 1301 | return 0; |
|---|
| 1226 | 1302 | } |
|---|
| .. | .. |
|---|
| 1235 | 1311 | struct btrfs_fs_info *fs_info = root->fs_info; |
|---|
| 1236 | 1312 | |
|---|
| 1237 | 1313 | spin_lock(&fs_info->trans_lock); |
|---|
| 1238 | | - if (list_empty(&root->root_list)) |
|---|
| 1314 | + if (list_empty(&root->root_list)) { |
|---|
| 1315 | + btrfs_grab_root(root); |
|---|
| 1239 | 1316 | list_add_tail(&root->root_list, &fs_info->dead_roots); |
|---|
| 1317 | + } |
|---|
| 1240 | 1318 | spin_unlock(&fs_info->trans_lock); |
|---|
| 1241 | 1319 | } |
|---|
| 1242 | 1320 | |
|---|
| .. | .. |
|---|
| 1360 | 1438 | return 0; |
|---|
| 1361 | 1439 | |
|---|
| 1362 | 1440 | /* |
|---|
| 1363 | | - * Ensure dirty @src will be commited. Or, after comming |
|---|
| 1441 | + * Ensure dirty @src will be committed. Or, after coming |
|---|
| 1364 | 1442 | * commit_fs_roots() and switch_commit_roots(), any dirty but not |
|---|
| 1365 | 1443 | * recorded root will never be updated again, causing an outdated root |
|---|
| 1366 | 1444 | * item. |
|---|
| .. | .. |
|---|
| 1401 | 1479 | ret = commit_cowonly_roots(trans); |
|---|
| 1402 | 1480 | if (ret) |
|---|
| 1403 | 1481 | goto out; |
|---|
| 1404 | | - switch_commit_roots(trans->transaction); |
|---|
| 1482 | + switch_commit_roots(trans); |
|---|
| 1405 | 1483 | ret = btrfs_write_and_wait_transaction(trans); |
|---|
| 1406 | 1484 | if (ret) |
|---|
| 1407 | 1485 | btrfs_handle_fs_error(fs_info, ret, |
|---|
| .. | .. |
|---|
| 1453 | 1531 | u64 index = 0; |
|---|
| 1454 | 1532 | u64 objectid; |
|---|
| 1455 | 1533 | u64 root_flags; |
|---|
| 1456 | | - uuid_le new_uuid; |
|---|
| 1457 | 1534 | |
|---|
| 1458 | 1535 | ASSERT(pending->path); |
|---|
| 1459 | 1536 | path = pending->path; |
|---|
| .. | .. |
|---|
| 1546 | 1623 | |
|---|
| 1547 | 1624 | btrfs_set_root_generation_v2(new_root_item, |
|---|
| 1548 | 1625 | trans->transid); |
|---|
| 1549 | | - uuid_le_gen(&new_uuid); |
|---|
| 1550 | | - memcpy(new_root_item->uuid, new_uuid.b, BTRFS_UUID_SIZE); |
|---|
| 1626 | + generate_random_guid(new_root_item->uuid); |
|---|
| 1551 | 1627 | memcpy(new_root_item->parent_uuid, root->root_item.uuid, |
|---|
| 1552 | 1628 | BTRFS_UUID_SIZE); |
|---|
| 1553 | 1629 | if (!(root_flags & BTRFS_ROOT_SUBVOL_RDONLY)) { |
|---|
| .. | .. |
|---|
| 1563 | 1639 | btrfs_set_root_otransid(new_root_item, trans->transid); |
|---|
| 1564 | 1640 | |
|---|
| 1565 | 1641 | old = btrfs_lock_root_node(root); |
|---|
| 1566 | | - ret = btrfs_cow_block(trans, root, old, NULL, 0, &old); |
|---|
| 1642 | + ret = btrfs_cow_block(trans, root, old, NULL, 0, &old, |
|---|
| 1643 | + BTRFS_NESTING_COW); |
|---|
| 1567 | 1644 | if (ret) { |
|---|
| 1568 | 1645 | btrfs_tree_unlock(old); |
|---|
| 1569 | 1646 | free_extent_buffer(old); |
|---|
| .. | .. |
|---|
| 1571 | 1648 | goto fail; |
|---|
| 1572 | 1649 | } |
|---|
| 1573 | 1650 | |
|---|
| 1574 | | - btrfs_set_lock_blocking(old); |
|---|
| 1651 | + btrfs_set_lock_blocking_write(old); |
|---|
| 1575 | 1652 | |
|---|
| 1576 | 1653 | ret = btrfs_copy_root(trans, root, old, &tmp, objectid); |
|---|
| 1577 | 1654 | /* clean up in any case */ |
|---|
| .. | .. |
|---|
| 1609 | 1686 | } |
|---|
| 1610 | 1687 | |
|---|
| 1611 | 1688 | key.offset = (u64)-1; |
|---|
| 1612 | | - pending->snap = btrfs_read_fs_root_no_name(fs_info, &key); |
|---|
| 1689 | + pending->snap = btrfs_get_new_fs_root(fs_info, objectid, pending->anon_dev); |
|---|
| 1613 | 1690 | if (IS_ERR(pending->snap)) { |
|---|
| 1614 | 1691 | ret = PTR_ERR(pending->snap); |
|---|
| 1692 | + pending->snap = NULL; |
|---|
| 1615 | 1693 | btrfs_abort_transaction(trans, ret); |
|---|
| 1616 | 1694 | goto fail; |
|---|
| 1617 | 1695 | } |
|---|
| .. | .. |
|---|
| 1639 | 1717 | if (ret < 0) |
|---|
| 1640 | 1718 | goto fail; |
|---|
| 1641 | 1719 | |
|---|
| 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); |
|---|
| 1720 | + ret = btrfs_insert_dir_item(trans, dentry->d_name.name, |
|---|
| 1721 | + dentry->d_name.len, BTRFS_I(parent_inode), |
|---|
| 1722 | + &key, BTRFS_FT_DIR, index); |
|---|
| 1646 | 1723 | /* We have check then name at the beginning, so it is impossible. */ |
|---|
| 1647 | 1724 | BUG_ON(ret == -EEXIST || ret == -EOVERFLOW); |
|---|
| 1648 | 1725 | if (ret) { |
|---|
| .. | .. |
|---|
| 1659 | 1736 | btrfs_abort_transaction(trans, ret); |
|---|
| 1660 | 1737 | goto fail; |
|---|
| 1661 | 1738 | } |
|---|
| 1662 | | - ret = btrfs_uuid_tree_add(trans, new_uuid.b, BTRFS_UUID_KEY_SUBVOL, |
|---|
| 1739 | + ret = btrfs_uuid_tree_add(trans, new_root_item->uuid, |
|---|
| 1740 | + BTRFS_UUID_KEY_SUBVOL, |
|---|
| 1663 | 1741 | objectid); |
|---|
| 1664 | 1742 | if (ret) { |
|---|
| 1665 | 1743 | btrfs_abort_transaction(trans, ret); |
|---|
| .. | .. |
|---|
| 1771 | 1849 | struct btrfs_transaction *trans) |
|---|
| 1772 | 1850 | { |
|---|
| 1773 | 1851 | wait_event(fs_info->transaction_blocked_wait, |
|---|
| 1774 | | - trans->state >= TRANS_STATE_COMMIT_START || trans->aborted); |
|---|
| 1852 | + trans->state >= TRANS_STATE_COMMIT_START || |
|---|
| 1853 | + TRANS_ABORTED(trans)); |
|---|
| 1775 | 1854 | } |
|---|
| 1776 | 1855 | |
|---|
| 1777 | 1856 | /* |
|---|
| .. | .. |
|---|
| 1783 | 1862 | struct btrfs_transaction *trans) |
|---|
| 1784 | 1863 | { |
|---|
| 1785 | 1864 | wait_event(fs_info->transaction_wait, |
|---|
| 1786 | | - trans->state >= TRANS_STATE_UNBLOCKED || trans->aborted); |
|---|
| 1865 | + trans->state >= TRANS_STATE_UNBLOCKED || |
|---|
| 1866 | + TRANS_ABORTED(trans)); |
|---|
| 1787 | 1867 | } |
|---|
| 1788 | 1868 | |
|---|
| 1789 | 1869 | /* |
|---|
| .. | .. |
|---|
| 1865 | 1945 | { |
|---|
| 1866 | 1946 | struct btrfs_fs_info *fs_info = trans->fs_info; |
|---|
| 1867 | 1947 | struct btrfs_transaction *cur_trans = trans->transaction; |
|---|
| 1868 | | - DEFINE_WAIT(wait); |
|---|
| 1869 | 1948 | |
|---|
| 1870 | 1949 | WARN_ON(refcount_read(&trans->use_count) > 1); |
|---|
| 1871 | 1950 | |
|---|
| .. | .. |
|---|
| 1880 | 1959 | */ |
|---|
| 1881 | 1960 | BUG_ON(list_empty(&cur_trans->list)); |
|---|
| 1882 | 1961 | |
|---|
| 1883 | | - list_del_init(&cur_trans->list); |
|---|
| 1884 | 1962 | if (cur_trans == fs_info->running_transaction) { |
|---|
| 1885 | 1963 | cur_trans->state = TRANS_STATE_COMMIT_DOING; |
|---|
| 1886 | 1964 | spin_unlock(&fs_info->trans_lock); |
|---|
| .. | .. |
|---|
| 1889 | 1967 | |
|---|
| 1890 | 1968 | spin_lock(&fs_info->trans_lock); |
|---|
| 1891 | 1969 | } |
|---|
| 1970 | + |
|---|
| 1971 | + /* |
|---|
| 1972 | + * Now that we know no one else is still using the transaction we can |
|---|
| 1973 | + * remove the transaction from the list of transactions. This avoids |
|---|
| 1974 | + * the transaction kthread from cleaning up the transaction while some |
|---|
| 1975 | + * other task is still using it, which could result in a use-after-free |
|---|
| 1976 | + * on things like log trees, as it forces the transaction kthread to |
|---|
| 1977 | + * wait for this transaction to be cleaned up by us. |
|---|
| 1978 | + */ |
|---|
| 1979 | + list_del_init(&cur_trans->list); |
|---|
| 1980 | + |
|---|
| 1892 | 1981 | spin_unlock(&fs_info->trans_lock); |
|---|
| 1893 | 1982 | |
|---|
| 1894 | 1983 | btrfs_cleanup_one_transaction(trans->transaction, fs_info); |
|---|
| .. | .. |
|---|
| 1912 | 2001 | kmem_cache_free(btrfs_trans_handle_cachep, trans); |
|---|
| 1913 | 2002 | } |
|---|
| 1914 | 2003 | |
|---|
| 1915 | | -static inline int btrfs_start_delalloc_flush(struct btrfs_fs_info *fs_info) |
|---|
| 2004 | +/* |
|---|
| 2005 | + * Release reserved delayed ref space of all pending block groups of the |
|---|
| 2006 | + * transaction and remove them from the list |
|---|
| 2007 | + */ |
|---|
| 2008 | +static void btrfs_cleanup_pending_block_groups(struct btrfs_trans_handle *trans) |
|---|
| 1916 | 2009 | { |
|---|
| 2010 | + struct btrfs_fs_info *fs_info = trans->fs_info; |
|---|
| 2011 | + struct btrfs_block_group *block_group, *tmp; |
|---|
| 2012 | + |
|---|
| 2013 | + list_for_each_entry_safe(block_group, tmp, &trans->new_bgs, bg_list) { |
|---|
| 2014 | + btrfs_delayed_refs_rsv_release(fs_info, 1); |
|---|
| 2015 | + list_del_init(&block_group->bg_list); |
|---|
| 2016 | + } |
|---|
| 2017 | +} |
|---|
| 2018 | + |
|---|
| 2019 | +static inline int btrfs_start_delalloc_flush(struct btrfs_trans_handle *trans) |
|---|
| 2020 | +{ |
|---|
| 2021 | + struct btrfs_fs_info *fs_info = trans->fs_info; |
|---|
| 2022 | + |
|---|
| 1917 | 2023 | /* |
|---|
| 1918 | 2024 | * We use writeback_inodes_sb here because if we used |
|---|
| 1919 | 2025 | * btrfs_start_delalloc_roots we would deadlock with fs freeze. |
|---|
| .. | .. |
|---|
| 1923 | 2029 | * from already being in a transaction and our join_transaction doesn't |
|---|
| 1924 | 2030 | * have to re-take the fs freeze lock. |
|---|
| 1925 | 2031 | */ |
|---|
| 1926 | | - if (btrfs_test_opt(fs_info, FLUSHONCOMMIT)) |
|---|
| 2032 | + if (btrfs_test_opt(fs_info, FLUSHONCOMMIT)) { |
|---|
| 1927 | 2033 | writeback_inodes_sb(fs_info->sb, WB_REASON_SYNC); |
|---|
| 2034 | + } else { |
|---|
| 2035 | + struct btrfs_pending_snapshot *pending; |
|---|
| 2036 | + struct list_head *head = &trans->transaction->pending_snapshots; |
|---|
| 2037 | + |
|---|
| 2038 | + /* |
|---|
| 2039 | + * Flush dellaloc for any root that is going to be snapshotted. |
|---|
| 2040 | + * This is done to avoid a corrupted version of files, in the |
|---|
| 2041 | + * snapshots, that had both buffered and direct IO writes (even |
|---|
| 2042 | + * if they were done sequentially) due to an unordered update of |
|---|
| 2043 | + * the inode's size on disk. |
|---|
| 2044 | + */ |
|---|
| 2045 | + list_for_each_entry(pending, head, list) { |
|---|
| 2046 | + int ret; |
|---|
| 2047 | + |
|---|
| 2048 | + ret = btrfs_start_delalloc_snapshot(pending->root); |
|---|
| 2049 | + if (ret) |
|---|
| 2050 | + return ret; |
|---|
| 2051 | + } |
|---|
| 2052 | + } |
|---|
| 1928 | 2053 | return 0; |
|---|
| 1929 | 2054 | } |
|---|
| 1930 | 2055 | |
|---|
| 1931 | | -static inline void btrfs_wait_delalloc_flush(struct btrfs_fs_info *fs_info) |
|---|
| 2056 | +static inline void btrfs_wait_delalloc_flush(struct btrfs_trans_handle *trans) |
|---|
| 1932 | 2057 | { |
|---|
| 1933 | | - if (btrfs_test_opt(fs_info, FLUSHONCOMMIT)) |
|---|
| 1934 | | - btrfs_wait_ordered_roots(fs_info, U64_MAX, 0, (u64)-1); |
|---|
| 1935 | | -} |
|---|
| 2058 | + struct btrfs_fs_info *fs_info = trans->fs_info; |
|---|
| 1936 | 2059 | |
|---|
| 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); |
|---|
| 2060 | + if (btrfs_test_opt(fs_info, FLUSHONCOMMIT)) { |
|---|
| 2061 | + btrfs_wait_ordered_roots(fs_info, U64_MAX, 0, (u64)-1); |
|---|
| 2062 | + } else { |
|---|
| 2063 | + struct btrfs_pending_snapshot *pending; |
|---|
| 2064 | + struct list_head *head = &trans->transaction->pending_snapshots; |
|---|
| 2065 | + |
|---|
| 2066 | + /* |
|---|
| 2067 | + * Wait for any dellaloc that we started previously for the roots |
|---|
| 2068 | + * that are going to be snapshotted. This is to avoid a corrupted |
|---|
| 2069 | + * version of files in the snapshots that had both buffered and |
|---|
| 2070 | + * direct IO writes (even if they were done sequentially). |
|---|
| 2071 | + */ |
|---|
| 2072 | + list_for_each_entry(pending, head, list) |
|---|
| 2073 | + btrfs_wait_ordered_extents(pending->root, |
|---|
| 2074 | + U64_MAX, 0, U64_MAX); |
|---|
| 2075 | + } |
|---|
| 1942 | 2076 | } |
|---|
| 1943 | 2077 | |
|---|
| 1944 | 2078 | int btrfs_commit_transaction(struct btrfs_trans_handle *trans) |
|---|
| .. | .. |
|---|
| 1947 | 2081 | struct btrfs_transaction *cur_trans = trans->transaction; |
|---|
| 1948 | 2082 | struct btrfs_transaction *prev_trans = NULL; |
|---|
| 1949 | 2083 | int ret; |
|---|
| 2084 | + |
|---|
| 2085 | + ASSERT(refcount_read(&trans->use_count) == 1); |
|---|
| 1950 | 2086 | |
|---|
| 1951 | 2087 | /* |
|---|
| 1952 | 2088 | * Some places just start a transaction to commit it. We need to make |
|---|
| .. | .. |
|---|
| 1957 | 2093 | trans->dirty = true; |
|---|
| 1958 | 2094 | |
|---|
| 1959 | 2095 | /* Stop the commit early if ->aborted is set */ |
|---|
| 1960 | | - if (unlikely(READ_ONCE(cur_trans->aborted))) { |
|---|
| 2096 | + if (TRANS_ABORTED(cur_trans)) { |
|---|
| 1961 | 2097 | ret = cur_trans->aborted; |
|---|
| 1962 | 2098 | btrfs_end_transaction(trans); |
|---|
| 1963 | 2099 | return ret; |
|---|
| .. | .. |
|---|
| 1984 | 2120 | cur_trans->delayed_refs.flushing = 1; |
|---|
| 1985 | 2121 | smp_wmb(); |
|---|
| 1986 | 2122 | |
|---|
| 1987 | | - if (!list_empty(&trans->new_bgs)) |
|---|
| 1988 | | - btrfs_create_pending_block_groups(trans); |
|---|
| 2123 | + btrfs_create_pending_block_groups(trans); |
|---|
| 1989 | 2124 | |
|---|
| 1990 | 2125 | ret = btrfs_run_delayed_refs(trans, 0); |
|---|
| 1991 | 2126 | if (ret) { |
|---|
| .. | .. |
|---|
| 2032 | 2167 | |
|---|
| 2033 | 2168 | wait_for_commit(cur_trans); |
|---|
| 2034 | 2169 | |
|---|
| 2035 | | - if (unlikely(cur_trans->aborted)) |
|---|
| 2170 | + if (TRANS_ABORTED(cur_trans)) |
|---|
| 2036 | 2171 | ret = cur_trans->aborted; |
|---|
| 2037 | 2172 | |
|---|
| 2038 | 2173 | btrfs_put_transaction(cur_trans); |
|---|
| .. | .. |
|---|
| 2051 | 2186 | spin_unlock(&fs_info->trans_lock); |
|---|
| 2052 | 2187 | |
|---|
| 2053 | 2188 | wait_for_commit(prev_trans); |
|---|
| 2054 | | - ret = prev_trans->aborted; |
|---|
| 2189 | + ret = READ_ONCE(prev_trans->aborted); |
|---|
| 2055 | 2190 | |
|---|
| 2056 | 2191 | btrfs_put_transaction(prev_trans); |
|---|
| 2057 | 2192 | if (ret) |
|---|
| .. | .. |
|---|
| 2075 | 2210 | |
|---|
| 2076 | 2211 | extwriter_counter_dec(cur_trans, trans->type); |
|---|
| 2077 | 2212 | |
|---|
| 2078 | | - ret = btrfs_start_delalloc_flush(fs_info); |
|---|
| 2213 | + ret = btrfs_start_delalloc_flush(trans); |
|---|
| 2079 | 2214 | if (ret) |
|---|
| 2080 | 2215 | goto cleanup_transaction; |
|---|
| 2081 | 2216 | |
|---|
| .. | .. |
|---|
| 2091 | 2226 | if (ret) |
|---|
| 2092 | 2227 | goto cleanup_transaction; |
|---|
| 2093 | 2228 | |
|---|
| 2094 | | - btrfs_wait_delalloc_flush(fs_info); |
|---|
| 2229 | + btrfs_wait_delalloc_flush(trans); |
|---|
| 2095 | 2230 | |
|---|
| 2096 | | - btrfs_wait_pending_ordered(cur_trans); |
|---|
| 2231 | + /* |
|---|
| 2232 | + * Wait for all ordered extents started by a fast fsync that joined this |
|---|
| 2233 | + * transaction. Otherwise if this transaction commits before the ordered |
|---|
| 2234 | + * extents complete we lose logged data after a power failure. |
|---|
| 2235 | + */ |
|---|
| 2236 | + wait_event(cur_trans->pending_wait, |
|---|
| 2237 | + atomic_read(&cur_trans->pending_ordered) == 0); |
|---|
| 2097 | 2238 | |
|---|
| 2098 | 2239 | btrfs_scrub_pause(fs_info); |
|---|
| 2099 | 2240 | /* |
|---|
| .. | .. |
|---|
| 2107 | 2248 | wait_event(cur_trans->writer_wait, |
|---|
| 2108 | 2249 | atomic_read(&cur_trans->num_writers) == 1); |
|---|
| 2109 | 2250 | |
|---|
| 2110 | | - /* ->aborted might be set after the previous check, so check it */ |
|---|
| 2111 | | - if (unlikely(READ_ONCE(cur_trans->aborted))) { |
|---|
| 2251 | + if (TRANS_ABORTED(cur_trans)) { |
|---|
| 2112 | 2252 | ret = cur_trans->aborted; |
|---|
| 2113 | 2253 | goto scrub_continue; |
|---|
| 2114 | 2254 | } |
|---|
| .. | .. |
|---|
| 2125 | 2265 | * core function of the snapshot creation. |
|---|
| 2126 | 2266 | */ |
|---|
| 2127 | 2267 | ret = create_pending_snapshots(trans); |
|---|
| 2128 | | - if (ret) { |
|---|
| 2129 | | - mutex_unlock(&fs_info->reloc_mutex); |
|---|
| 2130 | | - goto scrub_continue; |
|---|
| 2131 | | - } |
|---|
| 2268 | + if (ret) |
|---|
| 2269 | + goto unlock_reloc; |
|---|
| 2132 | 2270 | |
|---|
| 2133 | 2271 | /* |
|---|
| 2134 | 2272 | * We insert the dir indexes of the snapshots and update the inode |
|---|
| .. | .. |
|---|
| 2141 | 2279 | * the nodes and leaves. |
|---|
| 2142 | 2280 | */ |
|---|
| 2143 | 2281 | ret = btrfs_run_delayed_items(trans); |
|---|
| 2144 | | - if (ret) { |
|---|
| 2145 | | - mutex_unlock(&fs_info->reloc_mutex); |
|---|
| 2146 | | - goto scrub_continue; |
|---|
| 2147 | | - } |
|---|
| 2282 | + if (ret) |
|---|
| 2283 | + goto unlock_reloc; |
|---|
| 2148 | 2284 | |
|---|
| 2149 | 2285 | ret = btrfs_run_delayed_refs(trans, (unsigned long)-1); |
|---|
| 2150 | | - if (ret) { |
|---|
| 2151 | | - mutex_unlock(&fs_info->reloc_mutex); |
|---|
| 2152 | | - goto scrub_continue; |
|---|
| 2153 | | - } |
|---|
| 2286 | + if (ret) |
|---|
| 2287 | + goto unlock_reloc; |
|---|
| 2154 | 2288 | |
|---|
| 2155 | 2289 | /* |
|---|
| 2156 | 2290 | * make sure none of the code above managed to slip in a |
|---|
| .. | .. |
|---|
| 2176 | 2310 | mutex_lock(&fs_info->tree_log_mutex); |
|---|
| 2177 | 2311 | |
|---|
| 2178 | 2312 | 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 | | - } |
|---|
| 2313 | + if (ret) |
|---|
| 2314 | + goto unlock_tree_log; |
|---|
| 2184 | 2315 | |
|---|
| 2185 | 2316 | /* |
|---|
| 2186 | 2317 | * Since the transaction is done, we can apply the pending changes |
|---|
| .. | .. |
|---|
| 2198 | 2329 | * new delayed refs. Must handle them or qgroup can be wrong. |
|---|
| 2199 | 2330 | */ |
|---|
| 2200 | 2331 | 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 | | - } |
|---|
| 2332 | + if (ret) |
|---|
| 2333 | + goto unlock_tree_log; |
|---|
| 2206 | 2334 | |
|---|
| 2207 | 2335 | /* |
|---|
| 2208 | 2336 | * Since fs roots are all committed, we can get a quite accurate |
|---|
| 2209 | 2337 | * new_roots. So let's do quota accounting. |
|---|
| 2210 | 2338 | */ |
|---|
| 2211 | 2339 | 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 | | - } |
|---|
| 2340 | + if (ret < 0) |
|---|
| 2341 | + goto unlock_tree_log; |
|---|
| 2217 | 2342 | |
|---|
| 2218 | 2343 | 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 | | - } |
|---|
| 2344 | + if (ret) |
|---|
| 2345 | + goto unlock_tree_log; |
|---|
| 2224 | 2346 | |
|---|
| 2225 | 2347 | /* |
|---|
| 2226 | 2348 | * The tasks which save the space cache and inode cache may also |
|---|
| 2227 | 2349 | * update ->aborted, check it. |
|---|
| 2228 | 2350 | */ |
|---|
| 2229 | | - if (unlikely(READ_ONCE(cur_trans->aborted))) { |
|---|
| 2351 | + if (TRANS_ABORTED(cur_trans)) { |
|---|
| 2230 | 2352 | ret = cur_trans->aborted; |
|---|
| 2231 | | - mutex_unlock(&fs_info->tree_log_mutex); |
|---|
| 2232 | | - mutex_unlock(&fs_info->reloc_mutex); |
|---|
| 2233 | | - goto scrub_continue; |
|---|
| 2353 | + goto unlock_tree_log; |
|---|
| 2234 | 2354 | } |
|---|
| 2235 | | - |
|---|
| 2236 | | - btrfs_prepare_extent_commit(fs_info); |
|---|
| 2237 | 2355 | |
|---|
| 2238 | 2356 | cur_trans = fs_info->running_transaction; |
|---|
| 2239 | 2357 | |
|---|
| .. | .. |
|---|
| 2247 | 2365 | list_add_tail(&fs_info->chunk_root->dirty_list, |
|---|
| 2248 | 2366 | &cur_trans->switch_commits); |
|---|
| 2249 | 2367 | |
|---|
| 2250 | | - switch_commit_roots(cur_trans); |
|---|
| 2368 | + switch_commit_roots(trans); |
|---|
| 2251 | 2369 | |
|---|
| 2252 | 2370 | ASSERT(list_empty(&cur_trans->dirty_bgs)); |
|---|
| 2253 | 2371 | ASSERT(list_empty(&cur_trans->io_bgs)); |
|---|
| .. | .. |
|---|
| 2258 | 2376 | memcpy(fs_info->super_for_commit, fs_info->super_copy, |
|---|
| 2259 | 2377 | sizeof(*fs_info->super_copy)); |
|---|
| 2260 | 2378 | |
|---|
| 2261 | | - btrfs_update_commit_device_size(fs_info); |
|---|
| 2262 | | - btrfs_update_commit_device_bytes_used(cur_trans); |
|---|
| 2379 | + btrfs_commit_device_sizes(cur_trans); |
|---|
| 2263 | 2380 | |
|---|
| 2264 | 2381 | clear_bit(BTRFS_FS_LOG1_ERR, &fs_info->flags); |
|---|
| 2265 | 2382 | clear_bit(BTRFS_FS_LOG2_ERR, &fs_info->flags); |
|---|
| .. | .. |
|---|
| 2278 | 2395 | if (ret) { |
|---|
| 2279 | 2396 | btrfs_handle_fs_error(fs_info, ret, |
|---|
| 2280 | 2397 | "Error while writing out transaction"); |
|---|
| 2398 | + /* |
|---|
| 2399 | + * reloc_mutex has been unlocked, tree_log_mutex is still held |
|---|
| 2400 | + * but we can't jump to unlock_tree_log causing double unlock |
|---|
| 2401 | + */ |
|---|
| 2281 | 2402 | mutex_unlock(&fs_info->tree_log_mutex); |
|---|
| 2282 | 2403 | goto scrub_continue; |
|---|
| 2283 | 2404 | } |
|---|
| .. | .. |
|---|
| 2303 | 2424 | */ |
|---|
| 2304 | 2425 | cur_trans->state = TRANS_STATE_COMPLETED; |
|---|
| 2305 | 2426 | wake_up(&cur_trans->commit_wait); |
|---|
| 2306 | | - clear_bit(BTRFS_FS_NEED_ASYNC_COMMIT, &fs_info->flags); |
|---|
| 2307 | 2427 | |
|---|
| 2308 | 2428 | spin_lock(&fs_info->trans_lock); |
|---|
| 2309 | 2429 | list_del_init(&cur_trans->list); |
|---|
| .. | .. |
|---|
| 2326 | 2446 | |
|---|
| 2327 | 2447 | return ret; |
|---|
| 2328 | 2448 | |
|---|
| 2449 | +unlock_tree_log: |
|---|
| 2450 | + mutex_unlock(&fs_info->tree_log_mutex); |
|---|
| 2451 | +unlock_reloc: |
|---|
| 2452 | + mutex_unlock(&fs_info->reloc_mutex); |
|---|
| 2329 | 2453 | scrub_continue: |
|---|
| 2330 | 2454 | btrfs_scrub_continue(fs_info); |
|---|
| 2331 | 2455 | cleanup_transaction: |
|---|
| 2332 | 2456 | btrfs_trans_release_metadata(trans); |
|---|
| 2457 | + btrfs_cleanup_pending_block_groups(trans); |
|---|
| 2333 | 2458 | btrfs_trans_release_chunk_metadata(trans); |
|---|
| 2334 | 2459 | trans->block_rsv = NULL; |
|---|
| 2335 | 2460 | btrfs_warn(fs_info, "Skipping commit of aborted transaction."); |
|---|
| .. | .. |
|---|
| 2365 | 2490 | list_del_init(&root->root_list); |
|---|
| 2366 | 2491 | spin_unlock(&fs_info->trans_lock); |
|---|
| 2367 | 2492 | |
|---|
| 2368 | | - btrfs_debug(fs_info, "cleaner removing %llu", root->objectid); |
|---|
| 2493 | + btrfs_debug(fs_info, "cleaner removing %llu", root->root_key.objectid); |
|---|
| 2369 | 2494 | |
|---|
| 2370 | 2495 | btrfs_kill_all_delayed_nodes(root); |
|---|
| 2496 | + if (root->ino_cache_inode) { |
|---|
| 2497 | + iput(root->ino_cache_inode); |
|---|
| 2498 | + root->ino_cache_inode = NULL; |
|---|
| 2499 | + } |
|---|
| 2371 | 2500 | |
|---|
| 2372 | 2501 | if (btrfs_header_backref_rev(root->node) < |
|---|
| 2373 | 2502 | BTRFS_MIXED_BACKREF_REV) |
|---|
| 2374 | | - ret = btrfs_drop_snapshot(root, NULL, 0, 0); |
|---|
| 2503 | + ret = btrfs_drop_snapshot(root, 0, 0); |
|---|
| 2375 | 2504 | else |
|---|
| 2376 | | - ret = btrfs_drop_snapshot(root, NULL, 1, 0); |
|---|
| 2505 | + ret = btrfs_drop_snapshot(root, 1, 0); |
|---|
| 2377 | 2506 | |
|---|
| 2507 | + btrfs_put_root(root); |
|---|
| 2378 | 2508 | return (ret < 0) ? 0 : 1; |
|---|
| 2379 | 2509 | } |
|---|
| 2380 | 2510 | |
|---|