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 <scsi/scsi_proto.h> 27 #include <asm/unaligned.h> 28 29 #include <target/target_core_base.h> 30 #include <target/target_core_backend.h> 31 32 #include "target_core_iblock.h" 33 34 #define IBLOCK_MAX_BIO_PER_TASK 32 /* max # of bios to submit at a time */ 35 #define IBLOCK_BIO_POOL_SIZE 128 36 37 static inline struct iblock_dev *IBLOCK_DEV(struct se_device *dev) 38 { 39 return container_of(dev, struct iblock_dev, dev); 40 } 41 42 43 static int iblock_attach_hba(struct se_hba *hba, u32 host_id) 44 { 45 pr_debug("CORE_HBA[%d] - TCM iBlock HBA Driver %s on" 46 " Generic Target Core Stack %s\n", hba->hba_id, 47 IBLOCK_VERSION, TARGET_CORE_VERSION); 48 return 0; 49 } 50 51 static void iblock_detach_hba(struct se_hba *hba) 52 { 53 } 54 55 static struct se_device *iblock_alloc_device(struct se_hba *hba, const char *name) 56 { 57 struct iblock_dev *ib_dev = NULL; 58 59 ib_dev = kzalloc(sizeof(struct iblock_dev), GFP_KERNEL); 60 if (!ib_dev) { 61 pr_err("Unable to allocate struct iblock_dev\n"); 62 return NULL; 63 } 64 65 ib_dev->ibd_plug = kcalloc(nr_cpu_ids, sizeof(*ib_dev->ibd_plug), 66 GFP_KERNEL); 67 if (!ib_dev->ibd_plug) 68 goto free_dev; 69 70 pr_debug( "IBLOCK: Allocated ib_dev for %s\n", name); 71 72 return &ib_dev->dev; 73 74 free_dev: 75 kfree(ib_dev); 76 return NULL; 77 } 78 79 static bool iblock_configure_unmap(struct se_device *dev) 80 { 81 struct iblock_dev *ib_dev = IBLOCK_DEV(dev); 82 83 return target_configure_unmap_from_queue(&dev->dev_attrib, 84 ib_dev->ibd_bd); 85 } 86 87 static int iblock_configure_device(struct se_device *dev) 88 { 89 struct iblock_dev *ib_dev = IBLOCK_DEV(dev); 90 struct request_queue *q; 91 struct block_device *bd = NULL; 92 struct blk_integrity *bi; 93 blk_mode_t mode = BLK_OPEN_READ; 94 unsigned int max_write_zeroes_sectors; 95 int ret; 96 97 if (!(ib_dev->ibd_flags & IBDF_HAS_UDEV_PATH)) { 98 pr_err("Missing udev_path= parameters for IBLOCK\n"); 99 return -EINVAL; 100 } 101 102 ret = bioset_init(&ib_dev->ibd_bio_set, IBLOCK_BIO_POOL_SIZE, 0, BIOSET_NEED_BVECS); 103 if (ret) { 104 pr_err("IBLOCK: Unable to create bioset\n"); 105 goto out; 106 } 107 108 pr_debug( "IBLOCK: Claiming struct block_device: %s\n", 109 ib_dev->ibd_udev_path); 110 111 if (!ib_dev->ibd_readonly) 112 mode |= BLK_OPEN_WRITE; 113 else 114 dev->dev_flags |= DF_READ_ONLY; 115 116 bd = blkdev_get_by_path(ib_dev->ibd_udev_path, mode, ib_dev, NULL); 117 if (IS_ERR(bd)) { 118 ret = PTR_ERR(bd); 119 goto out_free_bioset; 120 } 121 ib_dev->ibd_bd = bd; 122 123 q = bdev_get_queue(bd); 124 125 dev->dev_attrib.hw_block_size = bdev_logical_block_size(bd); 126 dev->dev_attrib.hw_max_sectors = mult_frac(queue_max_hw_sectors(q), 127 SECTOR_SIZE, 128 dev->dev_attrib.hw_block_size); 129 dev->dev_attrib.hw_queue_depth = q->nr_requests; 130 131 /* 132 * Enable write same emulation for IBLOCK and use 0xFFFF as 133 * the smaller WRITE_SAME(10) only has a two-byte block count. 134 */ 135 max_write_zeroes_sectors = bdev_write_zeroes_sectors(bd); 136 if (max_write_zeroes_sectors) 137 dev->dev_attrib.max_write_same_len = max_write_zeroes_sectors; 138 else 139 dev->dev_attrib.max_write_same_len = 0xFFFF; 140 141 if (bdev_nonrot(bd)) 142 dev->dev_attrib.is_nonrot = 1; 143 144 bi = bdev_get_integrity(bd); 145 if (bi) { 146 struct bio_set *bs = &ib_dev->ibd_bio_set; 147 148 if (!strcmp(bi->profile->name, "T10-DIF-TYPE3-IP") || 149 !strcmp(bi->profile->name, "T10-DIF-TYPE1-IP")) { 150 pr_err("IBLOCK export of blk_integrity: %s not" 151 " supported\n", bi->profile->name); 152 ret = -ENOSYS; 153 goto out_blkdev_put; 154 } 155 156 if (!strcmp(bi->profile->name, "T10-DIF-TYPE3-CRC")) { 157 dev->dev_attrib.pi_prot_type = TARGET_DIF_TYPE3_PROT; 158 } else if (!strcmp(bi->profile->name, "T10-DIF-TYPE1-CRC")) { 159 dev->dev_attrib.pi_prot_type = TARGET_DIF_TYPE1_PROT; 160 } 161 162 if (dev->dev_attrib.pi_prot_type) { 163 if (bioset_integrity_create(bs, IBLOCK_BIO_POOL_SIZE) < 0) { 164 pr_err("Unable to allocate bioset for PI\n"); 165 ret = -ENOMEM; 166 goto out_blkdev_put; 167 } 168 pr_debug("IBLOCK setup BIP bs->bio_integrity_pool: %p\n", 169 &bs->bio_integrity_pool); 170 } 171 dev->dev_attrib.hw_pi_prot_type = dev->dev_attrib.pi_prot_type; 172 } 173 174 return 0; 175 176 out_blkdev_put: 177 blkdev_put(ib_dev->ibd_bd, ib_dev); 178 out_free_bioset: 179 bioset_exit(&ib_dev->ibd_bio_set); 180 out: 181 return ret; 182 } 183 184 static void iblock_dev_call_rcu(struct rcu_head *p) 185 { 186 struct se_device *dev = container_of(p, struct se_device, rcu_head); 187 struct iblock_dev *ib_dev = IBLOCK_DEV(dev); 188 189 kfree(ib_dev->ibd_plug); 190 kfree(ib_dev); 191 } 192 193 static void iblock_free_device(struct se_device *dev) 194 { 195 call_rcu(&dev->rcu_head, iblock_dev_call_rcu); 196 } 197 198 static void iblock_destroy_device(struct se_device *dev) 199 { 200 struct iblock_dev *ib_dev = IBLOCK_DEV(dev); 201 202 if (ib_dev->ibd_bd != NULL) 203 blkdev_put(ib_dev->ibd_bd, ib_dev); 204 bioset_exit(&ib_dev->ibd_bio_set); 205 } 206 207 static struct se_dev_plug *iblock_plug_device(struct se_device *se_dev) 208 { 209 struct iblock_dev *ib_dev = IBLOCK_DEV(se_dev); 210 struct iblock_dev_plug *ib_dev_plug; 211 212 /* 213 * Each se_device has a per cpu work this can be run from. We 214 * shouldn't have multiple threads on the same cpu calling this 215 * at the same time. 216 */ 217 ib_dev_plug = &ib_dev->ibd_plug[raw_smp_processor_id()]; 218 if (test_and_set_bit(IBD_PLUGF_PLUGGED, &ib_dev_plug->flags)) 219 return NULL; 220 221 blk_start_plug(&ib_dev_plug->blk_plug); 222 return &ib_dev_plug->se_plug; 223 } 224 225 static void iblock_unplug_device(struct se_dev_plug *se_plug) 226 { 227 struct iblock_dev_plug *ib_dev_plug = container_of(se_plug, 228 struct iblock_dev_plug, se_plug); 229 230 blk_finish_plug(&ib_dev_plug->blk_plug); 231 clear_bit(IBD_PLUGF_PLUGGED, &ib_dev_plug->flags); 232 } 233 234 static sector_t iblock_get_blocks(struct se_device *dev) 235 { 236 struct iblock_dev *ib_dev = IBLOCK_DEV(dev); 237 u32 block_size = bdev_logical_block_size(ib_dev->ibd_bd); 238 unsigned long long blocks_long = 239 div_u64(bdev_nr_bytes(ib_dev->ibd_bd), block_size) - 1; 240 241 if (block_size == dev->dev_attrib.block_size) 242 return blocks_long; 243 244 switch (block_size) { 245 case 4096: 246 switch (dev->dev_attrib.block_size) { 247 case 2048: 248 blocks_long <<= 1; 249 break; 250 case 1024: 251 blocks_long <<= 2; 252 break; 253 case 512: 254 blocks_long <<= 3; 255 break; 256 default: 257 break; 258 } 259 break; 260 case 2048: 261 switch (dev->dev_attrib.block_size) { 262 case 4096: 263 blocks_long >>= 1; 264 break; 265 case 1024: 266 blocks_long <<= 1; 267 break; 268 case 512: 269 blocks_long <<= 2; 270 break; 271 default: 272 break; 273 } 274 break; 275 case 1024: 276 switch (dev->dev_attrib.block_size) { 277 case 4096: 278 blocks_long >>= 2; 279 break; 280 case 2048: 281 blocks_long >>= 1; 282 break; 283 case 512: 284 blocks_long <<= 1; 285 break; 286 default: 287 break; 288 } 289 break; 290 case 512: 291 switch (dev->dev_attrib.block_size) { 292 case 4096: 293 blocks_long >>= 3; 294 break; 295 case 2048: 296 blocks_long >>= 2; 297 break; 298 case 1024: 299 blocks_long >>= 1; 300 break; 301 default: 302 break; 303 } 304 break; 305 default: 306 break; 307 } 308 309 return blocks_long; 310 } 311 312 static void iblock_complete_cmd(struct se_cmd *cmd) 313 { 314 struct iblock_req *ibr = cmd->priv; 315 u8 status; 316 317 if (!refcount_dec_and_test(&ibr->pending)) 318 return; 319 320 if (atomic_read(&ibr->ib_bio_err_cnt)) 321 status = SAM_STAT_CHECK_CONDITION; 322 else 323 status = SAM_STAT_GOOD; 324 325 target_complete_cmd(cmd, status); 326 kfree(ibr); 327 } 328 329 static void iblock_bio_done(struct bio *bio) 330 { 331 struct se_cmd *cmd = bio->bi_private; 332 struct iblock_req *ibr = cmd->priv; 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); 346 } 347 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 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 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 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 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 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 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 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 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 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 bip->bip_iter.bi_size = bio_integrity_bytes(bi, bio_sectors(bio)); 687 /* virtual start sector must be in integrity interval units */ 688 bip_set_seed(bip, bio->bi_iter.bi_sector >> 689 (bi->interval_exp - SECTOR_SHIFT)); 690 691 pr_debug("IBLOCK BIP Size: %u Sector: %llu\n", bip->bip_iter.bi_size, 692 (unsigned long long)bip->bip_iter.bi_sector); 693 694 resid = bip->bip_iter.bi_size; 695 while (resid > 0 && sg_miter_next(miter)) { 696 697 len = min_t(size_t, miter->length, resid); 698 rc = bio_integrity_add_page(bio, miter->page, len, 699 offset_in_page(miter->addr)); 700 if (rc != len) { 701 pr_err("bio_integrity_add_page() failed; %d\n", rc); 702 sg_miter_stop(miter); 703 return -ENOMEM; 704 } 705 706 pr_debug("Added bio integrity page: %p length: %zu offset: %lu\n", 707 miter->page, len, offset_in_page(miter->addr)); 708 709 resid -= len; 710 if (len < miter->length) 711 miter->consumed -= miter->length - len; 712 } 713 sg_miter_stop(miter); 714 715 return 0; 716 } 717 718 static sense_reason_t 719 iblock_execute_rw(struct se_cmd *cmd, struct scatterlist *sgl, u32 sgl_nents, 720 enum dma_data_direction data_direction) 721 { 722 struct se_device *dev = cmd->se_dev; 723 sector_t block_lba = target_to_linux_sector(dev, cmd->t_task_lba); 724 struct iblock_req *ibr; 725 struct bio *bio; 726 struct bio_list list; 727 struct scatterlist *sg; 728 u32 sg_num = sgl_nents; 729 blk_opf_t opf; 730 unsigned bio_cnt; 731 int i, rc; 732 struct sg_mapping_iter prot_miter; 733 unsigned int miter_dir; 734 735 if (data_direction == DMA_TO_DEVICE) { 736 struct iblock_dev *ib_dev = IBLOCK_DEV(dev); 737 /* 738 * Force writethrough using REQ_FUA if a volatile write cache 739 * is not enabled, or if initiator set the Force Unit Access bit. 740 */ 741 opf = REQ_OP_WRITE; 742 miter_dir = SG_MITER_TO_SG; 743 if (bdev_fua(ib_dev->ibd_bd)) { 744 if (cmd->se_cmd_flags & SCF_FUA) 745 opf |= REQ_FUA; 746 else if (!bdev_write_cache(ib_dev->ibd_bd)) 747 opf |= REQ_FUA; 748 } 749 } else { 750 opf = REQ_OP_READ; 751 miter_dir = SG_MITER_FROM_SG; 752 } 753 754 ibr = kzalloc(sizeof(struct iblock_req), GFP_KERNEL); 755 if (!ibr) 756 goto fail; 757 cmd->priv = ibr; 758 759 if (!sgl_nents) { 760 refcount_set(&ibr->pending, 1); 761 iblock_complete_cmd(cmd); 762 return 0; 763 } 764 765 bio = iblock_get_bio(cmd, block_lba, sgl_nents, opf); 766 if (!bio) 767 goto fail_free_ibr; 768 769 bio_list_init(&list); 770 bio_list_add(&list, bio); 771 772 refcount_set(&ibr->pending, 2); 773 bio_cnt = 1; 774 775 if (cmd->prot_type && dev->dev_attrib.pi_prot_type) 776 sg_miter_start(&prot_miter, cmd->t_prot_sg, cmd->t_prot_nents, 777 miter_dir); 778 779 for_each_sg(sgl, sg, sgl_nents, i) { 780 /* 781 * XXX: if the length the device accepts is shorter than the 782 * length of the S/G list entry this will cause and 783 * endless loop. Better hope no driver uses huge pages. 784 */ 785 while (bio_add_page(bio, sg_page(sg), sg->length, sg->offset) 786 != sg->length) { 787 if (cmd->prot_type && dev->dev_attrib.pi_prot_type) { 788 rc = iblock_alloc_bip(cmd, bio, &prot_miter); 789 if (rc) 790 goto fail_put_bios; 791 } 792 793 if (bio_cnt >= IBLOCK_MAX_BIO_PER_TASK) { 794 iblock_submit_bios(&list); 795 bio_cnt = 0; 796 } 797 798 bio = iblock_get_bio(cmd, block_lba, sg_num, opf); 799 if (!bio) 800 goto fail_put_bios; 801 802 refcount_inc(&ibr->pending); 803 bio_list_add(&list, bio); 804 bio_cnt++; 805 } 806 807 /* Always in 512 byte units for Linux/Block */ 808 block_lba += sg->length >> SECTOR_SHIFT; 809 sg_num--; 810 } 811 812 if (cmd->prot_type && dev->dev_attrib.pi_prot_type) { 813 rc = iblock_alloc_bip(cmd, bio, &prot_miter); 814 if (rc) 815 goto fail_put_bios; 816 } 817 818 iblock_submit_bios(&list); 819 iblock_complete_cmd(cmd); 820 return 0; 821 822 fail_put_bios: 823 while ((bio = bio_list_pop(&list))) 824 bio_put(bio); 825 fail_free_ibr: 826 kfree(ibr); 827 fail: 828 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; 829 } 830 831 static sector_t iblock_get_alignment_offset_lbas(struct se_device *dev) 832 { 833 struct iblock_dev *ib_dev = IBLOCK_DEV(dev); 834 struct block_device *bd = ib_dev->ibd_bd; 835 int ret; 836 837 ret = bdev_alignment_offset(bd); 838 if (ret == -1) 839 return 0; 840 841 /* convert offset-bytes to offset-lbas */ 842 return ret / bdev_logical_block_size(bd); 843 } 844 845 static unsigned int iblock_get_lbppbe(struct se_device *dev) 846 { 847 struct iblock_dev *ib_dev = IBLOCK_DEV(dev); 848 struct block_device *bd = ib_dev->ibd_bd; 849 unsigned int logs_per_phys = 850 bdev_physical_block_size(bd) / bdev_logical_block_size(bd); 851 852 return ilog2(logs_per_phys); 853 } 854 855 static unsigned int iblock_get_io_min(struct se_device *dev) 856 { 857 struct iblock_dev *ib_dev = IBLOCK_DEV(dev); 858 struct block_device *bd = ib_dev->ibd_bd; 859 860 return bdev_io_min(bd); 861 } 862 863 static unsigned int iblock_get_io_opt(struct se_device *dev) 864 { 865 struct iblock_dev *ib_dev = IBLOCK_DEV(dev); 866 struct block_device *bd = ib_dev->ibd_bd; 867 868 return bdev_io_opt(bd); 869 } 870 871 static struct sbc_ops iblock_sbc_ops = { 872 .execute_rw = iblock_execute_rw, 873 .execute_sync_cache = iblock_execute_sync_cache, 874 .execute_write_same = iblock_execute_write_same, 875 .execute_unmap = iblock_execute_unmap, 876 }; 877 878 static sense_reason_t 879 iblock_parse_cdb(struct se_cmd *cmd) 880 { 881 return sbc_parse_cdb(cmd, &iblock_sbc_ops); 882 } 883 884 static bool iblock_get_write_cache(struct se_device *dev) 885 { 886 return bdev_write_cache(IBLOCK_DEV(dev)->ibd_bd); 887 } 888 889 static const struct target_backend_ops iblock_ops = { 890 .name = "iblock", 891 .inquiry_prod = "IBLOCK", 892 .inquiry_rev = IBLOCK_VERSION, 893 .owner = THIS_MODULE, 894 .attach_hba = iblock_attach_hba, 895 .detach_hba = iblock_detach_hba, 896 .alloc_device = iblock_alloc_device, 897 .configure_device = iblock_configure_device, 898 .destroy_device = iblock_destroy_device, 899 .free_device = iblock_free_device, 900 .configure_unmap = iblock_configure_unmap, 901 .plug_device = iblock_plug_device, 902 .unplug_device = iblock_unplug_device, 903 .parse_cdb = iblock_parse_cdb, 904 .set_configfs_dev_params = iblock_set_configfs_dev_params, 905 .show_configfs_dev_params = iblock_show_configfs_dev_params, 906 .get_device_type = sbc_get_device_type, 907 .get_blocks = iblock_get_blocks, 908 .get_alignment_offset_lbas = iblock_get_alignment_offset_lbas, 909 .get_lbppbe = iblock_get_lbppbe, 910 .get_io_min = iblock_get_io_min, 911 .get_io_opt = iblock_get_io_opt, 912 .get_write_cache = iblock_get_write_cache, 913 .tb_dev_attrib_attrs = sbc_attrib_attrs, 914 }; 915 916 static int __init iblock_module_init(void) 917 { 918 return transport_backend_register(&iblock_ops); 919 } 920 921 static void __exit iblock_module_exit(void) 922 { 923 target_backend_unregister(&iblock_ops); 924 } 925 926 MODULE_DESCRIPTION("TCM IBLOCK subsystem plugin"); 927 MODULE_AUTHOR("nab@Linux-iSCSI.org"); 928 MODULE_LICENSE("GPL"); 929 930 module_init(iblock_module_init); 931 module_exit(iblock_module_exit); 932