xref: /linux/fs/btrfs/extent-tree.h (revision 336b78c655c84ce9ce47219185171b3912109c0a)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 
3 #ifndef BTRFS_EXTENT_TREE_H
4 #define BTRFS_EXTENT_TREE_H
5 
6 #include "misc.h"
7 #include "block-group.h"
8 
9 struct btrfs_free_cluster;
10 
11 enum btrfs_extent_allocation_policy {
12 	BTRFS_EXTENT_ALLOC_CLUSTERED,
13 	BTRFS_EXTENT_ALLOC_ZONED,
14 };
15 
16 struct find_free_extent_ctl {
17 	/* Basic allocation info */
18 	u64 ram_bytes;
19 	u64 num_bytes;
20 	u64 min_alloc_size;
21 	u64 empty_size;
22 	u64 flags;
23 	int delalloc;
24 
25 	/* Where to start the search inside the bg */
26 	u64 search_start;
27 
28 	/* For clustered allocation */
29 	u64 empty_cluster;
30 	struct btrfs_free_cluster *last_ptr;
31 	bool use_cluster;
32 
33 	bool have_caching_bg;
34 	bool orig_have_caching_bg;
35 
36 	/* Allocation is called for tree-log */
37 	bool for_treelog;
38 
39 	/* Allocation is called for data relocation */
40 	bool for_data_reloc;
41 
42 	/* RAID index, converted from flags */
43 	int index;
44 
45 	/*
46 	 * Current loop number, check find_free_extent_update_loop() for details
47 	 */
48 	int loop;
49 
50 	/*
51 	 * Whether we're refilling a cluster, if true we need to re-search
52 	 * current block group but don't try to refill the cluster again.
53 	 */
54 	bool retry_clustered;
55 
56 	/*
57 	 * Whether we're updating free space cache, if true we need to re-search
58 	 * current block group but don't try updating free space cache again.
59 	 */
60 	bool retry_unclustered;
61 
62 	/* If current block group is cached */
63 	int cached;
64 
65 	/* Max contiguous hole found */
66 	u64 max_extent_size;
67 
68 	/* Total free space from free space cache, not always contiguous */
69 	u64 total_free_space;
70 
71 	/* Found result */
72 	u64 found_offset;
73 
74 	/* Hint where to start looking for an empty space */
75 	u64 hint_byte;
76 
77 	/* Allocation policy */
78 	enum btrfs_extent_allocation_policy policy;
79 
80 	/* Whether or not the allocator is currently following a hint */
81 	bool hinted;
82 
83 	/* Size class of block groups to prefer in early loops */
84 	enum btrfs_block_group_size_class size_class;
85 };
86 
87 enum btrfs_inline_ref_type {
88 	BTRFS_REF_TYPE_INVALID,
89 	BTRFS_REF_TYPE_BLOCK,
90 	BTRFS_REF_TYPE_DATA,
91 	BTRFS_REF_TYPE_ANY,
92 };
93 
94 int btrfs_get_extent_inline_ref_type(const struct extent_buffer *eb,
95 				     struct btrfs_extent_inline_ref *iref,
96 				     enum btrfs_inline_ref_type is_data);
97 u64 hash_extent_data_ref(u64 root_objectid, u64 owner, u64 offset);
98 
99 int btrfs_add_excluded_extent(struct btrfs_fs_info *fs_info,
100 			      u64 start, u64 num_bytes);
101 void btrfs_free_excluded_extents(struct btrfs_block_group *cache);
102 int btrfs_run_delayed_refs(struct btrfs_trans_handle *trans, unsigned long count);
103 void btrfs_cleanup_ref_head_accounting(struct btrfs_fs_info *fs_info,
104 				  struct btrfs_delayed_ref_root *delayed_refs,
105 				  struct btrfs_delayed_ref_head *head);
106 int btrfs_lookup_data_extent(struct btrfs_fs_info *fs_info, u64 start, u64 len);
107 int btrfs_lookup_extent_info(struct btrfs_trans_handle *trans,
108 			     struct btrfs_fs_info *fs_info, u64 bytenr,
109 			     u64 offset, int metadata, u64 *refs, u64 *flags);
110 int btrfs_pin_extent(struct btrfs_trans_handle *trans, u64 bytenr, u64 num,
111 		     int reserved);
112 int btrfs_pin_extent_for_log_replay(struct btrfs_trans_handle *trans,
113 				    u64 bytenr, u64 num_bytes);
114 int btrfs_exclude_logged_extents(struct extent_buffer *eb);
115 int btrfs_cross_ref_exist(struct btrfs_root *root,
116 			  u64 objectid, u64 offset, u64 bytenr, bool strict,
117 			  struct btrfs_path *path);
118 struct extent_buffer *btrfs_alloc_tree_block(struct btrfs_trans_handle *trans,
119 					     struct btrfs_root *root,
120 					     u64 parent, u64 root_objectid,
121 					     const struct btrfs_disk_key *key,
122 					     int level, u64 hint,
123 					     u64 empty_size,
124 					     enum btrfs_lock_nesting nest);
125 void btrfs_free_tree_block(struct btrfs_trans_handle *trans,
126 			   u64 root_id,
127 			   struct extent_buffer *buf,
128 			   u64 parent, int last_ref);
129 int btrfs_alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
130 				     struct btrfs_root *root, u64 owner,
131 				     u64 offset, u64 ram_bytes,
132 				     struct btrfs_key *ins);
133 int btrfs_alloc_logged_file_extent(struct btrfs_trans_handle *trans,
134 				   u64 root_objectid, u64 owner, u64 offset,
135 				   struct btrfs_key *ins);
136 int btrfs_reserve_extent(struct btrfs_root *root, u64 ram_bytes, u64 num_bytes,
137 			 u64 min_alloc_size, u64 empty_size, u64 hint_byte,
138 			 struct btrfs_key *ins, int is_data, int delalloc);
139 int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
140 		  struct extent_buffer *buf, int full_backref);
141 int btrfs_dec_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
142 		  struct extent_buffer *buf, int full_backref);
143 int btrfs_set_disk_extent_flags(struct btrfs_trans_handle *trans,
144 				struct extent_buffer *eb, u64 flags, int level);
145 int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_ref *ref);
146 
147 int btrfs_free_reserved_extent(struct btrfs_fs_info *fs_info,
148 			       u64 start, u64 len, int delalloc);
149 int btrfs_pin_reserved_extent(struct btrfs_trans_handle *trans, u64 start, u64 len);
150 int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans);
151 int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans, struct btrfs_ref *generic_ref);
152 int __must_check btrfs_drop_snapshot(struct btrfs_root *root, int update_ref,
153 				     int for_reloc);
154 int btrfs_drop_subtree(struct btrfs_trans_handle *trans,
155 			struct btrfs_root *root,
156 			struct extent_buffer *node,
157 			struct extent_buffer *parent);
158 
159 #endif
160