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