| xj | b04a402 | 2021-11-25 15:01:52 +0800 | [diff] [blame] | 1 | /* | 
|  | 2 | * Copyright (C) 2017 Western Digital Corporation or its affiliates. | 
|  | 3 | * | 
|  | 4 | * This file is released under the GPL. | 
|  | 5 | */ | 
|  | 6 |  | 
|  | 7 | #ifndef DM_ZONED_H | 
|  | 8 | #define DM_ZONED_H | 
|  | 9 |  | 
|  | 10 | #include <linux/types.h> | 
|  | 11 | #include <linux/blkdev.h> | 
|  | 12 | #include <linux/device-mapper.h> | 
|  | 13 | #include <linux/dm-kcopyd.h> | 
|  | 14 | #include <linux/list.h> | 
|  | 15 | #include <linux/spinlock.h> | 
|  | 16 | #include <linux/mutex.h> | 
|  | 17 | #include <linux/workqueue.h> | 
|  | 18 | #include <linux/rwsem.h> | 
|  | 19 | #include <linux/rbtree.h> | 
|  | 20 | #include <linux/radix-tree.h> | 
|  | 21 | #include <linux/shrinker.h> | 
|  | 22 |  | 
|  | 23 | /* | 
|  | 24 | * dm-zoned creates block devices with 4KB blocks, always. | 
|  | 25 | */ | 
|  | 26 | #define DMZ_BLOCK_SHIFT		12 | 
|  | 27 | #define DMZ_BLOCK_SIZE		(1 << DMZ_BLOCK_SHIFT) | 
|  | 28 | #define DMZ_BLOCK_MASK		(DMZ_BLOCK_SIZE - 1) | 
|  | 29 |  | 
|  | 30 | #define DMZ_BLOCK_SHIFT_BITS	(DMZ_BLOCK_SHIFT + 3) | 
|  | 31 | #define DMZ_BLOCK_SIZE_BITS	(1 << DMZ_BLOCK_SHIFT_BITS) | 
|  | 32 | #define DMZ_BLOCK_MASK_BITS	(DMZ_BLOCK_SIZE_BITS - 1) | 
|  | 33 |  | 
|  | 34 | #define DMZ_BLOCK_SECTORS_SHIFT	(DMZ_BLOCK_SHIFT - SECTOR_SHIFT) | 
|  | 35 | #define DMZ_BLOCK_SECTORS	(DMZ_BLOCK_SIZE >> SECTOR_SHIFT) | 
|  | 36 | #define DMZ_BLOCK_SECTORS_MASK	(DMZ_BLOCK_SECTORS - 1) | 
|  | 37 |  | 
|  | 38 | /* | 
|  | 39 | * 4KB block <-> 512B sector conversion. | 
|  | 40 | */ | 
|  | 41 | #define dmz_blk2sect(b)		((sector_t)(b) << DMZ_BLOCK_SECTORS_SHIFT) | 
|  | 42 | #define dmz_sect2blk(s)		((sector_t)(s) >> DMZ_BLOCK_SECTORS_SHIFT) | 
|  | 43 |  | 
|  | 44 | #define dmz_bio_block(bio)	dmz_sect2blk((bio)->bi_iter.bi_sector) | 
|  | 45 | #define dmz_bio_blocks(bio)	dmz_sect2blk(bio_sectors(bio)) | 
|  | 46 |  | 
|  | 47 | /* | 
|  | 48 | * Zoned block device information. | 
|  | 49 | */ | 
|  | 50 | struct dmz_dev { | 
|  | 51 | struct block_device	*bdev; | 
|  | 52 |  | 
|  | 53 | char			name[BDEVNAME_SIZE]; | 
|  | 54 |  | 
|  | 55 | sector_t		capacity; | 
|  | 56 |  | 
|  | 57 | unsigned int		nr_zones; | 
|  | 58 |  | 
|  | 59 | unsigned int		flags; | 
|  | 60 |  | 
|  | 61 | sector_t		zone_nr_sectors; | 
|  | 62 | unsigned int		zone_nr_sectors_shift; | 
|  | 63 |  | 
|  | 64 | sector_t		zone_nr_blocks; | 
|  | 65 | sector_t		zone_nr_blocks_shift; | 
|  | 66 | }; | 
|  | 67 |  | 
|  | 68 | #define dmz_bio_chunk(dev, bio)	((bio)->bi_iter.bi_sector >> \ | 
|  | 69 | (dev)->zone_nr_sectors_shift) | 
|  | 70 | #define dmz_chunk_block(dev, b)	((b) & ((dev)->zone_nr_blocks - 1)) | 
|  | 71 |  | 
|  | 72 | /* Device flags. */ | 
|  | 73 | #define DMZ_BDEV_DYING		(1 << 0) | 
|  | 74 | #define DMZ_CHECK_BDEV		(2 << 0) | 
|  | 75 |  | 
|  | 76 | /* | 
|  | 77 | * Zone descriptor. | 
|  | 78 | */ | 
|  | 79 | struct dm_zone { | 
|  | 80 | /* For listing the zone depending on its state */ | 
|  | 81 | struct list_head	link; | 
|  | 82 |  | 
|  | 83 | /* Zone type and state */ | 
|  | 84 | unsigned long		flags; | 
|  | 85 |  | 
|  | 86 | /* Zone activation reference count */ | 
|  | 87 | atomic_t		refcount; | 
|  | 88 |  | 
|  | 89 | /* Zone write pointer block (relative to the zone start block) */ | 
|  | 90 | unsigned int		wp_block; | 
|  | 91 |  | 
|  | 92 | /* Zone weight (number of valid blocks in the zone) */ | 
|  | 93 | unsigned int		weight; | 
|  | 94 |  | 
|  | 95 | /* The chunk that the zone maps */ | 
|  | 96 | unsigned int		chunk; | 
|  | 97 |  | 
|  | 98 | /* | 
|  | 99 | * For a sequential data zone, pointer to the random zone | 
|  | 100 | * used as a buffer for processing unaligned writes. | 
|  | 101 | * For a buffer zone, this points back to the data zone. | 
|  | 102 | */ | 
|  | 103 | struct dm_zone		*bzone; | 
|  | 104 | }; | 
|  | 105 |  | 
|  | 106 | /* | 
|  | 107 | * Zone flags. | 
|  | 108 | */ | 
|  | 109 | enum { | 
|  | 110 | /* Zone write type */ | 
|  | 111 | DMZ_RND, | 
|  | 112 | DMZ_SEQ, | 
|  | 113 |  | 
|  | 114 | /* Zone critical condition */ | 
|  | 115 | DMZ_OFFLINE, | 
|  | 116 | DMZ_READ_ONLY, | 
|  | 117 |  | 
|  | 118 | /* How the zone is being used */ | 
|  | 119 | DMZ_META, | 
|  | 120 | DMZ_DATA, | 
|  | 121 | DMZ_BUF, | 
|  | 122 |  | 
|  | 123 | /* Zone internal state */ | 
|  | 124 | DMZ_RECLAIM, | 
|  | 125 | DMZ_SEQ_WRITE_ERR, | 
|  | 126 | }; | 
|  | 127 |  | 
|  | 128 | /* | 
|  | 129 | * Zone data accessors. | 
|  | 130 | */ | 
|  | 131 | #define dmz_is_rnd(z)		test_bit(DMZ_RND, &(z)->flags) | 
|  | 132 | #define dmz_is_seq(z)		test_bit(DMZ_SEQ, &(z)->flags) | 
|  | 133 | #define dmz_is_empty(z)		((z)->wp_block == 0) | 
|  | 134 | #define dmz_is_offline(z)	test_bit(DMZ_OFFLINE, &(z)->flags) | 
|  | 135 | #define dmz_is_readonly(z)	test_bit(DMZ_READ_ONLY, &(z)->flags) | 
|  | 136 | #define dmz_in_reclaim(z)	test_bit(DMZ_RECLAIM, &(z)->flags) | 
|  | 137 | #define dmz_seq_write_err(z)	test_bit(DMZ_SEQ_WRITE_ERR, &(z)->flags) | 
|  | 138 |  | 
|  | 139 | #define dmz_is_meta(z)		test_bit(DMZ_META, &(z)->flags) | 
|  | 140 | #define dmz_is_buf(z)		test_bit(DMZ_BUF, &(z)->flags) | 
|  | 141 | #define dmz_is_data(z)		test_bit(DMZ_DATA, &(z)->flags) | 
|  | 142 |  | 
|  | 143 | #define dmz_weight(z)		((z)->weight) | 
|  | 144 |  | 
|  | 145 | /* | 
|  | 146 | * Message functions. | 
|  | 147 | */ | 
|  | 148 | #define dmz_dev_info(dev, format, args...)	\ | 
|  | 149 | DMINFO("(%s): " format, (dev)->name, ## args) | 
|  | 150 |  | 
|  | 151 | #define dmz_dev_err(dev, format, args...)	\ | 
|  | 152 | DMERR("(%s): " format, (dev)->name, ## args) | 
|  | 153 |  | 
|  | 154 | #define dmz_dev_warn(dev, format, args...)	\ | 
|  | 155 | DMWARN("(%s): " format, (dev)->name, ## args) | 
|  | 156 |  | 
|  | 157 | #define dmz_dev_debug(dev, format, args...)	\ | 
|  | 158 | DMDEBUG("(%s): " format, (dev)->name, ## args) | 
|  | 159 |  | 
|  | 160 | struct dmz_metadata; | 
|  | 161 | struct dmz_reclaim; | 
|  | 162 |  | 
|  | 163 | /* | 
|  | 164 | * Functions defined in dm-zoned-metadata.c | 
|  | 165 | */ | 
|  | 166 | int dmz_ctr_metadata(struct dmz_dev *dev, struct dmz_metadata **zmd); | 
|  | 167 | void dmz_dtr_metadata(struct dmz_metadata *zmd); | 
|  | 168 | int dmz_resume_metadata(struct dmz_metadata *zmd); | 
|  | 169 |  | 
|  | 170 | void dmz_lock_map(struct dmz_metadata *zmd); | 
|  | 171 | void dmz_unlock_map(struct dmz_metadata *zmd); | 
|  | 172 | void dmz_lock_metadata(struct dmz_metadata *zmd); | 
|  | 173 | void dmz_unlock_metadata(struct dmz_metadata *zmd); | 
|  | 174 | void dmz_lock_flush(struct dmz_metadata *zmd); | 
|  | 175 | void dmz_unlock_flush(struct dmz_metadata *zmd); | 
|  | 176 | int dmz_flush_metadata(struct dmz_metadata *zmd); | 
|  | 177 |  | 
|  | 178 | unsigned int dmz_id(struct dmz_metadata *zmd, struct dm_zone *zone); | 
|  | 179 | sector_t dmz_start_sect(struct dmz_metadata *zmd, struct dm_zone *zone); | 
|  | 180 | sector_t dmz_start_block(struct dmz_metadata *zmd, struct dm_zone *zone); | 
|  | 181 | unsigned int dmz_nr_chunks(struct dmz_metadata *zmd); | 
|  | 182 |  | 
|  | 183 | #define DMZ_ALLOC_RND		0x01 | 
|  | 184 | #define DMZ_ALLOC_RECLAIM	0x02 | 
|  | 185 |  | 
|  | 186 | struct dm_zone *dmz_alloc_zone(struct dmz_metadata *zmd, unsigned long flags); | 
|  | 187 | void dmz_free_zone(struct dmz_metadata *zmd, struct dm_zone *zone); | 
|  | 188 |  | 
|  | 189 | void dmz_map_zone(struct dmz_metadata *zmd, struct dm_zone *zone, | 
|  | 190 | unsigned int chunk); | 
|  | 191 | void dmz_unmap_zone(struct dmz_metadata *zmd, struct dm_zone *zone); | 
|  | 192 | unsigned int dmz_nr_rnd_zones(struct dmz_metadata *zmd); | 
|  | 193 | unsigned int dmz_nr_unmap_rnd_zones(struct dmz_metadata *zmd); | 
|  | 194 |  | 
|  | 195 | /* | 
|  | 196 | * Activate a zone (increment its reference count). | 
|  | 197 | */ | 
|  | 198 | static inline void dmz_activate_zone(struct dm_zone *zone) | 
|  | 199 | { | 
|  | 200 | atomic_inc(&zone->refcount); | 
|  | 201 | } | 
|  | 202 |  | 
|  | 203 | /* | 
|  | 204 | * Deactivate a zone. This decrement the zone reference counter | 
|  | 205 | * indicating that all BIOs to the zone have completed when the count is 0. | 
|  | 206 | */ | 
|  | 207 | static inline void dmz_deactivate_zone(struct dm_zone *zone) | 
|  | 208 | { | 
|  | 209 | atomic_dec(&zone->refcount); | 
|  | 210 | } | 
|  | 211 |  | 
|  | 212 | /* | 
|  | 213 | * Test if a zone is active, that is, has a refcount > 0. | 
|  | 214 | */ | 
|  | 215 | static inline bool dmz_is_active(struct dm_zone *zone) | 
|  | 216 | { | 
|  | 217 | return atomic_read(&zone->refcount); | 
|  | 218 | } | 
|  | 219 |  | 
|  | 220 | int dmz_lock_zone_reclaim(struct dm_zone *zone); | 
|  | 221 | void dmz_unlock_zone_reclaim(struct dm_zone *zone); | 
|  | 222 | struct dm_zone *dmz_get_zone_for_reclaim(struct dmz_metadata *zmd); | 
|  | 223 |  | 
|  | 224 | struct dm_zone *dmz_get_chunk_mapping(struct dmz_metadata *zmd, | 
|  | 225 | unsigned int chunk, int op); | 
|  | 226 | void dmz_put_chunk_mapping(struct dmz_metadata *zmd, struct dm_zone *zone); | 
|  | 227 | struct dm_zone *dmz_get_chunk_buffer(struct dmz_metadata *zmd, | 
|  | 228 | struct dm_zone *dzone); | 
|  | 229 |  | 
|  | 230 | int dmz_validate_blocks(struct dmz_metadata *zmd, struct dm_zone *zone, | 
|  | 231 | sector_t chunk_block, unsigned int nr_blocks); | 
|  | 232 | int dmz_invalidate_blocks(struct dmz_metadata *zmd, struct dm_zone *zone, | 
|  | 233 | sector_t chunk_block, unsigned int nr_blocks); | 
|  | 234 | int dmz_block_valid(struct dmz_metadata *zmd, struct dm_zone *zone, | 
|  | 235 | sector_t chunk_block); | 
|  | 236 | int dmz_first_valid_block(struct dmz_metadata *zmd, struct dm_zone *zone, | 
|  | 237 | sector_t *chunk_block); | 
|  | 238 | int dmz_copy_valid_blocks(struct dmz_metadata *zmd, struct dm_zone *from_zone, | 
|  | 239 | struct dm_zone *to_zone); | 
|  | 240 | int dmz_merge_valid_blocks(struct dmz_metadata *zmd, struct dm_zone *from_zone, | 
|  | 241 | struct dm_zone *to_zone, sector_t chunk_block); | 
|  | 242 |  | 
|  | 243 | /* | 
|  | 244 | * Functions defined in dm-zoned-reclaim.c | 
|  | 245 | */ | 
|  | 246 | int dmz_ctr_reclaim(struct dmz_dev *dev, struct dmz_metadata *zmd, | 
|  | 247 | struct dmz_reclaim **zrc); | 
|  | 248 | void dmz_dtr_reclaim(struct dmz_reclaim *zrc); | 
|  | 249 | void dmz_suspend_reclaim(struct dmz_reclaim *zrc); | 
|  | 250 | void dmz_resume_reclaim(struct dmz_reclaim *zrc); | 
|  | 251 | void dmz_reclaim_bio_acc(struct dmz_reclaim *zrc); | 
|  | 252 | void dmz_schedule_reclaim(struct dmz_reclaim *zrc); | 
|  | 253 |  | 
|  | 254 | /* | 
|  | 255 | * Functions defined in dm-zoned-target.c | 
|  | 256 | */ | 
|  | 257 | bool dmz_bdev_is_dying(struct dmz_dev *dmz_dev); | 
|  | 258 | bool dmz_check_bdev(struct dmz_dev *dmz_dev); | 
|  | 259 |  | 
|  | 260 | #endif /* DM_ZONED_H */ |