xref: /openbmc/linux/fs/btrfs/zoned.h (revision 569820be)
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 "messages.h"
9 #include "volumes.h"
10 #include "disk-io.h"
11 #include "block-group.h"
12 #include "btrfs_inode.h"
13 
14 #define BTRFS_DEFAULT_RECLAIM_THRESH           			(75)
15 
16 struct btrfs_zoned_device_info {
17 	/*
18 	 * Number of zones, zone size and types of zones if bdev is a
19 	 * zoned block device.
20 	 */
21 	u64 zone_size;
22 	u8  zone_size_shift;
23 	u32 nr_zones;
24 	unsigned int max_active_zones;
25 	atomic_t active_zones_left;
26 	unsigned long *seq_zones;
27 	unsigned long *empty_zones;
28 	unsigned long *active_zones;
29 	struct blk_zone *zone_cache;
30 	struct blk_zone sb_zones[2 * BTRFS_SUPER_MIRROR_MAX];
31 };
32 
33 void btrfs_finish_ordered_zoned(struct btrfs_ordered_extent *ordered);
34 
35 #ifdef CONFIG_BLK_DEV_ZONED
36 int btrfs_get_dev_zone(struct btrfs_device *device, u64 pos,
37 		       struct blk_zone *zone);
38 int btrfs_get_dev_zone_info_all_devices(struct btrfs_fs_info *fs_info);
39 int btrfs_get_dev_zone_info(struct btrfs_device *device, bool populate_cache);
40 void btrfs_destroy_dev_zone_info(struct btrfs_device *device);
41 struct btrfs_zoned_device_info *btrfs_clone_dev_zone_info(struct btrfs_device *orig_dev);
42 int btrfs_check_zoned_mode(struct btrfs_fs_info *fs_info);
43 int btrfs_check_mountopts_zoned(struct btrfs_fs_info *info);
44 int btrfs_sb_log_location_bdev(struct block_device *bdev, int mirror, int rw,
45 			       u64 *bytenr_ret);
46 int btrfs_sb_log_location(struct btrfs_device *device, int mirror, int rw,
47 			  u64 *bytenr_ret);
48 int btrfs_advance_sb_log(struct btrfs_device *device, int mirror);
49 int btrfs_reset_sb_log_zones(struct block_device *bdev, int mirror);
50 u64 btrfs_find_allocatable_zones(struct btrfs_device *device, u64 hole_start,
51 				 u64 hole_end, u64 num_bytes);
52 int btrfs_reset_device_zone(struct btrfs_device *device, u64 physical,
53 			    u64 length, u64 *bytes);
54 int btrfs_ensure_empty_zones(struct btrfs_device *device, u64 start, u64 size);
55 int btrfs_load_block_group_zone_info(struct btrfs_block_group *cache, bool new);
56 void btrfs_calc_zone_unusable(struct btrfs_block_group *cache);
57 void btrfs_redirty_list_add(struct btrfs_transaction *trans,
58 			    struct extent_buffer *eb);
59 bool btrfs_use_zone_append(struct btrfs_bio *bbio);
60 void btrfs_record_physical_zoned(struct btrfs_bio *bbio);
61 bool btrfs_check_meta_write_pointer(struct btrfs_fs_info *fs_info,
62 				    struct extent_buffer *eb,
63 				    struct btrfs_block_group **cache_ret);
64 void btrfs_revert_meta_write_pointer(struct btrfs_block_group *cache,
65 				     struct extent_buffer *eb);
66 int btrfs_zoned_issue_zeroout(struct btrfs_device *device, u64 physical, u64 length);
67 int btrfs_sync_zone_write_pointer(struct btrfs_device *tgt_dev, u64 logical,
68 				  u64 physical_start, u64 physical_pos);
69 bool btrfs_zone_activate(struct btrfs_block_group *block_group);
70 int btrfs_zone_finish(struct btrfs_block_group *block_group);
71 bool btrfs_can_activate_zone(struct btrfs_fs_devices *fs_devices, u64 flags);
72 void btrfs_zone_finish_endio(struct btrfs_fs_info *fs_info, u64 logical,
73 			     u64 length);
74 void btrfs_schedule_zone_finish_bg(struct btrfs_block_group *bg,
75 				   struct extent_buffer *eb);
76 void btrfs_clear_data_reloc_bg(struct btrfs_block_group *bg);
77 void btrfs_free_zone_cache(struct btrfs_fs_info *fs_info);
78 bool btrfs_zoned_should_reclaim(struct btrfs_fs_info *fs_info);
79 void btrfs_zoned_release_data_reloc_bg(struct btrfs_fs_info *fs_info, u64 logical,
80 				       u64 length);
81 int btrfs_zone_finish_one_bg(struct btrfs_fs_info *fs_info);
82 int btrfs_zoned_activate_one_bg(struct btrfs_fs_info *fs_info,
83 				struct btrfs_space_info *space_info, bool do_finish);
84 #else /* CONFIG_BLK_DEV_ZONED */
85 static inline int btrfs_get_dev_zone(struct btrfs_device *device, u64 pos,
86 				     struct blk_zone *zone)
87 {
88 	return 0;
89 }
90 
91 static inline int btrfs_get_dev_zone_info_all_devices(struct btrfs_fs_info *fs_info)
92 {
93 	return 0;
94 }
95 
96 static inline int btrfs_get_dev_zone_info(struct btrfs_device *device,
97 					  bool populate_cache)
98 {
99 	return 0;
100 }
101 
102 static inline void btrfs_destroy_dev_zone_info(struct btrfs_device *device) { }
103 
104 /*
105  * In case the kernel is compiled without CONFIG_BLK_DEV_ZONED we'll never call
106  * into btrfs_clone_dev_zone_info() so it's safe to return NULL here.
107  */
108 static inline struct btrfs_zoned_device_info *btrfs_clone_dev_zone_info(
109 						 struct btrfs_device *orig_dev)
110 {
111 	return NULL;
112 }
113 
114 static inline int btrfs_check_zoned_mode(const struct btrfs_fs_info *fs_info)
115 {
116 	if (!btrfs_is_zoned(fs_info))
117 		return 0;
118 
119 	btrfs_err(fs_info, "zoned block devices support is not enabled");
120 	return -EOPNOTSUPP;
121 }
122 
123 static inline int btrfs_check_mountopts_zoned(struct btrfs_fs_info *info)
124 {
125 	return 0;
126 }
127 
128 static inline int btrfs_sb_log_location_bdev(struct block_device *bdev,
129 					     int mirror, int rw, u64 *bytenr_ret)
130 {
131 	*bytenr_ret = btrfs_sb_offset(mirror);
132 	return 0;
133 }
134 
135 static inline int btrfs_sb_log_location(struct btrfs_device *device, int mirror,
136 					int rw, u64 *bytenr_ret)
137 {
138 	*bytenr_ret = btrfs_sb_offset(mirror);
139 	return 0;
140 }
141 
142 static inline int btrfs_advance_sb_log(struct btrfs_device *device, int mirror)
143 {
144 	return 0;
145 }
146 
147 static inline int btrfs_reset_sb_log_zones(struct block_device *bdev, int mirror)
148 {
149 	return 0;
150 }
151 
152 static inline u64 btrfs_find_allocatable_zones(struct btrfs_device *device,
153 					       u64 hole_start, u64 hole_end,
154 					       u64 num_bytes)
155 {
156 	return hole_start;
157 }
158 
159 static inline int btrfs_reset_device_zone(struct btrfs_device *device,
160 					  u64 physical, u64 length, u64 *bytes)
161 {
162 	*bytes = 0;
163 	return 0;
164 }
165 
166 static inline int btrfs_ensure_empty_zones(struct btrfs_device *device,
167 					   u64 start, u64 size)
168 {
169 	return 0;
170 }
171 
172 static inline int btrfs_load_block_group_zone_info(
173 		struct btrfs_block_group *cache, bool new)
174 {
175 	return 0;
176 }
177 
178 static inline void btrfs_calc_zone_unusable(struct btrfs_block_group *cache) { }
179 
180 static inline void btrfs_redirty_list_add(struct btrfs_transaction *trans,
181 					  struct extent_buffer *eb) { }
182 
183 static inline bool btrfs_use_zone_append(struct btrfs_bio *bbio)
184 {
185 	return false;
186 }
187 
188 static inline void btrfs_record_physical_zoned(struct btrfs_bio *bbio)
189 {
190 }
191 
192 static inline bool btrfs_check_meta_write_pointer(struct btrfs_fs_info *fs_info,
193 			       struct extent_buffer *eb,
194 			       struct btrfs_block_group **cache_ret)
195 {
196 	return true;
197 }
198 
199 static inline void btrfs_revert_meta_write_pointer(
200 						struct btrfs_block_group *cache,
201 						struct extent_buffer *eb)
202 {
203 }
204 
205 static inline int btrfs_zoned_issue_zeroout(struct btrfs_device *device,
206 					    u64 physical, u64 length)
207 {
208 	return -EOPNOTSUPP;
209 }
210 
211 static inline int btrfs_sync_zone_write_pointer(struct btrfs_device *tgt_dev,
212 						u64 logical, u64 physical_start,
213 						u64 physical_pos)
214 {
215 	return -EOPNOTSUPP;
216 }
217 
218 static inline bool btrfs_zone_activate(struct btrfs_block_group *block_group)
219 {
220 	return true;
221 }
222 
223 static inline int btrfs_zone_finish(struct btrfs_block_group *block_group)
224 {
225 	return 0;
226 }
227 
228 static inline bool btrfs_can_activate_zone(struct btrfs_fs_devices *fs_devices,
229 					   u64 flags)
230 {
231 	return true;
232 }
233 
234 static inline void btrfs_zone_finish_endio(struct btrfs_fs_info *fs_info,
235 					   u64 logical, u64 length) { }
236 
237 static inline void btrfs_schedule_zone_finish_bg(struct btrfs_block_group *bg,
238 						 struct extent_buffer *eb) { }
239 
240 static inline void btrfs_clear_data_reloc_bg(struct btrfs_block_group *bg) { }
241 
242 static inline void btrfs_free_zone_cache(struct btrfs_fs_info *fs_info) { }
243 
244 static inline bool btrfs_zoned_should_reclaim(struct btrfs_fs_info *fs_info)
245 {
246 	return false;
247 }
248 
249 static inline void btrfs_zoned_release_data_reloc_bg(struct btrfs_fs_info *fs_info,
250 						     u64 logical, u64 length) { }
251 
252 static inline int btrfs_zone_finish_one_bg(struct btrfs_fs_info *fs_info)
253 {
254 	return 1;
255 }
256 
257 static inline int btrfs_zoned_activate_one_bg(struct btrfs_fs_info *fs_info,
258 					      struct btrfs_space_info *space_info,
259 					      bool do_finish)
260 {
261 	/* Consider all the block groups are active */
262 	return 0;
263 }
264 
265 #endif
266 
267 static inline bool btrfs_dev_is_sequential(struct btrfs_device *device, u64 pos)
268 {
269 	struct btrfs_zoned_device_info *zone_info = device->zone_info;
270 
271 	if (!zone_info)
272 		return false;
273 
274 	return test_bit(pos >> zone_info->zone_size_shift, zone_info->seq_zones);
275 }
276 
277 static inline bool btrfs_dev_is_empty_zone(struct btrfs_device *device, u64 pos)
278 {
279 	struct btrfs_zoned_device_info *zone_info = device->zone_info;
280 
281 	if (!zone_info)
282 		return true;
283 
284 	return test_bit(pos >> zone_info->zone_size_shift, zone_info->empty_zones);
285 }
286 
287 static inline void btrfs_dev_set_empty_zone_bit(struct btrfs_device *device,
288 						u64 pos, bool set)
289 {
290 	struct btrfs_zoned_device_info *zone_info = device->zone_info;
291 	unsigned int zno;
292 
293 	if (!zone_info)
294 		return;
295 
296 	zno = pos >> zone_info->zone_size_shift;
297 	if (set)
298 		set_bit(zno, zone_info->empty_zones);
299 	else
300 		clear_bit(zno, zone_info->empty_zones);
301 }
302 
303 static inline void btrfs_dev_set_zone_empty(struct btrfs_device *device, u64 pos)
304 {
305 	btrfs_dev_set_empty_zone_bit(device, pos, true);
306 }
307 
308 static inline void btrfs_dev_clear_zone_empty(struct btrfs_device *device, u64 pos)
309 {
310 	btrfs_dev_set_empty_zone_bit(device, pos, false);
311 }
312 
313 static inline bool btrfs_check_device_zone_type(const struct btrfs_fs_info *fs_info,
314 						struct block_device *bdev)
315 {
316 	if (btrfs_is_zoned(fs_info)) {
317 		/*
318 		 * We can allow a regular device on a zoned filesystem, because
319 		 * we will emulate the zoned capabilities.
320 		 */
321 		if (!bdev_is_zoned(bdev))
322 			return true;
323 
324 		return fs_info->zone_size ==
325 			(bdev_zone_sectors(bdev) << SECTOR_SHIFT);
326 	}
327 
328 	/* Do not allow Host Manged zoned device */
329 	return bdev_zoned_model(bdev) != BLK_ZONED_HM;
330 }
331 
332 static inline bool btrfs_check_super_location(struct btrfs_device *device, u64 pos)
333 {
334 	/*
335 	 * On a non-zoned device, any address is OK. On a zoned device,
336 	 * non-SEQUENTIAL WRITE REQUIRED zones are capable.
337 	 */
338 	return device->zone_info == NULL || !btrfs_dev_is_sequential(device, pos);
339 }
340 
341 static inline bool btrfs_can_zone_reset(struct btrfs_device *device,
342 					u64 physical, u64 length)
343 {
344 	u64 zone_size;
345 
346 	if (!btrfs_dev_is_sequential(device, physical))
347 		return false;
348 
349 	zone_size = device->zone_info->zone_size;
350 	if (!IS_ALIGNED(physical, zone_size) || !IS_ALIGNED(length, zone_size))
351 		return false;
352 
353 	return true;
354 }
355 
356 static inline void btrfs_zoned_meta_io_lock(struct btrfs_fs_info *fs_info)
357 {
358 	if (!btrfs_is_zoned(fs_info))
359 		return;
360 	mutex_lock(&fs_info->zoned_meta_io_lock);
361 }
362 
363 static inline void btrfs_zoned_meta_io_unlock(struct btrfs_fs_info *fs_info)
364 {
365 	if (!btrfs_is_zoned(fs_info))
366 		return;
367 	mutex_unlock(&fs_info->zoned_meta_io_lock);
368 }
369 
370 static inline void btrfs_clear_treelog_bg(struct btrfs_block_group *bg)
371 {
372 	struct btrfs_fs_info *fs_info = bg->fs_info;
373 
374 	if (!btrfs_is_zoned(fs_info))
375 		return;
376 
377 	spin_lock(&fs_info->treelog_bg_lock);
378 	if (fs_info->treelog_bg == bg->start)
379 		fs_info->treelog_bg = 0;
380 	spin_unlock(&fs_info->treelog_bg_lock);
381 }
382 
383 static inline void btrfs_zoned_data_reloc_lock(struct btrfs_inode *inode)
384 {
385 	struct btrfs_root *root = inode->root;
386 
387 	if (btrfs_is_data_reloc_root(root) && btrfs_is_zoned(root->fs_info))
388 		mutex_lock(&root->fs_info->zoned_data_reloc_io_lock);
389 }
390 
391 static inline void btrfs_zoned_data_reloc_unlock(struct btrfs_inode *inode)
392 {
393 	struct btrfs_root *root = inode->root;
394 
395 	if (btrfs_is_data_reloc_root(root) && btrfs_is_zoned(root->fs_info))
396 		mutex_unlock(&root->fs_info->zoned_data_reloc_io_lock);
397 }
398 
399 static inline bool btrfs_zoned_bg_is_full(const struct btrfs_block_group *bg)
400 {
401 	ASSERT(btrfs_is_zoned(bg->fs_info));
402 	return (bg->alloc_offset == bg->zone_capacity);
403 }
404 
405 #endif
406