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