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