blob: 5ed1ea2009eadcc2aad6bc757becd261414a2440 [file] [log] [blame]
Naohiro Aota5b316462020-11-10 20:26:07 +09001/* SPDX-License-Identifier: GPL-2.0 */
2
3#ifndef BTRFS_ZONED_H
4#define BTRFS_ZONED_H
5
6#include <linux/types.h>
Naohiro Aotab70f5092020-11-10 20:26:08 +09007#include <linux/blkdev.h>
Naohiro Aota12659252020-11-10 20:26:14 +09008#include "volumes.h"
9#include "disk-io.h"
Naohiro Aota5b316462020-11-10 20:26:07 +090010
11struct btrfs_zoned_device_info {
12 /*
13 * Number of zones, zone size and types of zones if bdev is a
14 * zoned block device.
15 */
16 u64 zone_size;
17 u8 zone_size_shift;
Naohiro Aota862931c2020-11-10 20:26:09 +090018 u64 max_zone_append_size;
Naohiro Aota5b316462020-11-10 20:26:07 +090019 u32 nr_zones;
20 unsigned long *seq_zones;
21 unsigned long *empty_zones;
Naohiro Aota12659252020-11-10 20:26:14 +090022 struct blk_zone sb_zones[2 * BTRFS_SUPER_MIRROR_MAX];
Naohiro Aota5b316462020-11-10 20:26:07 +090023};
24
25#ifdef CONFIG_BLK_DEV_ZONED
26int btrfs_get_dev_zone(struct btrfs_device *device, u64 pos,
27 struct blk_zone *zone);
Naohiro Aota73651042021-02-04 19:21:42 +090028int btrfs_get_dev_zone_info_all_devices(struct btrfs_fs_info *fs_info);
Naohiro Aota5b316462020-11-10 20:26:07 +090029int btrfs_get_dev_zone_info(struct btrfs_device *device);
30void btrfs_destroy_dev_zone_info(struct btrfs_device *device);
Naohiro Aotab70f5092020-11-10 20:26:08 +090031int btrfs_check_zoned_mode(struct btrfs_fs_info *fs_info);
Naohiro Aota5d1ab662020-11-10 20:26:10 +090032int btrfs_check_mountopts_zoned(struct btrfs_fs_info *info);
Naohiro Aota12659252020-11-10 20:26:14 +090033int btrfs_sb_log_location_bdev(struct block_device *bdev, int mirror, int rw,
34 u64 *bytenr_ret);
35int btrfs_sb_log_location(struct btrfs_device *device, int mirror, int rw,
36 u64 *bytenr_ret);
37void btrfs_advance_sb_log(struct btrfs_device *device, int mirror);
38int btrfs_reset_sb_log_zones(struct block_device *bdev, int mirror);
Naohiro Aota1cd61212021-02-04 19:21:48 +090039u64 btrfs_find_allocatable_zones(struct btrfs_device *device, u64 hole_start,
40 u64 hole_end, u64 num_bytes);
41int btrfs_reset_device_zone(struct btrfs_device *device, u64 physical,
42 u64 length, u64 *bytes);
43int btrfs_ensure_empty_zones(struct btrfs_device *device, u64 start, u64 size);
Naohiro Aotaa94794d2021-02-04 19:21:51 +090044int btrfs_load_block_group_zone_info(struct btrfs_block_group *cache, bool new);
Naohiro Aota169e0da2021-02-04 19:21:52 +090045void btrfs_calc_zone_unusable(struct btrfs_block_group *cache);
Naohiro Aotad35751562021-02-04 19:21:54 +090046void btrfs_redirty_list_add(struct btrfs_transaction *trans,
47 struct extent_buffer *eb);
48void btrfs_free_redirty_list(struct btrfs_transaction *trans);
Johannes Thumshirn08f455592021-02-04 19:22:03 +090049bool btrfs_use_zone_append(struct btrfs_inode *inode, struct extent_map *em);
Naohiro Aotad8e3fb12021-02-04 19:22:05 +090050void btrfs_record_physical_zoned(struct inode *inode, u64 file_offset,
51 struct bio *bio);
52void btrfs_rewrite_logical_zoned(struct btrfs_ordered_extent *ordered);
Naohiro Aota0bc09ca2021-02-04 19:22:08 +090053bool btrfs_check_meta_write_pointer(struct btrfs_fs_info *fs_info,
54 struct extent_buffer *eb,
55 struct btrfs_block_group **cache_ret);
56void btrfs_revert_meta_write_pointer(struct btrfs_block_group *cache,
57 struct extent_buffer *eb);
Naohiro Aotade17add2021-02-04 19:22:13 +090058int btrfs_zoned_issue_zeroout(struct btrfs_device *device, u64 physical, u64 length);
Naohiro Aota5b316462020-11-10 20:26:07 +090059#else /* CONFIG_BLK_DEV_ZONED */
60static inline int btrfs_get_dev_zone(struct btrfs_device *device, u64 pos,
61 struct blk_zone *zone)
62{
63 return 0;
64}
65
Naohiro Aota73651042021-02-04 19:21:42 +090066static inline int btrfs_get_dev_zone_info_all_devices(struct btrfs_fs_info *fs_info)
67{
68 return 0;
69}
70
Naohiro Aota5b316462020-11-10 20:26:07 +090071static inline int btrfs_get_dev_zone_info(struct btrfs_device *device)
72{
73 return 0;
74}
75
76static inline void btrfs_destroy_dev_zone_info(struct btrfs_device *device) { }
77
Naohiro Aotab70f5092020-11-10 20:26:08 +090078static inline int btrfs_check_zoned_mode(const struct btrfs_fs_info *fs_info)
79{
80 if (!btrfs_is_zoned(fs_info))
81 return 0;
82
83 btrfs_err(fs_info, "zoned block devices support is not enabled");
84 return -EOPNOTSUPP;
85}
86
Naohiro Aota5d1ab662020-11-10 20:26:10 +090087static inline int btrfs_check_mountopts_zoned(struct btrfs_fs_info *info)
88{
89 return 0;
90}
91
Naohiro Aota12659252020-11-10 20:26:14 +090092static inline int btrfs_sb_log_location_bdev(struct block_device *bdev,
93 int mirror, int rw, u64 *bytenr_ret)
94{
95 *bytenr_ret = btrfs_sb_offset(mirror);
96 return 0;
97}
98
99static inline int btrfs_sb_log_location(struct btrfs_device *device, int mirror,
100 int rw, u64 *bytenr_ret)
101{
102 *bytenr_ret = btrfs_sb_offset(mirror);
103 return 0;
104}
105
106static inline void btrfs_advance_sb_log(struct btrfs_device *device, int mirror)
107{ }
108
109static inline int btrfs_reset_sb_log_zones(struct block_device *bdev, int mirror)
110{
111 return 0;
112}
113
Naohiro Aota1cd61212021-02-04 19:21:48 +0900114static inline u64 btrfs_find_allocatable_zones(struct btrfs_device *device,
115 u64 hole_start, u64 hole_end,
116 u64 num_bytes)
117{
118 return hole_start;
119}
120
121static inline int btrfs_reset_device_zone(struct btrfs_device *device,
122 u64 physical, u64 length, u64 *bytes)
123{
124 *bytes = 0;
125 return 0;
126}
127
128static inline int btrfs_ensure_empty_zones(struct btrfs_device *device,
129 u64 start, u64 size)
130{
131 return 0;
132}
133
Naohiro Aota08e11a32021-02-04 19:21:50 +0900134static inline int btrfs_load_block_group_zone_info(
Naohiro Aotaa94794d2021-02-04 19:21:51 +0900135 struct btrfs_block_group *cache, bool new)
Naohiro Aota08e11a32021-02-04 19:21:50 +0900136{
137 return 0;
138}
139
Naohiro Aota169e0da2021-02-04 19:21:52 +0900140static inline void btrfs_calc_zone_unusable(struct btrfs_block_group *cache) { }
141
Naohiro Aotad35751562021-02-04 19:21:54 +0900142static inline void btrfs_redirty_list_add(struct btrfs_transaction *trans,
143 struct extent_buffer *eb) { }
144static inline void btrfs_free_redirty_list(struct btrfs_transaction *trans) { }
145
Johannes Thumshirn08f455592021-02-04 19:22:03 +0900146static inline bool btrfs_use_zone_append(struct btrfs_inode *inode,
147 struct extent_map *em)
148{
149 return false;
150}
Naohiro Aotad8e3fb12021-02-04 19:22:05 +0900151
152static inline void btrfs_record_physical_zoned(struct inode *inode,
153 u64 file_offset, struct bio *bio)
154{
155}
156
157static inline void btrfs_rewrite_logical_zoned(
158 struct btrfs_ordered_extent *ordered) { }
159
Naohiro Aota0bc09ca2021-02-04 19:22:08 +0900160static inline bool btrfs_check_meta_write_pointer(struct btrfs_fs_info *fs_info,
161 struct extent_buffer *eb,
162 struct btrfs_block_group **cache_ret)
163{
164 return true;
165}
166
167static inline void btrfs_revert_meta_write_pointer(
168 struct btrfs_block_group *cache,
169 struct extent_buffer *eb)
170{
171}
172
Naohiro Aotade17add2021-02-04 19:22:13 +0900173static inline int btrfs_zoned_issue_zeroout(struct btrfs_device *device,
174 u64 physical, u64 length)
175{
176 return -EOPNOTSUPP;
177}
178
Naohiro Aota5b316462020-11-10 20:26:07 +0900179#endif
180
181static inline bool btrfs_dev_is_sequential(struct btrfs_device *device, u64 pos)
182{
183 struct btrfs_zoned_device_info *zone_info = device->zone_info;
184
185 if (!zone_info)
186 return false;
187
188 return test_bit(pos >> zone_info->zone_size_shift, zone_info->seq_zones);
189}
190
191static inline bool btrfs_dev_is_empty_zone(struct btrfs_device *device, u64 pos)
192{
193 struct btrfs_zoned_device_info *zone_info = device->zone_info;
194
195 if (!zone_info)
196 return true;
197
198 return test_bit(pos >> zone_info->zone_size_shift, zone_info->empty_zones);
199}
200
201static inline void btrfs_dev_set_empty_zone_bit(struct btrfs_device *device,
202 u64 pos, bool set)
203{
204 struct btrfs_zoned_device_info *zone_info = device->zone_info;
205 unsigned int zno;
206
207 if (!zone_info)
208 return;
209
210 zno = pos >> zone_info->zone_size_shift;
211 if (set)
212 set_bit(zno, zone_info->empty_zones);
213 else
214 clear_bit(zno, zone_info->empty_zones);
215}
216
217static inline void btrfs_dev_set_zone_empty(struct btrfs_device *device, u64 pos)
218{
219 btrfs_dev_set_empty_zone_bit(device, pos, true);
220}
221
222static inline void btrfs_dev_clear_zone_empty(struct btrfs_device *device, u64 pos)
223{
224 btrfs_dev_set_empty_zone_bit(device, pos, false);
225}
226
Naohiro Aotab70f5092020-11-10 20:26:08 +0900227static inline bool btrfs_check_device_zone_type(const struct btrfs_fs_info *fs_info,
228 struct block_device *bdev)
229{
Naohiro Aotab70f5092020-11-10 20:26:08 +0900230 if (btrfs_is_zoned(fs_info)) {
Johannes Thumshirn3c9daa02021-02-04 19:21:47 +0900231 /*
232 * We can allow a regular device on a zoned filesystem, because
233 * we will emulate the zoned capabilities.
234 */
235 if (!bdev_is_zoned(bdev))
236 return true;
237
238 return fs_info->zone_size ==
239 (bdev_zone_sectors(bdev) << SECTOR_SHIFT);
Naohiro Aotab70f5092020-11-10 20:26:08 +0900240 }
241
242 /* Do not allow Host Manged zoned device */
243 return bdev_zoned_model(bdev) != BLK_ZONED_HM;
244}
245
Naohiro Aota12659252020-11-10 20:26:14 +0900246static inline bool btrfs_check_super_location(struct btrfs_device *device, u64 pos)
247{
248 /*
249 * On a non-zoned device, any address is OK. On a zoned device,
250 * non-SEQUENTIAL WRITE REQUIRED zones are capable.
251 */
252 return device->zone_info == NULL || !btrfs_dev_is_sequential(device, pos);
253}
254
Naohiro Aotadcba6e42021-02-04 19:21:56 +0900255static inline bool btrfs_can_zone_reset(struct btrfs_device *device,
256 u64 physical, u64 length)
257{
258 u64 zone_size;
259
260 if (!btrfs_dev_is_sequential(device, physical))
261 return false;
262
263 zone_size = device->zone_info->zone_size;
264 if (!IS_ALIGNED(physical, zone_size) || !IS_ALIGNED(length, zone_size))
265 return false;
266
267 return true;
268}
269
Naohiro Aota0bc09ca2021-02-04 19:22:08 +0900270static inline void btrfs_zoned_meta_io_lock(struct btrfs_fs_info *fs_info)
271{
272 if (!btrfs_is_zoned(fs_info))
273 return;
274 mutex_lock(&fs_info->zoned_meta_io_lock);
275}
276
277static inline void btrfs_zoned_meta_io_unlock(struct btrfs_fs_info *fs_info)
278{
279 if (!btrfs_is_zoned(fs_info))
280 return;
281 mutex_unlock(&fs_info->zoned_meta_io_lock);
282}
283
Naohiro Aota5b316462020-11-10 20:26:07 +0900284#endif