Naohiro Aota | 5b31646 | 2020-11-10 20:26:07 +0900 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
| 2 | |
| 3 | #ifndef BTRFS_ZONED_H |
| 4 | #define BTRFS_ZONED_H |
| 5 | |
| 6 | #include <linux/types.h> |
Naohiro Aota | b70f509 | 2020-11-10 20:26:08 +0900 | [diff] [blame] | 7 | #include <linux/blkdev.h> |
Naohiro Aota | 1265925 | 2020-11-10 20:26:14 +0900 | [diff] [blame] | 8 | #include "volumes.h" |
| 9 | #include "disk-io.h" |
Naohiro Aota | 40ab3be | 2021-02-04 19:22:18 +0900 | [diff] [blame] | 10 | #include "block-group.h" |
Naohiro Aota | 5b31646 | 2020-11-10 20:26:07 +0900 | [diff] [blame] | 11 | |
Johannes Thumshirn | 18bb8bb | 2021-04-19 16:41:02 +0900 | [diff] [blame] | 12 | /* |
| 13 | * Block groups with more than this value (percents) of unusable space will be |
| 14 | * scheduled for background reclaim. |
| 15 | */ |
| 16 | #define BTRFS_DEFAULT_RECLAIM_THRESH 75 |
| 17 | |
Naohiro Aota | 5b31646 | 2020-11-10 20:26:07 +0900 | [diff] [blame] | 18 | struct btrfs_zoned_device_info { |
| 19 | /* |
| 20 | * Number of zones, zone size and types of zones if bdev is a |
| 21 | * zoned block device. |
| 22 | */ |
| 23 | u64 zone_size; |
| 24 | u8 zone_size_shift; |
| 25 | u32 nr_zones; |
Naohiro Aota | ea6f8dd | 2021-08-19 21:19:15 +0900 | [diff] [blame] | 26 | unsigned int max_active_zones; |
| 27 | atomic_t active_zones_left; |
Naohiro Aota | 5b31646 | 2020-11-10 20:26:07 +0900 | [diff] [blame] | 28 | unsigned long *seq_zones; |
| 29 | unsigned long *empty_zones; |
Naohiro Aota | ea6f8dd | 2021-08-19 21:19:15 +0900 | [diff] [blame] | 30 | unsigned long *active_zones; |
Naohiro Aota | 1265925 | 2020-11-10 20:26:14 +0900 | [diff] [blame] | 31 | struct blk_zone sb_zones[2 * BTRFS_SUPER_MIRROR_MAX]; |
Naohiro Aota | 5b31646 | 2020-11-10 20:26:07 +0900 | [diff] [blame] | 32 | }; |
| 33 | |
| 34 | #ifdef CONFIG_BLK_DEV_ZONED |
| 35 | int btrfs_get_dev_zone(struct btrfs_device *device, u64 pos, |
| 36 | struct blk_zone *zone); |
Naohiro Aota | 7365104 | 2021-02-04 19:21:42 +0900 | [diff] [blame] | 37 | int btrfs_get_dev_zone_info_all_devices(struct btrfs_fs_info *fs_info); |
Naohiro Aota | 5b31646 | 2020-11-10 20:26:07 +0900 | [diff] [blame] | 38 | int btrfs_get_dev_zone_info(struct btrfs_device *device); |
| 39 | void btrfs_destroy_dev_zone_info(struct btrfs_device *device); |
Naohiro Aota | b70f509 | 2020-11-10 20:26:08 +0900 | [diff] [blame] | 40 | int btrfs_check_zoned_mode(struct btrfs_fs_info *fs_info); |
Naohiro Aota | 5d1ab66 | 2020-11-10 20:26:10 +0900 | [diff] [blame] | 41 | int btrfs_check_mountopts_zoned(struct btrfs_fs_info *info); |
Naohiro Aota | 1265925 | 2020-11-10 20:26:14 +0900 | [diff] [blame] | 42 | int btrfs_sb_log_location_bdev(struct block_device *bdev, int mirror, int rw, |
| 43 | u64 *bytenr_ret); |
| 44 | int btrfs_sb_log_location(struct btrfs_device *device, int mirror, int rw, |
| 45 | u64 *bytenr_ret); |
Naohiro Aota | 8376d9e1 | 2021-08-19 21:19:14 +0900 | [diff] [blame] | 46 | int btrfs_advance_sb_log(struct btrfs_device *device, int mirror); |
Naohiro Aota | 1265925 | 2020-11-10 20:26:14 +0900 | [diff] [blame] | 47 | int btrfs_reset_sb_log_zones(struct block_device *bdev, int mirror); |
Naohiro Aota | 1cd6121 | 2021-02-04 19:21:48 +0900 | [diff] [blame] | 48 | u64 btrfs_find_allocatable_zones(struct btrfs_device *device, u64 hole_start, |
| 49 | u64 hole_end, u64 num_bytes); |
| 50 | int btrfs_reset_device_zone(struct btrfs_device *device, u64 physical, |
| 51 | u64 length, u64 *bytes); |
| 52 | int btrfs_ensure_empty_zones(struct btrfs_device *device, u64 start, u64 size); |
Naohiro Aota | a94794d | 2021-02-04 19:21:51 +0900 | [diff] [blame] | 53 | int btrfs_load_block_group_zone_info(struct btrfs_block_group *cache, bool new); |
Naohiro Aota | 169e0da | 2021-02-04 19:21:52 +0900 | [diff] [blame] | 54 | void btrfs_calc_zone_unusable(struct btrfs_block_group *cache); |
Naohiro Aota | d3575156 | 2021-02-04 19:21:54 +0900 | [diff] [blame] | 55 | void btrfs_redirty_list_add(struct btrfs_transaction *trans, |
| 56 | struct extent_buffer *eb); |
| 57 | void btrfs_free_redirty_list(struct btrfs_transaction *trans); |
Johannes Thumshirn | e380adf | 2021-05-19 00:40:27 +0900 | [diff] [blame] | 58 | bool btrfs_use_zone_append(struct btrfs_inode *inode, u64 start); |
Naohiro Aota | d8e3fb1 | 2021-02-04 19:22:05 +0900 | [diff] [blame] | 59 | void btrfs_record_physical_zoned(struct inode *inode, u64 file_offset, |
| 60 | struct bio *bio); |
| 61 | void btrfs_rewrite_logical_zoned(struct btrfs_ordered_extent *ordered); |
Naohiro Aota | 0bc09ca | 2021-02-04 19:22:08 +0900 | [diff] [blame] | 62 | bool btrfs_check_meta_write_pointer(struct btrfs_fs_info *fs_info, |
| 63 | struct extent_buffer *eb, |
| 64 | struct btrfs_block_group **cache_ret); |
| 65 | void btrfs_revert_meta_write_pointer(struct btrfs_block_group *cache, |
| 66 | struct extent_buffer *eb); |
Naohiro Aota | de17add | 2021-02-04 19:22:13 +0900 | [diff] [blame] | 67 | int btrfs_zoned_issue_zeroout(struct btrfs_device *device, u64 physical, u64 length); |
Naohiro Aota | 7db1c5d | 2021-02-04 19:22:14 +0900 | [diff] [blame] | 68 | int btrfs_sync_zone_write_pointer(struct btrfs_device *tgt_dev, u64 logical, |
| 69 | u64 physical_start, u64 physical_pos); |
Johannes Thumshirn | e7ff9e6 | 2021-05-19 00:40:29 +0900 | [diff] [blame] | 70 | struct btrfs_device *btrfs_zoned_get_device(struct btrfs_fs_info *fs_info, |
| 71 | u64 logical, u64 length); |
Naohiro Aota | afba2bc | 2021-08-19 21:19:17 +0900 | [diff] [blame] | 72 | bool btrfs_zone_activate(struct btrfs_block_group *block_group); |
| 73 | int btrfs_zone_finish(struct btrfs_block_group *block_group); |
Naohiro Aota | a85f05e | 2021-08-19 21:19:22 +0900 | [diff] [blame] | 74 | bool btrfs_can_activate_zone(struct btrfs_fs_devices *fs_devices, |
| 75 | int raid_index); |
Naohiro Aota | be1a1d7 | 2021-08-19 21:19:23 +0900 | [diff] [blame] | 76 | void btrfs_zone_finish_endio(struct btrfs_fs_info *fs_info, u64 logical, |
| 77 | u64 length); |
Johannes Thumshirn | c2707a2 | 2021-09-09 01:19:26 +0900 | [diff] [blame] | 78 | void btrfs_clear_data_reloc_bg(struct btrfs_block_group *bg); |
Naohiro Aota | 5b31646 | 2020-11-10 20:26:07 +0900 | [diff] [blame] | 79 | #else /* CONFIG_BLK_DEV_ZONED */ |
| 80 | static inline int btrfs_get_dev_zone(struct btrfs_device *device, u64 pos, |
| 81 | struct blk_zone *zone) |
| 82 | { |
| 83 | return 0; |
| 84 | } |
| 85 | |
Naohiro Aota | 7365104 | 2021-02-04 19:21:42 +0900 | [diff] [blame] | 86 | static inline int btrfs_get_dev_zone_info_all_devices(struct btrfs_fs_info *fs_info) |
| 87 | { |
| 88 | return 0; |
| 89 | } |
| 90 | |
Naohiro Aota | 5b31646 | 2020-11-10 20:26:07 +0900 | [diff] [blame] | 91 | static inline int btrfs_get_dev_zone_info(struct btrfs_device *device) |
| 92 | { |
| 93 | return 0; |
| 94 | } |
| 95 | |
| 96 | static inline void btrfs_destroy_dev_zone_info(struct btrfs_device *device) { } |
| 97 | |
Naohiro Aota | b70f509 | 2020-11-10 20:26:08 +0900 | [diff] [blame] | 98 | static inline int btrfs_check_zoned_mode(const struct btrfs_fs_info *fs_info) |
| 99 | { |
| 100 | if (!btrfs_is_zoned(fs_info)) |
| 101 | return 0; |
| 102 | |
| 103 | btrfs_err(fs_info, "zoned block devices support is not enabled"); |
| 104 | return -EOPNOTSUPP; |
| 105 | } |
| 106 | |
Naohiro Aota | 5d1ab66 | 2020-11-10 20:26:10 +0900 | [diff] [blame] | 107 | static inline int btrfs_check_mountopts_zoned(struct btrfs_fs_info *info) |
| 108 | { |
| 109 | return 0; |
| 110 | } |
| 111 | |
Naohiro Aota | 1265925 | 2020-11-10 20:26:14 +0900 | [diff] [blame] | 112 | static inline int btrfs_sb_log_location_bdev(struct block_device *bdev, |
| 113 | int mirror, int rw, u64 *bytenr_ret) |
| 114 | { |
| 115 | *bytenr_ret = btrfs_sb_offset(mirror); |
| 116 | return 0; |
| 117 | } |
| 118 | |
| 119 | static inline int btrfs_sb_log_location(struct btrfs_device *device, int mirror, |
| 120 | int rw, u64 *bytenr_ret) |
| 121 | { |
| 122 | *bytenr_ret = btrfs_sb_offset(mirror); |
| 123 | return 0; |
| 124 | } |
| 125 | |
Naohiro Aota | 8376d9e1 | 2021-08-19 21:19:14 +0900 | [diff] [blame] | 126 | static inline int btrfs_advance_sb_log(struct btrfs_device *device, int mirror) |
| 127 | { |
| 128 | return 0; |
| 129 | } |
Naohiro Aota | 1265925 | 2020-11-10 20:26:14 +0900 | [diff] [blame] | 130 | |
| 131 | static inline int btrfs_reset_sb_log_zones(struct block_device *bdev, int mirror) |
| 132 | { |
| 133 | return 0; |
| 134 | } |
| 135 | |
Naohiro Aota | 1cd6121 | 2021-02-04 19:21:48 +0900 | [diff] [blame] | 136 | static inline u64 btrfs_find_allocatable_zones(struct btrfs_device *device, |
| 137 | u64 hole_start, u64 hole_end, |
| 138 | u64 num_bytes) |
| 139 | { |
| 140 | return hole_start; |
| 141 | } |
| 142 | |
| 143 | static inline int btrfs_reset_device_zone(struct btrfs_device *device, |
| 144 | u64 physical, u64 length, u64 *bytes) |
| 145 | { |
| 146 | *bytes = 0; |
| 147 | return 0; |
| 148 | } |
| 149 | |
| 150 | static inline int btrfs_ensure_empty_zones(struct btrfs_device *device, |
| 151 | u64 start, u64 size) |
| 152 | { |
| 153 | return 0; |
| 154 | } |
| 155 | |
Naohiro Aota | 08e11a3 | 2021-02-04 19:21:50 +0900 | [diff] [blame] | 156 | static inline int btrfs_load_block_group_zone_info( |
Naohiro Aota | a94794d | 2021-02-04 19:21:51 +0900 | [diff] [blame] | 157 | struct btrfs_block_group *cache, bool new) |
Naohiro Aota | 08e11a3 | 2021-02-04 19:21:50 +0900 | [diff] [blame] | 158 | { |
| 159 | return 0; |
| 160 | } |
| 161 | |
Naohiro Aota | 169e0da | 2021-02-04 19:21:52 +0900 | [diff] [blame] | 162 | static inline void btrfs_calc_zone_unusable(struct btrfs_block_group *cache) { } |
| 163 | |
Naohiro Aota | d3575156 | 2021-02-04 19:21:54 +0900 | [diff] [blame] | 164 | static inline void btrfs_redirty_list_add(struct btrfs_transaction *trans, |
| 165 | struct extent_buffer *eb) { } |
| 166 | static inline void btrfs_free_redirty_list(struct btrfs_transaction *trans) { } |
| 167 | |
Johannes Thumshirn | e380adf | 2021-05-19 00:40:27 +0900 | [diff] [blame] | 168 | static inline bool btrfs_use_zone_append(struct btrfs_inode *inode, u64 start) |
Johannes Thumshirn | 08f45559 | 2021-02-04 19:22:03 +0900 | [diff] [blame] | 169 | { |
| 170 | return false; |
| 171 | } |
Naohiro Aota | d8e3fb1 | 2021-02-04 19:22:05 +0900 | [diff] [blame] | 172 | |
| 173 | static inline void btrfs_record_physical_zoned(struct inode *inode, |
| 174 | u64 file_offset, struct bio *bio) |
| 175 | { |
| 176 | } |
| 177 | |
| 178 | static inline void btrfs_rewrite_logical_zoned( |
| 179 | struct btrfs_ordered_extent *ordered) { } |
| 180 | |
Naohiro Aota | 0bc09ca | 2021-02-04 19:22:08 +0900 | [diff] [blame] | 181 | static inline bool btrfs_check_meta_write_pointer(struct btrfs_fs_info *fs_info, |
| 182 | struct extent_buffer *eb, |
| 183 | struct btrfs_block_group **cache_ret) |
| 184 | { |
| 185 | return true; |
| 186 | } |
| 187 | |
| 188 | static inline void btrfs_revert_meta_write_pointer( |
| 189 | struct btrfs_block_group *cache, |
| 190 | struct extent_buffer *eb) |
| 191 | { |
| 192 | } |
| 193 | |
Naohiro Aota | de17add | 2021-02-04 19:22:13 +0900 | [diff] [blame] | 194 | static inline int btrfs_zoned_issue_zeroout(struct btrfs_device *device, |
| 195 | u64 physical, u64 length) |
| 196 | { |
| 197 | return -EOPNOTSUPP; |
| 198 | } |
| 199 | |
Naohiro Aota | 7db1c5d | 2021-02-04 19:22:14 +0900 | [diff] [blame] | 200 | static inline int btrfs_sync_zone_write_pointer(struct btrfs_device *tgt_dev, |
| 201 | u64 logical, u64 physical_start, |
| 202 | u64 physical_pos) |
| 203 | { |
| 204 | return -EOPNOTSUPP; |
| 205 | } |
| 206 | |
Johannes Thumshirn | e7ff9e6 | 2021-05-19 00:40:29 +0900 | [diff] [blame] | 207 | static inline struct btrfs_device *btrfs_zoned_get_device( |
| 208 | struct btrfs_fs_info *fs_info, |
| 209 | u64 logical, u64 length) |
| 210 | { |
| 211 | return ERR_PTR(-EOPNOTSUPP); |
| 212 | } |
| 213 | |
Naohiro Aota | afba2bc | 2021-08-19 21:19:17 +0900 | [diff] [blame] | 214 | static inline bool btrfs_zone_activate(struct btrfs_block_group *block_group) |
| 215 | { |
| 216 | return true; |
| 217 | } |
| 218 | |
| 219 | static inline int btrfs_zone_finish(struct btrfs_block_group *block_group) |
| 220 | { |
| 221 | return 0; |
| 222 | } |
| 223 | |
Naohiro Aota | a85f05e | 2021-08-19 21:19:22 +0900 | [diff] [blame] | 224 | static inline bool btrfs_can_activate_zone(struct btrfs_fs_devices *fs_devices, |
| 225 | int raid_index) |
| 226 | { |
| 227 | return true; |
| 228 | } |
| 229 | |
Naohiro Aota | be1a1d7 | 2021-08-19 21:19:23 +0900 | [diff] [blame] | 230 | static inline void btrfs_zone_finish_endio(struct btrfs_fs_info *fs_info, |
| 231 | u64 logical, u64 length) { } |
| 232 | |
Johannes Thumshirn | c2707a2 | 2021-09-09 01:19:26 +0900 | [diff] [blame] | 233 | static inline void btrfs_clear_data_reloc_bg(struct btrfs_block_group *bg) { } |
| 234 | |
Naohiro Aota | 5b31646 | 2020-11-10 20:26:07 +0900 | [diff] [blame] | 235 | #endif |
| 236 | |
| 237 | static inline bool btrfs_dev_is_sequential(struct btrfs_device *device, u64 pos) |
| 238 | { |
| 239 | struct btrfs_zoned_device_info *zone_info = device->zone_info; |
| 240 | |
| 241 | if (!zone_info) |
| 242 | return false; |
| 243 | |
| 244 | return test_bit(pos >> zone_info->zone_size_shift, zone_info->seq_zones); |
| 245 | } |
| 246 | |
| 247 | static inline bool btrfs_dev_is_empty_zone(struct btrfs_device *device, u64 pos) |
| 248 | { |
| 249 | struct btrfs_zoned_device_info *zone_info = device->zone_info; |
| 250 | |
| 251 | if (!zone_info) |
| 252 | return true; |
| 253 | |
| 254 | return test_bit(pos >> zone_info->zone_size_shift, zone_info->empty_zones); |
| 255 | } |
| 256 | |
| 257 | static inline void btrfs_dev_set_empty_zone_bit(struct btrfs_device *device, |
| 258 | u64 pos, bool set) |
| 259 | { |
| 260 | struct btrfs_zoned_device_info *zone_info = device->zone_info; |
| 261 | unsigned int zno; |
| 262 | |
| 263 | if (!zone_info) |
| 264 | return; |
| 265 | |
| 266 | zno = pos >> zone_info->zone_size_shift; |
| 267 | if (set) |
| 268 | set_bit(zno, zone_info->empty_zones); |
| 269 | else |
| 270 | clear_bit(zno, zone_info->empty_zones); |
| 271 | } |
| 272 | |
| 273 | static inline void btrfs_dev_set_zone_empty(struct btrfs_device *device, u64 pos) |
| 274 | { |
| 275 | btrfs_dev_set_empty_zone_bit(device, pos, true); |
| 276 | } |
| 277 | |
| 278 | static inline void btrfs_dev_clear_zone_empty(struct btrfs_device *device, u64 pos) |
| 279 | { |
| 280 | btrfs_dev_set_empty_zone_bit(device, pos, false); |
| 281 | } |
| 282 | |
Naohiro Aota | b70f509 | 2020-11-10 20:26:08 +0900 | [diff] [blame] | 283 | static inline bool btrfs_check_device_zone_type(const struct btrfs_fs_info *fs_info, |
| 284 | struct block_device *bdev) |
| 285 | { |
Naohiro Aota | b70f509 | 2020-11-10 20:26:08 +0900 | [diff] [blame] | 286 | if (btrfs_is_zoned(fs_info)) { |
Johannes Thumshirn | 3c9daa0 | 2021-02-04 19:21:47 +0900 | [diff] [blame] | 287 | /* |
| 288 | * We can allow a regular device on a zoned filesystem, because |
| 289 | * we will emulate the zoned capabilities. |
| 290 | */ |
| 291 | if (!bdev_is_zoned(bdev)) |
| 292 | return true; |
| 293 | |
| 294 | return fs_info->zone_size == |
| 295 | (bdev_zone_sectors(bdev) << SECTOR_SHIFT); |
Naohiro Aota | b70f509 | 2020-11-10 20:26:08 +0900 | [diff] [blame] | 296 | } |
| 297 | |
| 298 | /* Do not allow Host Manged zoned device */ |
| 299 | return bdev_zoned_model(bdev) != BLK_ZONED_HM; |
| 300 | } |
| 301 | |
Naohiro Aota | 1265925 | 2020-11-10 20:26:14 +0900 | [diff] [blame] | 302 | static inline bool btrfs_check_super_location(struct btrfs_device *device, u64 pos) |
| 303 | { |
| 304 | /* |
| 305 | * On a non-zoned device, any address is OK. On a zoned device, |
| 306 | * non-SEQUENTIAL WRITE REQUIRED zones are capable. |
| 307 | */ |
| 308 | return device->zone_info == NULL || !btrfs_dev_is_sequential(device, pos); |
| 309 | } |
| 310 | |
Naohiro Aota | dcba6e4 | 2021-02-04 19:21:56 +0900 | [diff] [blame] | 311 | static inline bool btrfs_can_zone_reset(struct btrfs_device *device, |
| 312 | u64 physical, u64 length) |
| 313 | { |
| 314 | u64 zone_size; |
| 315 | |
| 316 | if (!btrfs_dev_is_sequential(device, physical)) |
| 317 | return false; |
| 318 | |
| 319 | zone_size = device->zone_info->zone_size; |
| 320 | if (!IS_ALIGNED(physical, zone_size) || !IS_ALIGNED(length, zone_size)) |
| 321 | return false; |
| 322 | |
| 323 | return true; |
| 324 | } |
| 325 | |
Naohiro Aota | 0bc09ca | 2021-02-04 19:22:08 +0900 | [diff] [blame] | 326 | static inline void btrfs_zoned_meta_io_lock(struct btrfs_fs_info *fs_info) |
| 327 | { |
| 328 | if (!btrfs_is_zoned(fs_info)) |
| 329 | return; |
| 330 | mutex_lock(&fs_info->zoned_meta_io_lock); |
| 331 | } |
| 332 | |
| 333 | static inline void btrfs_zoned_meta_io_unlock(struct btrfs_fs_info *fs_info) |
| 334 | { |
| 335 | if (!btrfs_is_zoned(fs_info)) |
| 336 | return; |
| 337 | mutex_unlock(&fs_info->zoned_meta_io_lock); |
| 338 | } |
| 339 | |
Naohiro Aota | 40ab3be | 2021-02-04 19:22:18 +0900 | [diff] [blame] | 340 | static inline void btrfs_clear_treelog_bg(struct btrfs_block_group *bg) |
| 341 | { |
| 342 | struct btrfs_fs_info *fs_info = bg->fs_info; |
| 343 | |
| 344 | if (!btrfs_is_zoned(fs_info)) |
| 345 | return; |
| 346 | |
| 347 | spin_lock(&fs_info->treelog_bg_lock); |
| 348 | if (fs_info->treelog_bg == bg->start) |
| 349 | fs_info->treelog_bg = 0; |
| 350 | spin_unlock(&fs_info->treelog_bg_lock); |
| 351 | } |
| 352 | |
Naohiro Aota | 5b31646 | 2020-11-10 20:26:07 +0900 | [diff] [blame] | 353 | #endif |