1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2017 Western Digital Corporation or its affiliates.
5 * This file is released under the GPL.
10 #include <linux/module.h>
11 #include <linux/crc32.h>
12 #include <linux/sched/mm.h>
14 #define DM_MSG_PREFIX "zoned metadata"
19 #define DMZ_META_VER 1
22 * On-disk super block magic.
24 #define DMZ_MAGIC ((((unsigned int)('D')) << 24) | \
25 (((unsigned int)('Z')) << 16) | \
26 (((unsigned int)('B')) << 8) | \
27 ((unsigned int)('D')))
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)
38 * All metadata blocks are stored in conventional zones, starting from
39 * the first conventional zone found on disk.
45 /* Metadata version number */
46 __le32 version; /* 8 */
48 /* Generation number */
51 /* This block number */
52 __le64 sb_block; /* 24 */
54 /* The number of metadata blocks, including this super block */
55 __le32 nr_meta_blocks; /* 28 */
57 /* The number of sequential zones reserved for reclaim */
58 __le32 nr_reserved_seq; /* 32 */
60 /* The number of entries in the mapping table */
61 __le32 nr_chunks; /* 36 */
63 /* The number of blocks used for the chunk mapping table */
64 __le32 nr_map_blocks; /* 40 */
66 /* The number of blocks used for the block bitmaps */
67 __le32 nr_bitmap_blocks; /* 44 */
72 /* Padding to full 512B sector */
73 u8 reserved[464]; /* 512 */
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
90 * Chunk mapping table metadata: 512 8-bytes entries per 4KB block.
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
98 * Meta data block descriptor (for cached metadata blocks).
102 struct list_head link;
111 * Metadata block state flags.
121 * Super block information (one per metadata set).
125 struct dmz_mblock *mblk;
126 struct dmz_super *sb;
130 * In-memory metadata.
132 struct dmz_metadata {
135 sector_t zone_bitmap_size;
136 unsigned int zone_nr_bitmap_blocks;
137 unsigned int zone_bits_per_mblk;
139 unsigned int nr_bitmap_blocks;
140 unsigned int nr_map_blocks;
142 unsigned int nr_useable_zones;
143 unsigned int nr_meta_blocks;
144 unsigned int nr_meta_zones;
145 unsigned int nr_data_zones;
146 unsigned int nr_rnd_zones;
147 unsigned int nr_reserved_seq;
148 unsigned int nr_chunks;
150 /* Zone information array */
151 struct dm_zone *zones;
153 struct dm_zone *sb_zone;
155 unsigned int mblk_primary;
157 unsigned int min_nr_mblks;
158 unsigned int max_nr_mblks;
160 struct rw_semaphore mblk_sem;
161 struct mutex mblk_flush_lock;
162 spinlock_t mblk_lock;
163 struct rb_root mblk_rbtree;
164 struct list_head mblk_lru_list;
165 struct list_head mblk_dirty_list;
166 struct shrinker mblk_shrinker;
168 /* Zone allocation management */
169 struct mutex map_lock;
170 struct dmz_mblock **map_mblk;
172 atomic_t unmap_nr_rnd;
173 struct list_head unmap_rnd_list;
174 struct list_head map_rnd_list;
177 atomic_t unmap_nr_seq;
178 struct list_head unmap_seq_list;
179 struct list_head map_seq_list;
181 atomic_t nr_reserved_seq_zones;
182 struct list_head reserved_seq_zones_list;
184 wait_queue_head_t free_wq;
190 unsigned int dmz_id(struct dmz_metadata *zmd, struct dm_zone *zone)
192 return ((unsigned int)(zone - zmd->zones));
195 sector_t dmz_start_sect(struct dmz_metadata *zmd, struct dm_zone *zone)
197 return (sector_t)dmz_id(zmd, zone) << zmd->dev->zone_nr_sectors_shift;
200 sector_t dmz_start_block(struct dmz_metadata *zmd, struct dm_zone *zone)
202 return (sector_t)dmz_id(zmd, zone) << zmd->dev->zone_nr_blocks_shift;
205 unsigned int dmz_nr_chunks(struct dmz_metadata *zmd)
207 return zmd->nr_chunks;
210 unsigned int dmz_nr_rnd_zones(struct dmz_metadata *zmd)
215 unsigned int dmz_nr_unmap_rnd_zones(struct dmz_metadata *zmd)
217 return atomic_read(&zmd->unmap_nr_rnd);
221 * Lock/unlock mapping table.
222 * The map lock also protects all the zone lists.
224 void dmz_lock_map(struct dmz_metadata *zmd)
226 mutex_lock(&zmd->map_lock);
229 void dmz_unlock_map(struct dmz_metadata *zmd)
231 mutex_unlock(&zmd->map_lock);
235 * Lock/unlock metadata access. This is a "read" lock on a semaphore
236 * that prevents metadata flush from running while metadata are being
237 * modified. The actual metadata write mutual exclusion is achieved with
238 * the map lock and zone state management (active and reclaim state are
239 * mutually exclusive).
241 void dmz_lock_metadata(struct dmz_metadata *zmd)
243 down_read(&zmd->mblk_sem);
246 void dmz_unlock_metadata(struct dmz_metadata *zmd)
248 up_read(&zmd->mblk_sem);
252 * Lock/unlock flush: prevent concurrent executions
253 * of dmz_flush_metadata as well as metadata modification in reclaim
254 * while flush is being executed.
256 void dmz_lock_flush(struct dmz_metadata *zmd)
258 mutex_lock(&zmd->mblk_flush_lock);
261 void dmz_unlock_flush(struct dmz_metadata *zmd)
263 mutex_unlock(&zmd->mblk_flush_lock);
267 * Allocate a metadata block.
269 static struct dmz_mblock *dmz_alloc_mblock(struct dmz_metadata *zmd,
272 struct dmz_mblock *mblk = NULL;
274 /* See if we can reuse cached blocks */
275 if (zmd->max_nr_mblks && atomic_read(&zmd->nr_mblks) > zmd->max_nr_mblks) {
276 spin_lock(&zmd->mblk_lock);
277 mblk = list_first_entry_or_null(&zmd->mblk_lru_list,
278 struct dmz_mblock, link);
280 list_del_init(&mblk->link);
281 rb_erase(&mblk->node, &zmd->mblk_rbtree);
284 spin_unlock(&zmd->mblk_lock);
289 /* Allocate a new block */
290 mblk = kmalloc(sizeof(struct dmz_mblock), GFP_NOIO);
294 mblk->page = alloc_page(GFP_NOIO);
300 RB_CLEAR_NODE(&mblk->node);
301 INIT_LIST_HEAD(&mblk->link);
305 mblk->data = page_address(mblk->page);
307 atomic_inc(&zmd->nr_mblks);
313 * Free a metadata block.
315 static void dmz_free_mblock(struct dmz_metadata *zmd, struct dmz_mblock *mblk)
317 __free_pages(mblk->page, 0);
320 atomic_dec(&zmd->nr_mblks);
324 * Insert a metadata block in the rbtree.
326 static void dmz_insert_mblock(struct dmz_metadata *zmd, struct dmz_mblock *mblk)
328 struct rb_root *root = &zmd->mblk_rbtree;
329 struct rb_node **new = &(root->rb_node), *parent = NULL;
330 struct dmz_mblock *b;
332 /* Figure out where to put the new node */
334 b = container_of(*new, struct dmz_mblock, node);
336 new = (b->no < mblk->no) ? &((*new)->rb_left) : &((*new)->rb_right);
339 /* Add new node and rebalance tree */
340 rb_link_node(&mblk->node, parent, new);
341 rb_insert_color(&mblk->node, root);
345 * Lookup a metadata block in the rbtree. If the block is found, increment
346 * its reference count.
348 static struct dmz_mblock *dmz_get_mblock_fast(struct dmz_metadata *zmd,
351 struct rb_root *root = &zmd->mblk_rbtree;
352 struct rb_node *node = root->rb_node;
353 struct dmz_mblock *mblk;
356 mblk = container_of(node, struct dmz_mblock, node);
357 if (mblk->no == mblk_no) {
359 * If this is the first reference to the block,
360 * remove it from the LRU list.
363 if (mblk->ref == 1 &&
364 !test_bit(DMZ_META_DIRTY, &mblk->state))
365 list_del_init(&mblk->link);
368 node = (mblk->no < mblk_no) ? node->rb_left : node->rb_right;
375 * Metadata block BIO end callback.
377 static void dmz_mblock_bio_end_io(struct bio *bio)
379 struct dmz_mblock *mblk = bio->bi_private;
383 set_bit(DMZ_META_ERROR, &mblk->state);
385 if (bio_op(bio) == REQ_OP_WRITE)
386 flag = DMZ_META_WRITING;
388 flag = DMZ_META_READING;
390 clear_bit_unlock(flag, &mblk->state);
391 smp_mb__after_atomic();
392 wake_up_bit(&mblk->state, flag);
398 * Read an uncached metadata block from disk and add it to the cache.
400 static struct dmz_mblock *dmz_get_mblock_slow(struct dmz_metadata *zmd,
403 struct dmz_mblock *mblk, *m;
404 sector_t block = zmd->sb[zmd->mblk_primary].block + mblk_no;
407 if (dmz_bdev_is_dying(zmd->dev))
408 return ERR_PTR(-EIO);
410 /* Get a new block and a BIO to read it */
411 mblk = dmz_alloc_mblock(zmd, mblk_no);
413 return ERR_PTR(-ENOMEM);
415 bio = bio_alloc(GFP_NOIO, 1);
417 dmz_free_mblock(zmd, mblk);
418 return ERR_PTR(-ENOMEM);
421 spin_lock(&zmd->mblk_lock);
424 * Make sure that another context did not start reading
427 m = dmz_get_mblock_fast(zmd, mblk_no);
429 spin_unlock(&zmd->mblk_lock);
430 dmz_free_mblock(zmd, mblk);
436 set_bit(DMZ_META_READING, &mblk->state);
437 dmz_insert_mblock(zmd, mblk);
439 spin_unlock(&zmd->mblk_lock);
441 /* Submit read BIO */
442 bio->bi_iter.bi_sector = dmz_blk2sect(block);
443 bio_set_dev(bio, zmd->dev->bdev);
444 bio->bi_private = mblk;
445 bio->bi_end_io = dmz_mblock_bio_end_io;
446 bio_set_op_attrs(bio, REQ_OP_READ, REQ_META | REQ_PRIO);
447 bio_add_page(bio, mblk->page, DMZ_BLOCK_SIZE, 0);
454 * Free metadata blocks.
456 static unsigned long dmz_shrink_mblock_cache(struct dmz_metadata *zmd,
459 struct dmz_mblock *mblk;
460 unsigned long count = 0;
462 if (!zmd->max_nr_mblks)
465 while (!list_empty(&zmd->mblk_lru_list) &&
466 atomic_read(&zmd->nr_mblks) > zmd->min_nr_mblks &&
468 mblk = list_first_entry(&zmd->mblk_lru_list,
469 struct dmz_mblock, link);
470 list_del_init(&mblk->link);
471 rb_erase(&mblk->node, &zmd->mblk_rbtree);
472 dmz_free_mblock(zmd, mblk);
480 * For mblock shrinker: get the number of unused metadata blocks in the cache.
482 static unsigned long dmz_mblock_shrinker_count(struct shrinker *shrink,
483 struct shrink_control *sc)
485 struct dmz_metadata *zmd = container_of(shrink, struct dmz_metadata, mblk_shrinker);
487 return atomic_read(&zmd->nr_mblks);
491 * For mblock shrinker: scan unused metadata blocks and shrink the cache.
493 static unsigned long dmz_mblock_shrinker_scan(struct shrinker *shrink,
494 struct shrink_control *sc)
496 struct dmz_metadata *zmd = container_of(shrink, struct dmz_metadata, mblk_shrinker);
499 spin_lock(&zmd->mblk_lock);
500 count = dmz_shrink_mblock_cache(zmd, sc->nr_to_scan);
501 spin_unlock(&zmd->mblk_lock);
503 return count ? count : SHRINK_STOP;
507 * Release a metadata block.
509 static void dmz_release_mblock(struct dmz_metadata *zmd,
510 struct dmz_mblock *mblk)
516 spin_lock(&zmd->mblk_lock);
519 if (mblk->ref == 0) {
520 if (test_bit(DMZ_META_ERROR, &mblk->state)) {
521 rb_erase(&mblk->node, &zmd->mblk_rbtree);
522 dmz_free_mblock(zmd, mblk);
523 } else if (!test_bit(DMZ_META_DIRTY, &mblk->state)) {
524 list_add_tail(&mblk->link, &zmd->mblk_lru_list);
525 dmz_shrink_mblock_cache(zmd, 1);
529 spin_unlock(&zmd->mblk_lock);
533 * Get a metadata block from the rbtree. If the block
534 * is not present, read it from disk.
536 static struct dmz_mblock *dmz_get_mblock(struct dmz_metadata *zmd,
539 struct dmz_mblock *mblk;
542 spin_lock(&zmd->mblk_lock);
543 mblk = dmz_get_mblock_fast(zmd, mblk_no);
544 spin_unlock(&zmd->mblk_lock);
547 /* Cache miss: read the block from disk */
548 mblk = dmz_get_mblock_slow(zmd, mblk_no);
553 /* Wait for on-going read I/O and check for error */
554 wait_on_bit_io(&mblk->state, DMZ_META_READING,
555 TASK_UNINTERRUPTIBLE);
556 if (test_bit(DMZ_META_ERROR, &mblk->state)) {
557 dmz_release_mblock(zmd, mblk);
558 dmz_check_bdev(zmd->dev);
559 return ERR_PTR(-EIO);
566 * Mark a metadata block dirty.
568 static void dmz_dirty_mblock(struct dmz_metadata *zmd, struct dmz_mblock *mblk)
570 spin_lock(&zmd->mblk_lock);
571 if (!test_and_set_bit(DMZ_META_DIRTY, &mblk->state))
572 list_add_tail(&mblk->link, &zmd->mblk_dirty_list);
573 spin_unlock(&zmd->mblk_lock);
577 * Issue a metadata block write BIO.
579 static int dmz_write_mblock(struct dmz_metadata *zmd, struct dmz_mblock *mblk,
582 sector_t block = zmd->sb[set].block + mblk->no;
585 if (dmz_bdev_is_dying(zmd->dev))
588 bio = bio_alloc(GFP_NOIO, 1);
590 set_bit(DMZ_META_ERROR, &mblk->state);
594 set_bit(DMZ_META_WRITING, &mblk->state);
596 bio->bi_iter.bi_sector = dmz_blk2sect(block);
597 bio_set_dev(bio, zmd->dev->bdev);
598 bio->bi_private = mblk;
599 bio->bi_end_io = dmz_mblock_bio_end_io;
600 bio_set_op_attrs(bio, REQ_OP_WRITE, REQ_META | REQ_PRIO);
601 bio_add_page(bio, mblk->page, DMZ_BLOCK_SIZE, 0);
608 * Read/write a metadata block.
610 static int dmz_rdwr_block(struct dmz_metadata *zmd, int op, sector_t block,
616 if (dmz_bdev_is_dying(zmd->dev))
619 bio = bio_alloc(GFP_NOIO, 1);
623 bio->bi_iter.bi_sector = dmz_blk2sect(block);
624 bio_set_dev(bio, zmd->dev->bdev);
625 bio_set_op_attrs(bio, op, REQ_SYNC | REQ_META | REQ_PRIO);
626 bio_add_page(bio, page, DMZ_BLOCK_SIZE, 0);
627 ret = submit_bio_wait(bio);
631 dmz_check_bdev(zmd->dev);
636 * Write super block of the specified metadata set.
638 static int dmz_write_sb(struct dmz_metadata *zmd, unsigned int set)
640 sector_t block = zmd->sb[set].block;
641 struct dmz_mblock *mblk = zmd->sb[set].mblk;
642 struct dmz_super *sb = zmd->sb[set].sb;
643 u64 sb_gen = zmd->sb_gen + 1;
646 sb->magic = cpu_to_le32(DMZ_MAGIC);
647 sb->version = cpu_to_le32(DMZ_META_VER);
649 sb->gen = cpu_to_le64(sb_gen);
651 sb->sb_block = cpu_to_le64(block);
652 sb->nr_meta_blocks = cpu_to_le32(zmd->nr_meta_blocks);
653 sb->nr_reserved_seq = cpu_to_le32(zmd->nr_reserved_seq);
654 sb->nr_chunks = cpu_to_le32(zmd->nr_chunks);
656 sb->nr_map_blocks = cpu_to_le32(zmd->nr_map_blocks);
657 sb->nr_bitmap_blocks = cpu_to_le32(zmd->nr_bitmap_blocks);
660 sb->crc = cpu_to_le32(crc32_le(sb_gen, (unsigned char *)sb, DMZ_BLOCK_SIZE));
662 ret = dmz_rdwr_block(zmd, REQ_OP_WRITE, block, mblk->page);
664 ret = blkdev_issue_flush(zmd->dev->bdev, GFP_NOIO, NULL);
670 * Write dirty metadata blocks to the specified set.
672 static int dmz_write_dirty_mblocks(struct dmz_metadata *zmd,
673 struct list_head *write_list,
676 struct dmz_mblock *mblk;
677 struct blk_plug plug;
678 int ret = 0, nr_mblks_submitted = 0;
681 blk_start_plug(&plug);
682 list_for_each_entry(mblk, write_list, link) {
683 ret = dmz_write_mblock(zmd, mblk, set);
686 nr_mblks_submitted++;
688 blk_finish_plug(&plug);
690 /* Wait for completion */
691 list_for_each_entry(mblk, write_list, link) {
692 if (!nr_mblks_submitted)
694 wait_on_bit_io(&mblk->state, DMZ_META_WRITING,
695 TASK_UNINTERRUPTIBLE);
696 if (test_bit(DMZ_META_ERROR, &mblk->state)) {
697 clear_bit(DMZ_META_ERROR, &mblk->state);
698 dmz_check_bdev(zmd->dev);
701 nr_mblks_submitted--;
704 /* Flush drive cache (this will also sync data) */
706 ret = blkdev_issue_flush(zmd->dev->bdev, GFP_NOIO, NULL);
712 * Log dirty metadata blocks.
714 static int dmz_log_dirty_mblocks(struct dmz_metadata *zmd,
715 struct list_head *write_list)
717 unsigned int log_set = zmd->mblk_primary ^ 0x1;
720 /* Write dirty blocks to the log */
721 ret = dmz_write_dirty_mblocks(zmd, write_list, log_set);
726 * No error so far: now validate the log by updating the
727 * log index super block generation.
729 ret = dmz_write_sb(zmd, log_set);
737 * Flush dirty metadata blocks.
739 int dmz_flush_metadata(struct dmz_metadata *zmd)
741 struct dmz_mblock *mblk;
742 struct list_head write_list;
748 INIT_LIST_HEAD(&write_list);
751 * Make sure that metadata blocks are stable before logging: take
752 * the write lock on the metadata semaphore to prevent target BIOs
753 * from modifying metadata.
755 down_write(&zmd->mblk_sem);
758 * This is called from the target flush work and reclaim work.
759 * Concurrent execution is not allowed.
763 if (dmz_bdev_is_dying(zmd->dev)) {
768 /* Get dirty blocks */
769 spin_lock(&zmd->mblk_lock);
770 list_splice_init(&zmd->mblk_dirty_list, &write_list);
771 spin_unlock(&zmd->mblk_lock);
773 /* If there are no dirty metadata blocks, just flush the device cache */
774 if (list_empty(&write_list)) {
775 ret = blkdev_issue_flush(zmd->dev->bdev, GFP_NOIO, NULL);
780 * The primary metadata set is still clean. Keep it this way until
781 * all updates are successful in the secondary set. That is, use
782 * the secondary set as a log.
784 ret = dmz_log_dirty_mblocks(zmd, &write_list);
789 * The log is on disk. It is now safe to update in place
790 * in the primary metadata set.
792 ret = dmz_write_dirty_mblocks(zmd, &write_list, zmd->mblk_primary);
796 ret = dmz_write_sb(zmd, zmd->mblk_primary);
800 while (!list_empty(&write_list)) {
801 mblk = list_first_entry(&write_list, struct dmz_mblock, link);
802 list_del_init(&mblk->link);
804 spin_lock(&zmd->mblk_lock);
805 clear_bit(DMZ_META_DIRTY, &mblk->state);
807 list_add_tail(&mblk->link, &zmd->mblk_lru_list);
808 spin_unlock(&zmd->mblk_lock);
813 dmz_unlock_flush(zmd);
814 up_write(&zmd->mblk_sem);
819 if (!list_empty(&write_list)) {
820 spin_lock(&zmd->mblk_lock);
821 list_splice(&write_list, &zmd->mblk_dirty_list);
822 spin_unlock(&zmd->mblk_lock);
824 if (!dmz_check_bdev(zmd->dev))
832 static int dmz_check_sb(struct dmz_metadata *zmd, struct dmz_super *sb)
834 unsigned int nr_meta_zones, nr_data_zones;
835 struct dmz_dev *dev = zmd->dev;
839 gen = le64_to_cpu(sb->gen);
840 stored_crc = le32_to_cpu(sb->crc);
842 crc = crc32_le(gen, (unsigned char *)sb, DMZ_BLOCK_SIZE);
843 if (crc != stored_crc) {
844 dmz_dev_err(dev, "Invalid checksum (needed 0x%08x, got 0x%08x)",
849 if (le32_to_cpu(sb->magic) != DMZ_MAGIC) {
850 dmz_dev_err(dev, "Invalid meta magic (needed 0x%08x, got 0x%08x)",
851 DMZ_MAGIC, le32_to_cpu(sb->magic));
855 if (le32_to_cpu(sb->version) != DMZ_META_VER) {
856 dmz_dev_err(dev, "Invalid meta version (needed %d, got %d)",
857 DMZ_META_VER, le32_to_cpu(sb->version));
861 nr_meta_zones = (le32_to_cpu(sb->nr_meta_blocks) + dev->zone_nr_blocks - 1)
862 >> dev->zone_nr_blocks_shift;
863 if (!nr_meta_zones ||
864 nr_meta_zones >= zmd->nr_rnd_zones) {
865 dmz_dev_err(dev, "Invalid number of metadata blocks");
869 if (!le32_to_cpu(sb->nr_reserved_seq) ||
870 le32_to_cpu(sb->nr_reserved_seq) >= (zmd->nr_useable_zones - nr_meta_zones)) {
871 dmz_dev_err(dev, "Invalid number of reserved sequential zones");
875 nr_data_zones = zmd->nr_useable_zones -
876 (nr_meta_zones * 2 + le32_to_cpu(sb->nr_reserved_seq));
877 if (le32_to_cpu(sb->nr_chunks) > nr_data_zones) {
878 dmz_dev_err(dev, "Invalid number of chunks %u / %u",
879 le32_to_cpu(sb->nr_chunks), nr_data_zones);
884 zmd->nr_meta_blocks = le32_to_cpu(sb->nr_meta_blocks);
885 zmd->nr_reserved_seq = le32_to_cpu(sb->nr_reserved_seq);
886 zmd->nr_chunks = le32_to_cpu(sb->nr_chunks);
887 zmd->nr_map_blocks = le32_to_cpu(sb->nr_map_blocks);
888 zmd->nr_bitmap_blocks = le32_to_cpu(sb->nr_bitmap_blocks);
889 zmd->nr_meta_zones = nr_meta_zones;
890 zmd->nr_data_zones = nr_data_zones;
896 * Read the first or second super block from disk.
898 static int dmz_read_sb(struct dmz_metadata *zmd, unsigned int set)
900 return dmz_rdwr_block(zmd, REQ_OP_READ, zmd->sb[set].block,
901 zmd->sb[set].mblk->page);
905 * Determine the position of the secondary super blocks on disk.
906 * This is used only if a corruption of the primary super block
909 static int dmz_lookup_secondary_sb(struct dmz_metadata *zmd)
911 unsigned int zone_nr_blocks = zmd->dev->zone_nr_blocks;
912 struct dmz_mblock *mblk;
915 /* Allocate a block */
916 mblk = dmz_alloc_mblock(zmd, 0);
920 zmd->sb[1].mblk = mblk;
921 zmd->sb[1].sb = mblk->data;
923 /* Bad first super block: search for the second one */
924 zmd->sb[1].block = zmd->sb[0].block + zone_nr_blocks;
925 for (i = 0; i < zmd->nr_rnd_zones - 1; i++) {
926 if (dmz_read_sb(zmd, 1) != 0)
928 if (le32_to_cpu(zmd->sb[1].sb->magic) == DMZ_MAGIC)
930 zmd->sb[1].block += zone_nr_blocks;
933 dmz_free_mblock(zmd, mblk);
934 zmd->sb[1].mblk = NULL;
940 * Read the first or second super block from disk.
942 static int dmz_get_sb(struct dmz_metadata *zmd, unsigned int set)
944 struct dmz_mblock *mblk;
947 /* Allocate a block */
948 mblk = dmz_alloc_mblock(zmd, 0);
952 zmd->sb[set].mblk = mblk;
953 zmd->sb[set].sb = mblk->data;
955 /* Read super block */
956 ret = dmz_read_sb(zmd, set);
958 dmz_free_mblock(zmd, mblk);
959 zmd->sb[set].mblk = NULL;
967 * Recover a metadata set.
969 static int dmz_recover_mblocks(struct dmz_metadata *zmd, unsigned int dst_set)
971 unsigned int src_set = dst_set ^ 0x1;
975 dmz_dev_warn(zmd->dev, "Metadata set %u invalid: recovering", dst_set);
978 zmd->sb[0].block = dmz_start_block(zmd, zmd->sb_zone);
980 zmd->sb[1].block = zmd->sb[0].block +
981 (zmd->nr_meta_zones << zmd->dev->zone_nr_blocks_shift);
984 page = alloc_page(GFP_NOIO);
988 /* Copy metadata blocks */
989 for (i = 1; i < zmd->nr_meta_blocks; i++) {
990 ret = dmz_rdwr_block(zmd, REQ_OP_READ,
991 zmd->sb[src_set].block + i, page);
994 ret = dmz_rdwr_block(zmd, REQ_OP_WRITE,
995 zmd->sb[dst_set].block + i, page);
1000 /* Finalize with the super block */
1001 if (!zmd->sb[dst_set].mblk) {
1002 zmd->sb[dst_set].mblk = dmz_alloc_mblock(zmd, 0);
1003 if (!zmd->sb[dst_set].mblk) {
1007 zmd->sb[dst_set].sb = zmd->sb[dst_set].mblk->data;
1010 ret = dmz_write_sb(zmd, dst_set);
1012 __free_pages(page, 0);
1018 * Get super block from disk.
1020 static int dmz_load_sb(struct dmz_metadata *zmd)
1022 bool sb_good[2] = {false, false};
1023 u64 sb_gen[2] = {0, 0};
1026 /* Read and check the primary super block */
1027 zmd->sb[0].block = dmz_start_block(zmd, zmd->sb_zone);
1028 ret = dmz_get_sb(zmd, 0);
1030 dmz_dev_err(zmd->dev, "Read primary super block failed");
1034 ret = dmz_check_sb(zmd, zmd->sb[0].sb);
1036 /* Read and check secondary super block */
1039 zmd->sb[1].block = zmd->sb[0].block +
1040 (zmd->nr_meta_zones << zmd->dev->zone_nr_blocks_shift);
1041 ret = dmz_get_sb(zmd, 1);
1043 ret = dmz_lookup_secondary_sb(zmd);
1046 dmz_dev_err(zmd->dev, "Read secondary super block failed");
1050 ret = dmz_check_sb(zmd, zmd->sb[1].sb);
1054 /* Use highest generation sb first */
1055 if (!sb_good[0] && !sb_good[1]) {
1056 dmz_dev_err(zmd->dev, "No valid super block found");
1061 sb_gen[0] = le64_to_cpu(zmd->sb[0].sb->gen);
1063 ret = dmz_recover_mblocks(zmd, 0);
1066 sb_gen[1] = le64_to_cpu(zmd->sb[1].sb->gen);
1068 ret = dmz_recover_mblocks(zmd, 1);
1071 dmz_dev_err(zmd->dev, "Recovery failed");
1075 if (sb_gen[0] >= sb_gen[1]) {
1076 zmd->sb_gen = sb_gen[0];
1077 zmd->mblk_primary = 0;
1079 zmd->sb_gen = sb_gen[1];
1080 zmd->mblk_primary = 1;
1083 dmz_dev_debug(zmd->dev, "Using super block %u (gen %llu)",
1084 zmd->mblk_primary, zmd->sb_gen);
1090 * Initialize a zone descriptor.
1092 static int dmz_init_zone(struct dmz_metadata *zmd, struct dm_zone *zone,
1093 struct blk_zone *blkz)
1095 struct dmz_dev *dev = zmd->dev;
1097 /* Ignore the eventual last runt (smaller) zone */
1098 if (blkz->len != dev->zone_nr_sectors) {
1099 if (blkz->start + blkz->len == dev->capacity)
1104 INIT_LIST_HEAD(&zone->link);
1105 atomic_set(&zone->refcount, 0);
1106 zone->chunk = DMZ_MAP_UNMAPPED;
1108 if (blkz->type == BLK_ZONE_TYPE_CONVENTIONAL) {
1109 set_bit(DMZ_RND, &zone->flags);
1110 zmd->nr_rnd_zones++;
1111 } else if (blkz->type == BLK_ZONE_TYPE_SEQWRITE_REQ ||
1112 blkz->type == BLK_ZONE_TYPE_SEQWRITE_PREF) {
1113 set_bit(DMZ_SEQ, &zone->flags);
1117 if (blkz->cond == BLK_ZONE_COND_OFFLINE)
1118 set_bit(DMZ_OFFLINE, &zone->flags);
1119 else if (blkz->cond == BLK_ZONE_COND_READONLY)
1120 set_bit(DMZ_READ_ONLY, &zone->flags);
1122 if (dmz_is_rnd(zone))
1125 zone->wp_block = dmz_sect2blk(blkz->wp - blkz->start);
1127 if (!dmz_is_offline(zone) && !dmz_is_readonly(zone)) {
1128 zmd->nr_useable_zones++;
1129 if (dmz_is_rnd(zone)) {
1130 zmd->nr_rnd_zones++;
1131 if (!zmd->sb_zone) {
1132 /* Super block zone */
1133 zmd->sb_zone = zone;
1142 * Free zones descriptors.
1144 static void dmz_drop_zones(struct dmz_metadata *zmd)
1151 * The size of a zone report in number of zones.
1152 * This results in 4096*64B=256KB report zones commands.
1154 #define DMZ_REPORT_NR_ZONES 4096
1157 * Allocate and initialize zone descriptors using the zone
1158 * information from disk.
1160 static int dmz_init_zones(struct dmz_metadata *zmd)
1162 struct dmz_dev *dev = zmd->dev;
1163 struct dm_zone *zone;
1164 struct blk_zone *blkz;
1165 unsigned int nr_blkz;
1166 sector_t sector = 0;
1170 zmd->zone_bitmap_size = dev->zone_nr_blocks >> 3;
1171 zmd->zone_nr_bitmap_blocks =
1172 max_t(sector_t, 1, zmd->zone_bitmap_size >> DMZ_BLOCK_SHIFT);
1173 zmd->zone_bits_per_mblk = min_t(sector_t, dev->zone_nr_blocks,
1174 DMZ_BLOCK_SIZE_BITS);
1176 /* Allocate zone array */
1177 zmd->zones = kcalloc(dev->nr_zones, sizeof(struct dm_zone), GFP_KERNEL);
1181 dmz_dev_info(dev, "Using %zu B for zone information",
1182 sizeof(struct dm_zone) * dev->nr_zones);
1184 /* Get zone information */
1185 nr_blkz = DMZ_REPORT_NR_ZONES;
1186 blkz = kcalloc(nr_blkz, sizeof(struct blk_zone), GFP_KERNEL);
1193 * Get zone information and initialize zone descriptors.
1194 * At the same time, determine where the super block
1195 * should be: first block of the first randomly writable
1199 while (sector < dev->capacity) {
1200 /* Get zone information */
1201 nr_blkz = DMZ_REPORT_NR_ZONES;
1202 ret = blkdev_report_zones(dev->bdev, sector, blkz, &nr_blkz);
1204 dmz_dev_err(dev, "Report zones failed %d", ret);
1211 /* Process report */
1212 for (i = 0; i < nr_blkz; i++) {
1213 ret = dmz_init_zone(zmd, zone, &blkz[i]);
1216 sector += dev->zone_nr_sectors;
1221 /* The entire zone configuration of the disk should now be known */
1222 if (sector < dev->capacity) {
1223 dmz_dev_err(dev, "Failed to get correct zone information");
1229 dmz_drop_zones(zmd);
1235 * Update a zone information.
1237 static int dmz_update_zone(struct dmz_metadata *zmd, struct dm_zone *zone)
1239 unsigned int nr_blkz = 1;
1240 unsigned int noio_flag;
1241 struct blk_zone blkz;
1245 * Get zone information from disk. Since blkdev_report_zones() uses
1246 * GFP_KERNEL by default for memory allocations, set the per-task
1247 * PF_MEMALLOC_NOIO flag so that all allocations are done as if
1248 * GFP_NOIO was specified.
1250 noio_flag = memalloc_noio_save();
1251 ret = blkdev_report_zones(zmd->dev->bdev, dmz_start_sect(zmd, zone),
1253 memalloc_noio_restore(noio_flag);
1257 dmz_dev_err(zmd->dev, "Get zone %u report failed",
1259 dmz_check_bdev(zmd->dev);
1263 clear_bit(DMZ_OFFLINE, &zone->flags);
1264 clear_bit(DMZ_READ_ONLY, &zone->flags);
1265 if (blkz.cond == BLK_ZONE_COND_OFFLINE)
1266 set_bit(DMZ_OFFLINE, &zone->flags);
1267 else if (blkz.cond == BLK_ZONE_COND_READONLY)
1268 set_bit(DMZ_READ_ONLY, &zone->flags);
1270 if (dmz_is_seq(zone))
1271 zone->wp_block = dmz_sect2blk(blkz.wp - blkz.start);
1279 * Check a zone write pointer position when the zone is marked
1280 * with the sequential write error flag.
1282 static int dmz_handle_seq_write_err(struct dmz_metadata *zmd,
1283 struct dm_zone *zone)
1285 unsigned int wp = 0;
1288 wp = zone->wp_block;
1289 ret = dmz_update_zone(zmd, zone);
1293 dmz_dev_warn(zmd->dev, "Processing zone %u write error (zone wp %u/%u)",
1294 dmz_id(zmd, zone), zone->wp_block, wp);
1296 if (zone->wp_block < wp) {
1297 dmz_invalidate_blocks(zmd, zone, zone->wp_block,
1298 wp - zone->wp_block);
1304 static struct dm_zone *dmz_get(struct dmz_metadata *zmd, unsigned int zone_id)
1306 return &zmd->zones[zone_id];
1310 * Reset a zone write pointer.
1312 static int dmz_reset_zone(struct dmz_metadata *zmd, struct dm_zone *zone)
1317 * Ignore offline zones, read only zones,
1318 * and conventional zones.
1320 if (dmz_is_offline(zone) ||
1321 dmz_is_readonly(zone) ||
1325 if (!dmz_is_empty(zone) || dmz_seq_write_err(zone)) {
1326 struct dmz_dev *dev = zmd->dev;
1328 ret = blkdev_reset_zones(dev->bdev,
1329 dmz_start_sect(zmd, zone),
1330 dev->zone_nr_sectors, GFP_NOIO);
1332 dmz_dev_err(dev, "Reset zone %u failed %d",
1333 dmz_id(zmd, zone), ret);
1338 /* Clear write error bit and rewind write pointer position */
1339 clear_bit(DMZ_SEQ_WRITE_ERR, &zone->flags);
1345 static void dmz_get_zone_weight(struct dmz_metadata *zmd, struct dm_zone *zone);
1348 * Initialize chunk mapping.
1350 static int dmz_load_mapping(struct dmz_metadata *zmd)
1352 struct dmz_dev *dev = zmd->dev;
1353 struct dm_zone *dzone, *bzone;
1354 struct dmz_mblock *dmap_mblk = NULL;
1355 struct dmz_map *dmap;
1356 unsigned int i = 0, e = 0, chunk = 0;
1357 unsigned int dzone_id;
1358 unsigned int bzone_id;
1360 /* Metadata block array for the chunk mapping table */
1361 zmd->map_mblk = kcalloc(zmd->nr_map_blocks,
1362 sizeof(struct dmz_mblk *), GFP_KERNEL);
1366 /* Get chunk mapping table blocks and initialize zone mapping */
1367 while (chunk < zmd->nr_chunks) {
1369 /* Get mapping block */
1370 dmap_mblk = dmz_get_mblock(zmd, i + 1);
1371 if (IS_ERR(dmap_mblk))
1372 return PTR_ERR(dmap_mblk);
1373 zmd->map_mblk[i] = dmap_mblk;
1374 dmap = (struct dmz_map *) dmap_mblk->data;
1379 /* Check data zone */
1380 dzone_id = le32_to_cpu(dmap[e].dzone_id);
1381 if (dzone_id == DMZ_MAP_UNMAPPED)
1384 if (dzone_id >= dev->nr_zones) {
1385 dmz_dev_err(dev, "Chunk %u mapping: invalid data zone ID %u",
1390 dzone = dmz_get(zmd, dzone_id);
1391 set_bit(DMZ_DATA, &dzone->flags);
1392 dzone->chunk = chunk;
1393 dmz_get_zone_weight(zmd, dzone);
1395 if (dmz_is_rnd(dzone))
1396 list_add_tail(&dzone->link, &zmd->map_rnd_list);
1398 list_add_tail(&dzone->link, &zmd->map_seq_list);
1400 /* Check buffer zone */
1401 bzone_id = le32_to_cpu(dmap[e].bzone_id);
1402 if (bzone_id == DMZ_MAP_UNMAPPED)
1405 if (bzone_id >= dev->nr_zones) {
1406 dmz_dev_err(dev, "Chunk %u mapping: invalid buffer zone ID %u",
1411 bzone = dmz_get(zmd, bzone_id);
1412 if (!dmz_is_rnd(bzone)) {
1413 dmz_dev_err(dev, "Chunk %u mapping: invalid buffer zone %u",
1418 set_bit(DMZ_DATA, &bzone->flags);
1419 set_bit(DMZ_BUF, &bzone->flags);
1420 bzone->chunk = chunk;
1421 bzone->bzone = dzone;
1422 dzone->bzone = bzone;
1423 dmz_get_zone_weight(zmd, bzone);
1424 list_add_tail(&bzone->link, &zmd->map_rnd_list);
1428 if (e >= DMZ_MAP_ENTRIES)
1433 * At this point, only meta zones and mapped data zones were
1434 * fully initialized. All remaining zones are unmapped data
1435 * zones. Finish initializing those here.
1437 for (i = 0; i < dev->nr_zones; i++) {
1438 dzone = dmz_get(zmd, i);
1439 if (dmz_is_meta(dzone))
1442 if (dmz_is_rnd(dzone))
1447 if (dmz_is_data(dzone)) {
1448 /* Already initialized */
1452 /* Unmapped data zone */
1453 set_bit(DMZ_DATA, &dzone->flags);
1454 dzone->chunk = DMZ_MAP_UNMAPPED;
1455 if (dmz_is_rnd(dzone)) {
1456 list_add_tail(&dzone->link, &zmd->unmap_rnd_list);
1457 atomic_inc(&zmd->unmap_nr_rnd);
1458 } else if (atomic_read(&zmd->nr_reserved_seq_zones) < zmd->nr_reserved_seq) {
1459 list_add_tail(&dzone->link, &zmd->reserved_seq_zones_list);
1460 atomic_inc(&zmd->nr_reserved_seq_zones);
1463 list_add_tail(&dzone->link, &zmd->unmap_seq_list);
1464 atomic_inc(&zmd->unmap_nr_seq);
1472 * Set a data chunk mapping.
1474 static void dmz_set_chunk_mapping(struct dmz_metadata *zmd, unsigned int chunk,
1475 unsigned int dzone_id, unsigned int bzone_id)
1477 struct dmz_mblock *dmap_mblk = zmd->map_mblk[chunk >> DMZ_MAP_ENTRIES_SHIFT];
1478 struct dmz_map *dmap = (struct dmz_map *) dmap_mblk->data;
1479 int map_idx = chunk & DMZ_MAP_ENTRIES_MASK;
1481 dmap[map_idx].dzone_id = cpu_to_le32(dzone_id);
1482 dmap[map_idx].bzone_id = cpu_to_le32(bzone_id);
1483 dmz_dirty_mblock(zmd, dmap_mblk);
1487 * The list of mapped zones is maintained in LRU order.
1488 * This rotates a zone at the end of its map list.
1490 static void __dmz_lru_zone(struct dmz_metadata *zmd, struct dm_zone *zone)
1492 if (list_empty(&zone->link))
1495 list_del_init(&zone->link);
1496 if (dmz_is_seq(zone)) {
1497 /* LRU rotate sequential zone */
1498 list_add_tail(&zone->link, &zmd->map_seq_list);
1500 /* LRU rotate random zone */
1501 list_add_tail(&zone->link, &zmd->map_rnd_list);
1506 * The list of mapped random zones is maintained
1507 * in LRU order. This rotates a zone at the end of the list.
1509 static void dmz_lru_zone(struct dmz_metadata *zmd, struct dm_zone *zone)
1511 __dmz_lru_zone(zmd, zone);
1513 __dmz_lru_zone(zmd, zone->bzone);
1517 * Wait for any zone to be freed.
1519 static void dmz_wait_for_free_zones(struct dmz_metadata *zmd)
1523 prepare_to_wait(&zmd->free_wq, &wait, TASK_UNINTERRUPTIBLE);
1524 dmz_unlock_map(zmd);
1525 dmz_unlock_metadata(zmd);
1527 io_schedule_timeout(HZ);
1529 dmz_lock_metadata(zmd);
1531 finish_wait(&zmd->free_wq, &wait);
1535 * Lock a zone for reclaim (set the zone RECLAIM bit).
1536 * Returns false if the zone cannot be locked or if it is already locked
1539 int dmz_lock_zone_reclaim(struct dm_zone *zone)
1541 /* Active zones cannot be reclaimed */
1542 if (dmz_is_active(zone))
1545 return !test_and_set_bit(DMZ_RECLAIM, &zone->flags);
1549 * Clear a zone reclaim flag.
1551 void dmz_unlock_zone_reclaim(struct dm_zone *zone)
1553 WARN_ON(dmz_is_active(zone));
1554 WARN_ON(!dmz_in_reclaim(zone));
1556 clear_bit_unlock(DMZ_RECLAIM, &zone->flags);
1557 smp_mb__after_atomic();
1558 wake_up_bit(&zone->flags, DMZ_RECLAIM);
1562 * Wait for a zone reclaim to complete.
1564 static void dmz_wait_for_reclaim(struct dmz_metadata *zmd, struct dm_zone *zone)
1566 dmz_unlock_map(zmd);
1567 dmz_unlock_metadata(zmd);
1568 wait_on_bit_timeout(&zone->flags, DMZ_RECLAIM, TASK_UNINTERRUPTIBLE, HZ);
1569 dmz_lock_metadata(zmd);
1574 * Select a random write zone for reclaim.
1576 static struct dm_zone *dmz_get_rnd_zone_for_reclaim(struct dmz_metadata *zmd)
1578 struct dm_zone *dzone = NULL;
1579 struct dm_zone *zone;
1581 if (list_empty(&zmd->map_rnd_list))
1582 return ERR_PTR(-EBUSY);
1584 list_for_each_entry(zone, &zmd->map_rnd_list, link) {
1585 if (dmz_is_buf(zone))
1586 dzone = zone->bzone;
1589 if (dmz_lock_zone_reclaim(dzone))
1593 return ERR_PTR(-EBUSY);
1597 * Select a buffered sequential zone for reclaim.
1599 static struct dm_zone *dmz_get_seq_zone_for_reclaim(struct dmz_metadata *zmd)
1601 struct dm_zone *zone;
1603 if (list_empty(&zmd->map_seq_list))
1604 return ERR_PTR(-EBUSY);
1606 list_for_each_entry(zone, &zmd->map_seq_list, link) {
1609 if (dmz_lock_zone_reclaim(zone))
1613 return ERR_PTR(-EBUSY);
1617 * Select a zone for reclaim.
1619 struct dm_zone *dmz_get_zone_for_reclaim(struct dmz_metadata *zmd)
1621 struct dm_zone *zone;
1624 * Search for a zone candidate to reclaim: 2 cases are possible.
1625 * (1) There is no free sequential zones. Then a random data zone
1626 * cannot be reclaimed. So choose a sequential zone to reclaim so
1627 * that afterward a random zone can be reclaimed.
1628 * (2) At least one free sequential zone is available, then choose
1629 * the oldest random zone (data or buffer) that can be locked.
1632 if (list_empty(&zmd->reserved_seq_zones_list))
1633 zone = dmz_get_seq_zone_for_reclaim(zmd);
1635 zone = dmz_get_rnd_zone_for_reclaim(zmd);
1636 dmz_unlock_map(zmd);
1642 * Get the zone mapping a chunk, if the chunk is mapped already.
1643 * If no mapping exist and the operation is WRITE, a zone is
1644 * allocated and used to map the chunk.
1645 * The zone returned will be set to the active state.
1647 struct dm_zone *dmz_get_chunk_mapping(struct dmz_metadata *zmd, unsigned int chunk, int op)
1649 struct dmz_mblock *dmap_mblk = zmd->map_mblk[chunk >> DMZ_MAP_ENTRIES_SHIFT];
1650 struct dmz_map *dmap = (struct dmz_map *) dmap_mblk->data;
1651 int dmap_idx = chunk & DMZ_MAP_ENTRIES_MASK;
1652 unsigned int dzone_id;
1653 struct dm_zone *dzone = NULL;
1658 /* Get the chunk mapping */
1659 dzone_id = le32_to_cpu(dmap[dmap_idx].dzone_id);
1660 if (dzone_id == DMZ_MAP_UNMAPPED) {
1662 * Read or discard in unmapped chunks are fine. But for
1663 * writes, we need a mapping, so get one.
1665 if (op != REQ_OP_WRITE)
1668 /* Allocate a random zone */
1669 dzone = dmz_alloc_zone(zmd, DMZ_ALLOC_RND);
1671 if (dmz_bdev_is_dying(zmd->dev)) {
1672 dzone = ERR_PTR(-EIO);
1675 dmz_wait_for_free_zones(zmd);
1679 dmz_map_zone(zmd, dzone, chunk);
1682 /* The chunk is already mapped: get the mapping zone */
1683 dzone = dmz_get(zmd, dzone_id);
1684 if (dzone->chunk != chunk) {
1685 dzone = ERR_PTR(-EIO);
1689 /* Repair write pointer if the sequential dzone has error */
1690 if (dmz_seq_write_err(dzone)) {
1691 ret = dmz_handle_seq_write_err(zmd, dzone);
1693 dzone = ERR_PTR(-EIO);
1696 clear_bit(DMZ_SEQ_WRITE_ERR, &dzone->flags);
1701 * If the zone is being reclaimed, the chunk mapping may change
1702 * to a different zone. So wait for reclaim and retry. Otherwise,
1703 * activate the zone (this will prevent reclaim from touching it).
1705 if (dmz_in_reclaim(dzone)) {
1706 dmz_wait_for_reclaim(zmd, dzone);
1709 dmz_activate_zone(dzone);
1710 dmz_lru_zone(zmd, dzone);
1712 dmz_unlock_map(zmd);
1718 * Write and discard change the block validity of data zones and their buffer
1719 * zones. Check here that valid blocks are still present. If all blocks are
1720 * invalid, the zones can be unmapped on the fly without waiting for reclaim
1723 void dmz_put_chunk_mapping(struct dmz_metadata *zmd, struct dm_zone *dzone)
1725 struct dm_zone *bzone;
1729 bzone = dzone->bzone;
1731 if (dmz_weight(bzone))
1732 dmz_lru_zone(zmd, bzone);
1734 /* Empty buffer zone: reclaim it */
1735 dmz_unmap_zone(zmd, bzone);
1736 dmz_free_zone(zmd, bzone);
1741 /* Deactivate the data zone */
1742 dmz_deactivate_zone(dzone);
1743 if (dmz_is_active(dzone) || bzone || dmz_weight(dzone))
1744 dmz_lru_zone(zmd, dzone);
1746 /* Unbuffered inactive empty data zone: reclaim it */
1747 dmz_unmap_zone(zmd, dzone);
1748 dmz_free_zone(zmd, dzone);
1751 dmz_unlock_map(zmd);
1755 * Allocate and map a random zone to buffer a chunk
1756 * already mapped to a sequential zone.
1758 struct dm_zone *dmz_get_chunk_buffer(struct dmz_metadata *zmd,
1759 struct dm_zone *dzone)
1761 struct dm_zone *bzone;
1765 bzone = dzone->bzone;
1769 /* Allocate a random zone */
1770 bzone = dmz_alloc_zone(zmd, DMZ_ALLOC_RND);
1772 if (dmz_bdev_is_dying(zmd->dev)) {
1773 bzone = ERR_PTR(-EIO);
1776 dmz_wait_for_free_zones(zmd);
1780 /* Update the chunk mapping */
1781 dmz_set_chunk_mapping(zmd, dzone->chunk, dmz_id(zmd, dzone),
1782 dmz_id(zmd, bzone));
1784 set_bit(DMZ_BUF, &bzone->flags);
1785 bzone->chunk = dzone->chunk;
1786 bzone->bzone = dzone;
1787 dzone->bzone = bzone;
1788 list_add_tail(&bzone->link, &zmd->map_rnd_list);
1790 dmz_unlock_map(zmd);
1796 * Get an unmapped (free) zone.
1797 * This must be called with the mapping lock held.
1799 struct dm_zone *dmz_alloc_zone(struct dmz_metadata *zmd, unsigned long flags)
1801 struct list_head *list;
1802 struct dm_zone *zone;
1804 if (flags & DMZ_ALLOC_RND)
1805 list = &zmd->unmap_rnd_list;
1807 list = &zmd->unmap_seq_list;
1809 if (list_empty(list)) {
1811 * No free zone: if this is for reclaim, allow using the
1812 * reserved sequential zones.
1814 if (!(flags & DMZ_ALLOC_RECLAIM) ||
1815 list_empty(&zmd->reserved_seq_zones_list))
1818 zone = list_first_entry(&zmd->reserved_seq_zones_list,
1819 struct dm_zone, link);
1820 list_del_init(&zone->link);
1821 atomic_dec(&zmd->nr_reserved_seq_zones);
1825 zone = list_first_entry(list, struct dm_zone, link);
1826 list_del_init(&zone->link);
1828 if (dmz_is_rnd(zone))
1829 atomic_dec(&zmd->unmap_nr_rnd);
1831 atomic_dec(&zmd->unmap_nr_seq);
1833 if (dmz_is_offline(zone)) {
1834 dmz_dev_warn(zmd->dev, "Zone %u is offline", dmz_id(zmd, zone));
1844 * This must be called with the mapping lock held.
1846 void dmz_free_zone(struct dmz_metadata *zmd, struct dm_zone *zone)
1848 /* If this is a sequential zone, reset it */
1849 if (dmz_is_seq(zone))
1850 dmz_reset_zone(zmd, zone);
1852 /* Return the zone to its type unmap list */
1853 if (dmz_is_rnd(zone)) {
1854 list_add_tail(&zone->link, &zmd->unmap_rnd_list);
1855 atomic_inc(&zmd->unmap_nr_rnd);
1856 } else if (atomic_read(&zmd->nr_reserved_seq_zones) <
1857 zmd->nr_reserved_seq) {
1858 list_add_tail(&zone->link, &zmd->reserved_seq_zones_list);
1859 atomic_inc(&zmd->nr_reserved_seq_zones);
1861 list_add_tail(&zone->link, &zmd->unmap_seq_list);
1862 atomic_inc(&zmd->unmap_nr_seq);
1865 wake_up_all(&zmd->free_wq);
1869 * Map a chunk to a zone.
1870 * This must be called with the mapping lock held.
1872 void dmz_map_zone(struct dmz_metadata *zmd, struct dm_zone *dzone,
1875 /* Set the chunk mapping */
1876 dmz_set_chunk_mapping(zmd, chunk, dmz_id(zmd, dzone),
1878 dzone->chunk = chunk;
1879 if (dmz_is_rnd(dzone))
1880 list_add_tail(&dzone->link, &zmd->map_rnd_list);
1882 list_add_tail(&dzone->link, &zmd->map_seq_list);
1887 * This must be called with the mapping lock held.
1889 void dmz_unmap_zone(struct dmz_metadata *zmd, struct dm_zone *zone)
1891 unsigned int chunk = zone->chunk;
1892 unsigned int dzone_id;
1894 if (chunk == DMZ_MAP_UNMAPPED) {
1895 /* Already unmapped */
1899 if (test_and_clear_bit(DMZ_BUF, &zone->flags)) {
1901 * Unmapping the chunk buffer zone: clear only
1902 * the chunk buffer mapping
1904 dzone_id = dmz_id(zmd, zone->bzone);
1905 zone->bzone->bzone = NULL;
1910 * Unmapping the chunk data zone: the zone must
1913 if (WARN_ON(zone->bzone)) {
1914 zone->bzone->bzone = NULL;
1917 dzone_id = DMZ_MAP_UNMAPPED;
1920 dmz_set_chunk_mapping(zmd, chunk, dzone_id, DMZ_MAP_UNMAPPED);
1922 zone->chunk = DMZ_MAP_UNMAPPED;
1923 list_del_init(&zone->link);
1927 * Set @nr_bits bits in @bitmap starting from @bit.
1928 * Return the number of bits changed from 0 to 1.
1930 static unsigned int dmz_set_bits(unsigned long *bitmap,
1931 unsigned int bit, unsigned int nr_bits)
1933 unsigned long *addr;
1934 unsigned int end = bit + nr_bits;
1938 if (((bit & (BITS_PER_LONG - 1)) == 0) &&
1939 ((end - bit) >= BITS_PER_LONG)) {
1940 /* Try to set the whole word at once */
1941 addr = bitmap + BIT_WORD(bit);
1945 bit += BITS_PER_LONG;
1950 if (!test_and_set_bit(bit, bitmap))
1959 * Get the bitmap block storing the bit for chunk_block in zone.
1961 static struct dmz_mblock *dmz_get_bitmap(struct dmz_metadata *zmd,
1962 struct dm_zone *zone,
1963 sector_t chunk_block)
1965 sector_t bitmap_block = 1 + zmd->nr_map_blocks +
1966 (sector_t)(dmz_id(zmd, zone) * zmd->zone_nr_bitmap_blocks) +
1967 (chunk_block >> DMZ_BLOCK_SHIFT_BITS);
1969 return dmz_get_mblock(zmd, bitmap_block);
1973 * Copy the valid blocks bitmap of from_zone to the bitmap of to_zone.
1975 int dmz_copy_valid_blocks(struct dmz_metadata *zmd, struct dm_zone *from_zone,
1976 struct dm_zone *to_zone)
1978 struct dmz_mblock *from_mblk, *to_mblk;
1979 sector_t chunk_block = 0;
1981 /* Get the zones bitmap blocks */
1982 while (chunk_block < zmd->dev->zone_nr_blocks) {
1983 from_mblk = dmz_get_bitmap(zmd, from_zone, chunk_block);
1984 if (IS_ERR(from_mblk))
1985 return PTR_ERR(from_mblk);
1986 to_mblk = dmz_get_bitmap(zmd, to_zone, chunk_block);
1987 if (IS_ERR(to_mblk)) {
1988 dmz_release_mblock(zmd, from_mblk);
1989 return PTR_ERR(to_mblk);
1992 memcpy(to_mblk->data, from_mblk->data, DMZ_BLOCK_SIZE);
1993 dmz_dirty_mblock(zmd, to_mblk);
1995 dmz_release_mblock(zmd, to_mblk);
1996 dmz_release_mblock(zmd, from_mblk);
1998 chunk_block += zmd->zone_bits_per_mblk;
2001 to_zone->weight = from_zone->weight;
2007 * Merge the valid blocks bitmap of from_zone into the bitmap of to_zone,
2008 * starting from chunk_block.
2010 int dmz_merge_valid_blocks(struct dmz_metadata *zmd, struct dm_zone *from_zone,
2011 struct dm_zone *to_zone, sector_t chunk_block)
2013 unsigned int nr_blocks;
2016 /* Get the zones bitmap blocks */
2017 while (chunk_block < zmd->dev->zone_nr_blocks) {
2018 /* Get a valid region from the source zone */
2019 ret = dmz_first_valid_block(zmd, from_zone, &chunk_block);
2024 ret = dmz_validate_blocks(zmd, to_zone, chunk_block, nr_blocks);
2028 chunk_block += nr_blocks;
2035 * Validate all the blocks in the range [block..block+nr_blocks-1].
2037 int dmz_validate_blocks(struct dmz_metadata *zmd, struct dm_zone *zone,
2038 sector_t chunk_block, unsigned int nr_blocks)
2040 unsigned int count, bit, nr_bits;
2041 unsigned int zone_nr_blocks = zmd->dev->zone_nr_blocks;
2042 struct dmz_mblock *mblk;
2045 dmz_dev_debug(zmd->dev, "=> VALIDATE zone %u, block %llu, %u blocks",
2046 dmz_id(zmd, zone), (unsigned long long)chunk_block,
2049 WARN_ON(chunk_block + nr_blocks > zone_nr_blocks);
2052 /* Get bitmap block */
2053 mblk = dmz_get_bitmap(zmd, zone, chunk_block);
2055 return PTR_ERR(mblk);
2058 bit = chunk_block & DMZ_BLOCK_MASK_BITS;
2059 nr_bits = min(nr_blocks, zmd->zone_bits_per_mblk - bit);
2061 count = dmz_set_bits((unsigned long *)mblk->data, bit, nr_bits);
2063 dmz_dirty_mblock(zmd, mblk);
2066 dmz_release_mblock(zmd, mblk);
2068 nr_blocks -= nr_bits;
2069 chunk_block += nr_bits;
2072 if (likely(zone->weight + n <= zone_nr_blocks))
2075 dmz_dev_warn(zmd->dev, "Zone %u: weight %u should be <= %u",
2076 dmz_id(zmd, zone), zone->weight,
2077 zone_nr_blocks - n);
2078 zone->weight = zone_nr_blocks;
2085 * Clear nr_bits bits in bitmap starting from bit.
2086 * Return the number of bits cleared.
2088 static int dmz_clear_bits(unsigned long *bitmap, int bit, int nr_bits)
2090 unsigned long *addr;
2091 int end = bit + nr_bits;
2095 if (((bit & (BITS_PER_LONG - 1)) == 0) &&
2096 ((end - bit) >= BITS_PER_LONG)) {
2097 /* Try to clear whole word at once */
2098 addr = bitmap + BIT_WORD(bit);
2099 if (*addr == ULONG_MAX) {
2102 bit += BITS_PER_LONG;
2107 if (test_and_clear_bit(bit, bitmap))
2116 * Invalidate all the blocks in the range [block..block+nr_blocks-1].
2118 int dmz_invalidate_blocks(struct dmz_metadata *zmd, struct dm_zone *zone,
2119 sector_t chunk_block, unsigned int nr_blocks)
2121 unsigned int count, bit, nr_bits;
2122 struct dmz_mblock *mblk;
2125 dmz_dev_debug(zmd->dev, "=> INVALIDATE zone %u, block %llu, %u blocks",
2126 dmz_id(zmd, zone), (u64)chunk_block, nr_blocks);
2128 WARN_ON(chunk_block + nr_blocks > zmd->dev->zone_nr_blocks);
2131 /* Get bitmap block */
2132 mblk = dmz_get_bitmap(zmd, zone, chunk_block);
2134 return PTR_ERR(mblk);
2137 bit = chunk_block & DMZ_BLOCK_MASK_BITS;
2138 nr_bits = min(nr_blocks, zmd->zone_bits_per_mblk - bit);
2140 count = dmz_clear_bits((unsigned long *)mblk->data,
2143 dmz_dirty_mblock(zmd, mblk);
2146 dmz_release_mblock(zmd, mblk);
2148 nr_blocks -= nr_bits;
2149 chunk_block += nr_bits;
2152 if (zone->weight >= n)
2155 dmz_dev_warn(zmd->dev, "Zone %u: weight %u should be >= %u",
2156 dmz_id(zmd, zone), zone->weight, n);
2164 * Get a block bit value.
2166 static int dmz_test_block(struct dmz_metadata *zmd, struct dm_zone *zone,
2167 sector_t chunk_block)
2169 struct dmz_mblock *mblk;
2172 WARN_ON(chunk_block >= zmd->dev->zone_nr_blocks);
2174 /* Get bitmap block */
2175 mblk = dmz_get_bitmap(zmd, zone, chunk_block);
2177 return PTR_ERR(mblk);
2180 ret = test_bit(chunk_block & DMZ_BLOCK_MASK_BITS,
2181 (unsigned long *) mblk->data) != 0;
2183 dmz_release_mblock(zmd, mblk);
2189 * Return the number of blocks from chunk_block to the first block with a bit
2190 * value specified by set. Search at most nr_blocks blocks from chunk_block.
2192 static int dmz_to_next_set_block(struct dmz_metadata *zmd, struct dm_zone *zone,
2193 sector_t chunk_block, unsigned int nr_blocks,
2196 struct dmz_mblock *mblk;
2197 unsigned int bit, set_bit, nr_bits;
2198 unsigned int zone_bits = zmd->zone_bits_per_mblk;
2199 unsigned long *bitmap;
2202 WARN_ON(chunk_block + nr_blocks > zmd->dev->zone_nr_blocks);
2205 /* Get bitmap block */
2206 mblk = dmz_get_bitmap(zmd, zone, chunk_block);
2208 return PTR_ERR(mblk);
2211 bitmap = (unsigned long *) mblk->data;
2212 bit = chunk_block & DMZ_BLOCK_MASK_BITS;
2213 nr_bits = min(nr_blocks, zone_bits - bit);
2215 set_bit = find_next_bit(bitmap, zone_bits, bit);
2217 set_bit = find_next_zero_bit(bitmap, zone_bits, bit);
2218 dmz_release_mblock(zmd, mblk);
2221 if (set_bit < zone_bits)
2224 nr_blocks -= nr_bits;
2225 chunk_block += nr_bits;
2232 * Test if chunk_block is valid. If it is, the number of consecutive
2233 * valid blocks from chunk_block will be returned.
2235 int dmz_block_valid(struct dmz_metadata *zmd, struct dm_zone *zone,
2236 sector_t chunk_block)
2240 valid = dmz_test_block(zmd, zone, chunk_block);
2244 /* The block is valid: get the number of valid blocks from block */
2245 return dmz_to_next_set_block(zmd, zone, chunk_block,
2246 zmd->dev->zone_nr_blocks - chunk_block, 0);
2250 * Find the first valid block from @chunk_block in @zone.
2251 * If such a block is found, its number is returned using
2252 * @chunk_block and the total number of valid blocks from @chunk_block
2255 int dmz_first_valid_block(struct dmz_metadata *zmd, struct dm_zone *zone,
2256 sector_t *chunk_block)
2258 sector_t start_block = *chunk_block;
2261 ret = dmz_to_next_set_block(zmd, zone, start_block,
2262 zmd->dev->zone_nr_blocks - start_block, 1);
2267 *chunk_block = start_block;
2269 return dmz_to_next_set_block(zmd, zone, start_block,
2270 zmd->dev->zone_nr_blocks - start_block, 0);
2274 * Count the number of bits set starting from bit up to bit + nr_bits - 1.
2276 static int dmz_count_bits(void *bitmap, int bit, int nr_bits)
2278 unsigned long *addr;
2279 int end = bit + nr_bits;
2283 if (((bit & (BITS_PER_LONG - 1)) == 0) &&
2284 ((end - bit) >= BITS_PER_LONG)) {
2285 addr = (unsigned long *)bitmap + BIT_WORD(bit);
2286 if (*addr == ULONG_MAX) {
2288 bit += BITS_PER_LONG;
2293 if (test_bit(bit, bitmap))
2302 * Get a zone weight.
2304 static void dmz_get_zone_weight(struct dmz_metadata *zmd, struct dm_zone *zone)
2306 struct dmz_mblock *mblk;
2307 sector_t chunk_block = 0;
2308 unsigned int bit, nr_bits;
2309 unsigned int nr_blocks = zmd->dev->zone_nr_blocks;
2314 /* Get bitmap block */
2315 mblk = dmz_get_bitmap(zmd, zone, chunk_block);
2321 /* Count bits in this block */
2322 bitmap = mblk->data;
2323 bit = chunk_block & DMZ_BLOCK_MASK_BITS;
2324 nr_bits = min(nr_blocks, zmd->zone_bits_per_mblk - bit);
2325 n += dmz_count_bits(bitmap, bit, nr_bits);
2327 dmz_release_mblock(zmd, mblk);
2329 nr_blocks -= nr_bits;
2330 chunk_block += nr_bits;
2337 * Cleanup the zoned metadata resources.
2339 static void dmz_cleanup_metadata(struct dmz_metadata *zmd)
2341 struct rb_root *root;
2342 struct dmz_mblock *mblk, *next;
2345 /* Release zone mapping resources */
2346 if (zmd->map_mblk) {
2347 for (i = 0; i < zmd->nr_map_blocks; i++)
2348 dmz_release_mblock(zmd, zmd->map_mblk[i]);
2349 kfree(zmd->map_mblk);
2350 zmd->map_mblk = NULL;
2353 /* Release super blocks */
2354 for (i = 0; i < 2; i++) {
2355 if (zmd->sb[i].mblk) {
2356 dmz_free_mblock(zmd, zmd->sb[i].mblk);
2357 zmd->sb[i].mblk = NULL;
2361 /* Free cached blocks */
2362 while (!list_empty(&zmd->mblk_dirty_list)) {
2363 mblk = list_first_entry(&zmd->mblk_dirty_list,
2364 struct dmz_mblock, link);
2365 dmz_dev_warn(zmd->dev, "mblock %llu still in dirty list (ref %u)",
2366 (u64)mblk->no, mblk->ref);
2367 list_del_init(&mblk->link);
2368 rb_erase(&mblk->node, &zmd->mblk_rbtree);
2369 dmz_free_mblock(zmd, mblk);
2372 while (!list_empty(&zmd->mblk_lru_list)) {
2373 mblk = list_first_entry(&zmd->mblk_lru_list,
2374 struct dmz_mblock, link);
2375 list_del_init(&mblk->link);
2376 rb_erase(&mblk->node, &zmd->mblk_rbtree);
2377 dmz_free_mblock(zmd, mblk);
2380 /* Sanity checks: the mblock rbtree should now be empty */
2381 root = &zmd->mblk_rbtree;
2382 rbtree_postorder_for_each_entry_safe(mblk, next, root, node) {
2383 dmz_dev_warn(zmd->dev, "mblock %llu ref %u still in rbtree",
2384 (u64)mblk->no, mblk->ref);
2386 dmz_free_mblock(zmd, mblk);
2389 /* Free the zone descriptors */
2390 dmz_drop_zones(zmd);
2392 mutex_destroy(&zmd->mblk_flush_lock);
2393 mutex_destroy(&zmd->map_lock);
2397 * Initialize the zoned metadata.
2399 int dmz_ctr_metadata(struct dmz_dev *dev, struct dmz_metadata **metadata)
2401 struct dmz_metadata *zmd;
2402 unsigned int i, zid;
2403 struct dm_zone *zone;
2406 zmd = kzalloc(sizeof(struct dmz_metadata), GFP_KERNEL);
2411 zmd->mblk_rbtree = RB_ROOT;
2412 init_rwsem(&zmd->mblk_sem);
2413 mutex_init(&zmd->mblk_flush_lock);
2414 spin_lock_init(&zmd->mblk_lock);
2415 INIT_LIST_HEAD(&zmd->mblk_lru_list);
2416 INIT_LIST_HEAD(&zmd->mblk_dirty_list);
2418 mutex_init(&zmd->map_lock);
2419 atomic_set(&zmd->unmap_nr_rnd, 0);
2420 INIT_LIST_HEAD(&zmd->unmap_rnd_list);
2421 INIT_LIST_HEAD(&zmd->map_rnd_list);
2423 atomic_set(&zmd->unmap_nr_seq, 0);
2424 INIT_LIST_HEAD(&zmd->unmap_seq_list);
2425 INIT_LIST_HEAD(&zmd->map_seq_list);
2427 atomic_set(&zmd->nr_reserved_seq_zones, 0);
2428 INIT_LIST_HEAD(&zmd->reserved_seq_zones_list);
2430 init_waitqueue_head(&zmd->free_wq);
2432 /* Initialize zone descriptors */
2433 ret = dmz_init_zones(zmd);
2437 /* Get super block */
2438 ret = dmz_load_sb(zmd);
2442 /* Set metadata zones starting from sb_zone */
2443 zid = dmz_id(zmd, zmd->sb_zone);
2444 for (i = 0; i < zmd->nr_meta_zones << 1; i++) {
2445 zone = dmz_get(zmd, zid + i);
2446 if (!dmz_is_rnd(zone))
2448 set_bit(DMZ_META, &zone->flags);
2451 /* Load mapping table */
2452 ret = dmz_load_mapping(zmd);
2457 * Cache size boundaries: allow at least 2 super blocks, the chunk map
2458 * blocks and enough blocks to be able to cache the bitmap blocks of
2459 * up to 16 zones when idle (min_nr_mblks). Otherwise, if busy, allow
2460 * the cache to add 512 more metadata blocks.
2462 zmd->min_nr_mblks = 2 + zmd->nr_map_blocks + zmd->zone_nr_bitmap_blocks * 16;
2463 zmd->max_nr_mblks = zmd->min_nr_mblks + 512;
2464 zmd->mblk_shrinker.count_objects = dmz_mblock_shrinker_count;
2465 zmd->mblk_shrinker.scan_objects = dmz_mblock_shrinker_scan;
2466 zmd->mblk_shrinker.seeks = DEFAULT_SEEKS;
2468 /* Metadata cache shrinker */
2469 ret = register_shrinker(&zmd->mblk_shrinker);
2471 dmz_dev_err(dev, "Register metadata cache shrinker failed");
2475 dmz_dev_info(dev, "Host-%s zoned block device",
2476 bdev_zoned_model(dev->bdev) == BLK_ZONED_HA ?
2477 "aware" : "managed");
2478 dmz_dev_info(dev, " %llu 512-byte logical sectors",
2479 (u64)dev->capacity);
2480 dmz_dev_info(dev, " %u zones of %llu 512-byte logical sectors",
2481 dev->nr_zones, (u64)dev->zone_nr_sectors);
2482 dmz_dev_info(dev, " %u metadata zones",
2483 zmd->nr_meta_zones * 2);
2484 dmz_dev_info(dev, " %u data zones for %u chunks",
2485 zmd->nr_data_zones, zmd->nr_chunks);
2486 dmz_dev_info(dev, " %u random zones (%u unmapped)",
2487 zmd->nr_rnd, atomic_read(&zmd->unmap_nr_rnd));
2488 dmz_dev_info(dev, " %u sequential zones (%u unmapped)",
2489 zmd->nr_seq, atomic_read(&zmd->unmap_nr_seq));
2490 dmz_dev_info(dev, " %u reserved sequential data zones",
2491 zmd->nr_reserved_seq);
2493 dmz_dev_debug(dev, "Format:");
2494 dmz_dev_debug(dev, "%u metadata blocks per set (%u max cache)",
2495 zmd->nr_meta_blocks, zmd->max_nr_mblks);
2496 dmz_dev_debug(dev, " %u data zone mapping blocks",
2497 zmd->nr_map_blocks);
2498 dmz_dev_debug(dev, " %u bitmap blocks",
2499 zmd->nr_bitmap_blocks);
2505 dmz_cleanup_metadata(zmd);
2513 * Cleanup the zoned metadata resources.
2515 void dmz_dtr_metadata(struct dmz_metadata *zmd)
2517 unregister_shrinker(&zmd->mblk_shrinker);
2518 dmz_cleanup_metadata(zmd);
2523 * Check zone information on resume.
2525 int dmz_resume_metadata(struct dmz_metadata *zmd)
2527 struct dmz_dev *dev = zmd->dev;
2528 struct dm_zone *zone;
2534 for (i = 0; i < dev->nr_zones; i++) {
2535 zone = dmz_get(zmd, i);
2537 dmz_dev_err(dev, "Unable to get zone %u", i);
2541 wp_block = zone->wp_block;
2543 ret = dmz_update_zone(zmd, zone);
2545 dmz_dev_err(dev, "Broken zone %u", i);
2549 if (dmz_is_offline(zone)) {
2550 dmz_dev_warn(dev, "Zone %u is offline", i);
2554 /* Check write pointer */
2555 if (!dmz_is_seq(zone))
2557 else if (zone->wp_block != wp_block) {
2558 dmz_dev_err(dev, "Zone %u: Invalid wp (%llu / %llu)",
2559 i, (u64)zone->wp_block, (u64)wp_block);
2560 zone->wp_block = wp_block;
2561 dmz_invalidate_blocks(zmd, zone, zone->wp_block,
2562 dev->zone_nr_blocks - zone->wp_block);