xref: /linux/fs/btrfs/extent-io-tree.h (revision 7a40974fd0efa3698de4c6d1d0ee0436bcc4445d)
19c7d3a54SJosef Bacik /* SPDX-License-Identifier: GPL-2.0 */
29c7d3a54SJosef Bacik 
39c7d3a54SJosef Bacik #ifndef BTRFS_EXTENT_IO_TREE_H
49c7d3a54SJosef Bacik #define BTRFS_EXTENT_IO_TREE_H
59c7d3a54SJosef Bacik 
6602035d7SDavid Sterba #include <linux/rbtree.h>
7602035d7SDavid Sterba #include <linux/spinlock.h>
8602035d7SDavid Sterba #include <linux/refcount.h>
9602035d7SDavid Sterba #include <linux/list.h>
10602035d7SDavid Sterba #include <linux/wait.h>
11d3b4d0fdSDavid Sterba #include "misc.h"
12d3b4d0fdSDavid Sterba 
139c7d3a54SJosef Bacik struct extent_changeset;
14602035d7SDavid Sterba struct btrfs_fs_info;
15602035d7SDavid Sterba struct btrfs_inode;
169c7d3a54SJosef Bacik 
179c7d3a54SJosef Bacik /* Bits for the extent state */
18d3b4d0fdSDavid Sterba enum {
19d3b4d0fdSDavid Sterba 	ENUM_BIT(EXTENT_DIRTY),
20d3b4d0fdSDavid Sterba 	ENUM_BIT(EXTENT_UPTODATE),
21d3b4d0fdSDavid Sterba 	ENUM_BIT(EXTENT_LOCKED),
22*7e2a5950SJosef Bacik 	ENUM_BIT(EXTENT_DIO_LOCKED),
23d3b4d0fdSDavid Sterba 	ENUM_BIT(EXTENT_NEW),
24d3b4d0fdSDavid Sterba 	ENUM_BIT(EXTENT_DELALLOC),
25d3b4d0fdSDavid Sterba 	ENUM_BIT(EXTENT_DEFRAG),
26d3b4d0fdSDavid Sterba 	ENUM_BIT(EXTENT_BOUNDARY),
27d3b4d0fdSDavid Sterba 	ENUM_BIT(EXTENT_NODATASUM),
28d3b4d0fdSDavid Sterba 	ENUM_BIT(EXTENT_CLEAR_META_RESV),
29d3b4d0fdSDavid Sterba 	ENUM_BIT(EXTENT_NEED_WAIT),
30d3b4d0fdSDavid Sterba 	ENUM_BIT(EXTENT_NORESERVE),
31d3b4d0fdSDavid Sterba 	ENUM_BIT(EXTENT_QGROUP_RESERVED),
32d3b4d0fdSDavid Sterba 	ENUM_BIT(EXTENT_CLEAR_DATA_RESV),
332766ff61SFilipe Manana 	/*
34d3b4d0fdSDavid Sterba 	 * Must be cleared only during ordered extent completion or on error
35d3b4d0fdSDavid Sterba 	 * paths if we did not manage to submit bios and create the ordered
36d3b4d0fdSDavid Sterba 	 * extents for the range.  Should not be cleared during page release
37d3b4d0fdSDavid Sterba 	 * and page invalidation (if there is an ordered extent in flight),
38d3b4d0fdSDavid Sterba 	 * that is left for the ordered extent completion.
392766ff61SFilipe Manana 	 */
40d3b4d0fdSDavid Sterba 	ENUM_BIT(EXTENT_DELALLOC_NEW),
412766ff61SFilipe Manana 	/*
42d3b4d0fdSDavid Sterba 	 * When an ordered extent successfully completes for a region marked as
43d3b4d0fdSDavid Sterba 	 * a new delalloc range, use this flag when clearing a new delalloc
44d3b4d0fdSDavid Sterba 	 * range to indicate that the VFS' inode number of bytes should be
45d3b4d0fdSDavid Sterba 	 * incremented and the inode's new delalloc bytes decremented, in an
46d3b4d0fdSDavid Sterba 	 * atomic way to prevent races with stat(2).
472766ff61SFilipe Manana 	 */
48d3b4d0fdSDavid Sterba 	ENUM_BIT(EXTENT_ADD_INODE_BYTES),
49bd015294SJosef Bacik 	/*
50d3b4d0fdSDavid Sterba 	 * Set during truncate when we're clearing an entire range and we just
51d3b4d0fdSDavid Sterba 	 * want the extent states to go away.
52bd015294SJosef Bacik 	 */
53d3b4d0fdSDavid Sterba 	ENUM_BIT(EXTENT_CLEAR_ALL_BITS),
5462bc6047SDavid Sterba 
5562bc6047SDavid Sterba 	/*
5662bc6047SDavid Sterba 	 * This must be last.
5762bc6047SDavid Sterba 	 *
5862bc6047SDavid Sterba 	 * Bit not representing a state but a request for NOWAIT semantics,
5962bc6047SDavid Sterba 	 * e.g. when allocating memory, and must be masked out from the other
6062bc6047SDavid Sterba 	 * bits.
6162bc6047SDavid Sterba 	 */
6262bc6047SDavid Sterba 	ENUM_BIT(EXTENT_NOWAIT)
63d3b4d0fdSDavid Sterba };
64bd015294SJosef Bacik 
659c7d3a54SJosef Bacik #define EXTENT_DO_ACCOUNTING    (EXTENT_CLEAR_META_RESV | \
669c7d3a54SJosef Bacik 				 EXTENT_CLEAR_DATA_RESV)
672766ff61SFilipe Manana #define EXTENT_CTLBITS		(EXTENT_DO_ACCOUNTING | \
68bd015294SJosef Bacik 				 EXTENT_ADD_INODE_BYTES | \
69bd015294SJosef Bacik 				 EXTENT_CLEAR_ALL_BITS)
709c7d3a54SJosef Bacik 
71*7e2a5950SJosef Bacik #define EXTENT_LOCK_BITS	(EXTENT_LOCKED | EXTENT_DIO_LOCKED)
72*7e2a5950SJosef Bacik 
739c7d3a54SJosef Bacik /*
749c7d3a54SJosef Bacik  * Redefined bits above which are used only in the device allocation tree,
759c7d3a54SJosef Bacik  * shouldn't be using EXTENT_LOCKED / EXTENT_BOUNDARY / EXTENT_CLEAR_META_RESV
769c7d3a54SJosef Bacik  * / EXTENT_CLEAR_DATA_RESV because they have special meaning to the bit
779c7d3a54SJosef Bacik  * manipulation functions
789c7d3a54SJosef Bacik  */
799c7d3a54SJosef Bacik #define CHUNK_ALLOCATED				EXTENT_DIRTY
809c7d3a54SJosef Bacik #define CHUNK_TRIMMED				EXTENT_DEFRAG
81c57dd1f2SQu Wenruo #define CHUNK_STATE_MASK			(CHUNK_ALLOCATED |		\
82c57dd1f2SQu Wenruo 						 CHUNK_TRIMMED)
839c7d3a54SJosef Bacik 
849c7d3a54SJosef Bacik enum {
85fe119a6eSNikolay Borisov 	IO_TREE_FS_PINNED_EXTENTS,
86fe119a6eSNikolay Borisov 	IO_TREE_FS_EXCLUDED_EXTENTS,
872c53a14dSQu Wenruo 	IO_TREE_BTREE_INODE_IO,
889c7d3a54SJosef Bacik 	IO_TREE_INODE_IO,
899c7d3a54SJosef Bacik 	IO_TREE_RELOC_BLOCKS,
909c7d3a54SJosef Bacik 	IO_TREE_TRANS_DIRTY_PAGES,
919c7d3a54SJosef Bacik 	IO_TREE_ROOT_DIRTY_LOG_PAGES,
9241a2ee75SJosef Bacik 	IO_TREE_INODE_FILE_EXTENT,
93e289f03eSFilipe Manana 	IO_TREE_LOG_CSUM_RANGE,
949c7d3a54SJosef Bacik 	IO_TREE_SELFTEST,
95154f7cb8SQu Wenruo 	IO_TREE_DEVICE_ALLOC_STATE,
969c7d3a54SJosef Bacik };
979c7d3a54SJosef Bacik 
989c7d3a54SJosef Bacik struct extent_io_tree {
999c7d3a54SJosef Bacik 	struct rb_root state;
100738290c0SDavid Sterba 	/*
101738290c0SDavid Sterba 	 * The fs_info is needed for trace points, a tree attached to an inode
102738290c0SDavid Sterba 	 * needs the inode.
103738290c0SDavid Sterba 	 *
104738290c0SDavid Sterba 	 * owner == IO_TREE_INODE_IO - then inode is valid and fs_info can be
105738290c0SDavid Sterba 	 *                             accessed as inode->root->fs_info
106738290c0SDavid Sterba 	 */
107738290c0SDavid Sterba 	union {
1089c7d3a54SJosef Bacik 		struct btrfs_fs_info *fs_info;
1090988fc7bSDavid Sterba 		struct btrfs_inode *inode;
110738290c0SDavid Sterba 	};
1119c7d3a54SJosef Bacik 
1129c7d3a54SJosef Bacik 	/* Who owns this io tree, should be one of IO_TREE_* */
1139c7d3a54SJosef Bacik 	u8 owner;
1149c7d3a54SJosef Bacik 
1159c7d3a54SJosef Bacik 	spinlock_t lock;
1169c7d3a54SJosef Bacik };
1179c7d3a54SJosef Bacik 
1189c7d3a54SJosef Bacik struct extent_state {
1199c7d3a54SJosef Bacik 	u64 start;
1209c7d3a54SJosef Bacik 	u64 end; /* inclusive */
1219c7d3a54SJosef Bacik 	struct rb_node rb_node;
1229c7d3a54SJosef Bacik 
1239c7d3a54SJosef Bacik 	/* ADD NEW ELEMENTS AFTER THIS */
1249c7d3a54SJosef Bacik 	wait_queue_head_t wq;
1259c7d3a54SJosef Bacik 	refcount_t refs;
126f97e27e9SQu Wenruo 	u32 state;
1279c7d3a54SJosef Bacik 
1289c7d3a54SJosef Bacik #ifdef CONFIG_BTRFS_DEBUG
1299c7d3a54SJosef Bacik 	struct list_head leak_list;
1309c7d3a54SJosef Bacik #endif
1319c7d3a54SJosef Bacik };
1329c7d3a54SJosef Bacik 
133738290c0SDavid Sterba struct btrfs_inode *extent_io_tree_to_inode(struct extent_io_tree *tree);
134738290c0SDavid Sterba const struct btrfs_inode *extent_io_tree_to_inode_const(const struct extent_io_tree *tree);
135738290c0SDavid Sterba const struct btrfs_fs_info *extent_io_tree_to_fs_info(const struct extent_io_tree *tree);
136738290c0SDavid Sterba 
1379c7d3a54SJosef Bacik void extent_io_tree_init(struct btrfs_fs_info *fs_info,
13835da5a7eSDavid Sterba 			 struct extent_io_tree *tree, unsigned int owner);
1399c7d3a54SJosef Bacik void extent_io_tree_release(struct extent_io_tree *tree);
140*7e2a5950SJosef Bacik int __lock_extent(struct extent_io_tree *tree, u64 start, u64 end, u32 bits,
141*7e2a5950SJosef Bacik 		  struct extent_state **cached);
142*7e2a5950SJosef Bacik bool __try_lock_extent(struct extent_io_tree *tree, u64 start, u64 end, u32 bits,
1439c7d3a54SJosef Bacik 		       struct extent_state **cached);
1449c7d3a54SJosef Bacik 
lock_extent(struct extent_io_tree * tree,u64 start,u64 end,struct extent_state ** cached)145*7e2a5950SJosef Bacik static inline int lock_extent(struct extent_io_tree *tree, u64 start, u64 end,
146*7e2a5950SJosef Bacik 			      struct extent_state **cached)
147*7e2a5950SJosef Bacik {
148*7e2a5950SJosef Bacik 	return __lock_extent(tree, start, end, EXTENT_LOCKED, cached);
149*7e2a5950SJosef Bacik }
150*7e2a5950SJosef Bacik 
try_lock_extent(struct extent_io_tree * tree,u64 start,u64 end,struct extent_state ** cached)151*7e2a5950SJosef Bacik static inline bool try_lock_extent(struct extent_io_tree *tree, u64 start,
152*7e2a5950SJosef Bacik 				   u64 end, struct extent_state **cached)
153*7e2a5950SJosef Bacik {
154*7e2a5950SJosef Bacik 	return __try_lock_extent(tree, start, end, EXTENT_LOCKED, cached);
155*7e2a5950SJosef Bacik }
1569c7d3a54SJosef Bacik 
157a62a3bd9SJosef Bacik int __init extent_state_init_cachep(void);
158a62a3bd9SJosef Bacik void __cold extent_state_free_cachep(void);
1599c7d3a54SJosef Bacik 
1609c7d3a54SJosef Bacik u64 count_range_bits(struct extent_io_tree *tree,
1619c7d3a54SJosef Bacik 		     u64 *start, u64 search_end,
1628c6e53a7SFilipe Manana 		     u64 max_bytes, u32 bits, int contig,
1638c6e53a7SFilipe Manana 		     struct extent_state **cached_state);
1649c7d3a54SJosef Bacik 
1659c7d3a54SJosef Bacik void free_extent_state(struct extent_state *state);
166893fe243SDavid Sterba bool test_range_bit(struct extent_io_tree *tree, u64 start, u64 end, u32 bit,
167893fe243SDavid Sterba 		    struct extent_state *cached_state);
16899be1a66SDavid Sterba bool test_range_bit_exists(struct extent_io_tree *tree, u64 start, u64 end, u32 bit);
1699c7d3a54SJosef Bacik int clear_record_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
170f97e27e9SQu Wenruo 			     u32 bits, struct extent_changeset *changeset);
1719c7d3a54SJosef Bacik int __clear_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
1721d126800SDavid Sterba 		       u32 bits, struct extent_state **cached,
173bd015294SJosef Bacik 		       struct extent_changeset *changeset);
1749c7d3a54SJosef Bacik 
clear_extent_bit(struct extent_io_tree * tree,u64 start,u64 end,u32 bits,struct extent_state ** cached)175a6631887SJosef Bacik static inline int clear_extent_bit(struct extent_io_tree *tree, u64 start,
176bd015294SJosef Bacik 				   u64 end, u32 bits,
177a6631887SJosef Bacik 				   struct extent_state **cached)
178a6631887SJosef Bacik {
1791d126800SDavid Sterba 	return __clear_extent_bit(tree, start, end, bits, cached, NULL);
180a6631887SJosef Bacik }
181a6631887SJosef Bacik 
unlock_extent(struct extent_io_tree * tree,u64 start,u64 end,struct extent_state ** cached)182570eb97bSJosef Bacik static inline int unlock_extent(struct extent_io_tree *tree, u64 start, u64 end,
183570eb97bSJosef Bacik 				struct extent_state **cached)
1849c7d3a54SJosef Bacik {
1851d126800SDavid Sterba 	return __clear_extent_bit(tree, start, end, EXTENT_LOCKED, cached, NULL);
1869c7d3a54SJosef Bacik }
1879c7d3a54SJosef Bacik 
clear_extent_bits(struct extent_io_tree * tree,u64 start,u64 end,u32 bits)1889c7d3a54SJosef Bacik static inline int clear_extent_bits(struct extent_io_tree *tree, u64 start,
189f97e27e9SQu Wenruo 				    u64 end, u32 bits)
1909c7d3a54SJosef Bacik {
191bd015294SJosef Bacik 	return clear_extent_bit(tree, start, end, bits, NULL);
1929c7d3a54SJosef Bacik }
1939c7d3a54SJosef Bacik 
1949c7d3a54SJosef Bacik int set_record_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
195f97e27e9SQu Wenruo 			   u32 bits, struct extent_changeset *changeset);
1969c7d3a54SJosef Bacik int set_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
1971d126800SDavid Sterba 		   u32 bits, struct extent_state **cached_state);
198a6631887SJosef Bacik 
clear_extent_uptodate(struct extent_io_tree * tree,u64 start,u64 end,struct extent_state ** cached_state)1999c7d3a54SJosef Bacik static inline int clear_extent_uptodate(struct extent_io_tree *tree, u64 start,
2009c7d3a54SJosef Bacik 		u64 end, struct extent_state **cached_state)
2019c7d3a54SJosef Bacik {
202bd015294SJosef Bacik 	return __clear_extent_bit(tree, start, end, EXTENT_UPTODATE,
2031d126800SDavid Sterba 				  cached_state, NULL);
2049c7d3a54SJosef Bacik }
2059c7d3a54SJosef Bacik 
clear_extent_dirty(struct extent_io_tree * tree,u64 start,u64 end,struct extent_state ** cached)2069c7d3a54SJosef Bacik static inline int clear_extent_dirty(struct extent_io_tree *tree, u64 start,
2079c7d3a54SJosef Bacik 				     u64 end, struct extent_state **cached)
2089c7d3a54SJosef Bacik {
2099c7d3a54SJosef Bacik 	return clear_extent_bit(tree, start, end,
2109c7d3a54SJosef Bacik 				EXTENT_DIRTY | EXTENT_DELALLOC |
211bd015294SJosef Bacik 				EXTENT_DO_ACCOUNTING, cached);
2129c7d3a54SJosef Bacik }
2139c7d3a54SJosef Bacik 
2149c7d3a54SJosef Bacik int convert_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
215f97e27e9SQu Wenruo 		       u32 bits, u32 clear_bits,
2169c7d3a54SJosef Bacik 		       struct extent_state **cached_state);
2179c7d3a54SJosef Bacik 
218e5860f82SFilipe Manana bool find_first_extent_bit(struct extent_io_tree *tree, u64 start,
219f97e27e9SQu Wenruo 			   u64 *start_ret, u64 *end_ret, u32 bits,
2209c7d3a54SJosef Bacik 			   struct extent_state **cached_state);
2219c7d3a54SJosef Bacik void find_first_clear_extent_bit(struct extent_io_tree *tree, u64 start,
222f97e27e9SQu Wenruo 				 u64 *start_ret, u64 *end_ret, u32 bits);
22341a2ee75SJosef Bacik int find_contiguous_extent_bit(struct extent_io_tree *tree, u64 start,
224f97e27e9SQu Wenruo 			       u64 *start_ret, u64 *end_ret, u32 bits);
225083e75e7SJosef Bacik bool btrfs_find_delalloc_range(struct extent_io_tree *tree, u64 *start,
226083e75e7SJosef Bacik 			       u64 *end, u64 max_bytes,
227083e75e7SJosef Bacik 			       struct extent_state **cached_state);
lock_dio_extent(struct extent_io_tree * tree,u64 start,u64 end,struct extent_state ** cached)228*7e2a5950SJosef Bacik static inline int lock_dio_extent(struct extent_io_tree *tree, u64 start,
229*7e2a5950SJosef Bacik 				  u64 end, struct extent_state **cached)
230*7e2a5950SJosef Bacik {
231*7e2a5950SJosef Bacik 	return __lock_extent(tree, start, end, EXTENT_DIO_LOCKED, cached);
232*7e2a5950SJosef Bacik }
233*7e2a5950SJosef Bacik 
try_lock_dio_extent(struct extent_io_tree * tree,u64 start,u64 end,struct extent_state ** cached)234*7e2a5950SJosef Bacik static inline bool try_lock_dio_extent(struct extent_io_tree *tree, u64 start,
235*7e2a5950SJosef Bacik 				       u64 end, struct extent_state **cached)
236*7e2a5950SJosef Bacik {
237*7e2a5950SJosef Bacik 	return __try_lock_extent(tree, start, end, EXTENT_DIO_LOCKED, cached);
238*7e2a5950SJosef Bacik }
239*7e2a5950SJosef Bacik 
unlock_dio_extent(struct extent_io_tree * tree,u64 start,u64 end,struct extent_state ** cached)240*7e2a5950SJosef Bacik static inline int unlock_dio_extent(struct extent_io_tree *tree, u64 start,
241*7e2a5950SJosef Bacik 				    u64 end, struct extent_state **cached)
242*7e2a5950SJosef Bacik {
243*7e2a5950SJosef Bacik 	return __clear_extent_bit(tree, start, end, EXTENT_DIO_LOCKED, cached, NULL);
244*7e2a5950SJosef Bacik }
2459c7d3a54SJosef Bacik 
2469c7d3a54SJosef Bacik #endif /* BTRFS_EXTENT_IO_TREE_H */
247