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