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
25 u64 max_zone_append_size;
27 unsigned long *seq_zones;
28 unsigned long *empty_zones;
29 struct blk_zone sb_zones[2 * BTRFS_SUPER_MIRROR_MAX];
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);
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,
42 int btrfs_sb_log_location(struct btrfs_device *device, int mirror, int rw,
44 void 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,
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 #else /* CONFIG_BLK_DEV_ZONED */
71 static inline int btrfs_get_dev_zone(struct btrfs_device *device, u64 pos,
72 struct blk_zone *zone)
77 static inline int btrfs_get_dev_zone_info_all_devices(struct btrfs_fs_info *fs_info)
82 static inline int btrfs_get_dev_zone_info(struct btrfs_device *device)
87 static inline void btrfs_destroy_dev_zone_info(struct btrfs_device *device) { }
89 static inline int btrfs_check_zoned_mode(const struct btrfs_fs_info *fs_info)
91 if (!btrfs_is_zoned(fs_info))
94 btrfs_err(fs_info, "zoned block devices support is not enabled");
98 static inline int btrfs_check_mountopts_zoned(struct btrfs_fs_info *info)
103 static inline int btrfs_sb_log_location_bdev(struct block_device *bdev,
104 int mirror, int rw, u64 *bytenr_ret)
106 *bytenr_ret = btrfs_sb_offset(mirror);
110 static inline int btrfs_sb_log_location(struct btrfs_device *device, int mirror,
111 int rw, u64 *bytenr_ret)
113 *bytenr_ret = btrfs_sb_offset(mirror);
117 static inline void btrfs_advance_sb_log(struct btrfs_device *device, int mirror)
120 static inline int btrfs_reset_sb_log_zones(struct block_device *bdev, int mirror)
125 static inline u64 btrfs_find_allocatable_zones(struct btrfs_device *device,
126 u64 hole_start, u64 hole_end,
132 static inline int btrfs_reset_device_zone(struct btrfs_device *device,
133 u64 physical, u64 length, u64 *bytes)
139 static inline int btrfs_ensure_empty_zones(struct btrfs_device *device,
145 static inline int btrfs_load_block_group_zone_info(
146 struct btrfs_block_group *cache, bool new)
151 static inline void btrfs_calc_zone_unusable(struct btrfs_block_group *cache) { }
153 static inline void btrfs_redirty_list_add(struct btrfs_transaction *trans,
154 struct extent_buffer *eb) { }
155 static inline void btrfs_free_redirty_list(struct btrfs_transaction *trans) { }
157 static inline bool btrfs_use_zone_append(struct btrfs_inode *inode, u64 start)
162 static inline void btrfs_record_physical_zoned(struct inode *inode,
163 u64 file_offset, struct bio *bio)
167 static inline void btrfs_rewrite_logical_zoned(
168 struct btrfs_ordered_extent *ordered) { }
170 static inline bool btrfs_check_meta_write_pointer(struct btrfs_fs_info *fs_info,
171 struct extent_buffer *eb,
172 struct btrfs_block_group **cache_ret)
177 static inline void btrfs_revert_meta_write_pointer(
178 struct btrfs_block_group *cache,
179 struct extent_buffer *eb)
183 static inline int btrfs_zoned_issue_zeroout(struct btrfs_device *device,
184 u64 physical, u64 length)
189 static inline int btrfs_sync_zone_write_pointer(struct btrfs_device *tgt_dev,
190 u64 logical, u64 physical_start,
196 static inline struct btrfs_device *btrfs_zoned_get_device(
197 struct btrfs_fs_info *fs_info,
198 u64 logical, u64 length)
200 return ERR_PTR(-EOPNOTSUPP);
205 static inline bool btrfs_dev_is_sequential(struct btrfs_device *device, u64 pos)
207 struct btrfs_zoned_device_info *zone_info = device->zone_info;
212 return test_bit(pos >> zone_info->zone_size_shift, zone_info->seq_zones);
215 static inline bool btrfs_dev_is_empty_zone(struct btrfs_device *device, u64 pos)
217 struct btrfs_zoned_device_info *zone_info = device->zone_info;
222 return test_bit(pos >> zone_info->zone_size_shift, zone_info->empty_zones);
225 static inline void btrfs_dev_set_empty_zone_bit(struct btrfs_device *device,
228 struct btrfs_zoned_device_info *zone_info = device->zone_info;
234 zno = pos >> zone_info->zone_size_shift;
236 set_bit(zno, zone_info->empty_zones);
238 clear_bit(zno, zone_info->empty_zones);
241 static inline void btrfs_dev_set_zone_empty(struct btrfs_device *device, u64 pos)
243 btrfs_dev_set_empty_zone_bit(device, pos, true);
246 static inline void btrfs_dev_clear_zone_empty(struct btrfs_device *device, u64 pos)
248 btrfs_dev_set_empty_zone_bit(device, pos, false);
251 static inline bool btrfs_check_device_zone_type(const struct btrfs_fs_info *fs_info,
252 struct block_device *bdev)
254 if (btrfs_is_zoned(fs_info)) {
256 * We can allow a regular device on a zoned filesystem, because
257 * we will emulate the zoned capabilities.
259 if (!bdev_is_zoned(bdev))
262 return fs_info->zone_size ==
263 (bdev_zone_sectors(bdev) << SECTOR_SHIFT);
266 /* Do not allow Host Manged zoned device */
267 return bdev_zoned_model(bdev) != BLK_ZONED_HM;
270 static inline bool btrfs_check_super_location(struct btrfs_device *device, u64 pos)
273 * On a non-zoned device, any address is OK. On a zoned device,
274 * non-SEQUENTIAL WRITE REQUIRED zones are capable.
276 return device->zone_info == NULL || !btrfs_dev_is_sequential(device, pos);
279 static inline bool btrfs_can_zone_reset(struct btrfs_device *device,
280 u64 physical, u64 length)
284 if (!btrfs_dev_is_sequential(device, physical))
287 zone_size = device->zone_info->zone_size;
288 if (!IS_ALIGNED(physical, zone_size) || !IS_ALIGNED(length, zone_size))
294 static inline void btrfs_zoned_meta_io_lock(struct btrfs_fs_info *fs_info)
296 if (!btrfs_is_zoned(fs_info))
298 mutex_lock(&fs_info->zoned_meta_io_lock);
301 static inline void btrfs_zoned_meta_io_unlock(struct btrfs_fs_info *fs_info)
303 if (!btrfs_is_zoned(fs_info))
305 mutex_unlock(&fs_info->zoned_meta_io_lock);
308 static inline void btrfs_clear_treelog_bg(struct btrfs_block_group *bg)
310 struct btrfs_fs_info *fs_info = bg->fs_info;
312 if (!btrfs_is_zoned(fs_info))
315 spin_lock(&fs_info->treelog_bg_lock);
316 if (fs_info->treelog_bg == bg->start)
317 fs_info->treelog_bg = 0;
318 spin_unlock(&fs_info->treelog_bg_lock);