xref: /openbmc/linux/fs/btrfs/extent-io-tree.h (revision 2279a270534c678e78f961fed8e09c9c69611ceb)
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)
249c7d3a54SJosef Bacik #define EXTENT_DELALLOC_NEW	(1U << 14)
259c7d3a54SJosef Bacik #define EXTENT_DO_ACCOUNTING    (EXTENT_CLEAR_META_RESV | \
269c7d3a54SJosef Bacik 				 EXTENT_CLEAR_DATA_RESV)
279c7d3a54SJosef Bacik #define EXTENT_CTLBITS		(EXTENT_DO_ACCOUNTING)
289c7d3a54SJosef Bacik 
299c7d3a54SJosef Bacik /*
309c7d3a54SJosef Bacik  * Redefined bits above which are used only in the device allocation tree,
319c7d3a54SJosef Bacik  * shouldn't be using EXTENT_LOCKED / EXTENT_BOUNDARY / EXTENT_CLEAR_META_RESV
329c7d3a54SJosef Bacik  * / EXTENT_CLEAR_DATA_RESV because they have special meaning to the bit
339c7d3a54SJosef Bacik  * manipulation functions
349c7d3a54SJosef Bacik  */
359c7d3a54SJosef Bacik #define CHUNK_ALLOCATED				EXTENT_DIRTY
369c7d3a54SJosef Bacik #define CHUNK_TRIMMED				EXTENT_DEFRAG
379c7d3a54SJosef Bacik 
389c7d3a54SJosef Bacik enum {
39fe119a6eSNikolay Borisov 	IO_TREE_FS_PINNED_EXTENTS,
40fe119a6eSNikolay Borisov 	IO_TREE_FS_EXCLUDED_EXTENTS,
419c7d3a54SJosef Bacik 	IO_TREE_INODE_IO,
429c7d3a54SJosef Bacik 	IO_TREE_INODE_IO_FAILURE,
439c7d3a54SJosef Bacik 	IO_TREE_RELOC_BLOCKS,
449c7d3a54SJosef Bacik 	IO_TREE_TRANS_DIRTY_PAGES,
459c7d3a54SJosef Bacik 	IO_TREE_ROOT_DIRTY_LOG_PAGES,
4641a2ee75SJosef Bacik 	IO_TREE_INODE_FILE_EXTENT,
47e289f03eSFilipe Manana 	IO_TREE_LOG_CSUM_RANGE,
489c7d3a54SJosef Bacik 	IO_TREE_SELFTEST,
499c7d3a54SJosef Bacik };
509c7d3a54SJosef Bacik 
519c7d3a54SJosef Bacik struct extent_io_tree {
529c7d3a54SJosef Bacik 	struct rb_root state;
539c7d3a54SJosef Bacik 	struct btrfs_fs_info *fs_info;
549c7d3a54SJosef Bacik 	void *private_data;
559c7d3a54SJosef Bacik 	u64 dirty_bytes;
569c7d3a54SJosef Bacik 	bool track_uptodate;
579c7d3a54SJosef Bacik 
589c7d3a54SJosef Bacik 	/* Who owns this io tree, should be one of IO_TREE_* */
599c7d3a54SJosef Bacik 	u8 owner;
609c7d3a54SJosef Bacik 
619c7d3a54SJosef Bacik 	spinlock_t lock;
629c7d3a54SJosef Bacik 	const struct extent_io_ops *ops;
639c7d3a54SJosef Bacik };
649c7d3a54SJosef Bacik 
659c7d3a54SJosef Bacik struct extent_state {
669c7d3a54SJosef Bacik 	u64 start;
679c7d3a54SJosef Bacik 	u64 end; /* inclusive */
689c7d3a54SJosef Bacik 	struct rb_node rb_node;
699c7d3a54SJosef Bacik 
709c7d3a54SJosef Bacik 	/* ADD NEW ELEMENTS AFTER THIS */
719c7d3a54SJosef Bacik 	wait_queue_head_t wq;
729c7d3a54SJosef Bacik 	refcount_t refs;
739c7d3a54SJosef Bacik 	unsigned state;
749c7d3a54SJosef Bacik 
759c7d3a54SJosef Bacik 	struct io_failure_record *failrec;
769c7d3a54SJosef Bacik 
779c7d3a54SJosef Bacik #ifdef CONFIG_BTRFS_DEBUG
789c7d3a54SJosef Bacik 	struct list_head leak_list;
799c7d3a54SJosef Bacik #endif
809c7d3a54SJosef Bacik };
819c7d3a54SJosef Bacik 
829c7d3a54SJosef Bacik int __init extent_state_cache_init(void);
839c7d3a54SJosef Bacik void __cold extent_state_cache_exit(void);
849c7d3a54SJosef Bacik 
859c7d3a54SJosef Bacik void extent_io_tree_init(struct btrfs_fs_info *fs_info,
869c7d3a54SJosef Bacik 			 struct extent_io_tree *tree, unsigned int owner,
879c7d3a54SJosef Bacik 			 void *private_data);
889c7d3a54SJosef Bacik void extent_io_tree_release(struct extent_io_tree *tree);
899c7d3a54SJosef Bacik 
909c7d3a54SJosef Bacik int lock_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
919c7d3a54SJosef Bacik 		     struct extent_state **cached);
929c7d3a54SJosef Bacik 
939c7d3a54SJosef Bacik static inline int lock_extent(struct extent_io_tree *tree, u64 start, u64 end)
949c7d3a54SJosef Bacik {
959c7d3a54SJosef Bacik 	return lock_extent_bits(tree, start, end, NULL);
969c7d3a54SJosef Bacik }
979c7d3a54SJosef Bacik 
989c7d3a54SJosef Bacik int try_lock_extent(struct extent_io_tree *tree, u64 start, u64 end);
999c7d3a54SJosef Bacik 
1009c7d3a54SJosef Bacik int __init extent_io_init(void);
1019c7d3a54SJosef Bacik void __cold extent_io_exit(void);
1029c7d3a54SJosef Bacik 
1039c7d3a54SJosef Bacik u64 count_range_bits(struct extent_io_tree *tree,
1049c7d3a54SJosef Bacik 		     u64 *start, u64 search_end,
1059c7d3a54SJosef Bacik 		     u64 max_bytes, unsigned bits, int contig);
1069c7d3a54SJosef Bacik 
1079c7d3a54SJosef Bacik void free_extent_state(struct extent_state *state);
1089c7d3a54SJosef Bacik int test_range_bit(struct extent_io_tree *tree, u64 start, u64 end,
1099c7d3a54SJosef Bacik 		   unsigned bits, int filled,
1109c7d3a54SJosef Bacik 		   struct extent_state *cached_state);
1119c7d3a54SJosef Bacik int clear_record_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
1129c7d3a54SJosef Bacik 		unsigned bits, struct extent_changeset *changeset);
1139c7d3a54SJosef Bacik int clear_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
1149c7d3a54SJosef Bacik 		     unsigned bits, int wake, int delete,
1159c7d3a54SJosef Bacik 		     struct extent_state **cached);
1169c7d3a54SJosef Bacik int __clear_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
1179c7d3a54SJosef Bacik 		     unsigned bits, int wake, int delete,
1189c7d3a54SJosef Bacik 		     struct extent_state **cached, gfp_t mask,
1199c7d3a54SJosef Bacik 		     struct extent_changeset *changeset);
1209c7d3a54SJosef Bacik 
1219c7d3a54SJosef Bacik static inline int unlock_extent(struct extent_io_tree *tree, u64 start, u64 end)
1229c7d3a54SJosef Bacik {
1239c7d3a54SJosef Bacik 	return clear_extent_bit(tree, start, end, EXTENT_LOCKED, 1, 0, NULL);
1249c7d3a54SJosef Bacik }
1259c7d3a54SJosef Bacik 
1269c7d3a54SJosef Bacik static inline int unlock_extent_cached(struct extent_io_tree *tree, u64 start,
1279c7d3a54SJosef Bacik 		u64 end, struct extent_state **cached)
1289c7d3a54SJosef Bacik {
1299c7d3a54SJosef Bacik 	return __clear_extent_bit(tree, start, end, EXTENT_LOCKED, 1, 0, cached,
1309c7d3a54SJosef Bacik 				GFP_NOFS, NULL);
1319c7d3a54SJosef Bacik }
1329c7d3a54SJosef Bacik 
1339c7d3a54SJosef Bacik static inline int unlock_extent_cached_atomic(struct extent_io_tree *tree,
1349c7d3a54SJosef Bacik 		u64 start, u64 end, struct extent_state **cached)
1359c7d3a54SJosef Bacik {
1369c7d3a54SJosef Bacik 	return __clear_extent_bit(tree, start, end, EXTENT_LOCKED, 1, 0, cached,
1379c7d3a54SJosef Bacik 				GFP_ATOMIC, NULL);
1389c7d3a54SJosef Bacik }
1399c7d3a54SJosef Bacik 
1409c7d3a54SJosef Bacik static inline int clear_extent_bits(struct extent_io_tree *tree, u64 start,
1419c7d3a54SJosef Bacik 		u64 end, unsigned bits)
1429c7d3a54SJosef Bacik {
1439c7d3a54SJosef Bacik 	int wake = 0;
1449c7d3a54SJosef Bacik 
1459c7d3a54SJosef Bacik 	if (bits & EXTENT_LOCKED)
1469c7d3a54SJosef Bacik 		wake = 1;
1479c7d3a54SJosef Bacik 
1489c7d3a54SJosef Bacik 	return clear_extent_bit(tree, start, end, bits, wake, 0, NULL);
1499c7d3a54SJosef Bacik }
1509c7d3a54SJosef Bacik 
1519c7d3a54SJosef Bacik int set_record_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
1529c7d3a54SJosef Bacik 			   unsigned bits, struct extent_changeset *changeset);
1539c7d3a54SJosef Bacik int set_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
1549c7d3a54SJosef Bacik 		   unsigned bits, u64 *failed_start,
1559c7d3a54SJosef Bacik 		   struct extent_state **cached_state, gfp_t mask);
1569c7d3a54SJosef Bacik int set_extent_bits_nowait(struct extent_io_tree *tree, u64 start, u64 end,
1579c7d3a54SJosef Bacik 			   unsigned bits);
1589c7d3a54SJosef Bacik 
1599c7d3a54SJosef Bacik static inline int set_extent_bits(struct extent_io_tree *tree, u64 start,
1609c7d3a54SJosef Bacik 		u64 end, unsigned bits)
1619c7d3a54SJosef Bacik {
1629c7d3a54SJosef Bacik 	return set_extent_bit(tree, start, end, bits, NULL, NULL, GFP_NOFS);
1639c7d3a54SJosef Bacik }
1649c7d3a54SJosef Bacik 
1659c7d3a54SJosef Bacik static inline int clear_extent_uptodate(struct extent_io_tree *tree, u64 start,
1669c7d3a54SJosef Bacik 		u64 end, struct extent_state **cached_state)
1679c7d3a54SJosef Bacik {
1689c7d3a54SJosef Bacik 	return __clear_extent_bit(tree, start, end, EXTENT_UPTODATE, 0, 0,
1699c7d3a54SJosef Bacik 				cached_state, GFP_NOFS, NULL);
1709c7d3a54SJosef Bacik }
1719c7d3a54SJosef Bacik 
1729c7d3a54SJosef Bacik static inline int set_extent_dirty(struct extent_io_tree *tree, u64 start,
1739c7d3a54SJosef Bacik 		u64 end, gfp_t mask)
1749c7d3a54SJosef Bacik {
1759c7d3a54SJosef Bacik 	return set_extent_bit(tree, start, end, EXTENT_DIRTY, NULL,
1769c7d3a54SJosef Bacik 			      NULL, mask);
1779c7d3a54SJosef Bacik }
1789c7d3a54SJosef Bacik 
1799c7d3a54SJosef Bacik static inline int clear_extent_dirty(struct extent_io_tree *tree, u64 start,
1809c7d3a54SJosef Bacik 				     u64 end, struct extent_state **cached)
1819c7d3a54SJosef Bacik {
1829c7d3a54SJosef Bacik 	return clear_extent_bit(tree, start, end,
1839c7d3a54SJosef Bacik 				EXTENT_DIRTY | EXTENT_DELALLOC |
1849c7d3a54SJosef Bacik 				EXTENT_DO_ACCOUNTING, 0, 0, cached);
1859c7d3a54SJosef Bacik }
1869c7d3a54SJosef Bacik 
1879c7d3a54SJosef Bacik int convert_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
1889c7d3a54SJosef Bacik 		       unsigned bits, unsigned clear_bits,
1899c7d3a54SJosef Bacik 		       struct extent_state **cached_state);
1909c7d3a54SJosef Bacik 
1919c7d3a54SJosef Bacik static inline int set_extent_delalloc(struct extent_io_tree *tree, u64 start,
1929c7d3a54SJosef Bacik 				      u64 end, unsigned int extra_bits,
1939c7d3a54SJosef Bacik 				      struct extent_state **cached_state)
1949c7d3a54SJosef Bacik {
1959c7d3a54SJosef Bacik 	return set_extent_bit(tree, start, end,
1969c7d3a54SJosef Bacik 			      EXTENT_DELALLOC | EXTENT_UPTODATE | extra_bits,
1979c7d3a54SJosef Bacik 			      NULL, cached_state, GFP_NOFS);
1989c7d3a54SJosef Bacik }
1999c7d3a54SJosef Bacik 
2009c7d3a54SJosef Bacik static inline int set_extent_defrag(struct extent_io_tree *tree, u64 start,
2019c7d3a54SJosef Bacik 		u64 end, struct extent_state **cached_state)
2029c7d3a54SJosef Bacik {
2039c7d3a54SJosef Bacik 	return set_extent_bit(tree, start, end,
2049c7d3a54SJosef Bacik 			      EXTENT_DELALLOC | EXTENT_UPTODATE | EXTENT_DEFRAG,
2059c7d3a54SJosef Bacik 			      NULL, cached_state, GFP_NOFS);
2069c7d3a54SJosef Bacik }
2079c7d3a54SJosef Bacik 
2089c7d3a54SJosef Bacik static inline int set_extent_new(struct extent_io_tree *tree, u64 start,
2099c7d3a54SJosef Bacik 		u64 end)
2109c7d3a54SJosef Bacik {
2119c7d3a54SJosef Bacik 	return set_extent_bit(tree, start, end, EXTENT_NEW, NULL, NULL,
2129c7d3a54SJosef Bacik 			GFP_NOFS);
2139c7d3a54SJosef Bacik }
2149c7d3a54SJosef Bacik 
2159c7d3a54SJosef Bacik static inline int set_extent_uptodate(struct extent_io_tree *tree, u64 start,
2169c7d3a54SJosef Bacik 		u64 end, struct extent_state **cached_state, gfp_t mask)
2179c7d3a54SJosef Bacik {
2189c7d3a54SJosef Bacik 	return set_extent_bit(tree, start, end, EXTENT_UPTODATE, NULL,
2199c7d3a54SJosef Bacik 			      cached_state, mask);
2209c7d3a54SJosef Bacik }
2219c7d3a54SJosef Bacik 
2229c7d3a54SJosef Bacik int find_first_extent_bit(struct extent_io_tree *tree, u64 start,
2239c7d3a54SJosef Bacik 			  u64 *start_ret, u64 *end_ret, unsigned bits,
2249c7d3a54SJosef Bacik 			  struct extent_state **cached_state);
2259c7d3a54SJosef Bacik void find_first_clear_extent_bit(struct extent_io_tree *tree, u64 start,
2269c7d3a54SJosef Bacik 				 u64 *start_ret, u64 *end_ret, unsigned bits);
22741a2ee75SJosef Bacik int find_contiguous_extent_bit(struct extent_io_tree *tree, u64 start,
22841a2ee75SJosef Bacik 			       u64 *start_ret, u64 *end_ret, unsigned bits);
2299c7d3a54SJosef Bacik int extent_invalidatepage(struct extent_io_tree *tree,
2309c7d3a54SJosef Bacik 			  struct page *page, unsigned long offset);
231083e75e7SJosef Bacik bool btrfs_find_delalloc_range(struct extent_io_tree *tree, u64 *start,
232083e75e7SJosef Bacik 			       u64 *end, u64 max_bytes,
233083e75e7SJosef Bacik 			       struct extent_state **cached_state);
2349c7d3a54SJosef Bacik 
235b3f167aaSJosef Bacik /* This should be reworked in the future and put elsewhere. */
236*2279a270SNikolay Borisov struct io_failure_record *get_state_failrec(struct extent_io_tree *tree, u64 start);
237b3f167aaSJosef Bacik int set_state_failrec(struct extent_io_tree *tree, u64 start,
238b3f167aaSJosef Bacik 		      struct io_failure_record *failrec);
239b3f167aaSJosef Bacik void btrfs_free_io_failure_record(struct btrfs_inode *inode, u64 start,
240b3f167aaSJosef Bacik 		u64 end);
241b3f167aaSJosef Bacik int btrfs_get_io_failure_record(struct inode *inode, u64 start, u64 end,
242b3f167aaSJosef Bacik 				struct io_failure_record **failrec_ret);
243b3f167aaSJosef Bacik int free_io_failure(struct extent_io_tree *failure_tree,
244b3f167aaSJosef Bacik 		    struct extent_io_tree *io_tree,
245b3f167aaSJosef Bacik 		    struct io_failure_record *rec);
246b3f167aaSJosef Bacik int clean_io_failure(struct btrfs_fs_info *fs_info,
247b3f167aaSJosef Bacik 		     struct extent_io_tree *failure_tree,
248b3f167aaSJosef Bacik 		     struct extent_io_tree *io_tree, u64 start,
249b3f167aaSJosef Bacik 		     struct page *page, u64 ino, unsigned int pg_offset);
250b3f167aaSJosef Bacik 
2519c7d3a54SJosef Bacik #endif /* BTRFS_EXTENT_IO_TREE_H */
252