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