xref: /linux/drivers/target/target_core_iblock.c (revision b4ada0618eed0fbd1b1630f73deb048c592b06a1)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*******************************************************************************
3  * Filename:  target_core_iblock.c
4  *
5  * This file contains the Storage Engine  <-> Linux BlockIO transport
6  * specific functions.
7  *
8  * (c) Copyright 2003-2013 Datera, Inc.
9  *
10  * Nicholas A. Bellinger <nab@kernel.org>
11  *
12  ******************************************************************************/
13 
14 #include <linux/string.h>
15 #include <linux/parser.h>
16 #include <linux/timer.h>
17 #include <linux/fs.h>
18 #include <linux/blkdev.h>
19 #include <linux/blk-integrity.h>
20 #include <linux/slab.h>
21 #include <linux/spinlock.h>
22 #include <linux/bio.h>
23 #include <linux/file.h>
24 #include <linux/module.h>
25 #include <linux/scatterlist.h>
26 #include <linux/pr.h>
27 #include <scsi/scsi_proto.h>
28 #include <scsi/scsi_common.h>
29 #include <linux/unaligned.h>
30 
31 #include <target/target_core_base.h>
32 #include <target/target_core_backend.h>
33 
34 #include "target_core_iblock.h"
35 #include "target_core_pr.h"
36 
37 #define IBLOCK_MAX_BIO_PER_TASK	 32	/* max # of bios to submit at a time */
38 #define IBLOCK_BIO_POOL_SIZE	128
39 
40 static inline struct iblock_dev *IBLOCK_DEV(struct se_device *dev)
41 {
42 	return container_of(dev, struct iblock_dev, dev);
43 }
44 
45 
46 static int iblock_attach_hba(struct se_hba *hba, u32 host_id)
47 {
48 	pr_debug("CORE_HBA[%d] - TCM iBlock HBA Driver %s on"
49 		" Generic Target Core Stack %s\n", hba->hba_id,
50 		IBLOCK_VERSION, TARGET_CORE_VERSION);
51 	return 0;
52 }
53 
54 static void iblock_detach_hba(struct se_hba *hba)
55 {
56 }
57 
58 static struct se_device *iblock_alloc_device(struct se_hba *hba, const char *name)
59 {
60 	struct iblock_dev *ib_dev = NULL;
61 
62 	ib_dev = kzalloc(sizeof(struct iblock_dev), GFP_KERNEL);
63 	if (!ib_dev) {
64 		pr_err("Unable to allocate struct iblock_dev\n");
65 		return NULL;
66 	}
67 	ib_dev->ibd_exclusive = true;
68 
69 	ib_dev->ibd_plug = kcalloc(nr_cpu_ids, sizeof(*ib_dev->ibd_plug),
70 				   GFP_KERNEL);
71 	if (!ib_dev->ibd_plug)
72 		goto free_dev;
73 
74 	pr_debug( "IBLOCK: Allocated ib_dev for %s\n", name);
75 
76 	return &ib_dev->dev;
77 
78 free_dev:
79 	kfree(ib_dev);
80 	return NULL;
81 }
82 
83 static bool iblock_configure_unmap(struct se_device *dev)
84 {
85 	struct iblock_dev *ib_dev = IBLOCK_DEV(dev);
86 
87 	return target_configure_unmap_from_queue(&dev->dev_attrib,
88 						 ib_dev->ibd_bd);
89 }
90 
91 static int iblock_configure_device(struct se_device *dev)
92 {
93 	struct iblock_dev *ib_dev = IBLOCK_DEV(dev);
94 	struct request_queue *q;
95 	struct file *bdev_file;
96 	struct block_device *bd;
97 	struct blk_integrity *bi;
98 	blk_mode_t mode = BLK_OPEN_READ;
99 	void *holder = ib_dev;
100 	unsigned int max_write_zeroes_sectors;
101 	int ret;
102 
103 	if (!(ib_dev->ibd_flags & IBDF_HAS_UDEV_PATH)) {
104 		pr_err("Missing udev_path= parameters for IBLOCK\n");
105 		return -EINVAL;
106 	}
107 
108 	ret = bioset_init(&ib_dev->ibd_bio_set, IBLOCK_BIO_POOL_SIZE, 0, BIOSET_NEED_BVECS);
109 	if (ret) {
110 		pr_err("IBLOCK: Unable to create bioset\n");
111 		goto out;
112 	}
113 
114 	pr_debug("IBLOCK: Claiming struct block_device: %s: %d\n",
115 		 ib_dev->ibd_udev_path, ib_dev->ibd_exclusive);
116 
117 	if (!ib_dev->ibd_readonly)
118 		mode |= BLK_OPEN_WRITE;
119 	else
120 		dev->dev_flags |= DF_READ_ONLY;
121 
122 	if (!ib_dev->ibd_exclusive)
123 		holder = NULL;
124 
125 	bdev_file = bdev_file_open_by_path(ib_dev->ibd_udev_path, mode, holder,
126 					NULL);
127 	if (IS_ERR(bdev_file)) {
128 		ret = PTR_ERR(bdev_file);
129 		goto out_free_bioset;
130 	}
131 	ib_dev->ibd_bdev_file = bdev_file;
132 	ib_dev->ibd_bd = bd = file_bdev(bdev_file);
133 
134 	q = bdev_get_queue(bd);
135 
136 	dev->dev_attrib.hw_block_size = bdev_logical_block_size(bd);
137 	dev->dev_attrib.hw_max_sectors = mult_frac(queue_max_hw_sectors(q),
138 			SECTOR_SIZE,
139 			dev->dev_attrib.hw_block_size);
140 	dev->dev_attrib.hw_queue_depth = q->nr_requests;
141 
142 	/*
143 	 * Enable write same emulation for IBLOCK and use 0xFFFF as
144 	 * the smaller WRITE_SAME(10) only has a two-byte block count.
145 	 */
146 	max_write_zeroes_sectors = bdev_write_zeroes_sectors(bd);
147 	if (max_write_zeroes_sectors)
148 		dev->dev_attrib.max_write_same_len = max_write_zeroes_sectors;
149 	else
150 		dev->dev_attrib.max_write_same_len = 0xFFFF;
151 
152 	if (bdev_nonrot(bd))
153 		dev->dev_attrib.is_nonrot = 1;
154 
155 	bi = bdev_get_integrity(bd);
156 	if (!bi)
157 		return 0;
158 
159 	switch (bi->csum_type) {
160 	case BLK_INTEGRITY_CSUM_IP:
161 		pr_err("IBLOCK export of blk_integrity: %s not supported\n",
162 			blk_integrity_profile_name(bi));
163 		ret = -ENOSYS;
164 		goto out_blkdev_put;
165 	case BLK_INTEGRITY_CSUM_CRC:
166 		if (bi->flags & BLK_INTEGRITY_REF_TAG)
167 			dev->dev_attrib.pi_prot_type = TARGET_DIF_TYPE1_PROT;
168 		else
169 			dev->dev_attrib.pi_prot_type = TARGET_DIF_TYPE3_PROT;
170 		break;
171 	default:
172 		break;
173 	}
174 
175 	dev->dev_attrib.hw_pi_prot_type = dev->dev_attrib.pi_prot_type;
176 	return 0;
177 
178 out_blkdev_put:
179 	fput(ib_dev->ibd_bdev_file);
180 out_free_bioset:
181 	bioset_exit(&ib_dev->ibd_bio_set);
182 out:
183 	return ret;
184 }
185 
186 static void iblock_dev_call_rcu(struct rcu_head *p)
187 {
188 	struct se_device *dev = container_of(p, struct se_device, rcu_head);
189 	struct iblock_dev *ib_dev = IBLOCK_DEV(dev);
190 
191 	kfree(ib_dev->ibd_plug);
192 	kfree(ib_dev);
193 }
194 
195 static void iblock_free_device(struct se_device *dev)
196 {
197 	call_rcu(&dev->rcu_head, iblock_dev_call_rcu);
198 }
199 
200 static void iblock_destroy_device(struct se_device *dev)
201 {
202 	struct iblock_dev *ib_dev = IBLOCK_DEV(dev);
203 
204 	if (ib_dev->ibd_bdev_file)
205 		fput(ib_dev->ibd_bdev_file);
206 	bioset_exit(&ib_dev->ibd_bio_set);
207 }
208 
209 static struct se_dev_plug *iblock_plug_device(struct se_device *se_dev)
210 {
211 	struct iblock_dev *ib_dev = IBLOCK_DEV(se_dev);
212 	struct iblock_dev_plug *ib_dev_plug;
213 
214 	/*
215 	 * Each se_device has a per cpu work this can be run from. We
216 	 * shouldn't have multiple threads on the same cpu calling this
217 	 * at the same time.
218 	 */
219 	ib_dev_plug = &ib_dev->ibd_plug[raw_smp_processor_id()];
220 	if (test_and_set_bit(IBD_PLUGF_PLUGGED, &ib_dev_plug->flags))
221 		return NULL;
222 
223 	blk_start_plug(&ib_dev_plug->blk_plug);
224 	return &ib_dev_plug->se_plug;
225 }
226 
227 static void iblock_unplug_device(struct se_dev_plug *se_plug)
228 {
229 	struct iblock_dev_plug *ib_dev_plug = container_of(se_plug,
230 					struct iblock_dev_plug, se_plug);
231 
232 	blk_finish_plug(&ib_dev_plug->blk_plug);
233 	clear_bit(IBD_PLUGF_PLUGGED, &ib_dev_plug->flags);
234 }
235 
236 static sector_t iblock_get_blocks(struct se_device *dev)
237 {
238 	struct iblock_dev *ib_dev = IBLOCK_DEV(dev);
239 	u32 block_size = bdev_logical_block_size(ib_dev->ibd_bd);
240 	unsigned long long blocks_long =
241 		div_u64(bdev_nr_bytes(ib_dev->ibd_bd), block_size) - 1;
242 
243 	if (block_size == dev->dev_attrib.block_size)
244 		return blocks_long;
245 
246 	switch (block_size) {
247 	case 4096:
248 		switch (dev->dev_attrib.block_size) {
249 		case 2048:
250 			blocks_long <<= 1;
251 			break;
252 		case 1024:
253 			blocks_long <<= 2;
254 			break;
255 		case 512:
256 			blocks_long <<= 3;
257 			break;
258 		default:
259 			break;
260 		}
261 		break;
262 	case 2048:
263 		switch (dev->dev_attrib.block_size) {
264 		case 4096:
265 			blocks_long >>= 1;
266 			break;
267 		case 1024:
268 			blocks_long <<= 1;
269 			break;
270 		case 512:
271 			blocks_long <<= 2;
272 			break;
273 		default:
274 			break;
275 		}
276 		break;
277 	case 1024:
278 		switch (dev->dev_attrib.block_size) {
279 		case 4096:
280 			blocks_long >>= 2;
281 			break;
282 		case 2048:
283 			blocks_long >>= 1;
284 			break;
285 		case 512:
286 			blocks_long <<= 1;
287 			break;
288 		default:
289 			break;
290 		}
291 		break;
292 	case 512:
293 		switch (dev->dev_attrib.block_size) {
294 		case 4096:
295 			blocks_long >>= 3;
296 			break;
297 		case 2048:
298 			blocks_long >>= 2;
299 			break;
300 		case 1024:
301 			blocks_long >>= 1;
302 			break;
303 		default:
304 			break;
305 		}
306 		break;
307 	default:
308 		break;
309 	}
310 
311 	return blocks_long;
312 }
313 
314 static void iblock_complete_cmd(struct se_cmd *cmd, blk_status_t blk_status)
315 {
316 	struct iblock_req *ibr = cmd->priv;
317 	u8 status;
318 
319 	if (!refcount_dec_and_test(&ibr->pending))
320 		return;
321 
322 	if (blk_status == BLK_STS_RESV_CONFLICT)
323 		status = SAM_STAT_RESERVATION_CONFLICT;
324 	else if (atomic_read(&ibr->ib_bio_err_cnt))
325 		status = SAM_STAT_CHECK_CONDITION;
326 	else
327 		status = SAM_STAT_GOOD;
328 
329 	target_complete_cmd(cmd, status);
330 	kfree(ibr);
331 }
332 
333 static void iblock_bio_done(struct bio *bio)
334 {
335 	struct se_cmd *cmd = bio->bi_private;
336 	struct iblock_req *ibr = cmd->priv;
337 	blk_status_t blk_status = bio->bi_status;
338 
339 	if (bio->bi_status) {
340 		pr_err("bio error: %p,  err: %d\n", bio, bio->bi_status);
341 		/*
342 		 * Bump the ib_bio_err_cnt and release bio.
343 		 */
344 		atomic_inc(&ibr->ib_bio_err_cnt);
345 		smp_mb__after_atomic();
346 	}
347 
348 	bio_put(bio);
349 
350 	iblock_complete_cmd(cmd, blk_status);
351 }
352 
353 static struct bio *iblock_get_bio(struct se_cmd *cmd, sector_t lba, u32 sg_num,
354 				  blk_opf_t opf)
355 {
356 	struct iblock_dev *ib_dev = IBLOCK_DEV(cmd->se_dev);
357 	struct bio *bio;
358 
359 	/*
360 	 * Only allocate as many vector entries as the bio code allows us to,
361 	 * we'll loop later on until we have handled the whole request.
362 	 */
363 	bio = bio_alloc_bioset(ib_dev->ibd_bd, bio_max_segs(sg_num), opf,
364 			       GFP_NOIO, &ib_dev->ibd_bio_set);
365 	if (!bio) {
366 		pr_err("Unable to allocate memory for bio\n");
367 		return NULL;
368 	}
369 
370 	bio->bi_private = cmd;
371 	bio->bi_end_io = &iblock_bio_done;
372 	bio->bi_iter.bi_sector = lba;
373 
374 	return bio;
375 }
376 
377 static void iblock_submit_bios(struct bio_list *list)
378 {
379 	struct blk_plug plug;
380 	struct bio *bio;
381 	/*
382 	 * The block layer handles nested plugs, so just plug/unplug to handle
383 	 * fabric drivers that didn't support batching and multi bio cmds.
384 	 */
385 	blk_start_plug(&plug);
386 	while ((bio = bio_list_pop(list)))
387 		submit_bio(bio);
388 	blk_finish_plug(&plug);
389 }
390 
391 static void iblock_end_io_flush(struct bio *bio)
392 {
393 	struct se_cmd *cmd = bio->bi_private;
394 
395 	if (bio->bi_status)
396 		pr_err("IBLOCK: cache flush failed: %d\n", bio->bi_status);
397 
398 	if (cmd) {
399 		if (bio->bi_status)
400 			target_complete_cmd(cmd, SAM_STAT_CHECK_CONDITION);
401 		else
402 			target_complete_cmd(cmd, SAM_STAT_GOOD);
403 	}
404 
405 	bio_put(bio);
406 }
407 
408 /*
409  * Implement SYCHRONIZE CACHE.  Note that we can't handle lba ranges and must
410  * always flush the whole cache.
411  */
412 static sense_reason_t
413 iblock_execute_sync_cache(struct se_cmd *cmd)
414 {
415 	struct iblock_dev *ib_dev = IBLOCK_DEV(cmd->se_dev);
416 	int immed = (cmd->t_task_cdb[1] & 0x2);
417 	struct bio *bio;
418 
419 	/*
420 	 * If the Immediate bit is set, queue up the GOOD response
421 	 * for this SYNCHRONIZE_CACHE op.
422 	 */
423 	if (immed)
424 		target_complete_cmd(cmd, SAM_STAT_GOOD);
425 
426 	bio = bio_alloc(ib_dev->ibd_bd, 0, REQ_OP_WRITE | REQ_PREFLUSH,
427 			GFP_KERNEL);
428 	bio->bi_end_io = iblock_end_io_flush;
429 	if (!immed)
430 		bio->bi_private = cmd;
431 	submit_bio(bio);
432 	return 0;
433 }
434 
435 static sense_reason_t
436 iblock_execute_unmap(struct se_cmd *cmd, sector_t lba, sector_t nolb)
437 {
438 	struct block_device *bdev = IBLOCK_DEV(cmd->se_dev)->ibd_bd;
439 	struct se_device *dev = cmd->se_dev;
440 	int ret;
441 
442 	ret = blkdev_issue_discard(bdev,
443 				   target_to_linux_sector(dev, lba),
444 				   target_to_linux_sector(dev,  nolb),
445 				   GFP_KERNEL);
446 	if (ret < 0) {
447 		pr_err("blkdev_issue_discard() failed: %d\n", ret);
448 		return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
449 	}
450 
451 	return 0;
452 }
453 
454 static sense_reason_t
455 iblock_execute_zero_out(struct block_device *bdev, struct se_cmd *cmd)
456 {
457 	struct se_device *dev = cmd->se_dev;
458 	struct scatterlist *sg = &cmd->t_data_sg[0];
459 	unsigned char *buf, *not_zero;
460 	int ret;
461 
462 	buf = kmap(sg_page(sg)) + sg->offset;
463 	if (!buf)
464 		return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
465 	/*
466 	 * Fall back to block_execute_write_same() slow-path if
467 	 * incoming WRITE_SAME payload does not contain zeros.
468 	 */
469 	not_zero = memchr_inv(buf, 0x00, cmd->data_length);
470 	kunmap(sg_page(sg));
471 
472 	if (not_zero)
473 		return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
474 
475 	ret = blkdev_issue_zeroout(bdev,
476 				target_to_linux_sector(dev, cmd->t_task_lba),
477 				target_to_linux_sector(dev,
478 					sbc_get_write_same_sectors(cmd)),
479 				GFP_KERNEL, BLKDEV_ZERO_NOUNMAP);
480 	if (ret)
481 		return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
482 
483 	target_complete_cmd(cmd, SAM_STAT_GOOD);
484 	return 0;
485 }
486 
487 static sense_reason_t
488 iblock_execute_write_same(struct se_cmd *cmd)
489 {
490 	struct block_device *bdev = IBLOCK_DEV(cmd->se_dev)->ibd_bd;
491 	struct iblock_req *ibr;
492 	struct scatterlist *sg;
493 	struct bio *bio;
494 	struct bio_list list;
495 	struct se_device *dev = cmd->se_dev;
496 	sector_t block_lba = target_to_linux_sector(dev, cmd->t_task_lba);
497 	sector_t sectors = target_to_linux_sector(dev,
498 					sbc_get_write_same_sectors(cmd));
499 
500 	if (cmd->prot_op) {
501 		pr_err("WRITE_SAME: Protection information with IBLOCK"
502 		       " backends not supported\n");
503 		return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
504 	}
505 
506 	if (!cmd->t_data_nents)
507 		return TCM_INVALID_CDB_FIELD;
508 
509 	sg = &cmd->t_data_sg[0];
510 
511 	if (cmd->t_data_nents > 1 ||
512 	    sg->length != cmd->se_dev->dev_attrib.block_size) {
513 		pr_err("WRITE_SAME: Illegal SGL t_data_nents: %u length: %u"
514 			" block_size: %u\n", cmd->t_data_nents, sg->length,
515 			cmd->se_dev->dev_attrib.block_size);
516 		return TCM_INVALID_CDB_FIELD;
517 	}
518 
519 	if (bdev_write_zeroes_sectors(bdev)) {
520 		if (!iblock_execute_zero_out(bdev, cmd))
521 			return 0;
522 	}
523 
524 	ibr = kzalloc(sizeof(struct iblock_req), GFP_KERNEL);
525 	if (!ibr)
526 		goto fail;
527 	cmd->priv = ibr;
528 
529 	bio = iblock_get_bio(cmd, block_lba, 1, REQ_OP_WRITE);
530 	if (!bio)
531 		goto fail_free_ibr;
532 
533 	bio_list_init(&list);
534 	bio_list_add(&list, bio);
535 
536 	refcount_set(&ibr->pending, 1);
537 
538 	while (sectors) {
539 		while (bio_add_page(bio, sg_page(sg), sg->length, sg->offset)
540 				!= sg->length) {
541 
542 			bio = iblock_get_bio(cmd, block_lba, 1, REQ_OP_WRITE);
543 			if (!bio)
544 				goto fail_put_bios;
545 
546 			refcount_inc(&ibr->pending);
547 			bio_list_add(&list, bio);
548 		}
549 
550 		/* Always in 512 byte units for Linux/Block */
551 		block_lba += sg->length >> SECTOR_SHIFT;
552 		sectors -= sg->length >> SECTOR_SHIFT;
553 	}
554 
555 	iblock_submit_bios(&list);
556 	return 0;
557 
558 fail_put_bios:
559 	while ((bio = bio_list_pop(&list)))
560 		bio_put(bio);
561 fail_free_ibr:
562 	kfree(ibr);
563 fail:
564 	return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
565 }
566 
567 enum {
568 	Opt_udev_path, Opt_readonly, Opt_force, Opt_exclusive, Opt_err,
569 };
570 
571 static match_table_t tokens = {
572 	{Opt_udev_path, "udev_path=%s"},
573 	{Opt_readonly, "readonly=%d"},
574 	{Opt_force, "force=%d"},
575 	{Opt_exclusive, "exclusive=%d"},
576 	{Opt_err, NULL}
577 };
578 
579 static ssize_t iblock_set_configfs_dev_params(struct se_device *dev,
580 		const char *page, ssize_t count)
581 {
582 	struct iblock_dev *ib_dev = IBLOCK_DEV(dev);
583 	char *orig, *ptr, *arg_p, *opts;
584 	substring_t args[MAX_OPT_ARGS];
585 	int ret = 0, token, tmp_exclusive;
586 	unsigned long tmp_readonly;
587 
588 	opts = kstrdup(page, GFP_KERNEL);
589 	if (!opts)
590 		return -ENOMEM;
591 
592 	orig = opts;
593 
594 	while ((ptr = strsep(&opts, ",\n")) != NULL) {
595 		if (!*ptr)
596 			continue;
597 
598 		token = match_token(ptr, tokens, args);
599 		switch (token) {
600 		case Opt_udev_path:
601 			if (ib_dev->ibd_bd) {
602 				pr_err("Unable to set udev_path= while"
603 					" ib_dev->ibd_bd exists\n");
604 				ret = -EEXIST;
605 				goto out;
606 			}
607 			if (match_strlcpy(ib_dev->ibd_udev_path, &args[0],
608 				SE_UDEV_PATH_LEN) == 0) {
609 				ret = -EINVAL;
610 				break;
611 			}
612 			pr_debug("IBLOCK: Referencing UDEV path: %s\n",
613 					ib_dev->ibd_udev_path);
614 			ib_dev->ibd_flags |= IBDF_HAS_UDEV_PATH;
615 			break;
616 		case Opt_readonly:
617 			arg_p = match_strdup(&args[0]);
618 			if (!arg_p) {
619 				ret = -ENOMEM;
620 				break;
621 			}
622 			ret = kstrtoul(arg_p, 0, &tmp_readonly);
623 			kfree(arg_p);
624 			if (ret < 0) {
625 				pr_err("kstrtoul() failed for"
626 						" readonly=\n");
627 				goto out;
628 			}
629 			ib_dev->ibd_readonly = tmp_readonly;
630 			pr_debug("IBLOCK: readonly: %d\n", ib_dev->ibd_readonly);
631 			break;
632 		case Opt_exclusive:
633 			arg_p = match_strdup(&args[0]);
634 			if (!arg_p) {
635 				ret = -ENOMEM;
636 				break;
637 			}
638 			ret = kstrtoint(arg_p, 0, &tmp_exclusive);
639 			kfree(arg_p);
640 			if (ret < 0) {
641 				pr_err("kstrtoul() failed for exclusive=\n");
642 				goto out;
643 			}
644 			ib_dev->ibd_exclusive = tmp_exclusive;
645 			pr_debug("IBLOCK: exclusive: %d\n",
646 				 ib_dev->ibd_exclusive);
647 			break;
648 		case Opt_force:
649 			break;
650 		default:
651 			break;
652 		}
653 	}
654 
655 out:
656 	kfree(orig);
657 	return (!ret) ? count : ret;
658 }
659 
660 static ssize_t iblock_show_configfs_dev_params(struct se_device *dev, char *b)
661 {
662 	struct iblock_dev *ib_dev = IBLOCK_DEV(dev);
663 	struct block_device *bd = ib_dev->ibd_bd;
664 	ssize_t bl = 0;
665 
666 	if (bd)
667 		bl += sprintf(b + bl, "iBlock device: %pg", bd);
668 	if (ib_dev->ibd_flags & IBDF_HAS_UDEV_PATH)
669 		bl += sprintf(b + bl, "  UDEV PATH: %s",
670 				ib_dev->ibd_udev_path);
671 	bl += sprintf(b + bl, "  readonly: %d\n", ib_dev->ibd_readonly);
672 	bl += sprintf(b + bl, "  exclusive: %d\n", ib_dev->ibd_exclusive);
673 
674 	bl += sprintf(b + bl, "        ");
675 	if (bd) {
676 		bl += sprintf(b + bl, "Major: %d Minor: %d  %s\n",
677 			MAJOR(bd->bd_dev), MINOR(bd->bd_dev),
678 			"CLAIMED: IBLOCK");
679 	} else {
680 		bl += sprintf(b + bl, "Major: 0 Minor: 0\n");
681 	}
682 
683 	return bl;
684 }
685 
686 static int
687 iblock_alloc_bip(struct se_cmd *cmd, struct bio *bio,
688 		 struct sg_mapping_iter *miter)
689 {
690 	struct se_device *dev = cmd->se_dev;
691 	struct blk_integrity *bi;
692 	struct bio_integrity_payload *bip;
693 	struct iblock_dev *ib_dev = IBLOCK_DEV(dev);
694 	int rc;
695 	size_t resid, len;
696 
697 	bi = bdev_get_integrity(ib_dev->ibd_bd);
698 	if (!bi) {
699 		pr_err("Unable to locate bio_integrity\n");
700 		return -ENODEV;
701 	}
702 
703 	bip = bio_integrity_alloc(bio, GFP_NOIO, bio_max_segs(cmd->t_prot_nents));
704 	if (IS_ERR(bip)) {
705 		pr_err("Unable to allocate bio_integrity_payload\n");
706 		return PTR_ERR(bip);
707 	}
708 
709 	/* virtual start sector must be in integrity interval units */
710 	bip_set_seed(bip, bio->bi_iter.bi_sector >>
711 				  (bi->interval_exp - SECTOR_SHIFT));
712 
713 	pr_debug("IBLOCK BIP Size: %u Sector: %llu\n", bip->bip_iter.bi_size,
714 		 (unsigned long long)bip->bip_iter.bi_sector);
715 
716 	resid = bio_integrity_bytes(bi, bio_sectors(bio));
717 	while (resid > 0 && sg_miter_next(miter)) {
718 
719 		len = min_t(size_t, miter->length, resid);
720 		rc = bio_integrity_add_page(bio, miter->page, len,
721 					    offset_in_page(miter->addr));
722 		if (rc != len) {
723 			pr_err("bio_integrity_add_page() failed; %d\n", rc);
724 			sg_miter_stop(miter);
725 			return -ENOMEM;
726 		}
727 
728 		pr_debug("Added bio integrity page: %p length: %zu offset: %lu\n",
729 			  miter->page, len, offset_in_page(miter->addr));
730 
731 		resid -= len;
732 		if (len < miter->length)
733 			miter->consumed -= miter->length - len;
734 	}
735 	sg_miter_stop(miter);
736 
737 	return 0;
738 }
739 
740 static sense_reason_t
741 iblock_execute_rw(struct se_cmd *cmd, struct scatterlist *sgl, u32 sgl_nents,
742 		  enum dma_data_direction data_direction)
743 {
744 	struct se_device *dev = cmd->se_dev;
745 	sector_t block_lba = target_to_linux_sector(dev, cmd->t_task_lba);
746 	struct iblock_req *ibr;
747 	struct bio *bio;
748 	struct bio_list list;
749 	struct scatterlist *sg;
750 	u32 sg_num = sgl_nents;
751 	blk_opf_t opf;
752 	unsigned bio_cnt;
753 	int i, rc;
754 	struct sg_mapping_iter prot_miter;
755 	unsigned int miter_dir;
756 
757 	if (data_direction == DMA_TO_DEVICE) {
758 		struct iblock_dev *ib_dev = IBLOCK_DEV(dev);
759 
760 		/*
761 		 * Set bits to indicate WRITE_ODIRECT so we are not throttled
762 		 * by WBT.
763 		 */
764 		opf = REQ_OP_WRITE | REQ_SYNC | REQ_IDLE;
765 		/*
766 		 * Force writethrough using REQ_FUA if a volatile write cache
767 		 * is not enabled, or if initiator set the Force Unit Access bit.
768 		 */
769 		miter_dir = SG_MITER_TO_SG;
770 		if (bdev_fua(ib_dev->ibd_bd)) {
771 			if (cmd->se_cmd_flags & SCF_FUA)
772 				opf |= REQ_FUA;
773 			else if (!bdev_write_cache(ib_dev->ibd_bd))
774 				opf |= REQ_FUA;
775 		}
776 	} else {
777 		opf = REQ_OP_READ;
778 		miter_dir = SG_MITER_FROM_SG;
779 	}
780 
781 	ibr = kzalloc(sizeof(struct iblock_req), GFP_KERNEL);
782 	if (!ibr)
783 		goto fail;
784 	cmd->priv = ibr;
785 
786 	if (!sgl_nents) {
787 		refcount_set(&ibr->pending, 1);
788 		iblock_complete_cmd(cmd, BLK_STS_OK);
789 		return 0;
790 	}
791 
792 	bio = iblock_get_bio(cmd, block_lba, sgl_nents, opf);
793 	if (!bio)
794 		goto fail_free_ibr;
795 
796 	bio_list_init(&list);
797 	bio_list_add(&list, bio);
798 
799 	refcount_set(&ibr->pending, 2);
800 	bio_cnt = 1;
801 
802 	if (cmd->prot_type && dev->dev_attrib.pi_prot_type)
803 		sg_miter_start(&prot_miter, cmd->t_prot_sg, cmd->t_prot_nents,
804 			       miter_dir);
805 
806 	for_each_sg(sgl, sg, sgl_nents, i) {
807 		/*
808 		 * XXX: if the length the device accepts is shorter than the
809 		 *	length of the S/G list entry this will cause and
810 		 *	endless loop.  Better hope no driver uses huge pages.
811 		 */
812 		while (bio_add_page(bio, sg_page(sg), sg->length, sg->offset)
813 				!= sg->length) {
814 			if (cmd->prot_type && dev->dev_attrib.pi_prot_type) {
815 				rc = iblock_alloc_bip(cmd, bio, &prot_miter);
816 				if (rc)
817 					goto fail_put_bios;
818 			}
819 
820 			if (bio_cnt >= IBLOCK_MAX_BIO_PER_TASK) {
821 				iblock_submit_bios(&list);
822 				bio_cnt = 0;
823 			}
824 
825 			bio = iblock_get_bio(cmd, block_lba, sg_num, opf);
826 			if (!bio)
827 				goto fail_put_bios;
828 
829 			refcount_inc(&ibr->pending);
830 			bio_list_add(&list, bio);
831 			bio_cnt++;
832 		}
833 
834 		/* Always in 512 byte units for Linux/Block */
835 		block_lba += sg->length >> SECTOR_SHIFT;
836 		sg_num--;
837 	}
838 
839 	if (cmd->prot_type && dev->dev_attrib.pi_prot_type) {
840 		rc = iblock_alloc_bip(cmd, bio, &prot_miter);
841 		if (rc)
842 			goto fail_put_bios;
843 	}
844 
845 	iblock_submit_bios(&list);
846 	iblock_complete_cmd(cmd, BLK_STS_OK);
847 	return 0;
848 
849 fail_put_bios:
850 	while ((bio = bio_list_pop(&list)))
851 		bio_put(bio);
852 fail_free_ibr:
853 	kfree(ibr);
854 fail:
855 	return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
856 }
857 
858 static sense_reason_t iblock_execute_pr_out(struct se_cmd *cmd, u8 sa, u64 key,
859 					    u64 sa_key, u8 type, bool aptpl)
860 {
861 	struct se_device *dev = cmd->se_dev;
862 	struct iblock_dev *ib_dev = IBLOCK_DEV(dev);
863 	struct block_device *bdev = ib_dev->ibd_bd;
864 	const struct pr_ops *ops = bdev->bd_disk->fops->pr_ops;
865 	int ret;
866 
867 	if (!ops) {
868 		pr_err("Block device does not support pr_ops but iblock device has been configured for PR passthrough.\n");
869 		return TCM_UNSUPPORTED_SCSI_OPCODE;
870 	}
871 
872 	switch (sa) {
873 	case PRO_REGISTER:
874 	case PRO_REGISTER_AND_IGNORE_EXISTING_KEY:
875 		if (!ops->pr_register) {
876 			pr_err("block device does not support pr_register.\n");
877 			return TCM_UNSUPPORTED_SCSI_OPCODE;
878 		}
879 
880 		/* The block layer pr ops always enables aptpl */
881 		if (!aptpl)
882 			pr_info("APTPL not set by initiator, but will be used.\n");
883 
884 		ret = ops->pr_register(bdev, key, sa_key,
885 				sa == PRO_REGISTER ? 0 : PR_FL_IGNORE_KEY);
886 		break;
887 	case PRO_RESERVE:
888 		if (!ops->pr_reserve) {
889 			pr_err("block_device does not support pr_reserve.\n");
890 			return TCM_UNSUPPORTED_SCSI_OPCODE;
891 		}
892 
893 		ret = ops->pr_reserve(bdev, key, scsi_pr_type_to_block(type), 0);
894 		break;
895 	case PRO_CLEAR:
896 		if (!ops->pr_clear) {
897 			pr_err("block_device does not support pr_clear.\n");
898 			return TCM_UNSUPPORTED_SCSI_OPCODE;
899 		}
900 
901 		ret = ops->pr_clear(bdev, key);
902 		break;
903 	case PRO_PREEMPT:
904 	case PRO_PREEMPT_AND_ABORT:
905 		if (!ops->pr_clear) {
906 			pr_err("block_device does not support pr_preempt.\n");
907 			return TCM_UNSUPPORTED_SCSI_OPCODE;
908 		}
909 
910 		ret = ops->pr_preempt(bdev, key, sa_key,
911 				      scsi_pr_type_to_block(type),
912 				      sa == PRO_PREEMPT_AND_ABORT);
913 		break;
914 	case PRO_RELEASE:
915 		if (!ops->pr_clear) {
916 			pr_err("block_device does not support pr_pclear.\n");
917 			return TCM_UNSUPPORTED_SCSI_OPCODE;
918 		}
919 
920 		ret = ops->pr_release(bdev, key, scsi_pr_type_to_block(type));
921 		break;
922 	default:
923 		pr_err("Unknown PERSISTENT_RESERVE_OUT SA: 0x%02x\n", sa);
924 		return TCM_UNSUPPORTED_SCSI_OPCODE;
925 	}
926 
927 	if (!ret)
928 		return TCM_NO_SENSE;
929 	else if (ret == PR_STS_RESERVATION_CONFLICT)
930 		return TCM_RESERVATION_CONFLICT;
931 	else
932 		return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
933 }
934 
935 static void iblock_pr_report_caps(unsigned char *param_data)
936 {
937 	u16 len = 8;
938 
939 	put_unaligned_be16(len, &param_data[0]);
940 	/*
941 	 * When using the pr_ops passthrough method we only support exporting
942 	 * the device through one target port because from the backend module
943 	 * level we can't see the target port config. As a result we only
944 	 * support registration directly from the I_T nexus the cmd is sent
945 	 * through and do not set ATP_C here.
946 	 *
947 	 * The block layer pr_ops do not support passing in initiators so
948 	 * we don't set SIP_C here.
949 	 */
950 	/* PTPL_C: Persistence across Target Power Loss bit */
951 	param_data[2] |= 0x01;
952 	/*
953 	 * We are filling in the PERSISTENT RESERVATION TYPE MASK below, so
954 	 * set the TMV: Task Mask Valid bit.
955 	 */
956 	param_data[3] |= 0x80;
957 	/*
958 	 * Change ALLOW COMMANDs to 0x20 or 0x40 later from Table 166
959 	 */
960 	param_data[3] |= 0x10; /* ALLOW COMMANDs field 001b */
961 	/*
962 	 * PTPL_A: Persistence across Target Power Loss Active bit. The block
963 	 * layer pr ops always enables this so report it active.
964 	 */
965 	param_data[3] |= 0x01;
966 	/*
967 	 * Setup the PERSISTENT RESERVATION TYPE MASK from Table 212 spc4r37.
968 	 */
969 	param_data[4] |= 0x80; /* PR_TYPE_EXCLUSIVE_ACCESS_ALLREG */
970 	param_data[4] |= 0x40; /* PR_TYPE_EXCLUSIVE_ACCESS_REGONLY */
971 	param_data[4] |= 0x20; /* PR_TYPE_WRITE_EXCLUSIVE_REGONLY */
972 	param_data[4] |= 0x08; /* PR_TYPE_EXCLUSIVE_ACCESS */
973 	param_data[4] |= 0x02; /* PR_TYPE_WRITE_EXCLUSIVE */
974 	param_data[5] |= 0x01; /* PR_TYPE_EXCLUSIVE_ACCESS_ALLREG */
975 }
976 
977 static sense_reason_t iblock_pr_read_keys(struct se_cmd *cmd,
978 					  unsigned char *param_data)
979 {
980 	struct se_device *dev = cmd->se_dev;
981 	struct iblock_dev *ib_dev = IBLOCK_DEV(dev);
982 	struct block_device *bdev = ib_dev->ibd_bd;
983 	const struct pr_ops *ops = bdev->bd_disk->fops->pr_ops;
984 	int i, len, paths, data_offset;
985 	struct pr_keys *keys;
986 	sense_reason_t ret;
987 
988 	if (!ops) {
989 		pr_err("Block device does not support pr_ops but iblock device has been configured for PR passthrough.\n");
990 		return TCM_UNSUPPORTED_SCSI_OPCODE;
991 	}
992 
993 	if (!ops->pr_read_keys) {
994 		pr_err("Block device does not support read_keys.\n");
995 		return TCM_UNSUPPORTED_SCSI_OPCODE;
996 	}
997 
998 	/*
999 	 * We don't know what's under us, but dm-multipath will register every
1000 	 * path with the same key, so start off with enough space for 16 paths.
1001 	 * which is not a lot of memory and should normally be enough.
1002 	 */
1003 	paths = 16;
1004 retry:
1005 	len = 8 * paths;
1006 	keys = kzalloc(sizeof(*keys) + len, GFP_KERNEL);
1007 	if (!keys)
1008 		return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
1009 
1010 	keys->num_keys = paths;
1011 	if (!ops->pr_read_keys(bdev, keys)) {
1012 		if (keys->num_keys > paths) {
1013 			kfree(keys);
1014 			paths *= 2;
1015 			goto retry;
1016 		}
1017 	} else {
1018 		ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
1019 		goto free_keys;
1020 	}
1021 
1022 	ret = TCM_NO_SENSE;
1023 
1024 	put_unaligned_be32(keys->generation, &param_data[0]);
1025 	if (!keys->num_keys) {
1026 		put_unaligned_be32(0, &param_data[4]);
1027 		goto free_keys;
1028 	}
1029 
1030 	put_unaligned_be32(8 * keys->num_keys, &param_data[4]);
1031 
1032 	data_offset = 8;
1033 	for (i = 0; i < keys->num_keys; i++) {
1034 		if (data_offset + 8 > cmd->data_length)
1035 			break;
1036 
1037 		put_unaligned_be64(keys->keys[i], &param_data[data_offset]);
1038 		data_offset += 8;
1039 	}
1040 
1041 free_keys:
1042 	kfree(keys);
1043 	return ret;
1044 }
1045 
1046 static sense_reason_t iblock_pr_read_reservation(struct se_cmd *cmd,
1047 						 unsigned char *param_data)
1048 {
1049 	struct se_device *dev = cmd->se_dev;
1050 	struct iblock_dev *ib_dev = IBLOCK_DEV(dev);
1051 	struct block_device *bdev = ib_dev->ibd_bd;
1052 	const struct pr_ops *ops = bdev->bd_disk->fops->pr_ops;
1053 	struct pr_held_reservation rsv = { };
1054 
1055 	if (!ops) {
1056 		pr_err("Block device does not support pr_ops but iblock device has been configured for PR passthrough.\n");
1057 		return TCM_UNSUPPORTED_SCSI_OPCODE;
1058 	}
1059 
1060 	if (!ops->pr_read_reservation) {
1061 		pr_err("Block device does not support read_keys.\n");
1062 		return TCM_UNSUPPORTED_SCSI_OPCODE;
1063 	}
1064 
1065 	if (ops->pr_read_reservation(bdev, &rsv))
1066 		return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
1067 
1068 	put_unaligned_be32(rsv.generation, &param_data[0]);
1069 	if (!block_pr_type_to_scsi(rsv.type)) {
1070 		put_unaligned_be32(0, &param_data[4]);
1071 		return TCM_NO_SENSE;
1072 	}
1073 
1074 	put_unaligned_be32(16, &param_data[4]);
1075 
1076 	if (cmd->data_length < 16)
1077 		return TCM_NO_SENSE;
1078 	put_unaligned_be64(rsv.key, &param_data[8]);
1079 
1080 	if (cmd->data_length < 22)
1081 		return TCM_NO_SENSE;
1082 	param_data[21] = block_pr_type_to_scsi(rsv.type);
1083 
1084 	return TCM_NO_SENSE;
1085 }
1086 
1087 static sense_reason_t iblock_execute_pr_in(struct se_cmd *cmd, u8 sa,
1088 					   unsigned char *param_data)
1089 {
1090 	sense_reason_t ret = TCM_NO_SENSE;
1091 
1092 	switch (sa) {
1093 	case PRI_REPORT_CAPABILITIES:
1094 		iblock_pr_report_caps(param_data);
1095 		break;
1096 	case PRI_READ_KEYS:
1097 		ret = iblock_pr_read_keys(cmd, param_data);
1098 		break;
1099 	case PRI_READ_RESERVATION:
1100 		ret = iblock_pr_read_reservation(cmd, param_data);
1101 		break;
1102 	default:
1103 		pr_err("Unknown PERSISTENT_RESERVE_IN SA: 0x%02x\n", sa);
1104 		return TCM_UNSUPPORTED_SCSI_OPCODE;
1105 	}
1106 
1107 	return ret;
1108 }
1109 
1110 static sector_t iblock_get_alignment_offset_lbas(struct se_device *dev)
1111 {
1112 	struct iblock_dev *ib_dev = IBLOCK_DEV(dev);
1113 	struct block_device *bd = ib_dev->ibd_bd;
1114 	int ret;
1115 
1116 	ret = bdev_alignment_offset(bd);
1117 	if (ret == -1)
1118 		return 0;
1119 
1120 	/* convert offset-bytes to offset-lbas */
1121 	return ret / bdev_logical_block_size(bd);
1122 }
1123 
1124 static unsigned int iblock_get_lbppbe(struct se_device *dev)
1125 {
1126 	struct iblock_dev *ib_dev = IBLOCK_DEV(dev);
1127 	struct block_device *bd = ib_dev->ibd_bd;
1128 	unsigned int logs_per_phys =
1129 		bdev_physical_block_size(bd) / bdev_logical_block_size(bd);
1130 
1131 	return ilog2(logs_per_phys);
1132 }
1133 
1134 static unsigned int iblock_get_io_min(struct se_device *dev)
1135 {
1136 	struct iblock_dev *ib_dev = IBLOCK_DEV(dev);
1137 	struct block_device *bd = ib_dev->ibd_bd;
1138 
1139 	return bdev_io_min(bd);
1140 }
1141 
1142 static unsigned int iblock_get_io_opt(struct se_device *dev)
1143 {
1144 	struct iblock_dev *ib_dev = IBLOCK_DEV(dev);
1145 	struct block_device *bd = ib_dev->ibd_bd;
1146 
1147 	return bdev_io_opt(bd);
1148 }
1149 
1150 static struct exec_cmd_ops iblock_exec_cmd_ops = {
1151 	.execute_rw		= iblock_execute_rw,
1152 	.execute_sync_cache	= iblock_execute_sync_cache,
1153 	.execute_write_same	= iblock_execute_write_same,
1154 	.execute_unmap		= iblock_execute_unmap,
1155 	.execute_pr_out		= iblock_execute_pr_out,
1156 	.execute_pr_in		= iblock_execute_pr_in,
1157 };
1158 
1159 static sense_reason_t
1160 iblock_parse_cdb(struct se_cmd *cmd)
1161 {
1162 	return sbc_parse_cdb(cmd, &iblock_exec_cmd_ops);
1163 }
1164 
1165 static bool iblock_get_write_cache(struct se_device *dev)
1166 {
1167 	return bdev_write_cache(IBLOCK_DEV(dev)->ibd_bd);
1168 }
1169 
1170 static const struct target_backend_ops iblock_ops = {
1171 	.name			= "iblock",
1172 	.inquiry_prod		= "IBLOCK",
1173 	.transport_flags_changeable = TRANSPORT_FLAG_PASSTHROUGH_PGR,
1174 	.inquiry_rev		= IBLOCK_VERSION,
1175 	.owner			= THIS_MODULE,
1176 	.attach_hba		= iblock_attach_hba,
1177 	.detach_hba		= iblock_detach_hba,
1178 	.alloc_device		= iblock_alloc_device,
1179 	.configure_device	= iblock_configure_device,
1180 	.destroy_device		= iblock_destroy_device,
1181 	.free_device		= iblock_free_device,
1182 	.configure_unmap	= iblock_configure_unmap,
1183 	.plug_device		= iblock_plug_device,
1184 	.unplug_device		= iblock_unplug_device,
1185 	.parse_cdb		= iblock_parse_cdb,
1186 	.set_configfs_dev_params = iblock_set_configfs_dev_params,
1187 	.show_configfs_dev_params = iblock_show_configfs_dev_params,
1188 	.get_device_type	= sbc_get_device_type,
1189 	.get_blocks		= iblock_get_blocks,
1190 	.get_alignment_offset_lbas = iblock_get_alignment_offset_lbas,
1191 	.get_lbppbe		= iblock_get_lbppbe,
1192 	.get_io_min		= iblock_get_io_min,
1193 	.get_io_opt		= iblock_get_io_opt,
1194 	.get_write_cache	= iblock_get_write_cache,
1195 	.tb_dev_attrib_attrs	= sbc_attrib_attrs,
1196 };
1197 
1198 static int __init iblock_module_init(void)
1199 {
1200 	return transport_backend_register(&iblock_ops);
1201 }
1202 
1203 static void __exit iblock_module_exit(void)
1204 {
1205 	target_backend_unregister(&iblock_ops);
1206 }
1207 
1208 MODULE_DESCRIPTION("TCM IBLOCK subsystem plugin");
1209 MODULE_AUTHOR("nab@Linux-iSCSI.org");
1210 MODULE_LICENSE("GPL");
1211 
1212 module_init(iblock_module_init);
1213 module_exit(iblock_module_exit);
1214