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