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