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