]>
git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blob - fs/btrfs/zoned.h
3a826f7c2040305a6b66ec0ed20538c65603add6
1 /* SPDX-License-Identifier: GPL-2.0 */
6 #include <linux/types.h>
7 #include <linux/blkdev.h>
10 #include "block-group.h"
11 #include "btrfs_inode.h"
14 * Block groups with more than this value (percents) of unusable space will be
15 * scheduled for background reclaim.
17 #define BTRFS_DEFAULT_RECLAIM_THRESH 75
19 struct btrfs_zoned_device_info
{
21 * Number of zones, zone size and types of zones if bdev is a
27 unsigned long *seq_zones
;
28 unsigned long *empty_zones
;
29 struct blk_zone
*zone_cache
;
30 struct blk_zone sb_zones
[2 * BTRFS_SUPER_MIRROR_MAX
];
33 #ifdef CONFIG_BLK_DEV_ZONED
34 int btrfs_get_dev_zone(struct btrfs_device
*device
, u64 pos
,
35 struct blk_zone
*zone
);
36 int btrfs_get_dev_zone_info_all_devices(struct btrfs_fs_info
*fs_info
);
37 int btrfs_get_dev_zone_info(struct btrfs_device
*device
, bool populate_cache
);
38 void btrfs_destroy_dev_zone_info(struct btrfs_device
*device
);
39 int btrfs_check_zoned_mode(struct btrfs_fs_info
*fs_info
);
40 int btrfs_check_mountopts_zoned(struct btrfs_fs_info
*info
);
41 int btrfs_sb_log_location_bdev(struct block_device
*bdev
, int mirror
, int rw
,
43 int btrfs_sb_log_location(struct btrfs_device
*device
, int mirror
, int rw
,
45 void btrfs_advance_sb_log(struct btrfs_device
*device
, int mirror
);
46 int btrfs_reset_sb_log_zones(struct block_device
*bdev
, int mirror
);
47 u64
btrfs_find_allocatable_zones(struct btrfs_device
*device
, u64 hole_start
,
48 u64 hole_end
, u64 num_bytes
);
49 int btrfs_reset_device_zone(struct btrfs_device
*device
, u64 physical
,
50 u64 length
, u64
*bytes
);
51 int btrfs_ensure_empty_zones(struct btrfs_device
*device
, u64 start
, u64 size
);
52 int btrfs_load_block_group_zone_info(struct btrfs_block_group
*cache
, bool new);
53 void btrfs_calc_zone_unusable(struct btrfs_block_group
*cache
);
54 void btrfs_redirty_list_add(struct btrfs_transaction
*trans
,
55 struct extent_buffer
*eb
);
56 void btrfs_free_redirty_list(struct btrfs_transaction
*trans
);
57 bool btrfs_use_zone_append(struct btrfs_inode
*inode
, u64 start
);
58 void btrfs_record_physical_zoned(struct inode
*inode
, u64 file_offset
,
60 void btrfs_rewrite_logical_zoned(struct btrfs_ordered_extent
*ordered
);
61 bool btrfs_check_meta_write_pointer(struct btrfs_fs_info
*fs_info
,
62 struct extent_buffer
*eb
,
63 struct btrfs_block_group
**cache_ret
);
64 void btrfs_revert_meta_write_pointer(struct btrfs_block_group
*cache
,
65 struct extent_buffer
*eb
);
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 struct btrfs_device
*btrfs_zoned_get_device(struct btrfs_fs_info
*fs_info
,
70 u64 logical
, u64 length
);
71 void btrfs_clear_data_reloc_bg(struct btrfs_block_group
*bg
);
72 void btrfs_free_zone_cache(struct btrfs_fs_info
*fs_info
);
73 #else /* CONFIG_BLK_DEV_ZONED */
74 static inline int btrfs_get_dev_zone(struct btrfs_device
*device
, u64 pos
,
75 struct blk_zone
*zone
)
80 static inline int btrfs_get_dev_zone_info_all_devices(struct btrfs_fs_info
*fs_info
)
85 static inline int btrfs_get_dev_zone_info(struct btrfs_device
*device
,
91 static inline void btrfs_destroy_dev_zone_info(struct btrfs_device
*device
) { }
93 static inline int btrfs_check_zoned_mode(const struct btrfs_fs_info
*fs_info
)
95 if (!btrfs_is_zoned(fs_info
))
98 btrfs_err(fs_info
, "zoned block devices support is not enabled");
102 static inline int btrfs_check_mountopts_zoned(struct btrfs_fs_info
*info
)
107 static inline int btrfs_sb_log_location_bdev(struct block_device
*bdev
,
108 int mirror
, int rw
, u64
*bytenr_ret
)
110 *bytenr_ret
= btrfs_sb_offset(mirror
);
114 static inline int btrfs_sb_log_location(struct btrfs_device
*device
, int mirror
,
115 int rw
, u64
*bytenr_ret
)
117 *bytenr_ret
= btrfs_sb_offset(mirror
);
121 static inline void btrfs_advance_sb_log(struct btrfs_device
*device
, int mirror
)
124 static inline int btrfs_reset_sb_log_zones(struct block_device
*bdev
, int mirror
)
129 static inline u64
btrfs_find_allocatable_zones(struct btrfs_device
*device
,
130 u64 hole_start
, u64 hole_end
,
136 static inline int btrfs_reset_device_zone(struct btrfs_device
*device
,
137 u64 physical
, u64 length
, u64
*bytes
)
143 static inline int btrfs_ensure_empty_zones(struct btrfs_device
*device
,
149 static inline int btrfs_load_block_group_zone_info(
150 struct btrfs_block_group
*cache
, bool new)
155 static inline void btrfs_calc_zone_unusable(struct btrfs_block_group
*cache
) { }
157 static inline void btrfs_redirty_list_add(struct btrfs_transaction
*trans
,
158 struct extent_buffer
*eb
) { }
159 static inline void btrfs_free_redirty_list(struct btrfs_transaction
*trans
) { }
161 static inline bool btrfs_use_zone_append(struct btrfs_inode
*inode
, u64 start
)
166 static inline void btrfs_record_physical_zoned(struct inode
*inode
,
167 u64 file_offset
, struct bio
*bio
)
171 static inline void btrfs_rewrite_logical_zoned(
172 struct btrfs_ordered_extent
*ordered
) { }
174 static inline bool btrfs_check_meta_write_pointer(struct btrfs_fs_info
*fs_info
,
175 struct extent_buffer
*eb
,
176 struct btrfs_block_group
**cache_ret
)
181 static inline void btrfs_revert_meta_write_pointer(
182 struct btrfs_block_group
*cache
,
183 struct extent_buffer
*eb
)
187 static inline int btrfs_zoned_issue_zeroout(struct btrfs_device
*device
,
188 u64 physical
, u64 length
)
193 static inline int btrfs_sync_zone_write_pointer(struct btrfs_device
*tgt_dev
,
194 u64 logical
, u64 physical_start
,
200 static inline struct btrfs_device
*btrfs_zoned_get_device(
201 struct btrfs_fs_info
*fs_info
,
202 u64 logical
, u64 length
)
204 return ERR_PTR(-EOPNOTSUPP
);
207 static inline void btrfs_clear_data_reloc_bg(struct btrfs_block_group
*bg
) { }
209 static inline void btrfs_free_zone_cache(struct btrfs_fs_info
*fs_info
) { }
212 static inline bool btrfs_dev_is_sequential(struct btrfs_device
*device
, u64 pos
)
214 struct btrfs_zoned_device_info
*zone_info
= device
->zone_info
;
219 return test_bit(pos
>> zone_info
->zone_size_shift
, zone_info
->seq_zones
);
222 static inline bool btrfs_dev_is_empty_zone(struct btrfs_device
*device
, u64 pos
)
224 struct btrfs_zoned_device_info
*zone_info
= device
->zone_info
;
229 return test_bit(pos
>> zone_info
->zone_size_shift
, zone_info
->empty_zones
);
232 static inline void btrfs_dev_set_empty_zone_bit(struct btrfs_device
*device
,
235 struct btrfs_zoned_device_info
*zone_info
= device
->zone_info
;
241 zno
= pos
>> zone_info
->zone_size_shift
;
243 set_bit(zno
, zone_info
->empty_zones
);
245 clear_bit(zno
, zone_info
->empty_zones
);
248 static inline void btrfs_dev_set_zone_empty(struct btrfs_device
*device
, u64 pos
)
250 btrfs_dev_set_empty_zone_bit(device
, pos
, true);
253 static inline void btrfs_dev_clear_zone_empty(struct btrfs_device
*device
, u64 pos
)
255 btrfs_dev_set_empty_zone_bit(device
, pos
, false);
258 static inline bool btrfs_check_device_zone_type(const struct btrfs_fs_info
*fs_info
,
259 struct block_device
*bdev
)
261 if (btrfs_is_zoned(fs_info
)) {
263 * We can allow a regular device on a zoned filesystem, because
264 * we will emulate the zoned capabilities.
266 if (!bdev_is_zoned(bdev
))
269 return fs_info
->zone_size
==
270 (bdev_zone_sectors(bdev
) << SECTOR_SHIFT
);
273 /* Do not allow Host Manged zoned device */
274 return bdev_zoned_model(bdev
) != BLK_ZONED_HM
;
277 static inline bool btrfs_check_super_location(struct btrfs_device
*device
, u64 pos
)
280 * On a non-zoned device, any address is OK. On a zoned device,
281 * non-SEQUENTIAL WRITE REQUIRED zones are capable.
283 return device
->zone_info
== NULL
|| !btrfs_dev_is_sequential(device
, pos
);
286 static inline bool btrfs_can_zone_reset(struct btrfs_device
*device
,
287 u64 physical
, u64 length
)
291 if (!btrfs_dev_is_sequential(device
, physical
))
294 zone_size
= device
->zone_info
->zone_size
;
295 if (!IS_ALIGNED(physical
, zone_size
) || !IS_ALIGNED(length
, zone_size
))
301 static inline void btrfs_zoned_meta_io_lock(struct btrfs_fs_info
*fs_info
)
303 if (!btrfs_is_zoned(fs_info
))
305 mutex_lock(&fs_info
->zoned_meta_io_lock
);
308 static inline void btrfs_zoned_meta_io_unlock(struct btrfs_fs_info
*fs_info
)
310 if (!btrfs_is_zoned(fs_info
))
312 mutex_unlock(&fs_info
->zoned_meta_io_lock
);
315 static inline void btrfs_clear_treelog_bg(struct btrfs_block_group
*bg
)
317 struct btrfs_fs_info
*fs_info
= bg
->fs_info
;
319 if (!btrfs_is_zoned(fs_info
))
322 spin_lock(&fs_info
->treelog_bg_lock
);
323 if (fs_info
->treelog_bg
== bg
->start
)
324 fs_info
->treelog_bg
= 0;
325 spin_unlock(&fs_info
->treelog_bg_lock
);
328 static inline void btrfs_zoned_data_reloc_lock(struct btrfs_inode
*inode
)
330 struct btrfs_root
*root
= inode
->root
;
332 if (btrfs_is_data_reloc_root(root
) && btrfs_is_zoned(root
->fs_info
))
333 mutex_lock(&root
->fs_info
->zoned_data_reloc_io_lock
);
336 static inline void btrfs_zoned_data_reloc_unlock(struct btrfs_inode
*inode
)
338 struct btrfs_root
*root
= inode
->root
;
340 if (btrfs_is_data_reloc_root(root
) && btrfs_is_zoned(root
->fs_info
))
341 mutex_unlock(&root
->fs_info
->zoned_data_reloc_io_lock
);