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