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