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