xref: /openbmc/linux/fs/btrfs/zoned.h (revision 35267cea)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 
3 #ifndef BTRFS_ZONED_H
4 #define BTRFS_ZONED_H
5 
6 #include <linux/types.h>
7 #include <linux/blkdev.h>
8 #include "volumes.h"
9 #include "disk-io.h"
10 #include "block-group.h"
11 
12 /*
13  * Block groups with more than this value (percents) of unusable space will be
14  * scheduled for background reclaim.
15  */
16 #define BTRFS_DEFAULT_RECLAIM_THRESH		75
17 
18 struct btrfs_zoned_device_info {
19 	/*
20 	 * Number of zones, zone size and types of zones if bdev is a
21 	 * zoned block device.
22 	 */
23 	u64 zone_size;
24 	u8  zone_size_shift;
25 	u64 max_zone_append_size;
26 	u32 nr_zones;
27 	unsigned long *seq_zones;
28 	unsigned long *empty_zones;
29 	struct blk_zone sb_zones[2 * BTRFS_SUPER_MIRROR_MAX];
30 };
31 
32 #ifdef CONFIG_BLK_DEV_ZONED
33 int btrfs_get_dev_zone(struct btrfs_device *device, u64 pos,
34 		       struct blk_zone *zone);
35 int btrfs_get_dev_zone_info_all_devices(struct btrfs_fs_info *fs_info);
36 int btrfs_get_dev_zone_info(struct btrfs_device *device);
37 void btrfs_destroy_dev_zone_info(struct btrfs_device *device);
38 int btrfs_check_zoned_mode(struct btrfs_fs_info *fs_info);
39 int btrfs_check_mountopts_zoned(struct btrfs_fs_info *info);
40 int btrfs_sb_log_location_bdev(struct block_device *bdev, int mirror, int rw,
41 			       u64 *bytenr_ret);
42 int btrfs_sb_log_location(struct btrfs_device *device, int mirror, int rw,
43 			  u64 *bytenr_ret);
44 void btrfs_advance_sb_log(struct btrfs_device *device, int mirror);
45 int btrfs_reset_sb_log_zones(struct block_device *bdev, int mirror);
46 u64 btrfs_find_allocatable_zones(struct btrfs_device *device, u64 hole_start,
47 				 u64 hole_end, u64 num_bytes);
48 int btrfs_reset_device_zone(struct btrfs_device *device, u64 physical,
49 			    u64 length, u64 *bytes);
50 int btrfs_ensure_empty_zones(struct btrfs_device *device, u64 start, u64 size);
51 int btrfs_load_block_group_zone_info(struct btrfs_block_group *cache, bool new);
52 void btrfs_calc_zone_unusable(struct btrfs_block_group *cache);
53 void btrfs_redirty_list_add(struct btrfs_transaction *trans,
54 			    struct extent_buffer *eb);
55 void btrfs_free_redirty_list(struct btrfs_transaction *trans);
56 bool btrfs_use_zone_append(struct btrfs_inode *inode, u64 start);
57 void btrfs_record_physical_zoned(struct inode *inode, u64 file_offset,
58 				 struct bio *bio);
59 void btrfs_rewrite_logical_zoned(struct btrfs_ordered_extent *ordered);
60 bool btrfs_check_meta_write_pointer(struct btrfs_fs_info *fs_info,
61 				    struct extent_buffer *eb,
62 				    struct btrfs_block_group **cache_ret);
63 void btrfs_revert_meta_write_pointer(struct btrfs_block_group *cache,
64 				     struct extent_buffer *eb);
65 int btrfs_zoned_issue_zeroout(struct btrfs_device *device, u64 physical, u64 length);
66 int btrfs_sync_zone_write_pointer(struct btrfs_device *tgt_dev, u64 logical,
67 				  u64 physical_start, u64 physical_pos);
68 struct btrfs_device *btrfs_zoned_get_device(struct btrfs_fs_info *fs_info,
69 					    u64 logical, u64 length);
70 #else /* CONFIG_BLK_DEV_ZONED */
71 static inline int btrfs_get_dev_zone(struct btrfs_device *device, u64 pos,
72 				     struct blk_zone *zone)
73 {
74 	return 0;
75 }
76 
77 static inline int btrfs_get_dev_zone_info_all_devices(struct btrfs_fs_info *fs_info)
78 {
79 	return 0;
80 }
81 
82 static inline int btrfs_get_dev_zone_info(struct btrfs_device *device)
83 {
84 	return 0;
85 }
86 
87 static inline void btrfs_destroy_dev_zone_info(struct btrfs_device *device) { }
88 
89 static inline int btrfs_check_zoned_mode(const struct btrfs_fs_info *fs_info)
90 {
91 	if (!btrfs_is_zoned(fs_info))
92 		return 0;
93 
94 	btrfs_err(fs_info, "zoned block devices support is not enabled");
95 	return -EOPNOTSUPP;
96 }
97 
98 static inline int btrfs_check_mountopts_zoned(struct btrfs_fs_info *info)
99 {
100 	return 0;
101 }
102 
103 static inline int btrfs_sb_log_location_bdev(struct block_device *bdev,
104 					     int mirror, int rw, u64 *bytenr_ret)
105 {
106 	*bytenr_ret = btrfs_sb_offset(mirror);
107 	return 0;
108 }
109 
110 static inline int btrfs_sb_log_location(struct btrfs_device *device, int mirror,
111 					int rw, u64 *bytenr_ret)
112 {
113 	*bytenr_ret = btrfs_sb_offset(mirror);
114 	return 0;
115 }
116 
117 static inline void btrfs_advance_sb_log(struct btrfs_device *device, int mirror)
118 { }
119 
120 static inline int btrfs_reset_sb_log_zones(struct block_device *bdev, int mirror)
121 {
122 	return 0;
123 }
124 
125 static inline u64 btrfs_find_allocatable_zones(struct btrfs_device *device,
126 					       u64 hole_start, u64 hole_end,
127 					       u64 num_bytes)
128 {
129 	return hole_start;
130 }
131 
132 static inline int btrfs_reset_device_zone(struct btrfs_device *device,
133 					  u64 physical, u64 length, u64 *bytes)
134 {
135 	*bytes = 0;
136 	return 0;
137 }
138 
139 static inline int btrfs_ensure_empty_zones(struct btrfs_device *device,
140 					   u64 start, u64 size)
141 {
142 	return 0;
143 }
144 
145 static inline int btrfs_load_block_group_zone_info(
146 		struct btrfs_block_group *cache, bool new)
147 {
148 	return 0;
149 }
150 
151 static inline void btrfs_calc_zone_unusable(struct btrfs_block_group *cache) { }
152 
153 static inline void btrfs_redirty_list_add(struct btrfs_transaction *trans,
154 					  struct extent_buffer *eb) { }
155 static inline void btrfs_free_redirty_list(struct btrfs_transaction *trans) { }
156 
157 static inline bool btrfs_use_zone_append(struct btrfs_inode *inode, u64 start)
158 {
159 	return false;
160 }
161 
162 static inline void btrfs_record_physical_zoned(struct inode *inode,
163 					       u64 file_offset, struct bio *bio)
164 {
165 }
166 
167 static inline void btrfs_rewrite_logical_zoned(
168 				struct btrfs_ordered_extent *ordered) { }
169 
170 static inline bool btrfs_check_meta_write_pointer(struct btrfs_fs_info *fs_info,
171 			       struct extent_buffer *eb,
172 			       struct btrfs_block_group **cache_ret)
173 {
174 	return true;
175 }
176 
177 static inline void btrfs_revert_meta_write_pointer(
178 						struct btrfs_block_group *cache,
179 						struct extent_buffer *eb)
180 {
181 }
182 
183 static inline int btrfs_zoned_issue_zeroout(struct btrfs_device *device,
184 					    u64 physical, u64 length)
185 {
186 	return -EOPNOTSUPP;
187 }
188 
189 static inline int btrfs_sync_zone_write_pointer(struct btrfs_device *tgt_dev,
190 						u64 logical, u64 physical_start,
191 						u64 physical_pos)
192 {
193 	return -EOPNOTSUPP;
194 }
195 
196 static inline struct btrfs_device *btrfs_zoned_get_device(
197 						  struct btrfs_fs_info *fs_info,
198 						  u64 logical, u64 length)
199 {
200 	return ERR_PTR(-EOPNOTSUPP);
201 }
202 
203 #endif
204 
205 static inline bool btrfs_dev_is_sequential(struct btrfs_device *device, u64 pos)
206 {
207 	struct btrfs_zoned_device_info *zone_info = device->zone_info;
208 
209 	if (!zone_info)
210 		return false;
211 
212 	return test_bit(pos >> zone_info->zone_size_shift, zone_info->seq_zones);
213 }
214 
215 static inline bool btrfs_dev_is_empty_zone(struct btrfs_device *device, u64 pos)
216 {
217 	struct btrfs_zoned_device_info *zone_info = device->zone_info;
218 
219 	if (!zone_info)
220 		return true;
221 
222 	return test_bit(pos >> zone_info->zone_size_shift, zone_info->empty_zones);
223 }
224 
225 static inline void btrfs_dev_set_empty_zone_bit(struct btrfs_device *device,
226 						u64 pos, bool set)
227 {
228 	struct btrfs_zoned_device_info *zone_info = device->zone_info;
229 	unsigned int zno;
230 
231 	if (!zone_info)
232 		return;
233 
234 	zno = pos >> zone_info->zone_size_shift;
235 	if (set)
236 		set_bit(zno, zone_info->empty_zones);
237 	else
238 		clear_bit(zno, zone_info->empty_zones);
239 }
240 
241 static inline void btrfs_dev_set_zone_empty(struct btrfs_device *device, u64 pos)
242 {
243 	btrfs_dev_set_empty_zone_bit(device, pos, true);
244 }
245 
246 static inline void btrfs_dev_clear_zone_empty(struct btrfs_device *device, u64 pos)
247 {
248 	btrfs_dev_set_empty_zone_bit(device, pos, false);
249 }
250 
251 static inline bool btrfs_check_device_zone_type(const struct btrfs_fs_info *fs_info,
252 						struct block_device *bdev)
253 {
254 	if (btrfs_is_zoned(fs_info)) {
255 		/*
256 		 * We can allow a regular device on a zoned filesystem, because
257 		 * we will emulate the zoned capabilities.
258 		 */
259 		if (!bdev_is_zoned(bdev))
260 			return true;
261 
262 		return fs_info->zone_size ==
263 			(bdev_zone_sectors(bdev) << SECTOR_SHIFT);
264 	}
265 
266 	/* Do not allow Host Manged zoned device */
267 	return bdev_zoned_model(bdev) != BLK_ZONED_HM;
268 }
269 
270 static inline bool btrfs_check_super_location(struct btrfs_device *device, u64 pos)
271 {
272 	/*
273 	 * On a non-zoned device, any address is OK. On a zoned device,
274 	 * non-SEQUENTIAL WRITE REQUIRED zones are capable.
275 	 */
276 	return device->zone_info == NULL || !btrfs_dev_is_sequential(device, pos);
277 }
278 
279 static inline bool btrfs_can_zone_reset(struct btrfs_device *device,
280 					u64 physical, u64 length)
281 {
282 	u64 zone_size;
283 
284 	if (!btrfs_dev_is_sequential(device, physical))
285 		return false;
286 
287 	zone_size = device->zone_info->zone_size;
288 	if (!IS_ALIGNED(physical, zone_size) || !IS_ALIGNED(length, zone_size))
289 		return false;
290 
291 	return true;
292 }
293 
294 static inline void btrfs_zoned_meta_io_lock(struct btrfs_fs_info *fs_info)
295 {
296 	if (!btrfs_is_zoned(fs_info))
297 		return;
298 	mutex_lock(&fs_info->zoned_meta_io_lock);
299 }
300 
301 static inline void btrfs_zoned_meta_io_unlock(struct btrfs_fs_info *fs_info)
302 {
303 	if (!btrfs_is_zoned(fs_info))
304 		return;
305 	mutex_unlock(&fs_info->zoned_meta_io_lock);
306 }
307 
308 static inline void btrfs_clear_treelog_bg(struct btrfs_block_group *bg)
309 {
310 	struct btrfs_fs_info *fs_info = bg->fs_info;
311 
312 	if (!btrfs_is_zoned(fs_info))
313 		return;
314 
315 	spin_lock(&fs_info->treelog_bg_lock);
316 	if (fs_info->treelog_bg == bg->start)
317 		fs_info->treelog_bg = 0;
318 	spin_unlock(&fs_info->treelog_bg_lock);
319 }
320 
321 #endif
322