1 /* SPDX-License-Identifier: GPL-2.0 */
6 #include <linux/types.h>
7 #include <linux/atomic.h>
8 #include <linux/blkdev.h>
9 #include <linux/blkzoned.h>
10 #include <linux/errno.h>
11 #include <linux/spinlock.h>
12 #include <linux/mutex.h>
16 #include "block-group.h"
17 #include "btrfs_inode.h"
23 struct btrfs_ordered_extent
;
25 struct btrfs_space_info
;
26 struct btrfs_eb_write_context
;
27 struct btrfs_fs_devices
;
29 #define BTRFS_DEFAULT_RECLAIM_THRESH (75)
31 struct btrfs_zoned_device_info
{
33 * Number of zones, zone size and types of zones if bdev is a
39 unsigned int max_active_zones
;
41 * Reserved active zones for one metadata and one system block group.
42 * It can vary per-device depending on the allocation status.
44 int reserved_active_zones
;
45 atomic_t active_zones_left
;
46 unsigned long *seq_zones
;
47 unsigned long *empty_zones
;
48 unsigned long *active_zones
;
49 struct blk_zone
*zone_cache
;
50 struct blk_zone sb_zones
[2 * BTRFS_SUPER_MIRROR_MAX
];
53 void btrfs_finish_ordered_zoned(struct btrfs_ordered_extent
*ordered
);
55 #ifdef CONFIG_BLK_DEV_ZONED
56 int btrfs_get_dev_zone_info_all_devices(struct btrfs_fs_info
*fs_info
);
57 int btrfs_get_dev_zone_info(struct btrfs_device
*device
, bool populate_cache
);
58 void btrfs_destroy_dev_zone_info(struct btrfs_device
*device
);
59 struct btrfs_zoned_device_info
*btrfs_clone_dev_zone_info(struct btrfs_device
*orig_dev
);
60 int btrfs_check_zoned_mode(struct btrfs_fs_info
*fs_info
);
61 int btrfs_check_mountopts_zoned(const struct btrfs_fs_info
*info
,
62 unsigned long long *mount_opt
);
63 int btrfs_sb_log_location_bdev(struct block_device
*bdev
, int mirror
, int rw
,
65 int btrfs_sb_log_location(struct btrfs_device
*device
, int mirror
, int rw
,
67 int btrfs_advance_sb_log(struct btrfs_device
*device
, int mirror
);
68 int btrfs_reset_sb_log_zones(struct block_device
*bdev
, int mirror
);
69 u64
btrfs_find_allocatable_zones(struct btrfs_device
*device
, u64 hole_start
,
70 u64 hole_end
, u64 num_bytes
);
71 int btrfs_reset_device_zone(struct btrfs_device
*device
, u64 physical
,
72 u64 length
, u64
*bytes
);
73 int btrfs_ensure_empty_zones(struct btrfs_device
*device
, u64 start
, u64 size
);
74 int btrfs_load_block_group_zone_info(struct btrfs_block_group
*cache
, bool new);
75 void btrfs_calc_zone_unusable(struct btrfs_block_group
*cache
);
76 bool btrfs_use_zone_append(struct btrfs_bio
*bbio
);
77 void btrfs_record_physical_zoned(struct btrfs_bio
*bbio
);
78 int btrfs_check_meta_write_pointer(struct btrfs_fs_info
*fs_info
,
79 struct btrfs_eb_write_context
*ctx
);
80 int btrfs_zoned_issue_zeroout(struct btrfs_device
*device
, u64 physical
, u64 length
);
81 int btrfs_sync_zone_write_pointer(struct btrfs_device
*tgt_dev
, u64 logical
,
82 u64 physical_start
, u64 physical_pos
);
83 bool btrfs_zone_activate(struct btrfs_block_group
*block_group
);
84 int btrfs_zone_finish(struct btrfs_block_group
*block_group
);
85 bool btrfs_can_activate_zone(struct btrfs_fs_devices
*fs_devices
, u64 flags
);
86 void btrfs_zone_finish_endio(struct btrfs_fs_info
*fs_info
, u64 logical
,
88 void btrfs_schedule_zone_finish_bg(struct btrfs_block_group
*bg
,
89 struct extent_buffer
*eb
);
90 void btrfs_clear_data_reloc_bg(struct btrfs_block_group
*bg
);
91 void btrfs_free_zone_cache(struct btrfs_fs_info
*fs_info
);
92 bool btrfs_zoned_should_reclaim(const struct btrfs_fs_info
*fs_info
);
93 void btrfs_zoned_release_data_reloc_bg(struct btrfs_fs_info
*fs_info
, u64 logical
,
95 int btrfs_zone_finish_one_bg(struct btrfs_fs_info
*fs_info
);
96 int btrfs_zoned_activate_one_bg(struct btrfs_fs_info
*fs_info
,
97 struct btrfs_space_info
*space_info
, bool do_finish
);
98 void btrfs_check_active_zone_reservation(struct btrfs_fs_info
*fs_info
);
99 #else /* CONFIG_BLK_DEV_ZONED */
101 static inline int btrfs_get_dev_zone_info_all_devices(struct btrfs_fs_info
*fs_info
)
106 static inline int btrfs_get_dev_zone_info(struct btrfs_device
*device
,
112 static inline void btrfs_destroy_dev_zone_info(struct btrfs_device
*device
) { }
115 * In case the kernel is compiled without CONFIG_BLK_DEV_ZONED we'll never call
116 * into btrfs_clone_dev_zone_info() so it's safe to return NULL here.
118 static inline struct btrfs_zoned_device_info
*btrfs_clone_dev_zone_info(
119 struct btrfs_device
*orig_dev
)
124 static inline int btrfs_check_zoned_mode(const struct btrfs_fs_info
*fs_info
)
126 if (!btrfs_is_zoned(fs_info
))
129 btrfs_err(fs_info
, "zoned block devices support is not enabled");
133 static inline int btrfs_check_mountopts_zoned(const struct btrfs_fs_info
*info
,
134 unsigned long long *mount_opt
)
139 static inline int btrfs_sb_log_location_bdev(struct block_device
*bdev
,
140 int mirror
, int rw
, u64
*bytenr_ret
)
142 *bytenr_ret
= btrfs_sb_offset(mirror
);
146 static inline int btrfs_sb_log_location(struct btrfs_device
*device
, int mirror
,
147 int rw
, u64
*bytenr_ret
)
149 *bytenr_ret
= btrfs_sb_offset(mirror
);
153 static inline int btrfs_advance_sb_log(struct btrfs_device
*device
, int mirror
)
158 static inline int btrfs_reset_sb_log_zones(struct block_device
*bdev
, int mirror
)
163 static inline u64
btrfs_find_allocatable_zones(struct btrfs_device
*device
,
164 u64 hole_start
, u64 hole_end
,
170 static inline int btrfs_reset_device_zone(struct btrfs_device
*device
,
171 u64 physical
, u64 length
, u64
*bytes
)
177 static inline int btrfs_ensure_empty_zones(struct btrfs_device
*device
,
183 static inline int btrfs_load_block_group_zone_info(
184 struct btrfs_block_group
*cache
, bool new)
189 static inline void btrfs_calc_zone_unusable(struct btrfs_block_group
*cache
) { }
191 static inline bool btrfs_use_zone_append(struct btrfs_bio
*bbio
)
196 static inline void btrfs_record_physical_zoned(struct btrfs_bio
*bbio
)
200 static inline int btrfs_check_meta_write_pointer(struct btrfs_fs_info
*fs_info
,
201 struct btrfs_eb_write_context
*ctx
)
206 static inline int btrfs_zoned_issue_zeroout(struct btrfs_device
*device
,
207 u64 physical
, u64 length
)
212 static inline int btrfs_sync_zone_write_pointer(struct btrfs_device
*tgt_dev
,
213 u64 logical
, u64 physical_start
,
219 static inline bool btrfs_zone_activate(struct btrfs_block_group
*block_group
)
224 static inline int btrfs_zone_finish(struct btrfs_block_group
*block_group
)
229 static inline bool btrfs_can_activate_zone(struct btrfs_fs_devices
*fs_devices
,
235 static inline void btrfs_zone_finish_endio(struct btrfs_fs_info
*fs_info
,
236 u64 logical
, u64 length
) { }
238 static inline void btrfs_schedule_zone_finish_bg(struct btrfs_block_group
*bg
,
239 struct extent_buffer
*eb
) { }
241 static inline void btrfs_clear_data_reloc_bg(struct btrfs_block_group
*bg
) { }
243 static inline void btrfs_free_zone_cache(struct btrfs_fs_info
*fs_info
) { }
245 static inline bool btrfs_zoned_should_reclaim(const struct btrfs_fs_info
*fs_info
)
250 static inline void btrfs_zoned_release_data_reloc_bg(struct btrfs_fs_info
*fs_info
,
251 u64 logical
, u64 length
) { }
253 static inline int btrfs_zone_finish_one_bg(struct btrfs_fs_info
*fs_info
)
258 static inline int btrfs_zoned_activate_one_bg(struct btrfs_fs_info
*fs_info
,
259 struct btrfs_space_info
*space_info
,
262 /* Consider all the block groups are active */
266 static inline void btrfs_check_active_zone_reservation(struct btrfs_fs_info
*fs_info
) { }
270 static inline bool btrfs_dev_is_sequential(struct btrfs_device
*device
, u64 pos
)
272 struct btrfs_zoned_device_info
*zone_info
= device
->zone_info
;
277 return test_bit(pos
>> zone_info
->zone_size_shift
, zone_info
->seq_zones
);
280 static inline bool btrfs_dev_is_empty_zone(struct btrfs_device
*device
, u64 pos
)
282 struct btrfs_zoned_device_info
*zone_info
= device
->zone_info
;
287 return test_bit(pos
>> zone_info
->zone_size_shift
, zone_info
->empty_zones
);
290 static inline void btrfs_dev_set_empty_zone_bit(struct btrfs_device
*device
,
293 struct btrfs_zoned_device_info
*zone_info
= device
->zone_info
;
299 zno
= pos
>> zone_info
->zone_size_shift
;
301 set_bit(zno
, zone_info
->empty_zones
);
303 clear_bit(zno
, zone_info
->empty_zones
);
306 static inline void btrfs_dev_set_zone_empty(struct btrfs_device
*device
, u64 pos
)
308 btrfs_dev_set_empty_zone_bit(device
, pos
, true);
311 static inline void btrfs_dev_clear_zone_empty(struct btrfs_device
*device
, u64 pos
)
313 btrfs_dev_set_empty_zone_bit(device
, pos
, false);
316 static inline bool btrfs_check_device_zone_type(const struct btrfs_fs_info
*fs_info
,
317 struct block_device
*bdev
)
319 if (btrfs_is_zoned(fs_info
)) {
321 * We can allow a regular device on a zoned filesystem, because
322 * we will emulate the zoned capabilities.
324 if (!bdev_is_zoned(bdev
))
327 return fs_info
->zone_size
==
328 (bdev_zone_sectors(bdev
) << SECTOR_SHIFT
);
331 /* Do not allow Host Managed zoned device. */
332 return !bdev_is_zoned(bdev
);
335 static inline bool btrfs_check_super_location(struct btrfs_device
*device
, u64 pos
)
338 * On a non-zoned device, any address is OK. On a zoned device,
339 * non-SEQUENTIAL WRITE REQUIRED zones are capable.
341 return device
->zone_info
== NULL
|| !btrfs_dev_is_sequential(device
, pos
);
344 static inline bool btrfs_can_zone_reset(struct btrfs_device
*device
,
345 u64 physical
, u64 length
)
349 if (!btrfs_dev_is_sequential(device
, physical
))
352 zone_size
= device
->zone_info
->zone_size
;
353 if (!IS_ALIGNED(physical
, zone_size
) || !IS_ALIGNED(length
, zone_size
))
359 static inline void btrfs_zoned_meta_io_lock(struct btrfs_fs_info
*fs_info
)
361 if (!btrfs_is_zoned(fs_info
))
363 mutex_lock(&fs_info
->zoned_meta_io_lock
);
366 static inline void btrfs_zoned_meta_io_unlock(struct btrfs_fs_info
*fs_info
)
368 if (!btrfs_is_zoned(fs_info
))
370 mutex_unlock(&fs_info
->zoned_meta_io_lock
);
373 static inline void btrfs_clear_treelog_bg(struct btrfs_block_group
*bg
)
375 struct btrfs_fs_info
*fs_info
= bg
->fs_info
;
377 if (!btrfs_is_zoned(fs_info
))
380 spin_lock(&fs_info
->treelog_bg_lock
);
381 if (fs_info
->treelog_bg
== bg
->start
)
382 fs_info
->treelog_bg
= 0;
383 spin_unlock(&fs_info
->treelog_bg_lock
);
386 static inline void btrfs_zoned_data_reloc_lock(struct btrfs_inode
*inode
)
388 struct btrfs_root
*root
= inode
->root
;
390 if (btrfs_is_data_reloc_root(root
) && btrfs_is_zoned(root
->fs_info
))
391 mutex_lock(&root
->fs_info
->zoned_data_reloc_io_lock
);
394 static inline void btrfs_zoned_data_reloc_unlock(struct btrfs_inode
*inode
)
396 struct btrfs_root
*root
= inode
->root
;
398 if (btrfs_is_data_reloc_root(root
) && btrfs_is_zoned(root
->fs_info
))
399 mutex_unlock(&root
->fs_info
->zoned_data_reloc_io_lock
);
402 static inline bool btrfs_zoned_bg_is_full(const struct btrfs_block_group
*bg
)
404 ASSERT(btrfs_is_zoned(bg
->fs_info
));
405 return (bg
->alloc_offset
== bg
->zone_capacity
);