1 /* SPDX-License-Identifier: GPL-2.0 */ 2 3 #ifndef BTRFS_ZONED_H 4 #define BTRFS_ZONED_H 5 6 #include <linux/types.h> 7 #include <linux/atomic.h> 8 #include <linux/blkdev.h> 9 #include <linux/blkzoned.h> 10 #include <linux/errno.h> 11 #include <linux/spinlock.h> 12 #include <linux/mutex.h> 13 #include "messages.h" 14 #include "volumes.h" 15 #include "disk-io.h" 16 #include "block-group.h" 17 #include "btrfs_inode.h" 18 19 struct block_device; 20 struct extent_buffer; 21 struct btrfs_bio; 22 struct btrfs_ordered_extent; 23 struct btrfs_fs_info; 24 struct btrfs_space_info; 25 struct btrfs_eb_write_context; 26 struct btrfs_fs_devices; 27 28 #define BTRFS_DEFAULT_RECLAIM_THRESH (75) 29 30 struct btrfs_zoned_device_info { 31 /* 32 * Number of zones, zone size and types of zones if bdev is a 33 * zoned block device. 34 */ 35 u64 zone_size; 36 u8 zone_size_shift; 37 u32 nr_zones; 38 unsigned int max_active_zones; 39 /* 40 * Reserved active zones for one metadata and one system block group. 41 * It can vary per-device depending on the allocation status. 42 */ 43 int reserved_active_zones; 44 atomic_t active_zones_left; 45 unsigned long *seq_zones; 46 unsigned long *empty_zones; 47 unsigned long *active_zones; 48 struct blk_zone *zone_cache; 49 struct blk_zone sb_zones[2 * BTRFS_SUPER_MIRROR_MAX]; 50 }; 51 52 void btrfs_finish_ordered_zoned(struct btrfs_ordered_extent *ordered); 53 54 #ifdef CONFIG_BLK_DEV_ZONED 55 int btrfs_get_dev_zone_info_all_devices(struct btrfs_fs_info *fs_info); 56 int btrfs_get_dev_zone_info(struct btrfs_device *device, bool populate_cache); 57 void btrfs_destroy_dev_zone_info(struct btrfs_device *device); 58 struct btrfs_zoned_device_info *btrfs_clone_dev_zone_info(struct btrfs_device *orig_dev); 59 int btrfs_check_zoned_mode(struct btrfs_fs_info *fs_info); 60 int btrfs_check_mountopts_zoned(const struct btrfs_fs_info *info, 61 unsigned long long *mount_opt); 62 int btrfs_sb_log_location_bdev(struct block_device *bdev, int mirror, int rw, 63 u64 *bytenr_ret); 64 int btrfs_sb_log_location(struct btrfs_device *device, int mirror, int rw, 65 u64 *bytenr_ret); 66 int btrfs_advance_sb_log(struct btrfs_device *device, int mirror); 67 int btrfs_reset_sb_log_zones(struct block_device *bdev, int mirror); 68 u64 btrfs_find_allocatable_zones(struct btrfs_device *device, u64 hole_start, 69 u64 hole_end, u64 num_bytes); 70 int btrfs_reset_device_zone(struct btrfs_device *device, u64 physical, 71 u64 length, u64 *bytes); 72 int btrfs_ensure_empty_zones(struct btrfs_device *device, u64 start, u64 size); 73 int btrfs_load_block_group_zone_info(struct btrfs_block_group *cache, bool new); 74 void btrfs_calc_zone_unusable(struct btrfs_block_group *cache); 75 bool btrfs_use_zone_append(struct btrfs_bio *bbio); 76 void btrfs_record_physical_zoned(struct btrfs_bio *bbio); 77 int btrfs_check_meta_write_pointer(struct btrfs_fs_info *fs_info, 78 struct btrfs_eb_write_context *ctx); 79 int btrfs_zoned_issue_zeroout(struct btrfs_device *device, u64 physical, u64 length); 80 int btrfs_sync_zone_write_pointer(struct btrfs_device *tgt_dev, u64 logical, 81 u64 physical_start, u64 physical_pos); 82 bool btrfs_zone_activate(struct btrfs_block_group *block_group); 83 int btrfs_zone_finish(struct btrfs_block_group *block_group); 84 bool btrfs_can_activate_zone(struct btrfs_fs_devices *fs_devices, u64 flags); 85 int btrfs_zone_finish_endio(struct btrfs_fs_info *fs_info, u64 logical, 86 u64 length); 87 void btrfs_schedule_zone_finish_bg(struct btrfs_block_group *bg, 88 struct extent_buffer *eb); 89 void btrfs_clear_data_reloc_bg(struct btrfs_block_group *bg); 90 void btrfs_zoned_reserve_data_reloc_bg(struct btrfs_fs_info *fs_info); 91 void btrfs_free_zone_cache(struct btrfs_fs_info *fs_info); 92 bool btrfs_zoned_should_reclaim(const struct btrfs_fs_info *fs_info); 93 void btrfs_zoned_release_data_reloc_bg(struct btrfs_fs_info *fs_info, u64 logical, 94 u64 length); 95 int btrfs_zone_finish_one_bg(struct btrfs_fs_info *fs_info); 96 int btrfs_zoned_activate_one_bg(struct btrfs_space_info *space_info, bool do_finish); 97 void btrfs_check_active_zone_reservation(struct btrfs_fs_info *fs_info); 98 int btrfs_reset_unused_block_groups(struct btrfs_space_info *space_info, u64 num_bytes); 99 #else /* CONFIG_BLK_DEV_ZONED */ 100 101 static inline int btrfs_get_dev_zone_info_all_devices(struct btrfs_fs_info *fs_info) 102 { 103 return 0; 104 } 105 106 static inline int btrfs_get_dev_zone_info(struct btrfs_device *device, 107 bool populate_cache) 108 { 109 return 0; 110 } 111 112 static inline void btrfs_destroy_dev_zone_info(struct btrfs_device *device) { } 113 114 /* 115 * In case the kernel is compiled without CONFIG_BLK_DEV_ZONED we'll never call 116 * into btrfs_clone_dev_zone_info() so it's safe to return NULL here. 117 */ 118 static inline struct btrfs_zoned_device_info *btrfs_clone_dev_zone_info( 119 struct btrfs_device *orig_dev) 120 { 121 return NULL; 122 } 123 124 static inline int btrfs_check_zoned_mode(const struct btrfs_fs_info *fs_info) 125 { 126 if (!btrfs_is_zoned(fs_info)) 127 return 0; 128 129 btrfs_err(fs_info, "zoned block devices support is not enabled"); 130 return -EOPNOTSUPP; 131 } 132 133 static inline int btrfs_check_mountopts_zoned(const struct btrfs_fs_info *info, 134 unsigned long long *mount_opt) 135 { 136 return 0; 137 } 138 139 static inline int btrfs_sb_log_location_bdev(struct block_device *bdev, 140 int mirror, int rw, u64 *bytenr_ret) 141 { 142 *bytenr_ret = btrfs_sb_offset(mirror); 143 return 0; 144 } 145 146 static inline int btrfs_sb_log_location(struct btrfs_device *device, int mirror, 147 int rw, u64 *bytenr_ret) 148 { 149 *bytenr_ret = btrfs_sb_offset(mirror); 150 return 0; 151 } 152 153 static inline int btrfs_advance_sb_log(struct btrfs_device *device, int mirror) 154 { 155 return 0; 156 } 157 158 static inline int btrfs_reset_sb_log_zones(struct block_device *bdev, int mirror) 159 { 160 return 0; 161 } 162 163 static inline u64 btrfs_find_allocatable_zones(struct btrfs_device *device, 164 u64 hole_start, u64 hole_end, 165 u64 num_bytes) 166 { 167 return hole_start; 168 } 169 170 static inline int btrfs_reset_device_zone(struct btrfs_device *device, 171 u64 physical, u64 length, u64 *bytes) 172 { 173 *bytes = 0; 174 return 0; 175 } 176 177 static inline int btrfs_ensure_empty_zones(struct btrfs_device *device, 178 u64 start, u64 size) 179 { 180 return 0; 181 } 182 183 static inline int btrfs_load_block_group_zone_info( 184 struct btrfs_block_group *cache, bool new) 185 { 186 return 0; 187 } 188 189 static inline void btrfs_calc_zone_unusable(struct btrfs_block_group *cache) { } 190 191 static inline bool btrfs_use_zone_append(struct btrfs_bio *bbio) 192 { 193 return false; 194 } 195 196 static inline void btrfs_record_physical_zoned(struct btrfs_bio *bbio) 197 { 198 } 199 200 static inline int btrfs_check_meta_write_pointer(struct btrfs_fs_info *fs_info, 201 struct btrfs_eb_write_context *ctx) 202 { 203 return 0; 204 } 205 206 static inline int btrfs_zoned_issue_zeroout(struct btrfs_device *device, 207 u64 physical, u64 length) 208 { 209 return -EOPNOTSUPP; 210 } 211 212 static inline int btrfs_sync_zone_write_pointer(struct btrfs_device *tgt_dev, 213 u64 logical, u64 physical_start, 214 u64 physical_pos) 215 { 216 return -EOPNOTSUPP; 217 } 218 219 static inline bool btrfs_zone_activate(struct btrfs_block_group *block_group) 220 { 221 return true; 222 } 223 224 static inline int btrfs_zone_finish(struct btrfs_block_group *block_group) 225 { 226 return 0; 227 } 228 229 static inline bool btrfs_can_activate_zone(struct btrfs_fs_devices *fs_devices, 230 u64 flags) 231 { 232 return true; 233 } 234 235 static inline int btrfs_zone_finish_endio(struct btrfs_fs_info *fs_info, 236 u64 logical, u64 length) 237 { 238 return 0; 239 } 240 241 static inline void btrfs_schedule_zone_finish_bg(struct btrfs_block_group *bg, 242 struct extent_buffer *eb) { } 243 244 static inline void btrfs_clear_data_reloc_bg(struct btrfs_block_group *bg) { } 245 246 static inline void btrfs_zoned_reserve_data_reloc_bg(struct btrfs_fs_info *fs_info) { } 247 248 static inline void btrfs_free_zone_cache(struct btrfs_fs_info *fs_info) { } 249 250 static inline bool btrfs_zoned_should_reclaim(const struct btrfs_fs_info *fs_info) 251 { 252 return false; 253 } 254 255 static inline void btrfs_zoned_release_data_reloc_bg(struct btrfs_fs_info *fs_info, 256 u64 logical, u64 length) { } 257 258 static inline int btrfs_zone_finish_one_bg(struct btrfs_fs_info *fs_info) 259 { 260 return 1; 261 } 262 263 static inline int btrfs_zoned_activate_one_bg(struct btrfs_space_info *space_info, 264 bool do_finish) 265 { 266 /* Consider all the block groups are active */ 267 return 0; 268 } 269 270 static inline void btrfs_check_active_zone_reservation(struct btrfs_fs_info *fs_info) { } 271 272 static inline int btrfs_reset_unused_block_groups(struct btrfs_space_info *space_info, 273 u64 num_bytes) 274 { 275 return 0; 276 } 277 278 #endif 279 280 static inline bool btrfs_dev_is_sequential(struct btrfs_device *device, u64 pos) 281 { 282 struct btrfs_zoned_device_info *zone_info = device->zone_info; 283 284 if (!zone_info) 285 return false; 286 287 return test_bit(pos >> zone_info->zone_size_shift, zone_info->seq_zones); 288 } 289 290 static inline bool btrfs_dev_is_empty_zone(struct btrfs_device *device, u64 pos) 291 { 292 struct btrfs_zoned_device_info *zone_info = device->zone_info; 293 294 if (!zone_info) 295 return true; 296 297 return test_bit(pos >> zone_info->zone_size_shift, zone_info->empty_zones); 298 } 299 300 static inline void btrfs_dev_set_empty_zone_bit(struct btrfs_device *device, 301 u64 pos, bool set) 302 { 303 struct btrfs_zoned_device_info *zone_info = device->zone_info; 304 unsigned int zno; 305 306 if (!zone_info) 307 return; 308 309 zno = pos >> zone_info->zone_size_shift; 310 if (set) 311 set_bit(zno, zone_info->empty_zones); 312 else 313 clear_bit(zno, zone_info->empty_zones); 314 } 315 316 static inline void btrfs_dev_set_zone_empty(struct btrfs_device *device, u64 pos) 317 { 318 btrfs_dev_set_empty_zone_bit(device, pos, true); 319 } 320 321 static inline void btrfs_dev_clear_zone_empty(struct btrfs_device *device, u64 pos) 322 { 323 btrfs_dev_set_empty_zone_bit(device, pos, false); 324 } 325 326 static inline bool btrfs_check_device_zone_type(const struct btrfs_fs_info *fs_info, 327 struct block_device *bdev) 328 { 329 if (btrfs_is_zoned(fs_info)) { 330 /* 331 * We can allow a regular device on a zoned filesystem, because 332 * we will emulate the zoned capabilities. 333 */ 334 if (!bdev_is_zoned(bdev)) 335 return true; 336 337 return fs_info->zone_size == 338 (bdev_zone_sectors(bdev) << SECTOR_SHIFT); 339 } 340 341 /* Do not allow Host Managed zoned device. */ 342 return !bdev_is_zoned(bdev); 343 } 344 345 static inline bool btrfs_check_super_location(struct btrfs_device *device, u64 pos) 346 { 347 /* 348 * On a non-zoned device, any address is OK. On a zoned device, 349 * non-SEQUENTIAL WRITE REQUIRED zones are capable. 350 */ 351 return device->zone_info == NULL || !btrfs_dev_is_sequential(device, pos); 352 } 353 354 static inline bool btrfs_can_zone_reset(struct btrfs_device *device, 355 u64 physical, u64 length) 356 { 357 u64 zone_size; 358 359 if (!btrfs_dev_is_sequential(device, physical)) 360 return false; 361 362 zone_size = device->zone_info->zone_size; 363 if (!IS_ALIGNED(physical, zone_size) || !IS_ALIGNED(length, zone_size)) 364 return false; 365 366 return true; 367 } 368 369 static inline void btrfs_zoned_meta_io_lock(struct btrfs_fs_info *fs_info) 370 { 371 if (!btrfs_is_zoned(fs_info)) 372 return; 373 mutex_lock(&fs_info->zoned_meta_io_lock); 374 } 375 376 static inline void btrfs_zoned_meta_io_unlock(struct btrfs_fs_info *fs_info) 377 { 378 if (!btrfs_is_zoned(fs_info)) 379 return; 380 mutex_unlock(&fs_info->zoned_meta_io_lock); 381 } 382 383 static inline void btrfs_clear_treelog_bg(struct btrfs_block_group *bg) 384 { 385 struct btrfs_fs_info *fs_info = bg->fs_info; 386 387 if (!btrfs_is_zoned(fs_info)) 388 return; 389 390 spin_lock(&fs_info->treelog_bg_lock); 391 if (fs_info->treelog_bg == bg->start) 392 fs_info->treelog_bg = 0; 393 spin_unlock(&fs_info->treelog_bg_lock); 394 } 395 396 static inline void btrfs_zoned_data_reloc_lock(struct btrfs_inode *inode) 397 { 398 struct btrfs_root *root = inode->root; 399 400 if (btrfs_is_data_reloc_root(root) && btrfs_is_zoned(root->fs_info)) 401 mutex_lock(&root->fs_info->zoned_data_reloc_io_lock); 402 } 403 404 static inline void btrfs_zoned_data_reloc_unlock(struct btrfs_inode *inode) 405 { 406 struct btrfs_root *root = inode->root; 407 408 if (btrfs_is_data_reloc_root(root) && btrfs_is_zoned(root->fs_info)) 409 mutex_unlock(&root->fs_info->zoned_data_reloc_io_lock); 410 } 411 412 static inline bool btrfs_zoned_bg_is_full(const struct btrfs_block_group *bg) 413 { 414 ASSERT(btrfs_is_zoned(bg->fs_info)); 415 return (bg->alloc_offset == bg->zone_capacity); 416 } 417 418 #endif 419