18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Copyright (C) 2007 Oracle.  All rights reserved.
48c2ecf20Sopenharmony_ci */
58c2ecf20Sopenharmony_ci
68c2ecf20Sopenharmony_ci#ifndef BTRFS_TRANSACTION_H
78c2ecf20Sopenharmony_ci#define BTRFS_TRANSACTION_H
88c2ecf20Sopenharmony_ci
98c2ecf20Sopenharmony_ci#include <linux/refcount.h>
108c2ecf20Sopenharmony_ci#include "btrfs_inode.h"
118c2ecf20Sopenharmony_ci#include "delayed-ref.h"
128c2ecf20Sopenharmony_ci#include "ctree.h"
138c2ecf20Sopenharmony_ci
148c2ecf20Sopenharmony_cienum btrfs_trans_state {
158c2ecf20Sopenharmony_ci	TRANS_STATE_RUNNING,
168c2ecf20Sopenharmony_ci	TRANS_STATE_COMMIT_START,
178c2ecf20Sopenharmony_ci	TRANS_STATE_COMMIT_DOING,
188c2ecf20Sopenharmony_ci	TRANS_STATE_UNBLOCKED,
198c2ecf20Sopenharmony_ci	TRANS_STATE_COMPLETED,
208c2ecf20Sopenharmony_ci	TRANS_STATE_MAX,
218c2ecf20Sopenharmony_ci};
228c2ecf20Sopenharmony_ci
238c2ecf20Sopenharmony_ci#define BTRFS_TRANS_HAVE_FREE_BGS	0
248c2ecf20Sopenharmony_ci#define BTRFS_TRANS_DIRTY_BG_RUN	1
258c2ecf20Sopenharmony_ci#define BTRFS_TRANS_CACHE_ENOSPC	2
268c2ecf20Sopenharmony_ci
278c2ecf20Sopenharmony_cistruct btrfs_transaction {
288c2ecf20Sopenharmony_ci	u64 transid;
298c2ecf20Sopenharmony_ci	/*
308c2ecf20Sopenharmony_ci	 * total external writers(USERSPACE/START/ATTACH) in this
318c2ecf20Sopenharmony_ci	 * transaction, it must be zero before the transaction is
328c2ecf20Sopenharmony_ci	 * being committed
338c2ecf20Sopenharmony_ci	 */
348c2ecf20Sopenharmony_ci	atomic_t num_extwriters;
358c2ecf20Sopenharmony_ci	/*
368c2ecf20Sopenharmony_ci	 * total writers in this transaction, it must be zero before the
378c2ecf20Sopenharmony_ci	 * transaction can end
388c2ecf20Sopenharmony_ci	 */
398c2ecf20Sopenharmony_ci	atomic_t num_writers;
408c2ecf20Sopenharmony_ci	refcount_t use_count;
418c2ecf20Sopenharmony_ci
428c2ecf20Sopenharmony_ci	unsigned long flags;
438c2ecf20Sopenharmony_ci
448c2ecf20Sopenharmony_ci	/* Be protected by fs_info->trans_lock when we want to change it. */
458c2ecf20Sopenharmony_ci	enum btrfs_trans_state state;
468c2ecf20Sopenharmony_ci	int aborted;
478c2ecf20Sopenharmony_ci	struct list_head list;
488c2ecf20Sopenharmony_ci	struct extent_io_tree dirty_pages;
498c2ecf20Sopenharmony_ci	time64_t start_time;
508c2ecf20Sopenharmony_ci	wait_queue_head_t writer_wait;
518c2ecf20Sopenharmony_ci	wait_queue_head_t commit_wait;
528c2ecf20Sopenharmony_ci	struct list_head pending_snapshots;
538c2ecf20Sopenharmony_ci	struct list_head dev_update_list;
548c2ecf20Sopenharmony_ci	struct list_head switch_commits;
558c2ecf20Sopenharmony_ci	struct list_head dirty_bgs;
568c2ecf20Sopenharmony_ci
578c2ecf20Sopenharmony_ci	/*
588c2ecf20Sopenharmony_ci	 * There is no explicit lock which protects io_bgs, rather its
598c2ecf20Sopenharmony_ci	 * consistency is implied by the fact that all the sites which modify
608c2ecf20Sopenharmony_ci	 * it do so under some form of transaction critical section, namely:
618c2ecf20Sopenharmony_ci	 *
628c2ecf20Sopenharmony_ci	 * - btrfs_start_dirty_block_groups - This function can only ever be
638c2ecf20Sopenharmony_ci	 *   run by one of the transaction committers. Refer to
648c2ecf20Sopenharmony_ci	 *   BTRFS_TRANS_DIRTY_BG_RUN usage in btrfs_commit_transaction
658c2ecf20Sopenharmony_ci	 *
668c2ecf20Sopenharmony_ci	 * - btrfs_write_dirty_blockgroups - this is called by
678c2ecf20Sopenharmony_ci	 *   commit_cowonly_roots from transaction critical section
688c2ecf20Sopenharmony_ci	 *   (TRANS_STATE_COMMIT_DOING)
698c2ecf20Sopenharmony_ci	 *
708c2ecf20Sopenharmony_ci	 * - btrfs_cleanup_dirty_bgs - called on transaction abort
718c2ecf20Sopenharmony_ci	 */
728c2ecf20Sopenharmony_ci	struct list_head io_bgs;
738c2ecf20Sopenharmony_ci	struct list_head dropped_roots;
748c2ecf20Sopenharmony_ci	struct extent_io_tree pinned_extents;
758c2ecf20Sopenharmony_ci
768c2ecf20Sopenharmony_ci	/*
778c2ecf20Sopenharmony_ci	 * we need to make sure block group deletion doesn't race with
788c2ecf20Sopenharmony_ci	 * free space cache writeout.  This mutex keeps them from stomping
798c2ecf20Sopenharmony_ci	 * on each other
808c2ecf20Sopenharmony_ci	 */
818c2ecf20Sopenharmony_ci	struct mutex cache_write_mutex;
828c2ecf20Sopenharmony_ci	spinlock_t dirty_bgs_lock;
838c2ecf20Sopenharmony_ci	/* Protected by spin lock fs_info->unused_bgs_lock. */
848c2ecf20Sopenharmony_ci	struct list_head deleted_bgs;
858c2ecf20Sopenharmony_ci	spinlock_t dropped_roots_lock;
868c2ecf20Sopenharmony_ci	struct btrfs_delayed_ref_root delayed_refs;
878c2ecf20Sopenharmony_ci	struct btrfs_fs_info *fs_info;
888c2ecf20Sopenharmony_ci
898c2ecf20Sopenharmony_ci	/*
908c2ecf20Sopenharmony_ci	 * Number of ordered extents the transaction must wait for before
918c2ecf20Sopenharmony_ci	 * committing. These are ordered extents started by a fast fsync.
928c2ecf20Sopenharmony_ci	 */
938c2ecf20Sopenharmony_ci	atomic_t pending_ordered;
948c2ecf20Sopenharmony_ci	wait_queue_head_t pending_wait;
958c2ecf20Sopenharmony_ci};
968c2ecf20Sopenharmony_ci
978c2ecf20Sopenharmony_ci#define __TRANS_FREEZABLE	(1U << 0)
988c2ecf20Sopenharmony_ci
998c2ecf20Sopenharmony_ci#define __TRANS_START		(1U << 9)
1008c2ecf20Sopenharmony_ci#define __TRANS_ATTACH		(1U << 10)
1018c2ecf20Sopenharmony_ci#define __TRANS_JOIN		(1U << 11)
1028c2ecf20Sopenharmony_ci#define __TRANS_JOIN_NOLOCK	(1U << 12)
1038c2ecf20Sopenharmony_ci#define __TRANS_DUMMY		(1U << 13)
1048c2ecf20Sopenharmony_ci#define __TRANS_JOIN_NOSTART	(1U << 14)
1058c2ecf20Sopenharmony_ci
1068c2ecf20Sopenharmony_ci#define TRANS_START		(__TRANS_START | __TRANS_FREEZABLE)
1078c2ecf20Sopenharmony_ci#define TRANS_ATTACH		(__TRANS_ATTACH)
1088c2ecf20Sopenharmony_ci#define TRANS_JOIN		(__TRANS_JOIN | __TRANS_FREEZABLE)
1098c2ecf20Sopenharmony_ci#define TRANS_JOIN_NOLOCK	(__TRANS_JOIN_NOLOCK)
1108c2ecf20Sopenharmony_ci#define TRANS_JOIN_NOSTART	(__TRANS_JOIN_NOSTART)
1118c2ecf20Sopenharmony_ci
1128c2ecf20Sopenharmony_ci#define TRANS_EXTWRITERS	(__TRANS_START | __TRANS_ATTACH)
1138c2ecf20Sopenharmony_ci
1148c2ecf20Sopenharmony_ci#define BTRFS_SEND_TRANS_STUB	((void *)1)
1158c2ecf20Sopenharmony_ci#define BTRFS_DIO_SYNC_STUB	((void *)2)
1168c2ecf20Sopenharmony_ci
1178c2ecf20Sopenharmony_cistruct btrfs_trans_handle {
1188c2ecf20Sopenharmony_ci	u64 transid;
1198c2ecf20Sopenharmony_ci	u64 bytes_reserved;
1208c2ecf20Sopenharmony_ci	u64 chunk_bytes_reserved;
1218c2ecf20Sopenharmony_ci	unsigned long delayed_ref_updates;
1228c2ecf20Sopenharmony_ci	struct btrfs_transaction *transaction;
1238c2ecf20Sopenharmony_ci	struct btrfs_block_rsv *block_rsv;
1248c2ecf20Sopenharmony_ci	struct btrfs_block_rsv *orig_rsv;
1258c2ecf20Sopenharmony_ci	refcount_t use_count;
1268c2ecf20Sopenharmony_ci	unsigned int type;
1278c2ecf20Sopenharmony_ci	/*
1288c2ecf20Sopenharmony_ci	 * Error code of transaction abort, set outside of locks and must use
1298c2ecf20Sopenharmony_ci	 * the READ_ONCE/WRITE_ONCE access
1308c2ecf20Sopenharmony_ci	 */
1318c2ecf20Sopenharmony_ci	short aborted;
1328c2ecf20Sopenharmony_ci	bool adding_csums;
1338c2ecf20Sopenharmony_ci	bool allocating_chunk;
1348c2ecf20Sopenharmony_ci	bool can_flush_pending_bgs;
1358c2ecf20Sopenharmony_ci	bool reloc_reserved;
1368c2ecf20Sopenharmony_ci	bool dirty;
1378c2ecf20Sopenharmony_ci	struct btrfs_root *root;
1388c2ecf20Sopenharmony_ci	struct btrfs_fs_info *fs_info;
1398c2ecf20Sopenharmony_ci	struct list_head new_bgs;
1408c2ecf20Sopenharmony_ci};
1418c2ecf20Sopenharmony_ci
1428c2ecf20Sopenharmony_ci/*
1438c2ecf20Sopenharmony_ci * The abort status can be changed between calls and is not protected by locks.
1448c2ecf20Sopenharmony_ci * This accepts btrfs_transaction and btrfs_trans_handle as types. Once it's
1458c2ecf20Sopenharmony_ci * set to a non-zero value it does not change, so the macro should be in checks
1468c2ecf20Sopenharmony_ci * but is not necessary for further reads of the value.
1478c2ecf20Sopenharmony_ci */
1488c2ecf20Sopenharmony_ci#define TRANS_ABORTED(trans)		(unlikely(READ_ONCE((trans)->aborted)))
1498c2ecf20Sopenharmony_ci
1508c2ecf20Sopenharmony_cistruct btrfs_pending_snapshot {
1518c2ecf20Sopenharmony_ci	struct dentry *dentry;
1528c2ecf20Sopenharmony_ci	struct inode *dir;
1538c2ecf20Sopenharmony_ci	struct btrfs_root *root;
1548c2ecf20Sopenharmony_ci	struct btrfs_root_item *root_item;
1558c2ecf20Sopenharmony_ci	struct btrfs_root *snap;
1568c2ecf20Sopenharmony_ci	struct btrfs_qgroup_inherit *inherit;
1578c2ecf20Sopenharmony_ci	struct btrfs_path *path;
1588c2ecf20Sopenharmony_ci	/* block reservation for the operation */
1598c2ecf20Sopenharmony_ci	struct btrfs_block_rsv block_rsv;
1608c2ecf20Sopenharmony_ci	/* extra metadata reservation for relocation */
1618c2ecf20Sopenharmony_ci	int error;
1628c2ecf20Sopenharmony_ci	/* Preallocated anonymous block device number */
1638c2ecf20Sopenharmony_ci	dev_t anon_dev;
1648c2ecf20Sopenharmony_ci	bool readonly;
1658c2ecf20Sopenharmony_ci	struct list_head list;
1668c2ecf20Sopenharmony_ci};
1678c2ecf20Sopenharmony_ci
1688c2ecf20Sopenharmony_cistatic inline void btrfs_set_inode_last_trans(struct btrfs_trans_handle *trans,
1698c2ecf20Sopenharmony_ci					      struct btrfs_inode *inode)
1708c2ecf20Sopenharmony_ci{
1718c2ecf20Sopenharmony_ci	spin_lock(&inode->lock);
1728c2ecf20Sopenharmony_ci	inode->last_trans = trans->transaction->transid;
1738c2ecf20Sopenharmony_ci	inode->last_sub_trans = inode->root->log_transid;
1748c2ecf20Sopenharmony_ci	inode->last_log_commit = inode->last_sub_trans - 1;
1758c2ecf20Sopenharmony_ci	spin_unlock(&inode->lock);
1768c2ecf20Sopenharmony_ci}
1778c2ecf20Sopenharmony_ci
1788c2ecf20Sopenharmony_ci/*
1798c2ecf20Sopenharmony_ci * Make qgroup codes to skip given qgroupid, means the old/new_roots for
1808c2ecf20Sopenharmony_ci * qgroup won't contain the qgroupid in it.
1818c2ecf20Sopenharmony_ci */
1828c2ecf20Sopenharmony_cistatic inline void btrfs_set_skip_qgroup(struct btrfs_trans_handle *trans,
1838c2ecf20Sopenharmony_ci					 u64 qgroupid)
1848c2ecf20Sopenharmony_ci{
1858c2ecf20Sopenharmony_ci	struct btrfs_delayed_ref_root *delayed_refs;
1868c2ecf20Sopenharmony_ci
1878c2ecf20Sopenharmony_ci	delayed_refs = &trans->transaction->delayed_refs;
1888c2ecf20Sopenharmony_ci	WARN_ON(delayed_refs->qgroup_to_skip);
1898c2ecf20Sopenharmony_ci	delayed_refs->qgroup_to_skip = qgroupid;
1908c2ecf20Sopenharmony_ci}
1918c2ecf20Sopenharmony_ci
1928c2ecf20Sopenharmony_cistatic inline void btrfs_clear_skip_qgroup(struct btrfs_trans_handle *trans)
1938c2ecf20Sopenharmony_ci{
1948c2ecf20Sopenharmony_ci	struct btrfs_delayed_ref_root *delayed_refs;
1958c2ecf20Sopenharmony_ci
1968c2ecf20Sopenharmony_ci	delayed_refs = &trans->transaction->delayed_refs;
1978c2ecf20Sopenharmony_ci	WARN_ON(!delayed_refs->qgroup_to_skip);
1988c2ecf20Sopenharmony_ci	delayed_refs->qgroup_to_skip = 0;
1998c2ecf20Sopenharmony_ci}
2008c2ecf20Sopenharmony_ci
2018c2ecf20Sopenharmony_ciint btrfs_end_transaction(struct btrfs_trans_handle *trans);
2028c2ecf20Sopenharmony_cistruct btrfs_trans_handle *btrfs_start_transaction(struct btrfs_root *root,
2038c2ecf20Sopenharmony_ci						   unsigned int num_items);
2048c2ecf20Sopenharmony_cistruct btrfs_trans_handle *btrfs_start_transaction_fallback_global_rsv(
2058c2ecf20Sopenharmony_ci					struct btrfs_root *root,
2068c2ecf20Sopenharmony_ci					unsigned int num_items);
2078c2ecf20Sopenharmony_cistruct btrfs_trans_handle *btrfs_join_transaction(struct btrfs_root *root);
2088c2ecf20Sopenharmony_cistruct btrfs_trans_handle *btrfs_join_transaction_spacecache(struct btrfs_root *root);
2098c2ecf20Sopenharmony_cistruct btrfs_trans_handle *btrfs_join_transaction_nostart(struct btrfs_root *root);
2108c2ecf20Sopenharmony_cistruct btrfs_trans_handle *btrfs_attach_transaction(struct btrfs_root *root);
2118c2ecf20Sopenharmony_cistruct btrfs_trans_handle *btrfs_attach_transaction_barrier(
2128c2ecf20Sopenharmony_ci					struct btrfs_root *root);
2138c2ecf20Sopenharmony_ciint btrfs_wait_for_commit(struct btrfs_fs_info *fs_info, u64 transid);
2148c2ecf20Sopenharmony_ci
2158c2ecf20Sopenharmony_civoid btrfs_add_dead_root(struct btrfs_root *root);
2168c2ecf20Sopenharmony_ciint btrfs_defrag_root(struct btrfs_root *root);
2178c2ecf20Sopenharmony_ciint btrfs_clean_one_deleted_snapshot(struct btrfs_root *root);
2188c2ecf20Sopenharmony_ciint btrfs_commit_transaction(struct btrfs_trans_handle *trans);
2198c2ecf20Sopenharmony_ciint btrfs_commit_transaction_async(struct btrfs_trans_handle *trans,
2208c2ecf20Sopenharmony_ci				   int wait_for_unblock);
2218c2ecf20Sopenharmony_ciint btrfs_end_transaction_throttle(struct btrfs_trans_handle *trans);
2228c2ecf20Sopenharmony_ciint btrfs_should_end_transaction(struct btrfs_trans_handle *trans);
2238c2ecf20Sopenharmony_civoid btrfs_throttle(struct btrfs_fs_info *fs_info);
2248c2ecf20Sopenharmony_ciint btrfs_record_root_in_trans(struct btrfs_trans_handle *trans,
2258c2ecf20Sopenharmony_ci				struct btrfs_root *root);
2268c2ecf20Sopenharmony_ciint btrfs_write_marked_extents(struct btrfs_fs_info *fs_info,
2278c2ecf20Sopenharmony_ci				struct extent_io_tree *dirty_pages, int mark);
2288c2ecf20Sopenharmony_ciint btrfs_wait_tree_log_extents(struct btrfs_root *root, int mark);
2298c2ecf20Sopenharmony_ciint btrfs_transaction_blocked(struct btrfs_fs_info *info);
2308c2ecf20Sopenharmony_ciint btrfs_transaction_in_commit(struct btrfs_fs_info *info);
2318c2ecf20Sopenharmony_civoid btrfs_put_transaction(struct btrfs_transaction *transaction);
2328c2ecf20Sopenharmony_civoid btrfs_apply_pending_changes(struct btrfs_fs_info *fs_info);
2338c2ecf20Sopenharmony_civoid btrfs_add_dropped_root(struct btrfs_trans_handle *trans,
2348c2ecf20Sopenharmony_ci			    struct btrfs_root *root);
2358c2ecf20Sopenharmony_civoid btrfs_trans_release_chunk_metadata(struct btrfs_trans_handle *trans);
2368c2ecf20Sopenharmony_ci
2378c2ecf20Sopenharmony_ci#endif
238