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;
138 unsigned int nr_bitmap_blocks;
139 unsigned int nr_map_blocks;
141 unsigned int nr_useable_zones;
142 unsigned int nr_meta_blocks;
143 unsigned int nr_meta_zones;
144 unsigned int nr_data_zones;
145 unsigned int nr_rnd_zones;
146 unsigned int nr_reserved_seq;
147 unsigned int nr_chunks;
149 /* Zone information array */
150 struct dm_zone *zones;
152 struct dm_zone *sb_zone;
154 unsigned int mblk_primary;
156 unsigned int min_nr_mblks;
157 unsigned int max_nr_mblks;
159 struct rw_semaphore mblk_sem;
160 struct mutex mblk_flush_lock;
161 spinlock_t mblk_lock;
162 struct rb_root mblk_rbtree;
163 struct list_head mblk_lru_list;
164 struct list_head mblk_dirty_list;
165 struct shrinker mblk_shrinker;
167 /* Zone allocation management */
168 struct mutex map_lock;
169 struct dmz_mblock **map_mblk;
171 atomic_t unmap_nr_rnd;
172 struct list_head unmap_rnd_list;
173 struct list_head map_rnd_list;
176 atomic_t unmap_nr_seq;
177 struct list_head unmap_seq_list;
178 struct list_head map_seq_list;
180 atomic_t nr_reserved_seq_zones;
181 struct list_head reserved_seq_zones_list;
183 wait_queue_head_t free_wq;
189 unsigned int dmz_id(struct dmz_metadata *zmd, struct dm_zone *zone)
191 return ((unsigned int)(zone - zmd->zones));
194 sector_t dmz_start_sect(struct dmz_metadata *zmd, struct dm_zone *zone)
196 return (sector_t)dmz_id(zmd, zone) << zmd->dev->zone_nr_sectors_shift;
199 sector_t dmz_start_block(struct dmz_metadata *zmd, struct dm_zone *zone)
201 return (sector_t)dmz_id(zmd, zone) << zmd->dev->zone_nr_blocks_shift;
204 unsigned int dmz_nr_chunks(struct dmz_metadata *zmd)
206 return zmd->nr_chunks;
209 unsigned int dmz_nr_rnd_zones(struct dmz_metadata *zmd)
214 unsigned int dmz_nr_unmap_rnd_zones(struct dmz_metadata *zmd)
216 return atomic_read(&zmd->unmap_nr_rnd);
220 * Lock/unlock mapping table.
221 * The map lock also protects all the zone lists.
223 void dmz_lock_map(struct dmz_metadata *zmd)
225 mutex_lock(&zmd->map_lock);
228 void dmz_unlock_map(struct dmz_metadata *zmd)
230 mutex_unlock(&zmd->map_lock);
234 * Lock/unlock metadata access. This is a "read" lock on a semaphore
235 * that prevents metadata flush from running while metadata are being
236 * modified. The actual metadata write mutual exclusion is achieved with
237 * the map lock and zone state management (active and reclaim state are
238 * mutually exclusive).
240 void dmz_lock_metadata(struct dmz_metadata *zmd)
242 down_read(&zmd->mblk_sem);
245 void dmz_unlock_metadata(struct dmz_metadata *zmd)
247 up_read(&zmd->mblk_sem);
251 * Lock/unlock flush: prevent concurrent executions
252 * of dmz_flush_metadata as well as metadata modification in reclaim
253 * while flush is being executed.
255 void dmz_lock_flush(struct dmz_metadata *zmd)
257 mutex_lock(&zmd->mblk_flush_lock);
260 void dmz_unlock_flush(struct dmz_metadata *zmd)
262 mutex_unlock(&zmd->mblk_flush_lock);
266 * Allocate a metadata block.
268 static struct dmz_mblock *dmz_alloc_mblock(struct dmz_metadata *zmd,
271 struct dmz_mblock *mblk = NULL;
273 /* See if we can reuse cached blocks */
274 if (zmd->max_nr_mblks && atomic_read(&zmd->nr_mblks) > zmd->max_nr_mblks) {
275 spin_lock(&zmd->mblk_lock);
276 mblk = list_first_entry_or_null(&zmd->mblk_lru_list,
277 struct dmz_mblock, link);
279 list_del_init(&mblk->link);
280 rb_erase(&mblk->node, &zmd->mblk_rbtree);
283 spin_unlock(&zmd->mblk_lock);
288 /* Allocate a new block */
289 mblk = kmalloc(sizeof(struct dmz_mblock), GFP_NOIO);
293 mblk->page = alloc_page(GFP_NOIO);
299 RB_CLEAR_NODE(&mblk->node);
300 INIT_LIST_HEAD(&mblk->link);
304 mblk->data = page_address(mblk->page);
306 atomic_inc(&zmd->nr_mblks);
312 * Free a metadata block.
314 static void dmz_free_mblock(struct dmz_metadata *zmd, struct dmz_mblock *mblk)
316 __free_pages(mblk->page, 0);
319 atomic_dec(&zmd->nr_mblks);
323 * Insert a metadata block in the rbtree.
325 static void dmz_insert_mblock(struct dmz_metadata *zmd, struct dmz_mblock *mblk)
327 struct rb_root *root = &zmd->mblk_rbtree;
328 struct rb_node **new = &(root->rb_node), *parent = NULL;
329 struct dmz_mblock *b;
331 /* Figure out where to put the new node */
333 b = container_of(*new, struct dmz_mblock, node);
335 new = (b->no < mblk->no) ? &((*new)->rb_left) : &((*new)->rb_right);
338 /* Add new node and rebalance tree */
339 rb_link_node(&mblk->node, parent, new);
340 rb_insert_color(&mblk->node, root);
344 * Lookup a metadata block in the rbtree. If the block is found, increment
345 * its reference count.
347 static struct dmz_mblock *dmz_get_mblock_fast(struct dmz_metadata *zmd,
350 struct rb_root *root = &zmd->mblk_rbtree;
351 struct rb_node *node = root->rb_node;
352 struct dmz_mblock *mblk;
355 mblk = container_of(node, struct dmz_mblock, node);
356 if (mblk->no == mblk_no) {
358 * If this is the first reference to the block,
359 * remove it from the LRU list.
362 if (mblk->ref == 1 &&
363 !test_bit(DMZ_META_DIRTY, &mblk->state))
364 list_del_init(&mblk->link);
367 node = (mblk->no < mblk_no) ? node->rb_left : node->rb_right;
374 * Metadata block BIO end callback.
376 static void dmz_mblock_bio_end_io(struct bio *bio)
378 struct dmz_mblock *mblk = bio->bi_private;
382 set_bit(DMZ_META_ERROR, &mblk->state);
384 if (bio_op(bio) == REQ_OP_WRITE)
385 flag = DMZ_META_WRITING;
387 flag = DMZ_META_READING;
389 clear_bit_unlock(flag, &mblk->state);
390 smp_mb__after_atomic();
391 wake_up_bit(&mblk->state, flag);
397 * Read an uncached metadata block from disk and add it to the cache.
399 static struct dmz_mblock *dmz_get_mblock_slow(struct dmz_metadata *zmd,
402 struct dmz_mblock *mblk, *m;
403 sector_t block = zmd->sb[zmd->mblk_primary].block + mblk_no;
406 if (dmz_bdev_is_dying(zmd->dev))
407 return ERR_PTR(-EIO);
409 /* Get a new block and a BIO to read it */
410 mblk = dmz_alloc_mblock(zmd, mblk_no);
412 return ERR_PTR(-ENOMEM);
414 bio = bio_alloc(GFP_NOIO, 1);
416 dmz_free_mblock(zmd, mblk);
417 return ERR_PTR(-ENOMEM);
420 spin_lock(&zmd->mblk_lock);
423 * Make sure that another context did not start reading
426 m = dmz_get_mblock_fast(zmd, mblk_no);
428 spin_unlock(&zmd->mblk_lock);
429 dmz_free_mblock(zmd, mblk);
435 set_bit(DMZ_META_READING, &mblk->state);
436 dmz_insert_mblock(zmd, mblk);
438 spin_unlock(&zmd->mblk_lock);
440 /* Submit read BIO */
441 bio->bi_iter.bi_sector = dmz_blk2sect(block);
442 bio_set_dev(bio, zmd->dev->bdev);
443 bio->bi_private = mblk;
444 bio->bi_end_io = dmz_mblock_bio_end_io;
445 bio_set_op_attrs(bio, REQ_OP_READ, REQ_META | REQ_PRIO);
446 bio_add_page(bio, mblk->page, DMZ_BLOCK_SIZE, 0);
453 * Free metadata blocks.
455 static unsigned long dmz_shrink_mblock_cache(struct dmz_metadata *zmd,
458 struct dmz_mblock *mblk;
459 unsigned long count = 0;
461 if (!zmd->max_nr_mblks)
464 while (!list_empty(&zmd->mblk_lru_list) &&
465 atomic_read(&zmd->nr_mblks) > zmd->min_nr_mblks &&
467 mblk = list_first_entry(&zmd->mblk_lru_list,
468 struct dmz_mblock, link);
469 list_del_init(&mblk->link);
470 rb_erase(&mblk->node, &zmd->mblk_rbtree);
471 dmz_free_mblock(zmd, mblk);
479 * For mblock shrinker: get the number of unused metadata blocks in the cache.
481 static unsigned long dmz_mblock_shrinker_count(struct shrinker *shrink,
482 struct shrink_control *sc)
484 struct dmz_metadata *zmd = container_of(shrink, struct dmz_metadata, mblk_shrinker);
486 return atomic_read(&zmd->nr_mblks);
490 * For mblock shrinker: scan unused metadata blocks and shrink the cache.
492 static unsigned long dmz_mblock_shrinker_scan(struct shrinker *shrink,
493 struct shrink_control *sc)
495 struct dmz_metadata *zmd = container_of(shrink, struct dmz_metadata, mblk_shrinker);
498 spin_lock(&zmd->mblk_lock);
499 count = dmz_shrink_mblock_cache(zmd, sc->nr_to_scan);
500 spin_unlock(&zmd->mblk_lock);
502 return count ? count : SHRINK_STOP;
506 * Release a metadata block.
508 static void dmz_release_mblock(struct dmz_metadata *zmd,
509 struct dmz_mblock *mblk)
515 spin_lock(&zmd->mblk_lock);
518 if (mblk->ref == 0) {
519 if (test_bit(DMZ_META_ERROR, &mblk->state)) {
520 rb_erase(&mblk->node, &zmd->mblk_rbtree);
521 dmz_free_mblock(zmd, mblk);
522 } else if (!test_bit(DMZ_META_DIRTY, &mblk->state)) {
523 list_add_tail(&mblk->link, &zmd->mblk_lru_list);
524 dmz_shrink_mblock_cache(zmd, 1);
528 spin_unlock(&zmd->mblk_lock);
532 * Get a metadata block from the rbtree. If the block
533 * is not present, read it from disk.
535 static struct dmz_mblock *dmz_get_mblock(struct dmz_metadata *zmd,
538 struct dmz_mblock *mblk;
541 spin_lock(&zmd->mblk_lock);
542 mblk = dmz_get_mblock_fast(zmd, mblk_no);
543 spin_unlock(&zmd->mblk_lock);
546 /* Cache miss: read the block from disk */
547 mblk = dmz_get_mblock_slow(zmd, mblk_no);
552 /* Wait for on-going read I/O and check for error */
553 wait_on_bit_io(&mblk->state, DMZ_META_READING,
554 TASK_UNINTERRUPTIBLE);
555 if (test_bit(DMZ_META_ERROR, &mblk->state)) {
556 dmz_release_mblock(zmd, mblk);
557 dmz_check_bdev(zmd->dev);
558 return ERR_PTR(-EIO);
565 * Mark a metadata block dirty.
567 static void dmz_dirty_mblock(struct dmz_metadata *zmd, struct dmz_mblock *mblk)
569 spin_lock(&zmd->mblk_lock);
570 if (!test_and_set_bit(DMZ_META_DIRTY, &mblk->state))
571 list_add_tail(&mblk->link, &zmd->mblk_dirty_list);
572 spin_unlock(&zmd->mblk_lock);
576 * Issue a metadata block write BIO.
578 static int dmz_write_mblock(struct dmz_metadata *zmd, struct dmz_mblock *mblk,
581 sector_t block = zmd->sb[set].block + mblk->no;
584 if (dmz_bdev_is_dying(zmd->dev))
587 bio = bio_alloc(GFP_NOIO, 1);
589 set_bit(DMZ_META_ERROR, &mblk->state);
593 set_bit(DMZ_META_WRITING, &mblk->state);
595 bio->bi_iter.bi_sector = dmz_blk2sect(block);
596 bio_set_dev(bio, zmd->dev->bdev);
597 bio->bi_private = mblk;
598 bio->bi_end_io = dmz_mblock_bio_end_io;
599 bio_set_op_attrs(bio, REQ_OP_WRITE, REQ_META | REQ_PRIO);
600 bio_add_page(bio, mblk->page, DMZ_BLOCK_SIZE, 0);
607 * Read/write a metadata block.
609 static int dmz_rdwr_block(struct dmz_metadata *zmd, int op, sector_t block,
615 if (dmz_bdev_is_dying(zmd->dev))
618 bio = bio_alloc(GFP_NOIO, 1);
622 bio->bi_iter.bi_sector = dmz_blk2sect(block);
623 bio_set_dev(bio, zmd->dev->bdev);
624 bio_set_op_attrs(bio, op, REQ_SYNC | REQ_META | REQ_PRIO);
625 bio_add_page(bio, page, DMZ_BLOCK_SIZE, 0);
626 ret = submit_bio_wait(bio);
630 dmz_check_bdev(zmd->dev);
635 * Write super block of the specified metadata set.
637 static int dmz_write_sb(struct dmz_metadata *zmd, unsigned int set)
639 sector_t block = zmd->sb[set].block;
640 struct dmz_mblock *mblk = zmd->sb[set].mblk;
641 struct dmz_super *sb = zmd->sb[set].sb;
642 u64 sb_gen = zmd->sb_gen + 1;
645 sb->magic = cpu_to_le32(DMZ_MAGIC);
646 sb->version = cpu_to_le32(DMZ_META_VER);
648 sb->gen = cpu_to_le64(sb_gen);
650 sb->sb_block = cpu_to_le64(block);
651 sb->nr_meta_blocks = cpu_to_le32(zmd->nr_meta_blocks);
652 sb->nr_reserved_seq = cpu_to_le32(zmd->nr_reserved_seq);
653 sb->nr_chunks = cpu_to_le32(zmd->nr_chunks);
655 sb->nr_map_blocks = cpu_to_le32(zmd->nr_map_blocks);
656 sb->nr_bitmap_blocks = cpu_to_le32(zmd->nr_bitmap_blocks);
659 sb->crc = cpu_to_le32(crc32_le(sb_gen, (unsigned char *)sb, DMZ_BLOCK_SIZE));
661 ret = dmz_rdwr_block(zmd, REQ_OP_WRITE, block, mblk->page);
663 ret = blkdev_issue_flush(zmd->dev->bdev, GFP_NOIO, NULL);
669 * Write dirty metadata blocks to the specified set.
671 static int dmz_write_dirty_mblocks(struct dmz_metadata *zmd,
672 struct list_head *write_list,
675 struct dmz_mblock *mblk;
676 struct blk_plug plug;
677 int ret = 0, nr_mblks_submitted = 0;
680 blk_start_plug(&plug);
681 list_for_each_entry(mblk, write_list, link) {
682 ret = dmz_write_mblock(zmd, mblk, set);
685 nr_mblks_submitted++;
687 blk_finish_plug(&plug);
689 /* Wait for completion */
690 list_for_each_entry(mblk, write_list, link) {
691 if (!nr_mblks_submitted)
693 wait_on_bit_io(&mblk->state, DMZ_META_WRITING,
694 TASK_UNINTERRUPTIBLE);
695 if (test_bit(DMZ_META_ERROR, &mblk->state)) {
696 clear_bit(DMZ_META_ERROR, &mblk->state);
697 dmz_check_bdev(zmd->dev);
700 nr_mblks_submitted--;
703 /* Flush drive cache (this will also sync data) */
705 ret = blkdev_issue_flush(zmd->dev->bdev, GFP_NOIO, NULL);
711 * Log dirty metadata blocks.
713 static int dmz_log_dirty_mblocks(struct dmz_metadata *zmd,
714 struct list_head *write_list)
716 unsigned int log_set = zmd->mblk_primary ^ 0x1;
719 /* Write dirty blocks to the log */
720 ret = dmz_write_dirty_mblocks(zmd, write_list, log_set);
725 * No error so far: now validate the log by updating the
726 * log index super block generation.
728 ret = dmz_write_sb(zmd, log_set);
736 * Flush dirty metadata blocks.
738 int dmz_flush_metadata(struct dmz_metadata *zmd)
740 struct dmz_mblock *mblk;
741 struct list_head write_list;
747 INIT_LIST_HEAD(&write_list);
750 * Make sure that metadata blocks are stable before logging: take
751 * the write lock on the metadata semaphore to prevent target BIOs
752 * from modifying metadata.
754 down_write(&zmd->mblk_sem);
757 * This is called from the target flush work and reclaim work.
758 * Concurrent execution is not allowed.
762 if (dmz_bdev_is_dying(zmd->dev)) {
767 /* Get dirty blocks */
768 spin_lock(&zmd->mblk_lock);
769 list_splice_init(&zmd->mblk_dirty_list, &write_list);
770 spin_unlock(&zmd->mblk_lock);
772 /* If there are no dirty metadata blocks, just flush the device cache */
773 if (list_empty(&write_list)) {
774 ret = blkdev_issue_flush(zmd->dev->bdev, GFP_NOIO, NULL);
779 * The primary metadata set is still clean. Keep it this way until
780 * all updates are successful in the secondary set. That is, use
781 * the secondary set as a log.
783 ret = dmz_log_dirty_mblocks(zmd, &write_list);
788 * The log is on disk. It is now safe to update in place
789 * in the primary metadata set.
791 ret = dmz_write_dirty_mblocks(zmd, &write_list, zmd->mblk_primary);
795 ret = dmz_write_sb(zmd, zmd->mblk_primary);
799 while (!list_empty(&write_list)) {
800 mblk = list_first_entry(&write_list, struct dmz_mblock, link);
801 list_del_init(&mblk->link);
803 spin_lock(&zmd->mblk_lock);
804 clear_bit(DMZ_META_DIRTY, &mblk->state);
806 list_add_tail(&mblk->link, &zmd->mblk_lru_list);
807 spin_unlock(&zmd->mblk_lock);
812 dmz_unlock_flush(zmd);
813 up_write(&zmd->mblk_sem);
818 if (!list_empty(&write_list)) {
819 spin_lock(&zmd->mblk_lock);
820 list_splice(&write_list, &zmd->mblk_dirty_list);
821 spin_unlock(&zmd->mblk_lock);
823 if (!dmz_check_bdev(zmd->dev))
831 static int dmz_check_sb(struct dmz_metadata *zmd, struct dmz_super *sb)
833 unsigned int nr_meta_zones, nr_data_zones;
834 struct dmz_dev *dev = zmd->dev;
838 gen = le64_to_cpu(sb->gen);
839 stored_crc = le32_to_cpu(sb->crc);
841 crc = crc32_le(gen, (unsigned char *)sb, DMZ_BLOCK_SIZE);
842 if (crc != stored_crc) {
843 dmz_dev_err(dev, "Invalid checksum (needed 0x%08x, got 0x%08x)",
848 if (le32_to_cpu(sb->magic) != DMZ_MAGIC) {
849 dmz_dev_err(dev, "Invalid meta magic (needed 0x%08x, got 0x%08x)",
850 DMZ_MAGIC, le32_to_cpu(sb->magic));
854 if (le32_to_cpu(sb->version) != DMZ_META_VER) {
855 dmz_dev_err(dev, "Invalid meta version (needed %d, got %d)",
856 DMZ_META_VER, le32_to_cpu(sb->version));
860 nr_meta_zones = (le32_to_cpu(sb->nr_meta_blocks) + dev->zone_nr_blocks - 1)
861 >> dev->zone_nr_blocks_shift;
862 if (!nr_meta_zones ||
863 nr_meta_zones >= zmd->nr_rnd_zones) {
864 dmz_dev_err(dev, "Invalid number of metadata blocks");
868 if (!le32_to_cpu(sb->nr_reserved_seq) ||
869 le32_to_cpu(sb->nr_reserved_seq) >= (zmd->nr_useable_zones - nr_meta_zones)) {
870 dmz_dev_err(dev, "Invalid number of reserved sequential zones");
874 nr_data_zones = zmd->nr_useable_zones -
875 (nr_meta_zones * 2 + le32_to_cpu(sb->nr_reserved_seq));
876 if (le32_to_cpu(sb->nr_chunks) > nr_data_zones) {
877 dmz_dev_err(dev, "Invalid number of chunks %u / %u",
878 le32_to_cpu(sb->nr_chunks), nr_data_zones);
883 zmd->nr_meta_blocks = le32_to_cpu(sb->nr_meta_blocks);
884 zmd->nr_reserved_seq = le32_to_cpu(sb->nr_reserved_seq);
885 zmd->nr_chunks = le32_to_cpu(sb->nr_chunks);
886 zmd->nr_map_blocks = le32_to_cpu(sb->nr_map_blocks);
887 zmd->nr_bitmap_blocks = le32_to_cpu(sb->nr_bitmap_blocks);
888 zmd->nr_meta_zones = nr_meta_zones;
889 zmd->nr_data_zones = nr_data_zones;
895 * Read the first or second super block from disk.
897 static int dmz_read_sb(struct dmz_metadata *zmd, unsigned int set)
899 return dmz_rdwr_block(zmd, REQ_OP_READ, zmd->sb[set].block,
900 zmd->sb[set].mblk->page);
904 * Determine the position of the secondary super blocks on disk.
905 * This is used only if a corruption of the primary super block
908 static int dmz_lookup_secondary_sb(struct dmz_metadata *zmd)
910 unsigned int zone_nr_blocks = zmd->dev->zone_nr_blocks;
911 struct dmz_mblock *mblk;
914 /* Allocate a block */
915 mblk = dmz_alloc_mblock(zmd, 0);
919 zmd->sb[1].mblk = mblk;
920 zmd->sb[1].sb = mblk->data;
922 /* Bad first super block: search for the second one */
923 zmd->sb[1].block = zmd->sb[0].block + zone_nr_blocks;
924 for (i = 0; i < zmd->nr_rnd_zones - 1; i++) {
925 if (dmz_read_sb(zmd, 1) != 0)
927 if (le32_to_cpu(zmd->sb[1].sb->magic) == DMZ_MAGIC)
929 zmd->sb[1].block += zone_nr_blocks;
932 dmz_free_mblock(zmd, mblk);
933 zmd->sb[1].mblk = NULL;
939 * Read the first or second super block from disk.
941 static int dmz_get_sb(struct dmz_metadata *zmd, unsigned int set)
943 struct dmz_mblock *mblk;
946 /* Allocate a block */
947 mblk = dmz_alloc_mblock(zmd, 0);
951 zmd->sb[set].mblk = mblk;
952 zmd->sb[set].sb = mblk->data;
954 /* Read super block */
955 ret = dmz_read_sb(zmd, set);
957 dmz_free_mblock(zmd, mblk);
958 zmd->sb[set].mblk = NULL;
966 * Recover a metadata set.
968 static int dmz_recover_mblocks(struct dmz_metadata *zmd, unsigned int dst_set)
970 unsigned int src_set = dst_set ^ 0x1;
974 dmz_dev_warn(zmd->dev, "Metadata set %u invalid: recovering", dst_set);
977 zmd->sb[0].block = dmz_start_block(zmd, zmd->sb_zone);
979 zmd->sb[1].block = zmd->sb[0].block +
980 (zmd->nr_meta_zones << zmd->dev->zone_nr_blocks_shift);
983 page = alloc_page(GFP_NOIO);
987 /* Copy metadata blocks */
988 for (i = 1; i < zmd->nr_meta_blocks; i++) {
989 ret = dmz_rdwr_block(zmd, REQ_OP_READ,
990 zmd->sb[src_set].block + i, page);
993 ret = dmz_rdwr_block(zmd, REQ_OP_WRITE,
994 zmd->sb[dst_set].block + i, page);
999 /* Finalize with the super block */
1000 if (!zmd->sb[dst_set].mblk) {
1001 zmd->sb[dst_set].mblk = dmz_alloc_mblock(zmd, 0);
1002 if (!zmd->sb[dst_set].mblk) {
1006 zmd->sb[dst_set].sb = zmd->sb[dst_set].mblk->data;
1009 ret = dmz_write_sb(zmd, dst_set);
1011 __free_pages(page, 0);
1017 * Get super block from disk.
1019 static int dmz_load_sb(struct dmz_metadata *zmd)
1021 bool sb_good[2] = {false, false};
1022 u64 sb_gen[2] = {0, 0};
1025 /* Read and check the primary super block */
1026 zmd->sb[0].block = dmz_start_block(zmd, zmd->sb_zone);
1027 ret = dmz_get_sb(zmd, 0);
1029 dmz_dev_err(zmd->dev, "Read primary super block failed");
1033 ret = dmz_check_sb(zmd, zmd->sb[0].sb);
1035 /* Read and check secondary super block */
1038 zmd->sb[1].block = zmd->sb[0].block +
1039 (zmd->nr_meta_zones << zmd->dev->zone_nr_blocks_shift);
1040 ret = dmz_get_sb(zmd, 1);
1042 ret = dmz_lookup_secondary_sb(zmd);
1045 dmz_dev_err(zmd->dev, "Read secondary super block failed");
1049 ret = dmz_check_sb(zmd, zmd->sb[1].sb);
1053 /* Use highest generation sb first */
1054 if (!sb_good[0] && !sb_good[1]) {
1055 dmz_dev_err(zmd->dev, "No valid super block found");
1060 sb_gen[0] = le64_to_cpu(zmd->sb[0].sb->gen);
1062 ret = dmz_recover_mblocks(zmd, 0);
1065 sb_gen[1] = le64_to_cpu(zmd->sb[1].sb->gen);
1067 ret = dmz_recover_mblocks(zmd, 1);
1070 dmz_dev_err(zmd->dev, "Recovery failed");
1074 if (sb_gen[0] >= sb_gen[1]) {
1075 zmd->sb_gen = sb_gen[0];
1076 zmd->mblk_primary = 0;
1078 zmd->sb_gen = sb_gen[1];
1079 zmd->mblk_primary = 1;
1082 dmz_dev_debug(zmd->dev, "Using super block %u (gen %llu)",
1083 zmd->mblk_primary, zmd->sb_gen);
1089 * Initialize a zone descriptor.
1091 static int dmz_init_zone(struct dmz_metadata *zmd, struct dm_zone *zone,
1092 struct blk_zone *blkz)
1094 struct dmz_dev *dev = zmd->dev;
1096 /* Ignore the eventual last runt (smaller) zone */
1097 if (blkz->len != dev->zone_nr_sectors) {
1098 if (blkz->start + blkz->len == dev->capacity)
1103 INIT_LIST_HEAD(&zone->link);
1104 atomic_set(&zone->refcount, 0);
1105 zone->chunk = DMZ_MAP_UNMAPPED;
1107 if (blkz->type == BLK_ZONE_TYPE_CONVENTIONAL) {
1108 set_bit(DMZ_RND, &zone->flags);
1109 zmd->nr_rnd_zones++;
1110 } else if (blkz->type == BLK_ZONE_TYPE_SEQWRITE_REQ ||
1111 blkz->type == BLK_ZONE_TYPE_SEQWRITE_PREF) {
1112 set_bit(DMZ_SEQ, &zone->flags);
1116 if (blkz->cond == BLK_ZONE_COND_OFFLINE)
1117 set_bit(DMZ_OFFLINE, &zone->flags);
1118 else if (blkz->cond == BLK_ZONE_COND_READONLY)
1119 set_bit(DMZ_READ_ONLY, &zone->flags);
1121 if (dmz_is_rnd(zone))
1124 zone->wp_block = dmz_sect2blk(blkz->wp - blkz->start);
1126 if (!dmz_is_offline(zone) && !dmz_is_readonly(zone)) {
1127 zmd->nr_useable_zones++;
1128 if (dmz_is_rnd(zone)) {
1129 zmd->nr_rnd_zones++;
1130 if (!zmd->sb_zone) {
1131 /* Super block zone */
1132 zmd->sb_zone = zone;
1141 * Free zones descriptors.
1143 static void dmz_drop_zones(struct dmz_metadata *zmd)
1150 * The size of a zone report in number of zones.
1151 * This results in 4096*64B=256KB report zones commands.
1153 #define DMZ_REPORT_NR_ZONES 4096
1156 * Allocate and initialize zone descriptors using the zone
1157 * information from disk.
1159 static int dmz_init_zones(struct dmz_metadata *zmd)
1161 struct dmz_dev *dev = zmd->dev;
1162 struct dm_zone *zone;
1163 struct blk_zone *blkz;
1164 unsigned int nr_blkz;
1165 sector_t sector = 0;
1169 zmd->zone_bitmap_size = dev->zone_nr_blocks >> 3;
1170 zmd->zone_nr_bitmap_blocks = zmd->zone_bitmap_size >> DMZ_BLOCK_SHIFT;
1172 /* Allocate zone array */
1173 zmd->zones = kcalloc(dev->nr_zones, sizeof(struct dm_zone), GFP_KERNEL);
1177 dmz_dev_info(dev, "Using %zu B for zone information",
1178 sizeof(struct dm_zone) * dev->nr_zones);
1180 /* Get zone information */
1181 nr_blkz = DMZ_REPORT_NR_ZONES;
1182 blkz = kcalloc(nr_blkz, sizeof(struct blk_zone), GFP_KERNEL);
1189 * Get zone information and initialize zone descriptors.
1190 * At the same time, determine where the super block
1191 * should be: first block of the first randomly writable
1195 while (sector < dev->capacity) {
1196 /* Get zone information */
1197 nr_blkz = DMZ_REPORT_NR_ZONES;
1198 ret = blkdev_report_zones(dev->bdev, sector, blkz, &nr_blkz);
1200 dmz_dev_err(dev, "Report zones failed %d", ret);
1207 /* Process report */
1208 for (i = 0; i < nr_blkz; i++) {
1209 ret = dmz_init_zone(zmd, zone, &blkz[i]);
1212 sector += dev->zone_nr_sectors;
1217 /* The entire zone configuration of the disk should now be known */
1218 if (sector < dev->capacity) {
1219 dmz_dev_err(dev, "Failed to get correct zone information");
1225 dmz_drop_zones(zmd);
1231 * Update a zone information.
1233 static int dmz_update_zone(struct dmz_metadata *zmd, struct dm_zone *zone)
1235 unsigned int nr_blkz = 1;
1236 unsigned int noio_flag;
1237 struct blk_zone blkz;
1241 * Get zone information from disk. Since blkdev_report_zones() uses
1242 * GFP_KERNEL by default for memory allocations, set the per-task
1243 * PF_MEMALLOC_NOIO flag so that all allocations are done as if
1244 * GFP_NOIO was specified.
1246 noio_flag = memalloc_noio_save();
1247 ret = blkdev_report_zones(zmd->dev->bdev, dmz_start_sect(zmd, zone),
1249 memalloc_noio_restore(noio_flag);
1253 dmz_dev_err(zmd->dev, "Get zone %u report failed",
1255 dmz_check_bdev(zmd->dev);
1259 clear_bit(DMZ_OFFLINE, &zone->flags);
1260 clear_bit(DMZ_READ_ONLY, &zone->flags);
1261 if (blkz.cond == BLK_ZONE_COND_OFFLINE)
1262 set_bit(DMZ_OFFLINE, &zone->flags);
1263 else if (blkz.cond == BLK_ZONE_COND_READONLY)
1264 set_bit(DMZ_READ_ONLY, &zone->flags);
1266 if (dmz_is_seq(zone))
1267 zone->wp_block = dmz_sect2blk(blkz.wp - blkz.start);
1275 * Check a zone write pointer position when the zone is marked
1276 * with the sequential write error flag.
1278 static int dmz_handle_seq_write_err(struct dmz_metadata *zmd,
1279 struct dm_zone *zone)
1281 unsigned int wp = 0;
1284 wp = zone->wp_block;
1285 ret = dmz_update_zone(zmd, zone);
1289 dmz_dev_warn(zmd->dev, "Processing zone %u write error (zone wp %u/%u)",
1290 dmz_id(zmd, zone), zone->wp_block, wp);
1292 if (zone->wp_block < wp) {
1293 dmz_invalidate_blocks(zmd, zone, zone->wp_block,
1294 wp - zone->wp_block);
1300 static struct dm_zone *dmz_get(struct dmz_metadata *zmd, unsigned int zone_id)
1302 return &zmd->zones[zone_id];
1306 * Reset a zone write pointer.
1308 static int dmz_reset_zone(struct dmz_metadata *zmd, struct dm_zone *zone)
1313 * Ignore offline zones, read only zones,
1314 * and conventional zones.
1316 if (dmz_is_offline(zone) ||
1317 dmz_is_readonly(zone) ||
1321 if (!dmz_is_empty(zone) || dmz_seq_write_err(zone)) {
1322 struct dmz_dev *dev = zmd->dev;
1324 ret = blkdev_reset_zones(dev->bdev,
1325 dmz_start_sect(zmd, zone),
1326 dev->zone_nr_sectors, GFP_NOIO);
1328 dmz_dev_err(dev, "Reset zone %u failed %d",
1329 dmz_id(zmd, zone), ret);
1334 /* Clear write error bit and rewind write pointer position */
1335 clear_bit(DMZ_SEQ_WRITE_ERR, &zone->flags);
1341 static void dmz_get_zone_weight(struct dmz_metadata *zmd, struct dm_zone *zone);
1344 * Initialize chunk mapping.
1346 static int dmz_load_mapping(struct dmz_metadata *zmd)
1348 struct dmz_dev *dev = zmd->dev;
1349 struct dm_zone *dzone, *bzone;
1350 struct dmz_mblock *dmap_mblk = NULL;
1351 struct dmz_map *dmap;
1352 unsigned int i = 0, e = 0, chunk = 0;
1353 unsigned int dzone_id;
1354 unsigned int bzone_id;
1356 /* Metadata block array for the chunk mapping table */
1357 zmd->map_mblk = kcalloc(zmd->nr_map_blocks,
1358 sizeof(struct dmz_mblk *), GFP_KERNEL);
1362 /* Get chunk mapping table blocks and initialize zone mapping */
1363 while (chunk < zmd->nr_chunks) {
1365 /* Get mapping block */
1366 dmap_mblk = dmz_get_mblock(zmd, i + 1);
1367 if (IS_ERR(dmap_mblk))
1368 return PTR_ERR(dmap_mblk);
1369 zmd->map_mblk[i] = dmap_mblk;
1370 dmap = (struct dmz_map *) dmap_mblk->data;
1375 /* Check data zone */
1376 dzone_id = le32_to_cpu(dmap[e].dzone_id);
1377 if (dzone_id == DMZ_MAP_UNMAPPED)
1380 if (dzone_id >= dev->nr_zones) {
1381 dmz_dev_err(dev, "Chunk %u mapping: invalid data zone ID %u",
1386 dzone = dmz_get(zmd, dzone_id);
1387 set_bit(DMZ_DATA, &dzone->flags);
1388 dzone->chunk = chunk;
1389 dmz_get_zone_weight(zmd, dzone);
1391 if (dmz_is_rnd(dzone))
1392 list_add_tail(&dzone->link, &zmd->map_rnd_list);
1394 list_add_tail(&dzone->link, &zmd->map_seq_list);
1396 /* Check buffer zone */
1397 bzone_id = le32_to_cpu(dmap[e].bzone_id);
1398 if (bzone_id == DMZ_MAP_UNMAPPED)
1401 if (bzone_id >= dev->nr_zones) {
1402 dmz_dev_err(dev, "Chunk %u mapping: invalid buffer zone ID %u",
1407 bzone = dmz_get(zmd, bzone_id);
1408 if (!dmz_is_rnd(bzone)) {
1409 dmz_dev_err(dev, "Chunk %u mapping: invalid buffer zone %u",
1414 set_bit(DMZ_DATA, &bzone->flags);
1415 set_bit(DMZ_BUF, &bzone->flags);
1416 bzone->chunk = chunk;
1417 bzone->bzone = dzone;
1418 dzone->bzone = bzone;
1419 dmz_get_zone_weight(zmd, bzone);
1420 list_add_tail(&bzone->link, &zmd->map_rnd_list);
1424 if (e >= DMZ_MAP_ENTRIES)
1429 * At this point, only meta zones and mapped data zones were
1430 * fully initialized. All remaining zones are unmapped data
1431 * zones. Finish initializing those here.
1433 for (i = 0; i < dev->nr_zones; i++) {
1434 dzone = dmz_get(zmd, i);
1435 if (dmz_is_meta(dzone))
1438 if (dmz_is_rnd(dzone))
1443 if (dmz_is_data(dzone)) {
1444 /* Already initialized */
1448 /* Unmapped data zone */
1449 set_bit(DMZ_DATA, &dzone->flags);
1450 dzone->chunk = DMZ_MAP_UNMAPPED;
1451 if (dmz_is_rnd(dzone)) {
1452 list_add_tail(&dzone->link, &zmd->unmap_rnd_list);
1453 atomic_inc(&zmd->unmap_nr_rnd);
1454 } else if (atomic_read(&zmd->nr_reserved_seq_zones) < zmd->nr_reserved_seq) {
1455 list_add_tail(&dzone->link, &zmd->reserved_seq_zones_list);
1456 atomic_inc(&zmd->nr_reserved_seq_zones);
1459 list_add_tail(&dzone->link, &zmd->unmap_seq_list);
1460 atomic_inc(&zmd->unmap_nr_seq);
1468 * Set a data chunk mapping.
1470 static void dmz_set_chunk_mapping(struct dmz_metadata *zmd, unsigned int chunk,
1471 unsigned int dzone_id, unsigned int bzone_id)
1473 struct dmz_mblock *dmap_mblk = zmd->map_mblk[chunk >> DMZ_MAP_ENTRIES_SHIFT];
1474 struct dmz_map *dmap = (struct dmz_map *) dmap_mblk->data;
1475 int map_idx = chunk & DMZ_MAP_ENTRIES_MASK;
1477 dmap[map_idx].dzone_id = cpu_to_le32(dzone_id);
1478 dmap[map_idx].bzone_id = cpu_to_le32(bzone_id);
1479 dmz_dirty_mblock(zmd, dmap_mblk);
1483 * The list of mapped zones is maintained in LRU order.
1484 * This rotates a zone at the end of its map list.
1486 static void __dmz_lru_zone(struct dmz_metadata *zmd, struct dm_zone *zone)
1488 if (list_empty(&zone->link))
1491 list_del_init(&zone->link);
1492 if (dmz_is_seq(zone)) {
1493 /* LRU rotate sequential zone */
1494 list_add_tail(&zone->link, &zmd->map_seq_list);
1496 /* LRU rotate random zone */
1497 list_add_tail(&zone->link, &zmd->map_rnd_list);
1502 * The list of mapped random zones is maintained
1503 * in LRU order. This rotates a zone at the end of the list.
1505 static void dmz_lru_zone(struct dmz_metadata *zmd, struct dm_zone *zone)
1507 __dmz_lru_zone(zmd, zone);
1509 __dmz_lru_zone(zmd, zone->bzone);
1513 * Wait for any zone to be freed.
1515 static void dmz_wait_for_free_zones(struct dmz_metadata *zmd)
1519 prepare_to_wait(&zmd->free_wq, &wait, TASK_UNINTERRUPTIBLE);
1520 dmz_unlock_map(zmd);
1521 dmz_unlock_metadata(zmd);
1523 io_schedule_timeout(HZ);
1525 dmz_lock_metadata(zmd);
1527 finish_wait(&zmd->free_wq, &wait);
1531 * Lock a zone for reclaim (set the zone RECLAIM bit).
1532 * Returns false if the zone cannot be locked or if it is already locked
1535 int dmz_lock_zone_reclaim(struct dm_zone *zone)
1537 /* Active zones cannot be reclaimed */
1538 if (dmz_is_active(zone))
1541 return !test_and_set_bit(DMZ_RECLAIM, &zone->flags);
1545 * Clear a zone reclaim flag.
1547 void dmz_unlock_zone_reclaim(struct dm_zone *zone)
1549 WARN_ON(dmz_is_active(zone));
1550 WARN_ON(!dmz_in_reclaim(zone));
1552 clear_bit_unlock(DMZ_RECLAIM, &zone->flags);
1553 smp_mb__after_atomic();
1554 wake_up_bit(&zone->flags, DMZ_RECLAIM);
1558 * Wait for a zone reclaim to complete.
1560 static void dmz_wait_for_reclaim(struct dmz_metadata *zmd, struct dm_zone *zone)
1562 dmz_unlock_map(zmd);
1563 dmz_unlock_metadata(zmd);
1564 wait_on_bit_timeout(&zone->flags, DMZ_RECLAIM, TASK_UNINTERRUPTIBLE, HZ);
1565 dmz_lock_metadata(zmd);
1570 * Select a random write zone for reclaim.
1572 static struct dm_zone *dmz_get_rnd_zone_for_reclaim(struct dmz_metadata *zmd)
1574 struct dm_zone *dzone = NULL;
1575 struct dm_zone *zone;
1577 if (list_empty(&zmd->map_rnd_list))
1578 return ERR_PTR(-EBUSY);
1580 list_for_each_entry(zone, &zmd->map_rnd_list, link) {
1581 if (dmz_is_buf(zone))
1582 dzone = zone->bzone;
1585 if (dmz_lock_zone_reclaim(dzone))
1589 return ERR_PTR(-EBUSY);
1593 * Select a buffered sequential zone for reclaim.
1595 static struct dm_zone *dmz_get_seq_zone_for_reclaim(struct dmz_metadata *zmd)
1597 struct dm_zone *zone;
1599 if (list_empty(&zmd->map_seq_list))
1600 return ERR_PTR(-EBUSY);
1602 list_for_each_entry(zone, &zmd->map_seq_list, link) {
1605 if (dmz_lock_zone_reclaim(zone))
1609 return ERR_PTR(-EBUSY);
1613 * Select a zone for reclaim.
1615 struct dm_zone *dmz_get_zone_for_reclaim(struct dmz_metadata *zmd)
1617 struct dm_zone *zone;
1620 * Search for a zone candidate to reclaim: 2 cases are possible.
1621 * (1) There is no free sequential zones. Then a random data zone
1622 * cannot be reclaimed. So choose a sequential zone to reclaim so
1623 * that afterward a random zone can be reclaimed.
1624 * (2) At least one free sequential zone is available, then choose
1625 * the oldest random zone (data or buffer) that can be locked.
1628 if (list_empty(&zmd->reserved_seq_zones_list))
1629 zone = dmz_get_seq_zone_for_reclaim(zmd);
1631 zone = dmz_get_rnd_zone_for_reclaim(zmd);
1632 dmz_unlock_map(zmd);
1638 * Get the zone mapping a chunk, if the chunk is mapped already.
1639 * If no mapping exist and the operation is WRITE, a zone is
1640 * allocated and used to map the chunk.
1641 * The zone returned will be set to the active state.
1643 struct dm_zone *dmz_get_chunk_mapping(struct dmz_metadata *zmd, unsigned int chunk, int op)
1645 struct dmz_mblock *dmap_mblk = zmd->map_mblk[chunk >> DMZ_MAP_ENTRIES_SHIFT];
1646 struct dmz_map *dmap = (struct dmz_map *) dmap_mblk->data;
1647 int dmap_idx = chunk & DMZ_MAP_ENTRIES_MASK;
1648 unsigned int dzone_id;
1649 struct dm_zone *dzone = NULL;
1654 /* Get the chunk mapping */
1655 dzone_id = le32_to_cpu(dmap[dmap_idx].dzone_id);
1656 if (dzone_id == DMZ_MAP_UNMAPPED) {
1658 * Read or discard in unmapped chunks are fine. But for
1659 * writes, we need a mapping, so get one.
1661 if (op != REQ_OP_WRITE)
1664 /* Allocate a random zone */
1665 dzone = dmz_alloc_zone(zmd, DMZ_ALLOC_RND);
1667 if (dmz_bdev_is_dying(zmd->dev)) {
1668 dzone = ERR_PTR(-EIO);
1671 dmz_wait_for_free_zones(zmd);
1675 dmz_map_zone(zmd, dzone, chunk);
1678 /* The chunk is already mapped: get the mapping zone */
1679 dzone = dmz_get(zmd, dzone_id);
1680 if (dzone->chunk != chunk) {
1681 dzone = ERR_PTR(-EIO);
1685 /* Repair write pointer if the sequential dzone has error */
1686 if (dmz_seq_write_err(dzone)) {
1687 ret = dmz_handle_seq_write_err(zmd, dzone);
1689 dzone = ERR_PTR(-EIO);
1692 clear_bit(DMZ_SEQ_WRITE_ERR, &dzone->flags);
1697 * If the zone is being reclaimed, the chunk mapping may change
1698 * to a different zone. So wait for reclaim and retry. Otherwise,
1699 * activate the zone (this will prevent reclaim from touching it).
1701 if (dmz_in_reclaim(dzone)) {
1702 dmz_wait_for_reclaim(zmd, dzone);
1705 dmz_activate_zone(dzone);
1706 dmz_lru_zone(zmd, dzone);
1708 dmz_unlock_map(zmd);
1714 * Write and discard change the block validity of data zones and their buffer
1715 * zones. Check here that valid blocks are still present. If all blocks are
1716 * invalid, the zones can be unmapped on the fly without waiting for reclaim
1719 void dmz_put_chunk_mapping(struct dmz_metadata *zmd, struct dm_zone *dzone)
1721 struct dm_zone *bzone;
1725 bzone = dzone->bzone;
1727 if (dmz_weight(bzone))
1728 dmz_lru_zone(zmd, bzone);
1730 /* Empty buffer zone: reclaim it */
1731 dmz_unmap_zone(zmd, bzone);
1732 dmz_free_zone(zmd, bzone);
1737 /* Deactivate the data zone */
1738 dmz_deactivate_zone(dzone);
1739 if (dmz_is_active(dzone) || bzone || dmz_weight(dzone))
1740 dmz_lru_zone(zmd, dzone);
1742 /* Unbuffered inactive empty data zone: reclaim it */
1743 dmz_unmap_zone(zmd, dzone);
1744 dmz_free_zone(zmd, dzone);
1747 dmz_unlock_map(zmd);
1751 * Allocate and map a random zone to buffer a chunk
1752 * already mapped to a sequential zone.
1754 struct dm_zone *dmz_get_chunk_buffer(struct dmz_metadata *zmd,
1755 struct dm_zone *dzone)
1757 struct dm_zone *bzone;
1761 bzone = dzone->bzone;
1765 /* Allocate a random zone */
1766 bzone = dmz_alloc_zone(zmd, DMZ_ALLOC_RND);
1768 if (dmz_bdev_is_dying(zmd->dev)) {
1769 bzone = ERR_PTR(-EIO);
1772 dmz_wait_for_free_zones(zmd);
1776 /* Update the chunk mapping */
1777 dmz_set_chunk_mapping(zmd, dzone->chunk, dmz_id(zmd, dzone),
1778 dmz_id(zmd, bzone));
1780 set_bit(DMZ_BUF, &bzone->flags);
1781 bzone->chunk = dzone->chunk;
1782 bzone->bzone = dzone;
1783 dzone->bzone = bzone;
1784 list_add_tail(&bzone->link, &zmd->map_rnd_list);
1786 dmz_unlock_map(zmd);
1792 * Get an unmapped (free) zone.
1793 * This must be called with the mapping lock held.
1795 struct dm_zone *dmz_alloc_zone(struct dmz_metadata *zmd, unsigned long flags)
1797 struct list_head *list;
1798 struct dm_zone *zone;
1800 if (flags & DMZ_ALLOC_RND)
1801 list = &zmd->unmap_rnd_list;
1803 list = &zmd->unmap_seq_list;
1805 if (list_empty(list)) {
1807 * No free zone: if this is for reclaim, allow using the
1808 * reserved sequential zones.
1810 if (!(flags & DMZ_ALLOC_RECLAIM) ||
1811 list_empty(&zmd->reserved_seq_zones_list))
1814 zone = list_first_entry(&zmd->reserved_seq_zones_list,
1815 struct dm_zone, link);
1816 list_del_init(&zone->link);
1817 atomic_dec(&zmd->nr_reserved_seq_zones);
1821 zone = list_first_entry(list, struct dm_zone, link);
1822 list_del_init(&zone->link);
1824 if (dmz_is_rnd(zone))
1825 atomic_dec(&zmd->unmap_nr_rnd);
1827 atomic_dec(&zmd->unmap_nr_seq);
1829 if (dmz_is_offline(zone)) {
1830 dmz_dev_warn(zmd->dev, "Zone %u is offline", dmz_id(zmd, zone));
1840 * This must be called with the mapping lock held.
1842 void dmz_free_zone(struct dmz_metadata *zmd, struct dm_zone *zone)
1844 /* If this is a sequential zone, reset it */
1845 if (dmz_is_seq(zone))
1846 dmz_reset_zone(zmd, zone);
1848 /* Return the zone to its type unmap list */
1849 if (dmz_is_rnd(zone)) {
1850 list_add_tail(&zone->link, &zmd->unmap_rnd_list);
1851 atomic_inc(&zmd->unmap_nr_rnd);
1852 } else if (atomic_read(&zmd->nr_reserved_seq_zones) <
1853 zmd->nr_reserved_seq) {
1854 list_add_tail(&zone->link, &zmd->reserved_seq_zones_list);
1855 atomic_inc(&zmd->nr_reserved_seq_zones);
1857 list_add_tail(&zone->link, &zmd->unmap_seq_list);
1858 atomic_inc(&zmd->unmap_nr_seq);
1861 wake_up_all(&zmd->free_wq);
1865 * Map a chunk to a zone.
1866 * This must be called with the mapping lock held.
1868 void dmz_map_zone(struct dmz_metadata *zmd, struct dm_zone *dzone,
1871 /* Set the chunk mapping */
1872 dmz_set_chunk_mapping(zmd, chunk, dmz_id(zmd, dzone),
1874 dzone->chunk = chunk;
1875 if (dmz_is_rnd(dzone))
1876 list_add_tail(&dzone->link, &zmd->map_rnd_list);
1878 list_add_tail(&dzone->link, &zmd->map_seq_list);
1883 * This must be called with the mapping lock held.
1885 void dmz_unmap_zone(struct dmz_metadata *zmd, struct dm_zone *zone)
1887 unsigned int chunk = zone->chunk;
1888 unsigned int dzone_id;
1890 if (chunk == DMZ_MAP_UNMAPPED) {
1891 /* Already unmapped */
1895 if (test_and_clear_bit(DMZ_BUF, &zone->flags)) {
1897 * Unmapping the chunk buffer zone: clear only
1898 * the chunk buffer mapping
1900 dzone_id = dmz_id(zmd, zone->bzone);
1901 zone->bzone->bzone = NULL;
1906 * Unmapping the chunk data zone: the zone must
1909 if (WARN_ON(zone->bzone)) {
1910 zone->bzone->bzone = NULL;
1913 dzone_id = DMZ_MAP_UNMAPPED;
1916 dmz_set_chunk_mapping(zmd, chunk, dzone_id, DMZ_MAP_UNMAPPED);
1918 zone->chunk = DMZ_MAP_UNMAPPED;
1919 list_del_init(&zone->link);
1923 * Set @nr_bits bits in @bitmap starting from @bit.
1924 * Return the number of bits changed from 0 to 1.
1926 static unsigned int dmz_set_bits(unsigned long *bitmap,
1927 unsigned int bit, unsigned int nr_bits)
1929 unsigned long *addr;
1930 unsigned int end = bit + nr_bits;
1934 if (((bit & (BITS_PER_LONG - 1)) == 0) &&
1935 ((end - bit) >= BITS_PER_LONG)) {
1936 /* Try to set the whole word at once */
1937 addr = bitmap + BIT_WORD(bit);
1941 bit += BITS_PER_LONG;
1946 if (!test_and_set_bit(bit, bitmap))
1955 * Get the bitmap block storing the bit for chunk_block in zone.
1957 static struct dmz_mblock *dmz_get_bitmap(struct dmz_metadata *zmd,
1958 struct dm_zone *zone,
1959 sector_t chunk_block)
1961 sector_t bitmap_block = 1 + zmd->nr_map_blocks +
1962 (sector_t)(dmz_id(zmd, zone) * zmd->zone_nr_bitmap_blocks) +
1963 (chunk_block >> DMZ_BLOCK_SHIFT_BITS);
1965 return dmz_get_mblock(zmd, bitmap_block);
1969 * Copy the valid blocks bitmap of from_zone to the bitmap of to_zone.
1971 int dmz_copy_valid_blocks(struct dmz_metadata *zmd, struct dm_zone *from_zone,
1972 struct dm_zone *to_zone)
1974 struct dmz_mblock *from_mblk, *to_mblk;
1975 sector_t chunk_block = 0;
1977 /* Get the zones bitmap blocks */
1978 while (chunk_block < zmd->dev->zone_nr_blocks) {
1979 from_mblk = dmz_get_bitmap(zmd, from_zone, chunk_block);
1980 if (IS_ERR(from_mblk))
1981 return PTR_ERR(from_mblk);
1982 to_mblk = dmz_get_bitmap(zmd, to_zone, chunk_block);
1983 if (IS_ERR(to_mblk)) {
1984 dmz_release_mblock(zmd, from_mblk);
1985 return PTR_ERR(to_mblk);
1988 memcpy(to_mblk->data, from_mblk->data, DMZ_BLOCK_SIZE);
1989 dmz_dirty_mblock(zmd, to_mblk);
1991 dmz_release_mblock(zmd, to_mblk);
1992 dmz_release_mblock(zmd, from_mblk);
1994 chunk_block += DMZ_BLOCK_SIZE_BITS;
1997 to_zone->weight = from_zone->weight;
2003 * Merge the valid blocks bitmap of from_zone into the bitmap of to_zone,
2004 * starting from chunk_block.
2006 int dmz_merge_valid_blocks(struct dmz_metadata *zmd, struct dm_zone *from_zone,
2007 struct dm_zone *to_zone, sector_t chunk_block)
2009 unsigned int nr_blocks;
2012 /* Get the zones bitmap blocks */
2013 while (chunk_block < zmd->dev->zone_nr_blocks) {
2014 /* Get a valid region from the source zone */
2015 ret = dmz_first_valid_block(zmd, from_zone, &chunk_block);
2020 ret = dmz_validate_blocks(zmd, to_zone, chunk_block, nr_blocks);
2024 chunk_block += nr_blocks;
2031 * Validate all the blocks in the range [block..block+nr_blocks-1].
2033 int dmz_validate_blocks(struct dmz_metadata *zmd, struct dm_zone *zone,
2034 sector_t chunk_block, unsigned int nr_blocks)
2036 unsigned int count, bit, nr_bits;
2037 unsigned int zone_nr_blocks = zmd->dev->zone_nr_blocks;
2038 struct dmz_mblock *mblk;
2041 dmz_dev_debug(zmd->dev, "=> VALIDATE zone %u, block %llu, %u blocks",
2042 dmz_id(zmd, zone), (unsigned long long)chunk_block,
2045 WARN_ON(chunk_block + nr_blocks > zone_nr_blocks);
2048 /* Get bitmap block */
2049 mblk = dmz_get_bitmap(zmd, zone, chunk_block);
2051 return PTR_ERR(mblk);
2054 bit = chunk_block & DMZ_BLOCK_MASK_BITS;
2055 nr_bits = min(nr_blocks, DMZ_BLOCK_SIZE_BITS - bit);
2057 count = dmz_set_bits((unsigned long *)mblk->data, bit, nr_bits);
2059 dmz_dirty_mblock(zmd, mblk);
2062 dmz_release_mblock(zmd, mblk);
2064 nr_blocks -= nr_bits;
2065 chunk_block += nr_bits;
2068 if (likely(zone->weight + n <= zone_nr_blocks))
2071 dmz_dev_warn(zmd->dev, "Zone %u: weight %u should be <= %u",
2072 dmz_id(zmd, zone), zone->weight,
2073 zone_nr_blocks - n);
2074 zone->weight = zone_nr_blocks;
2081 * Clear nr_bits bits in bitmap starting from bit.
2082 * Return the number of bits cleared.
2084 static int dmz_clear_bits(unsigned long *bitmap, int bit, int nr_bits)
2086 unsigned long *addr;
2087 int end = bit + nr_bits;
2091 if (((bit & (BITS_PER_LONG - 1)) == 0) &&
2092 ((end - bit) >= BITS_PER_LONG)) {
2093 /* Try to clear whole word at once */
2094 addr = bitmap + BIT_WORD(bit);
2095 if (*addr == ULONG_MAX) {
2098 bit += BITS_PER_LONG;
2103 if (test_and_clear_bit(bit, bitmap))
2112 * Invalidate all the blocks in the range [block..block+nr_blocks-1].
2114 int dmz_invalidate_blocks(struct dmz_metadata *zmd, struct dm_zone *zone,
2115 sector_t chunk_block, unsigned int nr_blocks)
2117 unsigned int count, bit, nr_bits;
2118 struct dmz_mblock *mblk;
2121 dmz_dev_debug(zmd->dev, "=> INVALIDATE zone %u, block %llu, %u blocks",
2122 dmz_id(zmd, zone), (u64)chunk_block, nr_blocks);
2124 WARN_ON(chunk_block + nr_blocks > zmd->dev->zone_nr_blocks);
2127 /* Get bitmap block */
2128 mblk = dmz_get_bitmap(zmd, zone, chunk_block);
2130 return PTR_ERR(mblk);
2133 bit = chunk_block & DMZ_BLOCK_MASK_BITS;
2134 nr_bits = min(nr_blocks, DMZ_BLOCK_SIZE_BITS - bit);
2136 count = dmz_clear_bits((unsigned long *)mblk->data,
2139 dmz_dirty_mblock(zmd, mblk);
2142 dmz_release_mblock(zmd, mblk);
2144 nr_blocks -= nr_bits;
2145 chunk_block += nr_bits;
2148 if (zone->weight >= n)
2151 dmz_dev_warn(zmd->dev, "Zone %u: weight %u should be >= %u",
2152 dmz_id(zmd, zone), zone->weight, n);
2160 * Get a block bit value.
2162 static int dmz_test_block(struct dmz_metadata *zmd, struct dm_zone *zone,
2163 sector_t chunk_block)
2165 struct dmz_mblock *mblk;
2168 WARN_ON(chunk_block >= zmd->dev->zone_nr_blocks);
2170 /* Get bitmap block */
2171 mblk = dmz_get_bitmap(zmd, zone, chunk_block);
2173 return PTR_ERR(mblk);
2176 ret = test_bit(chunk_block & DMZ_BLOCK_MASK_BITS,
2177 (unsigned long *) mblk->data) != 0;
2179 dmz_release_mblock(zmd, mblk);
2185 * Return the number of blocks from chunk_block to the first block with a bit
2186 * value specified by set. Search at most nr_blocks blocks from chunk_block.
2188 static int dmz_to_next_set_block(struct dmz_metadata *zmd, struct dm_zone *zone,
2189 sector_t chunk_block, unsigned int nr_blocks,
2192 struct dmz_mblock *mblk;
2193 unsigned int bit, set_bit, nr_bits;
2194 unsigned long *bitmap;
2197 WARN_ON(chunk_block + nr_blocks > zmd->dev->zone_nr_blocks);
2200 /* Get bitmap block */
2201 mblk = dmz_get_bitmap(zmd, zone, chunk_block);
2203 return PTR_ERR(mblk);
2206 bitmap = (unsigned long *) mblk->data;
2207 bit = chunk_block & DMZ_BLOCK_MASK_BITS;
2208 nr_bits = min(nr_blocks, DMZ_BLOCK_SIZE_BITS - bit);
2210 set_bit = find_next_bit(bitmap, DMZ_BLOCK_SIZE_BITS, bit);
2212 set_bit = find_next_zero_bit(bitmap, DMZ_BLOCK_SIZE_BITS, bit);
2213 dmz_release_mblock(zmd, mblk);
2216 if (set_bit < DMZ_BLOCK_SIZE_BITS)
2219 nr_blocks -= nr_bits;
2220 chunk_block += nr_bits;
2227 * Test if chunk_block is valid. If it is, the number of consecutive
2228 * valid blocks from chunk_block will be returned.
2230 int dmz_block_valid(struct dmz_metadata *zmd, struct dm_zone *zone,
2231 sector_t chunk_block)
2235 valid = dmz_test_block(zmd, zone, chunk_block);
2239 /* The block is valid: get the number of valid blocks from block */
2240 return dmz_to_next_set_block(zmd, zone, chunk_block,
2241 zmd->dev->zone_nr_blocks - chunk_block, 0);
2245 * Find the first valid block from @chunk_block in @zone.
2246 * If such a block is found, its number is returned using
2247 * @chunk_block and the total number of valid blocks from @chunk_block
2250 int dmz_first_valid_block(struct dmz_metadata *zmd, struct dm_zone *zone,
2251 sector_t *chunk_block)
2253 sector_t start_block = *chunk_block;
2256 ret = dmz_to_next_set_block(zmd, zone, start_block,
2257 zmd->dev->zone_nr_blocks - start_block, 1);
2262 *chunk_block = start_block;
2264 return dmz_to_next_set_block(zmd, zone, start_block,
2265 zmd->dev->zone_nr_blocks - start_block, 0);
2269 * Count the number of bits set starting from bit up to bit + nr_bits - 1.
2271 static int dmz_count_bits(void *bitmap, int bit, int nr_bits)
2273 unsigned long *addr;
2274 int end = bit + nr_bits;
2278 if (((bit & (BITS_PER_LONG - 1)) == 0) &&
2279 ((end - bit) >= BITS_PER_LONG)) {
2280 addr = (unsigned long *)bitmap + BIT_WORD(bit);
2281 if (*addr == ULONG_MAX) {
2283 bit += BITS_PER_LONG;
2288 if (test_bit(bit, bitmap))
2297 * Get a zone weight.
2299 static void dmz_get_zone_weight(struct dmz_metadata *zmd, struct dm_zone *zone)
2301 struct dmz_mblock *mblk;
2302 sector_t chunk_block = 0;
2303 unsigned int bit, nr_bits;
2304 unsigned int nr_blocks = zmd->dev->zone_nr_blocks;
2309 /* Get bitmap block */
2310 mblk = dmz_get_bitmap(zmd, zone, chunk_block);
2316 /* Count bits in this block */
2317 bitmap = mblk->data;
2318 bit = chunk_block & DMZ_BLOCK_MASK_BITS;
2319 nr_bits = min(nr_blocks, DMZ_BLOCK_SIZE_BITS - bit);
2320 n += dmz_count_bits(bitmap, bit, nr_bits);
2322 dmz_release_mblock(zmd, mblk);
2324 nr_blocks -= nr_bits;
2325 chunk_block += nr_bits;
2332 * Cleanup the zoned metadata resources.
2334 static void dmz_cleanup_metadata(struct dmz_metadata *zmd)
2336 struct rb_root *root;
2337 struct dmz_mblock *mblk, *next;
2340 /* Release zone mapping resources */
2341 if (zmd->map_mblk) {
2342 for (i = 0; i < zmd->nr_map_blocks; i++)
2343 dmz_release_mblock(zmd, zmd->map_mblk[i]);
2344 kfree(zmd->map_mblk);
2345 zmd->map_mblk = NULL;
2348 /* Release super blocks */
2349 for (i = 0; i < 2; i++) {
2350 if (zmd->sb[i].mblk) {
2351 dmz_free_mblock(zmd, zmd->sb[i].mblk);
2352 zmd->sb[i].mblk = NULL;
2356 /* Free cached blocks */
2357 while (!list_empty(&zmd->mblk_dirty_list)) {
2358 mblk = list_first_entry(&zmd->mblk_dirty_list,
2359 struct dmz_mblock, link);
2360 dmz_dev_warn(zmd->dev, "mblock %llu still in dirty list (ref %u)",
2361 (u64)mblk->no, mblk->ref);
2362 list_del_init(&mblk->link);
2363 rb_erase(&mblk->node, &zmd->mblk_rbtree);
2364 dmz_free_mblock(zmd, mblk);
2367 while (!list_empty(&zmd->mblk_lru_list)) {
2368 mblk = list_first_entry(&zmd->mblk_lru_list,
2369 struct dmz_mblock, link);
2370 list_del_init(&mblk->link);
2371 rb_erase(&mblk->node, &zmd->mblk_rbtree);
2372 dmz_free_mblock(zmd, mblk);
2375 /* Sanity checks: the mblock rbtree should now be empty */
2376 root = &zmd->mblk_rbtree;
2377 rbtree_postorder_for_each_entry_safe(mblk, next, root, node) {
2378 dmz_dev_warn(zmd->dev, "mblock %llu ref %u still in rbtree",
2379 (u64)mblk->no, mblk->ref);
2381 dmz_free_mblock(zmd, mblk);
2384 /* Free the zone descriptors */
2385 dmz_drop_zones(zmd);
2387 mutex_destroy(&zmd->mblk_flush_lock);
2388 mutex_destroy(&zmd->map_lock);
2392 * Initialize the zoned metadata.
2394 int dmz_ctr_metadata(struct dmz_dev *dev, struct dmz_metadata **metadata)
2396 struct dmz_metadata *zmd;
2397 unsigned int i, zid;
2398 struct dm_zone *zone;
2401 zmd = kzalloc(sizeof(struct dmz_metadata), GFP_KERNEL);
2406 zmd->mblk_rbtree = RB_ROOT;
2407 init_rwsem(&zmd->mblk_sem);
2408 mutex_init(&zmd->mblk_flush_lock);
2409 spin_lock_init(&zmd->mblk_lock);
2410 INIT_LIST_HEAD(&zmd->mblk_lru_list);
2411 INIT_LIST_HEAD(&zmd->mblk_dirty_list);
2413 mutex_init(&zmd->map_lock);
2414 atomic_set(&zmd->unmap_nr_rnd, 0);
2415 INIT_LIST_HEAD(&zmd->unmap_rnd_list);
2416 INIT_LIST_HEAD(&zmd->map_rnd_list);
2418 atomic_set(&zmd->unmap_nr_seq, 0);
2419 INIT_LIST_HEAD(&zmd->unmap_seq_list);
2420 INIT_LIST_HEAD(&zmd->map_seq_list);
2422 atomic_set(&zmd->nr_reserved_seq_zones, 0);
2423 INIT_LIST_HEAD(&zmd->reserved_seq_zones_list);
2425 init_waitqueue_head(&zmd->free_wq);
2427 /* Initialize zone descriptors */
2428 ret = dmz_init_zones(zmd);
2432 /* Get super block */
2433 ret = dmz_load_sb(zmd);
2437 /* Set metadata zones starting from sb_zone */
2438 zid = dmz_id(zmd, zmd->sb_zone);
2439 for (i = 0; i < zmd->nr_meta_zones << 1; i++) {
2440 zone = dmz_get(zmd, zid + i);
2441 if (!dmz_is_rnd(zone))
2443 set_bit(DMZ_META, &zone->flags);
2446 /* Load mapping table */
2447 ret = dmz_load_mapping(zmd);
2452 * Cache size boundaries: allow at least 2 super blocks, the chunk map
2453 * blocks and enough blocks to be able to cache the bitmap blocks of
2454 * up to 16 zones when idle (min_nr_mblks). Otherwise, if busy, allow
2455 * the cache to add 512 more metadata blocks.
2457 zmd->min_nr_mblks = 2 + zmd->nr_map_blocks + zmd->zone_nr_bitmap_blocks * 16;
2458 zmd->max_nr_mblks = zmd->min_nr_mblks + 512;
2459 zmd->mblk_shrinker.count_objects = dmz_mblock_shrinker_count;
2460 zmd->mblk_shrinker.scan_objects = dmz_mblock_shrinker_scan;
2461 zmd->mblk_shrinker.seeks = DEFAULT_SEEKS;
2463 /* Metadata cache shrinker */
2464 ret = register_shrinker(&zmd->mblk_shrinker);
2466 dmz_dev_err(dev, "Register metadata cache shrinker failed");
2470 dmz_dev_info(dev, "Host-%s zoned block device",
2471 bdev_zoned_model(dev->bdev) == BLK_ZONED_HA ?
2472 "aware" : "managed");
2473 dmz_dev_info(dev, " %llu 512-byte logical sectors",
2474 (u64)dev->capacity);
2475 dmz_dev_info(dev, " %u zones of %llu 512-byte logical sectors",
2476 dev->nr_zones, (u64)dev->zone_nr_sectors);
2477 dmz_dev_info(dev, " %u metadata zones",
2478 zmd->nr_meta_zones * 2);
2479 dmz_dev_info(dev, " %u data zones for %u chunks",
2480 zmd->nr_data_zones, zmd->nr_chunks);
2481 dmz_dev_info(dev, " %u random zones (%u unmapped)",
2482 zmd->nr_rnd, atomic_read(&zmd->unmap_nr_rnd));
2483 dmz_dev_info(dev, " %u sequential zones (%u unmapped)",
2484 zmd->nr_seq, atomic_read(&zmd->unmap_nr_seq));
2485 dmz_dev_info(dev, " %u reserved sequential data zones",
2486 zmd->nr_reserved_seq);
2488 dmz_dev_debug(dev, "Format:");
2489 dmz_dev_debug(dev, "%u metadata blocks per set (%u max cache)",
2490 zmd->nr_meta_blocks, zmd->max_nr_mblks);
2491 dmz_dev_debug(dev, " %u data zone mapping blocks",
2492 zmd->nr_map_blocks);
2493 dmz_dev_debug(dev, " %u bitmap blocks",
2494 zmd->nr_bitmap_blocks);
2500 dmz_cleanup_metadata(zmd);
2508 * Cleanup the zoned metadata resources.
2510 void dmz_dtr_metadata(struct dmz_metadata *zmd)
2512 unregister_shrinker(&zmd->mblk_shrinker);
2513 dmz_cleanup_metadata(zmd);
2518 * Check zone information on resume.
2520 int dmz_resume_metadata(struct dmz_metadata *zmd)
2522 struct dmz_dev *dev = zmd->dev;
2523 struct dm_zone *zone;
2529 for (i = 0; i < dev->nr_zones; i++) {
2530 zone = dmz_get(zmd, i);
2532 dmz_dev_err(dev, "Unable to get zone %u", i);
2536 wp_block = zone->wp_block;
2538 ret = dmz_update_zone(zmd, zone);
2540 dmz_dev_err(dev, "Broken zone %u", i);
2544 if (dmz_is_offline(zone)) {
2545 dmz_dev_warn(dev, "Zone %u is offline", i);
2549 /* Check write pointer */
2550 if (!dmz_is_seq(zone))
2552 else if (zone->wp_block != wp_block) {
2553 dmz_dev_err(dev, "Zone %u: Invalid wp (%llu / %llu)",
2554 i, (u64)zone->wp_block, (u64)wp_block);
2555 zone->wp_block = wp_block;
2556 dmz_invalidate_blocks(zmd, zone, zone->wp_block,
2557 dev->zone_nr_blocks - zone->wp_block);