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