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 fmode_t mode; 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 mode = FMODE_READ|FMODE_EXCL; 112 if (!ib_dev->ibd_readonly) 113 mode |= FMODE_WRITE; 114 else 115 dev->dev_flags |= DF_READ_ONLY; 116 117 bd = blkdev_get_by_path(ib_dev->ibd_udev_path, mode, ib_dev); 118 if (IS_ERR(bd)) { 119 ret = PTR_ERR(bd); 120 goto out_free_bioset; 121 } 122 ib_dev->ibd_bd = bd; 123 124 q = bdev_get_queue(bd); 125 126 dev->dev_attrib.hw_block_size = bdev_logical_block_size(bd); 127 dev->dev_attrib.hw_max_sectors = mult_frac(queue_max_hw_sectors(q), 128 SECTOR_SIZE, 129 dev->dev_attrib.hw_block_size); 130 dev->dev_attrib.hw_queue_depth = q->nr_requests; 131 132 /* 133 * Enable write same emulation for IBLOCK and use 0xFFFF as 134 * the smaller WRITE_SAME(10) only has a two-byte block count. 135 */ 136 max_write_zeroes_sectors = bdev_write_zeroes_sectors(bd); 137 if (max_write_zeroes_sectors) 138 dev->dev_attrib.max_write_same_len = max_write_zeroes_sectors; 139 else 140 dev->dev_attrib.max_write_same_len = 0xFFFF; 141 142 if (bdev_nonrot(bd)) 143 dev->dev_attrib.is_nonrot = 1; 144 145 bi = bdev_get_integrity(bd); 146 if (bi) { 147 struct bio_set *bs = &ib_dev->ibd_bio_set; 148 149 if (!strcmp(bi->profile->name, "T10-DIF-TYPE3-IP") || 150 !strcmp(bi->profile->name, "T10-DIF-TYPE1-IP")) { 151 pr_err("IBLOCK export of blk_integrity: %s not" 152 " supported\n", bi->profile->name); 153 ret = -ENOSYS; 154 goto out_blkdev_put; 155 } 156 157 if (!strcmp(bi->profile->name, "T10-DIF-TYPE3-CRC")) { 158 dev->dev_attrib.pi_prot_type = TARGET_DIF_TYPE3_PROT; 159 } else if (!strcmp(bi->profile->name, "T10-DIF-TYPE1-CRC")) { 160 dev->dev_attrib.pi_prot_type = TARGET_DIF_TYPE1_PROT; 161 } 162 163 if (dev->dev_attrib.pi_prot_type) { 164 if (bioset_integrity_create(bs, IBLOCK_BIO_POOL_SIZE) < 0) { 165 pr_err("Unable to allocate bioset for PI\n"); 166 ret = -ENOMEM; 167 goto out_blkdev_put; 168 } 169 pr_debug("IBLOCK setup BIP bs->bio_integrity_pool: %p\n", 170 &bs->bio_integrity_pool); 171 } 172 dev->dev_attrib.hw_pi_prot_type = dev->dev_attrib.pi_prot_type; 173 } 174 175 return 0; 176 177 out_blkdev_put: 178 blkdev_put(ib_dev->ibd_bd, FMODE_WRITE|FMODE_READ|FMODE_EXCL); 179 out_free_bioset: 180 bioset_exit(&ib_dev->ibd_bio_set); 181 out: 182 return ret; 183 } 184 185 static void iblock_dev_call_rcu(struct rcu_head *p) 186 { 187 struct se_device *dev = container_of(p, struct se_device, rcu_head); 188 struct iblock_dev *ib_dev = IBLOCK_DEV(dev); 189 190 kfree(ib_dev->ibd_plug); 191 kfree(ib_dev); 192 } 193 194 static void iblock_free_device(struct se_device *dev) 195 { 196 call_rcu(&dev->rcu_head, iblock_dev_call_rcu); 197 } 198 199 static void iblock_destroy_device(struct se_device *dev) 200 { 201 struct iblock_dev *ib_dev = IBLOCK_DEV(dev); 202 203 if (ib_dev->ibd_bd != NULL) 204 blkdev_put(ib_dev->ibd_bd, FMODE_WRITE|FMODE_READ|FMODE_EXCL); 205 bioset_exit(&ib_dev->ibd_bio_set); 206 } 207 208 static struct se_dev_plug *iblock_plug_device(struct se_device *se_dev) 209 { 210 struct iblock_dev *ib_dev = IBLOCK_DEV(se_dev); 211 struct iblock_dev_plug *ib_dev_plug; 212 213 /* 214 * Each se_device has a per cpu work this can be run from. We 215 * shouldn't have multiple threads on the same cpu calling this 216 * at the same time. 217 */ 218 ib_dev_plug = &ib_dev->ibd_plug[raw_smp_processor_id()]; 219 if (test_and_set_bit(IBD_PLUGF_PLUGGED, &ib_dev_plug->flags)) 220 return NULL; 221 222 blk_start_plug(&ib_dev_plug->blk_plug); 223 return &ib_dev_plug->se_plug; 224 } 225 226 static void iblock_unplug_device(struct se_dev_plug *se_plug) 227 { 228 struct iblock_dev_plug *ib_dev_plug = container_of(se_plug, 229 struct iblock_dev_plug, se_plug); 230 231 blk_finish_plug(&ib_dev_plug->blk_plug); 232 clear_bit(IBD_PLUGF_PLUGGED, &ib_dev_plug->flags); 233 } 234 235 static unsigned long long iblock_emulate_read_cap_with_block_size( 236 struct se_device *dev, 237 struct block_device *bd, 238 struct request_queue *q) 239 { 240 u32 block_size = bdev_logical_block_size(bd); 241 unsigned long long blocks_long = 242 div_u64(bdev_nr_bytes(bd), block_size) - 1; 243 244 if (block_size == dev->dev_attrib.block_size) 245 return blocks_long; 246 247 switch (block_size) { 248 case 4096: 249 switch (dev->dev_attrib.block_size) { 250 case 2048: 251 blocks_long <<= 1; 252 break; 253 case 1024: 254 blocks_long <<= 2; 255 break; 256 case 512: 257 blocks_long <<= 3; 258 break; 259 default: 260 break; 261 } 262 break; 263 case 2048: 264 switch (dev->dev_attrib.block_size) { 265 case 4096: 266 blocks_long >>= 1; 267 break; 268 case 1024: 269 blocks_long <<= 1; 270 break; 271 case 512: 272 blocks_long <<= 2; 273 break; 274 default: 275 break; 276 } 277 break; 278 case 1024: 279 switch (dev->dev_attrib.block_size) { 280 case 4096: 281 blocks_long >>= 2; 282 break; 283 case 2048: 284 blocks_long >>= 1; 285 break; 286 case 512: 287 blocks_long <<= 1; 288 break; 289 default: 290 break; 291 } 292 break; 293 case 512: 294 switch (dev->dev_attrib.block_size) { 295 case 4096: 296 blocks_long >>= 3; 297 break; 298 case 2048: 299 blocks_long >>= 2; 300 break; 301 case 1024: 302 blocks_long >>= 1; 303 break; 304 default: 305 break; 306 } 307 break; 308 default: 309 break; 310 } 311 312 return blocks_long; 313 } 314 315 static void iblock_complete_cmd(struct se_cmd *cmd) 316 { 317 struct iblock_req *ibr = cmd->priv; 318 u8 status; 319 320 if (!refcount_dec_and_test(&ibr->pending)) 321 return; 322 323 if (atomic_read(&ibr->ib_bio_err_cnt)) 324 status = SAM_STAT_CHECK_CONDITION; 325 else 326 status = SAM_STAT_GOOD; 327 328 target_complete_cmd(cmd, status); 329 kfree(ibr); 330 } 331 332 static void iblock_bio_done(struct bio *bio) 333 { 334 struct se_cmd *cmd = bio->bi_private; 335 struct iblock_req *ibr = cmd->priv; 336 337 if (bio->bi_status) { 338 pr_err("bio error: %p, err: %d\n", bio, bio->bi_status); 339 /* 340 * Bump the ib_bio_err_cnt and release bio. 341 */ 342 atomic_inc(&ibr->ib_bio_err_cnt); 343 smp_mb__after_atomic(); 344 } 345 346 bio_put(bio); 347 348 iblock_complete_cmd(cmd); 349 } 350 351 static struct bio *iblock_get_bio(struct se_cmd *cmd, sector_t lba, u32 sg_num, 352 blk_opf_t opf) 353 { 354 struct iblock_dev *ib_dev = IBLOCK_DEV(cmd->se_dev); 355 struct bio *bio; 356 357 /* 358 * Only allocate as many vector entries as the bio code allows us to, 359 * we'll loop later on until we have handled the whole request. 360 */ 361 bio = bio_alloc_bioset(ib_dev->ibd_bd, bio_max_segs(sg_num), opf, 362 GFP_NOIO, &ib_dev->ibd_bio_set); 363 if (!bio) { 364 pr_err("Unable to allocate memory for bio\n"); 365 return NULL; 366 } 367 368 bio->bi_private = cmd; 369 bio->bi_end_io = &iblock_bio_done; 370 bio->bi_iter.bi_sector = lba; 371 372 return bio; 373 } 374 375 static void iblock_submit_bios(struct bio_list *list) 376 { 377 struct blk_plug plug; 378 struct bio *bio; 379 /* 380 * The block layer handles nested plugs, so just plug/unplug to handle 381 * fabric drivers that didn't support batching and multi bio cmds. 382 */ 383 blk_start_plug(&plug); 384 while ((bio = bio_list_pop(list))) 385 submit_bio(bio); 386 blk_finish_plug(&plug); 387 } 388 389 static void iblock_end_io_flush(struct bio *bio) 390 { 391 struct se_cmd *cmd = bio->bi_private; 392 393 if (bio->bi_status) 394 pr_err("IBLOCK: cache flush failed: %d\n", bio->bi_status); 395 396 if (cmd) { 397 if (bio->bi_status) 398 target_complete_cmd(cmd, SAM_STAT_CHECK_CONDITION); 399 else 400 target_complete_cmd(cmd, SAM_STAT_GOOD); 401 } 402 403 bio_put(bio); 404 } 405 406 /* 407 * Implement SYCHRONIZE CACHE. Note that we can't handle lba ranges and must 408 * always flush the whole cache. 409 */ 410 static sense_reason_t 411 iblock_execute_sync_cache(struct se_cmd *cmd) 412 { 413 struct iblock_dev *ib_dev = IBLOCK_DEV(cmd->se_dev); 414 int immed = (cmd->t_task_cdb[1] & 0x2); 415 struct bio *bio; 416 417 /* 418 * If the Immediate bit is set, queue up the GOOD response 419 * for this SYNCHRONIZE_CACHE op. 420 */ 421 if (immed) 422 target_complete_cmd(cmd, SAM_STAT_GOOD); 423 424 bio = bio_alloc(ib_dev->ibd_bd, 0, REQ_OP_WRITE | REQ_PREFLUSH, 425 GFP_KERNEL); 426 bio->bi_end_io = iblock_end_io_flush; 427 if (!immed) 428 bio->bi_private = cmd; 429 submit_bio(bio); 430 return 0; 431 } 432 433 static sense_reason_t 434 iblock_execute_unmap(struct se_cmd *cmd, sector_t lba, sector_t nolb) 435 { 436 struct block_device *bdev = IBLOCK_DEV(cmd->se_dev)->ibd_bd; 437 struct se_device *dev = cmd->se_dev; 438 int ret; 439 440 ret = blkdev_issue_discard(bdev, 441 target_to_linux_sector(dev, lba), 442 target_to_linux_sector(dev, nolb), 443 GFP_KERNEL); 444 if (ret < 0) { 445 pr_err("blkdev_issue_discard() failed: %d\n", ret); 446 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; 447 } 448 449 return 0; 450 } 451 452 static sense_reason_t 453 iblock_execute_zero_out(struct block_device *bdev, struct se_cmd *cmd) 454 { 455 struct se_device *dev = cmd->se_dev; 456 struct scatterlist *sg = &cmd->t_data_sg[0]; 457 unsigned char *buf, *not_zero; 458 int ret; 459 460 buf = kmap(sg_page(sg)) + sg->offset; 461 if (!buf) 462 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; 463 /* 464 * Fall back to block_execute_write_same() slow-path if 465 * incoming WRITE_SAME payload does not contain zeros. 466 */ 467 not_zero = memchr_inv(buf, 0x00, cmd->data_length); 468 kunmap(sg_page(sg)); 469 470 if (not_zero) 471 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; 472 473 ret = blkdev_issue_zeroout(bdev, 474 target_to_linux_sector(dev, cmd->t_task_lba), 475 target_to_linux_sector(dev, 476 sbc_get_write_same_sectors(cmd)), 477 GFP_KERNEL, BLKDEV_ZERO_NOUNMAP); 478 if (ret) 479 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; 480 481 target_complete_cmd(cmd, SAM_STAT_GOOD); 482 return 0; 483 } 484 485 static sense_reason_t 486 iblock_execute_write_same(struct se_cmd *cmd) 487 { 488 struct block_device *bdev = IBLOCK_DEV(cmd->se_dev)->ibd_bd; 489 struct iblock_req *ibr; 490 struct scatterlist *sg; 491 struct bio *bio; 492 struct bio_list list; 493 struct se_device *dev = cmd->se_dev; 494 sector_t block_lba = target_to_linux_sector(dev, cmd->t_task_lba); 495 sector_t sectors = target_to_linux_sector(dev, 496 sbc_get_write_same_sectors(cmd)); 497 498 if (cmd->prot_op) { 499 pr_err("WRITE_SAME: Protection information with IBLOCK" 500 " backends not supported\n"); 501 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; 502 } 503 504 if (!cmd->t_data_nents) 505 return TCM_INVALID_CDB_FIELD; 506 507 sg = &cmd->t_data_sg[0]; 508 509 if (cmd->t_data_nents > 1 || 510 sg->length != cmd->se_dev->dev_attrib.block_size) { 511 pr_err("WRITE_SAME: Illegal SGL t_data_nents: %u length: %u" 512 " block_size: %u\n", cmd->t_data_nents, sg->length, 513 cmd->se_dev->dev_attrib.block_size); 514 return TCM_INVALID_CDB_FIELD; 515 } 516 517 if (bdev_write_zeroes_sectors(bdev)) { 518 if (!iblock_execute_zero_out(bdev, cmd)) 519 return 0; 520 } 521 522 ibr = kzalloc(sizeof(struct iblock_req), GFP_KERNEL); 523 if (!ibr) 524 goto fail; 525 cmd->priv = ibr; 526 527 bio = iblock_get_bio(cmd, block_lba, 1, REQ_OP_WRITE); 528 if (!bio) 529 goto fail_free_ibr; 530 531 bio_list_init(&list); 532 bio_list_add(&list, bio); 533 534 refcount_set(&ibr->pending, 1); 535 536 while (sectors) { 537 while (bio_add_page(bio, sg_page(sg), sg->length, sg->offset) 538 != sg->length) { 539 540 bio = iblock_get_bio(cmd, block_lba, 1, REQ_OP_WRITE); 541 if (!bio) 542 goto fail_put_bios; 543 544 refcount_inc(&ibr->pending); 545 bio_list_add(&list, bio); 546 } 547 548 /* Always in 512 byte units for Linux/Block */ 549 block_lba += sg->length >> SECTOR_SHIFT; 550 sectors -= sg->length >> SECTOR_SHIFT; 551 } 552 553 iblock_submit_bios(&list); 554 return 0; 555 556 fail_put_bios: 557 while ((bio = bio_list_pop(&list))) 558 bio_put(bio); 559 fail_free_ibr: 560 kfree(ibr); 561 fail: 562 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; 563 } 564 565 enum { 566 Opt_udev_path, Opt_readonly, Opt_force, Opt_err 567 }; 568 569 static match_table_t tokens = { 570 {Opt_udev_path, "udev_path=%s"}, 571 {Opt_readonly, "readonly=%d"}, 572 {Opt_force, "force=%d"}, 573 {Opt_err, NULL} 574 }; 575 576 static ssize_t iblock_set_configfs_dev_params(struct se_device *dev, 577 const char *page, ssize_t count) 578 { 579 struct iblock_dev *ib_dev = IBLOCK_DEV(dev); 580 char *orig, *ptr, *arg_p, *opts; 581 substring_t args[MAX_OPT_ARGS]; 582 int ret = 0, token; 583 unsigned long tmp_readonly; 584 585 opts = kstrdup(page, GFP_KERNEL); 586 if (!opts) 587 return -ENOMEM; 588 589 orig = opts; 590 591 while ((ptr = strsep(&opts, ",\n")) != NULL) { 592 if (!*ptr) 593 continue; 594 595 token = match_token(ptr, tokens, args); 596 switch (token) { 597 case Opt_udev_path: 598 if (ib_dev->ibd_bd) { 599 pr_err("Unable to set udev_path= while" 600 " ib_dev->ibd_bd exists\n"); 601 ret = -EEXIST; 602 goto out; 603 } 604 if (match_strlcpy(ib_dev->ibd_udev_path, &args[0], 605 SE_UDEV_PATH_LEN) == 0) { 606 ret = -EINVAL; 607 break; 608 } 609 pr_debug("IBLOCK: Referencing UDEV path: %s\n", 610 ib_dev->ibd_udev_path); 611 ib_dev->ibd_flags |= IBDF_HAS_UDEV_PATH; 612 break; 613 case Opt_readonly: 614 arg_p = match_strdup(&args[0]); 615 if (!arg_p) { 616 ret = -ENOMEM; 617 break; 618 } 619 ret = kstrtoul(arg_p, 0, &tmp_readonly); 620 kfree(arg_p); 621 if (ret < 0) { 622 pr_err("kstrtoul() failed for" 623 " readonly=\n"); 624 goto out; 625 } 626 ib_dev->ibd_readonly = tmp_readonly; 627 pr_debug("IBLOCK: readonly: %d\n", ib_dev->ibd_readonly); 628 break; 629 case Opt_force: 630 break; 631 default: 632 break; 633 } 634 } 635 636 out: 637 kfree(orig); 638 return (!ret) ? count : ret; 639 } 640 641 static ssize_t iblock_show_configfs_dev_params(struct se_device *dev, char *b) 642 { 643 struct iblock_dev *ib_dev = IBLOCK_DEV(dev); 644 struct block_device *bd = ib_dev->ibd_bd; 645 ssize_t bl = 0; 646 647 if (bd) 648 bl += sprintf(b + bl, "iBlock device: %pg", bd); 649 if (ib_dev->ibd_flags & IBDF_HAS_UDEV_PATH) 650 bl += sprintf(b + bl, " UDEV PATH: %s", 651 ib_dev->ibd_udev_path); 652 bl += sprintf(b + bl, " readonly: %d\n", ib_dev->ibd_readonly); 653 654 bl += sprintf(b + bl, " "); 655 if (bd) { 656 bl += sprintf(b + bl, "Major: %d Minor: %d %s\n", 657 MAJOR(bd->bd_dev), MINOR(bd->bd_dev), 658 "CLAIMED: IBLOCK"); 659 } else { 660 bl += sprintf(b + bl, "Major: 0 Minor: 0\n"); 661 } 662 663 return bl; 664 } 665 666 static int 667 iblock_alloc_bip(struct se_cmd *cmd, struct bio *bio, 668 struct sg_mapping_iter *miter) 669 { 670 struct se_device *dev = cmd->se_dev; 671 struct blk_integrity *bi; 672 struct bio_integrity_payload *bip; 673 struct iblock_dev *ib_dev = IBLOCK_DEV(dev); 674 int rc; 675 size_t resid, len; 676 677 bi = bdev_get_integrity(ib_dev->ibd_bd); 678 if (!bi) { 679 pr_err("Unable to locate bio_integrity\n"); 680 return -ENODEV; 681 } 682 683 bip = bio_integrity_alloc(bio, GFP_NOIO, bio_max_segs(cmd->t_prot_nents)); 684 if (IS_ERR(bip)) { 685 pr_err("Unable to allocate bio_integrity_payload\n"); 686 return PTR_ERR(bip); 687 } 688 689 bip->bip_iter.bi_size = bio_integrity_bytes(bi, bio_sectors(bio)); 690 /* virtual start sector must be in integrity interval units */ 691 bip_set_seed(bip, bio->bi_iter.bi_sector >> 692 (bi->interval_exp - SECTOR_SHIFT)); 693 694 pr_debug("IBLOCK BIP Size: %u Sector: %llu\n", bip->bip_iter.bi_size, 695 (unsigned long long)bip->bip_iter.bi_sector); 696 697 resid = bip->bip_iter.bi_size; 698 while (resid > 0 && sg_miter_next(miter)) { 699 700 len = min_t(size_t, miter->length, resid); 701 rc = bio_integrity_add_page(bio, miter->page, len, 702 offset_in_page(miter->addr)); 703 if (rc != len) { 704 pr_err("bio_integrity_add_page() failed; %d\n", rc); 705 sg_miter_stop(miter); 706 return -ENOMEM; 707 } 708 709 pr_debug("Added bio integrity page: %p length: %zu offset: %lu\n", 710 miter->page, len, offset_in_page(miter->addr)); 711 712 resid -= len; 713 if (len < miter->length) 714 miter->consumed -= miter->length - len; 715 } 716 sg_miter_stop(miter); 717 718 return 0; 719 } 720 721 static sense_reason_t 722 iblock_execute_rw(struct se_cmd *cmd, struct scatterlist *sgl, u32 sgl_nents, 723 enum dma_data_direction data_direction) 724 { 725 struct se_device *dev = cmd->se_dev; 726 sector_t block_lba = target_to_linux_sector(dev, cmd->t_task_lba); 727 struct iblock_req *ibr; 728 struct bio *bio; 729 struct bio_list list; 730 struct scatterlist *sg; 731 u32 sg_num = sgl_nents; 732 blk_opf_t opf; 733 unsigned bio_cnt; 734 int i, rc; 735 struct sg_mapping_iter prot_miter; 736 unsigned int miter_dir; 737 738 if (data_direction == DMA_TO_DEVICE) { 739 struct iblock_dev *ib_dev = IBLOCK_DEV(dev); 740 /* 741 * Force writethrough using REQ_FUA if a volatile write cache 742 * is not enabled, or if initiator set the Force Unit Access bit. 743 */ 744 opf = REQ_OP_WRITE; 745 miter_dir = SG_MITER_TO_SG; 746 if (bdev_fua(ib_dev->ibd_bd)) { 747 if (cmd->se_cmd_flags & SCF_FUA) 748 opf |= REQ_FUA; 749 else if (!bdev_write_cache(ib_dev->ibd_bd)) 750 opf |= REQ_FUA; 751 } 752 } else { 753 opf = REQ_OP_READ; 754 miter_dir = SG_MITER_FROM_SG; 755 } 756 757 ibr = kzalloc(sizeof(struct iblock_req), GFP_KERNEL); 758 if (!ibr) 759 goto fail; 760 cmd->priv = ibr; 761 762 if (!sgl_nents) { 763 refcount_set(&ibr->pending, 1); 764 iblock_complete_cmd(cmd); 765 return 0; 766 } 767 768 bio = iblock_get_bio(cmd, block_lba, sgl_nents, opf); 769 if (!bio) 770 goto fail_free_ibr; 771 772 bio_list_init(&list); 773 bio_list_add(&list, bio); 774 775 refcount_set(&ibr->pending, 2); 776 bio_cnt = 1; 777 778 if (cmd->prot_type && dev->dev_attrib.pi_prot_type) 779 sg_miter_start(&prot_miter, cmd->t_prot_sg, cmd->t_prot_nents, 780 miter_dir); 781 782 for_each_sg(sgl, sg, sgl_nents, i) { 783 /* 784 * XXX: if the length the device accepts is shorter than the 785 * length of the S/G list entry this will cause and 786 * endless loop. Better hope no driver uses huge pages. 787 */ 788 while (bio_add_page(bio, sg_page(sg), sg->length, sg->offset) 789 != sg->length) { 790 if (cmd->prot_type && dev->dev_attrib.pi_prot_type) { 791 rc = iblock_alloc_bip(cmd, bio, &prot_miter); 792 if (rc) 793 goto fail_put_bios; 794 } 795 796 if (bio_cnt >= IBLOCK_MAX_BIO_PER_TASK) { 797 iblock_submit_bios(&list); 798 bio_cnt = 0; 799 } 800 801 bio = iblock_get_bio(cmd, block_lba, sg_num, opf); 802 if (!bio) 803 goto fail_put_bios; 804 805 refcount_inc(&ibr->pending); 806 bio_list_add(&list, bio); 807 bio_cnt++; 808 } 809 810 /* Always in 512 byte units for Linux/Block */ 811 block_lba += sg->length >> SECTOR_SHIFT; 812 sg_num--; 813 } 814 815 if (cmd->prot_type && dev->dev_attrib.pi_prot_type) { 816 rc = iblock_alloc_bip(cmd, bio, &prot_miter); 817 if (rc) 818 goto fail_put_bios; 819 } 820 821 iblock_submit_bios(&list); 822 iblock_complete_cmd(cmd); 823 return 0; 824 825 fail_put_bios: 826 while ((bio = bio_list_pop(&list))) 827 bio_put(bio); 828 fail_free_ibr: 829 kfree(ibr); 830 fail: 831 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; 832 } 833 834 static sector_t iblock_get_blocks(struct se_device *dev) 835 { 836 struct iblock_dev *ib_dev = IBLOCK_DEV(dev); 837 struct block_device *bd = ib_dev->ibd_bd; 838 struct request_queue *q = bdev_get_queue(bd); 839 840 return iblock_emulate_read_cap_with_block_size(dev, bd, q); 841 } 842 843 static sector_t iblock_get_alignment_offset_lbas(struct se_device *dev) 844 { 845 struct iblock_dev *ib_dev = IBLOCK_DEV(dev); 846 struct block_device *bd = ib_dev->ibd_bd; 847 int ret; 848 849 ret = bdev_alignment_offset(bd); 850 if (ret == -1) 851 return 0; 852 853 /* convert offset-bytes to offset-lbas */ 854 return ret / bdev_logical_block_size(bd); 855 } 856 857 static unsigned int iblock_get_lbppbe(struct se_device *dev) 858 { 859 struct iblock_dev *ib_dev = IBLOCK_DEV(dev); 860 struct block_device *bd = ib_dev->ibd_bd; 861 unsigned int logs_per_phys = 862 bdev_physical_block_size(bd) / bdev_logical_block_size(bd); 863 864 return ilog2(logs_per_phys); 865 } 866 867 static unsigned int iblock_get_io_min(struct se_device *dev) 868 { 869 struct iblock_dev *ib_dev = IBLOCK_DEV(dev); 870 struct block_device *bd = ib_dev->ibd_bd; 871 872 return bdev_io_min(bd); 873 } 874 875 static unsigned int iblock_get_io_opt(struct se_device *dev) 876 { 877 struct iblock_dev *ib_dev = IBLOCK_DEV(dev); 878 struct block_device *bd = ib_dev->ibd_bd; 879 880 return bdev_io_opt(bd); 881 } 882 883 static struct sbc_ops iblock_sbc_ops = { 884 .execute_rw = iblock_execute_rw, 885 .execute_sync_cache = iblock_execute_sync_cache, 886 .execute_write_same = iblock_execute_write_same, 887 .execute_unmap = iblock_execute_unmap, 888 }; 889 890 static sense_reason_t 891 iblock_parse_cdb(struct se_cmd *cmd) 892 { 893 return sbc_parse_cdb(cmd, &iblock_sbc_ops); 894 } 895 896 static bool iblock_get_write_cache(struct se_device *dev) 897 { 898 return bdev_write_cache(IBLOCK_DEV(dev)->ibd_bd); 899 } 900 901 static const struct target_backend_ops iblock_ops = { 902 .name = "iblock", 903 .inquiry_prod = "IBLOCK", 904 .inquiry_rev = IBLOCK_VERSION, 905 .owner = THIS_MODULE, 906 .attach_hba = iblock_attach_hba, 907 .detach_hba = iblock_detach_hba, 908 .alloc_device = iblock_alloc_device, 909 .configure_device = iblock_configure_device, 910 .destroy_device = iblock_destroy_device, 911 .free_device = iblock_free_device, 912 .configure_unmap = iblock_configure_unmap, 913 .plug_device = iblock_plug_device, 914 .unplug_device = iblock_unplug_device, 915 .parse_cdb = iblock_parse_cdb, 916 .set_configfs_dev_params = iblock_set_configfs_dev_params, 917 .show_configfs_dev_params = iblock_show_configfs_dev_params, 918 .get_device_type = sbc_get_device_type, 919 .get_blocks = iblock_get_blocks, 920 .get_alignment_offset_lbas = iblock_get_alignment_offset_lbas, 921 .get_lbppbe = iblock_get_lbppbe, 922 .get_io_min = iblock_get_io_min, 923 .get_io_opt = iblock_get_io_opt, 924 .get_write_cache = iblock_get_write_cache, 925 .tb_dev_attrib_attrs = sbc_attrib_attrs, 926 }; 927 928 static int __init iblock_module_init(void) 929 { 930 return transport_backend_register(&iblock_ops); 931 } 932 933 static void __exit iblock_module_exit(void) 934 { 935 target_backend_unregister(&iblock_ops); 936 } 937 938 MODULE_DESCRIPTION("TCM IBLOCK subsystem plugin"); 939 MODULE_AUTHOR("nab@Linux-iSCSI.org"); 940 MODULE_LICENSE("GPL"); 941 942 module_init(iblock_module_init); 943 module_exit(iblock_module_exit); 944