xref: /linux/fs/btrfs/extent-io-tree.h (revision 994bcd1eae5bc6f24a90765b1fb8be471ab56b33)
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 
69c7d3a54SJosef Bacik struct extent_changeset;
7b3f167aaSJosef Bacik struct io_failure_record;
89c7d3a54SJosef Bacik 
99c7d3a54SJosef Bacik /* Bits for the extent state */
109c7d3a54SJosef Bacik #define EXTENT_DIRTY		(1U << 0)
119c7d3a54SJosef Bacik #define EXTENT_UPTODATE		(1U << 1)
129c7d3a54SJosef Bacik #define EXTENT_LOCKED		(1U << 2)
139c7d3a54SJosef Bacik #define EXTENT_NEW		(1U << 3)
149c7d3a54SJosef Bacik #define EXTENT_DELALLOC		(1U << 4)
159c7d3a54SJosef Bacik #define EXTENT_DEFRAG		(1U << 5)
169c7d3a54SJosef Bacik #define EXTENT_BOUNDARY		(1U << 6)
179c7d3a54SJosef Bacik #define EXTENT_NODATASUM	(1U << 7)
189c7d3a54SJosef Bacik #define EXTENT_CLEAR_META_RESV	(1U << 8)
199c7d3a54SJosef Bacik #define EXTENT_NEED_WAIT	(1U << 9)
209c7d3a54SJosef Bacik #define EXTENT_DAMAGED		(1U << 10)
219c7d3a54SJosef Bacik #define EXTENT_NORESERVE	(1U << 11)
229c7d3a54SJosef Bacik #define EXTENT_QGROUP_RESERVED	(1U << 12)
239c7d3a54SJosef Bacik #define EXTENT_CLEAR_DATA_RESV	(1U << 13)
242766ff61SFilipe Manana /*
252766ff61SFilipe Manana  * Must be cleared only during ordered extent completion or on error paths if we
262766ff61SFilipe Manana  * did not manage to submit bios and create the ordered extents for the range.
272766ff61SFilipe Manana  * Should not be cleared during page release and page invalidation (if there is
282766ff61SFilipe Manana  * an ordered extent in flight), that is left for the ordered extent completion.
292766ff61SFilipe Manana  */
309c7d3a54SJosef Bacik #define EXTENT_DELALLOC_NEW	(1U << 14)
312766ff61SFilipe Manana /*
322766ff61SFilipe Manana  * When an ordered extent successfully completes for a region marked as a new
332766ff61SFilipe Manana  * delalloc range, use this flag when clearing a new delalloc range to indicate
342766ff61SFilipe Manana  * that the VFS' inode number of bytes should be incremented and the inode's new
352766ff61SFilipe Manana  * delalloc bytes decremented, in an atomic way to prevent races with stat(2).
362766ff61SFilipe Manana  */
372766ff61SFilipe Manana #define EXTENT_ADD_INODE_BYTES  (1U << 15)
389c7d3a54SJosef Bacik #define EXTENT_DO_ACCOUNTING    (EXTENT_CLEAR_META_RESV | \
399c7d3a54SJosef Bacik 				 EXTENT_CLEAR_DATA_RESV)
402766ff61SFilipe Manana #define EXTENT_CTLBITS		(EXTENT_DO_ACCOUNTING | \
412766ff61SFilipe Manana 				 EXTENT_ADD_INODE_BYTES)
429c7d3a54SJosef Bacik 
439c7d3a54SJosef Bacik /*
449c7d3a54SJosef Bacik  * Redefined bits above which are used only in the device allocation tree,
459c7d3a54SJosef Bacik  * shouldn't be using EXTENT_LOCKED / EXTENT_BOUNDARY / EXTENT_CLEAR_META_RESV
469c7d3a54SJosef Bacik  * / EXTENT_CLEAR_DATA_RESV because they have special meaning to the bit
479c7d3a54SJosef Bacik  * manipulation functions
489c7d3a54SJosef Bacik  */
499c7d3a54SJosef Bacik #define CHUNK_ALLOCATED				EXTENT_DIRTY
509c7d3a54SJosef Bacik #define CHUNK_TRIMMED				EXTENT_DEFRAG
51c57dd1f2SQu Wenruo #define CHUNK_STATE_MASK			(CHUNK_ALLOCATED |		\
52c57dd1f2SQu Wenruo 						 CHUNK_TRIMMED)
539c7d3a54SJosef Bacik 
549c7d3a54SJosef Bacik enum {
55fe119a6eSNikolay Borisov 	IO_TREE_FS_PINNED_EXTENTS,
56fe119a6eSNikolay Borisov 	IO_TREE_FS_EXCLUDED_EXTENTS,
572c53a14dSQu Wenruo 	IO_TREE_BTREE_INODE_IO,
589c7d3a54SJosef Bacik 	IO_TREE_INODE_IO,
599c7d3a54SJosef Bacik 	IO_TREE_RELOC_BLOCKS,
609c7d3a54SJosef Bacik 	IO_TREE_TRANS_DIRTY_PAGES,
619c7d3a54SJosef Bacik 	IO_TREE_ROOT_DIRTY_LOG_PAGES,
6241a2ee75SJosef Bacik 	IO_TREE_INODE_FILE_EXTENT,
63e289f03eSFilipe Manana 	IO_TREE_LOG_CSUM_RANGE,
649c7d3a54SJosef Bacik 	IO_TREE_SELFTEST,
65154f7cb8SQu Wenruo 	IO_TREE_DEVICE_ALLOC_STATE,
669c7d3a54SJosef Bacik };
679c7d3a54SJosef Bacik 
689c7d3a54SJosef Bacik struct extent_io_tree {
699c7d3a54SJosef Bacik 	struct rb_root state;
709c7d3a54SJosef Bacik 	struct btrfs_fs_info *fs_info;
719c7d3a54SJosef Bacik 	void *private_data;
729c7d3a54SJosef Bacik 	u64 dirty_bytes;
739c7d3a54SJosef Bacik 	bool track_uptodate;
749c7d3a54SJosef Bacik 
759c7d3a54SJosef Bacik 	/* Who owns this io tree, should be one of IO_TREE_* */
769c7d3a54SJosef Bacik 	u8 owner;
779c7d3a54SJosef Bacik 
789c7d3a54SJosef Bacik 	spinlock_t lock;
799c7d3a54SJosef Bacik };
809c7d3a54SJosef Bacik 
819c7d3a54SJosef Bacik struct extent_state {
829c7d3a54SJosef Bacik 	u64 start;
839c7d3a54SJosef Bacik 	u64 end; /* inclusive */
849c7d3a54SJosef Bacik 	struct rb_node rb_node;
859c7d3a54SJosef Bacik 
869c7d3a54SJosef Bacik 	/* ADD NEW ELEMENTS AFTER THIS */
879c7d3a54SJosef Bacik 	wait_queue_head_t wq;
889c7d3a54SJosef Bacik 	refcount_t refs;
89f97e27e9SQu Wenruo 	u32 state;
909c7d3a54SJosef Bacik 
919c7d3a54SJosef Bacik #ifdef CONFIG_BTRFS_DEBUG
929c7d3a54SJosef Bacik 	struct list_head leak_list;
939c7d3a54SJosef Bacik #endif
949c7d3a54SJosef Bacik };
959c7d3a54SJosef Bacik 
969c7d3a54SJosef Bacik void extent_io_tree_init(struct btrfs_fs_info *fs_info,
979c7d3a54SJosef Bacik 			 struct extent_io_tree *tree, unsigned int owner,
989c7d3a54SJosef Bacik 			 void *private_data);
999c7d3a54SJosef Bacik void extent_io_tree_release(struct extent_io_tree *tree);
1009c7d3a54SJosef Bacik 
1019c7d3a54SJosef Bacik int lock_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
1029c7d3a54SJosef Bacik 		     struct extent_state **cached);
1039c7d3a54SJosef Bacik 
1049c7d3a54SJosef Bacik static inline int lock_extent(struct extent_io_tree *tree, u64 start, u64 end)
1059c7d3a54SJosef Bacik {
1069c7d3a54SJosef Bacik 	return lock_extent_bits(tree, start, end, NULL);
1079c7d3a54SJosef Bacik }
1089c7d3a54SJosef Bacik 
1099c7d3a54SJosef Bacik int try_lock_extent(struct extent_io_tree *tree, u64 start, u64 end);
1109c7d3a54SJosef Bacik 
111a62a3bd9SJosef Bacik int __init extent_state_init_cachep(void);
112a62a3bd9SJosef Bacik void __cold extent_state_free_cachep(void);
1139c7d3a54SJosef Bacik 
1149c7d3a54SJosef Bacik u64 count_range_bits(struct extent_io_tree *tree,
1159c7d3a54SJosef Bacik 		     u64 *start, u64 search_end,
116f97e27e9SQu Wenruo 		     u64 max_bytes, u32 bits, int contig);
1179c7d3a54SJosef Bacik 
1189c7d3a54SJosef Bacik void free_extent_state(struct extent_state *state);
1199c7d3a54SJosef Bacik int test_range_bit(struct extent_io_tree *tree, u64 start, u64 end,
120f97e27e9SQu Wenruo 		   u32 bits, int filled, struct extent_state *cached_state);
1219c7d3a54SJosef Bacik int clear_record_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
122f97e27e9SQu Wenruo 			     u32 bits, struct extent_changeset *changeset);
1239c7d3a54SJosef Bacik int __clear_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
124dbbf4992SJosef Bacik 		       u32 bits, int delete, struct extent_state **cached,
125dbbf4992SJosef Bacik 		       gfp_t mask, struct extent_changeset *changeset);
1269c7d3a54SJosef Bacik 
127a6631887SJosef Bacik static inline int clear_extent_bit(struct extent_io_tree *tree, u64 start,
128dbbf4992SJosef Bacik 				   u64 end, u32 bits, int delete,
129a6631887SJosef Bacik 				   struct extent_state **cached)
130a6631887SJosef Bacik {
131dbbf4992SJosef Bacik 	return __clear_extent_bit(tree, start, end, bits, delete, cached,
132dbbf4992SJosef Bacik 				  GFP_NOFS, NULL);
133a6631887SJosef Bacik }
134a6631887SJosef Bacik 
1359c7d3a54SJosef Bacik static inline int unlock_extent(struct extent_io_tree *tree, u64 start, u64 end)
1369c7d3a54SJosef Bacik {
137dbbf4992SJosef Bacik 	return clear_extent_bit(tree, start, end, EXTENT_LOCKED, 0, NULL);
1389c7d3a54SJosef Bacik }
1399c7d3a54SJosef Bacik 
1409c7d3a54SJosef Bacik static inline int unlock_extent_cached(struct extent_io_tree *tree, u64 start,
1419c7d3a54SJosef Bacik 		u64 end, struct extent_state **cached)
1429c7d3a54SJosef Bacik {
143dbbf4992SJosef Bacik 	return __clear_extent_bit(tree, start, end, EXTENT_LOCKED, 0, cached,
1449c7d3a54SJosef Bacik 				  GFP_NOFS, NULL);
1459c7d3a54SJosef Bacik }
1469c7d3a54SJosef Bacik 
1479c7d3a54SJosef Bacik static inline int unlock_extent_cached_atomic(struct extent_io_tree *tree,
1489c7d3a54SJosef Bacik 		u64 start, u64 end, struct extent_state **cached)
1499c7d3a54SJosef Bacik {
150dbbf4992SJosef Bacik 	return __clear_extent_bit(tree, start, end, EXTENT_LOCKED, 0, cached,
1519c7d3a54SJosef Bacik 				  GFP_ATOMIC, NULL);
1529c7d3a54SJosef Bacik }
1539c7d3a54SJosef Bacik 
1549c7d3a54SJosef Bacik static inline int clear_extent_bits(struct extent_io_tree *tree, u64 start,
155f97e27e9SQu Wenruo 				    u64 end, u32 bits)
1569c7d3a54SJosef Bacik {
157dbbf4992SJosef Bacik 	return clear_extent_bit(tree, start, end, bits, 0, NULL);
1589c7d3a54SJosef Bacik }
1599c7d3a54SJosef Bacik 
1609c7d3a54SJosef Bacik int set_record_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
161f97e27e9SQu Wenruo 			   u32 bits, struct extent_changeset *changeset);
1629c7d3a54SJosef Bacik int set_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
163*994bcd1eSJosef Bacik 		   u32 bits, struct extent_state **cached_state, gfp_t mask,
1641cab5e72SNikolay Borisov 		   struct extent_changeset *changeset);
165a6631887SJosef Bacik 
166a6631887SJosef Bacik static inline int set_extent_bits_nowait(struct extent_io_tree *tree, u64 start,
167a6631887SJosef Bacik 					 u64 end, u32 bits)
168a6631887SJosef Bacik {
169*994bcd1eSJosef Bacik 	return set_extent_bit(tree, start, end, bits, NULL, GFP_NOWAIT, NULL);
170a6631887SJosef Bacik }
1719c7d3a54SJosef Bacik 
1729c7d3a54SJosef Bacik static inline int set_extent_bits(struct extent_io_tree *tree, u64 start,
173f97e27e9SQu Wenruo 		u64 end, u32 bits)
1749c7d3a54SJosef Bacik {
175*994bcd1eSJosef Bacik 	return set_extent_bit(tree, start, end, bits, NULL, GFP_NOFS, NULL);
1769c7d3a54SJosef Bacik }
1779c7d3a54SJosef Bacik 
1789c7d3a54SJosef Bacik static inline int clear_extent_uptodate(struct extent_io_tree *tree, u64 start,
1799c7d3a54SJosef Bacik 		u64 end, struct extent_state **cached_state)
1809c7d3a54SJosef Bacik {
181dbbf4992SJosef Bacik 	return __clear_extent_bit(tree, start, end, EXTENT_UPTODATE, 0,
1829c7d3a54SJosef Bacik 				  cached_state, GFP_NOFS, NULL);
1839c7d3a54SJosef Bacik }
1849c7d3a54SJosef Bacik 
1859c7d3a54SJosef Bacik static inline int set_extent_dirty(struct extent_io_tree *tree, u64 start,
1869c7d3a54SJosef Bacik 		u64 end, gfp_t mask)
1879c7d3a54SJosef Bacik {
188*994bcd1eSJosef Bacik 	return set_extent_bit(tree, start, end, EXTENT_DIRTY, NULL, mask, NULL);
1899c7d3a54SJosef Bacik }
1909c7d3a54SJosef Bacik 
1919c7d3a54SJosef Bacik static inline int clear_extent_dirty(struct extent_io_tree *tree, u64 start,
1929c7d3a54SJosef Bacik 				     u64 end, struct extent_state **cached)
1939c7d3a54SJosef Bacik {
1949c7d3a54SJosef Bacik 	return clear_extent_bit(tree, start, end,
1959c7d3a54SJosef Bacik 				EXTENT_DIRTY | EXTENT_DELALLOC |
196dbbf4992SJosef Bacik 				EXTENT_DO_ACCOUNTING, 0, cached);
1979c7d3a54SJosef Bacik }
1989c7d3a54SJosef Bacik 
1999c7d3a54SJosef Bacik int convert_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
200f97e27e9SQu Wenruo 		       u32 bits, u32 clear_bits,
2019c7d3a54SJosef Bacik 		       struct extent_state **cached_state);
2029c7d3a54SJosef Bacik 
2039c7d3a54SJosef Bacik static inline int set_extent_delalloc(struct extent_io_tree *tree, u64 start,
204f97e27e9SQu Wenruo 				      u64 end, u32 extra_bits,
2059c7d3a54SJosef Bacik 				      struct extent_state **cached_state)
2069c7d3a54SJosef Bacik {
2079c7d3a54SJosef Bacik 	return set_extent_bit(tree, start, end,
20852b029f4SEthan Lien 			      EXTENT_DELALLOC | extra_bits,
209*994bcd1eSJosef Bacik 			      cached_state, GFP_NOFS, NULL);
2109c7d3a54SJosef Bacik }
2119c7d3a54SJosef Bacik 
2129c7d3a54SJosef Bacik static inline int set_extent_defrag(struct extent_io_tree *tree, u64 start,
2139c7d3a54SJosef Bacik 		u64 end, struct extent_state **cached_state)
2149c7d3a54SJosef Bacik {
2159c7d3a54SJosef Bacik 	return set_extent_bit(tree, start, end,
21652b029f4SEthan Lien 			      EXTENT_DELALLOC | EXTENT_DEFRAG,
217*994bcd1eSJosef Bacik 			      cached_state, GFP_NOFS, NULL);
2189c7d3a54SJosef Bacik }
2199c7d3a54SJosef Bacik 
2209c7d3a54SJosef Bacik static inline int set_extent_new(struct extent_io_tree *tree, u64 start,
2219c7d3a54SJosef Bacik 		u64 end)
2229c7d3a54SJosef Bacik {
223*994bcd1eSJosef Bacik 	return set_extent_bit(tree, start, end, EXTENT_NEW, NULL, GFP_NOFS,
224*994bcd1eSJosef Bacik 			      NULL);
2259c7d3a54SJosef Bacik }
2269c7d3a54SJosef Bacik 
2279c7d3a54SJosef Bacik static inline int set_extent_uptodate(struct extent_io_tree *tree, u64 start,
2289c7d3a54SJosef Bacik 		u64 end, struct extent_state **cached_state, gfp_t mask)
2299c7d3a54SJosef Bacik {
230*994bcd1eSJosef Bacik 	return set_extent_bit(tree, start, end, EXTENT_UPTODATE,
2311cab5e72SNikolay Borisov 			      cached_state, mask, NULL);
2329c7d3a54SJosef Bacik }
2339c7d3a54SJosef Bacik 
2349c7d3a54SJosef Bacik int find_first_extent_bit(struct extent_io_tree *tree, u64 start,
235f97e27e9SQu Wenruo 			  u64 *start_ret, u64 *end_ret, u32 bits,
2369c7d3a54SJosef Bacik 			  struct extent_state **cached_state);
2379c7d3a54SJosef Bacik void find_first_clear_extent_bit(struct extent_io_tree *tree, u64 start,
238f97e27e9SQu Wenruo 				 u64 *start_ret, u64 *end_ret, u32 bits);
23941a2ee75SJosef Bacik int find_contiguous_extent_bit(struct extent_io_tree *tree, u64 start,
240f97e27e9SQu Wenruo 			       u64 *start_ret, u64 *end_ret, u32 bits);
241083e75e7SJosef Bacik bool btrfs_find_delalloc_range(struct extent_io_tree *tree, u64 *start,
242083e75e7SJosef Bacik 			       u64 *end, u64 max_bytes,
243083e75e7SJosef Bacik 			       struct extent_state **cached_state);
244ec39e39bSJosef Bacik void wait_extent_bit(struct extent_io_tree *tree, u64 start, u64 end, u32 bits);
2459c7d3a54SJosef Bacik 
2469c7d3a54SJosef Bacik #endif /* BTRFS_EXTENT_IO_TREE_H */
247