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>
12 #define DM_MSG_PREFIX "zoned"
14 #define DMZ_MIN_BIOS 8192
20 struct dmz_target *target;
27 * Chunk work descriptor.
29 struct dm_chunk_work {
30 struct work_struct work;
32 struct dmz_target *target;
34 struct bio_list bio_list;
45 /* Zoned block device information */
48 /* For metadata handling */
49 struct dmz_metadata *metadata;
52 struct dmz_reclaim *reclaim;
55 struct radix_tree_root chunk_rxtree;
56 struct workqueue_struct *chunk_wq;
57 struct mutex chunk_lock;
59 /* For cloned BIOs to zones */
60 struct bio_set bio_set;
63 spinlock_t flush_lock;
64 struct bio_list flush_list;
65 struct delayed_work flush_work;
66 struct workqueue_struct *flush_wq;
70 * Flush intervals (seconds).
72 #define DMZ_FLUSH_PERIOD (10 * HZ)
75 * Target BIO completion.
77 static inline void dmz_bio_endio(struct bio *bio, blk_status_t status)
79 struct dmz_bioctx *bioctx = dm_per_bio_data(bio, sizeof(struct dmz_bioctx));
81 if (status != BLK_STS_OK && bio->bi_status == BLK_STS_OK)
82 bio->bi_status = status;
83 if (bio->bi_status != BLK_STS_OK)
84 bioctx->target->dev->flags |= DMZ_CHECK_BDEV;
86 if (refcount_dec_and_test(&bioctx->ref)) {
87 struct dm_zone *zone = bioctx->zone;
90 if (bio->bi_status != BLK_STS_OK &&
91 bio_op(bio) == REQ_OP_WRITE &&
93 set_bit(DMZ_SEQ_WRITE_ERR, &zone->flags);
94 dmz_deactivate_zone(zone);
101 * Completion callback for an internally cloned target BIO. This terminates the
102 * target BIO when there are no more references to its context.
104 static void dmz_clone_endio(struct bio *clone)
106 struct dmz_bioctx *bioctx = clone->bi_private;
107 blk_status_t status = clone->bi_status;
110 dmz_bio_endio(bioctx->bio, status);
114 * Issue a clone of a target BIO. The clone may only partially process the
115 * original target BIO.
117 static int dmz_submit_bio(struct dmz_target *dmz, struct dm_zone *zone,
118 struct bio *bio, sector_t chunk_block,
119 unsigned int nr_blocks)
121 struct dmz_bioctx *bioctx = dm_per_bio_data(bio, sizeof(struct dmz_bioctx));
124 clone = bio_clone_fast(bio, GFP_NOIO, &dmz->bio_set);
128 bio_set_dev(clone, dmz->dev->bdev);
129 clone->bi_iter.bi_sector =
130 dmz_start_sect(dmz->metadata, zone) + dmz_blk2sect(chunk_block);
131 clone->bi_iter.bi_size = dmz_blk2sect(nr_blocks) << SECTOR_SHIFT;
132 clone->bi_end_io = dmz_clone_endio;
133 clone->bi_private = bioctx;
135 bio_advance(bio, clone->bi_iter.bi_size);
137 refcount_inc(&bioctx->ref);
138 generic_make_request(clone);
140 if (bio_op(bio) == REQ_OP_WRITE && dmz_is_seq(zone))
141 zone->wp_block += nr_blocks;
147 * Zero out pages of discarded blocks accessed by a read BIO.
149 static void dmz_handle_read_zero(struct dmz_target *dmz, struct bio *bio,
150 sector_t chunk_block, unsigned int nr_blocks)
152 unsigned int size = nr_blocks << DMZ_BLOCK_SHIFT;
154 /* Clear nr_blocks */
155 swap(bio->bi_iter.bi_size, size);
157 swap(bio->bi_iter.bi_size, size);
159 bio_advance(bio, size);
163 * Process a read BIO.
165 static int dmz_handle_read(struct dmz_target *dmz, struct dm_zone *zone,
168 sector_t chunk_block = dmz_chunk_block(dmz->dev, dmz_bio_block(bio));
169 unsigned int nr_blocks = dmz_bio_blocks(bio);
170 sector_t end_block = chunk_block + nr_blocks;
171 struct dm_zone *rzone, *bzone;
174 /* Read into unmapped chunks need only zeroing the BIO buffer */
180 dmz_dev_debug(dmz->dev, "READ chunk %llu -> %s zone %u, block %llu, %u blocks",
181 (unsigned long long)dmz_bio_chunk(dmz->dev, bio),
182 (dmz_is_rnd(zone) ? "RND" : "SEQ"),
183 dmz_id(dmz->metadata, zone),
184 (unsigned long long)chunk_block, nr_blocks);
186 /* Check block validity to determine the read location */
188 while (chunk_block < end_block) {
190 if (dmz_is_rnd(zone) || chunk_block < zone->wp_block) {
191 /* Test block validity in the data zone */
192 ret = dmz_block_valid(dmz->metadata, zone, chunk_block);
196 /* Read data zone blocks */
203 * No valid blocks found in the data zone.
204 * Check the buffer zone, if there is one.
206 if (!nr_blocks && bzone) {
207 ret = dmz_block_valid(dmz->metadata, bzone, chunk_block);
211 /* Read buffer zone blocks */
218 /* Valid blocks found: read them */
219 nr_blocks = min_t(unsigned int, nr_blocks, end_block - chunk_block);
220 ret = dmz_submit_bio(dmz, rzone, bio, chunk_block, nr_blocks);
223 chunk_block += nr_blocks;
225 /* No valid block: zeroout the current BIO block */
226 dmz_handle_read_zero(dmz, bio, chunk_block, 1);
235 * Write blocks directly in a data zone, at the write pointer.
236 * If a buffer zone is assigned, invalidate the blocks written
239 static int dmz_handle_direct_write(struct dmz_target *dmz,
240 struct dm_zone *zone, struct bio *bio,
241 sector_t chunk_block,
242 unsigned int nr_blocks)
244 struct dmz_metadata *zmd = dmz->metadata;
245 struct dm_zone *bzone = zone->bzone;
248 if (dmz_is_readonly(zone))
252 ret = dmz_submit_bio(dmz, zone, bio, chunk_block, nr_blocks);
257 * Validate the blocks in the data zone and invalidate
258 * in the buffer zone, if there is one.
260 ret = dmz_validate_blocks(zmd, zone, chunk_block, nr_blocks);
261 if (ret == 0 && bzone)
262 ret = dmz_invalidate_blocks(zmd, bzone, chunk_block, nr_blocks);
268 * Write blocks in the buffer zone of @zone.
269 * If no buffer zone is assigned yet, get one.
270 * Called with @zone write locked.
272 static int dmz_handle_buffered_write(struct dmz_target *dmz,
273 struct dm_zone *zone, struct bio *bio,
274 sector_t chunk_block,
275 unsigned int nr_blocks)
277 struct dmz_metadata *zmd = dmz->metadata;
278 struct dm_zone *bzone;
281 /* Get the buffer zone. One will be allocated if needed */
282 bzone = dmz_get_chunk_buffer(zmd, zone);
284 return PTR_ERR(bzone);
286 if (dmz_is_readonly(bzone))
290 ret = dmz_submit_bio(dmz, bzone, bio, chunk_block, nr_blocks);
295 * Validate the blocks in the buffer zone
296 * and invalidate in the data zone.
298 ret = dmz_validate_blocks(zmd, bzone, chunk_block, nr_blocks);
299 if (ret == 0 && chunk_block < zone->wp_block)
300 ret = dmz_invalidate_blocks(zmd, zone, chunk_block, nr_blocks);
306 * Process a write BIO.
308 static int dmz_handle_write(struct dmz_target *dmz, struct dm_zone *zone,
311 sector_t chunk_block = dmz_chunk_block(dmz->dev, dmz_bio_block(bio));
312 unsigned int nr_blocks = dmz_bio_blocks(bio);
317 dmz_dev_debug(dmz->dev, "WRITE chunk %llu -> %s zone %u, block %llu, %u blocks",
318 (unsigned long long)dmz_bio_chunk(dmz->dev, bio),
319 (dmz_is_rnd(zone) ? "RND" : "SEQ"),
320 dmz_id(dmz->metadata, zone),
321 (unsigned long long)chunk_block, nr_blocks);
323 if (dmz_is_rnd(zone) || chunk_block == zone->wp_block) {
325 * zone is a random zone or it is a sequential zone
326 * and the BIO is aligned to the zone write pointer:
327 * direct write the zone.
329 return dmz_handle_direct_write(dmz, zone, bio, chunk_block, nr_blocks);
333 * This is an unaligned write in a sequential zone:
334 * use buffered write.
336 return dmz_handle_buffered_write(dmz, zone, bio, chunk_block, nr_blocks);
340 * Process a discard BIO.
342 static int dmz_handle_discard(struct dmz_target *dmz, struct dm_zone *zone,
345 struct dmz_metadata *zmd = dmz->metadata;
346 sector_t block = dmz_bio_block(bio);
347 unsigned int nr_blocks = dmz_bio_blocks(bio);
348 sector_t chunk_block = dmz_chunk_block(dmz->dev, block);
351 /* For unmapped chunks, there is nothing to do */
355 if (dmz_is_readonly(zone))
358 dmz_dev_debug(dmz->dev, "DISCARD chunk %llu -> zone %u, block %llu, %u blocks",
359 (unsigned long long)dmz_bio_chunk(dmz->dev, bio),
361 (unsigned long long)chunk_block, nr_blocks);
364 * Invalidate blocks in the data zone and its
365 * buffer zone if one is mapped.
367 if (dmz_is_rnd(zone) || chunk_block < zone->wp_block)
368 ret = dmz_invalidate_blocks(zmd, zone, chunk_block, nr_blocks);
369 if (ret == 0 && zone->bzone)
370 ret = dmz_invalidate_blocks(zmd, zone->bzone,
371 chunk_block, nr_blocks);
378 static void dmz_handle_bio(struct dmz_target *dmz, struct dm_chunk_work *cw,
381 struct dmz_bioctx *bioctx = dm_per_bio_data(bio, sizeof(struct dmz_bioctx));
382 struct dmz_metadata *zmd = dmz->metadata;
383 struct dm_zone *zone;
387 * Write may trigger a zone allocation. So make sure the
388 * allocation can succeed.
390 if (bio_op(bio) == REQ_OP_WRITE)
391 dmz_schedule_reclaim(dmz->reclaim);
393 dmz_lock_metadata(zmd);
395 if (dmz->dev->flags & DMZ_BDEV_DYING) {
401 * Get the data zone mapping the chunk. There may be no
402 * mapping for read and discard. If a mapping is obtained,
403 + the zone returned will be set to active state.
405 zone = dmz_get_chunk_mapping(zmd, dmz_bio_chunk(dmz->dev, bio),
412 /* Process the BIO */
414 dmz_activate_zone(zone);
418 switch (bio_op(bio)) {
420 ret = dmz_handle_read(dmz, zone, bio);
423 ret = dmz_handle_write(dmz, zone, bio);
426 case REQ_OP_WRITE_ZEROES:
427 ret = dmz_handle_discard(dmz, zone, bio);
430 dmz_dev_err(dmz->dev, "Unsupported BIO operation 0x%x",
436 * Release the chunk mapping. This will check that the mapping
437 * is still valid, that is, that the zone used still has valid blocks.
440 dmz_put_chunk_mapping(zmd, zone);
442 dmz_bio_endio(bio, errno_to_blk_status(ret));
444 dmz_unlock_metadata(zmd);
448 * Increment a chunk reference counter.
450 static inline void dmz_get_chunk_work(struct dm_chunk_work *cw)
452 refcount_inc(&cw->refcount);
456 * Decrement a chunk work reference count and
457 * free it if it becomes 0.
459 static void dmz_put_chunk_work(struct dm_chunk_work *cw)
461 if (refcount_dec_and_test(&cw->refcount)) {
462 WARN_ON(!bio_list_empty(&cw->bio_list));
463 radix_tree_delete(&cw->target->chunk_rxtree, cw->chunk);
469 * Chunk BIO work function.
471 static void dmz_chunk_work(struct work_struct *work)
473 struct dm_chunk_work *cw = container_of(work, struct dm_chunk_work, work);
474 struct dmz_target *dmz = cw->target;
477 mutex_lock(&dmz->chunk_lock);
479 /* Process the chunk BIOs */
480 while ((bio = bio_list_pop(&cw->bio_list))) {
481 mutex_unlock(&dmz->chunk_lock);
482 dmz_handle_bio(dmz, cw, bio);
483 mutex_lock(&dmz->chunk_lock);
484 dmz_put_chunk_work(cw);
487 /* Queueing the work incremented the work refcount */
488 dmz_put_chunk_work(cw);
490 mutex_unlock(&dmz->chunk_lock);
496 static void dmz_flush_work(struct work_struct *work)
498 struct dmz_target *dmz = container_of(work, struct dmz_target, flush_work.work);
502 /* Flush dirty metadata blocks */
503 ret = dmz_flush_metadata(dmz->metadata);
505 dmz_dev_debug(dmz->dev, "Metadata flush failed, rc=%d\n", ret);
507 /* Process queued flush requests */
509 spin_lock(&dmz->flush_lock);
510 bio = bio_list_pop(&dmz->flush_list);
511 spin_unlock(&dmz->flush_lock);
516 dmz_bio_endio(bio, errno_to_blk_status(ret));
519 queue_delayed_work(dmz->flush_wq, &dmz->flush_work, DMZ_FLUSH_PERIOD);
523 * Get a chunk work and start it to process a new BIO.
524 * If the BIO chunk has no work yet, create one.
526 static int dmz_queue_chunk_work(struct dmz_target *dmz, struct bio *bio)
528 unsigned int chunk = dmz_bio_chunk(dmz->dev, bio);
529 struct dm_chunk_work *cw;
532 mutex_lock(&dmz->chunk_lock);
534 /* Get the BIO chunk work. If one is not active yet, create one */
535 cw = radix_tree_lookup(&dmz->chunk_rxtree, chunk);
537 dmz_get_chunk_work(cw);
539 /* Create a new chunk work */
540 cw = kmalloc(sizeof(struct dm_chunk_work), GFP_NOIO);
546 INIT_WORK(&cw->work, dmz_chunk_work);
547 refcount_set(&cw->refcount, 1);
550 bio_list_init(&cw->bio_list);
552 ret = radix_tree_insert(&dmz->chunk_rxtree, chunk, cw);
559 bio_list_add(&cw->bio_list, bio);
561 dmz_reclaim_bio_acc(dmz->reclaim);
562 if (queue_work(dmz->chunk_wq, &cw->work))
563 dmz_get_chunk_work(cw);
565 mutex_unlock(&dmz->chunk_lock);
570 * Check if the backing device is being removed. If it's on the way out,
571 * start failing I/O. Reclaim and metadata components also call this
572 * function to cleanly abort operation in the event of such failure.
574 bool dmz_bdev_is_dying(struct dmz_dev *dmz_dev)
576 if (dmz_dev->flags & DMZ_BDEV_DYING)
579 if (dmz_dev->flags & DMZ_CHECK_BDEV)
580 return !dmz_check_bdev(dmz_dev);
582 if (blk_queue_dying(bdev_get_queue(dmz_dev->bdev))) {
583 dmz_dev_warn(dmz_dev, "Backing device queue dying");
584 dmz_dev->flags |= DMZ_BDEV_DYING;
587 return dmz_dev->flags & DMZ_BDEV_DYING;
591 * Check the backing device availability. This detects such events as
592 * backing device going offline due to errors, media removals, etc.
593 * This check is less efficient than dmz_bdev_is_dying() and should
594 * only be performed as a part of error handling.
596 bool dmz_check_bdev(struct dmz_dev *dmz_dev)
598 struct gendisk *disk;
600 dmz_dev->flags &= ~DMZ_CHECK_BDEV;
602 if (dmz_bdev_is_dying(dmz_dev))
605 disk = dmz_dev->bdev->bd_disk;
606 if (disk->fops->check_events &&
607 disk->fops->check_events(disk, 0) & DISK_EVENT_MEDIA_CHANGE) {
608 dmz_dev_warn(dmz_dev, "Backing device offline");
609 dmz_dev->flags |= DMZ_BDEV_DYING;
612 return !(dmz_dev->flags & DMZ_BDEV_DYING);
618 static int dmz_map(struct dm_target *ti, struct bio *bio)
620 struct dmz_target *dmz = ti->private;
621 struct dmz_dev *dev = dmz->dev;
622 struct dmz_bioctx *bioctx = dm_per_bio_data(bio, sizeof(struct dmz_bioctx));
623 sector_t sector = bio->bi_iter.bi_sector;
624 unsigned int nr_sectors = bio_sectors(bio);
625 sector_t chunk_sector;
628 if (dmz_bdev_is_dying(dmz->dev))
629 return DM_MAPIO_KILL;
631 dmz_dev_debug(dev, "BIO op %d sector %llu + %u => chunk %llu, block %llu, %u blocks",
632 bio_op(bio), (unsigned long long)sector, nr_sectors,
633 (unsigned long long)dmz_bio_chunk(dmz->dev, bio),
634 (unsigned long long)dmz_chunk_block(dmz->dev, dmz_bio_block(bio)),
635 (unsigned int)dmz_bio_blocks(bio));
637 bio_set_dev(bio, dev->bdev);
639 if (!nr_sectors && bio_op(bio) != REQ_OP_WRITE)
640 return DM_MAPIO_REMAPPED;
642 /* The BIO should be block aligned */
643 if ((nr_sectors & DMZ_BLOCK_SECTORS_MASK) || (sector & DMZ_BLOCK_SECTORS_MASK))
644 return DM_MAPIO_KILL;
646 /* Initialize the BIO context */
647 bioctx->target = dmz;
650 refcount_set(&bioctx->ref, 1);
652 /* Set the BIO pending in the flush list */
653 if (!nr_sectors && bio_op(bio) == REQ_OP_WRITE) {
654 spin_lock(&dmz->flush_lock);
655 bio_list_add(&dmz->flush_list, bio);
656 spin_unlock(&dmz->flush_lock);
657 mod_delayed_work(dmz->flush_wq, &dmz->flush_work, 0);
658 return DM_MAPIO_SUBMITTED;
661 /* Split zone BIOs to fit entirely into a zone */
662 chunk_sector = sector & (dev->zone_nr_sectors - 1);
663 if (chunk_sector + nr_sectors > dev->zone_nr_sectors)
664 dm_accept_partial_bio(bio, dev->zone_nr_sectors - chunk_sector);
666 /* Now ready to handle this BIO */
667 ret = dmz_queue_chunk_work(dmz, bio);
669 dmz_dev_debug(dmz->dev,
670 "BIO op %d, can't process chunk %llu, err %i\n",
671 bio_op(bio), (u64)dmz_bio_chunk(dmz->dev, bio),
673 return DM_MAPIO_REQUEUE;
676 return DM_MAPIO_SUBMITTED;
680 * Get zoned device information.
682 static int dmz_get_zoned_device(struct dm_target *ti, char *path)
684 struct dmz_target *dmz = ti->private;
685 struct request_queue *q;
687 sector_t aligned_capacity;
690 /* Get the target device */
691 ret = dm_get_device(ti, path, dm_table_get_mode(ti->table), &dmz->ddev);
693 ti->error = "Get target device failed";
698 dev = kzalloc(sizeof(struct dmz_dev), GFP_KERNEL);
704 dev->bdev = dmz->ddev->bdev;
705 (void)bdevname(dev->bdev, dev->name);
707 if (bdev_zoned_model(dev->bdev) == BLK_ZONED_NONE) {
708 ti->error = "Not a zoned block device";
713 q = bdev_get_queue(dev->bdev);
714 dev->capacity = i_size_read(dev->bdev->bd_inode) >> SECTOR_SHIFT;
715 aligned_capacity = dev->capacity &
716 ~((sector_t)blk_queue_zone_sectors(q) - 1);
718 ((ti->len != dev->capacity) && (ti->len != aligned_capacity))) {
719 ti->error = "Partial mapping not supported";
724 dev->zone_nr_sectors = blk_queue_zone_sectors(q);
725 dev->zone_nr_sectors_shift = ilog2(dev->zone_nr_sectors);
727 dev->zone_nr_blocks = dmz_sect2blk(dev->zone_nr_sectors);
728 dev->zone_nr_blocks_shift = ilog2(dev->zone_nr_blocks);
730 dev->nr_zones = blkdev_nr_zones(dev->bdev);
736 dm_put_device(ti, dmz->ddev);
743 * Cleanup zoned device information.
745 static void dmz_put_zoned_device(struct dm_target *ti)
747 struct dmz_target *dmz = ti->private;
749 dm_put_device(ti, dmz->ddev);
757 static int dmz_ctr(struct dm_target *ti, unsigned int argc, char **argv)
759 struct dmz_target *dmz;
763 /* Check arguments */
765 ti->error = "Invalid argument count";
769 /* Allocate and initialize the target descriptor */
770 dmz = kzalloc(sizeof(struct dmz_target), GFP_KERNEL);
772 ti->error = "Unable to allocate the zoned target descriptor";
777 /* Get the target zoned block device */
778 ret = dmz_get_zoned_device(ti, argv[0]);
784 /* Initialize metadata */
786 ret = dmz_ctr_metadata(dev, &dmz->metadata);
788 ti->error = "Metadata initialization failed";
792 /* Set target (no write same support) */
793 ti->max_io_len = dev->zone_nr_sectors << 9;
794 ti->num_flush_bios = 1;
795 ti->num_discard_bios = 1;
796 ti->num_write_zeroes_bios = 1;
797 ti->per_io_data_size = sizeof(struct dmz_bioctx);
798 ti->flush_supported = true;
799 ti->discards_supported = true;
801 /* The exposed capacity is the number of chunks that can be mapped */
802 ti->len = (sector_t)dmz_nr_chunks(dmz->metadata) << dev->zone_nr_sectors_shift;
805 ret = bioset_init(&dmz->bio_set, DMZ_MIN_BIOS, 0, 0);
807 ti->error = "Create BIO set failed";
812 mutex_init(&dmz->chunk_lock);
813 INIT_RADIX_TREE(&dmz->chunk_rxtree, GFP_NOIO);
814 dmz->chunk_wq = alloc_workqueue("dmz_cwq_%s", WQ_MEM_RECLAIM | WQ_UNBOUND,
816 if (!dmz->chunk_wq) {
817 ti->error = "Create chunk workqueue failed";
823 spin_lock_init(&dmz->flush_lock);
824 bio_list_init(&dmz->flush_list);
825 INIT_DELAYED_WORK(&dmz->flush_work, dmz_flush_work);
826 dmz->flush_wq = alloc_ordered_workqueue("dmz_fwq_%s", WQ_MEM_RECLAIM,
828 if (!dmz->flush_wq) {
829 ti->error = "Create flush workqueue failed";
833 mod_delayed_work(dmz->flush_wq, &dmz->flush_work, DMZ_FLUSH_PERIOD);
835 /* Initialize reclaim */
836 ret = dmz_ctr_reclaim(dev, dmz->metadata, &dmz->reclaim);
838 ti->error = "Zone reclaim initialization failed";
842 dmz_dev_info(dev, "Target device: %llu 512-byte logical sectors (%llu blocks)",
843 (unsigned long long)ti->len,
844 (unsigned long long)dmz_sect2blk(ti->len));
848 destroy_workqueue(dmz->flush_wq);
850 destroy_workqueue(dmz->chunk_wq);
852 mutex_destroy(&dmz->chunk_lock);
853 bioset_exit(&dmz->bio_set);
855 dmz_dtr_metadata(dmz->metadata);
857 dmz_put_zoned_device(ti);
867 static void dmz_dtr(struct dm_target *ti)
869 struct dmz_target *dmz = ti->private;
871 flush_workqueue(dmz->chunk_wq);
872 destroy_workqueue(dmz->chunk_wq);
874 dmz_dtr_reclaim(dmz->reclaim);
876 cancel_delayed_work_sync(&dmz->flush_work);
877 destroy_workqueue(dmz->flush_wq);
879 (void) dmz_flush_metadata(dmz->metadata);
881 dmz_dtr_metadata(dmz->metadata);
883 bioset_exit(&dmz->bio_set);
885 dmz_put_zoned_device(ti);
887 mutex_destroy(&dmz->chunk_lock);
893 * Setup target request queue limits.
895 static void dmz_io_hints(struct dm_target *ti, struct queue_limits *limits)
897 struct dmz_target *dmz = ti->private;
898 unsigned int chunk_sectors = dmz->dev->zone_nr_sectors;
900 limits->logical_block_size = DMZ_BLOCK_SIZE;
901 limits->physical_block_size = DMZ_BLOCK_SIZE;
903 blk_limits_io_min(limits, DMZ_BLOCK_SIZE);
904 blk_limits_io_opt(limits, DMZ_BLOCK_SIZE);
906 limits->discard_alignment = DMZ_BLOCK_SIZE;
907 limits->discard_granularity = DMZ_BLOCK_SIZE;
908 limits->max_discard_sectors = chunk_sectors;
909 limits->max_hw_discard_sectors = chunk_sectors;
910 limits->max_write_zeroes_sectors = chunk_sectors;
912 /* FS hint to try to align to the device zone size */
913 limits->chunk_sectors = chunk_sectors;
914 limits->max_sectors = chunk_sectors;
916 /* We are exposing a drive-managed zoned block device */
917 limits->zoned = BLK_ZONED_NONE;
921 * Pass on ioctl to the backend device.
923 static int dmz_prepare_ioctl(struct dm_target *ti, struct block_device **bdev)
925 struct dmz_target *dmz = ti->private;
927 if (!dmz_check_bdev(dmz->dev))
930 *bdev = dmz->dev->bdev;
936 * Stop works on suspend.
938 static void dmz_suspend(struct dm_target *ti)
940 struct dmz_target *dmz = ti->private;
942 flush_workqueue(dmz->chunk_wq);
943 dmz_suspend_reclaim(dmz->reclaim);
944 cancel_delayed_work_sync(&dmz->flush_work);
948 * Restart works on resume or if suspend failed.
950 static void dmz_resume(struct dm_target *ti)
952 struct dmz_target *dmz = ti->private;
954 queue_delayed_work(dmz->flush_wq, &dmz->flush_work, DMZ_FLUSH_PERIOD);
955 dmz_resume_reclaim(dmz->reclaim);
958 static int dmz_iterate_devices(struct dm_target *ti,
959 iterate_devices_callout_fn fn, void *data)
961 struct dmz_target *dmz = ti->private;
962 struct dmz_dev *dev = dmz->dev;
963 sector_t capacity = dev->capacity & ~(dev->zone_nr_sectors - 1);
965 return fn(ti, dmz->ddev, 0, capacity, data);
968 static struct target_type dmz_type = {
970 .version = {1, 0, 0},
971 .features = DM_TARGET_SINGLETON | DM_TARGET_ZONED_HM,
972 .module = THIS_MODULE,
976 .io_hints = dmz_io_hints,
977 .prepare_ioctl = dmz_prepare_ioctl,
978 .postsuspend = dmz_suspend,
979 .resume = dmz_resume,
980 .iterate_devices = dmz_iterate_devices,
983 static int __init dmz_init(void)
985 return dm_register_target(&dmz_type);
988 static void __exit dmz_exit(void)
990 dm_unregister_target(&dmz_type);
993 module_init(dmz_init);
994 module_exit(dmz_exit);
996 MODULE_DESCRIPTION(DM_NAME " target for zoned block devices");
997 MODULE_AUTHOR("Damien Le Moal <damien.lemoal@wdc.com>");
998 MODULE_LICENSE("GPL");