19888c340SDavid Sterba /* SPDX-License-Identifier: GPL-2.0 */
26cbd5570SChris Mason /*
36cbd5570SChris Mason * Copyright (C) 2007 Oracle. All rights reserved.
46cbd5570SChris Mason */
56cbd5570SChris Mason
69888c340SDavid Sterba #ifndef BTRFS_TRANSACTION_H
79888c340SDavid Sterba #define BTRFS_TRANSACTION_H
89b64f57dSElena Reshetova
99b64f57dSElena Reshetova #include <linux/refcount.h>
1031f3c99bSChris Mason #include "btrfs_inode.h"
1156bec294SChris Mason #include "delayed-ref.h"
12bed92eaeSArne Jansen #include "ctree.h"
13cc37ea61SDavid Sterba #include "misc.h"
14e089f05cSChris Mason
15*624bc6f6SBoris Burkov /* Radix-tree tag for roots that are part of the trasaction. */
16*624bc6f6SBoris Burkov #define BTRFS_ROOT_TRANS_TAG 0
17*624bc6f6SBoris Burkov
184a9d8bdeSMiao Xie enum btrfs_trans_state {
19bbe339ccSDavid Sterba TRANS_STATE_RUNNING,
2077d20c68SJosef Bacik TRANS_STATE_COMMIT_PREP,
21bbe339ccSDavid Sterba TRANS_STATE_COMMIT_START,
22bbe339ccSDavid Sterba TRANS_STATE_COMMIT_DOING,
23bbe339ccSDavid Sterba TRANS_STATE_UNBLOCKED,
24d0c2f4faSFilipe Manana TRANS_STATE_SUPER_COMMITTED,
25bbe339ccSDavid Sterba TRANS_STATE_COMPLETED,
26bbe339ccSDavid Sterba TRANS_STATE_MAX,
274a9d8bdeSMiao Xie };
284a9d8bdeSMiao Xie
293204d33cSJosef Bacik #define BTRFS_TRANS_HAVE_FREE_BGS 0
303204d33cSJosef Bacik #define BTRFS_TRANS_DIRTY_BG_RUN 1
312968b1f4SJosef Bacik #define BTRFS_TRANS_CACHE_ENOSPC 2
323204d33cSJosef Bacik
3379154b1bSChris Mason struct btrfs_transaction {
3479154b1bSChris Mason u64 transid;
35b7ec40d7SChris Mason /*
360860adfdSMiao Xie * total external writers(USERSPACE/START/ATTACH) in this
370860adfdSMiao Xie * transaction, it must be zero before the transaction is
380860adfdSMiao Xie * being committed
390860adfdSMiao Xie */
400860adfdSMiao Xie atomic_t num_extwriters;
410860adfdSMiao Xie /*
42b7ec40d7SChris Mason * total writers in this transaction, it must be zero before the
43b7ec40d7SChris Mason * transaction can end
44b7ec40d7SChris Mason */
4513c5a93eSJosef Bacik atomic_t num_writers;
469b64f57dSElena Reshetova refcount_t use_count;
47b7ec40d7SChris Mason
483204d33cSJosef Bacik unsigned long flags;
4913212b54SZhao Lei
504a9d8bdeSMiao Xie /* Be protected by fs_info->trans_lock when we want to change it. */
514a9d8bdeSMiao Xie enum btrfs_trans_state state;
525302e089SDavid Sterba int aborted;
538fd17795SChris Mason struct list_head list;
54d1310b2eSChris Mason struct extent_io_tree dirty_pages;
55a944442cSAllen Pais time64_t start_time;
5679154b1bSChris Mason wait_queue_head_t writer_wait;
5779154b1bSChris Mason wait_queue_head_t commit_wait;
583063d29fSChris Mason struct list_head pending_snapshots;
59bbbf7243SNikolay Borisov struct list_head dev_update_list;
609e351cc8SJosef Bacik struct list_head switch_commits;
61ce93ec54SJosef Bacik struct list_head dirty_bgs;
6245ae2c18SNikolay Borisov
6345ae2c18SNikolay Borisov /*
6445ae2c18SNikolay Borisov * There is no explicit lock which protects io_bgs, rather its
6545ae2c18SNikolay Borisov * consistency is implied by the fact that all the sites which modify
6645ae2c18SNikolay Borisov * it do so under some form of transaction critical section, namely:
6745ae2c18SNikolay Borisov *
6845ae2c18SNikolay Borisov * - btrfs_start_dirty_block_groups - This function can only ever be
6945ae2c18SNikolay Borisov * run by one of the transaction committers. Refer to
7045ae2c18SNikolay Borisov * BTRFS_TRANS_DIRTY_BG_RUN usage in btrfs_commit_transaction
7145ae2c18SNikolay Borisov *
7245ae2c18SNikolay Borisov * - btrfs_write_dirty_blockgroups - this is called by
7345ae2c18SNikolay Borisov * commit_cowonly_roots from transaction critical section
7445ae2c18SNikolay Borisov * (TRANS_STATE_COMMIT_DOING)
7545ae2c18SNikolay Borisov *
7645ae2c18SNikolay Borisov * - btrfs_cleanup_dirty_bgs - called on transaction abort
7745ae2c18SNikolay Borisov */
781bbc621eSChris Mason struct list_head io_bgs;
792b9dbef2SJosef Bacik struct list_head dropped_roots;
80fe119a6eSNikolay Borisov struct extent_io_tree pinned_extents;
811bbc621eSChris Mason
821bbc621eSChris Mason /*
831bbc621eSChris Mason * we need to make sure block group deletion doesn't race with
841bbc621eSChris Mason * free space cache writeout. This mutex keeps them from stomping
851bbc621eSChris Mason * on each other
861bbc621eSChris Mason */
871bbc621eSChris Mason struct mutex cache_write_mutex;
88ce93ec54SJosef Bacik spinlock_t dirty_bgs_lock;
89348a0013SFilipe Manana /* Protected by spin lock fs_info->unused_bgs_lock. */
90e33e17eeSJeff Mahoney struct list_head deleted_bgs;
912b9dbef2SJosef Bacik spinlock_t dropped_roots_lock;
9256bec294SChris Mason struct btrfs_delayed_ref_root delayed_refs;
93ab8d0fc4SJeff Mahoney struct btrfs_fs_info *fs_info;
9448778179SFilipe Manana
9548778179SFilipe Manana /*
9648778179SFilipe Manana * Number of ordered extents the transaction must wait for before
9748778179SFilipe Manana * committing. These are ordered extents started by a fast fsync.
9848778179SFilipe Manana */
9948778179SFilipe Manana atomic_t pending_ordered;
10048778179SFilipe Manana wait_queue_head_t pending_wait;
10179154b1bSChris Mason };
10279154b1bSChris Mason
103cc37ea61SDavid Sterba enum {
104cc37ea61SDavid Sterba ENUM_BIT(__TRANS_FREEZABLE),
105cc37ea61SDavid Sterba ENUM_BIT(__TRANS_START),
106cc37ea61SDavid Sterba ENUM_BIT(__TRANS_ATTACH),
107cc37ea61SDavid Sterba ENUM_BIT(__TRANS_JOIN),
108cc37ea61SDavid Sterba ENUM_BIT(__TRANS_JOIN_NOLOCK),
109cc37ea61SDavid Sterba ENUM_BIT(__TRANS_DUMMY),
110cc37ea61SDavid Sterba ENUM_BIT(__TRANS_JOIN_NOSTART),
111cc37ea61SDavid Sterba };
1120860adfdSMiao Xie
1130860adfdSMiao Xie #define TRANS_START (__TRANS_START | __TRANS_FREEZABLE)
1140860adfdSMiao Xie #define TRANS_ATTACH (__TRANS_ATTACH)
1150860adfdSMiao Xie #define TRANS_JOIN (__TRANS_JOIN | __TRANS_FREEZABLE)
1160860adfdSMiao Xie #define TRANS_JOIN_NOLOCK (__TRANS_JOIN_NOLOCK)
117a6d155d2SFilipe Manana #define TRANS_JOIN_NOSTART (__TRANS_JOIN_NOSTART)
1180860adfdSMiao Xie
119bcf3a3e7SNikolay Borisov #define TRANS_EXTWRITERS (__TRANS_START | __TRANS_ATTACH)
120a698d075SMiao Xie
121e089f05cSChris Mason struct btrfs_trans_handle {
122e089f05cSChris Mason u64 transid;
123f0486c68SYan, Zheng u64 bytes_reserved;
1244fbcdf66SFilipe Manana u64 chunk_bytes_reserved;
12556bec294SChris Mason unsigned long delayed_ref_updates;
126f0486c68SYan, Zheng struct btrfs_transaction *transaction;
127f0486c68SYan, Zheng struct btrfs_block_rsv *block_rsv;
1282a1eb461SJosef Bacik struct btrfs_block_rsv *orig_rsv;
12928b21c55SFilipe Manana /* Set by a task that wants to create a snapshot. */
13028b21c55SFilipe Manana struct btrfs_pending_snapshot *pending_snapshot;
1311ca4bb63SDavid Sterba refcount_t use_count;
1321ca4bb63SDavid Sterba unsigned int type;
133bf31f87fSDavid Sterba /*
134bf31f87fSDavid Sterba * Error code of transaction abort, set outside of locks and must use
135bf31f87fSDavid Sterba * the READ_ONCE/WRITE_ONCE access
136bf31f87fSDavid Sterba */
137a698d075SMiao Xie short aborted;
1387c2871a2SDavid Sterba bool adding_csums;
139c6b305a8SJosef Bacik bool allocating_chunk;
14079bd3712SFilipe Manana bool removing_chunk;
14120dd2cbfSMiao Xie bool reloc_reserved;
142d0c2f4faSFilipe Manana bool in_fsync;
14364b63580SJeff Mahoney struct btrfs_fs_info *fs_info;
144ea658badSJosef Bacik struct list_head new_bgs;
145e089f05cSChris Mason };
146e089f05cSChris Mason
147bf31f87fSDavid Sterba /*
148bf31f87fSDavid Sterba * The abort status can be changed between calls and is not protected by locks.
149bf31f87fSDavid Sterba * This accepts btrfs_transaction and btrfs_trans_handle as types. Once it's
150bf31f87fSDavid Sterba * set to a non-zero value it does not change, so the macro should be in checks
151bf31f87fSDavid Sterba * but is not necessary for further reads of the value.
152bf31f87fSDavid Sterba */
153bf31f87fSDavid Sterba #define TRANS_ABORTED(trans) (unlikely(READ_ONCE((trans)->aborted)))
154bf31f87fSDavid Sterba
1553063d29fSChris Mason struct btrfs_pending_snapshot {
1563de4586cSChris Mason struct dentry *dentry;
157e9662f70SMiao Xie struct inode *dir;
1583063d29fSChris Mason struct btrfs_root *root;
159b0c0ea63SDavid Sterba struct btrfs_root_item *root_item;
160a22285a6SYan, Zheng struct btrfs_root *snap;
1616f72c7e2SArne Jansen struct btrfs_qgroup_inherit *inherit;
1628546b570SDavid Sterba struct btrfs_path *path;
163a22285a6SYan, Zheng /* block reservation for the operation */
164a22285a6SYan, Zheng struct btrfs_block_rsv block_rsv;
16501327610SNicholas D Steeves /* extra metadata reservation for relocation */
166a22285a6SYan, Zheng int error;
1672dfb1e43SQu Wenruo /* Preallocated anonymous block device number */
1682dfb1e43SQu Wenruo dev_t anon_dev;
169b83cc969SLi Zefan bool readonly;
1703063d29fSChris Mason struct list_head list;
1713063d29fSChris Mason };
1723063d29fSChris Mason
btrfs_set_inode_last_trans(struct btrfs_trans_handle * trans,struct btrfs_inode * inode)17315ee9bc7SJosef Bacik static inline void btrfs_set_inode_last_trans(struct btrfs_trans_handle *trans,
174d9094414SNikolay Borisov struct btrfs_inode *inode)
17515ee9bc7SJosef Bacik {
176d9094414SNikolay Borisov spin_lock(&inode->lock);
177d9094414SNikolay Borisov inode->last_trans = trans->transaction->transid;
178d9094414SNikolay Borisov inode->last_sub_trans = inode->root->log_transid;
179bc0939fcSFilipe Manana inode->last_log_commit = inode->last_sub_trans - 1;
180d9094414SNikolay Borisov spin_unlock(&inode->lock);
18115ee9bc7SJosef Bacik }
18215ee9bc7SJosef Bacik
1839086db86SQu Wenruo /*
1849086db86SQu Wenruo * Make qgroup codes to skip given qgroupid, means the old/new_roots for
1859086db86SQu Wenruo * qgroup won't contain the qgroupid in it.
1869086db86SQu Wenruo */
btrfs_set_skip_qgroup(struct btrfs_trans_handle * trans,u64 qgroupid)1879086db86SQu Wenruo static inline void btrfs_set_skip_qgroup(struct btrfs_trans_handle *trans,
1889086db86SQu Wenruo u64 qgroupid)
1899086db86SQu Wenruo {
1909086db86SQu Wenruo struct btrfs_delayed_ref_root *delayed_refs;
1919086db86SQu Wenruo
1929086db86SQu Wenruo delayed_refs = &trans->transaction->delayed_refs;
1939086db86SQu Wenruo WARN_ON(delayed_refs->qgroup_to_skip);
1949086db86SQu Wenruo delayed_refs->qgroup_to_skip = qgroupid;
1959086db86SQu Wenruo }
1969086db86SQu Wenruo
btrfs_clear_skip_qgroup(struct btrfs_trans_handle * trans)1979086db86SQu Wenruo static inline void btrfs_clear_skip_qgroup(struct btrfs_trans_handle *trans)
1989086db86SQu Wenruo {
1999086db86SQu Wenruo struct btrfs_delayed_ref_root *delayed_refs;
2009086db86SQu Wenruo
2019086db86SQu Wenruo delayed_refs = &trans->transaction->delayed_refs;
2029086db86SQu Wenruo WARN_ON(!delayed_refs->qgroup_to_skip);
2039086db86SQu Wenruo delayed_refs->qgroup_to_skip = 0;
2049086db86SQu Wenruo }
2059086db86SQu Wenruo
206fccf0c84SJosef Bacik bool __cold abort_should_print_stack(int errno);
207fccf0c84SJosef Bacik
208fccf0c84SJosef Bacik /*
209fccf0c84SJosef Bacik * Call btrfs_abort_transaction as early as possible when an error condition is
210fccf0c84SJosef Bacik * detected, that way the exact stack trace is reported for some errors.
211fccf0c84SJosef Bacik */
212fccf0c84SJosef Bacik #define btrfs_abort_transaction(trans, errno) \
213fccf0c84SJosef Bacik do { \
214fccf0c84SJosef Bacik bool first = false; \
215fccf0c84SJosef Bacik /* Report first abort since mount */ \
216fccf0c84SJosef Bacik if (!test_and_set_bit(BTRFS_FS_STATE_TRANS_ABORTED, \
217fccf0c84SJosef Bacik &((trans)->fs_info->fs_state))) { \
218fccf0c84SJosef Bacik first = true; \
219fccf0c84SJosef Bacik if (WARN(abort_should_print_stack(errno), \
220fccf0c84SJosef Bacik KERN_ERR \
221fccf0c84SJosef Bacik "BTRFS: Transaction aborted (error %d)\n", \
222fccf0c84SJosef Bacik (errno))) { \
223fccf0c84SJosef Bacik /* Stack trace printed. */ \
224fccf0c84SJosef Bacik } else { \
225f8d1b011SFilipe Manana btrfs_err((trans)->fs_info, \
226fccf0c84SJosef Bacik "Transaction aborted (error %d)", \
227fccf0c84SJosef Bacik (errno)); \
228fccf0c84SJosef Bacik } \
229fccf0c84SJosef Bacik } \
230fccf0c84SJosef Bacik __btrfs_abort_transaction((trans), __func__, \
231fccf0c84SJosef Bacik __LINE__, (errno), first); \
232fccf0c84SJosef Bacik } while (0)
233fccf0c84SJosef Bacik
2343a45bb20SJeff Mahoney int btrfs_end_transaction(struct btrfs_trans_handle *trans);
23579154b1bSChris Mason struct btrfs_trans_handle *btrfs_start_transaction(struct btrfs_root *root,
2365aed1dd8SAlexandru Moise unsigned int num_items);
2378eab77ffSFilipe Manana struct btrfs_trans_handle *btrfs_start_transaction_fallback_global_rsv(
2388eab77ffSFilipe Manana struct btrfs_root *root,
2397f9fe614SJosef Bacik unsigned int num_items);
2407a7eaa40SJosef Bacik struct btrfs_trans_handle *btrfs_join_transaction(struct btrfs_root *root);
2418d510121SNikolay Borisov struct btrfs_trans_handle *btrfs_join_transaction_spacecache(struct btrfs_root *root);
242a6d155d2SFilipe Manana struct btrfs_trans_handle *btrfs_join_transaction_nostart(struct btrfs_root *root);
243354aa0fbSMiao Xie struct btrfs_trans_handle *btrfs_attach_transaction(struct btrfs_root *root);
244d4edf39bSMiao Xie struct btrfs_trans_handle *btrfs_attach_transaction_barrier(
245d4edf39bSMiao Xie struct btrfs_root *root);
2462ff7e61eSJeff Mahoney int btrfs_wait_for_commit(struct btrfs_fs_info *fs_info, u64 transid);
24708607c1bSChris Mason
248cfad392bSJosef Bacik void btrfs_add_dead_root(struct btrfs_root *root);
249de78b51aSEric Sandeen int btrfs_defrag_root(struct btrfs_root *root);
250b4be6aefSJosef Bacik void btrfs_maybe_wake_unfinished_drop(struct btrfs_fs_info *fs_info);
25133c44184SJosef Bacik int btrfs_clean_one_deleted_snapshot(struct btrfs_fs_info *fs_info);
2523a45bb20SJeff Mahoney int btrfs_commit_transaction(struct btrfs_trans_handle *trans);
253fdfbf020SJosef Bacik void btrfs_commit_transaction_async(struct btrfs_trans_handle *trans);
2543a45bb20SJeff Mahoney int btrfs_end_transaction_throttle(struct btrfs_trans_handle *trans);
255a2633b6aSNikolay Borisov bool btrfs_should_end_transaction(struct btrfs_trans_handle *trans);
2562ff7e61eSJeff Mahoney void btrfs_throttle(struct btrfs_fs_info *fs_info);
2575d4f98a2SYan Zheng int btrfs_record_root_in_trans(struct btrfs_trans_handle *trans,
2585d4f98a2SYan Zheng struct btrfs_root *root);
2592ff7e61eSJeff Mahoney int btrfs_write_marked_extents(struct btrfs_fs_info *fs_info,
2608cef4e16SYan, Zheng struct extent_io_tree *dirty_pages, int mark);
261bf89d38fSJeff Mahoney int btrfs_wait_tree_log_extents(struct btrfs_root *root, int mark);
2628929ecfaSYan, Zheng int btrfs_transaction_blocked(struct btrfs_fs_info *info);
263f36f3042SChris Mason int btrfs_transaction_in_commit(struct btrfs_fs_info *info);
264724e2315SJosef Bacik void btrfs_put_transaction(struct btrfs_transaction *transaction);
2652b9dbef2SJosef Bacik void btrfs_add_dropped_root(struct btrfs_trans_handle *trans,
2662b9dbef2SJosef Bacik struct btrfs_root *root);
267fb6dea26SJosef Bacik void btrfs_trans_release_chunk_metadata(struct btrfs_trans_handle *trans);
268fccf0c84SJosef Bacik void __cold __btrfs_abort_transaction(struct btrfs_trans_handle *trans,
269fccf0c84SJosef Bacik const char *function,
270fccf0c84SJosef Bacik unsigned int line, int errno, bool first_hit);
2719888c340SDavid Sterba
272956504a3SJosef Bacik int __init btrfs_transaction_init(void);
273956504a3SJosef Bacik void __cold btrfs_transaction_exit(void);
274956504a3SJosef Bacik
275e089f05cSChris Mason #endif
276