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