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