Dmitry Fomichev | bae9a0a | 2019-08-02 15:02:50 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
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 | #include "dm-zoned.h" |
| 9 | |
| 10 | #include <linux/module.h> |
| 11 | #include <linux/crc32.h> |
Damien Le Moal | bd976e5 | 2019-07-01 14:09:16 +0900 | [diff] [blame] | 12 | #include <linux/sched/mm.h> |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 13 | |
| 14 | #define DM_MSG_PREFIX "zoned metadata" |
| 15 | |
| 16 | /* |
| 17 | * Metadata version. |
| 18 | */ |
Hannes Reinecke | bd5c403 | 2020-05-11 10:24:30 +0200 | [diff] [blame] | 19 | #define DMZ_META_VER 2 |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 20 | |
| 21 | /* |
| 22 | * On-disk super block magic. |
| 23 | */ |
| 24 | #define DMZ_MAGIC ((((unsigned int)('D')) << 24) | \ |
| 25 | (((unsigned int)('Z')) << 16) | \ |
| 26 | (((unsigned int)('B')) << 8) | \ |
| 27 | ((unsigned int)('D'))) |
| 28 | |
| 29 | /* |
| 30 | * On disk super block. |
| 31 | * This uses only 512 B but uses on disk a full 4KB block. This block is |
| 32 | * followed on disk by the mapping table of chunks to zones and the bitmap |
| 33 | * blocks indicating zone block validity. |
| 34 | * The overall resulting metadata format is: |
| 35 | * (1) Super block (1 block) |
| 36 | * (2) Chunk mapping table (nr_map_blocks) |
| 37 | * (3) Bitmap blocks (nr_bitmap_blocks) |
Dmitry Fomichev | ad1bd57 | 2019-08-02 15:02:51 -0700 | [diff] [blame] | 38 | * All metadata blocks are stored in conventional zones, starting from |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 39 | * the first conventional zone found on disk. |
| 40 | */ |
| 41 | struct dmz_super { |
| 42 | /* Magic number */ |
| 43 | __le32 magic; /* 4 */ |
| 44 | |
| 45 | /* Metadata version number */ |
| 46 | __le32 version; /* 8 */ |
| 47 | |
| 48 | /* Generation number */ |
| 49 | __le64 gen; /* 16 */ |
| 50 | |
| 51 | /* This block number */ |
| 52 | __le64 sb_block; /* 24 */ |
| 53 | |
| 54 | /* The number of metadata blocks, including this super block */ |
| 55 | __le32 nr_meta_blocks; /* 28 */ |
| 56 | |
| 57 | /* The number of sequential zones reserved for reclaim */ |
| 58 | __le32 nr_reserved_seq; /* 32 */ |
| 59 | |
| 60 | /* The number of entries in the mapping table */ |
| 61 | __le32 nr_chunks; /* 36 */ |
| 62 | |
| 63 | /* The number of blocks used for the chunk mapping table */ |
| 64 | __le32 nr_map_blocks; /* 40 */ |
| 65 | |
| 66 | /* The number of blocks used for the block bitmaps */ |
| 67 | __le32 nr_bitmap_blocks; /* 44 */ |
| 68 | |
| 69 | /* Checksum */ |
| 70 | __le32 crc; /* 48 */ |
| 71 | |
Hannes Reinecke | bd5c403 | 2020-05-11 10:24:30 +0200 | [diff] [blame] | 72 | /* DM-Zoned label */ |
| 73 | u8 dmz_label[32]; /* 80 */ |
| 74 | |
| 75 | /* DM-Zoned UUID */ |
| 76 | u8 dmz_uuid[16]; /* 96 */ |
| 77 | |
| 78 | /* Device UUID */ |
| 79 | u8 dev_uuid[16]; /* 112 */ |
| 80 | |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 81 | /* Padding to full 512B sector */ |
Hannes Reinecke | bd5c403 | 2020-05-11 10:24:30 +0200 | [diff] [blame] | 82 | u8 reserved[400]; /* 512 */ |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 83 | }; |
| 84 | |
| 85 | /* |
| 86 | * Chunk mapping entry: entries are indexed by chunk number |
| 87 | * and give the zone ID (dzone_id) mapping the chunk on disk. |
| 88 | * This zone may be sequential or random. If it is a sequential |
| 89 | * zone, a second zone (bzone_id) used as a write buffer may |
| 90 | * also be specified. This second zone will always be a randomly |
| 91 | * writeable zone. |
| 92 | */ |
| 93 | struct dmz_map { |
| 94 | __le32 dzone_id; |
| 95 | __le32 bzone_id; |
| 96 | }; |
| 97 | |
| 98 | /* |
| 99 | * Chunk mapping table metadata: 512 8-bytes entries per 4KB block. |
| 100 | */ |
| 101 | #define DMZ_MAP_ENTRIES (DMZ_BLOCK_SIZE / sizeof(struct dmz_map)) |
| 102 | #define DMZ_MAP_ENTRIES_SHIFT (ilog2(DMZ_MAP_ENTRIES)) |
| 103 | #define DMZ_MAP_ENTRIES_MASK (DMZ_MAP_ENTRIES - 1) |
| 104 | #define DMZ_MAP_UNMAPPED UINT_MAX |
| 105 | |
| 106 | /* |
| 107 | * Meta data block descriptor (for cached metadata blocks). |
| 108 | */ |
| 109 | struct dmz_mblock { |
| 110 | struct rb_node node; |
| 111 | struct list_head link; |
| 112 | sector_t no; |
Damien Le Moal | 33c2865 | 2018-10-17 18:05:07 +0900 | [diff] [blame] | 113 | unsigned int ref; |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 114 | unsigned long state; |
| 115 | struct page *page; |
| 116 | void *data; |
| 117 | }; |
| 118 | |
| 119 | /* |
| 120 | * Metadata block state flags. |
| 121 | */ |
| 122 | enum { |
| 123 | DMZ_META_DIRTY, |
| 124 | DMZ_META_READING, |
| 125 | DMZ_META_WRITING, |
| 126 | DMZ_META_ERROR, |
| 127 | }; |
| 128 | |
| 129 | /* |
| 130 | * Super block information (one per metadata set). |
| 131 | */ |
| 132 | struct dmz_sb { |
| 133 | sector_t block; |
Hannes Reinecke | bf28a3b | 2020-05-11 10:24:20 +0200 | [diff] [blame] | 134 | struct dmz_dev *dev; |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 135 | struct dmz_mblock *mblk; |
| 136 | struct dmz_super *sb; |
Hannes Reinecke | 735bd7e4 | 2020-05-11 10:24:19 +0200 | [diff] [blame] | 137 | struct dm_zone *zone; |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 138 | }; |
| 139 | |
| 140 | /* |
| 141 | * In-memory metadata. |
| 142 | */ |
| 143 | struct dmz_metadata { |
| 144 | struct dmz_dev *dev; |
Hannes Reinecke | bd5c403 | 2020-05-11 10:24:30 +0200 | [diff] [blame] | 145 | unsigned int nr_devs; |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 146 | |
Hannes Reinecke | 2234e73 | 2020-05-11 10:24:22 +0200 | [diff] [blame] | 147 | char devname[BDEVNAME_SIZE]; |
Hannes Reinecke | bd5c403 | 2020-05-11 10:24:30 +0200 | [diff] [blame] | 148 | char label[BDEVNAME_SIZE]; |
| 149 | uuid_t uuid; |
Hannes Reinecke | 2234e73 | 2020-05-11 10:24:22 +0200 | [diff] [blame] | 150 | |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 151 | sector_t zone_bitmap_size; |
| 152 | unsigned int zone_nr_bitmap_blocks; |
Dmitry Fomichev | b399629 | 2019-12-23 17:05:46 -0800 | [diff] [blame] | 153 | unsigned int zone_bits_per_mblk; |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 154 | |
Hannes Reinecke | 3682056 | 2020-05-11 10:24:21 +0200 | [diff] [blame] | 155 | sector_t zone_nr_blocks; |
| 156 | sector_t zone_nr_blocks_shift; |
| 157 | |
| 158 | sector_t zone_nr_sectors; |
| 159 | sector_t zone_nr_sectors_shift; |
| 160 | |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 161 | unsigned int nr_bitmap_blocks; |
| 162 | unsigned int nr_map_blocks; |
| 163 | |
Hannes Reinecke | 3682056 | 2020-05-11 10:24:21 +0200 | [diff] [blame] | 164 | unsigned int nr_zones; |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 165 | unsigned int nr_useable_zones; |
| 166 | unsigned int nr_meta_blocks; |
| 167 | unsigned int nr_meta_zones; |
| 168 | unsigned int nr_data_zones; |
Hannes Reinecke | 34f5aff | 2020-05-19 10:14:20 +0200 | [diff] [blame] | 169 | unsigned int nr_cache_zones; |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 170 | unsigned int nr_rnd_zones; |
| 171 | unsigned int nr_reserved_seq; |
| 172 | unsigned int nr_chunks; |
| 173 | |
| 174 | /* Zone information array */ |
| 175 | struct dm_zone *zones; |
| 176 | |
Hannes Reinecke | bd5c403 | 2020-05-11 10:24:30 +0200 | [diff] [blame] | 177 | struct dmz_sb sb[3]; |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 178 | unsigned int mblk_primary; |
Hannes Reinecke | bd5c403 | 2020-05-11 10:24:30 +0200 | [diff] [blame] | 179 | unsigned int sb_version; |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 180 | u64 sb_gen; |
| 181 | unsigned int min_nr_mblks; |
| 182 | unsigned int max_nr_mblks; |
| 183 | atomic_t nr_mblks; |
| 184 | struct rw_semaphore mblk_sem; |
| 185 | struct mutex mblk_flush_lock; |
| 186 | spinlock_t mblk_lock; |
| 187 | struct rb_root mblk_rbtree; |
| 188 | struct list_head mblk_lru_list; |
| 189 | struct list_head mblk_dirty_list; |
| 190 | struct shrinker mblk_shrinker; |
| 191 | |
| 192 | /* Zone allocation management */ |
| 193 | struct mutex map_lock; |
| 194 | struct dmz_mblock **map_mblk; |
| 195 | unsigned int nr_rnd; |
| 196 | atomic_t unmap_nr_rnd; |
| 197 | struct list_head unmap_rnd_list; |
| 198 | struct list_head map_rnd_list; |
| 199 | |
Hannes Reinecke | 34f5aff | 2020-05-19 10:14:20 +0200 | [diff] [blame] | 200 | unsigned int nr_cache; |
| 201 | atomic_t unmap_nr_cache; |
| 202 | struct list_head unmap_cache_list; |
| 203 | struct list_head map_cache_list; |
| 204 | |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 205 | unsigned int nr_seq; |
| 206 | atomic_t unmap_nr_seq; |
| 207 | struct list_head unmap_seq_list; |
| 208 | struct list_head map_seq_list; |
| 209 | |
| 210 | atomic_t nr_reserved_seq_zones; |
| 211 | struct list_head reserved_seq_zones_list; |
| 212 | |
| 213 | wait_queue_head_t free_wq; |
| 214 | }; |
| 215 | |
Hannes Reinecke | ca1a704 | 2020-05-11 10:24:27 +0200 | [diff] [blame] | 216 | #define dmz_zmd_info(zmd, format, args...) \ |
Hannes Reinecke | bd5c403 | 2020-05-11 10:24:30 +0200 | [diff] [blame] | 217 | DMINFO("(%s): " format, (zmd)->label, ## args) |
Hannes Reinecke | ca1a704 | 2020-05-11 10:24:27 +0200 | [diff] [blame] | 218 | |
| 219 | #define dmz_zmd_err(zmd, format, args...) \ |
Hannes Reinecke | bd5c403 | 2020-05-11 10:24:30 +0200 | [diff] [blame] | 220 | DMERR("(%s): " format, (zmd)->label, ## args) |
Hannes Reinecke | ca1a704 | 2020-05-11 10:24:27 +0200 | [diff] [blame] | 221 | |
| 222 | #define dmz_zmd_warn(zmd, format, args...) \ |
Hannes Reinecke | bd5c403 | 2020-05-11 10:24:30 +0200 | [diff] [blame] | 223 | DMWARN("(%s): " format, (zmd)->label, ## args) |
Hannes Reinecke | ca1a704 | 2020-05-11 10:24:27 +0200 | [diff] [blame] | 224 | |
| 225 | #define dmz_zmd_debug(zmd, format, args...) \ |
Hannes Reinecke | bd5c403 | 2020-05-11 10:24:30 +0200 | [diff] [blame] | 226 | DMDEBUG("(%s): " format, (zmd)->label, ## args) |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 227 | /* |
| 228 | * Various accessors |
| 229 | */ |
Hannes Reinecke | bd5c403 | 2020-05-11 10:24:30 +0200 | [diff] [blame] | 230 | static unsigned int dmz_dev_zone_id(struct dmz_metadata *zmd, struct dm_zone *zone) |
| 231 | { |
| 232 | unsigned int zone_id; |
| 233 | |
| 234 | if (WARN_ON(!zone)) |
| 235 | return 0; |
| 236 | |
| 237 | zone_id = zone->id; |
| 238 | if (zmd->nr_devs > 1 && |
| 239 | (zone_id >= zmd->dev[1].zone_offset)) |
| 240 | zone_id -= zmd->dev[1].zone_offset; |
| 241 | return zone_id; |
| 242 | } |
| 243 | |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 244 | sector_t dmz_start_sect(struct dmz_metadata *zmd, struct dm_zone *zone) |
| 245 | { |
Hannes Reinecke | bd5c403 | 2020-05-11 10:24:30 +0200 | [diff] [blame] | 246 | unsigned int zone_id = dmz_dev_zone_id(zmd, zone); |
| 247 | |
| 248 | return (sector_t)zone_id << zmd->zone_nr_sectors_shift; |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 249 | } |
| 250 | |
| 251 | sector_t dmz_start_block(struct dmz_metadata *zmd, struct dm_zone *zone) |
| 252 | { |
Hannes Reinecke | bd5c403 | 2020-05-11 10:24:30 +0200 | [diff] [blame] | 253 | unsigned int zone_id = dmz_dev_zone_id(zmd, zone); |
| 254 | |
| 255 | return (sector_t)zone_id << zmd->zone_nr_blocks_shift; |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 256 | } |
| 257 | |
Hannes Reinecke | bf28a3b | 2020-05-11 10:24:20 +0200 | [diff] [blame] | 258 | struct dmz_dev *dmz_zone_to_dev(struct dmz_metadata *zmd, struct dm_zone *zone) |
| 259 | { |
Hannes Reinecke | bd5c403 | 2020-05-11 10:24:30 +0200 | [diff] [blame] | 260 | if (WARN_ON(!zone)) |
| 261 | return &zmd->dev[0]; |
| 262 | |
| 263 | if (zmd->nr_devs > 1 && |
| 264 | zone->id >= zmd->dev[1].zone_offset) |
| 265 | return &zmd->dev[1]; |
| 266 | |
Hannes Reinecke | bf28a3b | 2020-05-11 10:24:20 +0200 | [diff] [blame] | 267 | return &zmd->dev[0]; |
| 268 | } |
| 269 | |
Hannes Reinecke | 3682056 | 2020-05-11 10:24:21 +0200 | [diff] [blame] | 270 | unsigned int dmz_zone_nr_blocks(struct dmz_metadata *zmd) |
| 271 | { |
| 272 | return zmd->zone_nr_blocks; |
| 273 | } |
| 274 | |
| 275 | unsigned int dmz_zone_nr_blocks_shift(struct dmz_metadata *zmd) |
| 276 | { |
| 277 | return zmd->zone_nr_blocks_shift; |
| 278 | } |
| 279 | |
| 280 | unsigned int dmz_zone_nr_sectors(struct dmz_metadata *zmd) |
| 281 | { |
| 282 | return zmd->zone_nr_sectors; |
| 283 | } |
| 284 | |
| 285 | unsigned int dmz_zone_nr_sectors_shift(struct dmz_metadata *zmd) |
| 286 | { |
| 287 | return zmd->zone_nr_sectors_shift; |
| 288 | } |
| 289 | |
Hannes Reinecke | bc3d571 | 2020-05-11 10:24:16 +0200 | [diff] [blame] | 290 | unsigned int dmz_nr_zones(struct dmz_metadata *zmd) |
| 291 | { |
Hannes Reinecke | 3682056 | 2020-05-11 10:24:21 +0200 | [diff] [blame] | 292 | return zmd->nr_zones; |
Hannes Reinecke | bc3d571 | 2020-05-11 10:24:16 +0200 | [diff] [blame] | 293 | } |
| 294 | |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 295 | unsigned int dmz_nr_chunks(struct dmz_metadata *zmd) |
| 296 | { |
| 297 | return zmd->nr_chunks; |
| 298 | } |
| 299 | |
| 300 | unsigned int dmz_nr_rnd_zones(struct dmz_metadata *zmd) |
| 301 | { |
| 302 | return zmd->nr_rnd; |
| 303 | } |
| 304 | |
| 305 | unsigned int dmz_nr_unmap_rnd_zones(struct dmz_metadata *zmd) |
| 306 | { |
| 307 | return atomic_read(&zmd->unmap_nr_rnd); |
| 308 | } |
| 309 | |
Hannes Reinecke | 34f5aff | 2020-05-19 10:14:20 +0200 | [diff] [blame] | 310 | unsigned int dmz_nr_cache_zones(struct dmz_metadata *zmd) |
| 311 | { |
| 312 | return zmd->nr_cache; |
| 313 | } |
| 314 | |
| 315 | unsigned int dmz_nr_unmap_cache_zones(struct dmz_metadata *zmd) |
| 316 | { |
| 317 | return atomic_read(&zmd->unmap_nr_cache); |
| 318 | } |
| 319 | |
Hannes Reinecke | bc3d571 | 2020-05-11 10:24:16 +0200 | [diff] [blame] | 320 | unsigned int dmz_nr_seq_zones(struct dmz_metadata *zmd) |
| 321 | { |
| 322 | return zmd->nr_seq; |
| 323 | } |
| 324 | |
| 325 | unsigned int dmz_nr_unmap_seq_zones(struct dmz_metadata *zmd) |
| 326 | { |
| 327 | return atomic_read(&zmd->unmap_nr_seq); |
| 328 | } |
| 329 | |
Hannes Reinecke | 2234e73 | 2020-05-11 10:24:22 +0200 | [diff] [blame] | 330 | const char *dmz_metadata_label(struct dmz_metadata *zmd) |
| 331 | { |
Hannes Reinecke | bd5c403 | 2020-05-11 10:24:30 +0200 | [diff] [blame] | 332 | return (const char *)zmd->label; |
Hannes Reinecke | 2234e73 | 2020-05-11 10:24:22 +0200 | [diff] [blame] | 333 | } |
| 334 | |
Hannes Reinecke | d0e21ce | 2020-05-11 10:24:23 +0200 | [diff] [blame] | 335 | bool dmz_check_dev(struct dmz_metadata *zmd) |
| 336 | { |
Hannes Reinecke | bd5c403 | 2020-05-11 10:24:30 +0200 | [diff] [blame] | 337 | unsigned int i; |
| 338 | |
| 339 | for (i = 0; i < zmd->nr_devs; i++) { |
| 340 | if (!dmz_check_bdev(&zmd->dev[i])) |
| 341 | return false; |
| 342 | } |
| 343 | return true; |
Hannes Reinecke | d0e21ce | 2020-05-11 10:24:23 +0200 | [diff] [blame] | 344 | } |
| 345 | |
| 346 | bool dmz_dev_is_dying(struct dmz_metadata *zmd) |
| 347 | { |
Hannes Reinecke | bd5c403 | 2020-05-11 10:24:30 +0200 | [diff] [blame] | 348 | unsigned int i; |
| 349 | |
| 350 | for (i = 0; i < zmd->nr_devs; i++) { |
| 351 | if (dmz_bdev_is_dying(&zmd->dev[i])) |
| 352 | return true; |
| 353 | } |
| 354 | return false; |
Hannes Reinecke | d0e21ce | 2020-05-11 10:24:23 +0200 | [diff] [blame] | 355 | } |
| 356 | |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 357 | /* |
| 358 | * Lock/unlock mapping table. |
| 359 | * The map lock also protects all the zone lists. |
| 360 | */ |
| 361 | void dmz_lock_map(struct dmz_metadata *zmd) |
| 362 | { |
| 363 | mutex_lock(&zmd->map_lock); |
| 364 | } |
| 365 | |
| 366 | void dmz_unlock_map(struct dmz_metadata *zmd) |
| 367 | { |
| 368 | mutex_unlock(&zmd->map_lock); |
| 369 | } |
| 370 | |
| 371 | /* |
| 372 | * Lock/unlock metadata access. This is a "read" lock on a semaphore |
| 373 | * that prevents metadata flush from running while metadata are being |
| 374 | * modified. The actual metadata write mutual exclusion is achieved with |
Dmitry Fomichev | ad1bd57 | 2019-08-02 15:02:51 -0700 | [diff] [blame] | 375 | * the map lock and zone state management (active and reclaim state are |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 376 | * mutually exclusive). |
| 377 | */ |
| 378 | void dmz_lock_metadata(struct dmz_metadata *zmd) |
| 379 | { |
| 380 | down_read(&zmd->mblk_sem); |
| 381 | } |
| 382 | |
| 383 | void dmz_unlock_metadata(struct dmz_metadata *zmd) |
| 384 | { |
| 385 | up_read(&zmd->mblk_sem); |
| 386 | } |
| 387 | |
| 388 | /* |
| 389 | * Lock/unlock flush: prevent concurrent executions |
| 390 | * of dmz_flush_metadata as well as metadata modification in reclaim |
| 391 | * while flush is being executed. |
| 392 | */ |
| 393 | void dmz_lock_flush(struct dmz_metadata *zmd) |
| 394 | { |
| 395 | mutex_lock(&zmd->mblk_flush_lock); |
| 396 | } |
| 397 | |
| 398 | void dmz_unlock_flush(struct dmz_metadata *zmd) |
| 399 | { |
| 400 | mutex_unlock(&zmd->mblk_flush_lock); |
| 401 | } |
| 402 | |
| 403 | /* |
| 404 | * Allocate a metadata block. |
| 405 | */ |
| 406 | static struct dmz_mblock *dmz_alloc_mblock(struct dmz_metadata *zmd, |
| 407 | sector_t mblk_no) |
| 408 | { |
| 409 | struct dmz_mblock *mblk = NULL; |
| 410 | |
| 411 | /* See if we can reuse cached blocks */ |
| 412 | if (zmd->max_nr_mblks && atomic_read(&zmd->nr_mblks) > zmd->max_nr_mblks) { |
| 413 | spin_lock(&zmd->mblk_lock); |
| 414 | mblk = list_first_entry_or_null(&zmd->mblk_lru_list, |
| 415 | struct dmz_mblock, link); |
| 416 | if (mblk) { |
| 417 | list_del_init(&mblk->link); |
| 418 | rb_erase(&mblk->node, &zmd->mblk_rbtree); |
| 419 | mblk->no = mblk_no; |
| 420 | } |
| 421 | spin_unlock(&zmd->mblk_lock); |
| 422 | if (mblk) |
| 423 | return mblk; |
| 424 | } |
| 425 | |
| 426 | /* Allocate a new block */ |
| 427 | mblk = kmalloc(sizeof(struct dmz_mblock), GFP_NOIO); |
| 428 | if (!mblk) |
| 429 | return NULL; |
| 430 | |
| 431 | mblk->page = alloc_page(GFP_NOIO); |
| 432 | if (!mblk->page) { |
| 433 | kfree(mblk); |
| 434 | return NULL; |
| 435 | } |
| 436 | |
| 437 | RB_CLEAR_NODE(&mblk->node); |
| 438 | INIT_LIST_HEAD(&mblk->link); |
Damien Le Moal | 33c2865 | 2018-10-17 18:05:07 +0900 | [diff] [blame] | 439 | mblk->ref = 0; |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 440 | mblk->state = 0; |
| 441 | mblk->no = mblk_no; |
| 442 | mblk->data = page_address(mblk->page); |
| 443 | |
| 444 | atomic_inc(&zmd->nr_mblks); |
| 445 | |
| 446 | return mblk; |
| 447 | } |
| 448 | |
| 449 | /* |
| 450 | * Free a metadata block. |
| 451 | */ |
| 452 | static void dmz_free_mblock(struct dmz_metadata *zmd, struct dmz_mblock *mblk) |
| 453 | { |
| 454 | __free_pages(mblk->page, 0); |
| 455 | kfree(mblk); |
| 456 | |
| 457 | atomic_dec(&zmd->nr_mblks); |
| 458 | } |
| 459 | |
| 460 | /* |
| 461 | * Insert a metadata block in the rbtree. |
| 462 | */ |
| 463 | static void dmz_insert_mblock(struct dmz_metadata *zmd, struct dmz_mblock *mblk) |
| 464 | { |
| 465 | struct rb_root *root = &zmd->mblk_rbtree; |
| 466 | struct rb_node **new = &(root->rb_node), *parent = NULL; |
| 467 | struct dmz_mblock *b; |
| 468 | |
| 469 | /* Figure out where to put the new node */ |
| 470 | while (*new) { |
| 471 | b = container_of(*new, struct dmz_mblock, node); |
| 472 | parent = *new; |
| 473 | new = (b->no < mblk->no) ? &((*new)->rb_left) : &((*new)->rb_right); |
| 474 | } |
| 475 | |
| 476 | /* Add new node and rebalance tree */ |
| 477 | rb_link_node(&mblk->node, parent, new); |
| 478 | rb_insert_color(&mblk->node, root); |
| 479 | } |
| 480 | |
| 481 | /* |
Damien Le Moal | 3d4e738 | 2018-10-17 18:05:08 +0900 | [diff] [blame] | 482 | * Lookup a metadata block in the rbtree. If the block is found, increment |
| 483 | * its reference count. |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 484 | */ |
Damien Le Moal | 3d4e738 | 2018-10-17 18:05:08 +0900 | [diff] [blame] | 485 | static struct dmz_mblock *dmz_get_mblock_fast(struct dmz_metadata *zmd, |
| 486 | sector_t mblk_no) |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 487 | { |
| 488 | struct rb_root *root = &zmd->mblk_rbtree; |
| 489 | struct rb_node *node = root->rb_node; |
| 490 | struct dmz_mblock *mblk; |
| 491 | |
| 492 | while (node) { |
| 493 | mblk = container_of(node, struct dmz_mblock, node); |
Damien Le Moal | 3d4e738 | 2018-10-17 18:05:08 +0900 | [diff] [blame] | 494 | if (mblk->no == mblk_no) { |
| 495 | /* |
| 496 | * If this is the first reference to the block, |
| 497 | * remove it from the LRU list. |
| 498 | */ |
| 499 | mblk->ref++; |
| 500 | if (mblk->ref == 1 && |
| 501 | !test_bit(DMZ_META_DIRTY, &mblk->state)) |
| 502 | list_del_init(&mblk->link); |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 503 | return mblk; |
Damien Le Moal | 3d4e738 | 2018-10-17 18:05:08 +0900 | [diff] [blame] | 504 | } |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 505 | node = (mblk->no < mblk_no) ? node->rb_left : node->rb_right; |
| 506 | } |
| 507 | |
| 508 | return NULL; |
| 509 | } |
| 510 | |
| 511 | /* |
| 512 | * Metadata block BIO end callback. |
| 513 | */ |
| 514 | static void dmz_mblock_bio_end_io(struct bio *bio) |
| 515 | { |
| 516 | struct dmz_mblock *mblk = bio->bi_private; |
| 517 | int flag; |
| 518 | |
| 519 | if (bio->bi_status) |
| 520 | set_bit(DMZ_META_ERROR, &mblk->state); |
| 521 | |
| 522 | if (bio_op(bio) == REQ_OP_WRITE) |
| 523 | flag = DMZ_META_WRITING; |
| 524 | else |
| 525 | flag = DMZ_META_READING; |
| 526 | |
| 527 | clear_bit_unlock(flag, &mblk->state); |
| 528 | smp_mb__after_atomic(); |
| 529 | wake_up_bit(&mblk->state, flag); |
| 530 | |
| 531 | bio_put(bio); |
| 532 | } |
| 533 | |
| 534 | /* |
Damien Le Moal | 3d4e738 | 2018-10-17 18:05:08 +0900 | [diff] [blame] | 535 | * Read an uncached metadata block from disk and add it to the cache. |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 536 | */ |
Damien Le Moal | 3d4e738 | 2018-10-17 18:05:08 +0900 | [diff] [blame] | 537 | static struct dmz_mblock *dmz_get_mblock_slow(struct dmz_metadata *zmd, |
| 538 | sector_t mblk_no) |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 539 | { |
Damien Le Moal | 3d4e738 | 2018-10-17 18:05:08 +0900 | [diff] [blame] | 540 | struct dmz_mblock *mblk, *m; |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 541 | sector_t block = zmd->sb[zmd->mblk_primary].block + mblk_no; |
Hannes Reinecke | bf28a3b | 2020-05-11 10:24:20 +0200 | [diff] [blame] | 542 | struct dmz_dev *dev = zmd->sb[zmd->mblk_primary].dev; |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 543 | struct bio *bio; |
| 544 | |
Hannes Reinecke | bf28a3b | 2020-05-11 10:24:20 +0200 | [diff] [blame] | 545 | if (dmz_bdev_is_dying(dev)) |
Dmitry Fomichev | 75d66ff | 2019-08-10 14:43:11 -0700 | [diff] [blame] | 546 | return ERR_PTR(-EIO); |
| 547 | |
Damien Le Moal | 3d4e738 | 2018-10-17 18:05:08 +0900 | [diff] [blame] | 548 | /* Get a new block and a BIO to read it */ |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 549 | mblk = dmz_alloc_mblock(zmd, mblk_no); |
| 550 | if (!mblk) |
Dmitry Fomichev | 75d66ff | 2019-08-10 14:43:11 -0700 | [diff] [blame] | 551 | return ERR_PTR(-ENOMEM); |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 552 | |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 553 | bio = bio_alloc(GFP_NOIO, 1); |
| 554 | if (!bio) { |
| 555 | dmz_free_mblock(zmd, mblk); |
Dmitry Fomichev | 75d66ff | 2019-08-10 14:43:11 -0700 | [diff] [blame] | 556 | return ERR_PTR(-ENOMEM); |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 557 | } |
| 558 | |
Damien Le Moal | 3d4e738 | 2018-10-17 18:05:08 +0900 | [diff] [blame] | 559 | spin_lock(&zmd->mblk_lock); |
| 560 | |
| 561 | /* |
| 562 | * Make sure that another context did not start reading |
| 563 | * the block already. |
| 564 | */ |
| 565 | m = dmz_get_mblock_fast(zmd, mblk_no); |
| 566 | if (m) { |
| 567 | spin_unlock(&zmd->mblk_lock); |
| 568 | dmz_free_mblock(zmd, mblk); |
| 569 | bio_put(bio); |
| 570 | return m; |
| 571 | } |
| 572 | |
| 573 | mblk->ref++; |
| 574 | set_bit(DMZ_META_READING, &mblk->state); |
| 575 | dmz_insert_mblock(zmd, mblk); |
| 576 | |
| 577 | spin_unlock(&zmd->mblk_lock); |
| 578 | |
| 579 | /* Submit read BIO */ |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 580 | bio->bi_iter.bi_sector = dmz_blk2sect(block); |
Hannes Reinecke | bf28a3b | 2020-05-11 10:24:20 +0200 | [diff] [blame] | 581 | bio_set_dev(bio, dev->bdev); |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 582 | bio->bi_private = mblk; |
| 583 | bio->bi_end_io = dmz_mblock_bio_end_io; |
| 584 | bio_set_op_attrs(bio, REQ_OP_READ, REQ_META | REQ_PRIO); |
| 585 | bio_add_page(bio, mblk->page, DMZ_BLOCK_SIZE, 0); |
| 586 | submit_bio(bio); |
| 587 | |
| 588 | return mblk; |
| 589 | } |
| 590 | |
| 591 | /* |
| 592 | * Free metadata blocks. |
| 593 | */ |
| 594 | static unsigned long dmz_shrink_mblock_cache(struct dmz_metadata *zmd, |
| 595 | unsigned long limit) |
| 596 | { |
| 597 | struct dmz_mblock *mblk; |
| 598 | unsigned long count = 0; |
| 599 | |
| 600 | if (!zmd->max_nr_mblks) |
| 601 | return 0; |
| 602 | |
| 603 | while (!list_empty(&zmd->mblk_lru_list) && |
| 604 | atomic_read(&zmd->nr_mblks) > zmd->min_nr_mblks && |
| 605 | count < limit) { |
| 606 | mblk = list_first_entry(&zmd->mblk_lru_list, |
| 607 | struct dmz_mblock, link); |
| 608 | list_del_init(&mblk->link); |
| 609 | rb_erase(&mblk->node, &zmd->mblk_rbtree); |
| 610 | dmz_free_mblock(zmd, mblk); |
| 611 | count++; |
| 612 | } |
| 613 | |
| 614 | return count; |
| 615 | } |
| 616 | |
| 617 | /* |
| 618 | * For mblock shrinker: get the number of unused metadata blocks in the cache. |
| 619 | */ |
| 620 | static unsigned long dmz_mblock_shrinker_count(struct shrinker *shrink, |
| 621 | struct shrink_control *sc) |
| 622 | { |
| 623 | struct dmz_metadata *zmd = container_of(shrink, struct dmz_metadata, mblk_shrinker); |
| 624 | |
| 625 | return atomic_read(&zmd->nr_mblks); |
| 626 | } |
| 627 | |
| 628 | /* |
| 629 | * For mblock shrinker: scan unused metadata blocks and shrink the cache. |
| 630 | */ |
| 631 | static unsigned long dmz_mblock_shrinker_scan(struct shrinker *shrink, |
| 632 | struct shrink_control *sc) |
| 633 | { |
| 634 | struct dmz_metadata *zmd = container_of(shrink, struct dmz_metadata, mblk_shrinker); |
| 635 | unsigned long count; |
| 636 | |
| 637 | spin_lock(&zmd->mblk_lock); |
| 638 | count = dmz_shrink_mblock_cache(zmd, sc->nr_to_scan); |
| 639 | spin_unlock(&zmd->mblk_lock); |
| 640 | |
| 641 | return count ? count : SHRINK_STOP; |
| 642 | } |
| 643 | |
| 644 | /* |
| 645 | * Release a metadata block. |
| 646 | */ |
| 647 | static void dmz_release_mblock(struct dmz_metadata *zmd, |
| 648 | struct dmz_mblock *mblk) |
| 649 | { |
| 650 | |
| 651 | if (!mblk) |
| 652 | return; |
| 653 | |
| 654 | spin_lock(&zmd->mblk_lock); |
| 655 | |
Damien Le Moal | 33c2865 | 2018-10-17 18:05:07 +0900 | [diff] [blame] | 656 | mblk->ref--; |
| 657 | if (mblk->ref == 0) { |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 658 | if (test_bit(DMZ_META_ERROR, &mblk->state)) { |
| 659 | rb_erase(&mblk->node, &zmd->mblk_rbtree); |
| 660 | dmz_free_mblock(zmd, mblk); |
| 661 | } else if (!test_bit(DMZ_META_DIRTY, &mblk->state)) { |
| 662 | list_add_tail(&mblk->link, &zmd->mblk_lru_list); |
| 663 | dmz_shrink_mblock_cache(zmd, 1); |
| 664 | } |
| 665 | } |
| 666 | |
| 667 | spin_unlock(&zmd->mblk_lock); |
| 668 | } |
| 669 | |
| 670 | /* |
| 671 | * Get a metadata block from the rbtree. If the block |
| 672 | * is not present, read it from disk. |
| 673 | */ |
| 674 | static struct dmz_mblock *dmz_get_mblock(struct dmz_metadata *zmd, |
| 675 | sector_t mblk_no) |
| 676 | { |
| 677 | struct dmz_mblock *mblk; |
Hannes Reinecke | bf28a3b | 2020-05-11 10:24:20 +0200 | [diff] [blame] | 678 | struct dmz_dev *dev = zmd->sb[zmd->mblk_primary].dev; |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 679 | |
| 680 | /* Check rbtree */ |
| 681 | spin_lock(&zmd->mblk_lock); |
Damien Le Moal | 3d4e738 | 2018-10-17 18:05:08 +0900 | [diff] [blame] | 682 | mblk = dmz_get_mblock_fast(zmd, mblk_no); |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 683 | spin_unlock(&zmd->mblk_lock); |
| 684 | |
| 685 | if (!mblk) { |
| 686 | /* Cache miss: read the block from disk */ |
Damien Le Moal | 3d4e738 | 2018-10-17 18:05:08 +0900 | [diff] [blame] | 687 | mblk = dmz_get_mblock_slow(zmd, mblk_no); |
Dmitry Fomichev | 75d66ff | 2019-08-10 14:43:11 -0700 | [diff] [blame] | 688 | if (IS_ERR(mblk)) |
| 689 | return mblk; |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 690 | } |
| 691 | |
| 692 | /* Wait for on-going read I/O and check for error */ |
| 693 | wait_on_bit_io(&mblk->state, DMZ_META_READING, |
| 694 | TASK_UNINTERRUPTIBLE); |
| 695 | if (test_bit(DMZ_META_ERROR, &mblk->state)) { |
| 696 | dmz_release_mblock(zmd, mblk); |
Hannes Reinecke | bf28a3b | 2020-05-11 10:24:20 +0200 | [diff] [blame] | 697 | dmz_check_bdev(dev); |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 698 | return ERR_PTR(-EIO); |
| 699 | } |
| 700 | |
| 701 | return mblk; |
| 702 | } |
| 703 | |
| 704 | /* |
| 705 | * Mark a metadata block dirty. |
| 706 | */ |
| 707 | static void dmz_dirty_mblock(struct dmz_metadata *zmd, struct dmz_mblock *mblk) |
| 708 | { |
| 709 | spin_lock(&zmd->mblk_lock); |
| 710 | if (!test_and_set_bit(DMZ_META_DIRTY, &mblk->state)) |
| 711 | list_add_tail(&mblk->link, &zmd->mblk_dirty_list); |
| 712 | spin_unlock(&zmd->mblk_lock); |
| 713 | } |
| 714 | |
| 715 | /* |
| 716 | * Issue a metadata block write BIO. |
| 717 | */ |
Dmitry Fomichev | 75d66ff | 2019-08-10 14:43:11 -0700 | [diff] [blame] | 718 | static int dmz_write_mblock(struct dmz_metadata *zmd, struct dmz_mblock *mblk, |
| 719 | unsigned int set) |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 720 | { |
Hannes Reinecke | bf28a3b | 2020-05-11 10:24:20 +0200 | [diff] [blame] | 721 | struct dmz_dev *dev = zmd->sb[set].dev; |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 722 | sector_t block = zmd->sb[set].block + mblk->no; |
| 723 | struct bio *bio; |
| 724 | |
Hannes Reinecke | bf28a3b | 2020-05-11 10:24:20 +0200 | [diff] [blame] | 725 | if (dmz_bdev_is_dying(dev)) |
Dmitry Fomichev | 75d66ff | 2019-08-10 14:43:11 -0700 | [diff] [blame] | 726 | return -EIO; |
| 727 | |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 728 | bio = bio_alloc(GFP_NOIO, 1); |
| 729 | if (!bio) { |
| 730 | set_bit(DMZ_META_ERROR, &mblk->state); |
Dmitry Fomichev | 75d66ff | 2019-08-10 14:43:11 -0700 | [diff] [blame] | 731 | return -ENOMEM; |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 732 | } |
| 733 | |
| 734 | set_bit(DMZ_META_WRITING, &mblk->state); |
| 735 | |
| 736 | bio->bi_iter.bi_sector = dmz_blk2sect(block); |
Hannes Reinecke | bf28a3b | 2020-05-11 10:24:20 +0200 | [diff] [blame] | 737 | bio_set_dev(bio, dev->bdev); |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 738 | bio->bi_private = mblk; |
| 739 | bio->bi_end_io = dmz_mblock_bio_end_io; |
| 740 | bio_set_op_attrs(bio, REQ_OP_WRITE, REQ_META | REQ_PRIO); |
| 741 | bio_add_page(bio, mblk->page, DMZ_BLOCK_SIZE, 0); |
| 742 | submit_bio(bio); |
Dmitry Fomichev | 75d66ff | 2019-08-10 14:43:11 -0700 | [diff] [blame] | 743 | |
| 744 | return 0; |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 745 | } |
| 746 | |
| 747 | /* |
| 748 | * Read/write a metadata block. |
| 749 | */ |
Hannes Reinecke | bf28a3b | 2020-05-11 10:24:20 +0200 | [diff] [blame] | 750 | static int dmz_rdwr_block(struct dmz_dev *dev, int op, |
| 751 | sector_t block, struct page *page) |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 752 | { |
| 753 | struct bio *bio; |
| 754 | int ret; |
| 755 | |
Hannes Reinecke | bd5c403 | 2020-05-11 10:24:30 +0200 | [diff] [blame] | 756 | if (WARN_ON(!dev)) |
| 757 | return -EIO; |
| 758 | |
Hannes Reinecke | bf28a3b | 2020-05-11 10:24:20 +0200 | [diff] [blame] | 759 | if (dmz_bdev_is_dying(dev)) |
Dmitry Fomichev | 75d66ff | 2019-08-10 14:43:11 -0700 | [diff] [blame] | 760 | return -EIO; |
| 761 | |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 762 | bio = bio_alloc(GFP_NOIO, 1); |
| 763 | if (!bio) |
| 764 | return -ENOMEM; |
| 765 | |
| 766 | bio->bi_iter.bi_sector = dmz_blk2sect(block); |
Hannes Reinecke | bf28a3b | 2020-05-11 10:24:20 +0200 | [diff] [blame] | 767 | bio_set_dev(bio, dev->bdev); |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 768 | bio_set_op_attrs(bio, op, REQ_SYNC | REQ_META | REQ_PRIO); |
| 769 | bio_add_page(bio, page, DMZ_BLOCK_SIZE, 0); |
| 770 | ret = submit_bio_wait(bio); |
| 771 | bio_put(bio); |
| 772 | |
Dmitry Fomichev | e7fad90 | 2019-11-06 14:34:35 -0800 | [diff] [blame] | 773 | if (ret) |
Hannes Reinecke | bf28a3b | 2020-05-11 10:24:20 +0200 | [diff] [blame] | 774 | dmz_check_bdev(dev); |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 775 | return ret; |
| 776 | } |
| 777 | |
| 778 | /* |
| 779 | * Write super block of the specified metadata set. |
| 780 | */ |
| 781 | static int dmz_write_sb(struct dmz_metadata *zmd, unsigned int set) |
| 782 | { |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 783 | struct dmz_mblock *mblk = zmd->sb[set].mblk; |
| 784 | struct dmz_super *sb = zmd->sb[set].sb; |
Hannes Reinecke | bf28a3b | 2020-05-11 10:24:20 +0200 | [diff] [blame] | 785 | struct dmz_dev *dev = zmd->sb[set].dev; |
Hannes Reinecke | bd5c403 | 2020-05-11 10:24:30 +0200 | [diff] [blame] | 786 | sector_t sb_block; |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 787 | u64 sb_gen = zmd->sb_gen + 1; |
| 788 | int ret; |
| 789 | |
| 790 | sb->magic = cpu_to_le32(DMZ_MAGIC); |
Hannes Reinecke | bd5c403 | 2020-05-11 10:24:30 +0200 | [diff] [blame] | 791 | |
| 792 | sb->version = cpu_to_le32(zmd->sb_version); |
| 793 | if (zmd->sb_version > 1) { |
| 794 | BUILD_BUG_ON(UUID_SIZE != 16); |
| 795 | export_uuid(sb->dmz_uuid, &zmd->uuid); |
| 796 | memcpy(sb->dmz_label, zmd->label, BDEVNAME_SIZE); |
| 797 | export_uuid(sb->dev_uuid, &dev->uuid); |
| 798 | } |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 799 | |
| 800 | sb->gen = cpu_to_le64(sb_gen); |
| 801 | |
Hannes Reinecke | bd5c403 | 2020-05-11 10:24:30 +0200 | [diff] [blame] | 802 | /* |
| 803 | * The metadata always references the absolute block address, |
| 804 | * ie relative to the entire block range, not the per-device |
| 805 | * block address. |
| 806 | */ |
| 807 | sb_block = zmd->sb[set].zone->id << zmd->zone_nr_blocks_shift; |
| 808 | sb->sb_block = cpu_to_le64(sb_block); |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 809 | sb->nr_meta_blocks = cpu_to_le32(zmd->nr_meta_blocks); |
| 810 | sb->nr_reserved_seq = cpu_to_le32(zmd->nr_reserved_seq); |
| 811 | sb->nr_chunks = cpu_to_le32(zmd->nr_chunks); |
| 812 | |
| 813 | sb->nr_map_blocks = cpu_to_le32(zmd->nr_map_blocks); |
| 814 | sb->nr_bitmap_blocks = cpu_to_le32(zmd->nr_bitmap_blocks); |
| 815 | |
| 816 | sb->crc = 0; |
| 817 | sb->crc = cpu_to_le32(crc32_le(sb_gen, (unsigned char *)sb, DMZ_BLOCK_SIZE)); |
| 818 | |
Hannes Reinecke | bd5c403 | 2020-05-11 10:24:30 +0200 | [diff] [blame] | 819 | ret = dmz_rdwr_block(dev, REQ_OP_WRITE, zmd->sb[set].block, |
| 820 | mblk->page); |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 821 | if (ret == 0) |
Hannes Reinecke | bf28a3b | 2020-05-11 10:24:20 +0200 | [diff] [blame] | 822 | ret = blkdev_issue_flush(dev->bdev, GFP_NOIO, NULL); |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 823 | |
| 824 | return ret; |
| 825 | } |
| 826 | |
| 827 | /* |
| 828 | * Write dirty metadata blocks to the specified set. |
| 829 | */ |
| 830 | static int dmz_write_dirty_mblocks(struct dmz_metadata *zmd, |
| 831 | struct list_head *write_list, |
| 832 | unsigned int set) |
| 833 | { |
| 834 | struct dmz_mblock *mblk; |
Hannes Reinecke | bf28a3b | 2020-05-11 10:24:20 +0200 | [diff] [blame] | 835 | struct dmz_dev *dev = zmd->sb[set].dev; |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 836 | struct blk_plug plug; |
Dmitry Fomichev | 75d66ff | 2019-08-10 14:43:11 -0700 | [diff] [blame] | 837 | int ret = 0, nr_mblks_submitted = 0; |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 838 | |
| 839 | /* Issue writes */ |
| 840 | blk_start_plug(&plug); |
Dmitry Fomichev | 75d66ff | 2019-08-10 14:43:11 -0700 | [diff] [blame] | 841 | list_for_each_entry(mblk, write_list, link) { |
| 842 | ret = dmz_write_mblock(zmd, mblk, set); |
| 843 | if (ret) |
| 844 | break; |
| 845 | nr_mblks_submitted++; |
| 846 | } |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 847 | blk_finish_plug(&plug); |
| 848 | |
| 849 | /* Wait for completion */ |
| 850 | list_for_each_entry(mblk, write_list, link) { |
Dmitry Fomichev | 75d66ff | 2019-08-10 14:43:11 -0700 | [diff] [blame] | 851 | if (!nr_mblks_submitted) |
| 852 | break; |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 853 | wait_on_bit_io(&mblk->state, DMZ_META_WRITING, |
| 854 | TASK_UNINTERRUPTIBLE); |
| 855 | if (test_bit(DMZ_META_ERROR, &mblk->state)) { |
| 856 | clear_bit(DMZ_META_ERROR, &mblk->state); |
Hannes Reinecke | bf28a3b | 2020-05-11 10:24:20 +0200 | [diff] [blame] | 857 | dmz_check_bdev(dev); |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 858 | ret = -EIO; |
| 859 | } |
Dmitry Fomichev | 75d66ff | 2019-08-10 14:43:11 -0700 | [diff] [blame] | 860 | nr_mblks_submitted--; |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 861 | } |
| 862 | |
| 863 | /* Flush drive cache (this will also sync data) */ |
| 864 | if (ret == 0) |
Hannes Reinecke | bf28a3b | 2020-05-11 10:24:20 +0200 | [diff] [blame] | 865 | ret = blkdev_issue_flush(dev->bdev, GFP_NOIO, NULL); |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 866 | |
| 867 | return ret; |
| 868 | } |
| 869 | |
| 870 | /* |
| 871 | * Log dirty metadata blocks. |
| 872 | */ |
| 873 | static int dmz_log_dirty_mblocks(struct dmz_metadata *zmd, |
| 874 | struct list_head *write_list) |
| 875 | { |
| 876 | unsigned int log_set = zmd->mblk_primary ^ 0x1; |
| 877 | int ret; |
| 878 | |
| 879 | /* Write dirty blocks to the log */ |
| 880 | ret = dmz_write_dirty_mblocks(zmd, write_list, log_set); |
| 881 | if (ret) |
| 882 | return ret; |
| 883 | |
| 884 | /* |
| 885 | * No error so far: now validate the log by updating the |
| 886 | * log index super block generation. |
| 887 | */ |
| 888 | ret = dmz_write_sb(zmd, log_set); |
| 889 | if (ret) |
| 890 | return ret; |
| 891 | |
| 892 | return 0; |
| 893 | } |
| 894 | |
| 895 | /* |
| 896 | * Flush dirty metadata blocks. |
| 897 | */ |
| 898 | int dmz_flush_metadata(struct dmz_metadata *zmd) |
| 899 | { |
| 900 | struct dmz_mblock *mblk; |
| 901 | struct list_head write_list; |
Hannes Reinecke | bf28a3b | 2020-05-11 10:24:20 +0200 | [diff] [blame] | 902 | struct dmz_dev *dev; |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 903 | int ret; |
| 904 | |
| 905 | if (WARN_ON(!zmd)) |
| 906 | return 0; |
| 907 | |
| 908 | INIT_LIST_HEAD(&write_list); |
| 909 | |
| 910 | /* |
| 911 | * Make sure that metadata blocks are stable before logging: take |
| 912 | * the write lock on the metadata semaphore to prevent target BIOs |
| 913 | * from modifying metadata. |
| 914 | */ |
| 915 | down_write(&zmd->mblk_sem); |
Hannes Reinecke | bf28a3b | 2020-05-11 10:24:20 +0200 | [diff] [blame] | 916 | dev = zmd->sb[zmd->mblk_primary].dev; |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 917 | |
| 918 | /* |
| 919 | * This is called from the target flush work and reclaim work. |
| 920 | * Concurrent execution is not allowed. |
| 921 | */ |
| 922 | dmz_lock_flush(zmd); |
| 923 | |
Hannes Reinecke | bf28a3b | 2020-05-11 10:24:20 +0200 | [diff] [blame] | 924 | if (dmz_bdev_is_dying(dev)) { |
Dmitry Fomichev | 75d66ff | 2019-08-10 14:43:11 -0700 | [diff] [blame] | 925 | ret = -EIO; |
| 926 | goto out; |
| 927 | } |
| 928 | |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 929 | /* Get dirty blocks */ |
| 930 | spin_lock(&zmd->mblk_lock); |
| 931 | list_splice_init(&zmd->mblk_dirty_list, &write_list); |
| 932 | spin_unlock(&zmd->mblk_lock); |
| 933 | |
| 934 | /* If there are no dirty metadata blocks, just flush the device cache */ |
| 935 | if (list_empty(&write_list)) { |
Hannes Reinecke | bf28a3b | 2020-05-11 10:24:20 +0200 | [diff] [blame] | 936 | ret = blkdev_issue_flush(dev->bdev, GFP_NOIO, NULL); |
Dmitry Fomichev | e7fad90 | 2019-11-06 14:34:35 -0800 | [diff] [blame] | 937 | goto err; |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 938 | } |
| 939 | |
| 940 | /* |
| 941 | * The primary metadata set is still clean. Keep it this way until |
| 942 | * all updates are successful in the secondary set. That is, use |
| 943 | * the secondary set as a log. |
| 944 | */ |
| 945 | ret = dmz_log_dirty_mblocks(zmd, &write_list); |
| 946 | if (ret) |
Dmitry Fomichev | e7fad90 | 2019-11-06 14:34:35 -0800 | [diff] [blame] | 947 | goto err; |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 948 | |
| 949 | /* |
| 950 | * The log is on disk. It is now safe to update in place |
| 951 | * in the primary metadata set. |
| 952 | */ |
| 953 | ret = dmz_write_dirty_mblocks(zmd, &write_list, zmd->mblk_primary); |
| 954 | if (ret) |
Dmitry Fomichev | e7fad90 | 2019-11-06 14:34:35 -0800 | [diff] [blame] | 955 | goto err; |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 956 | |
| 957 | ret = dmz_write_sb(zmd, zmd->mblk_primary); |
| 958 | if (ret) |
Dmitry Fomichev | e7fad90 | 2019-11-06 14:34:35 -0800 | [diff] [blame] | 959 | goto err; |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 960 | |
| 961 | while (!list_empty(&write_list)) { |
| 962 | mblk = list_first_entry(&write_list, struct dmz_mblock, link); |
| 963 | list_del_init(&mblk->link); |
| 964 | |
| 965 | spin_lock(&zmd->mblk_lock); |
| 966 | clear_bit(DMZ_META_DIRTY, &mblk->state); |
Damien Le Moal | 33c2865 | 2018-10-17 18:05:07 +0900 | [diff] [blame] | 967 | if (mblk->ref == 0) |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 968 | list_add_tail(&mblk->link, &zmd->mblk_lru_list); |
| 969 | spin_unlock(&zmd->mblk_lock); |
| 970 | } |
| 971 | |
| 972 | zmd->sb_gen++; |
| 973 | out: |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 974 | dmz_unlock_flush(zmd); |
| 975 | up_write(&zmd->mblk_sem); |
| 976 | |
| 977 | return ret; |
Dmitry Fomichev | e7fad90 | 2019-11-06 14:34:35 -0800 | [diff] [blame] | 978 | |
| 979 | err: |
| 980 | if (!list_empty(&write_list)) { |
| 981 | spin_lock(&zmd->mblk_lock); |
| 982 | list_splice(&write_list, &zmd->mblk_dirty_list); |
| 983 | spin_unlock(&zmd->mblk_lock); |
| 984 | } |
Hannes Reinecke | bf28a3b | 2020-05-11 10:24:20 +0200 | [diff] [blame] | 985 | if (!dmz_check_bdev(dev)) |
Dmitry Fomichev | e7fad90 | 2019-11-06 14:34:35 -0800 | [diff] [blame] | 986 | ret = -EIO; |
| 987 | goto out; |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 988 | } |
| 989 | |
| 990 | /* |
| 991 | * Check super block. |
| 992 | */ |
Hannes Reinecke | 735bd7e4 | 2020-05-11 10:24:19 +0200 | [diff] [blame] | 993 | static int dmz_check_sb(struct dmz_metadata *zmd, unsigned int set) |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 994 | { |
Hannes Reinecke | 735bd7e4 | 2020-05-11 10:24:19 +0200 | [diff] [blame] | 995 | struct dmz_super *sb = zmd->sb[set].sb; |
Hannes Reinecke | bf28a3b | 2020-05-11 10:24:20 +0200 | [diff] [blame] | 996 | struct dmz_dev *dev = zmd->sb[set].dev; |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 997 | unsigned int nr_meta_zones, nr_data_zones; |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 998 | u32 crc, stored_crc; |
| 999 | u64 gen; |
| 1000 | |
Hannes Reinecke | bd5c403 | 2020-05-11 10:24:30 +0200 | [diff] [blame] | 1001 | if (le32_to_cpu(sb->magic) != DMZ_MAGIC) { |
| 1002 | dmz_dev_err(dev, "Invalid meta magic (needed 0x%08x, got 0x%08x)", |
| 1003 | DMZ_MAGIC, le32_to_cpu(sb->magic)); |
| 1004 | return -ENXIO; |
| 1005 | } |
| 1006 | |
| 1007 | zmd->sb_version = le32_to_cpu(sb->version); |
| 1008 | if (zmd->sb_version > DMZ_META_VER) { |
| 1009 | dmz_dev_err(dev, "Invalid meta version (needed %d, got %d)", |
| 1010 | DMZ_META_VER, zmd->sb_version); |
| 1011 | return -EINVAL; |
| 1012 | } |
| 1013 | if ((zmd->sb_version < 1) && (set == 2)) { |
| 1014 | dmz_dev_err(dev, "Tertiary superblocks are not supported"); |
| 1015 | return -EINVAL; |
| 1016 | } |
| 1017 | |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 1018 | gen = le64_to_cpu(sb->gen); |
| 1019 | stored_crc = le32_to_cpu(sb->crc); |
| 1020 | sb->crc = 0; |
| 1021 | crc = crc32_le(gen, (unsigned char *)sb, DMZ_BLOCK_SIZE); |
| 1022 | if (crc != stored_crc) { |
| 1023 | dmz_dev_err(dev, "Invalid checksum (needed 0x%08x, got 0x%08x)", |
| 1024 | crc, stored_crc); |
| 1025 | return -ENXIO; |
| 1026 | } |
| 1027 | |
Hannes Reinecke | bd5c403 | 2020-05-11 10:24:30 +0200 | [diff] [blame] | 1028 | if (zmd->sb_version > 1) { |
| 1029 | uuid_t sb_uuid; |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 1030 | |
Hannes Reinecke | bd5c403 | 2020-05-11 10:24:30 +0200 | [diff] [blame] | 1031 | import_uuid(&sb_uuid, sb->dmz_uuid); |
| 1032 | if (uuid_is_null(&sb_uuid)) { |
| 1033 | dmz_dev_err(dev, "NULL DM-Zoned uuid"); |
| 1034 | return -ENXIO; |
| 1035 | } else if (uuid_is_null(&zmd->uuid)) { |
| 1036 | uuid_copy(&zmd->uuid, &sb_uuid); |
| 1037 | } else if (!uuid_equal(&zmd->uuid, &sb_uuid)) { |
| 1038 | dmz_dev_err(dev, "mismatching DM-Zoned uuid, " |
| 1039 | "is %pUl expected %pUl", |
| 1040 | &sb_uuid, &zmd->uuid); |
| 1041 | return -ENXIO; |
| 1042 | } |
| 1043 | if (!strlen(zmd->label)) |
| 1044 | memcpy(zmd->label, sb->dmz_label, BDEVNAME_SIZE); |
| 1045 | else if (memcmp(zmd->label, sb->dmz_label, BDEVNAME_SIZE)) { |
| 1046 | dmz_dev_err(dev, "mismatching DM-Zoned label, " |
| 1047 | "is %s expected %s", |
| 1048 | sb->dmz_label, zmd->label); |
| 1049 | return -ENXIO; |
| 1050 | } |
| 1051 | import_uuid(&dev->uuid, sb->dev_uuid); |
| 1052 | if (uuid_is_null(&dev->uuid)) { |
| 1053 | dmz_dev_err(dev, "NULL device uuid"); |
| 1054 | return -ENXIO; |
| 1055 | } |
| 1056 | |
| 1057 | if (set == 2) { |
| 1058 | /* |
| 1059 | * Generation number should be 0, but it doesn't |
| 1060 | * really matter if it isn't. |
| 1061 | */ |
| 1062 | if (gen != 0) |
| 1063 | dmz_dev_warn(dev, "Invalid generation %llu", |
| 1064 | gen); |
| 1065 | return 0; |
| 1066 | } |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 1067 | } |
| 1068 | |
Hannes Reinecke | 3682056 | 2020-05-11 10:24:21 +0200 | [diff] [blame] | 1069 | nr_meta_zones = (le32_to_cpu(sb->nr_meta_blocks) + zmd->zone_nr_blocks - 1) |
| 1070 | >> zmd->zone_nr_blocks_shift; |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 1071 | if (!nr_meta_zones || |
| 1072 | nr_meta_zones >= zmd->nr_rnd_zones) { |
| 1073 | dmz_dev_err(dev, "Invalid number of metadata blocks"); |
| 1074 | return -ENXIO; |
| 1075 | } |
| 1076 | |
| 1077 | if (!le32_to_cpu(sb->nr_reserved_seq) || |
| 1078 | le32_to_cpu(sb->nr_reserved_seq) >= (zmd->nr_useable_zones - nr_meta_zones)) { |
| 1079 | dmz_dev_err(dev, "Invalid number of reserved sequential zones"); |
| 1080 | return -ENXIO; |
| 1081 | } |
| 1082 | |
| 1083 | nr_data_zones = zmd->nr_useable_zones - |
| 1084 | (nr_meta_zones * 2 + le32_to_cpu(sb->nr_reserved_seq)); |
| 1085 | if (le32_to_cpu(sb->nr_chunks) > nr_data_zones) { |
| 1086 | dmz_dev_err(dev, "Invalid number of chunks %u / %u", |
| 1087 | le32_to_cpu(sb->nr_chunks), nr_data_zones); |
| 1088 | return -ENXIO; |
| 1089 | } |
| 1090 | |
| 1091 | /* OK */ |
| 1092 | zmd->nr_meta_blocks = le32_to_cpu(sb->nr_meta_blocks); |
| 1093 | zmd->nr_reserved_seq = le32_to_cpu(sb->nr_reserved_seq); |
| 1094 | zmd->nr_chunks = le32_to_cpu(sb->nr_chunks); |
| 1095 | zmd->nr_map_blocks = le32_to_cpu(sb->nr_map_blocks); |
| 1096 | zmd->nr_bitmap_blocks = le32_to_cpu(sb->nr_bitmap_blocks); |
| 1097 | zmd->nr_meta_zones = nr_meta_zones; |
| 1098 | zmd->nr_data_zones = nr_data_zones; |
| 1099 | |
| 1100 | return 0; |
| 1101 | } |
| 1102 | |
| 1103 | /* |
| 1104 | * Read the first or second super block from disk. |
| 1105 | */ |
| 1106 | static int dmz_read_sb(struct dmz_metadata *zmd, unsigned int set) |
| 1107 | { |
Hannes Reinecke | bf28a3b | 2020-05-11 10:24:20 +0200 | [diff] [blame] | 1108 | return dmz_rdwr_block(zmd->sb[set].dev, REQ_OP_READ, |
| 1109 | zmd->sb[set].block, zmd->sb[set].mblk->page); |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 1110 | } |
| 1111 | |
| 1112 | /* |
| 1113 | * Determine the position of the secondary super blocks on disk. |
| 1114 | * This is used only if a corruption of the primary super block |
| 1115 | * is detected. |
| 1116 | */ |
| 1117 | static int dmz_lookup_secondary_sb(struct dmz_metadata *zmd) |
| 1118 | { |
Hannes Reinecke | 3682056 | 2020-05-11 10:24:21 +0200 | [diff] [blame] | 1119 | unsigned int zone_nr_blocks = zmd->zone_nr_blocks; |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 1120 | struct dmz_mblock *mblk; |
| 1121 | int i; |
| 1122 | |
| 1123 | /* Allocate a block */ |
| 1124 | mblk = dmz_alloc_mblock(zmd, 0); |
| 1125 | if (!mblk) |
| 1126 | return -ENOMEM; |
| 1127 | |
| 1128 | zmd->sb[1].mblk = mblk; |
| 1129 | zmd->sb[1].sb = mblk->data; |
| 1130 | |
| 1131 | /* Bad first super block: search for the second one */ |
| 1132 | zmd->sb[1].block = zmd->sb[0].block + zone_nr_blocks; |
Hannes Reinecke | 735bd7e4 | 2020-05-11 10:24:19 +0200 | [diff] [blame] | 1133 | zmd->sb[1].zone = zmd->sb[0].zone + 1; |
Hannes Reinecke | bf28a3b | 2020-05-11 10:24:20 +0200 | [diff] [blame] | 1134 | zmd->sb[1].dev = dmz_zone_to_dev(zmd, zmd->sb[1].zone); |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 1135 | for (i = 0; i < zmd->nr_rnd_zones - 1; i++) { |
| 1136 | if (dmz_read_sb(zmd, 1) != 0) |
| 1137 | break; |
Hannes Reinecke | 735bd7e4 | 2020-05-11 10:24:19 +0200 | [diff] [blame] | 1138 | if (le32_to_cpu(zmd->sb[1].sb->magic) == DMZ_MAGIC) { |
| 1139 | zmd->sb[1].zone += i; |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 1140 | return 0; |
Hannes Reinecke | 735bd7e4 | 2020-05-11 10:24:19 +0200 | [diff] [blame] | 1141 | } |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 1142 | zmd->sb[1].block += zone_nr_blocks; |
Hannes Reinecke | bf28a3b | 2020-05-11 10:24:20 +0200 | [diff] [blame] | 1143 | zmd->sb[1].dev = dmz_zone_to_dev(zmd, zmd->sb[1].zone + i); |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 1144 | } |
| 1145 | |
| 1146 | dmz_free_mblock(zmd, mblk); |
| 1147 | zmd->sb[1].mblk = NULL; |
Hannes Reinecke | 735bd7e4 | 2020-05-11 10:24:19 +0200 | [diff] [blame] | 1148 | zmd->sb[1].zone = NULL; |
Hannes Reinecke | bf28a3b | 2020-05-11 10:24:20 +0200 | [diff] [blame] | 1149 | zmd->sb[1].dev = NULL; |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 1150 | |
| 1151 | return -EIO; |
| 1152 | } |
| 1153 | |
| 1154 | /* |
| 1155 | * Read the first or second super block from disk. |
| 1156 | */ |
| 1157 | static int dmz_get_sb(struct dmz_metadata *zmd, unsigned int set) |
| 1158 | { |
| 1159 | struct dmz_mblock *mblk; |
| 1160 | int ret; |
| 1161 | |
| 1162 | /* Allocate a block */ |
| 1163 | mblk = dmz_alloc_mblock(zmd, 0); |
| 1164 | if (!mblk) |
| 1165 | return -ENOMEM; |
| 1166 | |
| 1167 | zmd->sb[set].mblk = mblk; |
| 1168 | zmd->sb[set].sb = mblk->data; |
| 1169 | |
| 1170 | /* Read super block */ |
| 1171 | ret = dmz_read_sb(zmd, set); |
| 1172 | if (ret) { |
| 1173 | dmz_free_mblock(zmd, mblk); |
| 1174 | zmd->sb[set].mblk = NULL; |
| 1175 | return ret; |
| 1176 | } |
| 1177 | |
| 1178 | return 0; |
| 1179 | } |
| 1180 | |
| 1181 | /* |
| 1182 | * Recover a metadata set. |
| 1183 | */ |
| 1184 | static int dmz_recover_mblocks(struct dmz_metadata *zmd, unsigned int dst_set) |
| 1185 | { |
| 1186 | unsigned int src_set = dst_set ^ 0x1; |
| 1187 | struct page *page; |
| 1188 | int i, ret; |
| 1189 | |
Hannes Reinecke | bf28a3b | 2020-05-11 10:24:20 +0200 | [diff] [blame] | 1190 | dmz_dev_warn(zmd->sb[dst_set].dev, |
| 1191 | "Metadata set %u invalid: recovering", dst_set); |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 1192 | |
| 1193 | if (dst_set == 0) |
Hannes Reinecke | 735bd7e4 | 2020-05-11 10:24:19 +0200 | [diff] [blame] | 1194 | zmd->sb[0].block = dmz_start_block(zmd, zmd->sb[0].zone); |
| 1195 | else |
| 1196 | zmd->sb[1].block = dmz_start_block(zmd, zmd->sb[1].zone); |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 1197 | |
Damien Le Moal | 4218a95 | 2017-07-24 16:44:37 +0900 | [diff] [blame] | 1198 | page = alloc_page(GFP_NOIO); |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 1199 | if (!page) |
| 1200 | return -ENOMEM; |
| 1201 | |
| 1202 | /* Copy metadata blocks */ |
| 1203 | for (i = 1; i < zmd->nr_meta_blocks; i++) { |
Hannes Reinecke | bf28a3b | 2020-05-11 10:24:20 +0200 | [diff] [blame] | 1204 | ret = dmz_rdwr_block(zmd->sb[src_set].dev, REQ_OP_READ, |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 1205 | zmd->sb[src_set].block + i, page); |
| 1206 | if (ret) |
| 1207 | goto out; |
Hannes Reinecke | bf28a3b | 2020-05-11 10:24:20 +0200 | [diff] [blame] | 1208 | ret = dmz_rdwr_block(zmd->sb[dst_set].dev, REQ_OP_WRITE, |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 1209 | zmd->sb[dst_set].block + i, page); |
| 1210 | if (ret) |
| 1211 | goto out; |
| 1212 | } |
| 1213 | |
| 1214 | /* Finalize with the super block */ |
| 1215 | if (!zmd->sb[dst_set].mblk) { |
| 1216 | zmd->sb[dst_set].mblk = dmz_alloc_mblock(zmd, 0); |
| 1217 | if (!zmd->sb[dst_set].mblk) { |
| 1218 | ret = -ENOMEM; |
| 1219 | goto out; |
| 1220 | } |
| 1221 | zmd->sb[dst_set].sb = zmd->sb[dst_set].mblk->data; |
| 1222 | } |
| 1223 | |
| 1224 | ret = dmz_write_sb(zmd, dst_set); |
| 1225 | out: |
| 1226 | __free_pages(page, 0); |
| 1227 | |
| 1228 | return ret; |
| 1229 | } |
| 1230 | |
| 1231 | /* |
| 1232 | * Get super block from disk. |
| 1233 | */ |
| 1234 | static int dmz_load_sb(struct dmz_metadata *zmd) |
| 1235 | { |
| 1236 | bool sb_good[2] = {false, false}; |
| 1237 | u64 sb_gen[2] = {0, 0}; |
| 1238 | int ret; |
| 1239 | |
Hannes Reinecke | 735bd7e4 | 2020-05-11 10:24:19 +0200 | [diff] [blame] | 1240 | if (!zmd->sb[0].zone) { |
Hannes Reinecke | ca1a704 | 2020-05-11 10:24:27 +0200 | [diff] [blame] | 1241 | dmz_zmd_err(zmd, "Primary super block zone not set"); |
Hannes Reinecke | 735bd7e4 | 2020-05-11 10:24:19 +0200 | [diff] [blame] | 1242 | return -ENXIO; |
| 1243 | } |
| 1244 | |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 1245 | /* Read and check the primary super block */ |
Hannes Reinecke | 735bd7e4 | 2020-05-11 10:24:19 +0200 | [diff] [blame] | 1246 | zmd->sb[0].block = dmz_start_block(zmd, zmd->sb[0].zone); |
Hannes Reinecke | bf28a3b | 2020-05-11 10:24:20 +0200 | [diff] [blame] | 1247 | zmd->sb[0].dev = dmz_zone_to_dev(zmd, zmd->sb[0].zone); |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 1248 | ret = dmz_get_sb(zmd, 0); |
| 1249 | if (ret) { |
Hannes Reinecke | bf28a3b | 2020-05-11 10:24:20 +0200 | [diff] [blame] | 1250 | dmz_dev_err(zmd->sb[0].dev, "Read primary super block failed"); |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 1251 | return ret; |
| 1252 | } |
| 1253 | |
Hannes Reinecke | 735bd7e4 | 2020-05-11 10:24:19 +0200 | [diff] [blame] | 1254 | ret = dmz_check_sb(zmd, 0); |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 1255 | |
| 1256 | /* Read and check secondary super block */ |
| 1257 | if (ret == 0) { |
| 1258 | sb_good[0] = true; |
Hannes Reinecke | 735bd7e4 | 2020-05-11 10:24:19 +0200 | [diff] [blame] | 1259 | if (!zmd->sb[1].zone) |
| 1260 | zmd->sb[1].zone = zmd->sb[0].zone + zmd->nr_meta_zones; |
| 1261 | zmd->sb[1].block = dmz_start_block(zmd, zmd->sb[1].zone); |
Hannes Reinecke | bf28a3b | 2020-05-11 10:24:20 +0200 | [diff] [blame] | 1262 | zmd->sb[1].dev = dmz_zone_to_dev(zmd, zmd->sb[1].zone); |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 1263 | ret = dmz_get_sb(zmd, 1); |
| 1264 | } else |
| 1265 | ret = dmz_lookup_secondary_sb(zmd); |
| 1266 | |
| 1267 | if (ret) { |
Hannes Reinecke | bf28a3b | 2020-05-11 10:24:20 +0200 | [diff] [blame] | 1268 | dmz_dev_err(zmd->sb[1].dev, "Read secondary super block failed"); |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 1269 | return ret; |
| 1270 | } |
| 1271 | |
Hannes Reinecke | 735bd7e4 | 2020-05-11 10:24:19 +0200 | [diff] [blame] | 1272 | ret = dmz_check_sb(zmd, 1); |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 1273 | if (ret == 0) |
| 1274 | sb_good[1] = true; |
| 1275 | |
| 1276 | /* Use highest generation sb first */ |
| 1277 | if (!sb_good[0] && !sb_good[1]) { |
Hannes Reinecke | ca1a704 | 2020-05-11 10:24:27 +0200 | [diff] [blame] | 1278 | dmz_zmd_err(zmd, "No valid super block found"); |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 1279 | return -EIO; |
| 1280 | } |
| 1281 | |
| 1282 | if (sb_good[0]) |
| 1283 | sb_gen[0] = le64_to_cpu(zmd->sb[0].sb->gen); |
Hannes Reinecke | bf28a3b | 2020-05-11 10:24:20 +0200 | [diff] [blame] | 1284 | else { |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 1285 | ret = dmz_recover_mblocks(zmd, 0); |
Hannes Reinecke | bf28a3b | 2020-05-11 10:24:20 +0200 | [diff] [blame] | 1286 | if (ret) { |
| 1287 | dmz_dev_err(zmd->sb[0].dev, |
| 1288 | "Recovery of superblock 0 failed"); |
| 1289 | return -EIO; |
| 1290 | } |
| 1291 | } |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 1292 | |
| 1293 | if (sb_good[1]) |
| 1294 | sb_gen[1] = le64_to_cpu(zmd->sb[1].sb->gen); |
Hannes Reinecke | bf28a3b | 2020-05-11 10:24:20 +0200 | [diff] [blame] | 1295 | else { |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 1296 | ret = dmz_recover_mblocks(zmd, 1); |
| 1297 | |
Hannes Reinecke | bf28a3b | 2020-05-11 10:24:20 +0200 | [diff] [blame] | 1298 | if (ret) { |
| 1299 | dmz_dev_err(zmd->sb[1].dev, |
| 1300 | "Recovery of superblock 1 failed"); |
| 1301 | return -EIO; |
| 1302 | } |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 1303 | } |
| 1304 | |
| 1305 | if (sb_gen[0] >= sb_gen[1]) { |
| 1306 | zmd->sb_gen = sb_gen[0]; |
| 1307 | zmd->mblk_primary = 0; |
| 1308 | } else { |
| 1309 | zmd->sb_gen = sb_gen[1]; |
| 1310 | zmd->mblk_primary = 1; |
| 1311 | } |
| 1312 | |
Hannes Reinecke | bf28a3b | 2020-05-11 10:24:20 +0200 | [diff] [blame] | 1313 | dmz_dev_debug(zmd->sb[zmd->mblk_primary].dev, |
| 1314 | "Using super block %u (gen %llu)", |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 1315 | zmd->mblk_primary, zmd->sb_gen); |
| 1316 | |
Hannes Reinecke | bd5c403 | 2020-05-11 10:24:30 +0200 | [diff] [blame] | 1317 | if ((zmd->sb_version > 1) && zmd->sb[2].zone) { |
| 1318 | zmd->sb[2].block = dmz_start_block(zmd, zmd->sb[2].zone); |
| 1319 | zmd->sb[2].dev = dmz_zone_to_dev(zmd, zmd->sb[2].zone); |
| 1320 | ret = dmz_get_sb(zmd, 2); |
| 1321 | if (ret) { |
| 1322 | dmz_dev_err(zmd->sb[2].dev, |
| 1323 | "Read tertiary super block failed"); |
| 1324 | return ret; |
| 1325 | } |
| 1326 | ret = dmz_check_sb(zmd, 2); |
| 1327 | if (ret == -EINVAL) |
| 1328 | return ret; |
| 1329 | } |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 1330 | return 0; |
| 1331 | } |
| 1332 | |
| 1333 | /* |
| 1334 | * Initialize a zone descriptor. |
| 1335 | */ |
Hannes Reinecke | bd5c403 | 2020-05-11 10:24:30 +0200 | [diff] [blame] | 1336 | static int dmz_init_zone(struct blk_zone *blkz, unsigned int num, void *data) |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 1337 | { |
Christoph Hellwig | d410035 | 2019-11-11 11:39:30 +0900 | [diff] [blame] | 1338 | struct dmz_metadata *zmd = data; |
Hannes Reinecke | bd5c403 | 2020-05-11 10:24:30 +0200 | [diff] [blame] | 1339 | struct dmz_dev *dev = zmd->nr_devs > 1 ? &zmd->dev[1] : &zmd->dev[0]; |
| 1340 | int idx = num + dev->zone_offset; |
Christoph Hellwig | d410035 | 2019-11-11 11:39:30 +0900 | [diff] [blame] | 1341 | struct dm_zone *zone = &zmd->zones[idx]; |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 1342 | |
Hannes Reinecke | 3682056 | 2020-05-11 10:24:21 +0200 | [diff] [blame] | 1343 | if (blkz->len != zmd->zone_nr_sectors) { |
Hannes Reinecke | bd5c403 | 2020-05-11 10:24:30 +0200 | [diff] [blame] | 1344 | if (zmd->sb_version > 1) { |
| 1345 | /* Ignore the eventual runt (smaller) zone */ |
| 1346 | set_bit(DMZ_OFFLINE, &zone->flags); |
| 1347 | return 0; |
| 1348 | } else if (blkz->start + blkz->len == dev->capacity) |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 1349 | return 0; |
| 1350 | return -ENXIO; |
| 1351 | } |
| 1352 | |
| 1353 | INIT_LIST_HEAD(&zone->link); |
| 1354 | atomic_set(&zone->refcount, 0); |
Hannes Reinecke | b712287 | 2020-05-11 10:24:18 +0200 | [diff] [blame] | 1355 | zone->id = idx; |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 1356 | zone->chunk = DMZ_MAP_UNMAPPED; |
| 1357 | |
Christoph Hellwig | d410035 | 2019-11-11 11:39:30 +0900 | [diff] [blame] | 1358 | switch (blkz->type) { |
| 1359 | case BLK_ZONE_TYPE_CONVENTIONAL: |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 1360 | set_bit(DMZ_RND, &zone->flags); |
Christoph Hellwig | d410035 | 2019-11-11 11:39:30 +0900 | [diff] [blame] | 1361 | break; |
| 1362 | case BLK_ZONE_TYPE_SEQWRITE_REQ: |
| 1363 | case BLK_ZONE_TYPE_SEQWRITE_PREF: |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 1364 | set_bit(DMZ_SEQ, &zone->flags); |
Christoph Hellwig | d410035 | 2019-11-11 11:39:30 +0900 | [diff] [blame] | 1365 | break; |
| 1366 | default: |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 1367 | return -ENXIO; |
Christoph Hellwig | d410035 | 2019-11-11 11:39:30 +0900 | [diff] [blame] | 1368 | } |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 1369 | |
| 1370 | if (dmz_is_rnd(zone)) |
| 1371 | zone->wp_block = 0; |
| 1372 | else |
| 1373 | zone->wp_block = dmz_sect2blk(blkz->wp - blkz->start); |
| 1374 | |
Christoph Hellwig | d410035 | 2019-11-11 11:39:30 +0900 | [diff] [blame] | 1375 | if (blkz->cond == BLK_ZONE_COND_OFFLINE) |
| 1376 | set_bit(DMZ_OFFLINE, &zone->flags); |
| 1377 | else if (blkz->cond == BLK_ZONE_COND_READONLY) |
| 1378 | set_bit(DMZ_READ_ONLY, &zone->flags); |
| 1379 | else { |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 1380 | zmd->nr_useable_zones++; |
| 1381 | if (dmz_is_rnd(zone)) { |
| 1382 | zmd->nr_rnd_zones++; |
Hannes Reinecke | bd5c403 | 2020-05-11 10:24:30 +0200 | [diff] [blame] | 1383 | if (zmd->nr_devs == 1 && !zmd->sb[0].zone) { |
| 1384 | /* Primary super block zone */ |
Hannes Reinecke | 735bd7e4 | 2020-05-11 10:24:19 +0200 | [diff] [blame] | 1385 | zmd->sb[0].zone = zone; |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 1386 | } |
| 1387 | } |
Hannes Reinecke | bd5c403 | 2020-05-11 10:24:30 +0200 | [diff] [blame] | 1388 | if (zmd->nr_devs > 1 && !zmd->sb[2].zone) { |
| 1389 | /* Tertiary superblock zone */ |
| 1390 | zmd->sb[2].zone = zone; |
| 1391 | } |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 1392 | } |
| 1393 | |
| 1394 | return 0; |
| 1395 | } |
| 1396 | |
Hannes Reinecke | bd5c403 | 2020-05-11 10:24:30 +0200 | [diff] [blame] | 1397 | static void dmz_emulate_zones(struct dmz_metadata *zmd, struct dmz_dev *dev) |
| 1398 | { |
| 1399 | int idx; |
| 1400 | sector_t zone_offset = 0; |
| 1401 | |
| 1402 | for(idx = 0; idx < dev->nr_zones; idx++) { |
| 1403 | struct dm_zone *zone = &zmd->zones[idx]; |
| 1404 | |
| 1405 | INIT_LIST_HEAD(&zone->link); |
| 1406 | atomic_set(&zone->refcount, 0); |
| 1407 | zone->id = idx; |
| 1408 | zone->chunk = DMZ_MAP_UNMAPPED; |
Hannes Reinecke | 34f5aff | 2020-05-19 10:14:20 +0200 | [diff] [blame] | 1409 | set_bit(DMZ_CACHE, &zone->flags); |
Hannes Reinecke | bd5c403 | 2020-05-11 10:24:30 +0200 | [diff] [blame] | 1410 | zone->wp_block = 0; |
Hannes Reinecke | 34f5aff | 2020-05-19 10:14:20 +0200 | [diff] [blame] | 1411 | zmd->nr_cache_zones++; |
Hannes Reinecke | bd5c403 | 2020-05-11 10:24:30 +0200 | [diff] [blame] | 1412 | zmd->nr_useable_zones++; |
| 1413 | if (dev->capacity - zone_offset < zmd->zone_nr_sectors) { |
| 1414 | /* Disable runt zone */ |
| 1415 | set_bit(DMZ_OFFLINE, &zone->flags); |
| 1416 | break; |
| 1417 | } |
| 1418 | zone_offset += zmd->zone_nr_sectors; |
| 1419 | } |
| 1420 | } |
| 1421 | |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 1422 | /* |
| 1423 | * Free zones descriptors. |
| 1424 | */ |
| 1425 | static void dmz_drop_zones(struct dmz_metadata *zmd) |
| 1426 | { |
| 1427 | kfree(zmd->zones); |
| 1428 | zmd->zones = NULL; |
| 1429 | } |
| 1430 | |
| 1431 | /* |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 1432 | * Allocate and initialize zone descriptors using the zone |
| 1433 | * information from disk. |
| 1434 | */ |
| 1435 | static int dmz_init_zones(struct dmz_metadata *zmd) |
| 1436 | { |
Hannes Reinecke | bd5c403 | 2020-05-11 10:24:30 +0200 | [diff] [blame] | 1437 | int i, ret; |
| 1438 | struct dmz_dev *zoned_dev = &zmd->dev[0]; |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 1439 | |
| 1440 | /* Init */ |
Hannes Reinecke | bd5c403 | 2020-05-11 10:24:30 +0200 | [diff] [blame] | 1441 | zmd->zone_nr_sectors = zmd->dev[0].zone_nr_sectors; |
Hannes Reinecke | 3682056 | 2020-05-11 10:24:21 +0200 | [diff] [blame] | 1442 | zmd->zone_nr_sectors_shift = ilog2(zmd->zone_nr_sectors); |
| 1443 | zmd->zone_nr_blocks = dmz_sect2blk(zmd->zone_nr_sectors); |
| 1444 | zmd->zone_nr_blocks_shift = ilog2(zmd->zone_nr_blocks); |
| 1445 | zmd->zone_bitmap_size = zmd->zone_nr_blocks >> 3; |
Dmitry Fomichev | b399629 | 2019-12-23 17:05:46 -0800 | [diff] [blame] | 1446 | zmd->zone_nr_bitmap_blocks = |
| 1447 | max_t(sector_t, 1, zmd->zone_bitmap_size >> DMZ_BLOCK_SHIFT); |
Hannes Reinecke | 3682056 | 2020-05-11 10:24:21 +0200 | [diff] [blame] | 1448 | zmd->zone_bits_per_mblk = min_t(sector_t, zmd->zone_nr_blocks, |
Dmitry Fomichev | b399629 | 2019-12-23 17:05:46 -0800 | [diff] [blame] | 1449 | DMZ_BLOCK_SIZE_BITS); |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 1450 | |
| 1451 | /* Allocate zone array */ |
Hannes Reinecke | bd5c403 | 2020-05-11 10:24:30 +0200 | [diff] [blame] | 1452 | zmd->nr_zones = 0; |
| 1453 | for (i = 0; i < zmd->nr_devs; i++) |
| 1454 | zmd->nr_zones += zmd->dev[i].nr_zones; |
| 1455 | |
| 1456 | if (!zmd->nr_zones) { |
| 1457 | DMERR("(%s): No zones found", zmd->devname); |
| 1458 | return -ENXIO; |
| 1459 | } |
Hannes Reinecke | 3682056 | 2020-05-11 10:24:21 +0200 | [diff] [blame] | 1460 | zmd->zones = kcalloc(zmd->nr_zones, sizeof(struct dm_zone), GFP_KERNEL); |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 1461 | if (!zmd->zones) |
| 1462 | return -ENOMEM; |
| 1463 | |
Hannes Reinecke | ae3c1f1 | 2020-05-11 10:24:28 +0200 | [diff] [blame] | 1464 | DMDEBUG("(%s): Using %zu B for zone information", |
| 1465 | zmd->devname, sizeof(struct dm_zone) * zmd->nr_zones); |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 1466 | |
Hannes Reinecke | bd5c403 | 2020-05-11 10:24:30 +0200 | [diff] [blame] | 1467 | if (zmd->nr_devs > 1) { |
| 1468 | dmz_emulate_zones(zmd, &zmd->dev[0]); |
| 1469 | /* |
| 1470 | * Primary superblock zone is always at zone 0 when multiple |
| 1471 | * drives are present. |
| 1472 | */ |
| 1473 | zmd->sb[0].zone = &zmd->zones[0]; |
| 1474 | |
| 1475 | zoned_dev = &zmd->dev[1]; |
| 1476 | } |
| 1477 | |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 1478 | /* |
Christoph Hellwig | d410035 | 2019-11-11 11:39:30 +0900 | [diff] [blame] | 1479 | * Get zone information and initialize zone descriptors. At the same |
| 1480 | * time, determine where the super block should be: first block of the |
| 1481 | * first randomly writable zone. |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 1482 | */ |
Hannes Reinecke | bd5c403 | 2020-05-11 10:24:30 +0200 | [diff] [blame] | 1483 | ret = blkdev_report_zones(zoned_dev->bdev, 0, BLK_ALL_ZONES, |
| 1484 | dmz_init_zone, zmd); |
Christoph Hellwig | d410035 | 2019-11-11 11:39:30 +0900 | [diff] [blame] | 1485 | if (ret < 0) { |
Hannes Reinecke | bd5c403 | 2020-05-11 10:24:30 +0200 | [diff] [blame] | 1486 | DMDEBUG("(%s): Failed to report zones, error %d", |
| 1487 | zmd->devname, ret); |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 1488 | dmz_drop_zones(zmd); |
Christoph Hellwig | d410035 | 2019-11-11 11:39:30 +0900 | [diff] [blame] | 1489 | return ret; |
| 1490 | } |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 1491 | |
Christoph Hellwig | d410035 | 2019-11-11 11:39:30 +0900 | [diff] [blame] | 1492 | return 0; |
| 1493 | } |
| 1494 | |
| 1495 | static int dmz_update_zone_cb(struct blk_zone *blkz, unsigned int idx, |
| 1496 | void *data) |
| 1497 | { |
| 1498 | struct dm_zone *zone = data; |
| 1499 | |
| 1500 | clear_bit(DMZ_OFFLINE, &zone->flags); |
| 1501 | clear_bit(DMZ_READ_ONLY, &zone->flags); |
| 1502 | if (blkz->cond == BLK_ZONE_COND_OFFLINE) |
| 1503 | set_bit(DMZ_OFFLINE, &zone->flags); |
| 1504 | else if (blkz->cond == BLK_ZONE_COND_READONLY) |
| 1505 | set_bit(DMZ_READ_ONLY, &zone->flags); |
| 1506 | |
| 1507 | if (dmz_is_seq(zone)) |
| 1508 | zone->wp_block = dmz_sect2blk(blkz->wp - blkz->start); |
| 1509 | else |
| 1510 | zone->wp_block = 0; |
| 1511 | return 0; |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 1512 | } |
| 1513 | |
| 1514 | /* |
| 1515 | * Update a zone information. |
| 1516 | */ |
| 1517 | static int dmz_update_zone(struct dmz_metadata *zmd, struct dm_zone *zone) |
| 1518 | { |
Hannes Reinecke | aa821c8 | 2020-05-11 10:24:26 +0200 | [diff] [blame] | 1519 | struct dmz_dev *dev = dmz_zone_to_dev(zmd, zone); |
Damien Le Moal | bd976e5 | 2019-07-01 14:09:16 +0900 | [diff] [blame] | 1520 | unsigned int noio_flag; |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 1521 | int ret; |
| 1522 | |
Hannes Reinecke | bd5c403 | 2020-05-11 10:24:30 +0200 | [diff] [blame] | 1523 | if (dev->flags & DMZ_BDEV_REGULAR) |
| 1524 | return 0; |
| 1525 | |
Damien Le Moal | bd976e5 | 2019-07-01 14:09:16 +0900 | [diff] [blame] | 1526 | /* |
| 1527 | * Get zone information from disk. Since blkdev_report_zones() uses |
| 1528 | * GFP_KERNEL by default for memory allocations, set the per-task |
| 1529 | * PF_MEMALLOC_NOIO flag so that all allocations are done as if |
| 1530 | * GFP_NOIO was specified. |
| 1531 | */ |
| 1532 | noio_flag = memalloc_noio_save(); |
Hannes Reinecke | aa821c8 | 2020-05-11 10:24:26 +0200 | [diff] [blame] | 1533 | ret = blkdev_report_zones(dev->bdev, dmz_start_sect(zmd, zone), 1, |
Christoph Hellwig | d410035 | 2019-11-11 11:39:30 +0900 | [diff] [blame] | 1534 | dmz_update_zone_cb, zone); |
Damien Le Moal | bd976e5 | 2019-07-01 14:09:16 +0900 | [diff] [blame] | 1535 | memalloc_noio_restore(noio_flag); |
Christoph Hellwig | d410035 | 2019-11-11 11:39:30 +0900 | [diff] [blame] | 1536 | |
| 1537 | if (ret == 0) |
Damien Le Moal | 7aedf75 | 2019-04-18 18:03:07 +0900 | [diff] [blame] | 1538 | ret = -EIO; |
Christoph Hellwig | d410035 | 2019-11-11 11:39:30 +0900 | [diff] [blame] | 1539 | if (ret < 0) { |
Hannes Reinecke | aa821c8 | 2020-05-11 10:24:26 +0200 | [diff] [blame] | 1540 | dmz_dev_err(dev, "Get zone %u report failed", |
Hannes Reinecke | b712287 | 2020-05-11 10:24:18 +0200 | [diff] [blame] | 1541 | zone->id); |
Hannes Reinecke | aa821c8 | 2020-05-11 10:24:26 +0200 | [diff] [blame] | 1542 | dmz_check_bdev(dev); |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 1543 | return ret; |
| 1544 | } |
| 1545 | |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 1546 | return 0; |
| 1547 | } |
| 1548 | |
| 1549 | /* |
| 1550 | * Check a zone write pointer position when the zone is marked |
| 1551 | * with the sequential write error flag. |
| 1552 | */ |
| 1553 | static int dmz_handle_seq_write_err(struct dmz_metadata *zmd, |
| 1554 | struct dm_zone *zone) |
| 1555 | { |
Hannes Reinecke | aa821c8 | 2020-05-11 10:24:26 +0200 | [diff] [blame] | 1556 | struct dmz_dev *dev = dmz_zone_to_dev(zmd, zone); |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 1557 | unsigned int wp = 0; |
| 1558 | int ret; |
| 1559 | |
| 1560 | wp = zone->wp_block; |
| 1561 | ret = dmz_update_zone(zmd, zone); |
| 1562 | if (ret) |
| 1563 | return ret; |
| 1564 | |
Hannes Reinecke | aa821c8 | 2020-05-11 10:24:26 +0200 | [diff] [blame] | 1565 | dmz_dev_warn(dev, "Processing zone %u write error (zone wp %u/%u)", |
Hannes Reinecke | b712287 | 2020-05-11 10:24:18 +0200 | [diff] [blame] | 1566 | zone->id, zone->wp_block, wp); |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 1567 | |
| 1568 | if (zone->wp_block < wp) { |
| 1569 | dmz_invalidate_blocks(zmd, zone, zone->wp_block, |
| 1570 | wp - zone->wp_block); |
| 1571 | } |
| 1572 | |
| 1573 | return 0; |
| 1574 | } |
| 1575 | |
| 1576 | static struct dm_zone *dmz_get(struct dmz_metadata *zmd, unsigned int zone_id) |
| 1577 | { |
| 1578 | return &zmd->zones[zone_id]; |
| 1579 | } |
| 1580 | |
| 1581 | /* |
| 1582 | * Reset a zone write pointer. |
| 1583 | */ |
| 1584 | static int dmz_reset_zone(struct dmz_metadata *zmd, struct dm_zone *zone) |
| 1585 | { |
| 1586 | int ret; |
| 1587 | |
| 1588 | /* |
| 1589 | * Ignore offline zones, read only zones, |
| 1590 | * and conventional zones. |
| 1591 | */ |
| 1592 | if (dmz_is_offline(zone) || |
| 1593 | dmz_is_readonly(zone) || |
| 1594 | dmz_is_rnd(zone)) |
| 1595 | return 0; |
| 1596 | |
| 1597 | if (!dmz_is_empty(zone) || dmz_seq_write_err(zone)) { |
Hannes Reinecke | aa821c8 | 2020-05-11 10:24:26 +0200 | [diff] [blame] | 1598 | struct dmz_dev *dev = dmz_zone_to_dev(zmd, zone); |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 1599 | |
Ajay Joshi | 6c1b1da | 2019-10-27 23:05:45 +0900 | [diff] [blame] | 1600 | ret = blkdev_zone_mgmt(dev->bdev, REQ_OP_ZONE_RESET, |
| 1601 | dmz_start_sect(zmd, zone), |
Hannes Reinecke | 3682056 | 2020-05-11 10:24:21 +0200 | [diff] [blame] | 1602 | zmd->zone_nr_sectors, GFP_NOIO); |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 1603 | if (ret) { |
| 1604 | dmz_dev_err(dev, "Reset zone %u failed %d", |
Hannes Reinecke | b712287 | 2020-05-11 10:24:18 +0200 | [diff] [blame] | 1605 | zone->id, ret); |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 1606 | return ret; |
| 1607 | } |
| 1608 | } |
| 1609 | |
| 1610 | /* Clear write error bit and rewind write pointer position */ |
| 1611 | clear_bit(DMZ_SEQ_WRITE_ERR, &zone->flags); |
| 1612 | zone->wp_block = 0; |
| 1613 | |
| 1614 | return 0; |
| 1615 | } |
| 1616 | |
| 1617 | static void dmz_get_zone_weight(struct dmz_metadata *zmd, struct dm_zone *zone); |
| 1618 | |
| 1619 | /* |
| 1620 | * Initialize chunk mapping. |
| 1621 | */ |
| 1622 | static int dmz_load_mapping(struct dmz_metadata *zmd) |
| 1623 | { |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 1624 | struct dm_zone *dzone, *bzone; |
| 1625 | struct dmz_mblock *dmap_mblk = NULL; |
| 1626 | struct dmz_map *dmap; |
| 1627 | unsigned int i = 0, e = 0, chunk = 0; |
| 1628 | unsigned int dzone_id; |
| 1629 | unsigned int bzone_id; |
| 1630 | |
| 1631 | /* Metadata block array for the chunk mapping table */ |
| 1632 | zmd->map_mblk = kcalloc(zmd->nr_map_blocks, |
| 1633 | sizeof(struct dmz_mblk *), GFP_KERNEL); |
| 1634 | if (!zmd->map_mblk) |
| 1635 | return -ENOMEM; |
| 1636 | |
| 1637 | /* Get chunk mapping table blocks and initialize zone mapping */ |
| 1638 | while (chunk < zmd->nr_chunks) { |
| 1639 | if (!dmap_mblk) { |
| 1640 | /* Get mapping block */ |
| 1641 | dmap_mblk = dmz_get_mblock(zmd, i + 1); |
| 1642 | if (IS_ERR(dmap_mblk)) |
| 1643 | return PTR_ERR(dmap_mblk); |
| 1644 | zmd->map_mblk[i] = dmap_mblk; |
| 1645 | dmap = (struct dmz_map *) dmap_mblk->data; |
| 1646 | i++; |
| 1647 | e = 0; |
| 1648 | } |
| 1649 | |
| 1650 | /* Check data zone */ |
| 1651 | dzone_id = le32_to_cpu(dmap[e].dzone_id); |
| 1652 | if (dzone_id == DMZ_MAP_UNMAPPED) |
| 1653 | goto next; |
| 1654 | |
Hannes Reinecke | 3682056 | 2020-05-11 10:24:21 +0200 | [diff] [blame] | 1655 | if (dzone_id >= zmd->nr_zones) { |
Hannes Reinecke | ca1a704 | 2020-05-11 10:24:27 +0200 | [diff] [blame] | 1656 | dmz_zmd_err(zmd, "Chunk %u mapping: invalid data zone ID %u", |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 1657 | chunk, dzone_id); |
| 1658 | return -EIO; |
| 1659 | } |
| 1660 | |
| 1661 | dzone = dmz_get(zmd, dzone_id); |
| 1662 | set_bit(DMZ_DATA, &dzone->flags); |
| 1663 | dzone->chunk = chunk; |
| 1664 | dmz_get_zone_weight(zmd, dzone); |
| 1665 | |
Hannes Reinecke | 34f5aff | 2020-05-19 10:14:20 +0200 | [diff] [blame] | 1666 | if (dmz_is_cache(dzone)) |
| 1667 | list_add_tail(&dzone->link, &zmd->map_cache_list); |
| 1668 | else if (dmz_is_rnd(dzone)) |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 1669 | list_add_tail(&dzone->link, &zmd->map_rnd_list); |
| 1670 | else |
| 1671 | list_add_tail(&dzone->link, &zmd->map_seq_list); |
| 1672 | |
| 1673 | /* Check buffer zone */ |
| 1674 | bzone_id = le32_to_cpu(dmap[e].bzone_id); |
| 1675 | if (bzone_id == DMZ_MAP_UNMAPPED) |
| 1676 | goto next; |
| 1677 | |
Hannes Reinecke | 3682056 | 2020-05-11 10:24:21 +0200 | [diff] [blame] | 1678 | if (bzone_id >= zmd->nr_zones) { |
Hannes Reinecke | ca1a704 | 2020-05-11 10:24:27 +0200 | [diff] [blame] | 1679 | dmz_zmd_err(zmd, "Chunk %u mapping: invalid buffer zone ID %u", |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 1680 | chunk, bzone_id); |
| 1681 | return -EIO; |
| 1682 | } |
| 1683 | |
| 1684 | bzone = dmz_get(zmd, bzone_id); |
Hannes Reinecke | 34f5aff | 2020-05-19 10:14:20 +0200 | [diff] [blame] | 1685 | if (!dmz_is_rnd(bzone) && !dmz_is_cache(bzone)) { |
Hannes Reinecke | ca1a704 | 2020-05-11 10:24:27 +0200 | [diff] [blame] | 1686 | dmz_zmd_err(zmd, "Chunk %u mapping: invalid buffer zone %u", |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 1687 | chunk, bzone_id); |
| 1688 | return -EIO; |
| 1689 | } |
| 1690 | |
| 1691 | set_bit(DMZ_DATA, &bzone->flags); |
| 1692 | set_bit(DMZ_BUF, &bzone->flags); |
| 1693 | bzone->chunk = chunk; |
| 1694 | bzone->bzone = dzone; |
| 1695 | dzone->bzone = bzone; |
| 1696 | dmz_get_zone_weight(zmd, bzone); |
Hannes Reinecke | 34f5aff | 2020-05-19 10:14:20 +0200 | [diff] [blame] | 1697 | if (dmz_is_cache(bzone)) |
| 1698 | list_add_tail(&bzone->link, &zmd->map_cache_list); |
| 1699 | else |
| 1700 | list_add_tail(&bzone->link, &zmd->map_rnd_list); |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 1701 | next: |
| 1702 | chunk++; |
| 1703 | e++; |
| 1704 | if (e >= DMZ_MAP_ENTRIES) |
| 1705 | dmap_mblk = NULL; |
| 1706 | } |
| 1707 | |
| 1708 | /* |
| 1709 | * At this point, only meta zones and mapped data zones were |
| 1710 | * fully initialized. All remaining zones are unmapped data |
| 1711 | * zones. Finish initializing those here. |
| 1712 | */ |
Hannes Reinecke | 3682056 | 2020-05-11 10:24:21 +0200 | [diff] [blame] | 1713 | for (i = 0; i < zmd->nr_zones; i++) { |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 1714 | dzone = dmz_get(zmd, i); |
| 1715 | if (dmz_is_meta(dzone)) |
| 1716 | continue; |
Hannes Reinecke | 34f5aff | 2020-05-19 10:14:20 +0200 | [diff] [blame] | 1717 | if (dmz_is_offline(dzone)) |
| 1718 | continue; |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 1719 | |
Hannes Reinecke | 34f5aff | 2020-05-19 10:14:20 +0200 | [diff] [blame] | 1720 | if (dmz_is_cache(dzone)) |
| 1721 | zmd->nr_cache++; |
| 1722 | else if (dmz_is_rnd(dzone)) |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 1723 | zmd->nr_rnd++; |
| 1724 | else |
| 1725 | zmd->nr_seq++; |
| 1726 | |
| 1727 | if (dmz_is_data(dzone)) { |
| 1728 | /* Already initialized */ |
| 1729 | continue; |
| 1730 | } |
| 1731 | |
| 1732 | /* Unmapped data zone */ |
| 1733 | set_bit(DMZ_DATA, &dzone->flags); |
| 1734 | dzone->chunk = DMZ_MAP_UNMAPPED; |
Hannes Reinecke | 34f5aff | 2020-05-19 10:14:20 +0200 | [diff] [blame] | 1735 | if (dmz_is_cache(dzone)) { |
| 1736 | list_add_tail(&dzone->link, &zmd->unmap_cache_list); |
| 1737 | atomic_inc(&zmd->unmap_nr_cache); |
| 1738 | } else if (dmz_is_rnd(dzone)) { |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 1739 | list_add_tail(&dzone->link, &zmd->unmap_rnd_list); |
| 1740 | atomic_inc(&zmd->unmap_nr_rnd); |
| 1741 | } else if (atomic_read(&zmd->nr_reserved_seq_zones) < zmd->nr_reserved_seq) { |
| 1742 | list_add_tail(&dzone->link, &zmd->reserved_seq_zones_list); |
| 1743 | atomic_inc(&zmd->nr_reserved_seq_zones); |
| 1744 | zmd->nr_seq--; |
| 1745 | } else { |
| 1746 | list_add_tail(&dzone->link, &zmd->unmap_seq_list); |
| 1747 | atomic_inc(&zmd->unmap_nr_seq); |
| 1748 | } |
| 1749 | } |
| 1750 | |
| 1751 | return 0; |
| 1752 | } |
| 1753 | |
| 1754 | /* |
| 1755 | * Set a data chunk mapping. |
| 1756 | */ |
| 1757 | static void dmz_set_chunk_mapping(struct dmz_metadata *zmd, unsigned int chunk, |
| 1758 | unsigned int dzone_id, unsigned int bzone_id) |
| 1759 | { |
| 1760 | struct dmz_mblock *dmap_mblk = zmd->map_mblk[chunk >> DMZ_MAP_ENTRIES_SHIFT]; |
| 1761 | struct dmz_map *dmap = (struct dmz_map *) dmap_mblk->data; |
| 1762 | int map_idx = chunk & DMZ_MAP_ENTRIES_MASK; |
| 1763 | |
| 1764 | dmap[map_idx].dzone_id = cpu_to_le32(dzone_id); |
| 1765 | dmap[map_idx].bzone_id = cpu_to_le32(bzone_id); |
| 1766 | dmz_dirty_mblock(zmd, dmap_mblk); |
| 1767 | } |
| 1768 | |
| 1769 | /* |
| 1770 | * The list of mapped zones is maintained in LRU order. |
| 1771 | * This rotates a zone at the end of its map list. |
| 1772 | */ |
| 1773 | static void __dmz_lru_zone(struct dmz_metadata *zmd, struct dm_zone *zone) |
| 1774 | { |
| 1775 | if (list_empty(&zone->link)) |
| 1776 | return; |
| 1777 | |
| 1778 | list_del_init(&zone->link); |
| 1779 | if (dmz_is_seq(zone)) { |
| 1780 | /* LRU rotate sequential zone */ |
| 1781 | list_add_tail(&zone->link, &zmd->map_seq_list); |
Hannes Reinecke | 34f5aff | 2020-05-19 10:14:20 +0200 | [diff] [blame] | 1782 | } else if (dmz_is_cache(zone)) { |
| 1783 | /* LRU rotate cache zone */ |
| 1784 | list_add_tail(&zone->link, &zmd->map_cache_list); |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 1785 | } else { |
| 1786 | /* LRU rotate random zone */ |
| 1787 | list_add_tail(&zone->link, &zmd->map_rnd_list); |
| 1788 | } |
| 1789 | } |
| 1790 | |
| 1791 | /* |
| 1792 | * The list of mapped random zones is maintained |
| 1793 | * in LRU order. This rotates a zone at the end of the list. |
| 1794 | */ |
| 1795 | static void dmz_lru_zone(struct dmz_metadata *zmd, struct dm_zone *zone) |
| 1796 | { |
| 1797 | __dmz_lru_zone(zmd, zone); |
| 1798 | if (zone->bzone) |
| 1799 | __dmz_lru_zone(zmd, zone->bzone); |
| 1800 | } |
| 1801 | |
| 1802 | /* |
| 1803 | * Wait for any zone to be freed. |
| 1804 | */ |
| 1805 | static void dmz_wait_for_free_zones(struct dmz_metadata *zmd) |
| 1806 | { |
| 1807 | DEFINE_WAIT(wait); |
| 1808 | |
| 1809 | prepare_to_wait(&zmd->free_wq, &wait, TASK_UNINTERRUPTIBLE); |
| 1810 | dmz_unlock_map(zmd); |
| 1811 | dmz_unlock_metadata(zmd); |
| 1812 | |
| 1813 | io_schedule_timeout(HZ); |
| 1814 | |
| 1815 | dmz_lock_metadata(zmd); |
| 1816 | dmz_lock_map(zmd); |
| 1817 | finish_wait(&zmd->free_wq, &wait); |
| 1818 | } |
| 1819 | |
| 1820 | /* |
| 1821 | * Lock a zone for reclaim (set the zone RECLAIM bit). |
| 1822 | * Returns false if the zone cannot be locked or if it is already locked |
| 1823 | * and 1 otherwise. |
| 1824 | */ |
| 1825 | int dmz_lock_zone_reclaim(struct dm_zone *zone) |
| 1826 | { |
| 1827 | /* Active zones cannot be reclaimed */ |
| 1828 | if (dmz_is_active(zone)) |
| 1829 | return 0; |
| 1830 | |
| 1831 | return !test_and_set_bit(DMZ_RECLAIM, &zone->flags); |
| 1832 | } |
| 1833 | |
| 1834 | /* |
| 1835 | * Clear a zone reclaim flag. |
| 1836 | */ |
| 1837 | void dmz_unlock_zone_reclaim(struct dm_zone *zone) |
| 1838 | { |
| 1839 | WARN_ON(dmz_is_active(zone)); |
| 1840 | WARN_ON(!dmz_in_reclaim(zone)); |
| 1841 | |
| 1842 | clear_bit_unlock(DMZ_RECLAIM, &zone->flags); |
| 1843 | smp_mb__after_atomic(); |
| 1844 | wake_up_bit(&zone->flags, DMZ_RECLAIM); |
| 1845 | } |
| 1846 | |
| 1847 | /* |
| 1848 | * Wait for a zone reclaim to complete. |
| 1849 | */ |
| 1850 | static void dmz_wait_for_reclaim(struct dmz_metadata *zmd, struct dm_zone *zone) |
| 1851 | { |
| 1852 | dmz_unlock_map(zmd); |
| 1853 | dmz_unlock_metadata(zmd); |
| 1854 | wait_on_bit_timeout(&zone->flags, DMZ_RECLAIM, TASK_UNINTERRUPTIBLE, HZ); |
| 1855 | dmz_lock_metadata(zmd); |
| 1856 | dmz_lock_map(zmd); |
| 1857 | } |
| 1858 | |
| 1859 | /* |
Hannes Reinecke | 34f5aff | 2020-05-19 10:14:20 +0200 | [diff] [blame] | 1860 | * Select a cache or random write zone for reclaim. |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 1861 | */ |
Hannes Reinecke | 90a9b86 | 2020-05-19 10:14:21 +0200 | [diff] [blame^] | 1862 | static struct dm_zone *dmz_get_rnd_zone_for_reclaim(struct dmz_metadata *zmd, |
| 1863 | bool idle) |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 1864 | { |
| 1865 | struct dm_zone *dzone = NULL; |
| 1866 | struct dm_zone *zone; |
Hannes Reinecke | 34f5aff | 2020-05-19 10:14:20 +0200 | [diff] [blame] | 1867 | struct list_head *zone_list = &zmd->map_rnd_list; |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 1868 | |
Hannes Reinecke | 34f5aff | 2020-05-19 10:14:20 +0200 | [diff] [blame] | 1869 | /* If we have cache zones select from the cache zone list */ |
Hannes Reinecke | 90a9b86 | 2020-05-19 10:14:21 +0200 | [diff] [blame^] | 1870 | if (zmd->nr_cache) { |
Hannes Reinecke | 34f5aff | 2020-05-19 10:14:20 +0200 | [diff] [blame] | 1871 | zone_list = &zmd->map_cache_list; |
Hannes Reinecke | 90a9b86 | 2020-05-19 10:14:21 +0200 | [diff] [blame^] | 1872 | /* Try to relaim random zones, too, when idle */ |
| 1873 | if (idle && list_empty(zone_list)) |
| 1874 | zone_list = &zmd->map_rnd_list; |
| 1875 | } |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 1876 | |
Hannes Reinecke | 34f5aff | 2020-05-19 10:14:20 +0200 | [diff] [blame] | 1877 | list_for_each_entry(zone, zone_list, link) { |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 1878 | if (dmz_is_buf(zone)) |
| 1879 | dzone = zone->bzone; |
| 1880 | else |
| 1881 | dzone = zone; |
| 1882 | if (dmz_lock_zone_reclaim(dzone)) |
| 1883 | return dzone; |
| 1884 | } |
| 1885 | |
Hannes Reinecke | 489dc0f | 2020-05-19 10:14:19 +0200 | [diff] [blame] | 1886 | return NULL; |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 1887 | } |
| 1888 | |
| 1889 | /* |
| 1890 | * Select a buffered sequential zone for reclaim. |
| 1891 | */ |
| 1892 | static struct dm_zone *dmz_get_seq_zone_for_reclaim(struct dmz_metadata *zmd) |
| 1893 | { |
| 1894 | struct dm_zone *zone; |
| 1895 | |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 1896 | list_for_each_entry(zone, &zmd->map_seq_list, link) { |
| 1897 | if (!zone->bzone) |
| 1898 | continue; |
| 1899 | if (dmz_lock_zone_reclaim(zone)) |
| 1900 | return zone; |
| 1901 | } |
| 1902 | |
Hannes Reinecke | 489dc0f | 2020-05-19 10:14:19 +0200 | [diff] [blame] | 1903 | return NULL; |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 1904 | } |
| 1905 | |
| 1906 | /* |
| 1907 | * Select a zone for reclaim. |
| 1908 | */ |
Hannes Reinecke | 90a9b86 | 2020-05-19 10:14:21 +0200 | [diff] [blame^] | 1909 | struct dm_zone *dmz_get_zone_for_reclaim(struct dmz_metadata *zmd, bool idle) |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 1910 | { |
| 1911 | struct dm_zone *zone; |
| 1912 | |
| 1913 | /* |
| 1914 | * Search for a zone candidate to reclaim: 2 cases are possible. |
| 1915 | * (1) There is no free sequential zones. Then a random data zone |
| 1916 | * cannot be reclaimed. So choose a sequential zone to reclaim so |
| 1917 | * that afterward a random zone can be reclaimed. |
| 1918 | * (2) At least one free sequential zone is available, then choose |
| 1919 | * the oldest random zone (data or buffer) that can be locked. |
| 1920 | */ |
| 1921 | dmz_lock_map(zmd); |
| 1922 | if (list_empty(&zmd->reserved_seq_zones_list)) |
| 1923 | zone = dmz_get_seq_zone_for_reclaim(zmd); |
| 1924 | else |
Hannes Reinecke | 90a9b86 | 2020-05-19 10:14:21 +0200 | [diff] [blame^] | 1925 | zone = dmz_get_rnd_zone_for_reclaim(zmd, idle); |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 1926 | dmz_unlock_map(zmd); |
| 1927 | |
| 1928 | return zone; |
| 1929 | } |
| 1930 | |
| 1931 | /* |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 1932 | * Get the zone mapping a chunk, if the chunk is mapped already. |
| 1933 | * If no mapping exist and the operation is WRITE, a zone is |
| 1934 | * allocated and used to map the chunk. |
| 1935 | * The zone returned will be set to the active state. |
| 1936 | */ |
| 1937 | struct dm_zone *dmz_get_chunk_mapping(struct dmz_metadata *zmd, unsigned int chunk, int op) |
| 1938 | { |
| 1939 | struct dmz_mblock *dmap_mblk = zmd->map_mblk[chunk >> DMZ_MAP_ENTRIES_SHIFT]; |
| 1940 | struct dmz_map *dmap = (struct dmz_map *) dmap_mblk->data; |
| 1941 | int dmap_idx = chunk & DMZ_MAP_ENTRIES_MASK; |
| 1942 | unsigned int dzone_id; |
| 1943 | struct dm_zone *dzone = NULL; |
| 1944 | int ret = 0; |
Hannes Reinecke | 34f5aff | 2020-05-19 10:14:20 +0200 | [diff] [blame] | 1945 | int alloc_flags = zmd->nr_cache ? DMZ_ALLOC_CACHE : DMZ_ALLOC_RND; |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 1946 | |
| 1947 | dmz_lock_map(zmd); |
| 1948 | again: |
| 1949 | /* Get the chunk mapping */ |
| 1950 | dzone_id = le32_to_cpu(dmap[dmap_idx].dzone_id); |
| 1951 | if (dzone_id == DMZ_MAP_UNMAPPED) { |
| 1952 | /* |
| 1953 | * Read or discard in unmapped chunks are fine. But for |
| 1954 | * writes, we need a mapping, so get one. |
| 1955 | */ |
| 1956 | if (op != REQ_OP_WRITE) |
| 1957 | goto out; |
| 1958 | |
Dmitry Fomichev | ad1bd57 | 2019-08-02 15:02:51 -0700 | [diff] [blame] | 1959 | /* Allocate a random zone */ |
Hannes Reinecke | 34f5aff | 2020-05-19 10:14:20 +0200 | [diff] [blame] | 1960 | dzone = dmz_alloc_zone(zmd, alloc_flags); |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 1961 | if (!dzone) { |
Hannes Reinecke | d0e21ce | 2020-05-11 10:24:23 +0200 | [diff] [blame] | 1962 | if (dmz_dev_is_dying(zmd)) { |
Dmitry Fomichev | 75d66ff | 2019-08-10 14:43:11 -0700 | [diff] [blame] | 1963 | dzone = ERR_PTR(-EIO); |
| 1964 | goto out; |
| 1965 | } |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 1966 | dmz_wait_for_free_zones(zmd); |
| 1967 | goto again; |
| 1968 | } |
| 1969 | |
| 1970 | dmz_map_zone(zmd, dzone, chunk); |
| 1971 | |
| 1972 | } else { |
| 1973 | /* The chunk is already mapped: get the mapping zone */ |
| 1974 | dzone = dmz_get(zmd, dzone_id); |
| 1975 | if (dzone->chunk != chunk) { |
| 1976 | dzone = ERR_PTR(-EIO); |
| 1977 | goto out; |
| 1978 | } |
| 1979 | |
| 1980 | /* Repair write pointer if the sequential dzone has error */ |
| 1981 | if (dmz_seq_write_err(dzone)) { |
| 1982 | ret = dmz_handle_seq_write_err(zmd, dzone); |
| 1983 | if (ret) { |
| 1984 | dzone = ERR_PTR(-EIO); |
| 1985 | goto out; |
| 1986 | } |
| 1987 | clear_bit(DMZ_SEQ_WRITE_ERR, &dzone->flags); |
| 1988 | } |
| 1989 | } |
| 1990 | |
| 1991 | /* |
| 1992 | * If the zone is being reclaimed, the chunk mapping may change |
| 1993 | * to a different zone. So wait for reclaim and retry. Otherwise, |
| 1994 | * activate the zone (this will prevent reclaim from touching it). |
| 1995 | */ |
| 1996 | if (dmz_in_reclaim(dzone)) { |
| 1997 | dmz_wait_for_reclaim(zmd, dzone); |
| 1998 | goto again; |
| 1999 | } |
| 2000 | dmz_activate_zone(dzone); |
| 2001 | dmz_lru_zone(zmd, dzone); |
| 2002 | out: |
| 2003 | dmz_unlock_map(zmd); |
| 2004 | |
| 2005 | return dzone; |
| 2006 | } |
| 2007 | |
| 2008 | /* |
| 2009 | * Write and discard change the block validity of data zones and their buffer |
| 2010 | * zones. Check here that valid blocks are still present. If all blocks are |
| 2011 | * invalid, the zones can be unmapped on the fly without waiting for reclaim |
| 2012 | * to do it. |
| 2013 | */ |
| 2014 | void dmz_put_chunk_mapping(struct dmz_metadata *zmd, struct dm_zone *dzone) |
| 2015 | { |
| 2016 | struct dm_zone *bzone; |
| 2017 | |
| 2018 | dmz_lock_map(zmd); |
| 2019 | |
| 2020 | bzone = dzone->bzone; |
| 2021 | if (bzone) { |
| 2022 | if (dmz_weight(bzone)) |
| 2023 | dmz_lru_zone(zmd, bzone); |
| 2024 | else { |
| 2025 | /* Empty buffer zone: reclaim it */ |
| 2026 | dmz_unmap_zone(zmd, bzone); |
| 2027 | dmz_free_zone(zmd, bzone); |
| 2028 | bzone = NULL; |
| 2029 | } |
| 2030 | } |
| 2031 | |
| 2032 | /* Deactivate the data zone */ |
| 2033 | dmz_deactivate_zone(dzone); |
| 2034 | if (dmz_is_active(dzone) || bzone || dmz_weight(dzone)) |
| 2035 | dmz_lru_zone(zmd, dzone); |
| 2036 | else { |
| 2037 | /* Unbuffered inactive empty data zone: reclaim it */ |
| 2038 | dmz_unmap_zone(zmd, dzone); |
| 2039 | dmz_free_zone(zmd, dzone); |
| 2040 | } |
| 2041 | |
| 2042 | dmz_unlock_map(zmd); |
| 2043 | } |
| 2044 | |
| 2045 | /* |
| 2046 | * Allocate and map a random zone to buffer a chunk |
| 2047 | * already mapped to a sequential zone. |
| 2048 | */ |
| 2049 | struct dm_zone *dmz_get_chunk_buffer(struct dmz_metadata *zmd, |
| 2050 | struct dm_zone *dzone) |
| 2051 | { |
| 2052 | struct dm_zone *bzone; |
Hannes Reinecke | 34f5aff | 2020-05-19 10:14:20 +0200 | [diff] [blame] | 2053 | int alloc_flags = zmd->nr_cache ? DMZ_ALLOC_CACHE : DMZ_ALLOC_RND; |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 2054 | |
| 2055 | dmz_lock_map(zmd); |
| 2056 | again: |
| 2057 | bzone = dzone->bzone; |
| 2058 | if (bzone) |
| 2059 | goto out; |
| 2060 | |
Dmitry Fomichev | ad1bd57 | 2019-08-02 15:02:51 -0700 | [diff] [blame] | 2061 | /* Allocate a random zone */ |
Hannes Reinecke | 34f5aff | 2020-05-19 10:14:20 +0200 | [diff] [blame] | 2062 | bzone = dmz_alloc_zone(zmd, alloc_flags); |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 2063 | if (!bzone) { |
Hannes Reinecke | d0e21ce | 2020-05-11 10:24:23 +0200 | [diff] [blame] | 2064 | if (dmz_dev_is_dying(zmd)) { |
Dmitry Fomichev | 75d66ff | 2019-08-10 14:43:11 -0700 | [diff] [blame] | 2065 | bzone = ERR_PTR(-EIO); |
| 2066 | goto out; |
| 2067 | } |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 2068 | dmz_wait_for_free_zones(zmd); |
| 2069 | goto again; |
| 2070 | } |
| 2071 | |
| 2072 | /* Update the chunk mapping */ |
Hannes Reinecke | b712287 | 2020-05-11 10:24:18 +0200 | [diff] [blame] | 2073 | dmz_set_chunk_mapping(zmd, dzone->chunk, dzone->id, bzone->id); |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 2074 | |
| 2075 | set_bit(DMZ_BUF, &bzone->flags); |
| 2076 | bzone->chunk = dzone->chunk; |
| 2077 | bzone->bzone = dzone; |
| 2078 | dzone->bzone = bzone; |
Hannes Reinecke | 34f5aff | 2020-05-19 10:14:20 +0200 | [diff] [blame] | 2079 | if (dmz_is_cache(bzone)) |
| 2080 | list_add_tail(&bzone->link, &zmd->map_cache_list); |
| 2081 | else |
| 2082 | list_add_tail(&bzone->link, &zmd->map_rnd_list); |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 2083 | out: |
| 2084 | dmz_unlock_map(zmd); |
| 2085 | |
| 2086 | return bzone; |
| 2087 | } |
| 2088 | |
| 2089 | /* |
| 2090 | * Get an unmapped (free) zone. |
| 2091 | * This must be called with the mapping lock held. |
| 2092 | */ |
| 2093 | struct dm_zone *dmz_alloc_zone(struct dmz_metadata *zmd, unsigned long flags) |
| 2094 | { |
| 2095 | struct list_head *list; |
| 2096 | struct dm_zone *zone; |
| 2097 | |
Hannes Reinecke | 34f5aff | 2020-05-19 10:14:20 +0200 | [diff] [blame] | 2098 | if (flags & DMZ_ALLOC_CACHE) |
| 2099 | list = &zmd->unmap_cache_list; |
| 2100 | else if (flags & DMZ_ALLOC_RND) |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 2101 | list = &zmd->unmap_rnd_list; |
| 2102 | else |
| 2103 | list = &zmd->unmap_seq_list; |
Hannes Reinecke | 34f5aff | 2020-05-19 10:14:20 +0200 | [diff] [blame] | 2104 | |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 2105 | again: |
| 2106 | if (list_empty(list)) { |
| 2107 | /* |
Hannes Reinecke | 34f5aff | 2020-05-19 10:14:20 +0200 | [diff] [blame] | 2108 | * No free zone: return NULL if this is for not reclaim. |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 2109 | */ |
Hannes Reinecke | 34f5aff | 2020-05-19 10:14:20 +0200 | [diff] [blame] | 2110 | if (!(flags & DMZ_ALLOC_RECLAIM)) |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 2111 | return NULL; |
Hannes Reinecke | 34f5aff | 2020-05-19 10:14:20 +0200 | [diff] [blame] | 2112 | /* |
| 2113 | * Use sequential write zones if we started off with random |
| 2114 | * zones and the list is empty |
| 2115 | */ |
| 2116 | if (list == &zmd->unmap_rnd_list) { |
| 2117 | list = &zmd->unmap_seq_list; |
| 2118 | goto again; |
| 2119 | } |
| 2120 | /* |
| 2121 | * Fallback to the reserved sequential zones |
| 2122 | */ |
| 2123 | zone = list_first_entry_or_null(&zmd->reserved_seq_zones_list, |
| 2124 | struct dm_zone, link); |
| 2125 | if (zone) { |
| 2126 | list_del_init(&zone->link); |
| 2127 | atomic_dec(&zmd->nr_reserved_seq_zones); |
| 2128 | } |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 2129 | return zone; |
| 2130 | } |
| 2131 | |
| 2132 | zone = list_first_entry(list, struct dm_zone, link); |
| 2133 | list_del_init(&zone->link); |
| 2134 | |
Hannes Reinecke | 34f5aff | 2020-05-19 10:14:20 +0200 | [diff] [blame] | 2135 | if (dmz_is_cache(zone)) |
| 2136 | atomic_dec(&zmd->unmap_nr_cache); |
| 2137 | else if (dmz_is_rnd(zone)) |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 2138 | atomic_dec(&zmd->unmap_nr_rnd); |
| 2139 | else |
| 2140 | atomic_dec(&zmd->unmap_nr_seq); |
| 2141 | |
| 2142 | if (dmz_is_offline(zone)) { |
Hannes Reinecke | ca1a704 | 2020-05-11 10:24:27 +0200 | [diff] [blame] | 2143 | dmz_zmd_warn(zmd, "Zone %u is offline", zone->id); |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 2144 | zone = NULL; |
| 2145 | goto again; |
| 2146 | } |
Hannes Reinecke | dc076c8 | 2020-05-11 10:24:29 +0200 | [diff] [blame] | 2147 | if (dmz_is_meta(zone)) { |
| 2148 | struct dmz_dev *dev = dmz_zone_to_dev(zmd, zone); |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 2149 | |
Hannes Reinecke | dc076c8 | 2020-05-11 10:24:29 +0200 | [diff] [blame] | 2150 | dmz_dev_warn(dev, "Zone %u has metadata", zone->id); |
| 2151 | zone = NULL; |
| 2152 | goto again; |
| 2153 | } |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 2154 | return zone; |
| 2155 | } |
| 2156 | |
| 2157 | /* |
| 2158 | * Free a zone. |
| 2159 | * This must be called with the mapping lock held. |
| 2160 | */ |
| 2161 | void dmz_free_zone(struct dmz_metadata *zmd, struct dm_zone *zone) |
| 2162 | { |
| 2163 | /* If this is a sequential zone, reset it */ |
| 2164 | if (dmz_is_seq(zone)) |
| 2165 | dmz_reset_zone(zmd, zone); |
| 2166 | |
| 2167 | /* Return the zone to its type unmap list */ |
Hannes Reinecke | 34f5aff | 2020-05-19 10:14:20 +0200 | [diff] [blame] | 2168 | if (dmz_is_cache(zone)) { |
| 2169 | list_add_tail(&zone->link, &zmd->unmap_cache_list); |
| 2170 | atomic_inc(&zmd->unmap_nr_cache); |
| 2171 | } else if (dmz_is_rnd(zone)) { |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 2172 | list_add_tail(&zone->link, &zmd->unmap_rnd_list); |
| 2173 | atomic_inc(&zmd->unmap_nr_rnd); |
| 2174 | } else if (atomic_read(&zmd->nr_reserved_seq_zones) < |
| 2175 | zmd->nr_reserved_seq) { |
| 2176 | list_add_tail(&zone->link, &zmd->reserved_seq_zones_list); |
| 2177 | atomic_inc(&zmd->nr_reserved_seq_zones); |
| 2178 | } else { |
| 2179 | list_add_tail(&zone->link, &zmd->unmap_seq_list); |
| 2180 | atomic_inc(&zmd->unmap_nr_seq); |
| 2181 | } |
| 2182 | |
| 2183 | wake_up_all(&zmd->free_wq); |
| 2184 | } |
| 2185 | |
| 2186 | /* |
| 2187 | * Map a chunk to a zone. |
| 2188 | * This must be called with the mapping lock held. |
| 2189 | */ |
| 2190 | void dmz_map_zone(struct dmz_metadata *zmd, struct dm_zone *dzone, |
| 2191 | unsigned int chunk) |
| 2192 | { |
| 2193 | /* Set the chunk mapping */ |
Hannes Reinecke | b712287 | 2020-05-11 10:24:18 +0200 | [diff] [blame] | 2194 | dmz_set_chunk_mapping(zmd, chunk, dzone->id, |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 2195 | DMZ_MAP_UNMAPPED); |
| 2196 | dzone->chunk = chunk; |
Hannes Reinecke | 34f5aff | 2020-05-19 10:14:20 +0200 | [diff] [blame] | 2197 | if (dmz_is_cache(dzone)) |
| 2198 | list_add_tail(&dzone->link, &zmd->map_cache_list); |
| 2199 | else if (dmz_is_rnd(dzone)) |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 2200 | list_add_tail(&dzone->link, &zmd->map_rnd_list); |
| 2201 | else |
| 2202 | list_add_tail(&dzone->link, &zmd->map_seq_list); |
| 2203 | } |
| 2204 | |
| 2205 | /* |
| 2206 | * Unmap a zone. |
| 2207 | * This must be called with the mapping lock held. |
| 2208 | */ |
| 2209 | void dmz_unmap_zone(struct dmz_metadata *zmd, struct dm_zone *zone) |
| 2210 | { |
| 2211 | unsigned int chunk = zone->chunk; |
| 2212 | unsigned int dzone_id; |
| 2213 | |
| 2214 | if (chunk == DMZ_MAP_UNMAPPED) { |
| 2215 | /* Already unmapped */ |
| 2216 | return; |
| 2217 | } |
| 2218 | |
| 2219 | if (test_and_clear_bit(DMZ_BUF, &zone->flags)) { |
| 2220 | /* |
| 2221 | * Unmapping the chunk buffer zone: clear only |
| 2222 | * the chunk buffer mapping |
| 2223 | */ |
Hannes Reinecke | b712287 | 2020-05-11 10:24:18 +0200 | [diff] [blame] | 2224 | dzone_id = zone->bzone->id; |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 2225 | zone->bzone->bzone = NULL; |
| 2226 | zone->bzone = NULL; |
| 2227 | |
| 2228 | } else { |
| 2229 | /* |
| 2230 | * Unmapping the chunk data zone: the zone must |
| 2231 | * not be buffered. |
| 2232 | */ |
| 2233 | if (WARN_ON(zone->bzone)) { |
| 2234 | zone->bzone->bzone = NULL; |
| 2235 | zone->bzone = NULL; |
| 2236 | } |
| 2237 | dzone_id = DMZ_MAP_UNMAPPED; |
| 2238 | } |
| 2239 | |
| 2240 | dmz_set_chunk_mapping(zmd, chunk, dzone_id, DMZ_MAP_UNMAPPED); |
| 2241 | |
| 2242 | zone->chunk = DMZ_MAP_UNMAPPED; |
| 2243 | list_del_init(&zone->link); |
| 2244 | } |
| 2245 | |
| 2246 | /* |
| 2247 | * Set @nr_bits bits in @bitmap starting from @bit. |
| 2248 | * Return the number of bits changed from 0 to 1. |
| 2249 | */ |
| 2250 | static unsigned int dmz_set_bits(unsigned long *bitmap, |
| 2251 | unsigned int bit, unsigned int nr_bits) |
| 2252 | { |
| 2253 | unsigned long *addr; |
| 2254 | unsigned int end = bit + nr_bits; |
| 2255 | unsigned int n = 0; |
| 2256 | |
| 2257 | while (bit < end) { |
| 2258 | if (((bit & (BITS_PER_LONG - 1)) == 0) && |
| 2259 | ((end - bit) >= BITS_PER_LONG)) { |
| 2260 | /* Try to set the whole word at once */ |
| 2261 | addr = bitmap + BIT_WORD(bit); |
| 2262 | if (*addr == 0) { |
| 2263 | *addr = ULONG_MAX; |
| 2264 | n += BITS_PER_LONG; |
| 2265 | bit += BITS_PER_LONG; |
| 2266 | continue; |
| 2267 | } |
| 2268 | } |
| 2269 | |
| 2270 | if (!test_and_set_bit(bit, bitmap)) |
| 2271 | n++; |
| 2272 | bit++; |
| 2273 | } |
| 2274 | |
| 2275 | return n; |
| 2276 | } |
| 2277 | |
| 2278 | /* |
| 2279 | * Get the bitmap block storing the bit for chunk_block in zone. |
| 2280 | */ |
| 2281 | static struct dmz_mblock *dmz_get_bitmap(struct dmz_metadata *zmd, |
| 2282 | struct dm_zone *zone, |
| 2283 | sector_t chunk_block) |
| 2284 | { |
| 2285 | sector_t bitmap_block = 1 + zmd->nr_map_blocks + |
Hannes Reinecke | b712287 | 2020-05-11 10:24:18 +0200 | [diff] [blame] | 2286 | (sector_t)(zone->id * zmd->zone_nr_bitmap_blocks) + |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 2287 | (chunk_block >> DMZ_BLOCK_SHIFT_BITS); |
| 2288 | |
| 2289 | return dmz_get_mblock(zmd, bitmap_block); |
| 2290 | } |
| 2291 | |
| 2292 | /* |
| 2293 | * Copy the valid blocks bitmap of from_zone to the bitmap of to_zone. |
| 2294 | */ |
| 2295 | int dmz_copy_valid_blocks(struct dmz_metadata *zmd, struct dm_zone *from_zone, |
| 2296 | struct dm_zone *to_zone) |
| 2297 | { |
| 2298 | struct dmz_mblock *from_mblk, *to_mblk; |
| 2299 | sector_t chunk_block = 0; |
| 2300 | |
| 2301 | /* Get the zones bitmap blocks */ |
Hannes Reinecke | 3682056 | 2020-05-11 10:24:21 +0200 | [diff] [blame] | 2302 | while (chunk_block < zmd->zone_nr_blocks) { |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 2303 | from_mblk = dmz_get_bitmap(zmd, from_zone, chunk_block); |
| 2304 | if (IS_ERR(from_mblk)) |
| 2305 | return PTR_ERR(from_mblk); |
| 2306 | to_mblk = dmz_get_bitmap(zmd, to_zone, chunk_block); |
| 2307 | if (IS_ERR(to_mblk)) { |
| 2308 | dmz_release_mblock(zmd, from_mblk); |
| 2309 | return PTR_ERR(to_mblk); |
| 2310 | } |
| 2311 | |
| 2312 | memcpy(to_mblk->data, from_mblk->data, DMZ_BLOCK_SIZE); |
| 2313 | dmz_dirty_mblock(zmd, to_mblk); |
| 2314 | |
| 2315 | dmz_release_mblock(zmd, to_mblk); |
| 2316 | dmz_release_mblock(zmd, from_mblk); |
| 2317 | |
Dmitry Fomichev | b399629 | 2019-12-23 17:05:46 -0800 | [diff] [blame] | 2318 | chunk_block += zmd->zone_bits_per_mblk; |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 2319 | } |
| 2320 | |
| 2321 | to_zone->weight = from_zone->weight; |
| 2322 | |
| 2323 | return 0; |
| 2324 | } |
| 2325 | |
| 2326 | /* |
| 2327 | * Merge the valid blocks bitmap of from_zone into the bitmap of to_zone, |
| 2328 | * starting from chunk_block. |
| 2329 | */ |
| 2330 | int dmz_merge_valid_blocks(struct dmz_metadata *zmd, struct dm_zone *from_zone, |
| 2331 | struct dm_zone *to_zone, sector_t chunk_block) |
| 2332 | { |
| 2333 | unsigned int nr_blocks; |
| 2334 | int ret; |
| 2335 | |
| 2336 | /* Get the zones bitmap blocks */ |
Hannes Reinecke | 3682056 | 2020-05-11 10:24:21 +0200 | [diff] [blame] | 2337 | while (chunk_block < zmd->zone_nr_blocks) { |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 2338 | /* Get a valid region from the source zone */ |
| 2339 | ret = dmz_first_valid_block(zmd, from_zone, &chunk_block); |
| 2340 | if (ret <= 0) |
| 2341 | return ret; |
| 2342 | |
| 2343 | nr_blocks = ret; |
| 2344 | ret = dmz_validate_blocks(zmd, to_zone, chunk_block, nr_blocks); |
| 2345 | if (ret) |
| 2346 | return ret; |
| 2347 | |
| 2348 | chunk_block += nr_blocks; |
| 2349 | } |
| 2350 | |
| 2351 | return 0; |
| 2352 | } |
| 2353 | |
| 2354 | /* |
| 2355 | * Validate all the blocks in the range [block..block+nr_blocks-1]. |
| 2356 | */ |
| 2357 | int dmz_validate_blocks(struct dmz_metadata *zmd, struct dm_zone *zone, |
| 2358 | sector_t chunk_block, unsigned int nr_blocks) |
| 2359 | { |
| 2360 | unsigned int count, bit, nr_bits; |
Hannes Reinecke | 3682056 | 2020-05-11 10:24:21 +0200 | [diff] [blame] | 2361 | unsigned int zone_nr_blocks = zmd->zone_nr_blocks; |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 2362 | struct dmz_mblock *mblk; |
| 2363 | unsigned int n = 0; |
| 2364 | |
Hannes Reinecke | ca1a704 | 2020-05-11 10:24:27 +0200 | [diff] [blame] | 2365 | dmz_zmd_debug(zmd, "=> VALIDATE zone %u, block %llu, %u blocks", |
Hannes Reinecke | b712287 | 2020-05-11 10:24:18 +0200 | [diff] [blame] | 2366 | zone->id, (unsigned long long)chunk_block, |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 2367 | nr_blocks); |
| 2368 | |
| 2369 | WARN_ON(chunk_block + nr_blocks > zone_nr_blocks); |
| 2370 | |
| 2371 | while (nr_blocks) { |
| 2372 | /* Get bitmap block */ |
| 2373 | mblk = dmz_get_bitmap(zmd, zone, chunk_block); |
| 2374 | if (IS_ERR(mblk)) |
| 2375 | return PTR_ERR(mblk); |
| 2376 | |
| 2377 | /* Set bits */ |
| 2378 | bit = chunk_block & DMZ_BLOCK_MASK_BITS; |
Dmitry Fomichev | b399629 | 2019-12-23 17:05:46 -0800 | [diff] [blame] | 2379 | nr_bits = min(nr_blocks, zmd->zone_bits_per_mblk - bit); |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 2380 | |
| 2381 | count = dmz_set_bits((unsigned long *)mblk->data, bit, nr_bits); |
| 2382 | if (count) { |
| 2383 | dmz_dirty_mblock(zmd, mblk); |
| 2384 | n += count; |
| 2385 | } |
| 2386 | dmz_release_mblock(zmd, mblk); |
| 2387 | |
| 2388 | nr_blocks -= nr_bits; |
| 2389 | chunk_block += nr_bits; |
| 2390 | } |
| 2391 | |
| 2392 | if (likely(zone->weight + n <= zone_nr_blocks)) |
| 2393 | zone->weight += n; |
| 2394 | else { |
Hannes Reinecke | ca1a704 | 2020-05-11 10:24:27 +0200 | [diff] [blame] | 2395 | dmz_zmd_warn(zmd, "Zone %u: weight %u should be <= %u", |
Hannes Reinecke | b712287 | 2020-05-11 10:24:18 +0200 | [diff] [blame] | 2396 | zone->id, zone->weight, |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 2397 | zone_nr_blocks - n); |
| 2398 | zone->weight = zone_nr_blocks; |
| 2399 | } |
| 2400 | |
| 2401 | return 0; |
| 2402 | } |
| 2403 | |
| 2404 | /* |
| 2405 | * Clear nr_bits bits in bitmap starting from bit. |
| 2406 | * Return the number of bits cleared. |
| 2407 | */ |
| 2408 | static int dmz_clear_bits(unsigned long *bitmap, int bit, int nr_bits) |
| 2409 | { |
| 2410 | unsigned long *addr; |
| 2411 | int end = bit + nr_bits; |
| 2412 | int n = 0; |
| 2413 | |
| 2414 | while (bit < end) { |
| 2415 | if (((bit & (BITS_PER_LONG - 1)) == 0) && |
| 2416 | ((end - bit) >= BITS_PER_LONG)) { |
| 2417 | /* Try to clear whole word at once */ |
| 2418 | addr = bitmap + BIT_WORD(bit); |
| 2419 | if (*addr == ULONG_MAX) { |
| 2420 | *addr = 0; |
| 2421 | n += BITS_PER_LONG; |
| 2422 | bit += BITS_PER_LONG; |
| 2423 | continue; |
| 2424 | } |
| 2425 | } |
| 2426 | |
| 2427 | if (test_and_clear_bit(bit, bitmap)) |
| 2428 | n++; |
| 2429 | bit++; |
| 2430 | } |
| 2431 | |
| 2432 | return n; |
| 2433 | } |
| 2434 | |
| 2435 | /* |
| 2436 | * Invalidate all the blocks in the range [block..block+nr_blocks-1]. |
| 2437 | */ |
| 2438 | int dmz_invalidate_blocks(struct dmz_metadata *zmd, struct dm_zone *zone, |
| 2439 | sector_t chunk_block, unsigned int nr_blocks) |
| 2440 | { |
| 2441 | unsigned int count, bit, nr_bits; |
| 2442 | struct dmz_mblock *mblk; |
| 2443 | unsigned int n = 0; |
| 2444 | |
Hannes Reinecke | ca1a704 | 2020-05-11 10:24:27 +0200 | [diff] [blame] | 2445 | dmz_zmd_debug(zmd, "=> INVALIDATE zone %u, block %llu, %u blocks", |
Hannes Reinecke | b712287 | 2020-05-11 10:24:18 +0200 | [diff] [blame] | 2446 | zone->id, (u64)chunk_block, nr_blocks); |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 2447 | |
Hannes Reinecke | 3682056 | 2020-05-11 10:24:21 +0200 | [diff] [blame] | 2448 | WARN_ON(chunk_block + nr_blocks > zmd->zone_nr_blocks); |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 2449 | |
| 2450 | while (nr_blocks) { |
| 2451 | /* Get bitmap block */ |
| 2452 | mblk = dmz_get_bitmap(zmd, zone, chunk_block); |
| 2453 | if (IS_ERR(mblk)) |
| 2454 | return PTR_ERR(mblk); |
| 2455 | |
| 2456 | /* Clear bits */ |
| 2457 | bit = chunk_block & DMZ_BLOCK_MASK_BITS; |
Dmitry Fomichev | b399629 | 2019-12-23 17:05:46 -0800 | [diff] [blame] | 2458 | nr_bits = min(nr_blocks, zmd->zone_bits_per_mblk - bit); |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 2459 | |
| 2460 | count = dmz_clear_bits((unsigned long *)mblk->data, |
| 2461 | bit, nr_bits); |
| 2462 | if (count) { |
| 2463 | dmz_dirty_mblock(zmd, mblk); |
| 2464 | n += count; |
| 2465 | } |
| 2466 | dmz_release_mblock(zmd, mblk); |
| 2467 | |
| 2468 | nr_blocks -= nr_bits; |
| 2469 | chunk_block += nr_bits; |
| 2470 | } |
| 2471 | |
| 2472 | if (zone->weight >= n) |
| 2473 | zone->weight -= n; |
| 2474 | else { |
Hannes Reinecke | ca1a704 | 2020-05-11 10:24:27 +0200 | [diff] [blame] | 2475 | dmz_zmd_warn(zmd, "Zone %u: weight %u should be >= %u", |
Hannes Reinecke | b712287 | 2020-05-11 10:24:18 +0200 | [diff] [blame] | 2476 | zone->id, zone->weight, n); |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 2477 | zone->weight = 0; |
| 2478 | } |
| 2479 | |
| 2480 | return 0; |
| 2481 | } |
| 2482 | |
| 2483 | /* |
| 2484 | * Get a block bit value. |
| 2485 | */ |
| 2486 | static int dmz_test_block(struct dmz_metadata *zmd, struct dm_zone *zone, |
| 2487 | sector_t chunk_block) |
| 2488 | { |
| 2489 | struct dmz_mblock *mblk; |
| 2490 | int ret; |
| 2491 | |
Hannes Reinecke | 3682056 | 2020-05-11 10:24:21 +0200 | [diff] [blame] | 2492 | WARN_ON(chunk_block >= zmd->zone_nr_blocks); |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 2493 | |
| 2494 | /* Get bitmap block */ |
| 2495 | mblk = dmz_get_bitmap(zmd, zone, chunk_block); |
| 2496 | if (IS_ERR(mblk)) |
| 2497 | return PTR_ERR(mblk); |
| 2498 | |
| 2499 | /* Get offset */ |
| 2500 | ret = test_bit(chunk_block & DMZ_BLOCK_MASK_BITS, |
| 2501 | (unsigned long *) mblk->data) != 0; |
| 2502 | |
| 2503 | dmz_release_mblock(zmd, mblk); |
| 2504 | |
| 2505 | return ret; |
| 2506 | } |
| 2507 | |
| 2508 | /* |
| 2509 | * Return the number of blocks from chunk_block to the first block with a bit |
| 2510 | * value specified by set. Search at most nr_blocks blocks from chunk_block. |
| 2511 | */ |
| 2512 | static int dmz_to_next_set_block(struct dmz_metadata *zmd, struct dm_zone *zone, |
| 2513 | sector_t chunk_block, unsigned int nr_blocks, |
| 2514 | int set) |
| 2515 | { |
| 2516 | struct dmz_mblock *mblk; |
| 2517 | unsigned int bit, set_bit, nr_bits; |
Dmitry Fomichev | b399629 | 2019-12-23 17:05:46 -0800 | [diff] [blame] | 2518 | unsigned int zone_bits = zmd->zone_bits_per_mblk; |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 2519 | unsigned long *bitmap; |
| 2520 | int n = 0; |
| 2521 | |
Hannes Reinecke | 3682056 | 2020-05-11 10:24:21 +0200 | [diff] [blame] | 2522 | WARN_ON(chunk_block + nr_blocks > zmd->zone_nr_blocks); |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 2523 | |
| 2524 | while (nr_blocks) { |
| 2525 | /* Get bitmap block */ |
| 2526 | mblk = dmz_get_bitmap(zmd, zone, chunk_block); |
| 2527 | if (IS_ERR(mblk)) |
| 2528 | return PTR_ERR(mblk); |
| 2529 | |
| 2530 | /* Get offset */ |
| 2531 | bitmap = (unsigned long *) mblk->data; |
| 2532 | bit = chunk_block & DMZ_BLOCK_MASK_BITS; |
Dmitry Fomichev | b399629 | 2019-12-23 17:05:46 -0800 | [diff] [blame] | 2533 | nr_bits = min(nr_blocks, zone_bits - bit); |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 2534 | if (set) |
Dmitry Fomichev | b399629 | 2019-12-23 17:05:46 -0800 | [diff] [blame] | 2535 | set_bit = find_next_bit(bitmap, zone_bits, bit); |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 2536 | else |
Dmitry Fomichev | b399629 | 2019-12-23 17:05:46 -0800 | [diff] [blame] | 2537 | set_bit = find_next_zero_bit(bitmap, zone_bits, bit); |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 2538 | dmz_release_mblock(zmd, mblk); |
| 2539 | |
| 2540 | n += set_bit - bit; |
Dmitry Fomichev | b399629 | 2019-12-23 17:05:46 -0800 | [diff] [blame] | 2541 | if (set_bit < zone_bits) |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 2542 | break; |
| 2543 | |
| 2544 | nr_blocks -= nr_bits; |
| 2545 | chunk_block += nr_bits; |
| 2546 | } |
| 2547 | |
| 2548 | return n; |
| 2549 | } |
| 2550 | |
| 2551 | /* |
| 2552 | * Test if chunk_block is valid. If it is, the number of consecutive |
| 2553 | * valid blocks from chunk_block will be returned. |
| 2554 | */ |
| 2555 | int dmz_block_valid(struct dmz_metadata *zmd, struct dm_zone *zone, |
| 2556 | sector_t chunk_block) |
| 2557 | { |
| 2558 | int valid; |
| 2559 | |
| 2560 | valid = dmz_test_block(zmd, zone, chunk_block); |
| 2561 | if (valid <= 0) |
| 2562 | return valid; |
| 2563 | |
| 2564 | /* The block is valid: get the number of valid blocks from block */ |
| 2565 | return dmz_to_next_set_block(zmd, zone, chunk_block, |
Hannes Reinecke | 3682056 | 2020-05-11 10:24:21 +0200 | [diff] [blame] | 2566 | zmd->zone_nr_blocks - chunk_block, 0); |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 2567 | } |
| 2568 | |
| 2569 | /* |
| 2570 | * Find the first valid block from @chunk_block in @zone. |
| 2571 | * If such a block is found, its number is returned using |
| 2572 | * @chunk_block and the total number of valid blocks from @chunk_block |
| 2573 | * is returned. |
| 2574 | */ |
| 2575 | int dmz_first_valid_block(struct dmz_metadata *zmd, struct dm_zone *zone, |
| 2576 | sector_t *chunk_block) |
| 2577 | { |
| 2578 | sector_t start_block = *chunk_block; |
| 2579 | int ret; |
| 2580 | |
| 2581 | ret = dmz_to_next_set_block(zmd, zone, start_block, |
Hannes Reinecke | 3682056 | 2020-05-11 10:24:21 +0200 | [diff] [blame] | 2582 | zmd->zone_nr_blocks - start_block, 1); |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 2583 | if (ret < 0) |
| 2584 | return ret; |
| 2585 | |
| 2586 | start_block += ret; |
| 2587 | *chunk_block = start_block; |
| 2588 | |
| 2589 | return dmz_to_next_set_block(zmd, zone, start_block, |
Hannes Reinecke | 3682056 | 2020-05-11 10:24:21 +0200 | [diff] [blame] | 2590 | zmd->zone_nr_blocks - start_block, 0); |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 2591 | } |
| 2592 | |
| 2593 | /* |
| 2594 | * Count the number of bits set starting from bit up to bit + nr_bits - 1. |
| 2595 | */ |
| 2596 | static int dmz_count_bits(void *bitmap, int bit, int nr_bits) |
| 2597 | { |
| 2598 | unsigned long *addr; |
| 2599 | int end = bit + nr_bits; |
| 2600 | int n = 0; |
| 2601 | |
| 2602 | while (bit < end) { |
| 2603 | if (((bit & (BITS_PER_LONG - 1)) == 0) && |
| 2604 | ((end - bit) >= BITS_PER_LONG)) { |
| 2605 | addr = (unsigned long *)bitmap + BIT_WORD(bit); |
| 2606 | if (*addr == ULONG_MAX) { |
| 2607 | n += BITS_PER_LONG; |
| 2608 | bit += BITS_PER_LONG; |
| 2609 | continue; |
| 2610 | } |
| 2611 | } |
| 2612 | |
| 2613 | if (test_bit(bit, bitmap)) |
| 2614 | n++; |
| 2615 | bit++; |
| 2616 | } |
| 2617 | |
| 2618 | return n; |
| 2619 | } |
| 2620 | |
| 2621 | /* |
| 2622 | * Get a zone weight. |
| 2623 | */ |
| 2624 | static void dmz_get_zone_weight(struct dmz_metadata *zmd, struct dm_zone *zone) |
| 2625 | { |
| 2626 | struct dmz_mblock *mblk; |
| 2627 | sector_t chunk_block = 0; |
| 2628 | unsigned int bit, nr_bits; |
Hannes Reinecke | 3682056 | 2020-05-11 10:24:21 +0200 | [diff] [blame] | 2629 | unsigned int nr_blocks = zmd->zone_nr_blocks; |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 2630 | void *bitmap; |
| 2631 | int n = 0; |
| 2632 | |
| 2633 | while (nr_blocks) { |
| 2634 | /* Get bitmap block */ |
| 2635 | mblk = dmz_get_bitmap(zmd, zone, chunk_block); |
| 2636 | if (IS_ERR(mblk)) { |
| 2637 | n = 0; |
| 2638 | break; |
| 2639 | } |
| 2640 | |
| 2641 | /* Count bits in this block */ |
| 2642 | bitmap = mblk->data; |
| 2643 | bit = chunk_block & DMZ_BLOCK_MASK_BITS; |
Dmitry Fomichev | b399629 | 2019-12-23 17:05:46 -0800 | [diff] [blame] | 2644 | nr_bits = min(nr_blocks, zmd->zone_bits_per_mblk - bit); |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 2645 | n += dmz_count_bits(bitmap, bit, nr_bits); |
| 2646 | |
| 2647 | dmz_release_mblock(zmd, mblk); |
| 2648 | |
| 2649 | nr_blocks -= nr_bits; |
| 2650 | chunk_block += nr_bits; |
| 2651 | } |
| 2652 | |
| 2653 | zone->weight = n; |
| 2654 | } |
| 2655 | |
| 2656 | /* |
| 2657 | * Cleanup the zoned metadata resources. |
| 2658 | */ |
| 2659 | static void dmz_cleanup_metadata(struct dmz_metadata *zmd) |
| 2660 | { |
| 2661 | struct rb_root *root; |
| 2662 | struct dmz_mblock *mblk, *next; |
| 2663 | int i; |
| 2664 | |
| 2665 | /* Release zone mapping resources */ |
| 2666 | if (zmd->map_mblk) { |
| 2667 | for (i = 0; i < zmd->nr_map_blocks; i++) |
| 2668 | dmz_release_mblock(zmd, zmd->map_mblk[i]); |
| 2669 | kfree(zmd->map_mblk); |
| 2670 | zmd->map_mblk = NULL; |
| 2671 | } |
| 2672 | |
| 2673 | /* Release super blocks */ |
| 2674 | for (i = 0; i < 2; i++) { |
| 2675 | if (zmd->sb[i].mblk) { |
| 2676 | dmz_free_mblock(zmd, zmd->sb[i].mblk); |
| 2677 | zmd->sb[i].mblk = NULL; |
| 2678 | } |
| 2679 | } |
| 2680 | |
| 2681 | /* Free cached blocks */ |
| 2682 | while (!list_empty(&zmd->mblk_dirty_list)) { |
| 2683 | mblk = list_first_entry(&zmd->mblk_dirty_list, |
| 2684 | struct dmz_mblock, link); |
Hannes Reinecke | ca1a704 | 2020-05-11 10:24:27 +0200 | [diff] [blame] | 2685 | dmz_zmd_warn(zmd, "mblock %llu still in dirty list (ref %u)", |
Damien Le Moal | 33c2865 | 2018-10-17 18:05:07 +0900 | [diff] [blame] | 2686 | (u64)mblk->no, mblk->ref); |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 2687 | list_del_init(&mblk->link); |
| 2688 | rb_erase(&mblk->node, &zmd->mblk_rbtree); |
| 2689 | dmz_free_mblock(zmd, mblk); |
| 2690 | } |
| 2691 | |
| 2692 | while (!list_empty(&zmd->mblk_lru_list)) { |
| 2693 | mblk = list_first_entry(&zmd->mblk_lru_list, |
| 2694 | struct dmz_mblock, link); |
| 2695 | list_del_init(&mblk->link); |
| 2696 | rb_erase(&mblk->node, &zmd->mblk_rbtree); |
| 2697 | dmz_free_mblock(zmd, mblk); |
| 2698 | } |
| 2699 | |
| 2700 | /* Sanity checks: the mblock rbtree should now be empty */ |
| 2701 | root = &zmd->mblk_rbtree; |
| 2702 | rbtree_postorder_for_each_entry_safe(mblk, next, root, node) { |
Hannes Reinecke | ca1a704 | 2020-05-11 10:24:27 +0200 | [diff] [blame] | 2703 | dmz_zmd_warn(zmd, "mblock %llu ref %u still in rbtree", |
Damien Le Moal | 33c2865 | 2018-10-17 18:05:07 +0900 | [diff] [blame] | 2704 | (u64)mblk->no, mblk->ref); |
| 2705 | mblk->ref = 0; |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 2706 | dmz_free_mblock(zmd, mblk); |
| 2707 | } |
| 2708 | |
| 2709 | /* Free the zone descriptors */ |
| 2710 | dmz_drop_zones(zmd); |
Mike Snitzer | d5ffebd | 2018-01-05 21:17:20 -0500 | [diff] [blame] | 2711 | |
| 2712 | mutex_destroy(&zmd->mblk_flush_lock); |
| 2713 | mutex_destroy(&zmd->map_lock); |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 2714 | } |
| 2715 | |
Hannes Reinecke | ca1a704 | 2020-05-11 10:24:27 +0200 | [diff] [blame] | 2716 | static void dmz_print_dev(struct dmz_metadata *zmd, int num) |
| 2717 | { |
| 2718 | struct dmz_dev *dev = &zmd->dev[num]; |
| 2719 | |
Hannes Reinecke | bd5c403 | 2020-05-11 10:24:30 +0200 | [diff] [blame] | 2720 | if (bdev_zoned_model(dev->bdev) == BLK_ZONED_NONE) |
| 2721 | dmz_dev_info(dev, "Regular block device"); |
| 2722 | else |
| 2723 | dmz_dev_info(dev, "Host-%s zoned block device", |
| 2724 | bdev_zoned_model(dev->bdev) == BLK_ZONED_HA ? |
| 2725 | "aware" : "managed"); |
| 2726 | if (zmd->sb_version > 1) { |
| 2727 | sector_t sector_offset = |
| 2728 | dev->zone_offset << zmd->zone_nr_sectors_shift; |
| 2729 | |
| 2730 | dmz_dev_info(dev, " %llu 512-byte logical sectors (offset %llu)", |
| 2731 | (u64)dev->capacity, (u64)sector_offset); |
| 2732 | dmz_dev_info(dev, " %u zones of %llu 512-byte logical sectors (offset %llu)", |
| 2733 | dev->nr_zones, (u64)zmd->zone_nr_sectors, |
| 2734 | (u64)dev->zone_offset); |
| 2735 | } else { |
| 2736 | dmz_dev_info(dev, " %llu 512-byte logical sectors", |
| 2737 | (u64)dev->capacity); |
| 2738 | dmz_dev_info(dev, " %u zones of %llu 512-byte logical sectors", |
| 2739 | dev->nr_zones, (u64)zmd->zone_nr_sectors); |
| 2740 | } |
Hannes Reinecke | ca1a704 | 2020-05-11 10:24:27 +0200 | [diff] [blame] | 2741 | } |
| 2742 | |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 2743 | /* |
| 2744 | * Initialize the zoned metadata. |
| 2745 | */ |
Hannes Reinecke | bd5c403 | 2020-05-11 10:24:30 +0200 | [diff] [blame] | 2746 | int dmz_ctr_metadata(struct dmz_dev *dev, int num_dev, |
| 2747 | struct dmz_metadata **metadata, |
Hannes Reinecke | 2234e73 | 2020-05-11 10:24:22 +0200 | [diff] [blame] | 2748 | const char *devname) |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 2749 | { |
| 2750 | struct dmz_metadata *zmd; |
Hannes Reinecke | b712287 | 2020-05-11 10:24:18 +0200 | [diff] [blame] | 2751 | unsigned int i; |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 2752 | struct dm_zone *zone; |
| 2753 | int ret; |
| 2754 | |
| 2755 | zmd = kzalloc(sizeof(struct dmz_metadata), GFP_KERNEL); |
| 2756 | if (!zmd) |
| 2757 | return -ENOMEM; |
| 2758 | |
Hannes Reinecke | 2234e73 | 2020-05-11 10:24:22 +0200 | [diff] [blame] | 2759 | strcpy(zmd->devname, devname); |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 2760 | zmd->dev = dev; |
Hannes Reinecke | bd5c403 | 2020-05-11 10:24:30 +0200 | [diff] [blame] | 2761 | zmd->nr_devs = num_dev; |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 2762 | zmd->mblk_rbtree = RB_ROOT; |
| 2763 | init_rwsem(&zmd->mblk_sem); |
| 2764 | mutex_init(&zmd->mblk_flush_lock); |
| 2765 | spin_lock_init(&zmd->mblk_lock); |
| 2766 | INIT_LIST_HEAD(&zmd->mblk_lru_list); |
| 2767 | INIT_LIST_HEAD(&zmd->mblk_dirty_list); |
| 2768 | |
| 2769 | mutex_init(&zmd->map_lock); |
| 2770 | atomic_set(&zmd->unmap_nr_rnd, 0); |
| 2771 | INIT_LIST_HEAD(&zmd->unmap_rnd_list); |
| 2772 | INIT_LIST_HEAD(&zmd->map_rnd_list); |
| 2773 | |
Hannes Reinecke | 34f5aff | 2020-05-19 10:14:20 +0200 | [diff] [blame] | 2774 | atomic_set(&zmd->unmap_nr_cache, 0); |
| 2775 | INIT_LIST_HEAD(&zmd->unmap_cache_list); |
| 2776 | INIT_LIST_HEAD(&zmd->map_cache_list); |
| 2777 | |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 2778 | atomic_set(&zmd->unmap_nr_seq, 0); |
| 2779 | INIT_LIST_HEAD(&zmd->unmap_seq_list); |
| 2780 | INIT_LIST_HEAD(&zmd->map_seq_list); |
| 2781 | |
| 2782 | atomic_set(&zmd->nr_reserved_seq_zones, 0); |
| 2783 | INIT_LIST_HEAD(&zmd->reserved_seq_zones_list); |
| 2784 | |
| 2785 | init_waitqueue_head(&zmd->free_wq); |
| 2786 | |
| 2787 | /* Initialize zone descriptors */ |
| 2788 | ret = dmz_init_zones(zmd); |
| 2789 | if (ret) |
| 2790 | goto err; |
| 2791 | |
| 2792 | /* Get super block */ |
| 2793 | ret = dmz_load_sb(zmd); |
| 2794 | if (ret) |
| 2795 | goto err; |
| 2796 | |
| 2797 | /* Set metadata zones starting from sb_zone */ |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 2798 | for (i = 0; i < zmd->nr_meta_zones << 1; i++) { |
Hannes Reinecke | 735bd7e4 | 2020-05-11 10:24:19 +0200 | [diff] [blame] | 2799 | zone = dmz_get(zmd, zmd->sb[0].zone->id + i); |
Hannes Reinecke | 34f5aff | 2020-05-19 10:14:20 +0200 | [diff] [blame] | 2800 | if (!dmz_is_rnd(zone) && !dmz_is_cache(zone)) { |
Hannes Reinecke | bd5c403 | 2020-05-11 10:24:30 +0200 | [diff] [blame] | 2801 | dmz_zmd_err(zmd, |
| 2802 | "metadata zone %d is not random", i); |
| 2803 | ret = -ENXIO; |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 2804 | goto err; |
Hannes Reinecke | bd5c403 | 2020-05-11 10:24:30 +0200 | [diff] [blame] | 2805 | } |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 2806 | set_bit(DMZ_META, &zone->flags); |
| 2807 | } |
Hannes Reinecke | bd5c403 | 2020-05-11 10:24:30 +0200 | [diff] [blame] | 2808 | if (zmd->sb[2].zone) { |
| 2809 | zone = dmz_get(zmd, zmd->sb[2].zone->id); |
| 2810 | if (!zone) { |
| 2811 | dmz_zmd_err(zmd, |
| 2812 | "Tertiary metadata zone not present"); |
| 2813 | ret = -ENXIO; |
| 2814 | goto err; |
| 2815 | } |
| 2816 | set_bit(DMZ_META, &zone->flags); |
| 2817 | } |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 2818 | /* Load mapping table */ |
| 2819 | ret = dmz_load_mapping(zmd); |
| 2820 | if (ret) |
| 2821 | goto err; |
| 2822 | |
| 2823 | /* |
| 2824 | * Cache size boundaries: allow at least 2 super blocks, the chunk map |
| 2825 | * blocks and enough blocks to be able to cache the bitmap blocks of |
| 2826 | * up to 16 zones when idle (min_nr_mblks). Otherwise, if busy, allow |
| 2827 | * the cache to add 512 more metadata blocks. |
| 2828 | */ |
| 2829 | zmd->min_nr_mblks = 2 + zmd->nr_map_blocks + zmd->zone_nr_bitmap_blocks * 16; |
| 2830 | zmd->max_nr_mblks = zmd->min_nr_mblks + 512; |
| 2831 | zmd->mblk_shrinker.count_objects = dmz_mblock_shrinker_count; |
| 2832 | zmd->mblk_shrinker.scan_objects = dmz_mblock_shrinker_scan; |
| 2833 | zmd->mblk_shrinker.seeks = DEFAULT_SEEKS; |
| 2834 | |
| 2835 | /* Metadata cache shrinker */ |
| 2836 | ret = register_shrinker(&zmd->mblk_shrinker); |
| 2837 | if (ret) { |
Hannes Reinecke | ca1a704 | 2020-05-11 10:24:27 +0200 | [diff] [blame] | 2838 | dmz_zmd_err(zmd, "Register metadata cache shrinker failed"); |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 2839 | goto err; |
| 2840 | } |
| 2841 | |
Hannes Reinecke | bd5c403 | 2020-05-11 10:24:30 +0200 | [diff] [blame] | 2842 | dmz_zmd_info(zmd, "DM-Zoned metadata version %d", zmd->sb_version); |
| 2843 | for (i = 0; i < zmd->nr_devs; i++) |
| 2844 | dmz_print_dev(zmd, i); |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 2845 | |
Hannes Reinecke | ca1a704 | 2020-05-11 10:24:27 +0200 | [diff] [blame] | 2846 | dmz_zmd_info(zmd, " %u zones of %llu 512-byte logical sectors", |
| 2847 | zmd->nr_zones, (u64)zmd->zone_nr_sectors); |
Hannes Reinecke | ae3c1f1 | 2020-05-11 10:24:28 +0200 | [diff] [blame] | 2848 | dmz_zmd_debug(zmd, " %u metadata zones", |
| 2849 | zmd->nr_meta_zones * 2); |
| 2850 | dmz_zmd_debug(zmd, " %u data zones for %u chunks", |
| 2851 | zmd->nr_data_zones, zmd->nr_chunks); |
Hannes Reinecke | 34f5aff | 2020-05-19 10:14:20 +0200 | [diff] [blame] | 2852 | dmz_zmd_debug(zmd, " %u cache zones (%u unmapped)", |
| 2853 | zmd->nr_cache, atomic_read(&zmd->unmap_nr_cache)); |
Hannes Reinecke | ae3c1f1 | 2020-05-11 10:24:28 +0200 | [diff] [blame] | 2854 | dmz_zmd_debug(zmd, " %u random zones (%u unmapped)", |
| 2855 | zmd->nr_rnd, atomic_read(&zmd->unmap_nr_rnd)); |
| 2856 | dmz_zmd_debug(zmd, " %u sequential zones (%u unmapped)", |
| 2857 | zmd->nr_seq, atomic_read(&zmd->unmap_nr_seq)); |
| 2858 | dmz_zmd_debug(zmd, " %u reserved sequential data zones", |
| 2859 | zmd->nr_reserved_seq); |
Hannes Reinecke | ca1a704 | 2020-05-11 10:24:27 +0200 | [diff] [blame] | 2860 | dmz_zmd_debug(zmd, "Format:"); |
| 2861 | dmz_zmd_debug(zmd, "%u metadata blocks per set (%u max cache)", |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 2862 | zmd->nr_meta_blocks, zmd->max_nr_mblks); |
Hannes Reinecke | ca1a704 | 2020-05-11 10:24:27 +0200 | [diff] [blame] | 2863 | dmz_zmd_debug(zmd, " %u data zone mapping blocks", |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 2864 | zmd->nr_map_blocks); |
Hannes Reinecke | ca1a704 | 2020-05-11 10:24:27 +0200 | [diff] [blame] | 2865 | dmz_zmd_debug(zmd, " %u bitmap blocks", |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 2866 | zmd->nr_bitmap_blocks); |
| 2867 | |
| 2868 | *metadata = zmd; |
| 2869 | |
| 2870 | return 0; |
| 2871 | err: |
| 2872 | dmz_cleanup_metadata(zmd); |
| 2873 | kfree(zmd); |
| 2874 | *metadata = NULL; |
| 2875 | |
| 2876 | return ret; |
| 2877 | } |
| 2878 | |
| 2879 | /* |
| 2880 | * Cleanup the zoned metadata resources. |
| 2881 | */ |
| 2882 | void dmz_dtr_metadata(struct dmz_metadata *zmd) |
| 2883 | { |
| 2884 | unregister_shrinker(&zmd->mblk_shrinker); |
| 2885 | dmz_cleanup_metadata(zmd); |
| 2886 | kfree(zmd); |
| 2887 | } |
| 2888 | |
| 2889 | /* |
| 2890 | * Check zone information on resume. |
| 2891 | */ |
| 2892 | int dmz_resume_metadata(struct dmz_metadata *zmd) |
| 2893 | { |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 2894 | struct dm_zone *zone; |
| 2895 | sector_t wp_block; |
| 2896 | unsigned int i; |
| 2897 | int ret; |
| 2898 | |
| 2899 | /* Check zones */ |
Hannes Reinecke | 3682056 | 2020-05-11 10:24:21 +0200 | [diff] [blame] | 2900 | for (i = 0; i < zmd->nr_zones; i++) { |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 2901 | zone = dmz_get(zmd, i); |
| 2902 | if (!zone) { |
Hannes Reinecke | ca1a704 | 2020-05-11 10:24:27 +0200 | [diff] [blame] | 2903 | dmz_zmd_err(zmd, "Unable to get zone %u", i); |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 2904 | return -EIO; |
| 2905 | } |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 2906 | wp_block = zone->wp_block; |
| 2907 | |
| 2908 | ret = dmz_update_zone(zmd, zone); |
| 2909 | if (ret) { |
Hannes Reinecke | ca1a704 | 2020-05-11 10:24:27 +0200 | [diff] [blame] | 2910 | dmz_zmd_err(zmd, "Broken zone %u", i); |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 2911 | return ret; |
| 2912 | } |
| 2913 | |
| 2914 | if (dmz_is_offline(zone)) { |
Hannes Reinecke | ca1a704 | 2020-05-11 10:24:27 +0200 | [diff] [blame] | 2915 | dmz_zmd_warn(zmd, "Zone %u is offline", i); |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 2916 | continue; |
| 2917 | } |
| 2918 | |
| 2919 | /* Check write pointer */ |
| 2920 | if (!dmz_is_seq(zone)) |
| 2921 | zone->wp_block = 0; |
| 2922 | else if (zone->wp_block != wp_block) { |
Hannes Reinecke | ca1a704 | 2020-05-11 10:24:27 +0200 | [diff] [blame] | 2923 | dmz_zmd_err(zmd, "Zone %u: Invalid wp (%llu / %llu)", |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 2924 | i, (u64)zone->wp_block, (u64)wp_block); |
| 2925 | zone->wp_block = wp_block; |
| 2926 | dmz_invalidate_blocks(zmd, zone, zone->wp_block, |
Hannes Reinecke | 3682056 | 2020-05-11 10:24:21 +0200 | [diff] [blame] | 2927 | zmd->zone_nr_blocks - zone->wp_block); |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 2928 | } |
| 2929 | } |
| 2930 | |
| 2931 | return 0; |
| 2932 | } |