1 /* SPDX-License-Identifier: GPL-2.0 */
6 #include <linux/types.h>
7 #include <linux/blkdev.h>
10 #include "block-group.h"
12 struct btrfs_zoned_device_info {
14 * Number of zones, zone size and types of zones if bdev is a
19 u64 max_zone_append_size;
21 unsigned long *seq_zones;
22 unsigned long *empty_zones;
23 struct blk_zone sb_zones[2 * BTRFS_SUPER_MIRROR_MAX];
26 #ifdef CONFIG_BLK_DEV_ZONED
27 int btrfs_get_dev_zone(struct btrfs_device *device, u64 pos,
28 struct blk_zone *zone);
29 int btrfs_get_dev_zone_info_all_devices(struct btrfs_fs_info *fs_info);
30 int btrfs_get_dev_zone_info(struct btrfs_device *device);
31 void btrfs_destroy_dev_zone_info(struct btrfs_device *device);
32 int btrfs_check_zoned_mode(struct btrfs_fs_info *fs_info);
33 int btrfs_check_mountopts_zoned(struct btrfs_fs_info *info);
34 int btrfs_sb_log_location_bdev(struct block_device *bdev, int mirror, int rw,
36 int btrfs_sb_log_location(struct btrfs_device *device, int mirror, int rw,
38 void btrfs_advance_sb_log(struct btrfs_device *device, int mirror);
39 int btrfs_reset_sb_log_zones(struct block_device *bdev, int mirror);
40 u64 btrfs_find_allocatable_zones(struct btrfs_device *device, u64 hole_start,
41 u64 hole_end, u64 num_bytes);
42 int btrfs_reset_device_zone(struct btrfs_device *device, u64 physical,
43 u64 length, u64 *bytes);
44 int btrfs_ensure_empty_zones(struct btrfs_device *device, u64 start, u64 size);
45 int btrfs_load_block_group_zone_info(struct btrfs_block_group *cache, bool new);
46 void btrfs_calc_zone_unusable(struct btrfs_block_group *cache);
47 void btrfs_redirty_list_add(struct btrfs_transaction *trans,
48 struct extent_buffer *eb);
49 void btrfs_free_redirty_list(struct btrfs_transaction *trans);
50 bool btrfs_use_zone_append(struct btrfs_inode *inode, struct extent_map *em);
51 void btrfs_record_physical_zoned(struct inode *inode, u64 file_offset,
53 void btrfs_rewrite_logical_zoned(struct btrfs_ordered_extent *ordered);
54 bool btrfs_check_meta_write_pointer(struct btrfs_fs_info *fs_info,
55 struct extent_buffer *eb,
56 struct btrfs_block_group **cache_ret);
57 void btrfs_revert_meta_write_pointer(struct btrfs_block_group *cache,
58 struct extent_buffer *eb);
59 int btrfs_zoned_issue_zeroout(struct btrfs_device *device, u64 physical, u64 length);
60 int btrfs_sync_zone_write_pointer(struct btrfs_device *tgt_dev, u64 logical,
61 u64 physical_start, u64 physical_pos);
62 #else /* CONFIG_BLK_DEV_ZONED */
63 static inline int btrfs_get_dev_zone(struct btrfs_device *device, u64 pos,
64 struct blk_zone *zone)
69 static inline int btrfs_get_dev_zone_info_all_devices(struct btrfs_fs_info *fs_info)
74 static inline int btrfs_get_dev_zone_info(struct btrfs_device *device)
79 static inline void btrfs_destroy_dev_zone_info(struct btrfs_device *device) { }
81 static inline int btrfs_check_zoned_mode(const struct btrfs_fs_info *fs_info)
83 if (!btrfs_is_zoned(fs_info))
86 btrfs_err(fs_info, "zoned block devices support is not enabled");
90 static inline int btrfs_check_mountopts_zoned(struct btrfs_fs_info *info)
95 static inline int btrfs_sb_log_location_bdev(struct block_device *bdev,
96 int mirror, int rw, u64 *bytenr_ret)
98 *bytenr_ret = btrfs_sb_offset(mirror);
102 static inline int btrfs_sb_log_location(struct btrfs_device *device, int mirror,
103 int rw, u64 *bytenr_ret)
105 *bytenr_ret = btrfs_sb_offset(mirror);
109 static inline void btrfs_advance_sb_log(struct btrfs_device *device, int mirror)
112 static inline int btrfs_reset_sb_log_zones(struct block_device *bdev, int mirror)
117 static inline u64 btrfs_find_allocatable_zones(struct btrfs_device *device,
118 u64 hole_start, u64 hole_end,
124 static inline int btrfs_reset_device_zone(struct btrfs_device *device,
125 u64 physical, u64 length, u64 *bytes)
131 static inline int btrfs_ensure_empty_zones(struct btrfs_device *device,
137 static inline int btrfs_load_block_group_zone_info(
138 struct btrfs_block_group *cache, bool new)
143 static inline void btrfs_calc_zone_unusable(struct btrfs_block_group *cache) { }
145 static inline void btrfs_redirty_list_add(struct btrfs_transaction *trans,
146 struct extent_buffer *eb) { }
147 static inline void btrfs_free_redirty_list(struct btrfs_transaction *trans) { }
149 static inline bool btrfs_use_zone_append(struct btrfs_inode *inode,
150 struct extent_map *em)
155 static inline void btrfs_record_physical_zoned(struct inode *inode,
156 u64 file_offset, struct bio *bio)
160 static inline void btrfs_rewrite_logical_zoned(
161 struct btrfs_ordered_extent *ordered) { }
163 static inline bool btrfs_check_meta_write_pointer(struct btrfs_fs_info *fs_info,
164 struct extent_buffer *eb,
165 struct btrfs_block_group **cache_ret)
170 static inline void btrfs_revert_meta_write_pointer(
171 struct btrfs_block_group *cache,
172 struct extent_buffer *eb)
176 static inline int btrfs_zoned_issue_zeroout(struct btrfs_device *device,
177 u64 physical, u64 length)
182 static inline int btrfs_sync_zone_write_pointer(struct btrfs_device *tgt_dev,
183 u64 logical, u64 physical_start,
191 static inline bool btrfs_dev_is_sequential(struct btrfs_device *device, u64 pos)
193 struct btrfs_zoned_device_info *zone_info = device->zone_info;
198 return test_bit(pos >> zone_info->zone_size_shift, zone_info->seq_zones);
201 static inline bool btrfs_dev_is_empty_zone(struct btrfs_device *device, u64 pos)
203 struct btrfs_zoned_device_info *zone_info = device->zone_info;
208 return test_bit(pos >> zone_info->zone_size_shift, zone_info->empty_zones);
211 static inline void btrfs_dev_set_empty_zone_bit(struct btrfs_device *device,
214 struct btrfs_zoned_device_info *zone_info = device->zone_info;
220 zno = pos >> zone_info->zone_size_shift;
222 set_bit(zno, zone_info->empty_zones);
224 clear_bit(zno, zone_info->empty_zones);
227 static inline void btrfs_dev_set_zone_empty(struct btrfs_device *device, u64 pos)
229 btrfs_dev_set_empty_zone_bit(device, pos, true);
232 static inline void btrfs_dev_clear_zone_empty(struct btrfs_device *device, u64 pos)
234 btrfs_dev_set_empty_zone_bit(device, pos, false);
237 static inline bool btrfs_check_device_zone_type(const struct btrfs_fs_info *fs_info,
238 struct block_device *bdev)
240 if (btrfs_is_zoned(fs_info)) {
242 * We can allow a regular device on a zoned filesystem, because
243 * we will emulate the zoned capabilities.
245 if (!bdev_is_zoned(bdev))
248 return fs_info->zone_size ==
249 (bdev_zone_sectors(bdev) << SECTOR_SHIFT);
252 /* Do not allow Host Manged zoned device */
253 return bdev_zoned_model(bdev) != BLK_ZONED_HM;
256 static inline bool btrfs_check_super_location(struct btrfs_device *device, u64 pos)
259 * On a non-zoned device, any address is OK. On a zoned device,
260 * non-SEQUENTIAL WRITE REQUIRED zones are capable.
262 return device->zone_info == NULL || !btrfs_dev_is_sequential(device, pos);
265 static inline bool btrfs_can_zone_reset(struct btrfs_device *device,
266 u64 physical, u64 length)
270 if (!btrfs_dev_is_sequential(device, physical))
273 zone_size = device->zone_info->zone_size;
274 if (!IS_ALIGNED(physical, zone_size) || !IS_ALIGNED(length, zone_size))
280 static inline void btrfs_zoned_meta_io_lock(struct btrfs_fs_info *fs_info)
282 if (!btrfs_is_zoned(fs_info))
284 mutex_lock(&fs_info->zoned_meta_io_lock);
287 static inline void btrfs_zoned_meta_io_unlock(struct btrfs_fs_info *fs_info)
289 if (!btrfs_is_zoned(fs_info))
291 mutex_unlock(&fs_info->zoned_meta_io_lock);
294 static inline void btrfs_clear_treelog_bg(struct btrfs_block_group *bg)
296 struct btrfs_fs_info *fs_info = bg->fs_info;
298 if (!btrfs_is_zoned(fs_info))
301 spin_lock(&fs_info->treelog_bg_lock);
302 if (fs_info->treelog_bg == bg->start)
303 fs_info->treelog_bg = 0;
304 spin_unlock(&fs_info->treelog_bg_lock);