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 2
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 */
73 u8 dmz_label[32]; /* 80 */
76 u8 dmz_uuid[16]; /* 96 */
79 u8 dev_uuid[16]; /* 112 */
81 /* Padding to full 512B sector */
82 u8 reserved[400]; /* 512 */
86 * Chunk mapping entry: entries are indexed by chunk number
87 * and give the zone ID (dzone_id) mapping the chunk on disk.
88 * This zone may be sequential or random. If it is a sequential
89 * zone, a second zone (bzone_id) used as a write buffer may
90 * also be specified. This second zone will always be a randomly
99 * Chunk mapping table metadata: 512 8-bytes entries per 4KB block.
101 #define DMZ_MAP_ENTRIES (DMZ_BLOCK_SIZE / sizeof(struct dmz_map))
102 #define DMZ_MAP_ENTRIES_SHIFT (ilog2(DMZ_MAP_ENTRIES))
103 #define DMZ_MAP_ENTRIES_MASK (DMZ_MAP_ENTRIES - 1)
104 #define DMZ_MAP_UNMAPPED UINT_MAX
107 * Meta data block descriptor (for cached metadata blocks).
111 struct list_head link;
120 * Metadata block state flags.
130 * Super block information (one per metadata set).
135 struct dmz_mblock *mblk;
136 struct dmz_super *sb;
137 struct dm_zone *zone;
141 * In-memory metadata.
143 struct dmz_metadata {
145 unsigned int nr_devs;
147 char devname[BDEVNAME_SIZE];
148 char label[BDEVNAME_SIZE];
151 sector_t zone_bitmap_size;
152 unsigned int zone_nr_bitmap_blocks;
153 unsigned int zone_bits_per_mblk;
155 sector_t zone_nr_blocks;
156 sector_t zone_nr_blocks_shift;
158 sector_t zone_nr_sectors;
159 sector_t zone_nr_sectors_shift;
161 unsigned int nr_bitmap_blocks;
162 unsigned int nr_map_blocks;
164 unsigned int nr_zones;
165 unsigned int nr_useable_zones;
166 unsigned int nr_meta_blocks;
167 unsigned int nr_meta_zones;
168 unsigned int nr_data_zones;
169 unsigned int nr_cache_zones;
170 unsigned int nr_rnd_zones;
171 unsigned int nr_reserved_seq;
172 unsigned int nr_chunks;
174 /* Zone information array */
178 unsigned int mblk_primary;
179 unsigned int sb_version;
181 unsigned int min_nr_mblks;
182 unsigned int max_nr_mblks;
184 struct rw_semaphore mblk_sem;
185 struct mutex mblk_flush_lock;
186 spinlock_t mblk_lock;
187 struct rb_root mblk_rbtree;
188 struct list_head mblk_lru_list;
189 struct list_head mblk_dirty_list;
190 struct shrinker mblk_shrinker;
192 /* Zone allocation management */
193 struct mutex map_lock;
194 struct dmz_mblock **map_mblk;
196 unsigned int nr_cache;
197 atomic_t unmap_nr_cache;
198 struct list_head unmap_cache_list;
199 struct list_head map_cache_list;
201 atomic_t nr_reserved_seq_zones;
202 struct list_head reserved_seq_zones_list;
204 wait_queue_head_t free_wq;
207 #define dmz_zmd_info(zmd, format, args...) \
208 DMINFO("(%s): " format, (zmd)->label, ## args)
210 #define dmz_zmd_err(zmd, format, args...) \
211 DMERR("(%s): " format, (zmd)->label, ## args)
213 #define dmz_zmd_warn(zmd, format, args...) \
214 DMWARN("(%s): " format, (zmd)->label, ## args)
216 #define dmz_zmd_debug(zmd, format, args...) \
217 DMDEBUG("(%s): " format, (zmd)->label, ## args)
221 static unsigned int dmz_dev_zone_id(struct dmz_metadata *zmd, struct dm_zone *zone)
226 return zone->id - zone->dev->zone_offset;
229 sector_t dmz_start_sect(struct dmz_metadata *zmd, struct dm_zone *zone)
231 unsigned int zone_id = dmz_dev_zone_id(zmd, zone);
233 return (sector_t)zone_id << zmd->zone_nr_sectors_shift;
236 sector_t dmz_start_block(struct dmz_metadata *zmd, struct dm_zone *zone)
238 unsigned int zone_id = dmz_dev_zone_id(zmd, zone);
240 return (sector_t)zone_id << zmd->zone_nr_blocks_shift;
243 unsigned int dmz_zone_nr_blocks(struct dmz_metadata *zmd)
245 return zmd->zone_nr_blocks;
248 unsigned int dmz_zone_nr_blocks_shift(struct dmz_metadata *zmd)
250 return zmd->zone_nr_blocks_shift;
253 unsigned int dmz_zone_nr_sectors(struct dmz_metadata *zmd)
255 return zmd->zone_nr_sectors;
258 unsigned int dmz_zone_nr_sectors_shift(struct dmz_metadata *zmd)
260 return zmd->zone_nr_sectors_shift;
263 unsigned int dmz_nr_zones(struct dmz_metadata *zmd)
265 return zmd->nr_zones;
268 unsigned int dmz_nr_chunks(struct dmz_metadata *zmd)
270 return zmd->nr_chunks;
273 unsigned int dmz_nr_rnd_zones(struct dmz_metadata *zmd, int idx)
275 return zmd->dev[idx].nr_rnd;
278 unsigned int dmz_nr_unmap_rnd_zones(struct dmz_metadata *zmd, int idx)
280 return atomic_read(&zmd->dev[idx].unmap_nr_rnd);
283 unsigned int dmz_nr_cache_zones(struct dmz_metadata *zmd)
285 return zmd->nr_cache;
288 unsigned int dmz_nr_unmap_cache_zones(struct dmz_metadata *zmd)
290 return atomic_read(&zmd->unmap_nr_cache);
293 unsigned int dmz_nr_seq_zones(struct dmz_metadata *zmd, int idx)
295 return zmd->dev[idx].nr_seq;
298 unsigned int dmz_nr_unmap_seq_zones(struct dmz_metadata *zmd, int idx)
300 return atomic_read(&zmd->dev[idx].unmap_nr_seq);
303 static struct dm_zone *dmz_get(struct dmz_metadata *zmd, unsigned int zone_id)
305 return xa_load(&zmd->zones, zone_id);
308 static struct dm_zone *dmz_insert(struct dmz_metadata *zmd,
309 unsigned int zone_id, struct dmz_dev *dev)
311 struct dm_zone *zone = kzalloc(sizeof(struct dm_zone), GFP_KERNEL);
314 return ERR_PTR(-ENOMEM);
316 if (xa_insert(&zmd->zones, zone_id, zone, GFP_KERNEL)) {
318 return ERR_PTR(-EBUSY);
321 INIT_LIST_HEAD(&zone->link);
322 atomic_set(&zone->refcount, 0);
324 zone->chunk = DMZ_MAP_UNMAPPED;
330 const char *dmz_metadata_label(struct dmz_metadata *zmd)
332 return (const char *)zmd->label;
335 bool dmz_check_dev(struct dmz_metadata *zmd)
339 for (i = 0; i < zmd->nr_devs; i++) {
340 if (!dmz_check_bdev(&zmd->dev[i]))
346 bool dmz_dev_is_dying(struct dmz_metadata *zmd)
350 for (i = 0; i < zmd->nr_devs; i++) {
351 if (dmz_bdev_is_dying(&zmd->dev[i]))
358 * Lock/unlock mapping table.
359 * The map lock also protects all the zone lists.
361 void dmz_lock_map(struct dmz_metadata *zmd)
363 mutex_lock(&zmd->map_lock);
366 void dmz_unlock_map(struct dmz_metadata *zmd)
368 mutex_unlock(&zmd->map_lock);
372 * Lock/unlock metadata access. This is a "read" lock on a semaphore
373 * that prevents metadata flush from running while metadata are being
374 * modified. The actual metadata write mutual exclusion is achieved with
375 * the map lock and zone state management (active and reclaim state are
376 * mutually exclusive).
378 void dmz_lock_metadata(struct dmz_metadata *zmd)
380 down_read(&zmd->mblk_sem);
383 void dmz_unlock_metadata(struct dmz_metadata *zmd)
385 up_read(&zmd->mblk_sem);
389 * Lock/unlock flush: prevent concurrent executions
390 * of dmz_flush_metadata as well as metadata modification in reclaim
391 * while flush is being executed.
393 void dmz_lock_flush(struct dmz_metadata *zmd)
395 mutex_lock(&zmd->mblk_flush_lock);
398 void dmz_unlock_flush(struct dmz_metadata *zmd)
400 mutex_unlock(&zmd->mblk_flush_lock);
404 * Allocate a metadata block.
406 static struct dmz_mblock *dmz_alloc_mblock(struct dmz_metadata *zmd,
409 struct dmz_mblock *mblk = NULL;
411 /* See if we can reuse cached blocks */
412 if (zmd->max_nr_mblks && atomic_read(&zmd->nr_mblks) > zmd->max_nr_mblks) {
413 spin_lock(&zmd->mblk_lock);
414 mblk = list_first_entry_or_null(&zmd->mblk_lru_list,
415 struct dmz_mblock, link);
417 list_del_init(&mblk->link);
418 rb_erase(&mblk->node, &zmd->mblk_rbtree);
421 spin_unlock(&zmd->mblk_lock);
426 /* Allocate a new block */
427 mblk = kmalloc(sizeof(struct dmz_mblock), GFP_NOIO);
431 mblk->page = alloc_page(GFP_NOIO);
437 RB_CLEAR_NODE(&mblk->node);
438 INIT_LIST_HEAD(&mblk->link);
442 mblk->data = page_address(mblk->page);
444 atomic_inc(&zmd->nr_mblks);
450 * Free a metadata block.
452 static void dmz_free_mblock(struct dmz_metadata *zmd, struct dmz_mblock *mblk)
454 __free_pages(mblk->page, 0);
457 atomic_dec(&zmd->nr_mblks);
461 * Insert a metadata block in the rbtree.
463 static void dmz_insert_mblock(struct dmz_metadata *zmd, struct dmz_mblock *mblk)
465 struct rb_root *root = &zmd->mblk_rbtree;
466 struct rb_node **new = &(root->rb_node), *parent = NULL;
467 struct dmz_mblock *b;
469 /* Figure out where to put the new node */
471 b = container_of(*new, struct dmz_mblock, node);
473 new = (b->no < mblk->no) ? &((*new)->rb_left) : &((*new)->rb_right);
476 /* Add new node and rebalance tree */
477 rb_link_node(&mblk->node, parent, new);
478 rb_insert_color(&mblk->node, root);
482 * Lookup a metadata block in the rbtree. If the block is found, increment
483 * its reference count.
485 static struct dmz_mblock *dmz_get_mblock_fast(struct dmz_metadata *zmd,
488 struct rb_root *root = &zmd->mblk_rbtree;
489 struct rb_node *node = root->rb_node;
490 struct dmz_mblock *mblk;
493 mblk = container_of(node, struct dmz_mblock, node);
494 if (mblk->no == mblk_no) {
496 * If this is the first reference to the block,
497 * remove it from the LRU list.
500 if (mblk->ref == 1 &&
501 !test_bit(DMZ_META_DIRTY, &mblk->state))
502 list_del_init(&mblk->link);
505 node = (mblk->no < mblk_no) ? node->rb_left : node->rb_right;
512 * Metadata block BIO end callback.
514 static void dmz_mblock_bio_end_io(struct bio *bio)
516 struct dmz_mblock *mblk = bio->bi_private;
520 set_bit(DMZ_META_ERROR, &mblk->state);
522 if (bio_op(bio) == REQ_OP_WRITE)
523 flag = DMZ_META_WRITING;
525 flag = DMZ_META_READING;
527 clear_bit_unlock(flag, &mblk->state);
528 smp_mb__after_atomic();
529 wake_up_bit(&mblk->state, flag);
535 * Read an uncached metadata block from disk and add it to the cache.
537 static struct dmz_mblock *dmz_get_mblock_slow(struct dmz_metadata *zmd,
540 struct dmz_mblock *mblk, *m;
541 sector_t block = zmd->sb[zmd->mblk_primary].block + mblk_no;
542 struct dmz_dev *dev = zmd->sb[zmd->mblk_primary].dev;
545 if (dmz_bdev_is_dying(dev))
546 return ERR_PTR(-EIO);
548 /* Get a new block and a BIO to read it */
549 mblk = dmz_alloc_mblock(zmd, mblk_no);
551 return ERR_PTR(-ENOMEM);
553 bio = bio_alloc(dev->bdev, 1, REQ_OP_READ | REQ_META | REQ_PRIO,
556 spin_lock(&zmd->mblk_lock);
559 * Make sure that another context did not start reading
562 m = dmz_get_mblock_fast(zmd, mblk_no);
564 spin_unlock(&zmd->mblk_lock);
565 dmz_free_mblock(zmd, mblk);
571 set_bit(DMZ_META_READING, &mblk->state);
572 dmz_insert_mblock(zmd, mblk);
574 spin_unlock(&zmd->mblk_lock);
576 /* Submit read BIO */
577 bio->bi_iter.bi_sector = dmz_blk2sect(block);
578 bio->bi_private = mblk;
579 bio->bi_end_io = dmz_mblock_bio_end_io;
580 __bio_add_page(bio, mblk->page, DMZ_BLOCK_SIZE, 0);
587 * Free metadata blocks.
589 static unsigned long dmz_shrink_mblock_cache(struct dmz_metadata *zmd,
592 struct dmz_mblock *mblk;
593 unsigned long count = 0;
595 if (!zmd->max_nr_mblks)
598 while (!list_empty(&zmd->mblk_lru_list) &&
599 atomic_read(&zmd->nr_mblks) > zmd->min_nr_mblks &&
601 mblk = list_first_entry(&zmd->mblk_lru_list,
602 struct dmz_mblock, link);
603 list_del_init(&mblk->link);
604 rb_erase(&mblk->node, &zmd->mblk_rbtree);
605 dmz_free_mblock(zmd, mblk);
613 * For mblock shrinker: get the number of unused metadata blocks in the cache.
615 static unsigned long dmz_mblock_shrinker_count(struct shrinker *shrink,
616 struct shrink_control *sc)
618 struct dmz_metadata *zmd = container_of(shrink, struct dmz_metadata, mblk_shrinker);
620 return atomic_read(&zmd->nr_mblks);
624 * For mblock shrinker: scan unused metadata blocks and shrink the cache.
626 static unsigned long dmz_mblock_shrinker_scan(struct shrinker *shrink,
627 struct shrink_control *sc)
629 struct dmz_metadata *zmd = container_of(shrink, struct dmz_metadata, mblk_shrinker);
632 spin_lock(&zmd->mblk_lock);
633 count = dmz_shrink_mblock_cache(zmd, sc->nr_to_scan);
634 spin_unlock(&zmd->mblk_lock);
636 return count ? count : SHRINK_STOP;
640 * Release a metadata block.
642 static void dmz_release_mblock(struct dmz_metadata *zmd,
643 struct dmz_mblock *mblk)
649 spin_lock(&zmd->mblk_lock);
652 if (mblk->ref == 0) {
653 if (test_bit(DMZ_META_ERROR, &mblk->state)) {
654 rb_erase(&mblk->node, &zmd->mblk_rbtree);
655 dmz_free_mblock(zmd, mblk);
656 } else if (!test_bit(DMZ_META_DIRTY, &mblk->state)) {
657 list_add_tail(&mblk->link, &zmd->mblk_lru_list);
658 dmz_shrink_mblock_cache(zmd, 1);
662 spin_unlock(&zmd->mblk_lock);
666 * Get a metadata block from the rbtree. If the block
667 * is not present, read it from disk.
669 static struct dmz_mblock *dmz_get_mblock(struct dmz_metadata *zmd,
672 struct dmz_mblock *mblk;
673 struct dmz_dev *dev = zmd->sb[zmd->mblk_primary].dev;
676 spin_lock(&zmd->mblk_lock);
677 mblk = dmz_get_mblock_fast(zmd, mblk_no);
678 spin_unlock(&zmd->mblk_lock);
681 /* Cache miss: read the block from disk */
682 mblk = dmz_get_mblock_slow(zmd, mblk_no);
687 /* Wait for on-going read I/O and check for error */
688 wait_on_bit_io(&mblk->state, DMZ_META_READING,
689 TASK_UNINTERRUPTIBLE);
690 if (test_bit(DMZ_META_ERROR, &mblk->state)) {
691 dmz_release_mblock(zmd, mblk);
693 return ERR_PTR(-EIO);
700 * Mark a metadata block dirty.
702 static void dmz_dirty_mblock(struct dmz_metadata *zmd, struct dmz_mblock *mblk)
704 spin_lock(&zmd->mblk_lock);
705 if (!test_and_set_bit(DMZ_META_DIRTY, &mblk->state))
706 list_add_tail(&mblk->link, &zmd->mblk_dirty_list);
707 spin_unlock(&zmd->mblk_lock);
711 * Issue a metadata block write BIO.
713 static int dmz_write_mblock(struct dmz_metadata *zmd, struct dmz_mblock *mblk,
716 struct dmz_dev *dev = zmd->sb[set].dev;
717 sector_t block = zmd->sb[set].block + mblk->no;
720 if (dmz_bdev_is_dying(dev))
723 bio = bio_alloc(dev->bdev, 1, REQ_OP_WRITE | REQ_META | REQ_PRIO,
726 set_bit(DMZ_META_WRITING, &mblk->state);
728 bio->bi_iter.bi_sector = dmz_blk2sect(block);
729 bio->bi_private = mblk;
730 bio->bi_end_io = dmz_mblock_bio_end_io;
731 __bio_add_page(bio, mblk->page, DMZ_BLOCK_SIZE, 0);
738 * Read/write a metadata block.
740 static int dmz_rdwr_block(struct dmz_dev *dev, enum req_op op,
741 sector_t block, struct page *page)
749 if (dmz_bdev_is_dying(dev))
752 bio = bio_alloc(dev->bdev, 1, op | REQ_SYNC | REQ_META | REQ_PRIO,
754 bio->bi_iter.bi_sector = dmz_blk2sect(block);
755 __bio_add_page(bio, page, DMZ_BLOCK_SIZE, 0);
756 ret = submit_bio_wait(bio);
765 * Write super block of the specified metadata set.
767 static int dmz_write_sb(struct dmz_metadata *zmd, unsigned int set)
769 struct dmz_mblock *mblk = zmd->sb[set].mblk;
770 struct dmz_super *sb = zmd->sb[set].sb;
771 struct dmz_dev *dev = zmd->sb[set].dev;
773 u64 sb_gen = zmd->sb_gen + 1;
776 sb->magic = cpu_to_le32(DMZ_MAGIC);
778 sb->version = cpu_to_le32(zmd->sb_version);
779 if (zmd->sb_version > 1) {
780 BUILD_BUG_ON(UUID_SIZE != 16);
781 export_uuid(sb->dmz_uuid, &zmd->uuid);
782 memcpy(sb->dmz_label, zmd->label, BDEVNAME_SIZE);
783 export_uuid(sb->dev_uuid, &dev->uuid);
786 sb->gen = cpu_to_le64(sb_gen);
789 * The metadata always references the absolute block address,
790 * ie relative to the entire block range, not the per-device
793 sb_block = zmd->sb[set].zone->id << zmd->zone_nr_blocks_shift;
794 sb->sb_block = cpu_to_le64(sb_block);
795 sb->nr_meta_blocks = cpu_to_le32(zmd->nr_meta_blocks);
796 sb->nr_reserved_seq = cpu_to_le32(zmd->nr_reserved_seq);
797 sb->nr_chunks = cpu_to_le32(zmd->nr_chunks);
799 sb->nr_map_blocks = cpu_to_le32(zmd->nr_map_blocks);
800 sb->nr_bitmap_blocks = cpu_to_le32(zmd->nr_bitmap_blocks);
803 sb->crc = cpu_to_le32(crc32_le(sb_gen, (unsigned char *)sb, DMZ_BLOCK_SIZE));
805 ret = dmz_rdwr_block(dev, REQ_OP_WRITE, zmd->sb[set].block,
808 ret = blkdev_issue_flush(dev->bdev);
814 * Write dirty metadata blocks to the specified set.
816 static int dmz_write_dirty_mblocks(struct dmz_metadata *zmd,
817 struct list_head *write_list,
820 struct dmz_mblock *mblk;
821 struct dmz_dev *dev = zmd->sb[set].dev;
822 struct blk_plug plug;
823 int ret = 0, nr_mblks_submitted = 0;
826 blk_start_plug(&plug);
827 list_for_each_entry(mblk, write_list, link) {
828 ret = dmz_write_mblock(zmd, mblk, set);
831 nr_mblks_submitted++;
833 blk_finish_plug(&plug);
835 /* Wait for completion */
836 list_for_each_entry(mblk, write_list, link) {
837 if (!nr_mblks_submitted)
839 wait_on_bit_io(&mblk->state, DMZ_META_WRITING,
840 TASK_UNINTERRUPTIBLE);
841 if (test_bit(DMZ_META_ERROR, &mblk->state)) {
842 clear_bit(DMZ_META_ERROR, &mblk->state);
846 nr_mblks_submitted--;
849 /* Flush drive cache (this will also sync data) */
851 ret = blkdev_issue_flush(dev->bdev);
857 * Log dirty metadata blocks.
859 static int dmz_log_dirty_mblocks(struct dmz_metadata *zmd,
860 struct list_head *write_list)
862 unsigned int log_set = zmd->mblk_primary ^ 0x1;
865 /* Write dirty blocks to the log */
866 ret = dmz_write_dirty_mblocks(zmd, write_list, log_set);
871 * No error so far: now validate the log by updating the
872 * log index super block generation.
874 ret = dmz_write_sb(zmd, log_set);
882 * Flush dirty metadata blocks.
884 int dmz_flush_metadata(struct dmz_metadata *zmd)
886 struct dmz_mblock *mblk;
887 struct list_head write_list;
894 INIT_LIST_HEAD(&write_list);
897 * Make sure that metadata blocks are stable before logging: take
898 * the write lock on the metadata semaphore to prevent target BIOs
899 * from modifying metadata.
901 down_write(&zmd->mblk_sem);
902 dev = zmd->sb[zmd->mblk_primary].dev;
905 * This is called from the target flush work and reclaim work.
906 * Concurrent execution is not allowed.
910 if (dmz_bdev_is_dying(dev)) {
915 /* Get dirty blocks */
916 spin_lock(&zmd->mblk_lock);
917 list_splice_init(&zmd->mblk_dirty_list, &write_list);
918 spin_unlock(&zmd->mblk_lock);
920 /* If there are no dirty metadata blocks, just flush the device cache */
921 if (list_empty(&write_list)) {
922 ret = blkdev_issue_flush(dev->bdev);
927 * The primary metadata set is still clean. Keep it this way until
928 * all updates are successful in the secondary set. That is, use
929 * the secondary set as a log.
931 ret = dmz_log_dirty_mblocks(zmd, &write_list);
936 * The log is on disk. It is now safe to update in place
937 * in the primary metadata set.
939 ret = dmz_write_dirty_mblocks(zmd, &write_list, zmd->mblk_primary);
943 ret = dmz_write_sb(zmd, zmd->mblk_primary);
947 while (!list_empty(&write_list)) {
948 mblk = list_first_entry(&write_list, struct dmz_mblock, link);
949 list_del_init(&mblk->link);
951 spin_lock(&zmd->mblk_lock);
952 clear_bit(DMZ_META_DIRTY, &mblk->state);
954 list_add_tail(&mblk->link, &zmd->mblk_lru_list);
955 spin_unlock(&zmd->mblk_lock);
960 dmz_unlock_flush(zmd);
961 up_write(&zmd->mblk_sem);
966 if (!list_empty(&write_list)) {
967 spin_lock(&zmd->mblk_lock);
968 list_splice(&write_list, &zmd->mblk_dirty_list);
969 spin_unlock(&zmd->mblk_lock);
971 if (!dmz_check_bdev(dev))
979 static int dmz_check_sb(struct dmz_metadata *zmd, struct dmz_sb *dsb,
982 struct dmz_super *sb = dsb->sb;
983 struct dmz_dev *dev = dsb->dev;
984 unsigned int nr_meta_zones, nr_data_zones;
988 if (le32_to_cpu(sb->magic) != DMZ_MAGIC) {
989 dmz_dev_err(dev, "Invalid meta magic (needed 0x%08x, got 0x%08x)",
990 DMZ_MAGIC, le32_to_cpu(sb->magic));
994 zmd->sb_version = le32_to_cpu(sb->version);
995 if (zmd->sb_version > DMZ_META_VER) {
996 dmz_dev_err(dev, "Invalid meta version (needed %d, got %d)",
997 DMZ_META_VER, zmd->sb_version);
1000 if (zmd->sb_version < 2 && tertiary) {
1001 dmz_dev_err(dev, "Tertiary superblocks are not supported");
1005 gen = le64_to_cpu(sb->gen);
1006 stored_crc = le32_to_cpu(sb->crc);
1008 crc = crc32_le(gen, (unsigned char *)sb, DMZ_BLOCK_SIZE);
1009 if (crc != stored_crc) {
1010 dmz_dev_err(dev, "Invalid checksum (needed 0x%08x, got 0x%08x)",
1015 sb_block = le64_to_cpu(sb->sb_block);
1016 if (sb_block != (u64)dsb->zone->id << zmd->zone_nr_blocks_shift) {
1017 dmz_dev_err(dev, "Invalid superblock position (is %llu expected %llu)",
1018 sb_block, (u64)dsb->zone->id << zmd->zone_nr_blocks_shift);
1021 if (zmd->sb_version > 1) {
1024 import_uuid(&sb_uuid, sb->dmz_uuid);
1025 if (uuid_is_null(&sb_uuid)) {
1026 dmz_dev_err(dev, "NULL DM-Zoned uuid");
1028 } else if (uuid_is_null(&zmd->uuid)) {
1029 uuid_copy(&zmd->uuid, &sb_uuid);
1030 } else if (!uuid_equal(&zmd->uuid, &sb_uuid)) {
1031 dmz_dev_err(dev, "mismatching DM-Zoned uuid, is %pUl expected %pUl",
1032 &sb_uuid, &zmd->uuid);
1035 if (!strlen(zmd->label))
1036 memcpy(zmd->label, sb->dmz_label, BDEVNAME_SIZE);
1037 else if (memcmp(zmd->label, sb->dmz_label, BDEVNAME_SIZE)) {
1038 dmz_dev_err(dev, "mismatching DM-Zoned label, is %s expected %s",
1039 sb->dmz_label, zmd->label);
1042 import_uuid(&dev->uuid, sb->dev_uuid);
1043 if (uuid_is_null(&dev->uuid)) {
1044 dmz_dev_err(dev, "NULL device uuid");
1050 * Generation number should be 0, but it doesn't
1051 * really matter if it isn't.
1054 dmz_dev_warn(dev, "Invalid generation %llu",
1060 nr_meta_zones = (le32_to_cpu(sb->nr_meta_blocks) + zmd->zone_nr_blocks - 1)
1061 >> zmd->zone_nr_blocks_shift;
1062 if (!nr_meta_zones ||
1063 (zmd->nr_devs <= 1 && nr_meta_zones >= zmd->nr_rnd_zones) ||
1064 (zmd->nr_devs > 1 && nr_meta_zones >= zmd->nr_cache_zones)) {
1065 dmz_dev_err(dev, "Invalid number of metadata blocks");
1069 if (!le32_to_cpu(sb->nr_reserved_seq) ||
1070 le32_to_cpu(sb->nr_reserved_seq) >= (zmd->nr_useable_zones - nr_meta_zones)) {
1071 dmz_dev_err(dev, "Invalid number of reserved sequential zones");
1075 nr_data_zones = zmd->nr_useable_zones -
1076 (nr_meta_zones * 2 + le32_to_cpu(sb->nr_reserved_seq));
1077 if (le32_to_cpu(sb->nr_chunks) > nr_data_zones) {
1078 dmz_dev_err(dev, "Invalid number of chunks %u / %u",
1079 le32_to_cpu(sb->nr_chunks), nr_data_zones);
1084 zmd->nr_meta_blocks = le32_to_cpu(sb->nr_meta_blocks);
1085 zmd->nr_reserved_seq = le32_to_cpu(sb->nr_reserved_seq);
1086 zmd->nr_chunks = le32_to_cpu(sb->nr_chunks);
1087 zmd->nr_map_blocks = le32_to_cpu(sb->nr_map_blocks);
1088 zmd->nr_bitmap_blocks = le32_to_cpu(sb->nr_bitmap_blocks);
1089 zmd->nr_meta_zones = nr_meta_zones;
1090 zmd->nr_data_zones = nr_data_zones;
1096 * Read the first or second super block from disk.
1098 static int dmz_read_sb(struct dmz_metadata *zmd, struct dmz_sb *sb, int set)
1100 dmz_zmd_debug(zmd, "read superblock set %d dev %pg block %llu",
1101 set, sb->dev->bdev, sb->block);
1103 return dmz_rdwr_block(sb->dev, REQ_OP_READ,
1104 sb->block, sb->mblk->page);
1108 * Determine the position of the secondary super blocks on disk.
1109 * This is used only if a corruption of the primary super block
1112 static int dmz_lookup_secondary_sb(struct dmz_metadata *zmd)
1114 unsigned int zone_nr_blocks = zmd->zone_nr_blocks;
1115 struct dmz_mblock *mblk;
1116 unsigned int zone_id = zmd->sb[0].zone->id;
1119 /* Allocate a block */
1120 mblk = dmz_alloc_mblock(zmd, 0);
1124 zmd->sb[1].mblk = mblk;
1125 zmd->sb[1].sb = mblk->data;
1127 /* Bad first super block: search for the second one */
1128 zmd->sb[1].block = zmd->sb[0].block + zone_nr_blocks;
1129 zmd->sb[1].zone = dmz_get(zmd, zone_id + 1);
1130 zmd->sb[1].dev = zmd->sb[0].dev;
1131 for (i = 1; i < zmd->nr_rnd_zones; i++) {
1132 if (dmz_read_sb(zmd, &zmd->sb[1], 1) != 0)
1134 if (le32_to_cpu(zmd->sb[1].sb->magic) == DMZ_MAGIC)
1136 zmd->sb[1].block += zone_nr_blocks;
1137 zmd->sb[1].zone = dmz_get(zmd, zone_id + i);
1140 dmz_free_mblock(zmd, mblk);
1141 zmd->sb[1].mblk = NULL;
1142 zmd->sb[1].zone = NULL;
1143 zmd->sb[1].dev = NULL;
1149 * Read a super block from disk.
1151 static int dmz_get_sb(struct dmz_metadata *zmd, struct dmz_sb *sb, int set)
1153 struct dmz_mblock *mblk;
1156 /* Allocate a block */
1157 mblk = dmz_alloc_mblock(zmd, 0);
1162 sb->sb = mblk->data;
1164 /* Read super block */
1165 ret = dmz_read_sb(zmd, sb, set);
1167 dmz_free_mblock(zmd, mblk);
1176 * Recover a metadata set.
1178 static int dmz_recover_mblocks(struct dmz_metadata *zmd, unsigned int dst_set)
1180 unsigned int src_set = dst_set ^ 0x1;
1184 dmz_dev_warn(zmd->sb[dst_set].dev,
1185 "Metadata set %u invalid: recovering", dst_set);
1188 zmd->sb[0].block = dmz_start_block(zmd, zmd->sb[0].zone);
1190 zmd->sb[1].block = dmz_start_block(zmd, zmd->sb[1].zone);
1192 page = alloc_page(GFP_NOIO);
1196 /* Copy metadata blocks */
1197 for (i = 1; i < zmd->nr_meta_blocks; i++) {
1198 ret = dmz_rdwr_block(zmd->sb[src_set].dev, REQ_OP_READ,
1199 zmd->sb[src_set].block + i, page);
1202 ret = dmz_rdwr_block(zmd->sb[dst_set].dev, REQ_OP_WRITE,
1203 zmd->sb[dst_set].block + i, page);
1208 /* Finalize with the super block */
1209 if (!zmd->sb[dst_set].mblk) {
1210 zmd->sb[dst_set].mblk = dmz_alloc_mblock(zmd, 0);
1211 if (!zmd->sb[dst_set].mblk) {
1215 zmd->sb[dst_set].sb = zmd->sb[dst_set].mblk->data;
1218 ret = dmz_write_sb(zmd, dst_set);
1220 __free_pages(page, 0);
1226 * Get super block from disk.
1228 static int dmz_load_sb(struct dmz_metadata *zmd)
1230 bool sb_good[2] = {false, false};
1231 u64 sb_gen[2] = {0, 0};
1234 if (!zmd->sb[0].zone) {
1235 dmz_zmd_err(zmd, "Primary super block zone not set");
1239 /* Read and check the primary super block */
1240 zmd->sb[0].block = dmz_start_block(zmd, zmd->sb[0].zone);
1241 zmd->sb[0].dev = zmd->sb[0].zone->dev;
1242 ret = dmz_get_sb(zmd, &zmd->sb[0], 0);
1244 dmz_dev_err(zmd->sb[0].dev, "Read primary super block failed");
1248 ret = dmz_check_sb(zmd, &zmd->sb[0], false);
1250 /* Read and check secondary super block */
1253 if (!zmd->sb[1].zone) {
1254 unsigned int zone_id =
1255 zmd->sb[0].zone->id + zmd->nr_meta_zones;
1257 zmd->sb[1].zone = dmz_get(zmd, zone_id);
1259 zmd->sb[1].block = dmz_start_block(zmd, zmd->sb[1].zone);
1260 zmd->sb[1].dev = zmd->sb[0].dev;
1261 ret = dmz_get_sb(zmd, &zmd->sb[1], 1);
1263 ret = dmz_lookup_secondary_sb(zmd);
1266 dmz_dev_err(zmd->sb[1].dev, "Read secondary super block failed");
1270 ret = dmz_check_sb(zmd, &zmd->sb[1], false);
1274 /* Use highest generation sb first */
1275 if (!sb_good[0] && !sb_good[1]) {
1276 dmz_zmd_err(zmd, "No valid super block found");
1281 sb_gen[0] = le64_to_cpu(zmd->sb[0].sb->gen);
1283 ret = dmz_recover_mblocks(zmd, 0);
1285 dmz_dev_err(zmd->sb[0].dev,
1286 "Recovery of superblock 0 failed");
1292 sb_gen[1] = le64_to_cpu(zmd->sb[1].sb->gen);
1294 ret = dmz_recover_mblocks(zmd, 1);
1297 dmz_dev_err(zmd->sb[1].dev,
1298 "Recovery of superblock 1 failed");
1303 if (sb_gen[0] >= sb_gen[1]) {
1304 zmd->sb_gen = sb_gen[0];
1305 zmd->mblk_primary = 0;
1307 zmd->sb_gen = sb_gen[1];
1308 zmd->mblk_primary = 1;
1311 dmz_dev_debug(zmd->sb[zmd->mblk_primary].dev,
1312 "Using super block %u (gen %llu)",
1313 zmd->mblk_primary, zmd->sb_gen);
1315 if (zmd->sb_version > 1) {
1319 sb = kzalloc(sizeof(struct dmz_sb), GFP_KERNEL);
1322 for (i = 1; i < zmd->nr_devs; i++) {
1324 sb->zone = dmz_get(zmd, zmd->dev[i].zone_offset);
1325 sb->dev = &zmd->dev[i];
1326 if (!dmz_is_meta(sb->zone)) {
1327 dmz_dev_err(sb->dev,
1328 "Tertiary super block zone %u not marked as metadata zone",
1333 ret = dmz_get_sb(zmd, sb, i + 1);
1335 dmz_dev_err(sb->dev,
1336 "Read tertiary super block failed");
1337 dmz_free_mblock(zmd, sb->mblk);
1340 ret = dmz_check_sb(zmd, sb, true);
1341 dmz_free_mblock(zmd, sb->mblk);
1352 * Initialize a zone descriptor.
1354 static int dmz_init_zone(struct blk_zone *blkz, unsigned int num, void *data)
1356 struct dmz_dev *dev = data;
1357 struct dmz_metadata *zmd = dev->metadata;
1358 int idx = num + dev->zone_offset;
1359 struct dm_zone *zone;
1361 zone = dmz_insert(zmd, idx, dev);
1363 return PTR_ERR(zone);
1365 if (blkz->len != zmd->zone_nr_sectors) {
1366 if (zmd->sb_version > 1) {
1367 /* Ignore the eventual runt (smaller) zone */
1368 set_bit(DMZ_OFFLINE, &zone->flags);
1370 } else if (blkz->start + blkz->len == dev->capacity)
1376 * Devices that have zones with a capacity smaller than the zone size
1377 * (e.g. NVMe zoned namespaces) are not supported.
1379 if (blkz->capacity != blkz->len)
1382 switch (blkz->type) {
1383 case BLK_ZONE_TYPE_CONVENTIONAL:
1384 set_bit(DMZ_RND, &zone->flags);
1386 case BLK_ZONE_TYPE_SEQWRITE_REQ:
1387 case BLK_ZONE_TYPE_SEQWRITE_PREF:
1388 set_bit(DMZ_SEQ, &zone->flags);
1394 if (dmz_is_rnd(zone))
1397 zone->wp_block = dmz_sect2blk(blkz->wp - blkz->start);
1399 if (blkz->cond == BLK_ZONE_COND_OFFLINE)
1400 set_bit(DMZ_OFFLINE, &zone->flags);
1401 else if (blkz->cond == BLK_ZONE_COND_READONLY)
1402 set_bit(DMZ_READ_ONLY, &zone->flags);
1404 zmd->nr_useable_zones++;
1405 if (dmz_is_rnd(zone)) {
1406 zmd->nr_rnd_zones++;
1407 if (zmd->nr_devs == 1 && !zmd->sb[0].zone) {
1408 /* Primary super block zone */
1409 zmd->sb[0].zone = zone;
1412 if (zmd->nr_devs > 1 && num == 0) {
1414 * Tertiary superblock zones are always at the
1415 * start of the zoned devices, so mark them
1418 set_bit(DMZ_META, &zone->flags);
1424 static int dmz_emulate_zones(struct dmz_metadata *zmd, struct dmz_dev *dev)
1427 sector_t zone_offset = 0;
1429 for (idx = 0; idx < dev->nr_zones; idx++) {
1430 struct dm_zone *zone;
1432 zone = dmz_insert(zmd, idx, dev);
1434 return PTR_ERR(zone);
1435 set_bit(DMZ_CACHE, &zone->flags);
1437 zmd->nr_cache_zones++;
1438 zmd->nr_useable_zones++;
1439 if (dev->capacity - zone_offset < zmd->zone_nr_sectors) {
1440 /* Disable runt zone */
1441 set_bit(DMZ_OFFLINE, &zone->flags);
1444 zone_offset += zmd->zone_nr_sectors;
1450 * Free zones descriptors.
1452 static void dmz_drop_zones(struct dmz_metadata *zmd)
1456 for (idx = 0; idx < zmd->nr_zones; idx++) {
1457 struct dm_zone *zone = xa_load(&zmd->zones, idx);
1460 xa_erase(&zmd->zones, idx);
1462 xa_destroy(&zmd->zones);
1466 * Allocate and initialize zone descriptors using the zone
1467 * information from disk.
1469 static int dmz_init_zones(struct dmz_metadata *zmd)
1472 struct dmz_dev *zoned_dev = &zmd->dev[0];
1475 zmd->zone_nr_sectors = zmd->dev[0].zone_nr_sectors;
1476 zmd->zone_nr_sectors_shift = ilog2(zmd->zone_nr_sectors);
1477 zmd->zone_nr_blocks = dmz_sect2blk(zmd->zone_nr_sectors);
1478 zmd->zone_nr_blocks_shift = ilog2(zmd->zone_nr_blocks);
1479 zmd->zone_bitmap_size = zmd->zone_nr_blocks >> 3;
1480 zmd->zone_nr_bitmap_blocks =
1481 max_t(sector_t, 1, zmd->zone_bitmap_size >> DMZ_BLOCK_SHIFT);
1482 zmd->zone_bits_per_mblk = min_t(sector_t, zmd->zone_nr_blocks,
1483 DMZ_BLOCK_SIZE_BITS);
1485 /* Allocate zone array */
1487 for (i = 0; i < zmd->nr_devs; i++) {
1488 struct dmz_dev *dev = &zmd->dev[i];
1490 dev->metadata = zmd;
1491 zmd->nr_zones += dev->nr_zones;
1493 atomic_set(&dev->unmap_nr_rnd, 0);
1494 INIT_LIST_HEAD(&dev->unmap_rnd_list);
1495 INIT_LIST_HEAD(&dev->map_rnd_list);
1497 atomic_set(&dev->unmap_nr_seq, 0);
1498 INIT_LIST_HEAD(&dev->unmap_seq_list);
1499 INIT_LIST_HEAD(&dev->map_seq_list);
1502 if (!zmd->nr_zones) {
1503 DMERR("(%s): No zones found", zmd->devname);
1506 xa_init(&zmd->zones);
1508 DMDEBUG("(%s): Using %zu B for zone information",
1509 zmd->devname, sizeof(struct dm_zone) * zmd->nr_zones);
1511 if (zmd->nr_devs > 1) {
1512 ret = dmz_emulate_zones(zmd, &zmd->dev[0]);
1514 DMDEBUG("(%s): Failed to emulate zones, error %d",
1516 dmz_drop_zones(zmd);
1521 * Primary superblock zone is always at zone 0 when multiple
1522 * drives are present.
1524 zmd->sb[0].zone = dmz_get(zmd, 0);
1526 for (i = 1; i < zmd->nr_devs; i++) {
1527 zoned_dev = &zmd->dev[i];
1529 ret = blkdev_report_zones(zoned_dev->bdev, 0,
1531 dmz_init_zone, zoned_dev);
1533 DMDEBUG("(%s): Failed to report zones, error %d",
1535 dmz_drop_zones(zmd);
1543 * Get zone information and initialize zone descriptors. At the same
1544 * time, determine where the super block should be: first block of the
1545 * first randomly writable zone.
1547 ret = blkdev_report_zones(zoned_dev->bdev, 0, BLK_ALL_ZONES,
1548 dmz_init_zone, zoned_dev);
1550 DMDEBUG("(%s): Failed to report zones, error %d",
1552 dmz_drop_zones(zmd);
1559 static int dmz_update_zone_cb(struct blk_zone *blkz, unsigned int idx,
1562 struct dm_zone *zone = data;
1564 clear_bit(DMZ_OFFLINE, &zone->flags);
1565 clear_bit(DMZ_READ_ONLY, &zone->flags);
1566 if (blkz->cond == BLK_ZONE_COND_OFFLINE)
1567 set_bit(DMZ_OFFLINE, &zone->flags);
1568 else if (blkz->cond == BLK_ZONE_COND_READONLY)
1569 set_bit(DMZ_READ_ONLY, &zone->flags);
1571 if (dmz_is_seq(zone))
1572 zone->wp_block = dmz_sect2blk(blkz->wp - blkz->start);
1579 * Update a zone information.
1581 static int dmz_update_zone(struct dmz_metadata *zmd, struct dm_zone *zone)
1583 struct dmz_dev *dev = zone->dev;
1584 unsigned int noio_flag;
1587 if (dev->flags & DMZ_BDEV_REGULAR)
1591 * Get zone information from disk. Since blkdev_report_zones() uses
1592 * GFP_KERNEL by default for memory allocations, set the per-task
1593 * PF_MEMALLOC_NOIO flag so that all allocations are done as if
1594 * GFP_NOIO was specified.
1596 noio_flag = memalloc_noio_save();
1597 ret = blkdev_report_zones(dev->bdev, dmz_start_sect(zmd, zone), 1,
1598 dmz_update_zone_cb, zone);
1599 memalloc_noio_restore(noio_flag);
1604 dmz_dev_err(dev, "Get zone %u report failed",
1606 dmz_check_bdev(dev);
1614 * Check a zone write pointer position when the zone is marked
1615 * with the sequential write error flag.
1617 static int dmz_handle_seq_write_err(struct dmz_metadata *zmd,
1618 struct dm_zone *zone)
1620 struct dmz_dev *dev = zone->dev;
1621 unsigned int wp = 0;
1624 wp = zone->wp_block;
1625 ret = dmz_update_zone(zmd, zone);
1629 dmz_dev_warn(dev, "Processing zone %u write error (zone wp %u/%u)",
1630 zone->id, zone->wp_block, wp);
1632 if (zone->wp_block < wp) {
1633 dmz_invalidate_blocks(zmd, zone, zone->wp_block,
1634 wp - zone->wp_block);
1641 * Reset a zone write pointer.
1643 static int dmz_reset_zone(struct dmz_metadata *zmd, struct dm_zone *zone)
1648 * Ignore offline zones, read only zones,
1649 * and conventional zones.
1651 if (dmz_is_offline(zone) ||
1652 dmz_is_readonly(zone) ||
1656 if (!dmz_is_empty(zone) || dmz_seq_write_err(zone)) {
1657 struct dmz_dev *dev = zone->dev;
1659 ret = blkdev_zone_mgmt(dev->bdev, REQ_OP_ZONE_RESET,
1660 dmz_start_sect(zmd, zone),
1661 zmd->zone_nr_sectors, GFP_NOIO);
1663 dmz_dev_err(dev, "Reset zone %u failed %d",
1669 /* Clear write error bit and rewind write pointer position */
1670 clear_bit(DMZ_SEQ_WRITE_ERR, &zone->flags);
1676 static void dmz_get_zone_weight(struct dmz_metadata *zmd, struct dm_zone *zone);
1679 * Initialize chunk mapping.
1681 static int dmz_load_mapping(struct dmz_metadata *zmd)
1683 struct dm_zone *dzone, *bzone;
1684 struct dmz_mblock *dmap_mblk = NULL;
1685 struct dmz_map *dmap;
1686 unsigned int i = 0, e = 0, chunk = 0;
1687 unsigned int dzone_id;
1688 unsigned int bzone_id;
1690 /* Metadata block array for the chunk mapping table */
1691 zmd->map_mblk = kcalloc(zmd->nr_map_blocks,
1692 sizeof(struct dmz_mblk *), GFP_KERNEL);
1696 /* Get chunk mapping table blocks and initialize zone mapping */
1697 while (chunk < zmd->nr_chunks) {
1699 /* Get mapping block */
1700 dmap_mblk = dmz_get_mblock(zmd, i + 1);
1701 if (IS_ERR(dmap_mblk))
1702 return PTR_ERR(dmap_mblk);
1703 zmd->map_mblk[i] = dmap_mblk;
1704 dmap = dmap_mblk->data;
1709 /* Check data zone */
1710 dzone_id = le32_to_cpu(dmap[e].dzone_id);
1711 if (dzone_id == DMZ_MAP_UNMAPPED)
1714 if (dzone_id >= zmd->nr_zones) {
1715 dmz_zmd_err(zmd, "Chunk %u mapping: invalid data zone ID %u",
1720 dzone = dmz_get(zmd, dzone_id);
1722 dmz_zmd_err(zmd, "Chunk %u mapping: data zone %u not present",
1726 set_bit(DMZ_DATA, &dzone->flags);
1727 dzone->chunk = chunk;
1728 dmz_get_zone_weight(zmd, dzone);
1730 if (dmz_is_cache(dzone))
1731 list_add_tail(&dzone->link, &zmd->map_cache_list);
1732 else if (dmz_is_rnd(dzone))
1733 list_add_tail(&dzone->link, &dzone->dev->map_rnd_list);
1735 list_add_tail(&dzone->link, &dzone->dev->map_seq_list);
1737 /* Check buffer zone */
1738 bzone_id = le32_to_cpu(dmap[e].bzone_id);
1739 if (bzone_id == DMZ_MAP_UNMAPPED)
1742 if (bzone_id >= zmd->nr_zones) {
1743 dmz_zmd_err(zmd, "Chunk %u mapping: invalid buffer zone ID %u",
1748 bzone = dmz_get(zmd, bzone_id);
1750 dmz_zmd_err(zmd, "Chunk %u mapping: buffer zone %u not present",
1754 if (!dmz_is_rnd(bzone) && !dmz_is_cache(bzone)) {
1755 dmz_zmd_err(zmd, "Chunk %u mapping: invalid buffer zone %u",
1760 set_bit(DMZ_DATA, &bzone->flags);
1761 set_bit(DMZ_BUF, &bzone->flags);
1762 bzone->chunk = chunk;
1763 bzone->bzone = dzone;
1764 dzone->bzone = bzone;
1765 dmz_get_zone_weight(zmd, bzone);
1766 if (dmz_is_cache(bzone))
1767 list_add_tail(&bzone->link, &zmd->map_cache_list);
1769 list_add_tail(&bzone->link, &bzone->dev->map_rnd_list);
1773 if (e >= DMZ_MAP_ENTRIES)
1778 * At this point, only meta zones and mapped data zones were
1779 * fully initialized. All remaining zones are unmapped data
1780 * zones. Finish initializing those here.
1782 for (i = 0; i < zmd->nr_zones; i++) {
1783 dzone = dmz_get(zmd, i);
1786 if (dmz_is_meta(dzone))
1788 if (dmz_is_offline(dzone))
1791 if (dmz_is_cache(dzone))
1793 else if (dmz_is_rnd(dzone))
1794 dzone->dev->nr_rnd++;
1796 dzone->dev->nr_seq++;
1798 if (dmz_is_data(dzone)) {
1799 /* Already initialized */
1803 /* Unmapped data zone */
1804 set_bit(DMZ_DATA, &dzone->flags);
1805 dzone->chunk = DMZ_MAP_UNMAPPED;
1806 if (dmz_is_cache(dzone)) {
1807 list_add_tail(&dzone->link, &zmd->unmap_cache_list);
1808 atomic_inc(&zmd->unmap_nr_cache);
1809 } else if (dmz_is_rnd(dzone)) {
1810 list_add_tail(&dzone->link,
1811 &dzone->dev->unmap_rnd_list);
1812 atomic_inc(&dzone->dev->unmap_nr_rnd);
1813 } else if (atomic_read(&zmd->nr_reserved_seq_zones) < zmd->nr_reserved_seq) {
1814 list_add_tail(&dzone->link, &zmd->reserved_seq_zones_list);
1815 set_bit(DMZ_RESERVED, &dzone->flags);
1816 atomic_inc(&zmd->nr_reserved_seq_zones);
1817 dzone->dev->nr_seq--;
1819 list_add_tail(&dzone->link,
1820 &dzone->dev->unmap_seq_list);
1821 atomic_inc(&dzone->dev->unmap_nr_seq);
1829 * Set a data chunk mapping.
1831 static void dmz_set_chunk_mapping(struct dmz_metadata *zmd, unsigned int chunk,
1832 unsigned int dzone_id, unsigned int bzone_id)
1834 struct dmz_mblock *dmap_mblk = zmd->map_mblk[chunk >> DMZ_MAP_ENTRIES_SHIFT];
1835 struct dmz_map *dmap = dmap_mblk->data;
1836 int map_idx = chunk & DMZ_MAP_ENTRIES_MASK;
1838 dmap[map_idx].dzone_id = cpu_to_le32(dzone_id);
1839 dmap[map_idx].bzone_id = cpu_to_le32(bzone_id);
1840 dmz_dirty_mblock(zmd, dmap_mblk);
1844 * The list of mapped zones is maintained in LRU order.
1845 * This rotates a zone at the end of its map list.
1847 static void __dmz_lru_zone(struct dmz_metadata *zmd, struct dm_zone *zone)
1849 if (list_empty(&zone->link))
1852 list_del_init(&zone->link);
1853 if (dmz_is_seq(zone)) {
1854 /* LRU rotate sequential zone */
1855 list_add_tail(&zone->link, &zone->dev->map_seq_list);
1856 } else if (dmz_is_cache(zone)) {
1857 /* LRU rotate cache zone */
1858 list_add_tail(&zone->link, &zmd->map_cache_list);
1860 /* LRU rotate random zone */
1861 list_add_tail(&zone->link, &zone->dev->map_rnd_list);
1866 * The list of mapped random zones is maintained
1867 * in LRU order. This rotates a zone at the end of the list.
1869 static void dmz_lru_zone(struct dmz_metadata *zmd, struct dm_zone *zone)
1871 __dmz_lru_zone(zmd, zone);
1873 __dmz_lru_zone(zmd, zone->bzone);
1877 * Wait for any zone to be freed.
1879 static void dmz_wait_for_free_zones(struct dmz_metadata *zmd)
1883 prepare_to_wait(&zmd->free_wq, &wait, TASK_UNINTERRUPTIBLE);
1884 dmz_unlock_map(zmd);
1885 dmz_unlock_metadata(zmd);
1887 io_schedule_timeout(HZ);
1889 dmz_lock_metadata(zmd);
1891 finish_wait(&zmd->free_wq, &wait);
1895 * Lock a zone for reclaim (set the zone RECLAIM bit).
1896 * Returns false if the zone cannot be locked or if it is already locked
1899 int dmz_lock_zone_reclaim(struct dm_zone *zone)
1901 /* Active zones cannot be reclaimed */
1902 if (dmz_is_active(zone))
1905 return !test_and_set_bit(DMZ_RECLAIM, &zone->flags);
1909 * Clear a zone reclaim flag.
1911 void dmz_unlock_zone_reclaim(struct dm_zone *zone)
1913 WARN_ON(dmz_is_active(zone));
1914 WARN_ON(!dmz_in_reclaim(zone));
1916 clear_bit_unlock(DMZ_RECLAIM, &zone->flags);
1917 smp_mb__after_atomic();
1918 wake_up_bit(&zone->flags, DMZ_RECLAIM);
1922 * Wait for a zone reclaim to complete.
1924 static void dmz_wait_for_reclaim(struct dmz_metadata *zmd, struct dm_zone *zone)
1926 dmz_unlock_map(zmd);
1927 dmz_unlock_metadata(zmd);
1928 set_bit(DMZ_RECLAIM_TERMINATE, &zone->flags);
1929 wait_on_bit_timeout(&zone->flags, DMZ_RECLAIM, TASK_UNINTERRUPTIBLE, HZ);
1930 clear_bit(DMZ_RECLAIM_TERMINATE, &zone->flags);
1931 dmz_lock_metadata(zmd);
1936 * Select a cache or random write zone for reclaim.
1938 static struct dm_zone *dmz_get_rnd_zone_for_reclaim(struct dmz_metadata *zmd,
1939 unsigned int idx, bool idle)
1941 struct dm_zone *dzone = NULL;
1942 struct dm_zone *zone, *maxw_z = NULL;
1943 struct list_head *zone_list;
1945 /* If we have cache zones select from the cache zone list */
1946 if (zmd->nr_cache) {
1947 zone_list = &zmd->map_cache_list;
1948 /* Try to relaim random zones, too, when idle */
1949 if (idle && list_empty(zone_list))
1950 zone_list = &zmd->dev[idx].map_rnd_list;
1952 zone_list = &zmd->dev[idx].map_rnd_list;
1955 * Find the buffer zone with the heaviest weight or the first (oldest)
1956 * data zone that can be reclaimed.
1958 list_for_each_entry(zone, zone_list, link) {
1959 if (dmz_is_buf(zone)) {
1960 dzone = zone->bzone;
1961 if (dmz_is_rnd(dzone) && dzone->dev->dev_idx != idx)
1963 if (!maxw_z || maxw_z->weight < dzone->weight)
1967 if (dmz_lock_zone_reclaim(dzone))
1972 if (maxw_z && dmz_lock_zone_reclaim(maxw_z))
1976 * If we come here, none of the zones inspected could be locked for
1977 * reclaim. Try again, being more aggressive, that is, find the
1978 * first zone that can be reclaimed regardless of its weitght.
1980 list_for_each_entry(zone, zone_list, link) {
1981 if (dmz_is_buf(zone)) {
1982 dzone = zone->bzone;
1983 if (dmz_is_rnd(dzone) && dzone->dev->dev_idx != idx)
1987 if (dmz_lock_zone_reclaim(dzone))
1995 * Select a buffered sequential zone for reclaim.
1997 static struct dm_zone *dmz_get_seq_zone_for_reclaim(struct dmz_metadata *zmd,
2000 struct dm_zone *zone;
2002 list_for_each_entry(zone, &zmd->dev[idx].map_seq_list, link) {
2005 if (dmz_lock_zone_reclaim(zone))
2013 * Select a zone for reclaim.
2015 struct dm_zone *dmz_get_zone_for_reclaim(struct dmz_metadata *zmd,
2016 unsigned int dev_idx, bool idle)
2018 struct dm_zone *zone = NULL;
2021 * Search for a zone candidate to reclaim: 2 cases are possible.
2022 * (1) There is no free sequential zones. Then a random data zone
2023 * cannot be reclaimed. So choose a sequential zone to reclaim so
2024 * that afterward a random zone can be reclaimed.
2025 * (2) At least one free sequential zone is available, then choose
2026 * the oldest random zone (data or buffer) that can be locked.
2029 if (list_empty(&zmd->reserved_seq_zones_list))
2030 zone = dmz_get_seq_zone_for_reclaim(zmd, dev_idx);
2032 zone = dmz_get_rnd_zone_for_reclaim(zmd, dev_idx, idle);
2033 dmz_unlock_map(zmd);
2039 * Get the zone mapping a chunk, if the chunk is mapped already.
2040 * If no mapping exist and the operation is WRITE, a zone is
2041 * allocated and used to map the chunk.
2042 * The zone returned will be set to the active state.
2044 struct dm_zone *dmz_get_chunk_mapping(struct dmz_metadata *zmd,
2045 unsigned int chunk, enum req_op op)
2047 struct dmz_mblock *dmap_mblk = zmd->map_mblk[chunk >> DMZ_MAP_ENTRIES_SHIFT];
2048 struct dmz_map *dmap = dmap_mblk->data;
2049 int dmap_idx = chunk & DMZ_MAP_ENTRIES_MASK;
2050 unsigned int dzone_id;
2051 struct dm_zone *dzone = NULL;
2053 int alloc_flags = zmd->nr_cache ? DMZ_ALLOC_CACHE : DMZ_ALLOC_RND;
2057 /* Get the chunk mapping */
2058 dzone_id = le32_to_cpu(dmap[dmap_idx].dzone_id);
2059 if (dzone_id == DMZ_MAP_UNMAPPED) {
2061 * Read or discard in unmapped chunks are fine. But for
2062 * writes, we need a mapping, so get one.
2064 if (op != REQ_OP_WRITE)
2067 /* Allocate a random zone */
2068 dzone = dmz_alloc_zone(zmd, 0, alloc_flags);
2070 if (dmz_dev_is_dying(zmd)) {
2071 dzone = ERR_PTR(-EIO);
2074 dmz_wait_for_free_zones(zmd);
2078 dmz_map_zone(zmd, dzone, chunk);
2081 /* The chunk is already mapped: get the mapping zone */
2082 dzone = dmz_get(zmd, dzone_id);
2084 dzone = ERR_PTR(-EIO);
2087 if (dzone->chunk != chunk) {
2088 dzone = ERR_PTR(-EIO);
2092 /* Repair write pointer if the sequential dzone has error */
2093 if (dmz_seq_write_err(dzone)) {
2094 ret = dmz_handle_seq_write_err(zmd, dzone);
2096 dzone = ERR_PTR(-EIO);
2099 clear_bit(DMZ_SEQ_WRITE_ERR, &dzone->flags);
2104 * If the zone is being reclaimed, the chunk mapping may change
2105 * to a different zone. So wait for reclaim and retry. Otherwise,
2106 * activate the zone (this will prevent reclaim from touching it).
2108 if (dmz_in_reclaim(dzone)) {
2109 dmz_wait_for_reclaim(zmd, dzone);
2112 dmz_activate_zone(dzone);
2113 dmz_lru_zone(zmd, dzone);
2115 dmz_unlock_map(zmd);
2121 * Write and discard change the block validity of data zones and their buffer
2122 * zones. Check here that valid blocks are still present. If all blocks are
2123 * invalid, the zones can be unmapped on the fly without waiting for reclaim
2126 void dmz_put_chunk_mapping(struct dmz_metadata *zmd, struct dm_zone *dzone)
2128 struct dm_zone *bzone;
2132 bzone = dzone->bzone;
2134 if (dmz_weight(bzone))
2135 dmz_lru_zone(zmd, bzone);
2137 /* Empty buffer zone: reclaim it */
2138 dmz_unmap_zone(zmd, bzone);
2139 dmz_free_zone(zmd, bzone);
2144 /* Deactivate the data zone */
2145 dmz_deactivate_zone(dzone);
2146 if (dmz_is_active(dzone) || bzone || dmz_weight(dzone))
2147 dmz_lru_zone(zmd, dzone);
2149 /* Unbuffered inactive empty data zone: reclaim it */
2150 dmz_unmap_zone(zmd, dzone);
2151 dmz_free_zone(zmd, dzone);
2154 dmz_unlock_map(zmd);
2158 * Allocate and map a random zone to buffer a chunk
2159 * already mapped to a sequential zone.
2161 struct dm_zone *dmz_get_chunk_buffer(struct dmz_metadata *zmd,
2162 struct dm_zone *dzone)
2164 struct dm_zone *bzone;
2165 int alloc_flags = zmd->nr_cache ? DMZ_ALLOC_CACHE : DMZ_ALLOC_RND;
2169 bzone = dzone->bzone;
2173 /* Allocate a random zone */
2174 bzone = dmz_alloc_zone(zmd, 0, alloc_flags);
2176 if (dmz_dev_is_dying(zmd)) {
2177 bzone = ERR_PTR(-EIO);
2180 dmz_wait_for_free_zones(zmd);
2184 /* Update the chunk mapping */
2185 dmz_set_chunk_mapping(zmd, dzone->chunk, dzone->id, bzone->id);
2187 set_bit(DMZ_BUF, &bzone->flags);
2188 bzone->chunk = dzone->chunk;
2189 bzone->bzone = dzone;
2190 dzone->bzone = bzone;
2191 if (dmz_is_cache(bzone))
2192 list_add_tail(&bzone->link, &zmd->map_cache_list);
2194 list_add_tail(&bzone->link, &bzone->dev->map_rnd_list);
2196 dmz_unlock_map(zmd);
2202 * Get an unmapped (free) zone.
2203 * This must be called with the mapping lock held.
2205 struct dm_zone *dmz_alloc_zone(struct dmz_metadata *zmd, unsigned int dev_idx,
2206 unsigned long flags)
2208 struct list_head *list;
2209 struct dm_zone *zone;
2212 /* Schedule reclaim to ensure free zones are available */
2213 if (!(flags & DMZ_ALLOC_RECLAIM)) {
2214 for (i = 0; i < zmd->nr_devs; i++)
2215 dmz_schedule_reclaim(zmd->dev[i].reclaim);
2220 if (flags & DMZ_ALLOC_CACHE)
2221 list = &zmd->unmap_cache_list;
2222 else if (flags & DMZ_ALLOC_RND)
2223 list = &zmd->dev[dev_idx].unmap_rnd_list;
2225 list = &zmd->dev[dev_idx].unmap_seq_list;
2227 if (list_empty(list)) {
2229 * No free zone: return NULL if this is for not reclaim.
2231 if (!(flags & DMZ_ALLOC_RECLAIM))
2234 * Try to allocate from other devices
2236 if (i < zmd->nr_devs) {
2237 dev_idx = (dev_idx + 1) % zmd->nr_devs;
2243 * Fallback to the reserved sequential zones
2245 zone = list_first_entry_or_null(&zmd->reserved_seq_zones_list,
2246 struct dm_zone, link);
2248 list_del_init(&zone->link);
2249 atomic_dec(&zmd->nr_reserved_seq_zones);
2254 zone = list_first_entry(list, struct dm_zone, link);
2255 list_del_init(&zone->link);
2257 if (dmz_is_cache(zone))
2258 atomic_dec(&zmd->unmap_nr_cache);
2259 else if (dmz_is_rnd(zone))
2260 atomic_dec(&zone->dev->unmap_nr_rnd);
2262 atomic_dec(&zone->dev->unmap_nr_seq);
2264 if (dmz_is_offline(zone)) {
2265 dmz_zmd_warn(zmd, "Zone %u is offline", zone->id);
2269 if (dmz_is_meta(zone)) {
2270 dmz_zmd_warn(zmd, "Zone %u has metadata", zone->id);
2279 * This must be called with the mapping lock held.
2281 void dmz_free_zone(struct dmz_metadata *zmd, struct dm_zone *zone)
2283 /* If this is a sequential zone, reset it */
2284 if (dmz_is_seq(zone))
2285 dmz_reset_zone(zmd, zone);
2287 /* Return the zone to its type unmap list */
2288 if (dmz_is_cache(zone)) {
2289 list_add_tail(&zone->link, &zmd->unmap_cache_list);
2290 atomic_inc(&zmd->unmap_nr_cache);
2291 } else if (dmz_is_rnd(zone)) {
2292 list_add_tail(&zone->link, &zone->dev->unmap_rnd_list);
2293 atomic_inc(&zone->dev->unmap_nr_rnd);
2294 } else if (dmz_is_reserved(zone)) {
2295 list_add_tail(&zone->link, &zmd->reserved_seq_zones_list);
2296 atomic_inc(&zmd->nr_reserved_seq_zones);
2298 list_add_tail(&zone->link, &zone->dev->unmap_seq_list);
2299 atomic_inc(&zone->dev->unmap_nr_seq);
2302 wake_up_all(&zmd->free_wq);
2306 * Map a chunk to a zone.
2307 * This must be called with the mapping lock held.
2309 void dmz_map_zone(struct dmz_metadata *zmd, struct dm_zone *dzone,
2312 /* Set the chunk mapping */
2313 dmz_set_chunk_mapping(zmd, chunk, dzone->id,
2315 dzone->chunk = chunk;
2316 if (dmz_is_cache(dzone))
2317 list_add_tail(&dzone->link, &zmd->map_cache_list);
2318 else if (dmz_is_rnd(dzone))
2319 list_add_tail(&dzone->link, &dzone->dev->map_rnd_list);
2321 list_add_tail(&dzone->link, &dzone->dev->map_seq_list);
2326 * This must be called with the mapping lock held.
2328 void dmz_unmap_zone(struct dmz_metadata *zmd, struct dm_zone *zone)
2330 unsigned int chunk = zone->chunk;
2331 unsigned int dzone_id;
2333 if (chunk == DMZ_MAP_UNMAPPED) {
2334 /* Already unmapped */
2338 if (test_and_clear_bit(DMZ_BUF, &zone->flags)) {
2340 * Unmapping the chunk buffer zone: clear only
2341 * the chunk buffer mapping
2343 dzone_id = zone->bzone->id;
2344 zone->bzone->bzone = NULL;
2349 * Unmapping the chunk data zone: the zone must
2352 if (WARN_ON(zone->bzone)) {
2353 zone->bzone->bzone = NULL;
2356 dzone_id = DMZ_MAP_UNMAPPED;
2359 dmz_set_chunk_mapping(zmd, chunk, dzone_id, DMZ_MAP_UNMAPPED);
2361 zone->chunk = DMZ_MAP_UNMAPPED;
2362 list_del_init(&zone->link);
2366 * Set @nr_bits bits in @bitmap starting from @bit.
2367 * Return the number of bits changed from 0 to 1.
2369 static unsigned int dmz_set_bits(unsigned long *bitmap,
2370 unsigned int bit, unsigned int nr_bits)
2372 unsigned long *addr;
2373 unsigned int end = bit + nr_bits;
2377 if (((bit & (BITS_PER_LONG - 1)) == 0) &&
2378 ((end - bit) >= BITS_PER_LONG)) {
2379 /* Try to set the whole word at once */
2380 addr = bitmap + BIT_WORD(bit);
2384 bit += BITS_PER_LONG;
2389 if (!test_and_set_bit(bit, bitmap))
2398 * Get the bitmap block storing the bit for chunk_block in zone.
2400 static struct dmz_mblock *dmz_get_bitmap(struct dmz_metadata *zmd,
2401 struct dm_zone *zone,
2402 sector_t chunk_block)
2404 sector_t bitmap_block = 1 + zmd->nr_map_blocks +
2405 (sector_t)(zone->id * zmd->zone_nr_bitmap_blocks) +
2406 (chunk_block >> DMZ_BLOCK_SHIFT_BITS);
2408 return dmz_get_mblock(zmd, bitmap_block);
2412 * Copy the valid blocks bitmap of from_zone to the bitmap of to_zone.
2414 int dmz_copy_valid_blocks(struct dmz_metadata *zmd, struct dm_zone *from_zone,
2415 struct dm_zone *to_zone)
2417 struct dmz_mblock *from_mblk, *to_mblk;
2418 sector_t chunk_block = 0;
2420 /* Get the zones bitmap blocks */
2421 while (chunk_block < zmd->zone_nr_blocks) {
2422 from_mblk = dmz_get_bitmap(zmd, from_zone, chunk_block);
2423 if (IS_ERR(from_mblk))
2424 return PTR_ERR(from_mblk);
2425 to_mblk = dmz_get_bitmap(zmd, to_zone, chunk_block);
2426 if (IS_ERR(to_mblk)) {
2427 dmz_release_mblock(zmd, from_mblk);
2428 return PTR_ERR(to_mblk);
2431 memcpy(to_mblk->data, from_mblk->data, DMZ_BLOCK_SIZE);
2432 dmz_dirty_mblock(zmd, to_mblk);
2434 dmz_release_mblock(zmd, to_mblk);
2435 dmz_release_mblock(zmd, from_mblk);
2437 chunk_block += zmd->zone_bits_per_mblk;
2440 to_zone->weight = from_zone->weight;
2446 * Merge the valid blocks bitmap of from_zone into the bitmap of to_zone,
2447 * starting from chunk_block.
2449 int dmz_merge_valid_blocks(struct dmz_metadata *zmd, struct dm_zone *from_zone,
2450 struct dm_zone *to_zone, sector_t chunk_block)
2452 unsigned int nr_blocks;
2455 /* Get the zones bitmap blocks */
2456 while (chunk_block < zmd->zone_nr_blocks) {
2457 /* Get a valid region from the source zone */
2458 ret = dmz_first_valid_block(zmd, from_zone, &chunk_block);
2463 ret = dmz_validate_blocks(zmd, to_zone, chunk_block, nr_blocks);
2467 chunk_block += nr_blocks;
2474 * Validate all the blocks in the range [block..block+nr_blocks-1].
2476 int dmz_validate_blocks(struct dmz_metadata *zmd, struct dm_zone *zone,
2477 sector_t chunk_block, unsigned int nr_blocks)
2479 unsigned int count, bit, nr_bits;
2480 unsigned int zone_nr_blocks = zmd->zone_nr_blocks;
2481 struct dmz_mblock *mblk;
2484 dmz_zmd_debug(zmd, "=> VALIDATE zone %u, block %llu, %u blocks",
2485 zone->id, (unsigned long long)chunk_block,
2488 WARN_ON(chunk_block + nr_blocks > zone_nr_blocks);
2491 /* Get bitmap block */
2492 mblk = dmz_get_bitmap(zmd, zone, chunk_block);
2494 return PTR_ERR(mblk);
2497 bit = chunk_block & DMZ_BLOCK_MASK_BITS;
2498 nr_bits = min(nr_blocks, zmd->zone_bits_per_mblk - bit);
2500 count = dmz_set_bits((unsigned long *)mblk->data, bit, nr_bits);
2502 dmz_dirty_mblock(zmd, mblk);
2505 dmz_release_mblock(zmd, mblk);
2507 nr_blocks -= nr_bits;
2508 chunk_block += nr_bits;
2511 if (likely(zone->weight + n <= zone_nr_blocks))
2514 dmz_zmd_warn(zmd, "Zone %u: weight %u should be <= %u",
2515 zone->id, zone->weight,
2516 zone_nr_blocks - n);
2517 zone->weight = zone_nr_blocks;
2524 * Clear nr_bits bits in bitmap starting from bit.
2525 * Return the number of bits cleared.
2527 static int dmz_clear_bits(unsigned long *bitmap, int bit, int nr_bits)
2529 unsigned long *addr;
2530 int end = bit + nr_bits;
2534 if (((bit & (BITS_PER_LONG - 1)) == 0) &&
2535 ((end - bit) >= BITS_PER_LONG)) {
2536 /* Try to clear whole word at once */
2537 addr = bitmap + BIT_WORD(bit);
2538 if (*addr == ULONG_MAX) {
2541 bit += BITS_PER_LONG;
2546 if (test_and_clear_bit(bit, bitmap))
2555 * Invalidate all the blocks in the range [block..block+nr_blocks-1].
2557 int dmz_invalidate_blocks(struct dmz_metadata *zmd, struct dm_zone *zone,
2558 sector_t chunk_block, unsigned int nr_blocks)
2560 unsigned int count, bit, nr_bits;
2561 struct dmz_mblock *mblk;
2564 dmz_zmd_debug(zmd, "=> INVALIDATE zone %u, block %llu, %u blocks",
2565 zone->id, (u64)chunk_block, nr_blocks);
2567 WARN_ON(chunk_block + nr_blocks > zmd->zone_nr_blocks);
2570 /* Get bitmap block */
2571 mblk = dmz_get_bitmap(zmd, zone, chunk_block);
2573 return PTR_ERR(mblk);
2576 bit = chunk_block & DMZ_BLOCK_MASK_BITS;
2577 nr_bits = min(nr_blocks, zmd->zone_bits_per_mblk - bit);
2579 count = dmz_clear_bits((unsigned long *)mblk->data,
2582 dmz_dirty_mblock(zmd, mblk);
2585 dmz_release_mblock(zmd, mblk);
2587 nr_blocks -= nr_bits;
2588 chunk_block += nr_bits;
2591 if (zone->weight >= n)
2594 dmz_zmd_warn(zmd, "Zone %u: weight %u should be >= %u",
2595 zone->id, zone->weight, n);
2603 * Get a block bit value.
2605 static int dmz_test_block(struct dmz_metadata *zmd, struct dm_zone *zone,
2606 sector_t chunk_block)
2608 struct dmz_mblock *mblk;
2611 WARN_ON(chunk_block >= zmd->zone_nr_blocks);
2613 /* Get bitmap block */
2614 mblk = dmz_get_bitmap(zmd, zone, chunk_block);
2616 return PTR_ERR(mblk);
2619 ret = test_bit(chunk_block & DMZ_BLOCK_MASK_BITS,
2620 (unsigned long *) mblk->data) != 0;
2622 dmz_release_mblock(zmd, mblk);
2628 * Return the number of blocks from chunk_block to the first block with a bit
2629 * value specified by set. Search at most nr_blocks blocks from chunk_block.
2631 static int dmz_to_next_set_block(struct dmz_metadata *zmd, struct dm_zone *zone,
2632 sector_t chunk_block, unsigned int nr_blocks,
2635 struct dmz_mblock *mblk;
2636 unsigned int bit, set_bit, nr_bits;
2637 unsigned int zone_bits = zmd->zone_bits_per_mblk;
2638 unsigned long *bitmap;
2641 WARN_ON(chunk_block + nr_blocks > zmd->zone_nr_blocks);
2644 /* Get bitmap block */
2645 mblk = dmz_get_bitmap(zmd, zone, chunk_block);
2647 return PTR_ERR(mblk);
2650 bitmap = (unsigned long *) mblk->data;
2651 bit = chunk_block & DMZ_BLOCK_MASK_BITS;
2652 nr_bits = min(nr_blocks, zone_bits - bit);
2654 set_bit = find_next_bit(bitmap, zone_bits, bit);
2656 set_bit = find_next_zero_bit(bitmap, zone_bits, bit);
2657 dmz_release_mblock(zmd, mblk);
2660 if (set_bit < zone_bits)
2663 nr_blocks -= nr_bits;
2664 chunk_block += nr_bits;
2671 * Test if chunk_block is valid. If it is, the number of consecutive
2672 * valid blocks from chunk_block will be returned.
2674 int dmz_block_valid(struct dmz_metadata *zmd, struct dm_zone *zone,
2675 sector_t chunk_block)
2679 valid = dmz_test_block(zmd, zone, chunk_block);
2683 /* The block is valid: get the number of valid blocks from block */
2684 return dmz_to_next_set_block(zmd, zone, chunk_block,
2685 zmd->zone_nr_blocks - chunk_block, 0);
2689 * Find the first valid block from @chunk_block in @zone.
2690 * If such a block is found, its number is returned using
2691 * @chunk_block and the total number of valid blocks from @chunk_block
2694 int dmz_first_valid_block(struct dmz_metadata *zmd, struct dm_zone *zone,
2695 sector_t *chunk_block)
2697 sector_t start_block = *chunk_block;
2700 ret = dmz_to_next_set_block(zmd, zone, start_block,
2701 zmd->zone_nr_blocks - start_block, 1);
2706 *chunk_block = start_block;
2708 return dmz_to_next_set_block(zmd, zone, start_block,
2709 zmd->zone_nr_blocks - start_block, 0);
2713 * Count the number of bits set starting from bit up to bit + nr_bits - 1.
2715 static int dmz_count_bits(void *bitmap, int bit, int nr_bits)
2717 unsigned long *addr;
2718 int end = bit + nr_bits;
2722 if (((bit & (BITS_PER_LONG - 1)) == 0) &&
2723 ((end - bit) >= BITS_PER_LONG)) {
2724 addr = (unsigned long *)bitmap + BIT_WORD(bit);
2725 if (*addr == ULONG_MAX) {
2727 bit += BITS_PER_LONG;
2732 if (test_bit(bit, bitmap))
2741 * Get a zone weight.
2743 static void dmz_get_zone_weight(struct dmz_metadata *zmd, struct dm_zone *zone)
2745 struct dmz_mblock *mblk;
2746 sector_t chunk_block = 0;
2747 unsigned int bit, nr_bits;
2748 unsigned int nr_blocks = zmd->zone_nr_blocks;
2753 /* Get bitmap block */
2754 mblk = dmz_get_bitmap(zmd, zone, chunk_block);
2760 /* Count bits in this block */
2761 bitmap = mblk->data;
2762 bit = chunk_block & DMZ_BLOCK_MASK_BITS;
2763 nr_bits = min(nr_blocks, zmd->zone_bits_per_mblk - bit);
2764 n += dmz_count_bits(bitmap, bit, nr_bits);
2766 dmz_release_mblock(zmd, mblk);
2768 nr_blocks -= nr_bits;
2769 chunk_block += nr_bits;
2776 * Cleanup the zoned metadata resources.
2778 static void dmz_cleanup_metadata(struct dmz_metadata *zmd)
2780 struct rb_root *root;
2781 struct dmz_mblock *mblk, *next;
2784 /* Release zone mapping resources */
2785 if (zmd->map_mblk) {
2786 for (i = 0; i < zmd->nr_map_blocks; i++)
2787 dmz_release_mblock(zmd, zmd->map_mblk[i]);
2788 kfree(zmd->map_mblk);
2789 zmd->map_mblk = NULL;
2792 /* Release super blocks */
2793 for (i = 0; i < 2; i++) {
2794 if (zmd->sb[i].mblk) {
2795 dmz_free_mblock(zmd, zmd->sb[i].mblk);
2796 zmd->sb[i].mblk = NULL;
2800 /* Free cached blocks */
2801 while (!list_empty(&zmd->mblk_dirty_list)) {
2802 mblk = list_first_entry(&zmd->mblk_dirty_list,
2803 struct dmz_mblock, link);
2804 dmz_zmd_warn(zmd, "mblock %llu still in dirty list (ref %u)",
2805 (u64)mblk->no, mblk->ref);
2806 list_del_init(&mblk->link);
2807 rb_erase(&mblk->node, &zmd->mblk_rbtree);
2808 dmz_free_mblock(zmd, mblk);
2811 while (!list_empty(&zmd->mblk_lru_list)) {
2812 mblk = list_first_entry(&zmd->mblk_lru_list,
2813 struct dmz_mblock, link);
2814 list_del_init(&mblk->link);
2815 rb_erase(&mblk->node, &zmd->mblk_rbtree);
2816 dmz_free_mblock(zmd, mblk);
2819 /* Sanity checks: the mblock rbtree should now be empty */
2820 root = &zmd->mblk_rbtree;
2821 rbtree_postorder_for_each_entry_safe(mblk, next, root, node) {
2822 dmz_zmd_warn(zmd, "mblock %llu ref %u still in rbtree",
2823 (u64)mblk->no, mblk->ref);
2825 dmz_free_mblock(zmd, mblk);
2828 /* Free the zone descriptors */
2829 dmz_drop_zones(zmd);
2831 mutex_destroy(&zmd->mblk_flush_lock);
2832 mutex_destroy(&zmd->map_lock);
2835 static void dmz_print_dev(struct dmz_metadata *zmd, int num)
2837 struct dmz_dev *dev = &zmd->dev[num];
2839 if (bdev_zoned_model(dev->bdev) == BLK_ZONED_NONE)
2840 dmz_dev_info(dev, "Regular block device");
2842 dmz_dev_info(dev, "Host-%s zoned block device",
2843 bdev_zoned_model(dev->bdev) == BLK_ZONED_HA ?
2844 "aware" : "managed");
2845 if (zmd->sb_version > 1) {
2846 sector_t sector_offset =
2847 dev->zone_offset << zmd->zone_nr_sectors_shift;
2849 dmz_dev_info(dev, " %llu 512-byte logical sectors (offset %llu)",
2850 (u64)dev->capacity, (u64)sector_offset);
2851 dmz_dev_info(dev, " %u zones of %llu 512-byte logical sectors (offset %llu)",
2852 dev->nr_zones, (u64)zmd->zone_nr_sectors,
2853 (u64)dev->zone_offset);
2855 dmz_dev_info(dev, " %llu 512-byte logical sectors",
2856 (u64)dev->capacity);
2857 dmz_dev_info(dev, " %u zones of %llu 512-byte logical sectors",
2858 dev->nr_zones, (u64)zmd->zone_nr_sectors);
2863 * Initialize the zoned metadata.
2865 int dmz_ctr_metadata(struct dmz_dev *dev, int num_dev,
2866 struct dmz_metadata **metadata,
2867 const char *devname)
2869 struct dmz_metadata *zmd;
2871 struct dm_zone *zone;
2874 zmd = kzalloc(sizeof(struct dmz_metadata), GFP_KERNEL);
2878 strcpy(zmd->devname, devname);
2880 zmd->nr_devs = num_dev;
2881 zmd->mblk_rbtree = RB_ROOT;
2882 init_rwsem(&zmd->mblk_sem);
2883 mutex_init(&zmd->mblk_flush_lock);
2884 spin_lock_init(&zmd->mblk_lock);
2885 INIT_LIST_HEAD(&zmd->mblk_lru_list);
2886 INIT_LIST_HEAD(&zmd->mblk_dirty_list);
2888 mutex_init(&zmd->map_lock);
2890 atomic_set(&zmd->unmap_nr_cache, 0);
2891 INIT_LIST_HEAD(&zmd->unmap_cache_list);
2892 INIT_LIST_HEAD(&zmd->map_cache_list);
2894 atomic_set(&zmd->nr_reserved_seq_zones, 0);
2895 INIT_LIST_HEAD(&zmd->reserved_seq_zones_list);
2897 init_waitqueue_head(&zmd->free_wq);
2899 /* Initialize zone descriptors */
2900 ret = dmz_init_zones(zmd);
2904 /* Get super block */
2905 ret = dmz_load_sb(zmd);
2909 /* Set metadata zones starting from sb_zone */
2910 for (i = 0; i < zmd->nr_meta_zones << 1; i++) {
2911 zone = dmz_get(zmd, zmd->sb[0].zone->id + i);
2914 "metadata zone %u not present", i);
2918 if (!dmz_is_rnd(zone) && !dmz_is_cache(zone)) {
2920 "metadata zone %d is not random", i);
2924 set_bit(DMZ_META, &zone->flags);
2926 /* Load mapping table */
2927 ret = dmz_load_mapping(zmd);
2932 * Cache size boundaries: allow at least 2 super blocks, the chunk map
2933 * blocks and enough blocks to be able to cache the bitmap blocks of
2934 * up to 16 zones when idle (min_nr_mblks). Otherwise, if busy, allow
2935 * the cache to add 512 more metadata blocks.
2937 zmd->min_nr_mblks = 2 + zmd->nr_map_blocks + zmd->zone_nr_bitmap_blocks * 16;
2938 zmd->max_nr_mblks = zmd->min_nr_mblks + 512;
2939 zmd->mblk_shrinker.count_objects = dmz_mblock_shrinker_count;
2940 zmd->mblk_shrinker.scan_objects = dmz_mblock_shrinker_scan;
2941 zmd->mblk_shrinker.seeks = DEFAULT_SEEKS;
2943 /* Metadata cache shrinker */
2944 ret = register_shrinker(&zmd->mblk_shrinker, "dm-zoned-meta:(%u:%u)",
2945 MAJOR(dev->bdev->bd_dev),
2946 MINOR(dev->bdev->bd_dev));
2948 dmz_zmd_err(zmd, "Register metadata cache shrinker failed");
2952 dmz_zmd_info(zmd, "DM-Zoned metadata version %d", zmd->sb_version);
2953 for (i = 0; i < zmd->nr_devs; i++)
2954 dmz_print_dev(zmd, i);
2956 dmz_zmd_info(zmd, " %u zones of %llu 512-byte logical sectors",
2957 zmd->nr_zones, (u64)zmd->zone_nr_sectors);
2958 dmz_zmd_debug(zmd, " %u metadata zones",
2959 zmd->nr_meta_zones * 2);
2960 dmz_zmd_debug(zmd, " %u data zones for %u chunks",
2961 zmd->nr_data_zones, zmd->nr_chunks);
2962 dmz_zmd_debug(zmd, " %u cache zones (%u unmapped)",
2963 zmd->nr_cache, atomic_read(&zmd->unmap_nr_cache));
2964 for (i = 0; i < zmd->nr_devs; i++) {
2965 dmz_zmd_debug(zmd, " %u random zones (%u unmapped)",
2966 dmz_nr_rnd_zones(zmd, i),
2967 dmz_nr_unmap_rnd_zones(zmd, i));
2968 dmz_zmd_debug(zmd, " %u sequential zones (%u unmapped)",
2969 dmz_nr_seq_zones(zmd, i),
2970 dmz_nr_unmap_seq_zones(zmd, i));
2972 dmz_zmd_debug(zmd, " %u reserved sequential data zones",
2973 zmd->nr_reserved_seq);
2974 dmz_zmd_debug(zmd, "Format:");
2975 dmz_zmd_debug(zmd, "%u metadata blocks per set (%u max cache)",
2976 zmd->nr_meta_blocks, zmd->max_nr_mblks);
2977 dmz_zmd_debug(zmd, " %u data zone mapping blocks",
2978 zmd->nr_map_blocks);
2979 dmz_zmd_debug(zmd, " %u bitmap blocks",
2980 zmd->nr_bitmap_blocks);
2986 dmz_cleanup_metadata(zmd);
2994 * Cleanup the zoned metadata resources.
2996 void dmz_dtr_metadata(struct dmz_metadata *zmd)
2998 unregister_shrinker(&zmd->mblk_shrinker);
2999 dmz_cleanup_metadata(zmd);
3004 * Check zone information on resume.
3006 int dmz_resume_metadata(struct dmz_metadata *zmd)
3008 struct dm_zone *zone;
3014 for (i = 0; i < zmd->nr_zones; i++) {
3015 zone = dmz_get(zmd, i);
3017 dmz_zmd_err(zmd, "Unable to get zone %u", i);
3020 wp_block = zone->wp_block;
3022 ret = dmz_update_zone(zmd, zone);
3024 dmz_zmd_err(zmd, "Broken zone %u", i);
3028 if (dmz_is_offline(zone)) {
3029 dmz_zmd_warn(zmd, "Zone %u is offline", i);
3033 /* Check write pointer */
3034 if (!dmz_is_seq(zone))
3036 else if (zone->wp_block != wp_block) {
3037 dmz_zmd_err(zmd, "Zone %u: Invalid wp (%llu / %llu)",
3038 i, (u64)zone->wp_block, (u64)wp_block);
3039 zone->wp_block = wp_block;
3040 dmz_invalidate_blocks(zmd, zone, zone->wp_block,
3041 zmd->zone_nr_blocks - zone->wp_block);