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*7b5595f3SFilipe Manana /*
16*7b5595f3SFilipe Manana * Signal that a direct IO write is in progress, to avoid deadlock for sync
17*7b5595f3SFilipe Manana * direct IO writes when fsync is called during the direct IO write path.
18*7b5595f3SFilipe Manana */
19*7b5595f3SFilipe Manana #define BTRFS_TRANS_DIO_WRITE_STUB ((void *) 1)
20*7b5595f3SFilipe Manana
21624bc6f6SBoris Burkov /* Radix-tree tag for roots that are part of the trasaction. */
22624bc6f6SBoris Burkov #define BTRFS_ROOT_TRANS_TAG 0
23624bc6f6SBoris Burkov
244a9d8bdeSMiao Xie enum btrfs_trans_state {
25bbe339ccSDavid Sterba TRANS_STATE_RUNNING,
2677d20c68SJosef Bacik TRANS_STATE_COMMIT_PREP,
27bbe339ccSDavid Sterba TRANS_STATE_COMMIT_START,
28bbe339ccSDavid Sterba TRANS_STATE_COMMIT_DOING,
29bbe339ccSDavid Sterba TRANS_STATE_UNBLOCKED,
30d0c2f4faSFilipe Manana TRANS_STATE_SUPER_COMMITTED,
31bbe339ccSDavid Sterba TRANS_STATE_COMPLETED,
32bbe339ccSDavid Sterba TRANS_STATE_MAX,
334a9d8bdeSMiao Xie };
344a9d8bdeSMiao Xie
353204d33cSJosef Bacik #define BTRFS_TRANS_HAVE_FREE_BGS 0
363204d33cSJosef Bacik #define BTRFS_TRANS_DIRTY_BG_RUN 1
372968b1f4SJosef Bacik #define BTRFS_TRANS_CACHE_ENOSPC 2
383204d33cSJosef Bacik
3979154b1bSChris Mason struct btrfs_transaction {
4079154b1bSChris Mason u64 transid;
41b7ec40d7SChris Mason /*
420860adfdSMiao Xie * total external writers(USERSPACE/START/ATTACH) in this
430860adfdSMiao Xie * transaction, it must be zero before the transaction is
440860adfdSMiao Xie * being committed
450860adfdSMiao Xie */
460860adfdSMiao Xie atomic_t num_extwriters;
470860adfdSMiao Xie /*
48b7ec40d7SChris Mason * total writers in this transaction, it must be zero before the
49b7ec40d7SChris Mason * transaction can end
50b7ec40d7SChris Mason */
5113c5a93eSJosef Bacik atomic_t num_writers;
529b64f57dSElena Reshetova refcount_t use_count;
53b7ec40d7SChris Mason
543204d33cSJosef Bacik unsigned long flags;
5513212b54SZhao Lei
564a9d8bdeSMiao Xie /* Be protected by fs_info->trans_lock when we want to change it. */
574a9d8bdeSMiao Xie enum btrfs_trans_state state;
585302e089SDavid Sterba int aborted;
598fd17795SChris Mason struct list_head list;
60d1310b2eSChris Mason struct extent_io_tree dirty_pages;
61a944442cSAllen Pais time64_t start_time;
6279154b1bSChris Mason wait_queue_head_t writer_wait;
6379154b1bSChris Mason wait_queue_head_t commit_wait;
643063d29fSChris Mason struct list_head pending_snapshots;
65bbbf7243SNikolay Borisov struct list_head dev_update_list;
669e351cc8SJosef Bacik struct list_head switch_commits;
67ce93ec54SJosef Bacik struct list_head dirty_bgs;
6845ae2c18SNikolay Borisov
6945ae2c18SNikolay Borisov /*
7045ae2c18SNikolay Borisov * There is no explicit lock which protects io_bgs, rather its
7145ae2c18SNikolay Borisov * consistency is implied by the fact that all the sites which modify
7245ae2c18SNikolay Borisov * it do so under some form of transaction critical section, namely:
7345ae2c18SNikolay Borisov *
7445ae2c18SNikolay Borisov * - btrfs_start_dirty_block_groups - This function can only ever be
7545ae2c18SNikolay Borisov * run by one of the transaction committers. Refer to
7645ae2c18SNikolay Borisov * BTRFS_TRANS_DIRTY_BG_RUN usage in btrfs_commit_transaction
7745ae2c18SNikolay Borisov *
7845ae2c18SNikolay Borisov * - btrfs_write_dirty_blockgroups - this is called by
7945ae2c18SNikolay Borisov * commit_cowonly_roots from transaction critical section
8045ae2c18SNikolay Borisov * (TRANS_STATE_COMMIT_DOING)
8145ae2c18SNikolay Borisov *
8245ae2c18SNikolay Borisov * - btrfs_cleanup_dirty_bgs - called on transaction abort
8345ae2c18SNikolay Borisov */
841bbc621eSChris Mason struct list_head io_bgs;
852b9dbef2SJosef Bacik struct list_head dropped_roots;
86fe119a6eSNikolay Borisov struct extent_io_tree pinned_extents;
871bbc621eSChris Mason
881bbc621eSChris Mason /*
891bbc621eSChris Mason * we need to make sure block group deletion doesn't race with
901bbc621eSChris Mason * free space cache writeout. This mutex keeps them from stomping
911bbc621eSChris Mason * on each other
921bbc621eSChris Mason */
931bbc621eSChris Mason struct mutex cache_write_mutex;
94ce93ec54SJosef Bacik spinlock_t dirty_bgs_lock;
95348a0013SFilipe Manana /* Protected by spin lock fs_info->unused_bgs_lock. */
96e33e17eeSJeff Mahoney struct list_head deleted_bgs;
972b9dbef2SJosef Bacik spinlock_t dropped_roots_lock;
9856bec294SChris Mason struct btrfs_delayed_ref_root delayed_refs;
99ab8d0fc4SJeff Mahoney struct btrfs_fs_info *fs_info;
10048778179SFilipe Manana
10148778179SFilipe Manana /*
10248778179SFilipe Manana * Number of ordered extents the transaction must wait for before
10348778179SFilipe Manana * committing. These are ordered extents started by a fast fsync.
10448778179SFilipe Manana */
10548778179SFilipe Manana atomic_t pending_ordered;
10648778179SFilipe Manana wait_queue_head_t pending_wait;
10779154b1bSChris Mason };
10879154b1bSChris Mason
109cc37ea61SDavid Sterba enum {
110cc37ea61SDavid Sterba ENUM_BIT(__TRANS_FREEZABLE),
111cc37ea61SDavid Sterba ENUM_BIT(__TRANS_START),
112cc37ea61SDavid Sterba ENUM_BIT(__TRANS_ATTACH),
113cc37ea61SDavid Sterba ENUM_BIT(__TRANS_JOIN),
114cc37ea61SDavid Sterba ENUM_BIT(__TRANS_JOIN_NOLOCK),
115cc37ea61SDavid Sterba ENUM_BIT(__TRANS_DUMMY),
116cc37ea61SDavid Sterba ENUM_BIT(__TRANS_JOIN_NOSTART),
117cc37ea61SDavid Sterba };
1180860adfdSMiao Xie
1190860adfdSMiao Xie #define TRANS_START (__TRANS_START | __TRANS_FREEZABLE)
1200860adfdSMiao Xie #define TRANS_ATTACH (__TRANS_ATTACH)
1210860adfdSMiao Xie #define TRANS_JOIN (__TRANS_JOIN | __TRANS_FREEZABLE)
1220860adfdSMiao Xie #define TRANS_JOIN_NOLOCK (__TRANS_JOIN_NOLOCK)
123a6d155d2SFilipe Manana #define TRANS_JOIN_NOSTART (__TRANS_JOIN_NOSTART)
1240860adfdSMiao Xie
125bcf3a3e7SNikolay Borisov #define TRANS_EXTWRITERS (__TRANS_START | __TRANS_ATTACH)
126a698d075SMiao Xie
127e089f05cSChris Mason struct btrfs_trans_handle {
128e089f05cSChris Mason u64 transid;
129f0486c68SYan, Zheng u64 bytes_reserved;
1304fbcdf66SFilipe Manana u64 chunk_bytes_reserved;
13156bec294SChris Mason unsigned long delayed_ref_updates;
132f0486c68SYan, Zheng struct btrfs_transaction *transaction;
133f0486c68SYan, Zheng struct btrfs_block_rsv *block_rsv;
1342a1eb461SJosef Bacik struct btrfs_block_rsv *orig_rsv;
13528b21c55SFilipe Manana /* Set by a task that wants to create a snapshot. */
13628b21c55SFilipe Manana struct btrfs_pending_snapshot *pending_snapshot;
1371ca4bb63SDavid Sterba refcount_t use_count;
1381ca4bb63SDavid Sterba unsigned int type;
139bf31f87fSDavid Sterba /*
140bf31f87fSDavid Sterba * Error code of transaction abort, set outside of locks and must use
141bf31f87fSDavid Sterba * the READ_ONCE/WRITE_ONCE access
142bf31f87fSDavid Sterba */
143a698d075SMiao Xie short aborted;
1447c2871a2SDavid Sterba bool adding_csums;
145c6b305a8SJosef Bacik bool allocating_chunk;
14679bd3712SFilipe Manana bool removing_chunk;
14720dd2cbfSMiao Xie bool reloc_reserved;
148d0c2f4faSFilipe Manana bool in_fsync;
14964b63580SJeff Mahoney struct btrfs_fs_info *fs_info;
150ea658badSJosef Bacik struct list_head new_bgs;
151e089f05cSChris Mason };
152e089f05cSChris Mason
153bf31f87fSDavid Sterba /*
154bf31f87fSDavid Sterba * The abort status can be changed between calls and is not protected by locks.
155bf31f87fSDavid Sterba * This accepts btrfs_transaction and btrfs_trans_handle as types. Once it's
156bf31f87fSDavid Sterba * set to a non-zero value it does not change, so the macro should be in checks
157bf31f87fSDavid Sterba * but is not necessary for further reads of the value.
158bf31f87fSDavid Sterba */
159bf31f87fSDavid Sterba #define TRANS_ABORTED(trans) (unlikely(READ_ONCE((trans)->aborted)))
160bf31f87fSDavid Sterba
1613063d29fSChris Mason struct btrfs_pending_snapshot {
1623de4586cSChris Mason struct dentry *dentry;
163e9662f70SMiao Xie struct inode *dir;
1643063d29fSChris Mason struct btrfs_root *root;
165b0c0ea63SDavid Sterba struct btrfs_root_item *root_item;
166a22285a6SYan, Zheng struct btrfs_root *snap;
1676f72c7e2SArne Jansen struct btrfs_qgroup_inherit *inherit;
1688546b570SDavid Sterba struct btrfs_path *path;
169a22285a6SYan, Zheng /* block reservation for the operation */
170a22285a6SYan, Zheng struct btrfs_block_rsv block_rsv;
17101327610SNicholas D Steeves /* extra metadata reservation for relocation */
172a22285a6SYan, Zheng int error;
1732dfb1e43SQu Wenruo /* Preallocated anonymous block device number */
1742dfb1e43SQu Wenruo dev_t anon_dev;
175b83cc969SLi Zefan bool readonly;
1763063d29fSChris Mason struct list_head list;
1773063d29fSChris Mason };
1783063d29fSChris Mason
btrfs_set_inode_last_trans(struct btrfs_trans_handle * trans,struct btrfs_inode * inode)17915ee9bc7SJosef Bacik static inline void btrfs_set_inode_last_trans(struct btrfs_trans_handle *trans,
180d9094414SNikolay Borisov struct btrfs_inode *inode)
18115ee9bc7SJosef Bacik {
182d9094414SNikolay Borisov spin_lock(&inode->lock);
183d9094414SNikolay Borisov inode->last_trans = trans->transaction->transid;
184d9094414SNikolay Borisov inode->last_sub_trans = inode->root->log_transid;
185bc0939fcSFilipe Manana inode->last_log_commit = inode->last_sub_trans - 1;
186d9094414SNikolay Borisov spin_unlock(&inode->lock);
18715ee9bc7SJosef Bacik }
18815ee9bc7SJosef Bacik
1899086db86SQu Wenruo /*
1909086db86SQu Wenruo * Make qgroup codes to skip given qgroupid, means the old/new_roots for
1919086db86SQu Wenruo * qgroup won't contain the qgroupid in it.
1929086db86SQu Wenruo */
btrfs_set_skip_qgroup(struct btrfs_trans_handle * trans,u64 qgroupid)1939086db86SQu Wenruo static inline void btrfs_set_skip_qgroup(struct btrfs_trans_handle *trans,
1949086db86SQu Wenruo u64 qgroupid)
1959086db86SQu Wenruo {
1969086db86SQu Wenruo struct btrfs_delayed_ref_root *delayed_refs;
1979086db86SQu Wenruo
1989086db86SQu Wenruo delayed_refs = &trans->transaction->delayed_refs;
1999086db86SQu Wenruo WARN_ON(delayed_refs->qgroup_to_skip);
2009086db86SQu Wenruo delayed_refs->qgroup_to_skip = qgroupid;
2019086db86SQu Wenruo }
2029086db86SQu Wenruo
btrfs_clear_skip_qgroup(struct btrfs_trans_handle * trans)2039086db86SQu Wenruo static inline void btrfs_clear_skip_qgroup(struct btrfs_trans_handle *trans)
2049086db86SQu Wenruo {
2059086db86SQu Wenruo struct btrfs_delayed_ref_root *delayed_refs;
2069086db86SQu Wenruo
2079086db86SQu Wenruo delayed_refs = &trans->transaction->delayed_refs;
2089086db86SQu Wenruo WARN_ON(!delayed_refs->qgroup_to_skip);
2099086db86SQu Wenruo delayed_refs->qgroup_to_skip = 0;
2109086db86SQu Wenruo }
2119086db86SQu Wenruo
212fccf0c84SJosef Bacik bool __cold abort_should_print_stack(int errno);
213fccf0c84SJosef Bacik
214fccf0c84SJosef Bacik /*
215fccf0c84SJosef Bacik * Call btrfs_abort_transaction as early as possible when an error condition is
216fccf0c84SJosef Bacik * detected, that way the exact stack trace is reported for some errors.
217fccf0c84SJosef Bacik */
218fccf0c84SJosef Bacik #define btrfs_abort_transaction(trans, errno) \
219fccf0c84SJosef Bacik do { \
220fccf0c84SJosef Bacik bool first = false; \
221fccf0c84SJosef Bacik /* Report first abort since mount */ \
222fccf0c84SJosef Bacik if (!test_and_set_bit(BTRFS_FS_STATE_TRANS_ABORTED, \
223fccf0c84SJosef Bacik &((trans)->fs_info->fs_state))) { \
224fccf0c84SJosef Bacik first = true; \
225fccf0c84SJosef Bacik if (WARN(abort_should_print_stack(errno), \
226fccf0c84SJosef Bacik KERN_ERR \
227fccf0c84SJosef Bacik "BTRFS: Transaction aborted (error %d)\n", \
228fccf0c84SJosef Bacik (errno))) { \
229fccf0c84SJosef Bacik /* Stack trace printed. */ \
230fccf0c84SJosef Bacik } else { \
231f8d1b011SFilipe Manana btrfs_err((trans)->fs_info, \
232fccf0c84SJosef Bacik "Transaction aborted (error %d)", \
233fccf0c84SJosef Bacik (errno)); \
234fccf0c84SJosef Bacik } \
235fccf0c84SJosef Bacik } \
236fccf0c84SJosef Bacik __btrfs_abort_transaction((trans), __func__, \
237fccf0c84SJosef Bacik __LINE__, (errno), first); \
238fccf0c84SJosef Bacik } while (0)
239fccf0c84SJosef Bacik
2403a45bb20SJeff Mahoney int btrfs_end_transaction(struct btrfs_trans_handle *trans);
24179154b1bSChris Mason struct btrfs_trans_handle *btrfs_start_transaction(struct btrfs_root *root,
2425aed1dd8SAlexandru Moise unsigned int num_items);
2438eab77ffSFilipe Manana struct btrfs_trans_handle *btrfs_start_transaction_fallback_global_rsv(
2448eab77ffSFilipe Manana struct btrfs_root *root,
2457f9fe614SJosef Bacik unsigned int num_items);
2467a7eaa40SJosef Bacik struct btrfs_trans_handle *btrfs_join_transaction(struct btrfs_root *root);
2478d510121SNikolay Borisov struct btrfs_trans_handle *btrfs_join_transaction_spacecache(struct btrfs_root *root);
248a6d155d2SFilipe Manana struct btrfs_trans_handle *btrfs_join_transaction_nostart(struct btrfs_root *root);
249354aa0fbSMiao Xie struct btrfs_trans_handle *btrfs_attach_transaction(struct btrfs_root *root);
250d4edf39bSMiao Xie struct btrfs_trans_handle *btrfs_attach_transaction_barrier(
251d4edf39bSMiao Xie struct btrfs_root *root);
2522ff7e61eSJeff Mahoney int btrfs_wait_for_commit(struct btrfs_fs_info *fs_info, u64 transid);
25308607c1bSChris Mason
254cfad392bSJosef Bacik void btrfs_add_dead_root(struct btrfs_root *root);
255de78b51aSEric Sandeen int btrfs_defrag_root(struct btrfs_root *root);
256b4be6aefSJosef Bacik void btrfs_maybe_wake_unfinished_drop(struct btrfs_fs_info *fs_info);
25733c44184SJosef Bacik int btrfs_clean_one_deleted_snapshot(struct btrfs_fs_info *fs_info);
2583a45bb20SJeff Mahoney int btrfs_commit_transaction(struct btrfs_trans_handle *trans);
259fdfbf020SJosef Bacik void btrfs_commit_transaction_async(struct btrfs_trans_handle *trans);
2603a45bb20SJeff Mahoney int btrfs_end_transaction_throttle(struct btrfs_trans_handle *trans);
261a2633b6aSNikolay Borisov bool btrfs_should_end_transaction(struct btrfs_trans_handle *trans);
2622ff7e61eSJeff Mahoney void btrfs_throttle(struct btrfs_fs_info *fs_info);
2635d4f98a2SYan Zheng int btrfs_record_root_in_trans(struct btrfs_trans_handle *trans,
2645d4f98a2SYan Zheng struct btrfs_root *root);
2652ff7e61eSJeff Mahoney int btrfs_write_marked_extents(struct btrfs_fs_info *fs_info,
2668cef4e16SYan, Zheng struct extent_io_tree *dirty_pages, int mark);
267bf89d38fSJeff Mahoney int btrfs_wait_tree_log_extents(struct btrfs_root *root, int mark);
2688929ecfaSYan, Zheng int btrfs_transaction_blocked(struct btrfs_fs_info *info);
269f36f3042SChris Mason int btrfs_transaction_in_commit(struct btrfs_fs_info *info);
270724e2315SJosef Bacik void btrfs_put_transaction(struct btrfs_transaction *transaction);
2712b9dbef2SJosef Bacik void btrfs_add_dropped_root(struct btrfs_trans_handle *trans,
2722b9dbef2SJosef Bacik struct btrfs_root *root);
273fb6dea26SJosef Bacik void btrfs_trans_release_chunk_metadata(struct btrfs_trans_handle *trans);
274fccf0c84SJosef Bacik void __cold __btrfs_abort_transaction(struct btrfs_trans_handle *trans,
275fccf0c84SJosef Bacik const char *function,
276fccf0c84SJosef Bacik unsigned int line, int errno, bool first_hit);
2779888c340SDavid Sterba
278956504a3SJosef Bacik int __init btrfs_transaction_init(void);
279956504a3SJosef Bacik void __cold btrfs_transaction_exit(void);
280956504a3SJosef Bacik
281e089f05cSChris Mason #endif
282