1 /* SPDX-License-Identifier: GPL-2.0 */
6 #include <linux/types.h>
7 #include <linux/blkdev.h>
10 #include "block-group.h"
13 * Block groups with more than this value (percents) of unusable space will be
14 * scheduled for background reclaim.
16 #define BTRFS_DEFAULT_RECLAIM_THRESH 75
18 struct btrfs_zoned_device_info {
20 * Number of zones, zone size and types of zones if bdev is a
26 unsigned int max_active_zones;
27 atomic_t active_zones_left;
28 unsigned long *seq_zones;
29 unsigned long *empty_zones;
30 unsigned long *active_zones;
31 struct blk_zone sb_zones[2 * BTRFS_SUPER_MIRROR_MAX];
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);
39 void btrfs_destroy_dev_zone_info(struct btrfs_device *device);
40 int btrfs_check_zoned_mode(struct btrfs_fs_info *fs_info);
41 int btrfs_check_mountopts_zoned(struct btrfs_fs_info *info);
42 int btrfs_sb_log_location_bdev(struct block_device *bdev, int mirror, int rw,
44 int btrfs_sb_log_location(struct btrfs_device *device, int mirror, int rw,
46 int btrfs_advance_sb_log(struct btrfs_device *device, int mirror);
47 int btrfs_reset_sb_log_zones(struct block_device *bdev, int mirror);
48 u64 btrfs_find_allocatable_zones(struct btrfs_device *device, u64 hole_start,
49 u64 hole_end, u64 num_bytes);
50 int btrfs_reset_device_zone(struct btrfs_device *device, u64 physical,
51 u64 length, u64 *bytes);
52 int btrfs_ensure_empty_zones(struct btrfs_device *device, u64 start, u64 size);
53 int btrfs_load_block_group_zone_info(struct btrfs_block_group *cache, bool new);
54 void btrfs_calc_zone_unusable(struct btrfs_block_group *cache);
55 void btrfs_redirty_list_add(struct btrfs_transaction *trans,
56 struct extent_buffer *eb);
57 void btrfs_free_redirty_list(struct btrfs_transaction *trans);
58 bool btrfs_use_zone_append(struct btrfs_inode *inode, u64 start);
59 void btrfs_record_physical_zoned(struct inode *inode, u64 file_offset,
61 void btrfs_rewrite_logical_zoned(struct btrfs_ordered_extent *ordered);
62 bool btrfs_check_meta_write_pointer(struct btrfs_fs_info *fs_info,
63 struct extent_buffer *eb,
64 struct btrfs_block_group **cache_ret);
65 void btrfs_revert_meta_write_pointer(struct btrfs_block_group *cache,
66 struct extent_buffer *eb);
67 int btrfs_zoned_issue_zeroout(struct btrfs_device *device, u64 physical, u64 length);
68 int btrfs_sync_zone_write_pointer(struct btrfs_device *tgt_dev, u64 logical,
69 u64 physical_start, u64 physical_pos);
70 struct btrfs_device *btrfs_zoned_get_device(struct btrfs_fs_info *fs_info,
71 u64 logical, u64 length);
72 bool btrfs_zone_activate(struct btrfs_block_group *block_group);
73 int btrfs_zone_finish(struct btrfs_block_group *block_group);
74 bool btrfs_can_activate_zone(struct btrfs_fs_devices *fs_devices,
76 void btrfs_zone_finish_endio(struct btrfs_fs_info *fs_info, u64 logical,
78 void btrfs_clear_data_reloc_bg(struct btrfs_block_group *bg);
79 #else /* CONFIG_BLK_DEV_ZONED */
80 static inline int btrfs_get_dev_zone(struct btrfs_device *device, u64 pos,
81 struct blk_zone *zone)
86 static inline int btrfs_get_dev_zone_info_all_devices(struct btrfs_fs_info *fs_info)
91 static inline int btrfs_get_dev_zone_info(struct btrfs_device *device)
96 static inline void btrfs_destroy_dev_zone_info(struct btrfs_device *device) { }
98 static inline int btrfs_check_zoned_mode(const struct btrfs_fs_info *fs_info)
100 if (!btrfs_is_zoned(fs_info))
103 btrfs_err(fs_info, "zoned block devices support is not enabled");
107 static inline int btrfs_check_mountopts_zoned(struct btrfs_fs_info *info)
112 static inline int btrfs_sb_log_location_bdev(struct block_device *bdev,
113 int mirror, int rw, u64 *bytenr_ret)
115 *bytenr_ret = btrfs_sb_offset(mirror);
119 static inline int btrfs_sb_log_location(struct btrfs_device *device, int mirror,
120 int rw, u64 *bytenr_ret)
122 *bytenr_ret = btrfs_sb_offset(mirror);
126 static inline int btrfs_advance_sb_log(struct btrfs_device *device, int mirror)
131 static inline int btrfs_reset_sb_log_zones(struct block_device *bdev, int mirror)
136 static inline u64 btrfs_find_allocatable_zones(struct btrfs_device *device,
137 u64 hole_start, u64 hole_end,
143 static inline int btrfs_reset_device_zone(struct btrfs_device *device,
144 u64 physical, u64 length, u64 *bytes)
150 static inline int btrfs_ensure_empty_zones(struct btrfs_device *device,
156 static inline int btrfs_load_block_group_zone_info(
157 struct btrfs_block_group *cache, bool new)
162 static inline void btrfs_calc_zone_unusable(struct btrfs_block_group *cache) { }
164 static inline void btrfs_redirty_list_add(struct btrfs_transaction *trans,
165 struct extent_buffer *eb) { }
166 static inline void btrfs_free_redirty_list(struct btrfs_transaction *trans) { }
168 static inline bool btrfs_use_zone_append(struct btrfs_inode *inode, u64 start)
173 static inline void btrfs_record_physical_zoned(struct inode *inode,
174 u64 file_offset, struct bio *bio)
178 static inline void btrfs_rewrite_logical_zoned(
179 struct btrfs_ordered_extent *ordered) { }
181 static inline bool btrfs_check_meta_write_pointer(struct btrfs_fs_info *fs_info,
182 struct extent_buffer *eb,
183 struct btrfs_block_group **cache_ret)
188 static inline void btrfs_revert_meta_write_pointer(
189 struct btrfs_block_group *cache,
190 struct extent_buffer *eb)
194 static inline int btrfs_zoned_issue_zeroout(struct btrfs_device *device,
195 u64 physical, u64 length)
200 static inline int btrfs_sync_zone_write_pointer(struct btrfs_device *tgt_dev,
201 u64 logical, u64 physical_start,
207 static inline struct btrfs_device *btrfs_zoned_get_device(
208 struct btrfs_fs_info *fs_info,
209 u64 logical, u64 length)
211 return ERR_PTR(-EOPNOTSUPP);
214 static inline bool btrfs_zone_activate(struct btrfs_block_group *block_group)
219 static inline int btrfs_zone_finish(struct btrfs_block_group *block_group)
224 static inline bool btrfs_can_activate_zone(struct btrfs_fs_devices *fs_devices,
230 static inline void btrfs_zone_finish_endio(struct btrfs_fs_info *fs_info,
231 u64 logical, u64 length) { }
233 static inline void btrfs_clear_data_reloc_bg(struct btrfs_block_group *bg) { }
237 static inline bool btrfs_dev_is_sequential(struct btrfs_device *device, u64 pos)
239 struct btrfs_zoned_device_info *zone_info = device->zone_info;
244 return test_bit(pos >> zone_info->zone_size_shift, zone_info->seq_zones);
247 static inline bool btrfs_dev_is_empty_zone(struct btrfs_device *device, u64 pos)
249 struct btrfs_zoned_device_info *zone_info = device->zone_info;
254 return test_bit(pos >> zone_info->zone_size_shift, zone_info->empty_zones);
257 static inline void btrfs_dev_set_empty_zone_bit(struct btrfs_device *device,
260 struct btrfs_zoned_device_info *zone_info = device->zone_info;
266 zno = pos >> zone_info->zone_size_shift;
268 set_bit(zno, zone_info->empty_zones);
270 clear_bit(zno, zone_info->empty_zones);
273 static inline void btrfs_dev_set_zone_empty(struct btrfs_device *device, u64 pos)
275 btrfs_dev_set_empty_zone_bit(device, pos, true);
278 static inline void btrfs_dev_clear_zone_empty(struct btrfs_device *device, u64 pos)
280 btrfs_dev_set_empty_zone_bit(device, pos, false);
283 static inline bool btrfs_check_device_zone_type(const struct btrfs_fs_info *fs_info,
284 struct block_device *bdev)
286 if (btrfs_is_zoned(fs_info)) {
288 * We can allow a regular device on a zoned filesystem, because
289 * we will emulate the zoned capabilities.
291 if (!bdev_is_zoned(bdev))
294 return fs_info->zone_size ==
295 (bdev_zone_sectors(bdev) << SECTOR_SHIFT);
298 /* Do not allow Host Manged zoned device */
299 return bdev_zoned_model(bdev) != BLK_ZONED_HM;
302 static inline bool btrfs_check_super_location(struct btrfs_device *device, u64 pos)
305 * On a non-zoned device, any address is OK. On a zoned device,
306 * non-SEQUENTIAL WRITE REQUIRED zones are capable.
308 return device->zone_info == NULL || !btrfs_dev_is_sequential(device, pos);
311 static inline bool btrfs_can_zone_reset(struct btrfs_device *device,
312 u64 physical, u64 length)
316 if (!btrfs_dev_is_sequential(device, physical))
319 zone_size = device->zone_info->zone_size;
320 if (!IS_ALIGNED(physical, zone_size) || !IS_ALIGNED(length, zone_size))
326 static inline void btrfs_zoned_meta_io_lock(struct btrfs_fs_info *fs_info)
328 if (!btrfs_is_zoned(fs_info))
330 mutex_lock(&fs_info->zoned_meta_io_lock);
333 static inline void btrfs_zoned_meta_io_unlock(struct btrfs_fs_info *fs_info)
335 if (!btrfs_is_zoned(fs_info))
337 mutex_unlock(&fs_info->zoned_meta_io_lock);
340 static inline void btrfs_clear_treelog_bg(struct btrfs_block_group *bg)
342 struct btrfs_fs_info *fs_info = bg->fs_info;
344 if (!btrfs_is_zoned(fs_info))
347 spin_lock(&fs_info->treelog_bg_lock);
348 if (fs_info->treelog_bg == bg->start)
349 fs_info->treelog_bg = 0;
350 spin_unlock(&fs_info->treelog_bg_lock);