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