Naohiro Aota | 5b31646 | 2020-11-10 20:26:07 +0900 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
| 2 | |
| 3 | #ifndef BTRFS_ZONED_H |
| 4 | #define BTRFS_ZONED_H |
| 5 | |
| 6 | #include <linux/types.h> |
Naohiro Aota | b70f509 | 2020-11-10 20:26:08 +0900 | [diff] [blame] | 7 | #include <linux/blkdev.h> |
Naohiro Aota | 5b31646 | 2020-11-10 20:26:07 +0900 | [diff] [blame] | 8 | |
| 9 | struct btrfs_zoned_device_info { |
| 10 | /* |
| 11 | * Number of zones, zone size and types of zones if bdev is a |
| 12 | * zoned block device. |
| 13 | */ |
| 14 | u64 zone_size; |
| 15 | u8 zone_size_shift; |
Naohiro Aota | 862931c | 2020-11-10 20:26:09 +0900 | [diff] [blame] | 16 | u64 max_zone_append_size; |
Naohiro Aota | 5b31646 | 2020-11-10 20:26:07 +0900 | [diff] [blame] | 17 | u32 nr_zones; |
| 18 | unsigned long *seq_zones; |
| 19 | unsigned long *empty_zones; |
| 20 | }; |
| 21 | |
| 22 | #ifdef CONFIG_BLK_DEV_ZONED |
| 23 | int btrfs_get_dev_zone(struct btrfs_device *device, u64 pos, |
| 24 | struct blk_zone *zone); |
| 25 | int btrfs_get_dev_zone_info(struct btrfs_device *device); |
| 26 | void btrfs_destroy_dev_zone_info(struct btrfs_device *device); |
Naohiro Aota | b70f509 | 2020-11-10 20:26:08 +0900 | [diff] [blame] | 27 | int btrfs_check_zoned_mode(struct btrfs_fs_info *fs_info); |
Naohiro Aota | 5d1ab66 | 2020-11-10 20:26:10 +0900 | [diff] [blame] | 28 | int btrfs_check_mountopts_zoned(struct btrfs_fs_info *info); |
Naohiro Aota | 5b31646 | 2020-11-10 20:26:07 +0900 | [diff] [blame] | 29 | #else /* CONFIG_BLK_DEV_ZONED */ |
| 30 | static inline int btrfs_get_dev_zone(struct btrfs_device *device, u64 pos, |
| 31 | struct blk_zone *zone) |
| 32 | { |
| 33 | return 0; |
| 34 | } |
| 35 | |
| 36 | static inline int btrfs_get_dev_zone_info(struct btrfs_device *device) |
| 37 | { |
| 38 | return 0; |
| 39 | } |
| 40 | |
| 41 | static inline void btrfs_destroy_dev_zone_info(struct btrfs_device *device) { } |
| 42 | |
Naohiro Aota | b70f509 | 2020-11-10 20:26:08 +0900 | [diff] [blame] | 43 | static inline int btrfs_check_zoned_mode(const struct btrfs_fs_info *fs_info) |
| 44 | { |
| 45 | if (!btrfs_is_zoned(fs_info)) |
| 46 | return 0; |
| 47 | |
| 48 | btrfs_err(fs_info, "zoned block devices support is not enabled"); |
| 49 | return -EOPNOTSUPP; |
| 50 | } |
| 51 | |
Naohiro Aota | 5d1ab66 | 2020-11-10 20:26:10 +0900 | [diff] [blame] | 52 | static inline int btrfs_check_mountopts_zoned(struct btrfs_fs_info *info) |
| 53 | { |
| 54 | return 0; |
| 55 | } |
| 56 | |
Naohiro Aota | 5b31646 | 2020-11-10 20:26:07 +0900 | [diff] [blame] | 57 | #endif |
| 58 | |
| 59 | static inline bool btrfs_dev_is_sequential(struct btrfs_device *device, u64 pos) |
| 60 | { |
| 61 | struct btrfs_zoned_device_info *zone_info = device->zone_info; |
| 62 | |
| 63 | if (!zone_info) |
| 64 | return false; |
| 65 | |
| 66 | return test_bit(pos >> zone_info->zone_size_shift, zone_info->seq_zones); |
| 67 | } |
| 68 | |
| 69 | static inline bool btrfs_dev_is_empty_zone(struct btrfs_device *device, u64 pos) |
| 70 | { |
| 71 | struct btrfs_zoned_device_info *zone_info = device->zone_info; |
| 72 | |
| 73 | if (!zone_info) |
| 74 | return true; |
| 75 | |
| 76 | return test_bit(pos >> zone_info->zone_size_shift, zone_info->empty_zones); |
| 77 | } |
| 78 | |
| 79 | static inline void btrfs_dev_set_empty_zone_bit(struct btrfs_device *device, |
| 80 | u64 pos, bool set) |
| 81 | { |
| 82 | struct btrfs_zoned_device_info *zone_info = device->zone_info; |
| 83 | unsigned int zno; |
| 84 | |
| 85 | if (!zone_info) |
| 86 | return; |
| 87 | |
| 88 | zno = pos >> zone_info->zone_size_shift; |
| 89 | if (set) |
| 90 | set_bit(zno, zone_info->empty_zones); |
| 91 | else |
| 92 | clear_bit(zno, zone_info->empty_zones); |
| 93 | } |
| 94 | |
| 95 | static inline void btrfs_dev_set_zone_empty(struct btrfs_device *device, u64 pos) |
| 96 | { |
| 97 | btrfs_dev_set_empty_zone_bit(device, pos, true); |
| 98 | } |
| 99 | |
| 100 | static inline void btrfs_dev_clear_zone_empty(struct btrfs_device *device, u64 pos) |
| 101 | { |
| 102 | btrfs_dev_set_empty_zone_bit(device, pos, false); |
| 103 | } |
| 104 | |
Naohiro Aota | b70f509 | 2020-11-10 20:26:08 +0900 | [diff] [blame] | 105 | static inline bool btrfs_check_device_zone_type(const struct btrfs_fs_info *fs_info, |
| 106 | struct block_device *bdev) |
| 107 | { |
| 108 | u64 zone_size; |
| 109 | |
| 110 | if (btrfs_is_zoned(fs_info)) { |
| 111 | zone_size = bdev_zone_sectors(bdev) << SECTOR_SHIFT; |
| 112 | /* Do not allow non-zoned device */ |
| 113 | return bdev_is_zoned(bdev) && fs_info->zone_size == zone_size; |
| 114 | } |
| 115 | |
| 116 | /* Do not allow Host Manged zoned device */ |
| 117 | return bdev_zoned_model(bdev) != BLK_ZONED_HM; |
| 118 | } |
| 119 | |
Naohiro Aota | 5b31646 | 2020-11-10 20:26:07 +0900 | [diff] [blame] | 120 | #endif |