162306a36Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */
262306a36Sopenharmony_ci
362306a36Sopenharmony_ci#ifndef BTRFS_EXTENT_IO_TREE_H
462306a36Sopenharmony_ci#define BTRFS_EXTENT_IO_TREE_H
562306a36Sopenharmony_ci
662306a36Sopenharmony_ci#include "misc.h"
762306a36Sopenharmony_ci
862306a36Sopenharmony_cistruct extent_changeset;
962306a36Sopenharmony_ci
1062306a36Sopenharmony_ci/* Bits for the extent state */
1162306a36Sopenharmony_cienum {
1262306a36Sopenharmony_ci	ENUM_BIT(EXTENT_DIRTY),
1362306a36Sopenharmony_ci	ENUM_BIT(EXTENT_UPTODATE),
1462306a36Sopenharmony_ci	ENUM_BIT(EXTENT_LOCKED),
1562306a36Sopenharmony_ci	ENUM_BIT(EXTENT_NEW),
1662306a36Sopenharmony_ci	ENUM_BIT(EXTENT_DELALLOC),
1762306a36Sopenharmony_ci	ENUM_BIT(EXTENT_DEFRAG),
1862306a36Sopenharmony_ci	ENUM_BIT(EXTENT_BOUNDARY),
1962306a36Sopenharmony_ci	ENUM_BIT(EXTENT_NODATASUM),
2062306a36Sopenharmony_ci	ENUM_BIT(EXTENT_CLEAR_META_RESV),
2162306a36Sopenharmony_ci	ENUM_BIT(EXTENT_NEED_WAIT),
2262306a36Sopenharmony_ci	ENUM_BIT(EXTENT_NORESERVE),
2362306a36Sopenharmony_ci	ENUM_BIT(EXTENT_QGROUP_RESERVED),
2462306a36Sopenharmony_ci	ENUM_BIT(EXTENT_CLEAR_DATA_RESV),
2562306a36Sopenharmony_ci	/*
2662306a36Sopenharmony_ci	 * Must be cleared only during ordered extent completion or on error
2762306a36Sopenharmony_ci	 * paths if we did not manage to submit bios and create the ordered
2862306a36Sopenharmony_ci	 * extents for the range.  Should not be cleared during page release
2962306a36Sopenharmony_ci	 * and page invalidation (if there is an ordered extent in flight),
3062306a36Sopenharmony_ci	 * that is left for the ordered extent completion.
3162306a36Sopenharmony_ci	 */
3262306a36Sopenharmony_ci	ENUM_BIT(EXTENT_DELALLOC_NEW),
3362306a36Sopenharmony_ci	/*
3462306a36Sopenharmony_ci	 * When an ordered extent successfully completes for a region marked as
3562306a36Sopenharmony_ci	 * a new delalloc range, use this flag when clearing a new delalloc
3662306a36Sopenharmony_ci	 * range to indicate that the VFS' inode number of bytes should be
3762306a36Sopenharmony_ci	 * incremented and the inode's new delalloc bytes decremented, in an
3862306a36Sopenharmony_ci	 * atomic way to prevent races with stat(2).
3962306a36Sopenharmony_ci	 */
4062306a36Sopenharmony_ci	ENUM_BIT(EXTENT_ADD_INODE_BYTES),
4162306a36Sopenharmony_ci	/*
4262306a36Sopenharmony_ci	 * Set during truncate when we're clearing an entire range and we just
4362306a36Sopenharmony_ci	 * want the extent states to go away.
4462306a36Sopenharmony_ci	 */
4562306a36Sopenharmony_ci	ENUM_BIT(EXTENT_CLEAR_ALL_BITS),
4662306a36Sopenharmony_ci
4762306a36Sopenharmony_ci	/*
4862306a36Sopenharmony_ci	 * This must be last.
4962306a36Sopenharmony_ci	 *
5062306a36Sopenharmony_ci	 * Bit not representing a state but a request for NOWAIT semantics,
5162306a36Sopenharmony_ci	 * e.g. when allocating memory, and must be masked out from the other
5262306a36Sopenharmony_ci	 * bits.
5362306a36Sopenharmony_ci	 */
5462306a36Sopenharmony_ci	ENUM_BIT(EXTENT_NOWAIT)
5562306a36Sopenharmony_ci};
5662306a36Sopenharmony_ci
5762306a36Sopenharmony_ci#define EXTENT_DO_ACCOUNTING    (EXTENT_CLEAR_META_RESV | \
5862306a36Sopenharmony_ci				 EXTENT_CLEAR_DATA_RESV)
5962306a36Sopenharmony_ci#define EXTENT_CTLBITS		(EXTENT_DO_ACCOUNTING | \
6062306a36Sopenharmony_ci				 EXTENT_ADD_INODE_BYTES | \
6162306a36Sopenharmony_ci				 EXTENT_CLEAR_ALL_BITS)
6262306a36Sopenharmony_ci
6362306a36Sopenharmony_ci/*
6462306a36Sopenharmony_ci * Redefined bits above which are used only in the device allocation tree,
6562306a36Sopenharmony_ci * shouldn't be using EXTENT_LOCKED / EXTENT_BOUNDARY / EXTENT_CLEAR_META_RESV
6662306a36Sopenharmony_ci * / EXTENT_CLEAR_DATA_RESV because they have special meaning to the bit
6762306a36Sopenharmony_ci * manipulation functions
6862306a36Sopenharmony_ci */
6962306a36Sopenharmony_ci#define CHUNK_ALLOCATED				EXTENT_DIRTY
7062306a36Sopenharmony_ci#define CHUNK_TRIMMED				EXTENT_DEFRAG
7162306a36Sopenharmony_ci#define CHUNK_STATE_MASK			(CHUNK_ALLOCATED |		\
7262306a36Sopenharmony_ci						 CHUNK_TRIMMED)
7362306a36Sopenharmony_ci
7462306a36Sopenharmony_cienum {
7562306a36Sopenharmony_ci	IO_TREE_FS_PINNED_EXTENTS,
7662306a36Sopenharmony_ci	IO_TREE_FS_EXCLUDED_EXTENTS,
7762306a36Sopenharmony_ci	IO_TREE_BTREE_INODE_IO,
7862306a36Sopenharmony_ci	IO_TREE_INODE_IO,
7962306a36Sopenharmony_ci	IO_TREE_RELOC_BLOCKS,
8062306a36Sopenharmony_ci	IO_TREE_TRANS_DIRTY_PAGES,
8162306a36Sopenharmony_ci	IO_TREE_ROOT_DIRTY_LOG_PAGES,
8262306a36Sopenharmony_ci	IO_TREE_INODE_FILE_EXTENT,
8362306a36Sopenharmony_ci	IO_TREE_LOG_CSUM_RANGE,
8462306a36Sopenharmony_ci	IO_TREE_SELFTEST,
8562306a36Sopenharmony_ci	IO_TREE_DEVICE_ALLOC_STATE,
8662306a36Sopenharmony_ci};
8762306a36Sopenharmony_ci
8862306a36Sopenharmony_cistruct extent_io_tree {
8962306a36Sopenharmony_ci	struct rb_root state;
9062306a36Sopenharmony_ci	struct btrfs_fs_info *fs_info;
9162306a36Sopenharmony_ci	/* Inode associated with this tree, or NULL. */
9262306a36Sopenharmony_ci	struct btrfs_inode *inode;
9362306a36Sopenharmony_ci
9462306a36Sopenharmony_ci	/* Who owns this io tree, should be one of IO_TREE_* */
9562306a36Sopenharmony_ci	u8 owner;
9662306a36Sopenharmony_ci
9762306a36Sopenharmony_ci	spinlock_t lock;
9862306a36Sopenharmony_ci};
9962306a36Sopenharmony_ci
10062306a36Sopenharmony_cistruct extent_state {
10162306a36Sopenharmony_ci	u64 start;
10262306a36Sopenharmony_ci	u64 end; /* inclusive */
10362306a36Sopenharmony_ci	struct rb_node rb_node;
10462306a36Sopenharmony_ci
10562306a36Sopenharmony_ci	/* ADD NEW ELEMENTS AFTER THIS */
10662306a36Sopenharmony_ci	wait_queue_head_t wq;
10762306a36Sopenharmony_ci	refcount_t refs;
10862306a36Sopenharmony_ci	u32 state;
10962306a36Sopenharmony_ci
11062306a36Sopenharmony_ci#ifdef CONFIG_BTRFS_DEBUG
11162306a36Sopenharmony_ci	struct list_head leak_list;
11262306a36Sopenharmony_ci#endif
11362306a36Sopenharmony_ci};
11462306a36Sopenharmony_ci
11562306a36Sopenharmony_civoid extent_io_tree_init(struct btrfs_fs_info *fs_info,
11662306a36Sopenharmony_ci			 struct extent_io_tree *tree, unsigned int owner);
11762306a36Sopenharmony_civoid extent_io_tree_release(struct extent_io_tree *tree);
11862306a36Sopenharmony_ci
11962306a36Sopenharmony_ciint lock_extent(struct extent_io_tree *tree, u64 start, u64 end,
12062306a36Sopenharmony_ci		struct extent_state **cached);
12162306a36Sopenharmony_ci
12262306a36Sopenharmony_ciint try_lock_extent(struct extent_io_tree *tree, u64 start, u64 end,
12362306a36Sopenharmony_ci		    struct extent_state **cached);
12462306a36Sopenharmony_ci
12562306a36Sopenharmony_ciint __init extent_state_init_cachep(void);
12662306a36Sopenharmony_civoid __cold extent_state_free_cachep(void);
12762306a36Sopenharmony_ci
12862306a36Sopenharmony_ciu64 count_range_bits(struct extent_io_tree *tree,
12962306a36Sopenharmony_ci		     u64 *start, u64 search_end,
13062306a36Sopenharmony_ci		     u64 max_bytes, u32 bits, int contig,
13162306a36Sopenharmony_ci		     struct extent_state **cached_state);
13262306a36Sopenharmony_ci
13362306a36Sopenharmony_civoid free_extent_state(struct extent_state *state);
13462306a36Sopenharmony_ciint test_range_bit(struct extent_io_tree *tree, u64 start, u64 end,
13562306a36Sopenharmony_ci		   u32 bits, int filled, struct extent_state *cached_state);
13662306a36Sopenharmony_ciint clear_record_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
13762306a36Sopenharmony_ci			     u32 bits, struct extent_changeset *changeset);
13862306a36Sopenharmony_ciint __clear_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
13962306a36Sopenharmony_ci		       u32 bits, struct extent_state **cached,
14062306a36Sopenharmony_ci		       struct extent_changeset *changeset);
14162306a36Sopenharmony_ci
14262306a36Sopenharmony_cistatic inline int clear_extent_bit(struct extent_io_tree *tree, u64 start,
14362306a36Sopenharmony_ci				   u64 end, u32 bits,
14462306a36Sopenharmony_ci				   struct extent_state **cached)
14562306a36Sopenharmony_ci{
14662306a36Sopenharmony_ci	return __clear_extent_bit(tree, start, end, bits, cached, NULL);
14762306a36Sopenharmony_ci}
14862306a36Sopenharmony_ci
14962306a36Sopenharmony_cistatic inline int unlock_extent(struct extent_io_tree *tree, u64 start, u64 end,
15062306a36Sopenharmony_ci				struct extent_state **cached)
15162306a36Sopenharmony_ci{
15262306a36Sopenharmony_ci	return __clear_extent_bit(tree, start, end, EXTENT_LOCKED, cached, NULL);
15362306a36Sopenharmony_ci}
15462306a36Sopenharmony_ci
15562306a36Sopenharmony_cistatic inline int clear_extent_bits(struct extent_io_tree *tree, u64 start,
15662306a36Sopenharmony_ci				    u64 end, u32 bits)
15762306a36Sopenharmony_ci{
15862306a36Sopenharmony_ci	return clear_extent_bit(tree, start, end, bits, NULL);
15962306a36Sopenharmony_ci}
16062306a36Sopenharmony_ci
16162306a36Sopenharmony_ciint set_record_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
16262306a36Sopenharmony_ci			   u32 bits, struct extent_changeset *changeset);
16362306a36Sopenharmony_ciint set_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
16462306a36Sopenharmony_ci		   u32 bits, struct extent_state **cached_state);
16562306a36Sopenharmony_ci
16662306a36Sopenharmony_cistatic inline int clear_extent_uptodate(struct extent_io_tree *tree, u64 start,
16762306a36Sopenharmony_ci		u64 end, struct extent_state **cached_state)
16862306a36Sopenharmony_ci{
16962306a36Sopenharmony_ci	return __clear_extent_bit(tree, start, end, EXTENT_UPTODATE,
17062306a36Sopenharmony_ci				  cached_state, NULL);
17162306a36Sopenharmony_ci}
17262306a36Sopenharmony_ci
17362306a36Sopenharmony_cistatic inline int clear_extent_dirty(struct extent_io_tree *tree, u64 start,
17462306a36Sopenharmony_ci				     u64 end, struct extent_state **cached)
17562306a36Sopenharmony_ci{
17662306a36Sopenharmony_ci	return clear_extent_bit(tree, start, end,
17762306a36Sopenharmony_ci				EXTENT_DIRTY | EXTENT_DELALLOC |
17862306a36Sopenharmony_ci				EXTENT_DO_ACCOUNTING, cached);
17962306a36Sopenharmony_ci}
18062306a36Sopenharmony_ci
18162306a36Sopenharmony_ciint convert_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
18262306a36Sopenharmony_ci		       u32 bits, u32 clear_bits,
18362306a36Sopenharmony_ci		       struct extent_state **cached_state);
18462306a36Sopenharmony_ci
18562306a36Sopenharmony_cibool find_first_extent_bit(struct extent_io_tree *tree, u64 start,
18662306a36Sopenharmony_ci			   u64 *start_ret, u64 *end_ret, u32 bits,
18762306a36Sopenharmony_ci			   struct extent_state **cached_state);
18862306a36Sopenharmony_civoid find_first_clear_extent_bit(struct extent_io_tree *tree, u64 start,
18962306a36Sopenharmony_ci				 u64 *start_ret, u64 *end_ret, u32 bits);
19062306a36Sopenharmony_ciint find_contiguous_extent_bit(struct extent_io_tree *tree, u64 start,
19162306a36Sopenharmony_ci			       u64 *start_ret, u64 *end_ret, u32 bits);
19262306a36Sopenharmony_cibool btrfs_find_delalloc_range(struct extent_io_tree *tree, u64 *start,
19362306a36Sopenharmony_ci			       u64 *end, u64 max_bytes,
19462306a36Sopenharmony_ci			       struct extent_state **cached_state);
19562306a36Sopenharmony_civoid wait_extent_bit(struct extent_io_tree *tree, u64 start, u64 end, u32 bits,
19662306a36Sopenharmony_ci		     struct extent_state **cached_state);
19762306a36Sopenharmony_ci
19862306a36Sopenharmony_ci#endif /* BTRFS_EXTENT_IO_TREE_H */
199