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