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