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