Linux Audio

Check our new training course

Loading...
v6.2
   1// SPDX-License-Identifier: GPL-2.0-only
   2/*
   3 * Copyright (C) 2017 Western Digital Corporation or its affiliates.
   4 *
   5 * This file is released under the GPL.
   6 */
   7
   8#include "dm-zoned.h"
   9
  10#include <linux/module.h>
  11
  12#define	DM_MSG_PREFIX		"zoned"
  13
  14#define DMZ_MIN_BIOS		8192
  15
  16/*
  17 * Zone BIO context.
  18 */
  19struct dmz_bioctx {
  20	struct dmz_dev		*dev;
  21	struct dm_zone		*zone;
  22	struct bio		*bio;
  23	refcount_t		ref;
 
  24};
  25
  26/*
  27 * Chunk work descriptor.
  28 */
  29struct dm_chunk_work {
  30	struct work_struct	work;
  31	refcount_t		refcount;
  32	struct dmz_target	*target;
  33	unsigned int		chunk;
  34	struct bio_list		bio_list;
  35};
  36
  37/*
  38 * Target descriptor.
  39 */
  40struct dmz_target {
  41	struct dm_dev		**ddev;
  42	unsigned int		nr_ddevs;
  43
  44	unsigned int		flags;
  45
  46	/* Zoned block device information */
  47	struct dmz_dev		*dev;
  48
  49	/* For metadata handling */
  50	struct dmz_metadata     *metadata;
  51
 
 
 
  52	/* For chunk work */
 
  53	struct radix_tree_root	chunk_rxtree;
  54	struct workqueue_struct *chunk_wq;
  55	struct mutex		chunk_lock;
  56
  57	/* For cloned BIOs to zones */
  58	struct bio_set		bio_set;
  59
  60	/* For flush */
  61	spinlock_t		flush_lock;
  62	struct bio_list		flush_list;
  63	struct delayed_work	flush_work;
  64	struct workqueue_struct *flush_wq;
  65};
  66
  67/*
  68 * Flush intervals (seconds).
  69 */
  70#define DMZ_FLUSH_PERIOD	(10 * HZ)
  71
  72/*
  73 * Target BIO completion.
  74 */
  75static inline void dmz_bio_endio(struct bio *bio, blk_status_t status)
  76{
  77	struct dmz_bioctx *bioctx =
  78		dm_per_bio_data(bio, sizeof(struct dmz_bioctx));
  79
  80	if (status != BLK_STS_OK && bio->bi_status == BLK_STS_OK)
  81		bio->bi_status = status;
  82	if (bioctx->dev && bio->bi_status != BLK_STS_OK)
  83		bioctx->dev->flags |= DMZ_CHECK_BDEV;
  84
  85	if (refcount_dec_and_test(&bioctx->ref)) {
  86		struct dm_zone *zone = bioctx->zone;
  87
  88		if (zone) {
  89			if (bio->bi_status != BLK_STS_OK &&
  90			    bio_op(bio) == REQ_OP_WRITE &&
  91			    dmz_is_seq(zone))
  92				set_bit(DMZ_SEQ_WRITE_ERR, &zone->flags);
  93			dmz_deactivate_zone(zone);
  94		}
  95		bio_endio(bio);
  96	}
  97}
  98
  99/*
 100 * Completion callback for an internally cloned target BIO. This terminates the
 101 * target BIO when there are no more references to its context.
 102 */
 103static void dmz_clone_endio(struct bio *clone)
 104{
 105	struct dmz_bioctx *bioctx = clone->bi_private;
 106	blk_status_t status = clone->bi_status;
 107
 108	bio_put(clone);
 109	dmz_bio_endio(bioctx->bio, status);
 110}
 111
 112/*
 113 * Issue a clone of a target BIO. The clone may only partially process the
 114 * original target BIO.
 115 */
 116static int dmz_submit_bio(struct dmz_target *dmz, struct dm_zone *zone,
 117			  struct bio *bio, sector_t chunk_block,
 118			  unsigned int nr_blocks)
 119{
 120	struct dmz_bioctx *bioctx =
 121		dm_per_bio_data(bio, sizeof(struct dmz_bioctx));
 122	struct dmz_dev *dev = zone->dev;
 123	struct bio *clone;
 124
 125	if (dev->flags & DMZ_BDEV_DYING)
 126		return -EIO;
 
 
 
 
 
 
 
 
 
 127
 128	clone = bio_alloc_clone(dev->bdev, bio, GFP_NOIO, &dmz->bio_set);
 
 129	if (!clone)
 130		return -ENOMEM;
 131
 132	bioctx->dev = dev;
 133	clone->bi_iter.bi_sector =
 134		dmz_start_sect(dmz->metadata, zone) + dmz_blk2sect(chunk_block);
 135	clone->bi_iter.bi_size = dmz_blk2sect(nr_blocks) << SECTOR_SHIFT;
 136	clone->bi_end_io = dmz_clone_endio;
 137	clone->bi_private = bioctx;
 138
 139	bio_advance(bio, clone->bi_iter.bi_size);
 140
 141	refcount_inc(&bioctx->ref);
 142	submit_bio_noacct(clone);
 143
 144	if (bio_op(bio) == REQ_OP_WRITE && dmz_is_seq(zone))
 145		zone->wp_block += nr_blocks;
 146
 147	return 0;
 148}
 149
 150/*
 151 * Zero out pages of discarded blocks accessed by a read BIO.
 152 */
 153static void dmz_handle_read_zero(struct dmz_target *dmz, struct bio *bio,
 154				 sector_t chunk_block, unsigned int nr_blocks)
 155{
 156	unsigned int size = nr_blocks << DMZ_BLOCK_SHIFT;
 157
 158	/* Clear nr_blocks */
 159	swap(bio->bi_iter.bi_size, size);
 160	zero_fill_bio(bio);
 161	swap(bio->bi_iter.bi_size, size);
 162
 163	bio_advance(bio, size);
 164}
 165
 166/*
 167 * Process a read BIO.
 168 */
 169static int dmz_handle_read(struct dmz_target *dmz, struct dm_zone *zone,
 170			   struct bio *bio)
 171{
 172	struct dmz_metadata *zmd = dmz->metadata;
 173	sector_t chunk_block = dmz_chunk_block(zmd, dmz_bio_block(bio));
 174	unsigned int nr_blocks = dmz_bio_blocks(bio);
 175	sector_t end_block = chunk_block + nr_blocks;
 176	struct dm_zone *rzone, *bzone;
 177	int ret;
 178
 179	/* Read into unmapped chunks need only zeroing the BIO buffer */
 180	if (!zone) {
 181		zero_fill_bio(bio);
 182		return 0;
 183	}
 184
 185	DMDEBUG("(%s): READ chunk %llu -> %s zone %u, block %llu, %u blocks",
 186		dmz_metadata_label(zmd),
 187		(unsigned long long)dmz_bio_chunk(zmd, bio),
 188		(dmz_is_rnd(zone) ? "RND" :
 189		 (dmz_is_cache(zone) ? "CACHE" : "SEQ")),
 190		zone->id,
 191		(unsigned long long)chunk_block, nr_blocks);
 192
 193	/* Check block validity to determine the read location */
 194	bzone = zone->bzone;
 195	while (chunk_block < end_block) {
 196		nr_blocks = 0;
 197		if (dmz_is_rnd(zone) || dmz_is_cache(zone) ||
 198		    chunk_block < zone->wp_block) {
 199			/* Test block validity in the data zone */
 200			ret = dmz_block_valid(zmd, zone, chunk_block);
 201			if (ret < 0)
 202				return ret;
 203			if (ret > 0) {
 204				/* Read data zone blocks */
 205				nr_blocks = ret;
 206				rzone = zone;
 207			}
 208		}
 209
 210		/*
 211		 * No valid blocks found in the data zone.
 212		 * Check the buffer zone, if there is one.
 213		 */
 214		if (!nr_blocks && bzone) {
 215			ret = dmz_block_valid(zmd, bzone, chunk_block);
 216			if (ret < 0)
 217				return ret;
 218			if (ret > 0) {
 219				/* Read buffer zone blocks */
 220				nr_blocks = ret;
 221				rzone = bzone;
 222			}
 223		}
 224
 225		if (nr_blocks) {
 226			/* Valid blocks found: read them */
 227			nr_blocks = min_t(unsigned int, nr_blocks,
 228					  end_block - chunk_block);
 229			ret = dmz_submit_bio(dmz, rzone, bio,
 230					     chunk_block, nr_blocks);
 231			if (ret)
 232				return ret;
 233			chunk_block += nr_blocks;
 234		} else {
 235			/* No valid block: zeroout the current BIO block */
 236			dmz_handle_read_zero(dmz, bio, chunk_block, 1);
 237			chunk_block++;
 238		}
 239	}
 240
 241	return 0;
 242}
 243
 244/*
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 245 * Write blocks directly in a data zone, at the write pointer.
 246 * If a buffer zone is assigned, invalidate the blocks written
 247 * in place.
 248 */
 249static int dmz_handle_direct_write(struct dmz_target *dmz,
 250				   struct dm_zone *zone, struct bio *bio,
 251				   sector_t chunk_block,
 252				   unsigned int nr_blocks)
 253{
 254	struct dmz_metadata *zmd = dmz->metadata;
 255	struct dm_zone *bzone = zone->bzone;
 256	int ret;
 257
 258	if (dmz_is_readonly(zone))
 259		return -EROFS;
 260
 261	/* Submit write */
 262	ret = dmz_submit_bio(dmz, zone, bio, chunk_block, nr_blocks);
 263	if (ret)
 264		return ret;
 265
 266	/*
 267	 * Validate the blocks in the data zone and invalidate
 268	 * in the buffer zone, if there is one.
 269	 */
 270	ret = dmz_validate_blocks(zmd, zone, chunk_block, nr_blocks);
 271	if (ret == 0 && bzone)
 272		ret = dmz_invalidate_blocks(zmd, bzone, chunk_block, nr_blocks);
 273
 274	return ret;
 275}
 276
 277/*
 278 * Write blocks in the buffer zone of @zone.
 279 * If no buffer zone is assigned yet, get one.
 280 * Called with @zone write locked.
 281 */
 282static int dmz_handle_buffered_write(struct dmz_target *dmz,
 283				     struct dm_zone *zone, struct bio *bio,
 284				     sector_t chunk_block,
 285				     unsigned int nr_blocks)
 286{
 287	struct dmz_metadata *zmd = dmz->metadata;
 288	struct dm_zone *bzone;
 289	int ret;
 290
 291	/* Get the buffer zone. One will be allocated if needed */
 292	bzone = dmz_get_chunk_buffer(zmd, zone);
 293	if (IS_ERR(bzone))
 294		return PTR_ERR(bzone);
 295
 296	if (dmz_is_readonly(bzone))
 297		return -EROFS;
 298
 299	/* Submit write */
 300	ret = dmz_submit_bio(dmz, bzone, bio, chunk_block, nr_blocks);
 301	if (ret)
 302		return ret;
 303
 304	/*
 305	 * Validate the blocks in the buffer zone
 306	 * and invalidate in the data zone.
 307	 */
 308	ret = dmz_validate_blocks(zmd, bzone, chunk_block, nr_blocks);
 309	if (ret == 0 && chunk_block < zone->wp_block)
 310		ret = dmz_invalidate_blocks(zmd, zone, chunk_block, nr_blocks);
 311
 312	return ret;
 313}
 314
 315/*
 316 * Process a write BIO.
 317 */
 318static int dmz_handle_write(struct dmz_target *dmz, struct dm_zone *zone,
 319			    struct bio *bio)
 320{
 321	struct dmz_metadata *zmd = dmz->metadata;
 322	sector_t chunk_block = dmz_chunk_block(zmd, dmz_bio_block(bio));
 323	unsigned int nr_blocks = dmz_bio_blocks(bio);
 324
 325	if (!zone)
 326		return -ENOSPC;
 327
 328	DMDEBUG("(%s): WRITE chunk %llu -> %s zone %u, block %llu, %u blocks",
 329		dmz_metadata_label(zmd),
 330		(unsigned long long)dmz_bio_chunk(zmd, bio),
 331		(dmz_is_rnd(zone) ? "RND" :
 332		 (dmz_is_cache(zone) ? "CACHE" : "SEQ")),
 333		zone->id,
 334		(unsigned long long)chunk_block, nr_blocks);
 335
 336	if (dmz_is_rnd(zone) || dmz_is_cache(zone) ||
 337	    chunk_block == zone->wp_block) {
 338		/*
 339		 * zone is a random zone or it is a sequential zone
 340		 * and the BIO is aligned to the zone write pointer:
 341		 * direct write the zone.
 342		 */
 343		return dmz_handle_direct_write(dmz, zone, bio,
 344					       chunk_block, nr_blocks);
 345	}
 346
 347	/*
 348	 * This is an unaligned write in a sequential zone:
 349	 * use buffered write.
 350	 */
 351	return dmz_handle_buffered_write(dmz, zone, bio, chunk_block, nr_blocks);
 352}
 353
 354/*
 355 * Process a discard BIO.
 356 */
 357static int dmz_handle_discard(struct dmz_target *dmz, struct dm_zone *zone,
 358			      struct bio *bio)
 359{
 360	struct dmz_metadata *zmd = dmz->metadata;
 361	sector_t block = dmz_bio_block(bio);
 362	unsigned int nr_blocks = dmz_bio_blocks(bio);
 363	sector_t chunk_block = dmz_chunk_block(zmd, block);
 364	int ret = 0;
 365
 366	/* For unmapped chunks, there is nothing to do */
 367	if (!zone)
 368		return 0;
 369
 370	if (dmz_is_readonly(zone))
 371		return -EROFS;
 372
 373	DMDEBUG("(%s): DISCARD chunk %llu -> zone %u, block %llu, %u blocks",
 374		dmz_metadata_label(dmz->metadata),
 375		(unsigned long long)dmz_bio_chunk(zmd, bio),
 376		zone->id,
 377		(unsigned long long)chunk_block, nr_blocks);
 378
 379	/*
 380	 * Invalidate blocks in the data zone and its
 381	 * buffer zone if one is mapped.
 382	 */
 383	if (dmz_is_rnd(zone) || dmz_is_cache(zone) ||
 384	    chunk_block < zone->wp_block)
 385		ret = dmz_invalidate_blocks(zmd, zone, chunk_block, nr_blocks);
 386	if (ret == 0 && zone->bzone)
 387		ret = dmz_invalidate_blocks(zmd, zone->bzone,
 388					    chunk_block, nr_blocks);
 389	return ret;
 390}
 391
 392/*
 393 * Process a BIO.
 394 */
 395static void dmz_handle_bio(struct dmz_target *dmz, struct dm_chunk_work *cw,
 396			   struct bio *bio)
 397{
 398	struct dmz_bioctx *bioctx =
 399		dm_per_bio_data(bio, sizeof(struct dmz_bioctx));
 400	struct dmz_metadata *zmd = dmz->metadata;
 401	struct dm_zone *zone;
 402	int ret;
 403
 
 
 
 
 
 
 
 404	dmz_lock_metadata(zmd);
 405
 406	/*
 407	 * Get the data zone mapping the chunk. There may be no
 408	 * mapping for read and discard. If a mapping is obtained,
 409	 + the zone returned will be set to active state.
 410	 */
 411	zone = dmz_get_chunk_mapping(zmd, dmz_bio_chunk(zmd, bio),
 412				     bio_op(bio));
 413	if (IS_ERR(zone)) {
 414		ret = PTR_ERR(zone);
 415		goto out;
 416	}
 417
 418	/* Process the BIO */
 419	if (zone) {
 420		dmz_activate_zone(zone);
 421		bioctx->zone = zone;
 422		dmz_reclaim_bio_acc(zone->dev->reclaim);
 423	}
 424
 425	switch (bio_op(bio)) {
 426	case REQ_OP_READ:
 427		ret = dmz_handle_read(dmz, zone, bio);
 428		break;
 429	case REQ_OP_WRITE:
 430		ret = dmz_handle_write(dmz, zone, bio);
 431		break;
 432	case REQ_OP_DISCARD:
 433	case REQ_OP_WRITE_ZEROES:
 434		ret = dmz_handle_discard(dmz, zone, bio);
 435		break;
 436	default:
 437		DMERR("(%s): Unsupported BIO operation 0x%x",
 438		      dmz_metadata_label(dmz->metadata), bio_op(bio));
 439		ret = -EIO;
 440	}
 441
 442	/*
 443	 * Release the chunk mapping. This will check that the mapping
 444	 * is still valid, that is, that the zone used still has valid blocks.
 445	 */
 446	if (zone)
 447		dmz_put_chunk_mapping(zmd, zone);
 448out:
 449	dmz_bio_endio(bio, errno_to_blk_status(ret));
 450
 451	dmz_unlock_metadata(zmd);
 452}
 453
 454/*
 455 * Increment a chunk reference counter.
 456 */
 457static inline void dmz_get_chunk_work(struct dm_chunk_work *cw)
 458{
 459	refcount_inc(&cw->refcount);
 460}
 461
 462/*
 463 * Decrement a chunk work reference count and
 464 * free it if it becomes 0.
 465 */
 466static void dmz_put_chunk_work(struct dm_chunk_work *cw)
 467{
 468	if (refcount_dec_and_test(&cw->refcount)) {
 469		WARN_ON(!bio_list_empty(&cw->bio_list));
 470		radix_tree_delete(&cw->target->chunk_rxtree, cw->chunk);
 471		kfree(cw);
 472	}
 473}
 474
 475/*
 476 * Chunk BIO work function.
 477 */
 478static void dmz_chunk_work(struct work_struct *work)
 479{
 480	struct dm_chunk_work *cw = container_of(work, struct dm_chunk_work, work);
 481	struct dmz_target *dmz = cw->target;
 482	struct bio *bio;
 483
 484	mutex_lock(&dmz->chunk_lock);
 485
 486	/* Process the chunk BIOs */
 487	while ((bio = bio_list_pop(&cw->bio_list))) {
 488		mutex_unlock(&dmz->chunk_lock);
 489		dmz_handle_bio(dmz, cw, bio);
 490		mutex_lock(&dmz->chunk_lock);
 491		dmz_put_chunk_work(cw);
 492	}
 493
 494	/* Queueing the work incremented the work refcount */
 495	dmz_put_chunk_work(cw);
 496
 497	mutex_unlock(&dmz->chunk_lock);
 498}
 499
 500/*
 501 * Flush work.
 502 */
 503static void dmz_flush_work(struct work_struct *work)
 504{
 505	struct dmz_target *dmz = container_of(work, struct dmz_target, flush_work.work);
 506	struct bio *bio;
 507	int ret;
 508
 509	/* Flush dirty metadata blocks */
 510	ret = dmz_flush_metadata(dmz->metadata);
 511	if (ret)
 512		DMDEBUG("(%s): Metadata flush failed, rc=%d",
 513			dmz_metadata_label(dmz->metadata), ret);
 514
 515	/* Process queued flush requests */
 516	while (1) {
 517		spin_lock(&dmz->flush_lock);
 518		bio = bio_list_pop(&dmz->flush_list);
 519		spin_unlock(&dmz->flush_lock);
 520
 521		if (!bio)
 522			break;
 523
 524		dmz_bio_endio(bio, errno_to_blk_status(ret));
 525	}
 526
 527	queue_delayed_work(dmz->flush_wq, &dmz->flush_work, DMZ_FLUSH_PERIOD);
 528}
 529
 530/*
 531 * Get a chunk work and start it to process a new BIO.
 532 * If the BIO chunk has no work yet, create one.
 533 */
 534static int dmz_queue_chunk_work(struct dmz_target *dmz, struct bio *bio)
 535{
 536	unsigned int chunk = dmz_bio_chunk(dmz->metadata, bio);
 537	struct dm_chunk_work *cw;
 538	int ret = 0;
 539
 540	mutex_lock(&dmz->chunk_lock);
 541
 542	/* Get the BIO chunk work. If one is not active yet, create one */
 543	cw = radix_tree_lookup(&dmz->chunk_rxtree, chunk);
 544	if (cw) {
 545		dmz_get_chunk_work(cw);
 546	} else {
 547		/* Create a new chunk work */
 548		cw = kmalloc(sizeof(struct dm_chunk_work), GFP_NOIO);
 549		if (unlikely(!cw)) {
 550			ret = -ENOMEM;
 551			goto out;
 552		}
 553
 554		INIT_WORK(&cw->work, dmz_chunk_work);
 555		refcount_set(&cw->refcount, 1);
 556		cw->target = dmz;
 557		cw->chunk = chunk;
 558		bio_list_init(&cw->bio_list);
 559
 560		ret = radix_tree_insert(&dmz->chunk_rxtree, chunk, cw);
 561		if (unlikely(ret)) {
 562			kfree(cw);
 
 563			goto out;
 564		}
 565	}
 566
 567	bio_list_add(&cw->bio_list, bio);
 
 568
 569	if (queue_work(dmz->chunk_wq, &cw->work))
 570		dmz_get_chunk_work(cw);
 571out:
 572	mutex_unlock(&dmz->chunk_lock);
 573	return ret;
 574}
 575
 576/*
 577 * Check if the backing device is being removed. If it's on the way out,
 578 * start failing I/O. Reclaim and metadata components also call this
 579 * function to cleanly abort operation in the event of such failure.
 580 */
 581bool dmz_bdev_is_dying(struct dmz_dev *dmz_dev)
 582{
 583	if (dmz_dev->flags & DMZ_BDEV_DYING)
 584		return true;
 585
 586	if (dmz_dev->flags & DMZ_CHECK_BDEV)
 587		return !dmz_check_bdev(dmz_dev);
 588
 589	if (blk_queue_dying(bdev_get_queue(dmz_dev->bdev))) {
 590		dmz_dev_warn(dmz_dev, "Backing device queue dying");
 591		dmz_dev->flags |= DMZ_BDEV_DYING;
 592	}
 593
 594	return dmz_dev->flags & DMZ_BDEV_DYING;
 595}
 596
 597/*
 598 * Check the backing device availability. This detects such events as
 599 * backing device going offline due to errors, media removals, etc.
 600 * This check is less efficient than dmz_bdev_is_dying() and should
 601 * only be performed as a part of error handling.
 602 */
 603bool dmz_check_bdev(struct dmz_dev *dmz_dev)
 604{
 605	struct gendisk *disk;
 606
 607	dmz_dev->flags &= ~DMZ_CHECK_BDEV;
 608
 609	if (dmz_bdev_is_dying(dmz_dev))
 610		return false;
 611
 612	disk = dmz_dev->bdev->bd_disk;
 613	if (disk->fops->check_events &&
 614	    disk->fops->check_events(disk, 0) & DISK_EVENT_MEDIA_CHANGE) {
 615		dmz_dev_warn(dmz_dev, "Backing device offline");
 616		dmz_dev->flags |= DMZ_BDEV_DYING;
 617	}
 618
 619	return !(dmz_dev->flags & DMZ_BDEV_DYING);
 620}
 621
 622/*
 623 * Process a new BIO.
 624 */
 625static int dmz_map(struct dm_target *ti, struct bio *bio)
 626{
 627	struct dmz_target *dmz = ti->private;
 628	struct dmz_metadata *zmd = dmz->metadata;
 629	struct dmz_bioctx *bioctx = dm_per_bio_data(bio, sizeof(struct dmz_bioctx));
 630	sector_t sector = bio->bi_iter.bi_sector;
 631	unsigned int nr_sectors = bio_sectors(bio);
 632	sector_t chunk_sector;
 633	int ret;
 634
 635	if (dmz_dev_is_dying(zmd))
 636		return DM_MAPIO_KILL;
 
 
 
 637
 638	DMDEBUG("(%s): BIO op %d sector %llu + %u => chunk %llu, block %llu, %u blocks",
 639		dmz_metadata_label(zmd),
 640		bio_op(bio), (unsigned long long)sector, nr_sectors,
 641		(unsigned long long)dmz_bio_chunk(zmd, bio),
 642		(unsigned long long)dmz_chunk_block(zmd, dmz_bio_block(bio)),
 643		(unsigned int)dmz_bio_blocks(bio));
 644
 645	if (!nr_sectors && bio_op(bio) != REQ_OP_WRITE)
 646		return DM_MAPIO_REMAPPED;
 647
 648	/* The BIO should be block aligned */
 649	if ((nr_sectors & DMZ_BLOCK_SECTORS_MASK) || (sector & DMZ_BLOCK_SECTORS_MASK))
 650		return DM_MAPIO_KILL;
 651
 652	/* Initialize the BIO context */
 653	bioctx->dev = NULL;
 654	bioctx->zone = NULL;
 655	bioctx->bio = bio;
 656	refcount_set(&bioctx->ref, 1);
 
 657
 658	/* Set the BIO pending in the flush list */
 659	if (!nr_sectors && bio_op(bio) == REQ_OP_WRITE) {
 660		spin_lock(&dmz->flush_lock);
 661		bio_list_add(&dmz->flush_list, bio);
 662		spin_unlock(&dmz->flush_lock);
 663		mod_delayed_work(dmz->flush_wq, &dmz->flush_work, 0);
 664		return DM_MAPIO_SUBMITTED;
 665	}
 666
 667	/* Split zone BIOs to fit entirely into a zone */
 668	chunk_sector = sector & (dmz_zone_nr_sectors(zmd) - 1);
 669	if (chunk_sector + nr_sectors > dmz_zone_nr_sectors(zmd))
 670		dm_accept_partial_bio(bio, dmz_zone_nr_sectors(zmd) - chunk_sector);
 671
 672	/* Now ready to handle this BIO */
 673	ret = dmz_queue_chunk_work(dmz, bio);
 674	if (ret) {
 675		DMDEBUG("(%s): BIO op %d, can't process chunk %llu, err %i",
 676			dmz_metadata_label(zmd),
 677			bio_op(bio), (u64)dmz_bio_chunk(zmd, bio),
 678			ret);
 679		return DM_MAPIO_REQUEUE;
 680	}
 681
 682	return DM_MAPIO_SUBMITTED;
 683}
 684
 685/*
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 686 * Get zoned device information.
 687 */
 688static int dmz_get_zoned_device(struct dm_target *ti, char *path,
 689				int idx, int nr_devs)
 690{
 691	struct dmz_target *dmz = ti->private;
 692	struct dm_dev *ddev;
 693	struct dmz_dev *dev;
 
 694	int ret;
 695	struct block_device *bdev;
 696
 697	/* Get the target device */
 698	ret = dm_get_device(ti, path, dm_table_get_mode(ti->table), &ddev);
 699	if (ret) {
 700		ti->error = "Get target device failed";
 
 701		return ret;
 702	}
 703
 704	bdev = ddev->bdev;
 705	if (bdev_zoned_model(bdev) == BLK_ZONED_NONE) {
 706		if (nr_devs == 1) {
 707			ti->error = "Invalid regular device";
 708			goto err;
 709		}
 710		if (idx != 0) {
 711			ti->error = "First device must be a regular device";
 712			goto err;
 713		}
 714		if (dmz->ddev[0]) {
 715			ti->error = "Too many regular devices";
 716			goto err;
 717		}
 718		dev = &dmz->dev[idx];
 719		dev->flags = DMZ_BDEV_REGULAR;
 720	} else {
 721		if (dmz->ddev[idx]) {
 722			ti->error = "Too many zoned devices";
 723			goto err;
 724		}
 725		if (nr_devs > 1 && idx == 0) {
 726			ti->error = "First device must be a regular device";
 727			goto err;
 728		}
 729		dev = &dmz->dev[idx];
 730	}
 731	dev->bdev = bdev;
 732	dev->dev_idx = idx;
 733
 734	dev->capacity = bdev_nr_sectors(bdev);
 735	if (ti->begin) {
 736		ti->error = "Partial mapping is not supported";
 
 
 
 737		goto err;
 738	}
 739
 740	dmz->ddev[idx] = ddev;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 741
 742	return 0;
 743err:
 744	dm_put_device(ti, ddev);
 745	return -EINVAL;
 
 
 746}
 747
 748/*
 749 * Cleanup zoned device information.
 750 */
 751static void dmz_put_zoned_device(struct dm_target *ti)
 752{
 753	struct dmz_target *dmz = ti->private;
 754	int i;
 755
 756	for (i = 0; i < dmz->nr_ddevs; i++) {
 757		if (dmz->ddev[i]) {
 758			dm_put_device(ti, dmz->ddev[i]);
 759			dmz->ddev[i] = NULL;
 760		}
 761	}
 762}
 763
 764static int dmz_fixup_devices(struct dm_target *ti)
 765{
 766	struct dmz_target *dmz = ti->private;
 767	struct dmz_dev *reg_dev = NULL;
 768	sector_t zone_nr_sectors = 0;
 769	int i;
 770
 771	/*
 772	 * When we have more than on devices, the first one must be a
 773	 * regular block device and the others zoned block devices.
 774	 */
 775	if (dmz->nr_ddevs > 1) {
 776		reg_dev = &dmz->dev[0];
 777		if (!(reg_dev->flags & DMZ_BDEV_REGULAR)) {
 778			ti->error = "Primary disk is not a regular device";
 779			return -EINVAL;
 780		}
 781		for (i = 1; i < dmz->nr_ddevs; i++) {
 782			struct dmz_dev *zoned_dev = &dmz->dev[i];
 783			struct block_device *bdev = zoned_dev->bdev;
 784
 785			if (zoned_dev->flags & DMZ_BDEV_REGULAR) {
 786				ti->error = "Secondary disk is not a zoned device";
 787				return -EINVAL;
 788			}
 789			if (zone_nr_sectors &&
 790			    zone_nr_sectors != bdev_zone_sectors(bdev)) {
 791				ti->error = "Zone nr sectors mismatch";
 792				return -EINVAL;
 793			}
 794			zone_nr_sectors = bdev_zone_sectors(bdev);
 795			zoned_dev->zone_nr_sectors = zone_nr_sectors;
 796			zoned_dev->nr_zones = bdev_nr_zones(bdev);
 797		}
 798	} else {
 799		struct dmz_dev *zoned_dev = &dmz->dev[0];
 800		struct block_device *bdev = zoned_dev->bdev;
 801
 802		if (zoned_dev->flags & DMZ_BDEV_REGULAR) {
 803			ti->error = "Disk is not a zoned device";
 804			return -EINVAL;
 805		}
 806		zoned_dev->zone_nr_sectors = bdev_zone_sectors(bdev);
 807		zoned_dev->nr_zones = bdev_nr_zones(bdev);
 808	}
 809
 810	if (reg_dev) {
 811		sector_t zone_offset;
 812
 813		reg_dev->zone_nr_sectors = zone_nr_sectors;
 814		reg_dev->nr_zones =
 815			DIV_ROUND_UP_SECTOR_T(reg_dev->capacity,
 816					      reg_dev->zone_nr_sectors);
 817		reg_dev->zone_offset = 0;
 818		zone_offset = reg_dev->nr_zones;
 819		for (i = 1; i < dmz->nr_ddevs; i++) {
 820			dmz->dev[i].zone_offset = zone_offset;
 821			zone_offset += dmz->dev[i].nr_zones;
 822		}
 823	}
 824	return 0;
 825}
 826
 827/*
 828 * Setup target.
 829 */
 830static int dmz_ctr(struct dm_target *ti, unsigned int argc, char **argv)
 831{
 832	struct dmz_target *dmz;
 833	int ret, i;
 
 834
 835	/* Check arguments */
 836	if (argc < 1) {
 837		ti->error = "Invalid argument count";
 838		return -EINVAL;
 839	}
 840
 841	/* Allocate and initialize the target descriptor */
 842	dmz = kzalloc(sizeof(struct dmz_target), GFP_KERNEL);
 843	if (!dmz) {
 844		ti->error = "Unable to allocate the zoned target descriptor";
 845		return -ENOMEM;
 846	}
 847	dmz->dev = kcalloc(argc, sizeof(struct dmz_dev), GFP_KERNEL);
 848	if (!dmz->dev) {
 849		ti->error = "Unable to allocate the zoned device descriptors";
 850		kfree(dmz);
 851		return -ENOMEM;
 852	}
 853	dmz->ddev = kcalloc(argc, sizeof(struct dm_dev *), GFP_KERNEL);
 854	if (!dmz->ddev) {
 855		ti->error = "Unable to allocate the dm device descriptors";
 856		ret = -ENOMEM;
 857		goto err;
 858	}
 859	dmz->nr_ddevs = argc;
 860
 861	ti->private = dmz;
 862
 863	/* Get the target zoned block device */
 864	for (i = 0; i < argc; i++) {
 865		ret = dmz_get_zoned_device(ti, argv[i], i, argc);
 866		if (ret)
 867			goto err_dev;
 868	}
 869	ret = dmz_fixup_devices(ti);
 870	if (ret)
 871		goto err_dev;
 872
 873	/* Initialize metadata */
 874	ret = dmz_ctr_metadata(dmz->dev, argc, &dmz->metadata,
 875			       dm_table_device_name(ti->table));
 876	if (ret) {
 877		ti->error = "Metadata initialization failed";
 878		goto err_dev;
 879	}
 880
 881	/* Set target (no write same support) */
 882	ti->max_io_len = dmz_zone_nr_sectors(dmz->metadata);
 883	ti->num_flush_bios = 1;
 884	ti->num_discard_bios = 1;
 885	ti->num_write_zeroes_bios = 1;
 886	ti->per_io_data_size = sizeof(struct dmz_bioctx);
 887	ti->flush_supported = true;
 888	ti->discards_supported = true;
 
 889
 890	/* The exposed capacity is the number of chunks that can be mapped */
 891	ti->len = (sector_t)dmz_nr_chunks(dmz->metadata) <<
 892		dmz_zone_nr_sectors_shift(dmz->metadata);
 893
 894	/* Zone BIO */
 895	ret = bioset_init(&dmz->bio_set, DMZ_MIN_BIOS, 0, 0);
 896	if (ret) {
 897		ti->error = "Create BIO set failed";
 
 898		goto err_meta;
 899	}
 900
 901	/* Chunk BIO work */
 902	mutex_init(&dmz->chunk_lock);
 903	INIT_RADIX_TREE(&dmz->chunk_rxtree, GFP_NOIO);
 904	dmz->chunk_wq = alloc_workqueue("dmz_cwq_%s",
 905					WQ_MEM_RECLAIM | WQ_UNBOUND, 0,
 906					dmz_metadata_label(dmz->metadata));
 907	if (!dmz->chunk_wq) {
 908		ti->error = "Create chunk workqueue failed";
 909		ret = -ENOMEM;
 910		goto err_bio;
 911	}
 912
 913	/* Flush work */
 914	spin_lock_init(&dmz->flush_lock);
 915	bio_list_init(&dmz->flush_list);
 916	INIT_DELAYED_WORK(&dmz->flush_work, dmz_flush_work);
 917	dmz->flush_wq = alloc_ordered_workqueue("dmz_fwq_%s", WQ_MEM_RECLAIM,
 918						dmz_metadata_label(dmz->metadata));
 919	if (!dmz->flush_wq) {
 920		ti->error = "Create flush workqueue failed";
 921		ret = -ENOMEM;
 922		goto err_cwq;
 923	}
 924	mod_delayed_work(dmz->flush_wq, &dmz->flush_work, DMZ_FLUSH_PERIOD);
 925
 926	/* Initialize reclaim */
 927	for (i = 0; i < dmz->nr_ddevs; i++) {
 928		ret = dmz_ctr_reclaim(dmz->metadata, &dmz->dev[i].reclaim, i);
 929		if (ret) {
 930			ti->error = "Zone reclaim initialization failed";
 931			goto err_fwq;
 932		}
 933	}
 934
 935	DMINFO("(%s): Target device: %llu 512-byte logical sectors (%llu blocks)",
 936	       dmz_metadata_label(dmz->metadata),
 937	       (unsigned long long)ti->len,
 938	       (unsigned long long)dmz_sect2blk(ti->len));
 939
 940	return 0;
 941err_fwq:
 942	destroy_workqueue(dmz->flush_wq);
 943err_cwq:
 944	destroy_workqueue(dmz->chunk_wq);
 945err_bio:
 946	mutex_destroy(&dmz->chunk_lock);
 947	bioset_exit(&dmz->bio_set);
 948err_meta:
 949	dmz_dtr_metadata(dmz->metadata);
 950err_dev:
 951	dmz_put_zoned_device(ti);
 952err:
 953	kfree(dmz->dev);
 954	kfree(dmz);
 955
 956	return ret;
 957}
 958
 959/*
 960 * Cleanup target.
 961 */
 962static void dmz_dtr(struct dm_target *ti)
 963{
 964	struct dmz_target *dmz = ti->private;
 965	int i;
 966
 
 967	destroy_workqueue(dmz->chunk_wq);
 968
 969	for (i = 0; i < dmz->nr_ddevs; i++)
 970		dmz_dtr_reclaim(dmz->dev[i].reclaim);
 971
 972	cancel_delayed_work_sync(&dmz->flush_work);
 973	destroy_workqueue(dmz->flush_wq);
 974
 975	(void) dmz_flush_metadata(dmz->metadata);
 976
 977	dmz_dtr_metadata(dmz->metadata);
 978
 979	bioset_exit(&dmz->bio_set);
 980
 981	dmz_put_zoned_device(ti);
 982
 983	mutex_destroy(&dmz->chunk_lock);
 984
 985	kfree(dmz->dev);
 986	kfree(dmz);
 987}
 988
 989/*
 990 * Setup target request queue limits.
 991 */
 992static void dmz_io_hints(struct dm_target *ti, struct queue_limits *limits)
 993{
 994	struct dmz_target *dmz = ti->private;
 995	unsigned int chunk_sectors = dmz_zone_nr_sectors(dmz->metadata);
 996
 997	limits->logical_block_size = DMZ_BLOCK_SIZE;
 998	limits->physical_block_size = DMZ_BLOCK_SIZE;
 999
1000	blk_limits_io_min(limits, DMZ_BLOCK_SIZE);
1001	blk_limits_io_opt(limits, DMZ_BLOCK_SIZE);
1002
1003	limits->discard_alignment = 0;
1004	limits->discard_granularity = DMZ_BLOCK_SIZE;
1005	limits->max_discard_sectors = chunk_sectors;
1006	limits->max_hw_discard_sectors = chunk_sectors;
1007	limits->max_write_zeroes_sectors = chunk_sectors;
1008
1009	/* FS hint to try to align to the device zone size */
1010	limits->chunk_sectors = chunk_sectors;
1011	limits->max_sectors = chunk_sectors;
1012
1013	/* We are exposing a drive-managed zoned block device */
1014	limits->zoned = BLK_ZONED_NONE;
1015}
1016
1017/*
1018 * Pass on ioctl to the backend device.
1019 */
1020static int dmz_prepare_ioctl(struct dm_target *ti, struct block_device **bdev)
1021{
1022	struct dmz_target *dmz = ti->private;
1023	struct dmz_dev *dev = &dmz->dev[0];
1024
1025	if (!dmz_check_bdev(dev))
1026		return -EIO;
1027
1028	*bdev = dev->bdev;
1029
1030	return 0;
1031}
1032
1033/*
1034 * Stop works on suspend.
1035 */
1036static void dmz_suspend(struct dm_target *ti)
1037{
1038	struct dmz_target *dmz = ti->private;
1039	int i;
1040
1041	flush_workqueue(dmz->chunk_wq);
1042	for (i = 0; i < dmz->nr_ddevs; i++)
1043		dmz_suspend_reclaim(dmz->dev[i].reclaim);
1044	cancel_delayed_work_sync(&dmz->flush_work);
1045}
1046
1047/*
1048 * Restart works on resume or if suspend failed.
1049 */
1050static void dmz_resume(struct dm_target *ti)
1051{
1052	struct dmz_target *dmz = ti->private;
1053	int i;
1054
1055	queue_delayed_work(dmz->flush_wq, &dmz->flush_work, DMZ_FLUSH_PERIOD);
1056	for (i = 0; i < dmz->nr_ddevs; i++)
1057		dmz_resume_reclaim(dmz->dev[i].reclaim);
1058}
1059
1060static int dmz_iterate_devices(struct dm_target *ti,
1061			       iterate_devices_callout_fn fn, void *data)
1062{
1063	struct dmz_target *dmz = ti->private;
1064	unsigned int zone_nr_sectors = dmz_zone_nr_sectors(dmz->metadata);
1065	sector_t capacity;
1066	int i, r;
1067
1068	for (i = 0; i < dmz->nr_ddevs; i++) {
1069		capacity = dmz->dev[i].capacity & ~(zone_nr_sectors - 1);
1070		r = fn(ti, dmz->ddev[i], 0, capacity, data);
1071		if (r)
1072			break;
1073	}
1074	return r;
1075}
1076
1077static void dmz_status(struct dm_target *ti, status_type_t type,
1078		       unsigned int status_flags, char *result,
1079		       unsigned int maxlen)
1080{
1081	struct dmz_target *dmz = ti->private;
1082	ssize_t sz = 0;
1083	char buf[BDEVNAME_SIZE];
1084	struct dmz_dev *dev;
1085	int i;
1086
1087	switch (type) {
1088	case STATUSTYPE_INFO:
1089		DMEMIT("%u zones %u/%u cache",
1090		       dmz_nr_zones(dmz->metadata),
1091		       dmz_nr_unmap_cache_zones(dmz->metadata),
1092		       dmz_nr_cache_zones(dmz->metadata));
1093		for (i = 0; i < dmz->nr_ddevs; i++) {
1094			/*
1095			 * For a multi-device setup the first device
1096			 * contains only cache zones.
1097			 */
1098			if ((i == 0) &&
1099			    (dmz_nr_cache_zones(dmz->metadata) > 0))
1100				continue;
1101			DMEMIT(" %u/%u random %u/%u sequential",
1102			       dmz_nr_unmap_rnd_zones(dmz->metadata, i),
1103			       dmz_nr_rnd_zones(dmz->metadata, i),
1104			       dmz_nr_unmap_seq_zones(dmz->metadata, i),
1105			       dmz_nr_seq_zones(dmz->metadata, i));
1106		}
1107		break;
1108	case STATUSTYPE_TABLE:
1109		dev = &dmz->dev[0];
1110		format_dev_t(buf, dev->bdev->bd_dev);
1111		DMEMIT("%s", buf);
1112		for (i = 1; i < dmz->nr_ddevs; i++) {
1113			dev = &dmz->dev[i];
1114			format_dev_t(buf, dev->bdev->bd_dev);
1115			DMEMIT(" %s", buf);
1116		}
1117		break;
1118	case STATUSTYPE_IMA:
1119		*result = '\0';
1120		break;
1121	}
1122	return;
1123}
1124
1125static int dmz_message(struct dm_target *ti, unsigned int argc, char **argv,
1126		       char *result, unsigned int maxlen)
1127{
1128	struct dmz_target *dmz = ti->private;
1129	int r = -EINVAL;
1130
1131	if (!strcasecmp(argv[0], "reclaim")) {
1132		int i;
1133
1134		for (i = 0; i < dmz->nr_ddevs; i++)
1135			dmz_schedule_reclaim(dmz->dev[i].reclaim);
1136		r = 0;
1137	} else
1138		DMERR("unrecognized message %s", argv[0]);
1139	return r;
1140}
1141
1142static struct target_type dmz_type = {
1143	.name		 = "zoned",
1144	.version	 = {2, 0, 0},
1145	.features	 = DM_TARGET_SINGLETON | DM_TARGET_MIXED_ZONED_MODEL,
1146	.module		 = THIS_MODULE,
1147	.ctr		 = dmz_ctr,
1148	.dtr		 = dmz_dtr,
1149	.map		 = dmz_map,
 
1150	.io_hints	 = dmz_io_hints,
1151	.prepare_ioctl	 = dmz_prepare_ioctl,
1152	.postsuspend	 = dmz_suspend,
1153	.resume		 = dmz_resume,
1154	.iterate_devices = dmz_iterate_devices,
1155	.status		 = dmz_status,
1156	.message	 = dmz_message,
1157};
1158
1159static int __init dmz_init(void)
1160{
1161	return dm_register_target(&dmz_type);
1162}
1163
1164static void __exit dmz_exit(void)
1165{
1166	dm_unregister_target(&dmz_type);
1167}
1168
1169module_init(dmz_init);
1170module_exit(dmz_exit);
1171
1172MODULE_DESCRIPTION(DM_NAME " target for zoned block devices");
1173MODULE_AUTHOR("Damien Le Moal <damien.lemoal@wdc.com>");
1174MODULE_LICENSE("GPL");
v4.17
 
  1/*
  2 * Copyright (C) 2017 Western Digital Corporation or its affiliates.
  3 *
  4 * This file is released under the GPL.
  5 */
  6
  7#include "dm-zoned.h"
  8
  9#include <linux/module.h>
 10
 11#define	DM_MSG_PREFIX		"zoned"
 12
 13#define DMZ_MIN_BIOS		8192
 14
 15/*
 16 * Zone BIO context.
 17 */
 18struct dmz_bioctx {
 19	struct dmz_target	*target;
 20	struct dm_zone		*zone;
 21	struct bio		*bio;
 22	atomic_t		ref;
 23	blk_status_t		status;
 24};
 25
 26/*
 27 * Chunk work descriptor.
 28 */
 29struct dm_chunk_work {
 30	struct work_struct	work;
 31	atomic_t		refcount;
 32	struct dmz_target	*target;
 33	unsigned int		chunk;
 34	struct bio_list		bio_list;
 35};
 36
 37/*
 38 * Target descriptor.
 39 */
 40struct dmz_target {
 41	struct dm_dev		*ddev;
 
 42
 43	unsigned long		flags;
 44
 45	/* Zoned block device information */
 46	struct dmz_dev		*dev;
 47
 48	/* For metadata handling */
 49	struct dmz_metadata     *metadata;
 50
 51	/* For reclaim */
 52	struct dmz_reclaim	*reclaim;
 53
 54	/* For chunk work */
 55	struct mutex		chunk_lock;
 56	struct radix_tree_root	chunk_rxtree;
 57	struct workqueue_struct *chunk_wq;
 
 58
 59	/* For cloned BIOs to zones */
 60	struct bio_set		*bio_set;
 61
 62	/* For flush */
 63	spinlock_t		flush_lock;
 64	struct bio_list		flush_list;
 65	struct delayed_work	flush_work;
 66	struct workqueue_struct *flush_wq;
 67};
 68
 69/*
 70 * Flush intervals (seconds).
 71 */
 72#define DMZ_FLUSH_PERIOD	(10 * HZ)
 73
 74/*
 75 * Target BIO completion.
 76 */
 77static inline void dmz_bio_endio(struct bio *bio, blk_status_t status)
 78{
 79	struct dmz_bioctx *bioctx = dm_per_bio_data(bio, sizeof(struct dmz_bioctx));
 
 
 
 
 
 
 
 
 
 80
 81	if (bioctx->status == BLK_STS_OK && status != BLK_STS_OK)
 82		bioctx->status = status;
 83	bio_endio(bio);
 
 
 
 
 
 
 84}
 85
 86/*
 87 * Partial clone read BIO completion callback. This terminates the
 88 * target BIO when there are no more references to its context.
 89 */
 90static void dmz_read_bio_end_io(struct bio *bio)
 91{
 92	struct dmz_bioctx *bioctx = bio->bi_private;
 93	blk_status_t status = bio->bi_status;
 94
 95	bio_put(bio);
 96	dmz_bio_endio(bioctx->bio, status);
 97}
 98
 99/*
100 * Issue a BIO to a zone. The BIO may only partially process the
101 * original target BIO.
102 */
103static int dmz_submit_read_bio(struct dmz_target *dmz, struct dm_zone *zone,
104			       struct bio *bio, sector_t chunk_block,
105			       unsigned int nr_blocks)
106{
107	struct dmz_bioctx *bioctx = dm_per_bio_data(bio, sizeof(struct dmz_bioctx));
108	sector_t sector;
 
109	struct bio *clone;
110
111	/* BIO remap sector */
112	sector = dmz_start_sect(dmz->metadata, zone) + dmz_blk2sect(chunk_block);
113
114	/* If the read is not partial, there is no need to clone the BIO */
115	if (nr_blocks == dmz_bio_blocks(bio)) {
116		/* Setup and submit the BIO */
117		bio->bi_iter.bi_sector = sector;
118		atomic_inc(&bioctx->ref);
119		generic_make_request(bio);
120		return 0;
121	}
122
123	/* Partial BIO: we need to clone the BIO */
124	clone = bio_clone_fast(bio, GFP_NOIO, dmz->bio_set);
125	if (!clone)
126		return -ENOMEM;
127
128	/* Setup the clone */
129	clone->bi_iter.bi_sector = sector;
 
130	clone->bi_iter.bi_size = dmz_blk2sect(nr_blocks) << SECTOR_SHIFT;
131	clone->bi_end_io = dmz_read_bio_end_io;
132	clone->bi_private = bioctx;
133
134	bio_advance(bio, clone->bi_iter.bi_size);
135
136	/* Submit the clone */
137	atomic_inc(&bioctx->ref);
138	generic_make_request(clone);
 
 
139
140	return 0;
141}
142
143/*
144 * Zero out pages of discarded blocks accessed by a read BIO.
145 */
146static void dmz_handle_read_zero(struct dmz_target *dmz, struct bio *bio,
147				 sector_t chunk_block, unsigned int nr_blocks)
148{
149	unsigned int size = nr_blocks << DMZ_BLOCK_SHIFT;
150
151	/* Clear nr_blocks */
152	swap(bio->bi_iter.bi_size, size);
153	zero_fill_bio(bio);
154	swap(bio->bi_iter.bi_size, size);
155
156	bio_advance(bio, size);
157}
158
159/*
160 * Process a read BIO.
161 */
162static int dmz_handle_read(struct dmz_target *dmz, struct dm_zone *zone,
163			   struct bio *bio)
164{
165	sector_t chunk_block = dmz_chunk_block(dmz->dev, dmz_bio_block(bio));
 
166	unsigned int nr_blocks = dmz_bio_blocks(bio);
167	sector_t end_block = chunk_block + nr_blocks;
168	struct dm_zone *rzone, *bzone;
169	int ret;
170
171	/* Read into unmapped chunks need only zeroing the BIO buffer */
172	if (!zone) {
173		zero_fill_bio(bio);
174		return 0;
175	}
176
177	dmz_dev_debug(dmz->dev, "READ chunk %llu -> %s zone %u, block %llu, %u blocks",
178		      (unsigned long long)dmz_bio_chunk(dmz->dev, bio),
179		      (dmz_is_rnd(zone) ? "RND" : "SEQ"),
180		      dmz_id(dmz->metadata, zone),
181		      (unsigned long long)chunk_block, nr_blocks);
 
 
182
183	/* Check block validity to determine the read location */
184	bzone = zone->bzone;
185	while (chunk_block < end_block) {
186		nr_blocks = 0;
187		if (dmz_is_rnd(zone) || chunk_block < zone->wp_block) {
 
188			/* Test block validity in the data zone */
189			ret = dmz_block_valid(dmz->metadata, zone, chunk_block);
190			if (ret < 0)
191				return ret;
192			if (ret > 0) {
193				/* Read data zone blocks */
194				nr_blocks = ret;
195				rzone = zone;
196			}
197		}
198
199		/*
200		 * No valid blocks found in the data zone.
201		 * Check the buffer zone, if there is one.
202		 */
203		if (!nr_blocks && bzone) {
204			ret = dmz_block_valid(dmz->metadata, bzone, chunk_block);
205			if (ret < 0)
206				return ret;
207			if (ret > 0) {
208				/* Read buffer zone blocks */
209				nr_blocks = ret;
210				rzone = bzone;
211			}
212		}
213
214		if (nr_blocks) {
215			/* Valid blocks found: read them */
216			nr_blocks = min_t(unsigned int, nr_blocks, end_block - chunk_block);
217			ret = dmz_submit_read_bio(dmz, rzone, bio, chunk_block, nr_blocks);
 
 
218			if (ret)
219				return ret;
220			chunk_block += nr_blocks;
221		} else {
222			/* No valid block: zeroout the current BIO block */
223			dmz_handle_read_zero(dmz, bio, chunk_block, 1);
224			chunk_block++;
225		}
226	}
227
228	return 0;
229}
230
231/*
232 * Issue a write BIO to a zone.
233 */
234static void dmz_submit_write_bio(struct dmz_target *dmz, struct dm_zone *zone,
235				 struct bio *bio, sector_t chunk_block,
236				 unsigned int nr_blocks)
237{
238	struct dmz_bioctx *bioctx = dm_per_bio_data(bio, sizeof(struct dmz_bioctx));
239
240	/* Setup and submit the BIO */
241	bio_set_dev(bio, dmz->dev->bdev);
242	bio->bi_iter.bi_sector = dmz_start_sect(dmz->metadata, zone) + dmz_blk2sect(chunk_block);
243	atomic_inc(&bioctx->ref);
244	generic_make_request(bio);
245
246	if (dmz_is_seq(zone))
247		zone->wp_block += nr_blocks;
248}
249
250/*
251 * Write blocks directly in a data zone, at the write pointer.
252 * If a buffer zone is assigned, invalidate the blocks written
253 * in place.
254 */
255static int dmz_handle_direct_write(struct dmz_target *dmz,
256				   struct dm_zone *zone, struct bio *bio,
257				   sector_t chunk_block,
258				   unsigned int nr_blocks)
259{
260	struct dmz_metadata *zmd = dmz->metadata;
261	struct dm_zone *bzone = zone->bzone;
262	int ret;
263
264	if (dmz_is_readonly(zone))
265		return -EROFS;
266
267	/* Submit write */
268	dmz_submit_write_bio(dmz, zone, bio, chunk_block, nr_blocks);
 
 
269
270	/*
271	 * Validate the blocks in the data zone and invalidate
272	 * in the buffer zone, if there is one.
273	 */
274	ret = dmz_validate_blocks(zmd, zone, chunk_block, nr_blocks);
275	if (ret == 0 && bzone)
276		ret = dmz_invalidate_blocks(zmd, bzone, chunk_block, nr_blocks);
277
278	return ret;
279}
280
281/*
282 * Write blocks in the buffer zone of @zone.
283 * If no buffer zone is assigned yet, get one.
284 * Called with @zone write locked.
285 */
286static int dmz_handle_buffered_write(struct dmz_target *dmz,
287				     struct dm_zone *zone, struct bio *bio,
288				     sector_t chunk_block,
289				     unsigned int nr_blocks)
290{
291	struct dmz_metadata *zmd = dmz->metadata;
292	struct dm_zone *bzone;
293	int ret;
294
295	/* Get the buffer zone. One will be allocated if needed */
296	bzone = dmz_get_chunk_buffer(zmd, zone);
297	if (!bzone)
298		return -ENOSPC;
299
300	if (dmz_is_readonly(bzone))
301		return -EROFS;
302
303	/* Submit write */
304	dmz_submit_write_bio(dmz, bzone, bio, chunk_block, nr_blocks);
 
 
305
306	/*
307	 * Validate the blocks in the buffer zone
308	 * and invalidate in the data zone.
309	 */
310	ret = dmz_validate_blocks(zmd, bzone, chunk_block, nr_blocks);
311	if (ret == 0 && chunk_block < zone->wp_block)
312		ret = dmz_invalidate_blocks(zmd, zone, chunk_block, nr_blocks);
313
314	return ret;
315}
316
317/*
318 * Process a write BIO.
319 */
320static int dmz_handle_write(struct dmz_target *dmz, struct dm_zone *zone,
321			    struct bio *bio)
322{
323	sector_t chunk_block = dmz_chunk_block(dmz->dev, dmz_bio_block(bio));
 
324	unsigned int nr_blocks = dmz_bio_blocks(bio);
325
326	if (!zone)
327		return -ENOSPC;
328
329	dmz_dev_debug(dmz->dev, "WRITE chunk %llu -> %s zone %u, block %llu, %u blocks",
330		      (unsigned long long)dmz_bio_chunk(dmz->dev, bio),
331		      (dmz_is_rnd(zone) ? "RND" : "SEQ"),
332		      dmz_id(dmz->metadata, zone),
333		      (unsigned long long)chunk_block, nr_blocks);
 
 
334
335	if (dmz_is_rnd(zone) || chunk_block == zone->wp_block) {
 
336		/*
337		 * zone is a random zone or it is a sequential zone
338		 * and the BIO is aligned to the zone write pointer:
339		 * direct write the zone.
340		 */
341		return dmz_handle_direct_write(dmz, zone, bio, chunk_block, nr_blocks);
 
342	}
343
344	/*
345	 * This is an unaligned write in a sequential zone:
346	 * use buffered write.
347	 */
348	return dmz_handle_buffered_write(dmz, zone, bio, chunk_block, nr_blocks);
349}
350
351/*
352 * Process a discard BIO.
353 */
354static int dmz_handle_discard(struct dmz_target *dmz, struct dm_zone *zone,
355			      struct bio *bio)
356{
357	struct dmz_metadata *zmd = dmz->metadata;
358	sector_t block = dmz_bio_block(bio);
359	unsigned int nr_blocks = dmz_bio_blocks(bio);
360	sector_t chunk_block = dmz_chunk_block(dmz->dev, block);
361	int ret = 0;
362
363	/* For unmapped chunks, there is nothing to do */
364	if (!zone)
365		return 0;
366
367	if (dmz_is_readonly(zone))
368		return -EROFS;
369
370	dmz_dev_debug(dmz->dev, "DISCARD chunk %llu -> zone %u, block %llu, %u blocks",
371		      (unsigned long long)dmz_bio_chunk(dmz->dev, bio),
372		      dmz_id(zmd, zone),
373		      (unsigned long long)chunk_block, nr_blocks);
 
374
375	/*
376	 * Invalidate blocks in the data zone and its
377	 * buffer zone if one is mapped.
378	 */
379	if (dmz_is_rnd(zone) || chunk_block < zone->wp_block)
 
380		ret = dmz_invalidate_blocks(zmd, zone, chunk_block, nr_blocks);
381	if (ret == 0 && zone->bzone)
382		ret = dmz_invalidate_blocks(zmd, zone->bzone,
383					    chunk_block, nr_blocks);
384	return ret;
385}
386
387/*
388 * Process a BIO.
389 */
390static void dmz_handle_bio(struct dmz_target *dmz, struct dm_chunk_work *cw,
391			   struct bio *bio)
392{
393	struct dmz_bioctx *bioctx = dm_per_bio_data(bio, sizeof(struct dmz_bioctx));
 
394	struct dmz_metadata *zmd = dmz->metadata;
395	struct dm_zone *zone;
396	int ret;
397
398	/*
399	 * Write may trigger a zone allocation. So make sure the
400	 * allocation can succeed.
401	 */
402	if (bio_op(bio) == REQ_OP_WRITE)
403		dmz_schedule_reclaim(dmz->reclaim);
404
405	dmz_lock_metadata(zmd);
406
407	/*
408	 * Get the data zone mapping the chunk. There may be no
409	 * mapping for read and discard. If a mapping is obtained,
410	 + the zone returned will be set to active state.
411	 */
412	zone = dmz_get_chunk_mapping(zmd, dmz_bio_chunk(dmz->dev, bio),
413				     bio_op(bio));
414	if (IS_ERR(zone)) {
415		ret = PTR_ERR(zone);
416		goto out;
417	}
418
419	/* Process the BIO */
420	if (zone) {
421		dmz_activate_zone(zone);
422		bioctx->zone = zone;
 
423	}
424
425	switch (bio_op(bio)) {
426	case REQ_OP_READ:
427		ret = dmz_handle_read(dmz, zone, bio);
428		break;
429	case REQ_OP_WRITE:
430		ret = dmz_handle_write(dmz, zone, bio);
431		break;
432	case REQ_OP_DISCARD:
433	case REQ_OP_WRITE_ZEROES:
434		ret = dmz_handle_discard(dmz, zone, bio);
435		break;
436	default:
437		dmz_dev_err(dmz->dev, "Unsupported BIO operation 0x%x",
438			    bio_op(bio));
439		ret = -EIO;
440	}
441
442	/*
443	 * Release the chunk mapping. This will check that the mapping
444	 * is still valid, that is, that the zone used still has valid blocks.
445	 */
446	if (zone)
447		dmz_put_chunk_mapping(zmd, zone);
448out:
449	dmz_bio_endio(bio, errno_to_blk_status(ret));
450
451	dmz_unlock_metadata(zmd);
452}
453
454/*
455 * Increment a chunk reference counter.
456 */
457static inline void dmz_get_chunk_work(struct dm_chunk_work *cw)
458{
459	atomic_inc(&cw->refcount);
460}
461
462/*
463 * Decrement a chunk work reference count and
464 * free it if it becomes 0.
465 */
466static void dmz_put_chunk_work(struct dm_chunk_work *cw)
467{
468	if (atomic_dec_and_test(&cw->refcount)) {
469		WARN_ON(!bio_list_empty(&cw->bio_list));
470		radix_tree_delete(&cw->target->chunk_rxtree, cw->chunk);
471		kfree(cw);
472	}
473}
474
475/*
476 * Chunk BIO work function.
477 */
478static void dmz_chunk_work(struct work_struct *work)
479{
480	struct dm_chunk_work *cw = container_of(work, struct dm_chunk_work, work);
481	struct dmz_target *dmz = cw->target;
482	struct bio *bio;
483
484	mutex_lock(&dmz->chunk_lock);
485
486	/* Process the chunk BIOs */
487	while ((bio = bio_list_pop(&cw->bio_list))) {
488		mutex_unlock(&dmz->chunk_lock);
489		dmz_handle_bio(dmz, cw, bio);
490		mutex_lock(&dmz->chunk_lock);
491		dmz_put_chunk_work(cw);
492	}
493
494	/* Queueing the work incremented the work refcount */
495	dmz_put_chunk_work(cw);
496
497	mutex_unlock(&dmz->chunk_lock);
498}
499
500/*
501 * Flush work.
502 */
503static void dmz_flush_work(struct work_struct *work)
504{
505	struct dmz_target *dmz = container_of(work, struct dmz_target, flush_work.work);
506	struct bio *bio;
507	int ret;
508
509	/* Flush dirty metadata blocks */
510	ret = dmz_flush_metadata(dmz->metadata);
 
 
 
511
512	/* Process queued flush requests */
513	while (1) {
514		spin_lock(&dmz->flush_lock);
515		bio = bio_list_pop(&dmz->flush_list);
516		spin_unlock(&dmz->flush_lock);
517
518		if (!bio)
519			break;
520
521		dmz_bio_endio(bio, errno_to_blk_status(ret));
522	}
523
524	queue_delayed_work(dmz->flush_wq, &dmz->flush_work, DMZ_FLUSH_PERIOD);
525}
526
527/*
528 * Get a chunk work and start it to process a new BIO.
529 * If the BIO chunk has no work yet, create one.
530 */
531static void dmz_queue_chunk_work(struct dmz_target *dmz, struct bio *bio)
532{
533	unsigned int chunk = dmz_bio_chunk(dmz->dev, bio);
534	struct dm_chunk_work *cw;
 
535
536	mutex_lock(&dmz->chunk_lock);
537
538	/* Get the BIO chunk work. If one is not active yet, create one */
539	cw = radix_tree_lookup(&dmz->chunk_rxtree, chunk);
540	if (!cw) {
541		int ret;
542
543		/* Create a new chunk work */
544		cw = kmalloc(sizeof(struct dm_chunk_work), GFP_NOIO);
545		if (!cw)
 
546			goto out;
 
547
548		INIT_WORK(&cw->work, dmz_chunk_work);
549		atomic_set(&cw->refcount, 0);
550		cw->target = dmz;
551		cw->chunk = chunk;
552		bio_list_init(&cw->bio_list);
553
554		ret = radix_tree_insert(&dmz->chunk_rxtree, chunk, cw);
555		if (unlikely(ret)) {
556			kfree(cw);
557			cw = NULL;
558			goto out;
559		}
560	}
561
562	bio_list_add(&cw->bio_list, bio);
563	dmz_get_chunk_work(cw);
564
565	if (queue_work(dmz->chunk_wq, &cw->work))
566		dmz_get_chunk_work(cw);
567out:
568	mutex_unlock(&dmz->chunk_lock);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
569}
570
571/*
572 * Process a new BIO.
573 */
574static int dmz_map(struct dm_target *ti, struct bio *bio)
575{
576	struct dmz_target *dmz = ti->private;
577	struct dmz_dev *dev = dmz->dev;
578	struct dmz_bioctx *bioctx = dm_per_bio_data(bio, sizeof(struct dmz_bioctx));
579	sector_t sector = bio->bi_iter.bi_sector;
580	unsigned int nr_sectors = bio_sectors(bio);
581	sector_t chunk_sector;
 
582
583	dmz_dev_debug(dev, "BIO op %d sector %llu + %u => chunk %llu, block %llu, %u blocks",
584		      bio_op(bio), (unsigned long long)sector, nr_sectors,
585		      (unsigned long long)dmz_bio_chunk(dmz->dev, bio),
586		      (unsigned long long)dmz_chunk_block(dmz->dev, dmz_bio_block(bio)),
587		      (unsigned int)dmz_bio_blocks(bio));
588
589	bio_set_dev(bio, dev->bdev);
 
 
 
 
 
590
591	if (!nr_sectors && bio_op(bio) != REQ_OP_WRITE)
592		return DM_MAPIO_REMAPPED;
593
594	/* The BIO should be block aligned */
595	if ((nr_sectors & DMZ_BLOCK_SECTORS_MASK) || (sector & DMZ_BLOCK_SECTORS_MASK))
596		return DM_MAPIO_KILL;
597
598	/* Initialize the BIO context */
599	bioctx->target = dmz;
600	bioctx->zone = NULL;
601	bioctx->bio = bio;
602	atomic_set(&bioctx->ref, 1);
603	bioctx->status = BLK_STS_OK;
604
605	/* Set the BIO pending in the flush list */
606	if (!nr_sectors && bio_op(bio) == REQ_OP_WRITE) {
607		spin_lock(&dmz->flush_lock);
608		bio_list_add(&dmz->flush_list, bio);
609		spin_unlock(&dmz->flush_lock);
610		mod_delayed_work(dmz->flush_wq, &dmz->flush_work, 0);
611		return DM_MAPIO_SUBMITTED;
612	}
613
614	/* Split zone BIOs to fit entirely into a zone */
615	chunk_sector = sector & (dev->zone_nr_sectors - 1);
616	if (chunk_sector + nr_sectors > dev->zone_nr_sectors)
617		dm_accept_partial_bio(bio, dev->zone_nr_sectors - chunk_sector);
618
619	/* Now ready to handle this BIO */
620	dmz_reclaim_bio_acc(dmz->reclaim);
621	dmz_queue_chunk_work(dmz, bio);
 
 
 
 
 
 
622
623	return DM_MAPIO_SUBMITTED;
624}
625
626/*
627 * Completed target BIO processing.
628 */
629static int dmz_end_io(struct dm_target *ti, struct bio *bio, blk_status_t *error)
630{
631	struct dmz_bioctx *bioctx = dm_per_bio_data(bio, sizeof(struct dmz_bioctx));
632
633	if (bioctx->status == BLK_STS_OK && *error)
634		bioctx->status = *error;
635
636	if (!atomic_dec_and_test(&bioctx->ref))
637		return DM_ENDIO_INCOMPLETE;
638
639	/* Done */
640	bio->bi_status = bioctx->status;
641
642	if (bioctx->zone) {
643		struct dm_zone *zone = bioctx->zone;
644
645		if (*error && bio_op(bio) == REQ_OP_WRITE) {
646			if (dmz_is_seq(zone))
647				set_bit(DMZ_SEQ_WRITE_ERR, &zone->flags);
648		}
649		dmz_deactivate_zone(zone);
650	}
651
652	return DM_ENDIO_DONE;
653}
654
655/*
656 * Get zoned device information.
657 */
658static int dmz_get_zoned_device(struct dm_target *ti, char *path)
 
659{
660	struct dmz_target *dmz = ti->private;
661	struct request_queue *q;
662	struct dmz_dev *dev;
663	sector_t aligned_capacity;
664	int ret;
 
665
666	/* Get the target device */
667	ret = dm_get_device(ti, path, dm_table_get_mode(ti->table), &dmz->ddev);
668	if (ret) {
669		ti->error = "Get target device failed";
670		dmz->ddev = NULL;
671		return ret;
672	}
673
674	dev = kzalloc(sizeof(struct dmz_dev), GFP_KERNEL);
675	if (!dev) {
676		ret = -ENOMEM;
677		goto err;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
678	}
 
 
679
680	dev->bdev = dmz->ddev->bdev;
681	(void)bdevname(dev->bdev, dev->name);
682
683	if (bdev_zoned_model(dev->bdev) == BLK_ZONED_NONE) {
684		ti->error = "Not a zoned block device";
685		ret = -EINVAL;
686		goto err;
687	}
688
689	q = bdev_get_queue(dev->bdev);
690	dev->capacity = i_size_read(dev->bdev->bd_inode) >> SECTOR_SHIFT;
691	aligned_capacity = dev->capacity & ~(blk_queue_zone_sectors(q) - 1);
692	if (ti->begin ||
693	    ((ti->len != dev->capacity) && (ti->len != aligned_capacity))) {
694		ti->error = "Partial mapping not supported";
695		ret = -EINVAL;
696		goto err;
697	}
698
699	dev->zone_nr_sectors = blk_queue_zone_sectors(q);
700	dev->zone_nr_sectors_shift = ilog2(dev->zone_nr_sectors);
701
702	dev->zone_nr_blocks = dmz_sect2blk(dev->zone_nr_sectors);
703	dev->zone_nr_blocks_shift = ilog2(dev->zone_nr_blocks);
704
705	dev->nr_zones = (dev->capacity + dev->zone_nr_sectors - 1)
706		>> dev->zone_nr_sectors_shift;
707
708	dmz->dev = dev;
709
710	return 0;
711err:
712	dm_put_device(ti, dmz->ddev);
713	kfree(dev);
714
715	return ret;
716}
717
718/*
719 * Cleanup zoned device information.
720 */
721static void dmz_put_zoned_device(struct dm_target *ti)
722{
723	struct dmz_target *dmz = ti->private;
 
724
725	dm_put_device(ti, dmz->ddev);
726	kfree(dmz->dev);
727	dmz->dev = NULL;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
728}
729
730/*
731 * Setup target.
732 */
733static int dmz_ctr(struct dm_target *ti, unsigned int argc, char **argv)
734{
735	struct dmz_target *dmz;
736	struct dmz_dev *dev;
737	int ret;
738
739	/* Check arguments */
740	if (argc != 1) {
741		ti->error = "Invalid argument count";
742		return -EINVAL;
743	}
744
745	/* Allocate and initialize the target descriptor */
746	dmz = kzalloc(sizeof(struct dmz_target), GFP_KERNEL);
747	if (!dmz) {
748		ti->error = "Unable to allocate the zoned target descriptor";
749		return -ENOMEM;
750	}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
751	ti->private = dmz;
752
753	/* Get the target zoned block device */
754	ret = dmz_get_zoned_device(ti, argv[0]);
755	if (ret) {
756		dmz->ddev = NULL;
757		goto err;
758	}
 
 
 
759
760	/* Initialize metadata */
761	dev = dmz->dev;
762	ret = dmz_ctr_metadata(dev, &dmz->metadata);
763	if (ret) {
764		ti->error = "Metadata initialization failed";
765		goto err_dev;
766	}
767
768	/* Set target (no write same support) */
769	ti->max_io_len = dev->zone_nr_sectors << 9;
770	ti->num_flush_bios = 1;
771	ti->num_discard_bios = 1;
772	ti->num_write_zeroes_bios = 1;
773	ti->per_io_data_size = sizeof(struct dmz_bioctx);
774	ti->flush_supported = true;
775	ti->discards_supported = true;
776	ti->split_discard_bios = true;
777
778	/* The exposed capacity is the number of chunks that can be mapped */
779	ti->len = (sector_t)dmz_nr_chunks(dmz->metadata) << dev->zone_nr_sectors_shift;
 
780
781	/* Zone BIO */
782	dmz->bio_set = bioset_create(DMZ_MIN_BIOS, 0, 0);
783	if (!dmz->bio_set) {
784		ti->error = "Create BIO set failed";
785		ret = -ENOMEM;
786		goto err_meta;
787	}
788
789	/* Chunk BIO work */
790	mutex_init(&dmz->chunk_lock);
791	INIT_RADIX_TREE(&dmz->chunk_rxtree, GFP_KERNEL);
792	dmz->chunk_wq = alloc_workqueue("dmz_cwq_%s", WQ_MEM_RECLAIM | WQ_UNBOUND,
793					0, dev->name);
 
794	if (!dmz->chunk_wq) {
795		ti->error = "Create chunk workqueue failed";
796		ret = -ENOMEM;
797		goto err_bio;
798	}
799
800	/* Flush work */
801	spin_lock_init(&dmz->flush_lock);
802	bio_list_init(&dmz->flush_list);
803	INIT_DELAYED_WORK(&dmz->flush_work, dmz_flush_work);
804	dmz->flush_wq = alloc_ordered_workqueue("dmz_fwq_%s", WQ_MEM_RECLAIM,
805						dev->name);
806	if (!dmz->flush_wq) {
807		ti->error = "Create flush workqueue failed";
808		ret = -ENOMEM;
809		goto err_cwq;
810	}
811	mod_delayed_work(dmz->flush_wq, &dmz->flush_work, DMZ_FLUSH_PERIOD);
812
813	/* Initialize reclaim */
814	ret = dmz_ctr_reclaim(dev, dmz->metadata, &dmz->reclaim);
815	if (ret) {
816		ti->error = "Zone reclaim initialization failed";
817		goto err_fwq;
 
 
818	}
819
820	dmz_dev_info(dev, "Target device: %llu 512-byte logical sectors (%llu blocks)",
821		     (unsigned long long)ti->len,
822		     (unsigned long long)dmz_sect2blk(ti->len));
 
823
824	return 0;
825err_fwq:
826	destroy_workqueue(dmz->flush_wq);
827err_cwq:
828	destroy_workqueue(dmz->chunk_wq);
829err_bio:
830	mutex_destroy(&dmz->chunk_lock);
831	bioset_free(dmz->bio_set);
832err_meta:
833	dmz_dtr_metadata(dmz->metadata);
834err_dev:
835	dmz_put_zoned_device(ti);
836err:
 
837	kfree(dmz);
838
839	return ret;
840}
841
842/*
843 * Cleanup target.
844 */
845static void dmz_dtr(struct dm_target *ti)
846{
847	struct dmz_target *dmz = ti->private;
 
848
849	flush_workqueue(dmz->chunk_wq);
850	destroy_workqueue(dmz->chunk_wq);
851
852	dmz_dtr_reclaim(dmz->reclaim);
 
853
854	cancel_delayed_work_sync(&dmz->flush_work);
855	destroy_workqueue(dmz->flush_wq);
856
857	(void) dmz_flush_metadata(dmz->metadata);
858
859	dmz_dtr_metadata(dmz->metadata);
860
861	bioset_free(dmz->bio_set);
862
863	dmz_put_zoned_device(ti);
864
865	mutex_destroy(&dmz->chunk_lock);
866
 
867	kfree(dmz);
868}
869
870/*
871 * Setup target request queue limits.
872 */
873static void dmz_io_hints(struct dm_target *ti, struct queue_limits *limits)
874{
875	struct dmz_target *dmz = ti->private;
876	unsigned int chunk_sectors = dmz->dev->zone_nr_sectors;
877
878	limits->logical_block_size = DMZ_BLOCK_SIZE;
879	limits->physical_block_size = DMZ_BLOCK_SIZE;
880
881	blk_limits_io_min(limits, DMZ_BLOCK_SIZE);
882	blk_limits_io_opt(limits, DMZ_BLOCK_SIZE);
883
884	limits->discard_alignment = DMZ_BLOCK_SIZE;
885	limits->discard_granularity = DMZ_BLOCK_SIZE;
886	limits->max_discard_sectors = chunk_sectors;
887	limits->max_hw_discard_sectors = chunk_sectors;
888	limits->max_write_zeroes_sectors = chunk_sectors;
889
890	/* FS hint to try to align to the device zone size */
891	limits->chunk_sectors = chunk_sectors;
892	limits->max_sectors = chunk_sectors;
893
894	/* We are exposing a drive-managed zoned block device */
895	limits->zoned = BLK_ZONED_NONE;
896}
897
898/*
899 * Pass on ioctl to the backend device.
900 */
901static int dmz_prepare_ioctl(struct dm_target *ti, struct block_device **bdev)
902{
903	struct dmz_target *dmz = ti->private;
 
 
 
 
904
905	*bdev = dmz->dev->bdev;
906
907	return 0;
908}
909
910/*
911 * Stop works on suspend.
912 */
913static void dmz_suspend(struct dm_target *ti)
914{
915	struct dmz_target *dmz = ti->private;
 
916
917	flush_workqueue(dmz->chunk_wq);
918	dmz_suspend_reclaim(dmz->reclaim);
 
919	cancel_delayed_work_sync(&dmz->flush_work);
920}
921
922/*
923 * Restart works on resume or if suspend failed.
924 */
925static void dmz_resume(struct dm_target *ti)
926{
927	struct dmz_target *dmz = ti->private;
 
928
929	queue_delayed_work(dmz->flush_wq, &dmz->flush_work, DMZ_FLUSH_PERIOD);
930	dmz_resume_reclaim(dmz->reclaim);
 
931}
932
933static int dmz_iterate_devices(struct dm_target *ti,
934			       iterate_devices_callout_fn fn, void *data)
935{
936	struct dmz_target *dmz = ti->private;
937	struct dmz_dev *dev = dmz->dev;
938	sector_t capacity = dev->capacity & ~(dev->zone_nr_sectors - 1);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
939
940	return fn(ti, dmz->ddev, 0, capacity, data);
 
 
 
 
 
941}
942
943static struct target_type dmz_type = {
944	.name		 = "zoned",
945	.version	 = {1, 0, 0},
946	.features	 = DM_TARGET_SINGLETON | DM_TARGET_ZONED_HM,
947	.module		 = THIS_MODULE,
948	.ctr		 = dmz_ctr,
949	.dtr		 = dmz_dtr,
950	.map		 = dmz_map,
951	.end_io		 = dmz_end_io,
952	.io_hints	 = dmz_io_hints,
953	.prepare_ioctl	 = dmz_prepare_ioctl,
954	.postsuspend	 = dmz_suspend,
955	.resume		 = dmz_resume,
956	.iterate_devices = dmz_iterate_devices,
 
 
957};
958
959static int __init dmz_init(void)
960{
961	return dm_register_target(&dmz_type);
962}
963
964static void __exit dmz_exit(void)
965{
966	dm_unregister_target(&dmz_type);
967}
968
969module_init(dmz_init);
970module_exit(dmz_exit);
971
972MODULE_DESCRIPTION(DM_NAME " target for zoned block devices");
973MODULE_AUTHOR("Damien Le Moal <damien.lemoal@wdc.com>");
974MODULE_LICENSE("GPL");