162306a36Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci * Copyright (C) 2007 Oracle.  All rights reserved.
462306a36Sopenharmony_ci */
562306a36Sopenharmony_ci
662306a36Sopenharmony_ci#ifndef BTRFS_TRANSACTION_H
762306a36Sopenharmony_ci#define BTRFS_TRANSACTION_H
862306a36Sopenharmony_ci
962306a36Sopenharmony_ci#include <linux/refcount.h>
1062306a36Sopenharmony_ci#include "btrfs_inode.h"
1162306a36Sopenharmony_ci#include "delayed-ref.h"
1262306a36Sopenharmony_ci#include "ctree.h"
1362306a36Sopenharmony_ci#include "misc.h"
1462306a36Sopenharmony_ci
1562306a36Sopenharmony_ci/* Radix-tree tag for roots that are part of the trasaction. */
1662306a36Sopenharmony_ci#define BTRFS_ROOT_TRANS_TAG			0
1762306a36Sopenharmony_ci
1862306a36Sopenharmony_cienum btrfs_trans_state {
1962306a36Sopenharmony_ci	TRANS_STATE_RUNNING,
2062306a36Sopenharmony_ci	TRANS_STATE_COMMIT_PREP,
2162306a36Sopenharmony_ci	TRANS_STATE_COMMIT_START,
2262306a36Sopenharmony_ci	TRANS_STATE_COMMIT_DOING,
2362306a36Sopenharmony_ci	TRANS_STATE_UNBLOCKED,
2462306a36Sopenharmony_ci	TRANS_STATE_SUPER_COMMITTED,
2562306a36Sopenharmony_ci	TRANS_STATE_COMPLETED,
2662306a36Sopenharmony_ci	TRANS_STATE_MAX,
2762306a36Sopenharmony_ci};
2862306a36Sopenharmony_ci
2962306a36Sopenharmony_ci#define BTRFS_TRANS_HAVE_FREE_BGS	0
3062306a36Sopenharmony_ci#define BTRFS_TRANS_DIRTY_BG_RUN	1
3162306a36Sopenharmony_ci#define BTRFS_TRANS_CACHE_ENOSPC	2
3262306a36Sopenharmony_ci
3362306a36Sopenharmony_cistruct btrfs_transaction {
3462306a36Sopenharmony_ci	u64 transid;
3562306a36Sopenharmony_ci	/*
3662306a36Sopenharmony_ci	 * total external writers(USERSPACE/START/ATTACH) in this
3762306a36Sopenharmony_ci	 * transaction, it must be zero before the transaction is
3862306a36Sopenharmony_ci	 * being committed
3962306a36Sopenharmony_ci	 */
4062306a36Sopenharmony_ci	atomic_t num_extwriters;
4162306a36Sopenharmony_ci	/*
4262306a36Sopenharmony_ci	 * total writers in this transaction, it must be zero before the
4362306a36Sopenharmony_ci	 * transaction can end
4462306a36Sopenharmony_ci	 */
4562306a36Sopenharmony_ci	atomic_t num_writers;
4662306a36Sopenharmony_ci	refcount_t use_count;
4762306a36Sopenharmony_ci
4862306a36Sopenharmony_ci	unsigned long flags;
4962306a36Sopenharmony_ci
5062306a36Sopenharmony_ci	/* Be protected by fs_info->trans_lock when we want to change it. */
5162306a36Sopenharmony_ci	enum btrfs_trans_state state;
5262306a36Sopenharmony_ci	int aborted;
5362306a36Sopenharmony_ci	struct list_head list;
5462306a36Sopenharmony_ci	struct extent_io_tree dirty_pages;
5562306a36Sopenharmony_ci	time64_t start_time;
5662306a36Sopenharmony_ci	wait_queue_head_t writer_wait;
5762306a36Sopenharmony_ci	wait_queue_head_t commit_wait;
5862306a36Sopenharmony_ci	struct list_head pending_snapshots;
5962306a36Sopenharmony_ci	struct list_head dev_update_list;
6062306a36Sopenharmony_ci	struct list_head switch_commits;
6162306a36Sopenharmony_ci	struct list_head dirty_bgs;
6262306a36Sopenharmony_ci
6362306a36Sopenharmony_ci	/*
6462306a36Sopenharmony_ci	 * There is no explicit lock which protects io_bgs, rather its
6562306a36Sopenharmony_ci	 * consistency is implied by the fact that all the sites which modify
6662306a36Sopenharmony_ci	 * it do so under some form of transaction critical section, namely:
6762306a36Sopenharmony_ci	 *
6862306a36Sopenharmony_ci	 * - btrfs_start_dirty_block_groups - This function can only ever be
6962306a36Sopenharmony_ci	 *   run by one of the transaction committers. Refer to
7062306a36Sopenharmony_ci	 *   BTRFS_TRANS_DIRTY_BG_RUN usage in btrfs_commit_transaction
7162306a36Sopenharmony_ci	 *
7262306a36Sopenharmony_ci	 * - btrfs_write_dirty_blockgroups - this is called by
7362306a36Sopenharmony_ci	 *   commit_cowonly_roots from transaction critical section
7462306a36Sopenharmony_ci	 *   (TRANS_STATE_COMMIT_DOING)
7562306a36Sopenharmony_ci	 *
7662306a36Sopenharmony_ci	 * - btrfs_cleanup_dirty_bgs - called on transaction abort
7762306a36Sopenharmony_ci	 */
7862306a36Sopenharmony_ci	struct list_head io_bgs;
7962306a36Sopenharmony_ci	struct list_head dropped_roots;
8062306a36Sopenharmony_ci	struct extent_io_tree pinned_extents;
8162306a36Sopenharmony_ci
8262306a36Sopenharmony_ci	/*
8362306a36Sopenharmony_ci	 * we need to make sure block group deletion doesn't race with
8462306a36Sopenharmony_ci	 * free space cache writeout.  This mutex keeps them from stomping
8562306a36Sopenharmony_ci	 * on each other
8662306a36Sopenharmony_ci	 */
8762306a36Sopenharmony_ci	struct mutex cache_write_mutex;
8862306a36Sopenharmony_ci	spinlock_t dirty_bgs_lock;
8962306a36Sopenharmony_ci	/* Protected by spin lock fs_info->unused_bgs_lock. */
9062306a36Sopenharmony_ci	struct list_head deleted_bgs;
9162306a36Sopenharmony_ci	spinlock_t dropped_roots_lock;
9262306a36Sopenharmony_ci	struct btrfs_delayed_ref_root delayed_refs;
9362306a36Sopenharmony_ci	struct btrfs_fs_info *fs_info;
9462306a36Sopenharmony_ci
9562306a36Sopenharmony_ci	/*
9662306a36Sopenharmony_ci	 * Number of ordered extents the transaction must wait for before
9762306a36Sopenharmony_ci	 * committing. These are ordered extents started by a fast fsync.
9862306a36Sopenharmony_ci	 */
9962306a36Sopenharmony_ci	atomic_t pending_ordered;
10062306a36Sopenharmony_ci	wait_queue_head_t pending_wait;
10162306a36Sopenharmony_ci};
10262306a36Sopenharmony_ci
10362306a36Sopenharmony_cienum {
10462306a36Sopenharmony_ci	ENUM_BIT(__TRANS_FREEZABLE),
10562306a36Sopenharmony_ci	ENUM_BIT(__TRANS_START),
10662306a36Sopenharmony_ci	ENUM_BIT(__TRANS_ATTACH),
10762306a36Sopenharmony_ci	ENUM_BIT(__TRANS_JOIN),
10862306a36Sopenharmony_ci	ENUM_BIT(__TRANS_JOIN_NOLOCK),
10962306a36Sopenharmony_ci	ENUM_BIT(__TRANS_DUMMY),
11062306a36Sopenharmony_ci	ENUM_BIT(__TRANS_JOIN_NOSTART),
11162306a36Sopenharmony_ci};
11262306a36Sopenharmony_ci
11362306a36Sopenharmony_ci#define TRANS_START		(__TRANS_START | __TRANS_FREEZABLE)
11462306a36Sopenharmony_ci#define TRANS_ATTACH		(__TRANS_ATTACH)
11562306a36Sopenharmony_ci#define TRANS_JOIN		(__TRANS_JOIN | __TRANS_FREEZABLE)
11662306a36Sopenharmony_ci#define TRANS_JOIN_NOLOCK	(__TRANS_JOIN_NOLOCK)
11762306a36Sopenharmony_ci#define TRANS_JOIN_NOSTART	(__TRANS_JOIN_NOSTART)
11862306a36Sopenharmony_ci
11962306a36Sopenharmony_ci#define TRANS_EXTWRITERS	(__TRANS_START | __TRANS_ATTACH)
12062306a36Sopenharmony_ci
12162306a36Sopenharmony_cistruct btrfs_trans_handle {
12262306a36Sopenharmony_ci	u64 transid;
12362306a36Sopenharmony_ci	u64 bytes_reserved;
12462306a36Sopenharmony_ci	u64 chunk_bytes_reserved;
12562306a36Sopenharmony_ci	unsigned long delayed_ref_updates;
12662306a36Sopenharmony_ci	struct btrfs_transaction *transaction;
12762306a36Sopenharmony_ci	struct btrfs_block_rsv *block_rsv;
12862306a36Sopenharmony_ci	struct btrfs_block_rsv *orig_rsv;
12962306a36Sopenharmony_ci	/* Set by a task that wants to create a snapshot. */
13062306a36Sopenharmony_ci	struct btrfs_pending_snapshot *pending_snapshot;
13162306a36Sopenharmony_ci	refcount_t use_count;
13262306a36Sopenharmony_ci	unsigned int type;
13362306a36Sopenharmony_ci	/*
13462306a36Sopenharmony_ci	 * Error code of transaction abort, set outside of locks and must use
13562306a36Sopenharmony_ci	 * the READ_ONCE/WRITE_ONCE access
13662306a36Sopenharmony_ci	 */
13762306a36Sopenharmony_ci	short aborted;
13862306a36Sopenharmony_ci	bool adding_csums;
13962306a36Sopenharmony_ci	bool allocating_chunk;
14062306a36Sopenharmony_ci	bool removing_chunk;
14162306a36Sopenharmony_ci	bool reloc_reserved;
14262306a36Sopenharmony_ci	bool in_fsync;
14362306a36Sopenharmony_ci	struct btrfs_fs_info *fs_info;
14462306a36Sopenharmony_ci	struct list_head new_bgs;
14562306a36Sopenharmony_ci};
14662306a36Sopenharmony_ci
14762306a36Sopenharmony_ci/*
14862306a36Sopenharmony_ci * The abort status can be changed between calls and is not protected by locks.
14962306a36Sopenharmony_ci * This accepts btrfs_transaction and btrfs_trans_handle as types. Once it's
15062306a36Sopenharmony_ci * set to a non-zero value it does not change, so the macro should be in checks
15162306a36Sopenharmony_ci * but is not necessary for further reads of the value.
15262306a36Sopenharmony_ci */
15362306a36Sopenharmony_ci#define TRANS_ABORTED(trans)		(unlikely(READ_ONCE((trans)->aborted)))
15462306a36Sopenharmony_ci
15562306a36Sopenharmony_cistruct btrfs_pending_snapshot {
15662306a36Sopenharmony_ci	struct dentry *dentry;
15762306a36Sopenharmony_ci	struct inode *dir;
15862306a36Sopenharmony_ci	struct btrfs_root *root;
15962306a36Sopenharmony_ci	struct btrfs_root_item *root_item;
16062306a36Sopenharmony_ci	struct btrfs_root *snap;
16162306a36Sopenharmony_ci	struct btrfs_qgroup_inherit *inherit;
16262306a36Sopenharmony_ci	struct btrfs_path *path;
16362306a36Sopenharmony_ci	/* block reservation for the operation */
16462306a36Sopenharmony_ci	struct btrfs_block_rsv block_rsv;
16562306a36Sopenharmony_ci	/* extra metadata reservation for relocation */
16662306a36Sopenharmony_ci	int error;
16762306a36Sopenharmony_ci	/* Preallocated anonymous block device number */
16862306a36Sopenharmony_ci	dev_t anon_dev;
16962306a36Sopenharmony_ci	bool readonly;
17062306a36Sopenharmony_ci	struct list_head list;
17162306a36Sopenharmony_ci};
17262306a36Sopenharmony_ci
17362306a36Sopenharmony_cistatic inline void btrfs_set_inode_last_trans(struct btrfs_trans_handle *trans,
17462306a36Sopenharmony_ci					      struct btrfs_inode *inode)
17562306a36Sopenharmony_ci{
17662306a36Sopenharmony_ci	spin_lock(&inode->lock);
17762306a36Sopenharmony_ci	inode->last_trans = trans->transaction->transid;
17862306a36Sopenharmony_ci	inode->last_sub_trans = inode->root->log_transid;
17962306a36Sopenharmony_ci	inode->last_log_commit = inode->last_sub_trans - 1;
18062306a36Sopenharmony_ci	spin_unlock(&inode->lock);
18162306a36Sopenharmony_ci}
18262306a36Sopenharmony_ci
18362306a36Sopenharmony_ci/*
18462306a36Sopenharmony_ci * Make qgroup codes to skip given qgroupid, means the old/new_roots for
18562306a36Sopenharmony_ci * qgroup won't contain the qgroupid in it.
18662306a36Sopenharmony_ci */
18762306a36Sopenharmony_cistatic inline void btrfs_set_skip_qgroup(struct btrfs_trans_handle *trans,
18862306a36Sopenharmony_ci					 u64 qgroupid)
18962306a36Sopenharmony_ci{
19062306a36Sopenharmony_ci	struct btrfs_delayed_ref_root *delayed_refs;
19162306a36Sopenharmony_ci
19262306a36Sopenharmony_ci	delayed_refs = &trans->transaction->delayed_refs;
19362306a36Sopenharmony_ci	WARN_ON(delayed_refs->qgroup_to_skip);
19462306a36Sopenharmony_ci	delayed_refs->qgroup_to_skip = qgroupid;
19562306a36Sopenharmony_ci}
19662306a36Sopenharmony_ci
19762306a36Sopenharmony_cistatic inline void btrfs_clear_skip_qgroup(struct btrfs_trans_handle *trans)
19862306a36Sopenharmony_ci{
19962306a36Sopenharmony_ci	struct btrfs_delayed_ref_root *delayed_refs;
20062306a36Sopenharmony_ci
20162306a36Sopenharmony_ci	delayed_refs = &trans->transaction->delayed_refs;
20262306a36Sopenharmony_ci	WARN_ON(!delayed_refs->qgroup_to_skip);
20362306a36Sopenharmony_ci	delayed_refs->qgroup_to_skip = 0;
20462306a36Sopenharmony_ci}
20562306a36Sopenharmony_ci
20662306a36Sopenharmony_cibool __cold abort_should_print_stack(int errno);
20762306a36Sopenharmony_ci
20862306a36Sopenharmony_ci/*
20962306a36Sopenharmony_ci * Call btrfs_abort_transaction as early as possible when an error condition is
21062306a36Sopenharmony_ci * detected, that way the exact stack trace is reported for some errors.
21162306a36Sopenharmony_ci */
21262306a36Sopenharmony_ci#define btrfs_abort_transaction(trans, errno)		\
21362306a36Sopenharmony_cido {								\
21462306a36Sopenharmony_ci	bool first = false;					\
21562306a36Sopenharmony_ci	/* Report first abort since mount */			\
21662306a36Sopenharmony_ci	if (!test_and_set_bit(BTRFS_FS_STATE_TRANS_ABORTED,	\
21762306a36Sopenharmony_ci			&((trans)->fs_info->fs_state))) {	\
21862306a36Sopenharmony_ci		first = true;					\
21962306a36Sopenharmony_ci		if (WARN(abort_should_print_stack(errno),	\
22062306a36Sopenharmony_ci			KERN_ERR				\
22162306a36Sopenharmony_ci			"BTRFS: Transaction aborted (error %d)\n",	\
22262306a36Sopenharmony_ci			(errno))) {					\
22362306a36Sopenharmony_ci			/* Stack trace printed. */			\
22462306a36Sopenharmony_ci		} else {						\
22562306a36Sopenharmony_ci			btrfs_err((trans)->fs_info,			\
22662306a36Sopenharmony_ci				  "Transaction aborted (error %d)",	\
22762306a36Sopenharmony_ci				  (errno));			\
22862306a36Sopenharmony_ci		}						\
22962306a36Sopenharmony_ci	}							\
23062306a36Sopenharmony_ci	__btrfs_abort_transaction((trans), __func__,		\
23162306a36Sopenharmony_ci				  __LINE__, (errno), first);	\
23262306a36Sopenharmony_ci} while (0)
23362306a36Sopenharmony_ci
23462306a36Sopenharmony_ciint btrfs_end_transaction(struct btrfs_trans_handle *trans);
23562306a36Sopenharmony_cistruct btrfs_trans_handle *btrfs_start_transaction(struct btrfs_root *root,
23662306a36Sopenharmony_ci						   unsigned int num_items);
23762306a36Sopenharmony_cistruct btrfs_trans_handle *btrfs_start_transaction_fallback_global_rsv(
23862306a36Sopenharmony_ci					struct btrfs_root *root,
23962306a36Sopenharmony_ci					unsigned int num_items);
24062306a36Sopenharmony_cistruct btrfs_trans_handle *btrfs_join_transaction(struct btrfs_root *root);
24162306a36Sopenharmony_cistruct btrfs_trans_handle *btrfs_join_transaction_spacecache(struct btrfs_root *root);
24262306a36Sopenharmony_cistruct btrfs_trans_handle *btrfs_join_transaction_nostart(struct btrfs_root *root);
24362306a36Sopenharmony_cistruct btrfs_trans_handle *btrfs_attach_transaction(struct btrfs_root *root);
24462306a36Sopenharmony_cistruct btrfs_trans_handle *btrfs_attach_transaction_barrier(
24562306a36Sopenharmony_ci					struct btrfs_root *root);
24662306a36Sopenharmony_ciint btrfs_wait_for_commit(struct btrfs_fs_info *fs_info, u64 transid);
24762306a36Sopenharmony_ci
24862306a36Sopenharmony_civoid btrfs_add_dead_root(struct btrfs_root *root);
24962306a36Sopenharmony_ciint btrfs_defrag_root(struct btrfs_root *root);
25062306a36Sopenharmony_civoid btrfs_maybe_wake_unfinished_drop(struct btrfs_fs_info *fs_info);
25162306a36Sopenharmony_ciint btrfs_clean_one_deleted_snapshot(struct btrfs_fs_info *fs_info);
25262306a36Sopenharmony_ciint btrfs_commit_transaction(struct btrfs_trans_handle *trans);
25362306a36Sopenharmony_civoid btrfs_commit_transaction_async(struct btrfs_trans_handle *trans);
25462306a36Sopenharmony_ciint btrfs_end_transaction_throttle(struct btrfs_trans_handle *trans);
25562306a36Sopenharmony_cibool btrfs_should_end_transaction(struct btrfs_trans_handle *trans);
25662306a36Sopenharmony_civoid btrfs_throttle(struct btrfs_fs_info *fs_info);
25762306a36Sopenharmony_ciint btrfs_record_root_in_trans(struct btrfs_trans_handle *trans,
25862306a36Sopenharmony_ci				struct btrfs_root *root);
25962306a36Sopenharmony_ciint btrfs_write_marked_extents(struct btrfs_fs_info *fs_info,
26062306a36Sopenharmony_ci				struct extent_io_tree *dirty_pages, int mark);
26162306a36Sopenharmony_ciint btrfs_wait_tree_log_extents(struct btrfs_root *root, int mark);
26262306a36Sopenharmony_ciint btrfs_transaction_blocked(struct btrfs_fs_info *info);
26362306a36Sopenharmony_ciint btrfs_transaction_in_commit(struct btrfs_fs_info *info);
26462306a36Sopenharmony_civoid btrfs_put_transaction(struct btrfs_transaction *transaction);
26562306a36Sopenharmony_civoid btrfs_add_dropped_root(struct btrfs_trans_handle *trans,
26662306a36Sopenharmony_ci			    struct btrfs_root *root);
26762306a36Sopenharmony_civoid btrfs_trans_release_chunk_metadata(struct btrfs_trans_handle *trans);
26862306a36Sopenharmony_civoid __cold __btrfs_abort_transaction(struct btrfs_trans_handle *trans,
26962306a36Sopenharmony_ci				      const char *function,
27062306a36Sopenharmony_ci				      unsigned int line, int errno, bool first_hit);
27162306a36Sopenharmony_ci
27262306a36Sopenharmony_ciint __init btrfs_transaction_init(void);
27362306a36Sopenharmony_civoid __cold btrfs_transaction_exit(void);
27462306a36Sopenharmony_ci
27562306a36Sopenharmony_ci#endif
276