0001
0002
0003 #ifndef BTRFS_ZONED_H
0004 #define BTRFS_ZONED_H
0005
0006 #include <linux/types.h>
0007 #include <linux/blkdev.h>
0008 #include "volumes.h"
0009 #include "disk-io.h"
0010 #include "block-group.h"
0011 #include "btrfs_inode.h"
0012
0013 #define BTRFS_DEFAULT_RECLAIM_THRESH (75)
0014
0015 struct btrfs_zoned_device_info {
0016
0017
0018
0019
0020 u64 zone_size;
0021 u8 zone_size_shift;
0022 u64 max_zone_append_size;
0023 u32 nr_zones;
0024 unsigned int max_active_zones;
0025 atomic_t active_zones_left;
0026 unsigned long *seq_zones;
0027 unsigned long *empty_zones;
0028 unsigned long *active_zones;
0029 struct blk_zone *zone_cache;
0030 struct blk_zone sb_zones[2 * BTRFS_SUPER_MIRROR_MAX];
0031 };
0032
0033 #ifdef CONFIG_BLK_DEV_ZONED
0034 int btrfs_get_dev_zone(struct btrfs_device *device, u64 pos,
0035 struct blk_zone *zone);
0036 int btrfs_get_dev_zone_info_all_devices(struct btrfs_fs_info *fs_info);
0037 int btrfs_get_dev_zone_info(struct btrfs_device *device, bool populate_cache);
0038 void btrfs_destroy_dev_zone_info(struct btrfs_device *device);
0039 int btrfs_check_zoned_mode(struct btrfs_fs_info *fs_info);
0040 int btrfs_check_mountopts_zoned(struct btrfs_fs_info *info);
0041 int btrfs_sb_log_location_bdev(struct block_device *bdev, int mirror, int rw,
0042 u64 *bytenr_ret);
0043 int btrfs_sb_log_location(struct btrfs_device *device, int mirror, int rw,
0044 u64 *bytenr_ret);
0045 int btrfs_advance_sb_log(struct btrfs_device *device, int mirror);
0046 int btrfs_reset_sb_log_zones(struct block_device *bdev, int mirror);
0047 u64 btrfs_find_allocatable_zones(struct btrfs_device *device, u64 hole_start,
0048 u64 hole_end, u64 num_bytes);
0049 int btrfs_reset_device_zone(struct btrfs_device *device, u64 physical,
0050 u64 length, u64 *bytes);
0051 int btrfs_ensure_empty_zones(struct btrfs_device *device, u64 start, u64 size);
0052 int btrfs_load_block_group_zone_info(struct btrfs_block_group *cache, bool new);
0053 void btrfs_calc_zone_unusable(struct btrfs_block_group *cache);
0054 void btrfs_redirty_list_add(struct btrfs_transaction *trans,
0055 struct extent_buffer *eb);
0056 void btrfs_free_redirty_list(struct btrfs_transaction *trans);
0057 bool btrfs_use_zone_append(struct btrfs_inode *inode, u64 start);
0058 void btrfs_record_physical_zoned(struct inode *inode, u64 file_offset,
0059 struct bio *bio);
0060 void btrfs_rewrite_logical_zoned(struct btrfs_ordered_extent *ordered);
0061 bool btrfs_check_meta_write_pointer(struct btrfs_fs_info *fs_info,
0062 struct extent_buffer *eb,
0063 struct btrfs_block_group **cache_ret);
0064 void btrfs_revert_meta_write_pointer(struct btrfs_block_group *cache,
0065 struct extent_buffer *eb);
0066 int btrfs_zoned_issue_zeroout(struct btrfs_device *device, u64 physical, u64 length);
0067 int btrfs_sync_zone_write_pointer(struct btrfs_device *tgt_dev, u64 logical,
0068 u64 physical_start, u64 physical_pos);
0069 struct btrfs_device *btrfs_zoned_get_device(struct btrfs_fs_info *fs_info,
0070 u64 logical, u64 length);
0071 bool btrfs_zone_activate(struct btrfs_block_group *block_group);
0072 int btrfs_zone_finish(struct btrfs_block_group *block_group);
0073 bool btrfs_can_activate_zone(struct btrfs_fs_devices *fs_devices, u64 flags);
0074 void btrfs_zone_finish_endio(struct btrfs_fs_info *fs_info, u64 logical,
0075 u64 length);
0076 void btrfs_schedule_zone_finish_bg(struct btrfs_block_group *bg,
0077 struct extent_buffer *eb);
0078 void btrfs_clear_data_reloc_bg(struct btrfs_block_group *bg);
0079 void btrfs_free_zone_cache(struct btrfs_fs_info *fs_info);
0080 bool btrfs_zoned_should_reclaim(struct btrfs_fs_info *fs_info);
0081 void btrfs_zoned_release_data_reloc_bg(struct btrfs_fs_info *fs_info, u64 logical,
0082 u64 length);
0083 int btrfs_zone_finish_one_bg(struct btrfs_fs_info *fs_info);
0084 int btrfs_zoned_activate_one_bg(struct btrfs_fs_info *fs_info,
0085 struct btrfs_space_info *space_info, bool do_finish);
0086 #else
0087 static inline int btrfs_get_dev_zone(struct btrfs_device *device, u64 pos,
0088 struct blk_zone *zone)
0089 {
0090 return 0;
0091 }
0092
0093 static inline int btrfs_get_dev_zone_info_all_devices(struct btrfs_fs_info *fs_info)
0094 {
0095 return 0;
0096 }
0097
0098 static inline int btrfs_get_dev_zone_info(struct btrfs_device *device,
0099 bool populate_cache)
0100 {
0101 return 0;
0102 }
0103
0104 static inline void btrfs_destroy_dev_zone_info(struct btrfs_device *device) { }
0105
0106 static inline int btrfs_check_zoned_mode(const struct btrfs_fs_info *fs_info)
0107 {
0108 if (!btrfs_is_zoned(fs_info))
0109 return 0;
0110
0111 btrfs_err(fs_info, "zoned block devices support is not enabled");
0112 return -EOPNOTSUPP;
0113 }
0114
0115 static inline int btrfs_check_mountopts_zoned(struct btrfs_fs_info *info)
0116 {
0117 return 0;
0118 }
0119
0120 static inline int btrfs_sb_log_location_bdev(struct block_device *bdev,
0121 int mirror, int rw, u64 *bytenr_ret)
0122 {
0123 *bytenr_ret = btrfs_sb_offset(mirror);
0124 return 0;
0125 }
0126
0127 static inline int btrfs_sb_log_location(struct btrfs_device *device, int mirror,
0128 int rw, u64 *bytenr_ret)
0129 {
0130 *bytenr_ret = btrfs_sb_offset(mirror);
0131 return 0;
0132 }
0133
0134 static inline int btrfs_advance_sb_log(struct btrfs_device *device, int mirror)
0135 {
0136 return 0;
0137 }
0138
0139 static inline int btrfs_reset_sb_log_zones(struct block_device *bdev, int mirror)
0140 {
0141 return 0;
0142 }
0143
0144 static inline u64 btrfs_find_allocatable_zones(struct btrfs_device *device,
0145 u64 hole_start, u64 hole_end,
0146 u64 num_bytes)
0147 {
0148 return hole_start;
0149 }
0150
0151 static inline int btrfs_reset_device_zone(struct btrfs_device *device,
0152 u64 physical, u64 length, u64 *bytes)
0153 {
0154 *bytes = 0;
0155 return 0;
0156 }
0157
0158 static inline int btrfs_ensure_empty_zones(struct btrfs_device *device,
0159 u64 start, u64 size)
0160 {
0161 return 0;
0162 }
0163
0164 static inline int btrfs_load_block_group_zone_info(
0165 struct btrfs_block_group *cache, bool new)
0166 {
0167 return 0;
0168 }
0169
0170 static inline void btrfs_calc_zone_unusable(struct btrfs_block_group *cache) { }
0171
0172 static inline void btrfs_redirty_list_add(struct btrfs_transaction *trans,
0173 struct extent_buffer *eb) { }
0174 static inline void btrfs_free_redirty_list(struct btrfs_transaction *trans) { }
0175
0176 static inline bool btrfs_use_zone_append(struct btrfs_inode *inode, u64 start)
0177 {
0178 return false;
0179 }
0180
0181 static inline void btrfs_record_physical_zoned(struct inode *inode,
0182 u64 file_offset, struct bio *bio)
0183 {
0184 }
0185
0186 static inline void btrfs_rewrite_logical_zoned(
0187 struct btrfs_ordered_extent *ordered) { }
0188
0189 static inline bool btrfs_check_meta_write_pointer(struct btrfs_fs_info *fs_info,
0190 struct extent_buffer *eb,
0191 struct btrfs_block_group **cache_ret)
0192 {
0193 return true;
0194 }
0195
0196 static inline void btrfs_revert_meta_write_pointer(
0197 struct btrfs_block_group *cache,
0198 struct extent_buffer *eb)
0199 {
0200 }
0201
0202 static inline int btrfs_zoned_issue_zeroout(struct btrfs_device *device,
0203 u64 physical, u64 length)
0204 {
0205 return -EOPNOTSUPP;
0206 }
0207
0208 static inline int btrfs_sync_zone_write_pointer(struct btrfs_device *tgt_dev,
0209 u64 logical, u64 physical_start,
0210 u64 physical_pos)
0211 {
0212 return -EOPNOTSUPP;
0213 }
0214
0215 static inline struct btrfs_device *btrfs_zoned_get_device(
0216 struct btrfs_fs_info *fs_info,
0217 u64 logical, u64 length)
0218 {
0219 return ERR_PTR(-EOPNOTSUPP);
0220 }
0221
0222 static inline bool btrfs_zone_activate(struct btrfs_block_group *block_group)
0223 {
0224 return true;
0225 }
0226
0227 static inline int btrfs_zone_finish(struct btrfs_block_group *block_group)
0228 {
0229 return 0;
0230 }
0231
0232 static inline bool btrfs_can_activate_zone(struct btrfs_fs_devices *fs_devices,
0233 u64 flags)
0234 {
0235 return true;
0236 }
0237
0238 static inline void btrfs_zone_finish_endio(struct btrfs_fs_info *fs_info,
0239 u64 logical, u64 length) { }
0240
0241 static inline void btrfs_schedule_zone_finish_bg(struct btrfs_block_group *bg,
0242 struct extent_buffer *eb) { }
0243
0244 static inline void btrfs_clear_data_reloc_bg(struct btrfs_block_group *bg) { }
0245
0246 static inline void btrfs_free_zone_cache(struct btrfs_fs_info *fs_info) { }
0247
0248 static inline bool btrfs_zoned_should_reclaim(struct btrfs_fs_info *fs_info)
0249 {
0250 return false;
0251 }
0252
0253 static inline void btrfs_zoned_release_data_reloc_bg(struct btrfs_fs_info *fs_info,
0254 u64 logical, u64 length) { }
0255
0256 static inline int btrfs_zone_finish_one_bg(struct btrfs_fs_info *fs_info)
0257 {
0258 return 1;
0259 }
0260
0261 static inline int btrfs_zoned_activate_one_bg(struct btrfs_fs_info *fs_info,
0262 struct btrfs_space_info *space_info,
0263 bool do_finish)
0264 {
0265
0266 return 0;
0267 }
0268
0269 #endif
0270
0271 static inline bool btrfs_dev_is_sequential(struct btrfs_device *device, u64 pos)
0272 {
0273 struct btrfs_zoned_device_info *zone_info = device->zone_info;
0274
0275 if (!zone_info)
0276 return false;
0277
0278 return test_bit(pos >> zone_info->zone_size_shift, zone_info->seq_zones);
0279 }
0280
0281 static inline bool btrfs_dev_is_empty_zone(struct btrfs_device *device, u64 pos)
0282 {
0283 struct btrfs_zoned_device_info *zone_info = device->zone_info;
0284
0285 if (!zone_info)
0286 return true;
0287
0288 return test_bit(pos >> zone_info->zone_size_shift, zone_info->empty_zones);
0289 }
0290
0291 static inline void btrfs_dev_set_empty_zone_bit(struct btrfs_device *device,
0292 u64 pos, bool set)
0293 {
0294 struct btrfs_zoned_device_info *zone_info = device->zone_info;
0295 unsigned int zno;
0296
0297 if (!zone_info)
0298 return;
0299
0300 zno = pos >> zone_info->zone_size_shift;
0301 if (set)
0302 set_bit(zno, zone_info->empty_zones);
0303 else
0304 clear_bit(zno, zone_info->empty_zones);
0305 }
0306
0307 static inline void btrfs_dev_set_zone_empty(struct btrfs_device *device, u64 pos)
0308 {
0309 btrfs_dev_set_empty_zone_bit(device, pos, true);
0310 }
0311
0312 static inline void btrfs_dev_clear_zone_empty(struct btrfs_device *device, u64 pos)
0313 {
0314 btrfs_dev_set_empty_zone_bit(device, pos, false);
0315 }
0316
0317 static inline bool btrfs_check_device_zone_type(const struct btrfs_fs_info *fs_info,
0318 struct block_device *bdev)
0319 {
0320 if (btrfs_is_zoned(fs_info)) {
0321
0322
0323
0324
0325 if (!bdev_is_zoned(bdev))
0326 return true;
0327
0328 return fs_info->zone_size ==
0329 (bdev_zone_sectors(bdev) << SECTOR_SHIFT);
0330 }
0331
0332
0333 return bdev_zoned_model(bdev) != BLK_ZONED_HM;
0334 }
0335
0336 static inline bool btrfs_check_super_location(struct btrfs_device *device, u64 pos)
0337 {
0338
0339
0340
0341
0342 return device->zone_info == NULL || !btrfs_dev_is_sequential(device, pos);
0343 }
0344
0345 static inline bool btrfs_can_zone_reset(struct btrfs_device *device,
0346 u64 physical, u64 length)
0347 {
0348 u64 zone_size;
0349
0350 if (!btrfs_dev_is_sequential(device, physical))
0351 return false;
0352
0353 zone_size = device->zone_info->zone_size;
0354 if (!IS_ALIGNED(physical, zone_size) || !IS_ALIGNED(length, zone_size))
0355 return false;
0356
0357 return true;
0358 }
0359
0360 static inline void btrfs_zoned_meta_io_lock(struct btrfs_fs_info *fs_info)
0361 {
0362 if (!btrfs_is_zoned(fs_info))
0363 return;
0364 mutex_lock(&fs_info->zoned_meta_io_lock);
0365 }
0366
0367 static inline void btrfs_zoned_meta_io_unlock(struct btrfs_fs_info *fs_info)
0368 {
0369 if (!btrfs_is_zoned(fs_info))
0370 return;
0371 mutex_unlock(&fs_info->zoned_meta_io_lock);
0372 }
0373
0374 static inline void btrfs_clear_treelog_bg(struct btrfs_block_group *bg)
0375 {
0376 struct btrfs_fs_info *fs_info = bg->fs_info;
0377
0378 if (!btrfs_is_zoned(fs_info))
0379 return;
0380
0381 spin_lock(&fs_info->treelog_bg_lock);
0382 if (fs_info->treelog_bg == bg->start)
0383 fs_info->treelog_bg = 0;
0384 spin_unlock(&fs_info->treelog_bg_lock);
0385 }
0386
0387 static inline void btrfs_zoned_data_reloc_lock(struct btrfs_inode *inode)
0388 {
0389 struct btrfs_root *root = inode->root;
0390
0391 if (btrfs_is_data_reloc_root(root) && btrfs_is_zoned(root->fs_info))
0392 mutex_lock(&root->fs_info->zoned_data_reloc_io_lock);
0393 }
0394
0395 static inline void btrfs_zoned_data_reloc_unlock(struct btrfs_inode *inode)
0396 {
0397 struct btrfs_root *root = inode->root;
0398
0399 if (btrfs_is_data_reloc_root(root) && btrfs_is_zoned(root->fs_info))
0400 mutex_unlock(&root->fs_info->zoned_data_reloc_io_lock);
0401 }
0402
0403 static inline bool btrfs_zoned_bg_is_full(const struct btrfs_block_group *bg)
0404 {
0405 ASSERT(btrfs_is_zoned(bg->fs_info));
0406 return (bg->alloc_offset == bg->zone_capacity);
0407 }
0408
0409 #endif