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