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