1 /* SPDX-License-Identifier: GPL-2.0 */
6 #include <linux/types.h>
7 #include <linux/blkdev.h>
11 #include "block-group.h"
12 #include "btrfs_inode.h"
14 #define BTRFS_DEFAULT_RECLAIM_THRESH (75)
16 struct btrfs_zoned_device_info {
18 * Number of zones, zone size and types of zones if bdev is a
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];
33 void btrfs_finish_ordered_zoned(struct btrfs_ordered_extent *ordered);
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,
46 int btrfs_sb_log_location(struct btrfs_device *device, int mirror, int rw,
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,
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,
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)
91 static inline int btrfs_get_dev_zone_info_all_devices(struct btrfs_fs_info *fs_info)
96 static inline int btrfs_get_dev_zone_info(struct btrfs_device *device,
102 static inline void btrfs_destroy_dev_zone_info(struct btrfs_device *device) { }
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.
108 static inline struct btrfs_zoned_device_info *btrfs_clone_dev_zone_info(
109 struct btrfs_device *orig_dev)
114 static inline int btrfs_check_zoned_mode(const struct btrfs_fs_info *fs_info)
116 if (!btrfs_is_zoned(fs_info))
119 btrfs_err(fs_info, "zoned block devices support is not enabled");
123 static inline int btrfs_check_mountopts_zoned(struct btrfs_fs_info *info)
128 static inline int btrfs_sb_log_location_bdev(struct block_device *bdev,
129 int mirror, int rw, u64 *bytenr_ret)
131 *bytenr_ret = btrfs_sb_offset(mirror);
135 static inline int btrfs_sb_log_location(struct btrfs_device *device, int mirror,
136 int rw, u64 *bytenr_ret)
138 *bytenr_ret = btrfs_sb_offset(mirror);
142 static inline int btrfs_advance_sb_log(struct btrfs_device *device, int mirror)
147 static inline int btrfs_reset_sb_log_zones(struct block_device *bdev, int mirror)
152 static inline u64 btrfs_find_allocatable_zones(struct btrfs_device *device,
153 u64 hole_start, u64 hole_end,
159 static inline int btrfs_reset_device_zone(struct btrfs_device *device,
160 u64 physical, u64 length, u64 *bytes)
166 static inline int btrfs_ensure_empty_zones(struct btrfs_device *device,
172 static inline int btrfs_load_block_group_zone_info(
173 struct btrfs_block_group *cache, bool new)
178 static inline void btrfs_calc_zone_unusable(struct btrfs_block_group *cache) { }
180 static inline void btrfs_redirty_list_add(struct btrfs_transaction *trans,
181 struct extent_buffer *eb) { }
183 static inline bool btrfs_use_zone_append(struct btrfs_bio *bbio)
188 static inline void btrfs_record_physical_zoned(struct btrfs_bio *bbio)
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)
199 static inline void btrfs_revert_meta_write_pointer(
200 struct btrfs_block_group *cache,
201 struct extent_buffer *eb)
205 static inline int btrfs_zoned_issue_zeroout(struct btrfs_device *device,
206 u64 physical, u64 length)
211 static inline int btrfs_sync_zone_write_pointer(struct btrfs_device *tgt_dev,
212 u64 logical, u64 physical_start,
218 static inline bool btrfs_zone_activate(struct btrfs_block_group *block_group)
223 static inline int btrfs_zone_finish(struct btrfs_block_group *block_group)
228 static inline bool btrfs_can_activate_zone(struct btrfs_fs_devices *fs_devices,
234 static inline void btrfs_zone_finish_endio(struct btrfs_fs_info *fs_info,
235 u64 logical, u64 length) { }
237 static inline void btrfs_schedule_zone_finish_bg(struct btrfs_block_group *bg,
238 struct extent_buffer *eb) { }
240 static inline void btrfs_clear_data_reloc_bg(struct btrfs_block_group *bg) { }
242 static inline void btrfs_free_zone_cache(struct btrfs_fs_info *fs_info) { }
244 static inline bool btrfs_zoned_should_reclaim(struct btrfs_fs_info *fs_info)
249 static inline void btrfs_zoned_release_data_reloc_bg(struct btrfs_fs_info *fs_info,
250 u64 logical, u64 length) { }
252 static inline int btrfs_zone_finish_one_bg(struct btrfs_fs_info *fs_info)
257 static inline int btrfs_zoned_activate_one_bg(struct btrfs_fs_info *fs_info,
258 struct btrfs_space_info *space_info,
261 /* Consider all the block groups are active */
267 static inline bool btrfs_dev_is_sequential(struct btrfs_device *device, u64 pos)
269 struct btrfs_zoned_device_info *zone_info = device->zone_info;
274 return test_bit(pos >> zone_info->zone_size_shift, zone_info->seq_zones);
277 static inline bool btrfs_dev_is_empty_zone(struct btrfs_device *device, u64 pos)
279 struct btrfs_zoned_device_info *zone_info = device->zone_info;
284 return test_bit(pos >> zone_info->zone_size_shift, zone_info->empty_zones);
287 static inline void btrfs_dev_set_empty_zone_bit(struct btrfs_device *device,
290 struct btrfs_zoned_device_info *zone_info = device->zone_info;
296 zno = pos >> zone_info->zone_size_shift;
298 set_bit(zno, zone_info->empty_zones);
300 clear_bit(zno, zone_info->empty_zones);
303 static inline void btrfs_dev_set_zone_empty(struct btrfs_device *device, u64 pos)
305 btrfs_dev_set_empty_zone_bit(device, pos, true);
308 static inline void btrfs_dev_clear_zone_empty(struct btrfs_device *device, u64 pos)
310 btrfs_dev_set_empty_zone_bit(device, pos, false);
313 static inline bool btrfs_check_device_zone_type(const struct btrfs_fs_info *fs_info,
314 struct block_device *bdev)
316 if (btrfs_is_zoned(fs_info)) {
318 * We can allow a regular device on a zoned filesystem, because
319 * we will emulate the zoned capabilities.
321 if (!bdev_is_zoned(bdev))
324 return fs_info->zone_size ==
325 (bdev_zone_sectors(bdev) << SECTOR_SHIFT);
328 /* Do not allow Host Manged zoned device */
329 return bdev_zoned_model(bdev) != BLK_ZONED_HM;
332 static inline bool btrfs_check_super_location(struct btrfs_device *device, u64 pos)
335 * On a non-zoned device, any address is OK. On a zoned device,
336 * non-SEQUENTIAL WRITE REQUIRED zones are capable.
338 return device->zone_info == NULL || !btrfs_dev_is_sequential(device, pos);
341 static inline bool btrfs_can_zone_reset(struct btrfs_device *device,
342 u64 physical, u64 length)
346 if (!btrfs_dev_is_sequential(device, physical))
349 zone_size = device->zone_info->zone_size;
350 if (!IS_ALIGNED(physical, zone_size) || !IS_ALIGNED(length, zone_size))
356 static inline void btrfs_zoned_meta_io_lock(struct btrfs_fs_info *fs_info)
358 if (!btrfs_is_zoned(fs_info))
360 mutex_lock(&fs_info->zoned_meta_io_lock);
363 static inline void btrfs_zoned_meta_io_unlock(struct btrfs_fs_info *fs_info)
365 if (!btrfs_is_zoned(fs_info))
367 mutex_unlock(&fs_info->zoned_meta_io_lock);
370 static inline void btrfs_clear_treelog_bg(struct btrfs_block_group *bg)
372 struct btrfs_fs_info *fs_info = bg->fs_info;
374 if (!btrfs_is_zoned(fs_info))
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);
383 static inline void btrfs_zoned_data_reloc_lock(struct btrfs_inode *inode)
385 struct btrfs_root *root = inode->root;
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);
391 static inline void btrfs_zoned_data_reloc_unlock(struct btrfs_inode *inode)
393 struct btrfs_root *root = inode->root;
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);
399 static inline bool btrfs_zoned_bg_is_full(const struct btrfs_block_group *bg)
401 ASSERT(btrfs_is_zoned(bg->fs_info));
402 return (bg->alloc_offset == bg->zone_capacity);