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