xref: /openbmc/linux/fs/btrfs/zoned.h (revision 5fa2481c)
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 	u32 nr_zones;
23 	unsigned int max_active_zones;
24 	atomic_t active_zones_left;
25 	unsigned long *seq_zones;
26 	unsigned long *empty_zones;
27 	unsigned long *active_zones;
28 	struct blk_zone *zone_cache;
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, bool populate_cache);
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 int 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 bool btrfs_zone_activate(struct btrfs_block_group *block_group);
71 int btrfs_zone_finish(struct btrfs_block_group *block_group);
72 bool btrfs_can_activate_zone(struct btrfs_fs_devices *fs_devices, u64 flags);
73 void btrfs_zone_finish_endio(struct btrfs_fs_info *fs_info, u64 logical,
74 			     u64 length);
75 void btrfs_schedule_zone_finish_bg(struct btrfs_block_group *bg,
76 				   struct extent_buffer *eb);
77 void btrfs_clear_data_reloc_bg(struct btrfs_block_group *bg);
78 void btrfs_free_zone_cache(struct btrfs_fs_info *fs_info);
79 bool btrfs_zoned_should_reclaim(struct btrfs_fs_info *fs_info);
80 void btrfs_zoned_release_data_reloc_bg(struct btrfs_fs_info *fs_info, u64 logical,
81 				       u64 length);
82 #else /* CONFIG_BLK_DEV_ZONED */
83 static inline int btrfs_get_dev_zone(struct btrfs_device *device, u64 pos,
84 				     struct blk_zone *zone)
85 {
86 	return 0;
87 }
88 
89 static inline int btrfs_get_dev_zone_info_all_devices(struct btrfs_fs_info *fs_info)
90 {
91 	return 0;
92 }
93 
94 static inline int btrfs_get_dev_zone_info(struct btrfs_device *device,
95 					  bool populate_cache)
96 {
97 	return 0;
98 }
99 
100 static inline void btrfs_destroy_dev_zone_info(struct btrfs_device *device) { }
101 
102 static inline int btrfs_check_zoned_mode(const struct btrfs_fs_info *fs_info)
103 {
104 	if (!btrfs_is_zoned(fs_info))
105 		return 0;
106 
107 	btrfs_err(fs_info, "zoned block devices support is not enabled");
108 	return -EOPNOTSUPP;
109 }
110 
111 static inline int btrfs_check_mountopts_zoned(struct btrfs_fs_info *info)
112 {
113 	return 0;
114 }
115 
116 static inline int btrfs_sb_log_location_bdev(struct block_device *bdev,
117 					     int mirror, int rw, u64 *bytenr_ret)
118 {
119 	*bytenr_ret = btrfs_sb_offset(mirror);
120 	return 0;
121 }
122 
123 static inline int btrfs_sb_log_location(struct btrfs_device *device, int mirror,
124 					int rw, u64 *bytenr_ret)
125 {
126 	*bytenr_ret = btrfs_sb_offset(mirror);
127 	return 0;
128 }
129 
130 static inline int btrfs_advance_sb_log(struct btrfs_device *device, int mirror)
131 {
132 	return 0;
133 }
134 
135 static inline int btrfs_reset_sb_log_zones(struct block_device *bdev, int mirror)
136 {
137 	return 0;
138 }
139 
140 static inline u64 btrfs_find_allocatable_zones(struct btrfs_device *device,
141 					       u64 hole_start, u64 hole_end,
142 					       u64 num_bytes)
143 {
144 	return hole_start;
145 }
146 
147 static inline int btrfs_reset_device_zone(struct btrfs_device *device,
148 					  u64 physical, u64 length, u64 *bytes)
149 {
150 	*bytes = 0;
151 	return 0;
152 }
153 
154 static inline int btrfs_ensure_empty_zones(struct btrfs_device *device,
155 					   u64 start, u64 size)
156 {
157 	return 0;
158 }
159 
160 static inline int btrfs_load_block_group_zone_info(
161 		struct btrfs_block_group *cache, bool new)
162 {
163 	return 0;
164 }
165 
166 static inline void btrfs_calc_zone_unusable(struct btrfs_block_group *cache) { }
167 
168 static inline void btrfs_redirty_list_add(struct btrfs_transaction *trans,
169 					  struct extent_buffer *eb) { }
170 static inline void btrfs_free_redirty_list(struct btrfs_transaction *trans) { }
171 
172 static inline bool btrfs_use_zone_append(struct btrfs_inode *inode, u64 start)
173 {
174 	return false;
175 }
176 
177 static inline void btrfs_record_physical_zoned(struct inode *inode,
178 					       u64 file_offset, struct bio *bio)
179 {
180 }
181 
182 static inline void btrfs_rewrite_logical_zoned(
183 				struct btrfs_ordered_extent *ordered) { }
184 
185 static inline bool btrfs_check_meta_write_pointer(struct btrfs_fs_info *fs_info,
186 			       struct extent_buffer *eb,
187 			       struct btrfs_block_group **cache_ret)
188 {
189 	return true;
190 }
191 
192 static inline void btrfs_revert_meta_write_pointer(
193 						struct btrfs_block_group *cache,
194 						struct extent_buffer *eb)
195 {
196 }
197 
198 static inline int btrfs_zoned_issue_zeroout(struct btrfs_device *device,
199 					    u64 physical, u64 length)
200 {
201 	return -EOPNOTSUPP;
202 }
203 
204 static inline int btrfs_sync_zone_write_pointer(struct btrfs_device *tgt_dev,
205 						u64 logical, u64 physical_start,
206 						u64 physical_pos)
207 {
208 	return -EOPNOTSUPP;
209 }
210 
211 static inline struct btrfs_device *btrfs_zoned_get_device(
212 						  struct btrfs_fs_info *fs_info,
213 						  u64 logical, u64 length)
214 {
215 	return ERR_PTR(-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 #endif
252 
253 static inline bool btrfs_dev_is_sequential(struct btrfs_device *device, u64 pos)
254 {
255 	struct btrfs_zoned_device_info *zone_info = device->zone_info;
256 
257 	if (!zone_info)
258 		return false;
259 
260 	return test_bit(pos >> zone_info->zone_size_shift, zone_info->seq_zones);
261 }
262 
263 static inline bool btrfs_dev_is_empty_zone(struct btrfs_device *device, u64 pos)
264 {
265 	struct btrfs_zoned_device_info *zone_info = device->zone_info;
266 
267 	if (!zone_info)
268 		return true;
269 
270 	return test_bit(pos >> zone_info->zone_size_shift, zone_info->empty_zones);
271 }
272 
273 static inline void btrfs_dev_set_empty_zone_bit(struct btrfs_device *device,
274 						u64 pos, bool set)
275 {
276 	struct btrfs_zoned_device_info *zone_info = device->zone_info;
277 	unsigned int zno;
278 
279 	if (!zone_info)
280 		return;
281 
282 	zno = pos >> zone_info->zone_size_shift;
283 	if (set)
284 		set_bit(zno, zone_info->empty_zones);
285 	else
286 		clear_bit(zno, zone_info->empty_zones);
287 }
288 
289 static inline void btrfs_dev_set_zone_empty(struct btrfs_device *device, u64 pos)
290 {
291 	btrfs_dev_set_empty_zone_bit(device, pos, true);
292 }
293 
294 static inline void btrfs_dev_clear_zone_empty(struct btrfs_device *device, u64 pos)
295 {
296 	btrfs_dev_set_empty_zone_bit(device, pos, false);
297 }
298 
299 static inline bool btrfs_check_device_zone_type(const struct btrfs_fs_info *fs_info,
300 						struct block_device *bdev)
301 {
302 	if (btrfs_is_zoned(fs_info)) {
303 		/*
304 		 * We can allow a regular device on a zoned filesystem, because
305 		 * we will emulate the zoned capabilities.
306 		 */
307 		if (!bdev_is_zoned(bdev))
308 			return true;
309 
310 		return fs_info->zone_size ==
311 			(bdev_zone_sectors(bdev) << SECTOR_SHIFT);
312 	}
313 
314 	/* Do not allow Host Manged zoned device */
315 	return bdev_zoned_model(bdev) != BLK_ZONED_HM;
316 }
317 
318 static inline bool btrfs_check_super_location(struct btrfs_device *device, u64 pos)
319 {
320 	/*
321 	 * On a non-zoned device, any address is OK. On a zoned device,
322 	 * non-SEQUENTIAL WRITE REQUIRED zones are capable.
323 	 */
324 	return device->zone_info == NULL || !btrfs_dev_is_sequential(device, pos);
325 }
326 
327 static inline bool btrfs_can_zone_reset(struct btrfs_device *device,
328 					u64 physical, u64 length)
329 {
330 	u64 zone_size;
331 
332 	if (!btrfs_dev_is_sequential(device, physical))
333 		return false;
334 
335 	zone_size = device->zone_info->zone_size;
336 	if (!IS_ALIGNED(physical, zone_size) || !IS_ALIGNED(length, zone_size))
337 		return false;
338 
339 	return true;
340 }
341 
342 static inline void btrfs_zoned_meta_io_lock(struct btrfs_fs_info *fs_info)
343 {
344 	if (!btrfs_is_zoned(fs_info))
345 		return;
346 	mutex_lock(&fs_info->zoned_meta_io_lock);
347 }
348 
349 static inline void btrfs_zoned_meta_io_unlock(struct btrfs_fs_info *fs_info)
350 {
351 	if (!btrfs_is_zoned(fs_info))
352 		return;
353 	mutex_unlock(&fs_info->zoned_meta_io_lock);
354 }
355 
356 static inline void btrfs_clear_treelog_bg(struct btrfs_block_group *bg)
357 {
358 	struct btrfs_fs_info *fs_info = bg->fs_info;
359 
360 	if (!btrfs_is_zoned(fs_info))
361 		return;
362 
363 	spin_lock(&fs_info->treelog_bg_lock);
364 	if (fs_info->treelog_bg == bg->start)
365 		fs_info->treelog_bg = 0;
366 	spin_unlock(&fs_info->treelog_bg_lock);
367 }
368 
369 static inline void btrfs_zoned_data_reloc_lock(struct btrfs_inode *inode)
370 {
371 	struct btrfs_root *root = inode->root;
372 
373 	if (btrfs_is_data_reloc_root(root) && btrfs_is_zoned(root->fs_info))
374 		mutex_lock(&root->fs_info->zoned_data_reloc_io_lock);
375 }
376 
377 static inline void btrfs_zoned_data_reloc_unlock(struct btrfs_inode *inode)
378 {
379 	struct btrfs_root *root = inode->root;
380 
381 	if (btrfs_is_data_reloc_root(root) && btrfs_is_zoned(root->fs_info))
382 		mutex_unlock(&root->fs_info->zoned_data_reloc_io_lock);
383 }
384 
385 static inline bool btrfs_zoned_bg_is_full(const struct btrfs_block_group *bg)
386 {
387 	ASSERT(btrfs_is_zoned(bg->fs_info));
388 	return (bg->alloc_offset == bg->zone_capacity);
389 }
390 
391 #endif
392