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