transaction.c (bc44d7c4b2b179c4b74fba208b9908e2ecbc1b4d) transaction.c (bf31f87f71cc7a89871ab0a451c047a0c0144bf1)
1// SPDX-License-Identifier: GPL-2.0
2/*
3 * Copyright (C) 2007 Oracle. All rights reserved.
4 */
5
6#include <linux/fs.h>
7#include <linux/slab.h>
8#include <linux/sched.h>

--- 229 unchanged lines hidden (view full) ---

238 /* The file system has been taken offline. No new transactions. */
239 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) {
240 spin_unlock(&fs_info->trans_lock);
241 return -EROFS;
242 }
243
244 cur_trans = fs_info->running_transaction;
245 if (cur_trans) {
1// SPDX-License-Identifier: GPL-2.0
2/*
3 * Copyright (C) 2007 Oracle. All rights reserved.
4 */
5
6#include <linux/fs.h>
7#include <linux/slab.h>
8#include <linux/sched.h>

--- 229 unchanged lines hidden (view full) ---

238 /* The file system has been taken offline. No new transactions. */
239 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) {
240 spin_unlock(&fs_info->trans_lock);
241 return -EROFS;
242 }
243
244 cur_trans = fs_info->running_transaction;
245 if (cur_trans) {
246 if (cur_trans->aborted) {
246 if (TRANS_ABORTED(cur_trans)) {
247 spin_unlock(&fs_info->trans_lock);
248 return cur_trans->aborted;
249 }
250 if (btrfs_blocked_trans_types[cur_trans->state] & type) {
251 spin_unlock(&fs_info->trans_lock);
252 return -EBUSY;
253 }
254 refcount_inc(&cur_trans->use_count);

--- 199 unchanged lines hidden (view full) ---

454
455 return 0;
456}
457
458static inline int is_transaction_blocked(struct btrfs_transaction *trans)
459{
460 return (trans->state >= TRANS_STATE_COMMIT_START &&
461 trans->state < TRANS_STATE_UNBLOCKED &&
247 spin_unlock(&fs_info->trans_lock);
248 return cur_trans->aborted;
249 }
250 if (btrfs_blocked_trans_types[cur_trans->state] & type) {
251 spin_unlock(&fs_info->trans_lock);
252 return -EBUSY;
253 }
254 refcount_inc(&cur_trans->use_count);

--- 199 unchanged lines hidden (view full) ---

454
455 return 0;
456}
457
458static inline int is_transaction_blocked(struct btrfs_transaction *trans)
459{
460 return (trans->state >= TRANS_STATE_COMMIT_START &&
461 trans->state < TRANS_STATE_UNBLOCKED &&
462 !trans->aborted);
462 !TRANS_ABORTED(trans));
463}
464
465/* wait for commit against the current transaction to become unblocked
466 * when this is done, it is safe to start a new transaction, but the current
467 * transaction might not be fully on disk.
468 */
469static void wait_current_trans(struct btrfs_fs_info *fs_info)
470{
471 struct btrfs_transaction *cur_trans;
472
473 spin_lock(&fs_info->trans_lock);
474 cur_trans = fs_info->running_transaction;
475 if (cur_trans && is_transaction_blocked(cur_trans)) {
476 refcount_inc(&cur_trans->use_count);
477 spin_unlock(&fs_info->trans_lock);
478
479 wait_event(fs_info->transaction_wait,
480 cur_trans->state >= TRANS_STATE_UNBLOCKED ||
463}
464
465/* wait for commit against the current transaction to become unblocked
466 * when this is done, it is safe to start a new transaction, but the current
467 * transaction might not be fully on disk.
468 */
469static void wait_current_trans(struct btrfs_fs_info *fs_info)
470{
471 struct btrfs_transaction *cur_trans;
472
473 spin_lock(&fs_info->trans_lock);
474 cur_trans = fs_info->running_transaction;
475 if (cur_trans && is_transaction_blocked(cur_trans)) {
476 refcount_inc(&cur_trans->use_count);
477 spin_unlock(&fs_info->trans_lock);
478
479 wait_event(fs_info->transaction_wait,
480 cur_trans->state >= TRANS_STATE_UNBLOCKED ||
481 cur_trans->aborted);
481 TRANS_ABORTED(cur_trans));
482 btrfs_put_transaction(cur_trans);
483 } else {
484 spin_unlock(&fs_info->trans_lock);
485 }
486}
487
488static int may_wait_transaction(struct btrfs_fs_info *fs_info, int type)
489{

--- 442 unchanged lines hidden (view full) ---

932 btrfs_put_transaction(cur_trans);
933
934 if (current->journal_info == trans)
935 current->journal_info = NULL;
936
937 if (throttle)
938 btrfs_run_delayed_iputs(info);
939
482 btrfs_put_transaction(cur_trans);
483 } else {
484 spin_unlock(&fs_info->trans_lock);
485 }
486}
487
488static int may_wait_transaction(struct btrfs_fs_info *fs_info, int type)
489{

--- 442 unchanged lines hidden (view full) ---

932 btrfs_put_transaction(cur_trans);
933
934 if (current->journal_info == trans)
935 current->journal_info = NULL;
936
937 if (throttle)
938 btrfs_run_delayed_iputs(info);
939
940 if (trans->aborted ||
940 if (TRANS_ABORTED(trans) ||
941 test_bit(BTRFS_FS_STATE_ERROR, &info->fs_state)) {
942 wake_up_process(info->transaction_kthread);
943 err = -EIO;
944 }
945
946 kmem_cache_free(btrfs_trans_handle_cachep, trans);
947 return err;
948}

--- 840 unchanged lines hidden (view full) ---

1789/*
1790 * wait for the current transaction commit to start and block subsequent
1791 * transaction joins
1792 */
1793static void wait_current_trans_commit_start(struct btrfs_fs_info *fs_info,
1794 struct btrfs_transaction *trans)
1795{
1796 wait_event(fs_info->transaction_blocked_wait,
941 test_bit(BTRFS_FS_STATE_ERROR, &info->fs_state)) {
942 wake_up_process(info->transaction_kthread);
943 err = -EIO;
944 }
945
946 kmem_cache_free(btrfs_trans_handle_cachep, trans);
947 return err;
948}

--- 840 unchanged lines hidden (view full) ---

1789/*
1790 * wait for the current transaction commit to start and block subsequent
1791 * transaction joins
1792 */
1793static void wait_current_trans_commit_start(struct btrfs_fs_info *fs_info,
1794 struct btrfs_transaction *trans)
1795{
1796 wait_event(fs_info->transaction_blocked_wait,
1797 trans->state >= TRANS_STATE_COMMIT_START || trans->aborted);
1797 trans->state >= TRANS_STATE_COMMIT_START ||
1798 TRANS_ABORTED(trans));
1798}
1799
1800/*
1801 * wait for the current transaction to start and then become unblocked.
1802 * caller holds ref.
1803 */
1804static void wait_current_trans_commit_start_and_unblock(
1805 struct btrfs_fs_info *fs_info,
1806 struct btrfs_transaction *trans)
1807{
1808 wait_event(fs_info->transaction_wait,
1799}
1800
1801/*
1802 * wait for the current transaction to start and then become unblocked.
1803 * caller holds ref.
1804 */
1805static void wait_current_trans_commit_start_and_unblock(
1806 struct btrfs_fs_info *fs_info,
1807 struct btrfs_transaction *trans)
1808{
1809 wait_event(fs_info->transaction_wait,
1809 trans->state >= TRANS_STATE_UNBLOCKED || trans->aborted);
1810 trans->state >= TRANS_STATE_UNBLOCKED ||
1811 TRANS_ABORTED(trans));
1810}
1811
1812/*
1813 * commit transactions asynchronously. once btrfs_commit_transaction_async
1814 * returns, any subsequent transaction will not be allowed to join.
1815 */
1816struct btrfs_async_commit {
1817 struct btrfs_trans_handle *newtrans;

--- 203 unchanged lines hidden (view full) ---

2021 * Some places just start a transaction to commit it. We need to make
2022 * sure that if this commit fails that the abort code actually marks the
2023 * transaction as failed, so set trans->dirty to make the abort code do
2024 * the right thing.
2025 */
2026 trans->dirty = true;
2027
2028 /* Stop the commit early if ->aborted is set */
1812}
1813
1814/*
1815 * commit transactions asynchronously. once btrfs_commit_transaction_async
1816 * returns, any subsequent transaction will not be allowed to join.
1817 */
1818struct btrfs_async_commit {
1819 struct btrfs_trans_handle *newtrans;

--- 203 unchanged lines hidden (view full) ---

2023 * Some places just start a transaction to commit it. We need to make
2024 * sure that if this commit fails that the abort code actually marks the
2025 * transaction as failed, so set trans->dirty to make the abort code do
2026 * the right thing.
2027 */
2028 trans->dirty = true;
2029
2030 /* Stop the commit early if ->aborted is set */
2029 if (unlikely(READ_ONCE(cur_trans->aborted))) {
2031 if (TRANS_ABORTED(cur_trans)) {
2030 ret = cur_trans->aborted;
2031 btrfs_end_transaction(trans);
2032 return ret;
2033 }
2034
2035 btrfs_trans_release_metadata(trans);
2036 trans->block_rsv = NULL;
2037

--- 57 unchanged lines hidden (view full) ---

2095 spin_lock(&fs_info->trans_lock);
2096 if (cur_trans->state >= TRANS_STATE_COMMIT_START) {
2097 spin_unlock(&fs_info->trans_lock);
2098 refcount_inc(&cur_trans->use_count);
2099 ret = btrfs_end_transaction(trans);
2100
2101 wait_for_commit(cur_trans);
2102
2032 ret = cur_trans->aborted;
2033 btrfs_end_transaction(trans);
2034 return ret;
2035 }
2036
2037 btrfs_trans_release_metadata(trans);
2038 trans->block_rsv = NULL;
2039

--- 57 unchanged lines hidden (view full) ---

2097 spin_lock(&fs_info->trans_lock);
2098 if (cur_trans->state >= TRANS_STATE_COMMIT_START) {
2099 spin_unlock(&fs_info->trans_lock);
2100 refcount_inc(&cur_trans->use_count);
2101 ret = btrfs_end_transaction(trans);
2102
2103 wait_for_commit(cur_trans);
2104
2103 if (unlikely(cur_trans->aborted))
2105 if (TRANS_ABORTED(cur_trans))
2104 ret = cur_trans->aborted;
2105
2106 btrfs_put_transaction(cur_trans);
2107
2108 return ret;
2109 }
2110
2111 cur_trans->state = TRANS_STATE_COMMIT_START;
2112 wake_up(&fs_info->transaction_blocked_wait);
2113
2114 if (cur_trans->list.prev != &fs_info->trans_list) {
2115 prev_trans = list_entry(cur_trans->list.prev,
2116 struct btrfs_transaction, list);
2117 if (prev_trans->state != TRANS_STATE_COMPLETED) {
2118 refcount_inc(&prev_trans->use_count);
2119 spin_unlock(&fs_info->trans_lock);
2120
2121 wait_for_commit(prev_trans);
2106 ret = cur_trans->aborted;
2107
2108 btrfs_put_transaction(cur_trans);
2109
2110 return ret;
2111 }
2112
2113 cur_trans->state = TRANS_STATE_COMMIT_START;
2114 wake_up(&fs_info->transaction_blocked_wait);
2115
2116 if (cur_trans->list.prev != &fs_info->trans_list) {
2117 prev_trans = list_entry(cur_trans->list.prev,
2118 struct btrfs_transaction, list);
2119 if (prev_trans->state != TRANS_STATE_COMPLETED) {
2120 refcount_inc(&prev_trans->use_count);
2121 spin_unlock(&fs_info->trans_lock);
2122
2123 wait_for_commit(prev_trans);
2122 ret = prev_trans->aborted;
2124 ret = READ_ONCE(prev_trans->aborted);
2123
2124 btrfs_put_transaction(prev_trans);
2125 if (ret)
2126 goto cleanup_transaction;
2127 } else {
2128 spin_unlock(&fs_info->trans_lock);
2129 }
2130 } else {

--- 37 unchanged lines hidden (view full) ---

2168 * COMMIT_DOING so make sure to wait for num_writers to == 1 again.
2169 */
2170 spin_lock(&fs_info->trans_lock);
2171 cur_trans->state = TRANS_STATE_COMMIT_DOING;
2172 spin_unlock(&fs_info->trans_lock);
2173 wait_event(cur_trans->writer_wait,
2174 atomic_read(&cur_trans->num_writers) == 1);
2175
2125
2126 btrfs_put_transaction(prev_trans);
2127 if (ret)
2128 goto cleanup_transaction;
2129 } else {
2130 spin_unlock(&fs_info->trans_lock);
2131 }
2132 } else {

--- 37 unchanged lines hidden (view full) ---

2170 * COMMIT_DOING so make sure to wait for num_writers to == 1 again.
2171 */
2172 spin_lock(&fs_info->trans_lock);
2173 cur_trans->state = TRANS_STATE_COMMIT_DOING;
2174 spin_unlock(&fs_info->trans_lock);
2175 wait_event(cur_trans->writer_wait,
2176 atomic_read(&cur_trans->num_writers) == 1);
2177
2176 /* ->aborted might be set after the previous check, so check it */
2177 if (unlikely(READ_ONCE(cur_trans->aborted))) {
2178 if (TRANS_ABORTED(cur_trans)) {
2178 ret = cur_trans->aborted;
2179 goto scrub_continue;
2180 }
2181 /*
2182 * the reloc mutex makes sure that we stop
2183 * the balancing code from coming in and moving
2184 * extents around in the middle of the commit
2185 */

--- 101 unchanged lines hidden (view full) ---

2287 mutex_unlock(&fs_info->reloc_mutex);
2288 goto scrub_continue;
2289 }
2290
2291 /*
2292 * The tasks which save the space cache and inode cache may also
2293 * update ->aborted, check it.
2294 */
2179 ret = cur_trans->aborted;
2180 goto scrub_continue;
2181 }
2182 /*
2183 * the reloc mutex makes sure that we stop
2184 * the balancing code from coming in and moving
2185 * extents around in the middle of the commit
2186 */

--- 101 unchanged lines hidden (view full) ---

2288 mutex_unlock(&fs_info->reloc_mutex);
2289 goto scrub_continue;
2290 }
2291
2292 /*
2293 * The tasks which save the space cache and inode cache may also
2294 * update ->aborted, check it.
2295 */
2295 if (unlikely(READ_ONCE(cur_trans->aborted))) {
2296 if (TRANS_ABORTED(cur_trans)) {
2296 ret = cur_trans->aborted;
2297 mutex_unlock(&fs_info->tree_log_mutex);
2298 mutex_unlock(&fs_info->reloc_mutex);
2299 goto scrub_continue;
2300 }
2301
2302 btrfs_prepare_extent_commit(fs_info);
2303

--- 171 unchanged lines hidden ---
2297 ret = cur_trans->aborted;
2298 mutex_unlock(&fs_info->tree_log_mutex);
2299 mutex_unlock(&fs_info->reloc_mutex);
2300 goto scrub_continue;
2301 }
2302
2303 btrfs_prepare_extent_commit(fs_info);
2304

--- 171 unchanged lines hidden ---