0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014 #include <linux/string.h>
0015 #include <linux/parser.h>
0016 #include <linux/timer.h>
0017 #include <linux/fs.h>
0018 #include <linux/blkdev.h>
0019 #include <linux/blk-integrity.h>
0020 #include <linux/slab.h>
0021 #include <linux/spinlock.h>
0022 #include <linux/bio.h>
0023 #include <linux/file.h>
0024 #include <linux/module.h>
0025 #include <linux/scatterlist.h>
0026 #include <scsi/scsi_proto.h>
0027 #include <asm/unaligned.h>
0028
0029 #include <target/target_core_base.h>
0030 #include <target/target_core_backend.h>
0031
0032 #include "target_core_iblock.h"
0033
0034 #define IBLOCK_MAX_BIO_PER_TASK 32
0035 #define IBLOCK_BIO_POOL_SIZE 128
0036
0037 static inline struct iblock_dev *IBLOCK_DEV(struct se_device *dev)
0038 {
0039 return container_of(dev, struct iblock_dev, dev);
0040 }
0041
0042
0043 static int iblock_attach_hba(struct se_hba *hba, u32 host_id)
0044 {
0045 pr_debug("CORE_HBA[%d] - TCM iBlock HBA Driver %s on"
0046 " Generic Target Core Stack %s\n", hba->hba_id,
0047 IBLOCK_VERSION, TARGET_CORE_VERSION);
0048 return 0;
0049 }
0050
0051 static void iblock_detach_hba(struct se_hba *hba)
0052 {
0053 }
0054
0055 static struct se_device *iblock_alloc_device(struct se_hba *hba, const char *name)
0056 {
0057 struct iblock_dev *ib_dev = NULL;
0058
0059 ib_dev = kzalloc(sizeof(struct iblock_dev), GFP_KERNEL);
0060 if (!ib_dev) {
0061 pr_err("Unable to allocate struct iblock_dev\n");
0062 return NULL;
0063 }
0064
0065 ib_dev->ibd_plug = kcalloc(nr_cpu_ids, sizeof(*ib_dev->ibd_plug),
0066 GFP_KERNEL);
0067 if (!ib_dev->ibd_plug)
0068 goto free_dev;
0069
0070 pr_debug( "IBLOCK: Allocated ib_dev for %s\n", name);
0071
0072 return &ib_dev->dev;
0073
0074 free_dev:
0075 kfree(ib_dev);
0076 return NULL;
0077 }
0078
0079 static bool iblock_configure_unmap(struct se_device *dev)
0080 {
0081 struct iblock_dev *ib_dev = IBLOCK_DEV(dev);
0082
0083 return target_configure_unmap_from_queue(&dev->dev_attrib,
0084 ib_dev->ibd_bd);
0085 }
0086
0087 static int iblock_configure_device(struct se_device *dev)
0088 {
0089 struct iblock_dev *ib_dev = IBLOCK_DEV(dev);
0090 struct request_queue *q;
0091 struct block_device *bd = NULL;
0092 struct blk_integrity *bi;
0093 fmode_t mode;
0094 unsigned int max_write_zeroes_sectors;
0095 int ret;
0096
0097 if (!(ib_dev->ibd_flags & IBDF_HAS_UDEV_PATH)) {
0098 pr_err("Missing udev_path= parameters for IBLOCK\n");
0099 return -EINVAL;
0100 }
0101
0102 ret = bioset_init(&ib_dev->ibd_bio_set, IBLOCK_BIO_POOL_SIZE, 0, BIOSET_NEED_BVECS);
0103 if (ret) {
0104 pr_err("IBLOCK: Unable to create bioset\n");
0105 goto out;
0106 }
0107
0108 pr_debug( "IBLOCK: Claiming struct block_device: %s\n",
0109 ib_dev->ibd_udev_path);
0110
0111 mode = FMODE_READ|FMODE_EXCL;
0112 if (!ib_dev->ibd_readonly)
0113 mode |= FMODE_WRITE;
0114 else
0115 dev->dev_flags |= DF_READ_ONLY;
0116
0117 bd = blkdev_get_by_path(ib_dev->ibd_udev_path, mode, ib_dev);
0118 if (IS_ERR(bd)) {
0119 ret = PTR_ERR(bd);
0120 goto out_free_bioset;
0121 }
0122 ib_dev->ibd_bd = bd;
0123
0124 q = bdev_get_queue(bd);
0125
0126 dev->dev_attrib.hw_block_size = bdev_logical_block_size(bd);
0127 dev->dev_attrib.hw_max_sectors = queue_max_hw_sectors(q);
0128 dev->dev_attrib.hw_queue_depth = q->nr_requests;
0129
0130
0131
0132
0133
0134 max_write_zeroes_sectors = bdev_write_zeroes_sectors(bd);
0135 if (max_write_zeroes_sectors)
0136 dev->dev_attrib.max_write_same_len = max_write_zeroes_sectors;
0137 else
0138 dev->dev_attrib.max_write_same_len = 0xFFFF;
0139
0140 if (bdev_nonrot(bd))
0141 dev->dev_attrib.is_nonrot = 1;
0142
0143 bi = bdev_get_integrity(bd);
0144 if (bi) {
0145 struct bio_set *bs = &ib_dev->ibd_bio_set;
0146
0147 if (!strcmp(bi->profile->name, "T10-DIF-TYPE3-IP") ||
0148 !strcmp(bi->profile->name, "T10-DIF-TYPE1-IP")) {
0149 pr_err("IBLOCK export of blk_integrity: %s not"
0150 " supported\n", bi->profile->name);
0151 ret = -ENOSYS;
0152 goto out_blkdev_put;
0153 }
0154
0155 if (!strcmp(bi->profile->name, "T10-DIF-TYPE3-CRC")) {
0156 dev->dev_attrib.pi_prot_type = TARGET_DIF_TYPE3_PROT;
0157 } else if (!strcmp(bi->profile->name, "T10-DIF-TYPE1-CRC")) {
0158 dev->dev_attrib.pi_prot_type = TARGET_DIF_TYPE1_PROT;
0159 }
0160
0161 if (dev->dev_attrib.pi_prot_type) {
0162 if (bioset_integrity_create(bs, IBLOCK_BIO_POOL_SIZE) < 0) {
0163 pr_err("Unable to allocate bioset for PI\n");
0164 ret = -ENOMEM;
0165 goto out_blkdev_put;
0166 }
0167 pr_debug("IBLOCK setup BIP bs->bio_integrity_pool: %p\n",
0168 &bs->bio_integrity_pool);
0169 }
0170 dev->dev_attrib.hw_pi_prot_type = dev->dev_attrib.pi_prot_type;
0171 }
0172
0173 return 0;
0174
0175 out_blkdev_put:
0176 blkdev_put(ib_dev->ibd_bd, FMODE_WRITE|FMODE_READ|FMODE_EXCL);
0177 out_free_bioset:
0178 bioset_exit(&ib_dev->ibd_bio_set);
0179 out:
0180 return ret;
0181 }
0182
0183 static void iblock_dev_call_rcu(struct rcu_head *p)
0184 {
0185 struct se_device *dev = container_of(p, struct se_device, rcu_head);
0186 struct iblock_dev *ib_dev = IBLOCK_DEV(dev);
0187
0188 kfree(ib_dev->ibd_plug);
0189 kfree(ib_dev);
0190 }
0191
0192 static void iblock_free_device(struct se_device *dev)
0193 {
0194 call_rcu(&dev->rcu_head, iblock_dev_call_rcu);
0195 }
0196
0197 static void iblock_destroy_device(struct se_device *dev)
0198 {
0199 struct iblock_dev *ib_dev = IBLOCK_DEV(dev);
0200
0201 if (ib_dev->ibd_bd != NULL)
0202 blkdev_put(ib_dev->ibd_bd, FMODE_WRITE|FMODE_READ|FMODE_EXCL);
0203 bioset_exit(&ib_dev->ibd_bio_set);
0204 }
0205
0206 static struct se_dev_plug *iblock_plug_device(struct se_device *se_dev)
0207 {
0208 struct iblock_dev *ib_dev = IBLOCK_DEV(se_dev);
0209 struct iblock_dev_plug *ib_dev_plug;
0210
0211
0212
0213
0214
0215
0216 ib_dev_plug = &ib_dev->ibd_plug[raw_smp_processor_id()];
0217 if (test_and_set_bit(IBD_PLUGF_PLUGGED, &ib_dev_plug->flags))
0218 return NULL;
0219
0220 blk_start_plug(&ib_dev_plug->blk_plug);
0221 return &ib_dev_plug->se_plug;
0222 }
0223
0224 static void iblock_unplug_device(struct se_dev_plug *se_plug)
0225 {
0226 struct iblock_dev_plug *ib_dev_plug = container_of(se_plug,
0227 struct iblock_dev_plug, se_plug);
0228
0229 blk_finish_plug(&ib_dev_plug->blk_plug);
0230 clear_bit(IBD_PLUGF_PLUGGED, &ib_dev_plug->flags);
0231 }
0232
0233 static unsigned long long iblock_emulate_read_cap_with_block_size(
0234 struct se_device *dev,
0235 struct block_device *bd,
0236 struct request_queue *q)
0237 {
0238 u32 block_size = bdev_logical_block_size(bd);
0239 unsigned long long blocks_long =
0240 div_u64(bdev_nr_bytes(bd), block_size) - 1;
0241
0242 if (block_size == dev->dev_attrib.block_size)
0243 return blocks_long;
0244
0245 switch (block_size) {
0246 case 4096:
0247 switch (dev->dev_attrib.block_size) {
0248 case 2048:
0249 blocks_long <<= 1;
0250 break;
0251 case 1024:
0252 blocks_long <<= 2;
0253 break;
0254 case 512:
0255 blocks_long <<= 3;
0256 break;
0257 default:
0258 break;
0259 }
0260 break;
0261 case 2048:
0262 switch (dev->dev_attrib.block_size) {
0263 case 4096:
0264 blocks_long >>= 1;
0265 break;
0266 case 1024:
0267 blocks_long <<= 1;
0268 break;
0269 case 512:
0270 blocks_long <<= 2;
0271 break;
0272 default:
0273 break;
0274 }
0275 break;
0276 case 1024:
0277 switch (dev->dev_attrib.block_size) {
0278 case 4096:
0279 blocks_long >>= 2;
0280 break;
0281 case 2048:
0282 blocks_long >>= 1;
0283 break;
0284 case 512:
0285 blocks_long <<= 1;
0286 break;
0287 default:
0288 break;
0289 }
0290 break;
0291 case 512:
0292 switch (dev->dev_attrib.block_size) {
0293 case 4096:
0294 blocks_long >>= 3;
0295 break;
0296 case 2048:
0297 blocks_long >>= 2;
0298 break;
0299 case 1024:
0300 blocks_long >>= 1;
0301 break;
0302 default:
0303 break;
0304 }
0305 break;
0306 default:
0307 break;
0308 }
0309
0310 return blocks_long;
0311 }
0312
0313 static void iblock_complete_cmd(struct se_cmd *cmd)
0314 {
0315 struct iblock_req *ibr = cmd->priv;
0316 u8 status;
0317
0318 if (!refcount_dec_and_test(&ibr->pending))
0319 return;
0320
0321 if (atomic_read(&ibr->ib_bio_err_cnt))
0322 status = SAM_STAT_CHECK_CONDITION;
0323 else
0324 status = SAM_STAT_GOOD;
0325
0326 target_complete_cmd(cmd, status);
0327 kfree(ibr);
0328 }
0329
0330 static void iblock_bio_done(struct bio *bio)
0331 {
0332 struct se_cmd *cmd = bio->bi_private;
0333 struct iblock_req *ibr = cmd->priv;
0334
0335 if (bio->bi_status) {
0336 pr_err("bio error: %p, err: %d\n", bio, bio->bi_status);
0337
0338
0339
0340 atomic_inc(&ibr->ib_bio_err_cnt);
0341 smp_mb__after_atomic();
0342 }
0343
0344 bio_put(bio);
0345
0346 iblock_complete_cmd(cmd);
0347 }
0348
0349 static struct bio *iblock_get_bio(struct se_cmd *cmd, sector_t lba, u32 sg_num,
0350 blk_opf_t opf)
0351 {
0352 struct iblock_dev *ib_dev = IBLOCK_DEV(cmd->se_dev);
0353 struct bio *bio;
0354
0355
0356
0357
0358
0359 bio = bio_alloc_bioset(ib_dev->ibd_bd, bio_max_segs(sg_num), opf,
0360 GFP_NOIO, &ib_dev->ibd_bio_set);
0361 if (!bio) {
0362 pr_err("Unable to allocate memory for bio\n");
0363 return NULL;
0364 }
0365
0366 bio->bi_private = cmd;
0367 bio->bi_end_io = &iblock_bio_done;
0368 bio->bi_iter.bi_sector = lba;
0369
0370 return bio;
0371 }
0372
0373 static void iblock_submit_bios(struct bio_list *list)
0374 {
0375 struct blk_plug plug;
0376 struct bio *bio;
0377
0378
0379
0380
0381 blk_start_plug(&plug);
0382 while ((bio = bio_list_pop(list)))
0383 submit_bio(bio);
0384 blk_finish_plug(&plug);
0385 }
0386
0387 static void iblock_end_io_flush(struct bio *bio)
0388 {
0389 struct se_cmd *cmd = bio->bi_private;
0390
0391 if (bio->bi_status)
0392 pr_err("IBLOCK: cache flush failed: %d\n", bio->bi_status);
0393
0394 if (cmd) {
0395 if (bio->bi_status)
0396 target_complete_cmd(cmd, SAM_STAT_CHECK_CONDITION);
0397 else
0398 target_complete_cmd(cmd, SAM_STAT_GOOD);
0399 }
0400
0401 bio_put(bio);
0402 }
0403
0404
0405
0406
0407
0408 static sense_reason_t
0409 iblock_execute_sync_cache(struct se_cmd *cmd)
0410 {
0411 struct iblock_dev *ib_dev = IBLOCK_DEV(cmd->se_dev);
0412 int immed = (cmd->t_task_cdb[1] & 0x2);
0413 struct bio *bio;
0414
0415
0416
0417
0418
0419 if (immed)
0420 target_complete_cmd(cmd, SAM_STAT_GOOD);
0421
0422 bio = bio_alloc(ib_dev->ibd_bd, 0, REQ_OP_WRITE | REQ_PREFLUSH,
0423 GFP_KERNEL);
0424 bio->bi_end_io = iblock_end_io_flush;
0425 if (!immed)
0426 bio->bi_private = cmd;
0427 submit_bio(bio);
0428 return 0;
0429 }
0430
0431 static sense_reason_t
0432 iblock_execute_unmap(struct se_cmd *cmd, sector_t lba, sector_t nolb)
0433 {
0434 struct block_device *bdev = IBLOCK_DEV(cmd->se_dev)->ibd_bd;
0435 struct se_device *dev = cmd->se_dev;
0436 int ret;
0437
0438 ret = blkdev_issue_discard(bdev,
0439 target_to_linux_sector(dev, lba),
0440 target_to_linux_sector(dev, nolb),
0441 GFP_KERNEL);
0442 if (ret < 0) {
0443 pr_err("blkdev_issue_discard() failed: %d\n", ret);
0444 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
0445 }
0446
0447 return 0;
0448 }
0449
0450 static sense_reason_t
0451 iblock_execute_zero_out(struct block_device *bdev, struct se_cmd *cmd)
0452 {
0453 struct se_device *dev = cmd->se_dev;
0454 struct scatterlist *sg = &cmd->t_data_sg[0];
0455 unsigned char *buf, *not_zero;
0456 int ret;
0457
0458 buf = kmap(sg_page(sg)) + sg->offset;
0459 if (!buf)
0460 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
0461
0462
0463
0464
0465 not_zero = memchr_inv(buf, 0x00, cmd->data_length);
0466 kunmap(sg_page(sg));
0467
0468 if (not_zero)
0469 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
0470
0471 ret = blkdev_issue_zeroout(bdev,
0472 target_to_linux_sector(dev, cmd->t_task_lba),
0473 target_to_linux_sector(dev,
0474 sbc_get_write_same_sectors(cmd)),
0475 GFP_KERNEL, BLKDEV_ZERO_NOUNMAP);
0476 if (ret)
0477 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
0478
0479 target_complete_cmd(cmd, SAM_STAT_GOOD);
0480 return 0;
0481 }
0482
0483 static sense_reason_t
0484 iblock_execute_write_same(struct se_cmd *cmd)
0485 {
0486 struct block_device *bdev = IBLOCK_DEV(cmd->se_dev)->ibd_bd;
0487 struct iblock_req *ibr;
0488 struct scatterlist *sg;
0489 struct bio *bio;
0490 struct bio_list list;
0491 struct se_device *dev = cmd->se_dev;
0492 sector_t block_lba = target_to_linux_sector(dev, cmd->t_task_lba);
0493 sector_t sectors = target_to_linux_sector(dev,
0494 sbc_get_write_same_sectors(cmd));
0495
0496 if (cmd->prot_op) {
0497 pr_err("WRITE_SAME: Protection information with IBLOCK"
0498 " backends not supported\n");
0499 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
0500 }
0501
0502 if (!cmd->t_data_nents)
0503 return TCM_INVALID_CDB_FIELD;
0504
0505 sg = &cmd->t_data_sg[0];
0506
0507 if (cmd->t_data_nents > 1 ||
0508 sg->length != cmd->se_dev->dev_attrib.block_size) {
0509 pr_err("WRITE_SAME: Illegal SGL t_data_nents: %u length: %u"
0510 " block_size: %u\n", cmd->t_data_nents, sg->length,
0511 cmd->se_dev->dev_attrib.block_size);
0512 return TCM_INVALID_CDB_FIELD;
0513 }
0514
0515 if (bdev_write_zeroes_sectors(bdev)) {
0516 if (!iblock_execute_zero_out(bdev, cmd))
0517 return 0;
0518 }
0519
0520 ibr = kzalloc(sizeof(struct iblock_req), GFP_KERNEL);
0521 if (!ibr)
0522 goto fail;
0523 cmd->priv = ibr;
0524
0525 bio = iblock_get_bio(cmd, block_lba, 1, REQ_OP_WRITE);
0526 if (!bio)
0527 goto fail_free_ibr;
0528
0529 bio_list_init(&list);
0530 bio_list_add(&list, bio);
0531
0532 refcount_set(&ibr->pending, 1);
0533
0534 while (sectors) {
0535 while (bio_add_page(bio, sg_page(sg), sg->length, sg->offset)
0536 != sg->length) {
0537
0538 bio = iblock_get_bio(cmd, block_lba, 1, REQ_OP_WRITE);
0539 if (!bio)
0540 goto fail_put_bios;
0541
0542 refcount_inc(&ibr->pending);
0543 bio_list_add(&list, bio);
0544 }
0545
0546
0547 block_lba += sg->length >> SECTOR_SHIFT;
0548 sectors -= sg->length >> SECTOR_SHIFT;
0549 }
0550
0551 iblock_submit_bios(&list);
0552 return 0;
0553
0554 fail_put_bios:
0555 while ((bio = bio_list_pop(&list)))
0556 bio_put(bio);
0557 fail_free_ibr:
0558 kfree(ibr);
0559 fail:
0560 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
0561 }
0562
0563 enum {
0564 Opt_udev_path, Opt_readonly, Opt_force, Opt_err
0565 };
0566
0567 static match_table_t tokens = {
0568 {Opt_udev_path, "udev_path=%s"},
0569 {Opt_readonly, "readonly=%d"},
0570 {Opt_force, "force=%d"},
0571 {Opt_err, NULL}
0572 };
0573
0574 static ssize_t iblock_set_configfs_dev_params(struct se_device *dev,
0575 const char *page, ssize_t count)
0576 {
0577 struct iblock_dev *ib_dev = IBLOCK_DEV(dev);
0578 char *orig, *ptr, *arg_p, *opts;
0579 substring_t args[MAX_OPT_ARGS];
0580 int ret = 0, token;
0581 unsigned long tmp_readonly;
0582
0583 opts = kstrdup(page, GFP_KERNEL);
0584 if (!opts)
0585 return -ENOMEM;
0586
0587 orig = opts;
0588
0589 while ((ptr = strsep(&opts, ",\n")) != NULL) {
0590 if (!*ptr)
0591 continue;
0592
0593 token = match_token(ptr, tokens, args);
0594 switch (token) {
0595 case Opt_udev_path:
0596 if (ib_dev->ibd_bd) {
0597 pr_err("Unable to set udev_path= while"
0598 " ib_dev->ibd_bd exists\n");
0599 ret = -EEXIST;
0600 goto out;
0601 }
0602 if (match_strlcpy(ib_dev->ibd_udev_path, &args[0],
0603 SE_UDEV_PATH_LEN) == 0) {
0604 ret = -EINVAL;
0605 break;
0606 }
0607 pr_debug("IBLOCK: Referencing UDEV path: %s\n",
0608 ib_dev->ibd_udev_path);
0609 ib_dev->ibd_flags |= IBDF_HAS_UDEV_PATH;
0610 break;
0611 case Opt_readonly:
0612 arg_p = match_strdup(&args[0]);
0613 if (!arg_p) {
0614 ret = -ENOMEM;
0615 break;
0616 }
0617 ret = kstrtoul(arg_p, 0, &tmp_readonly);
0618 kfree(arg_p);
0619 if (ret < 0) {
0620 pr_err("kstrtoul() failed for"
0621 " readonly=\n");
0622 goto out;
0623 }
0624 ib_dev->ibd_readonly = tmp_readonly;
0625 pr_debug("IBLOCK: readonly: %d\n", ib_dev->ibd_readonly);
0626 break;
0627 case Opt_force:
0628 break;
0629 default:
0630 break;
0631 }
0632 }
0633
0634 out:
0635 kfree(orig);
0636 return (!ret) ? count : ret;
0637 }
0638
0639 static ssize_t iblock_show_configfs_dev_params(struct se_device *dev, char *b)
0640 {
0641 struct iblock_dev *ib_dev = IBLOCK_DEV(dev);
0642 struct block_device *bd = ib_dev->ibd_bd;
0643 ssize_t bl = 0;
0644
0645 if (bd)
0646 bl += sprintf(b + bl, "iBlock device: %pg", bd);
0647 if (ib_dev->ibd_flags & IBDF_HAS_UDEV_PATH)
0648 bl += sprintf(b + bl, " UDEV PATH: %s",
0649 ib_dev->ibd_udev_path);
0650 bl += sprintf(b + bl, " readonly: %d\n", ib_dev->ibd_readonly);
0651
0652 bl += sprintf(b + bl, " ");
0653 if (bd) {
0654 bl += sprintf(b + bl, "Major: %d Minor: %d %s\n",
0655 MAJOR(bd->bd_dev), MINOR(bd->bd_dev),
0656 "CLAIMED: IBLOCK");
0657 } else {
0658 bl += sprintf(b + bl, "Major: 0 Minor: 0\n");
0659 }
0660
0661 return bl;
0662 }
0663
0664 static int
0665 iblock_alloc_bip(struct se_cmd *cmd, struct bio *bio,
0666 struct sg_mapping_iter *miter)
0667 {
0668 struct se_device *dev = cmd->se_dev;
0669 struct blk_integrity *bi;
0670 struct bio_integrity_payload *bip;
0671 struct iblock_dev *ib_dev = IBLOCK_DEV(dev);
0672 int rc;
0673 size_t resid, len;
0674
0675 bi = bdev_get_integrity(ib_dev->ibd_bd);
0676 if (!bi) {
0677 pr_err("Unable to locate bio_integrity\n");
0678 return -ENODEV;
0679 }
0680
0681 bip = bio_integrity_alloc(bio, GFP_NOIO, bio_max_segs(cmd->t_prot_nents));
0682 if (IS_ERR(bip)) {
0683 pr_err("Unable to allocate bio_integrity_payload\n");
0684 return PTR_ERR(bip);
0685 }
0686
0687 bip->bip_iter.bi_size = bio_integrity_bytes(bi, bio_sectors(bio));
0688
0689 bip_set_seed(bip, bio->bi_iter.bi_sector >>
0690 (bi->interval_exp - SECTOR_SHIFT));
0691
0692 pr_debug("IBLOCK BIP Size: %u Sector: %llu\n", bip->bip_iter.bi_size,
0693 (unsigned long long)bip->bip_iter.bi_sector);
0694
0695 resid = bip->bip_iter.bi_size;
0696 while (resid > 0 && sg_miter_next(miter)) {
0697
0698 len = min_t(size_t, miter->length, resid);
0699 rc = bio_integrity_add_page(bio, miter->page, len,
0700 offset_in_page(miter->addr));
0701 if (rc != len) {
0702 pr_err("bio_integrity_add_page() failed; %d\n", rc);
0703 sg_miter_stop(miter);
0704 return -ENOMEM;
0705 }
0706
0707 pr_debug("Added bio integrity page: %p length: %zu offset: %lu\n",
0708 miter->page, len, offset_in_page(miter->addr));
0709
0710 resid -= len;
0711 if (len < miter->length)
0712 miter->consumed -= miter->length - len;
0713 }
0714 sg_miter_stop(miter);
0715
0716 return 0;
0717 }
0718
0719 static sense_reason_t
0720 iblock_execute_rw(struct se_cmd *cmd, struct scatterlist *sgl, u32 sgl_nents,
0721 enum dma_data_direction data_direction)
0722 {
0723 struct se_device *dev = cmd->se_dev;
0724 sector_t block_lba = target_to_linux_sector(dev, cmd->t_task_lba);
0725 struct iblock_req *ibr;
0726 struct bio *bio;
0727 struct bio_list list;
0728 struct scatterlist *sg;
0729 u32 sg_num = sgl_nents;
0730 blk_opf_t opf;
0731 unsigned bio_cnt;
0732 int i, rc;
0733 struct sg_mapping_iter prot_miter;
0734 unsigned int miter_dir;
0735
0736 if (data_direction == DMA_TO_DEVICE) {
0737 struct iblock_dev *ib_dev = IBLOCK_DEV(dev);
0738
0739
0740
0741
0742 opf = REQ_OP_WRITE;
0743 miter_dir = SG_MITER_TO_SG;
0744 if (bdev_fua(ib_dev->ibd_bd)) {
0745 if (cmd->se_cmd_flags & SCF_FUA)
0746 opf |= REQ_FUA;
0747 else if (!bdev_write_cache(ib_dev->ibd_bd))
0748 opf |= REQ_FUA;
0749 }
0750 } else {
0751 opf = REQ_OP_READ;
0752 miter_dir = SG_MITER_FROM_SG;
0753 }
0754
0755 ibr = kzalloc(sizeof(struct iblock_req), GFP_KERNEL);
0756 if (!ibr)
0757 goto fail;
0758 cmd->priv = ibr;
0759
0760 if (!sgl_nents) {
0761 refcount_set(&ibr->pending, 1);
0762 iblock_complete_cmd(cmd);
0763 return 0;
0764 }
0765
0766 bio = iblock_get_bio(cmd, block_lba, sgl_nents, opf);
0767 if (!bio)
0768 goto fail_free_ibr;
0769
0770 bio_list_init(&list);
0771 bio_list_add(&list, bio);
0772
0773 refcount_set(&ibr->pending, 2);
0774 bio_cnt = 1;
0775
0776 if (cmd->prot_type && dev->dev_attrib.pi_prot_type)
0777 sg_miter_start(&prot_miter, cmd->t_prot_sg, cmd->t_prot_nents,
0778 miter_dir);
0779
0780 for_each_sg(sgl, sg, sgl_nents, i) {
0781
0782
0783
0784
0785
0786 while (bio_add_page(bio, sg_page(sg), sg->length, sg->offset)
0787 != sg->length) {
0788 if (cmd->prot_type && dev->dev_attrib.pi_prot_type) {
0789 rc = iblock_alloc_bip(cmd, bio, &prot_miter);
0790 if (rc)
0791 goto fail_put_bios;
0792 }
0793
0794 if (bio_cnt >= IBLOCK_MAX_BIO_PER_TASK) {
0795 iblock_submit_bios(&list);
0796 bio_cnt = 0;
0797 }
0798
0799 bio = iblock_get_bio(cmd, block_lba, sg_num, opf);
0800 if (!bio)
0801 goto fail_put_bios;
0802
0803 refcount_inc(&ibr->pending);
0804 bio_list_add(&list, bio);
0805 bio_cnt++;
0806 }
0807
0808
0809 block_lba += sg->length >> SECTOR_SHIFT;
0810 sg_num--;
0811 }
0812
0813 if (cmd->prot_type && dev->dev_attrib.pi_prot_type) {
0814 rc = iblock_alloc_bip(cmd, bio, &prot_miter);
0815 if (rc)
0816 goto fail_put_bios;
0817 }
0818
0819 iblock_submit_bios(&list);
0820 iblock_complete_cmd(cmd);
0821 return 0;
0822
0823 fail_put_bios:
0824 while ((bio = bio_list_pop(&list)))
0825 bio_put(bio);
0826 fail_free_ibr:
0827 kfree(ibr);
0828 fail:
0829 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
0830 }
0831
0832 static sector_t iblock_get_blocks(struct se_device *dev)
0833 {
0834 struct iblock_dev *ib_dev = IBLOCK_DEV(dev);
0835 struct block_device *bd = ib_dev->ibd_bd;
0836 struct request_queue *q = bdev_get_queue(bd);
0837
0838 return iblock_emulate_read_cap_with_block_size(dev, bd, q);
0839 }
0840
0841 static sector_t iblock_get_alignment_offset_lbas(struct se_device *dev)
0842 {
0843 struct iblock_dev *ib_dev = IBLOCK_DEV(dev);
0844 struct block_device *bd = ib_dev->ibd_bd;
0845 int ret;
0846
0847 ret = bdev_alignment_offset(bd);
0848 if (ret == -1)
0849 return 0;
0850
0851
0852 return ret / bdev_logical_block_size(bd);
0853 }
0854
0855 static unsigned int iblock_get_lbppbe(struct se_device *dev)
0856 {
0857 struct iblock_dev *ib_dev = IBLOCK_DEV(dev);
0858 struct block_device *bd = ib_dev->ibd_bd;
0859 unsigned int logs_per_phys =
0860 bdev_physical_block_size(bd) / bdev_logical_block_size(bd);
0861
0862 return ilog2(logs_per_phys);
0863 }
0864
0865 static unsigned int iblock_get_io_min(struct se_device *dev)
0866 {
0867 struct iblock_dev *ib_dev = IBLOCK_DEV(dev);
0868 struct block_device *bd = ib_dev->ibd_bd;
0869
0870 return bdev_io_min(bd);
0871 }
0872
0873 static unsigned int iblock_get_io_opt(struct se_device *dev)
0874 {
0875 struct iblock_dev *ib_dev = IBLOCK_DEV(dev);
0876 struct block_device *bd = ib_dev->ibd_bd;
0877
0878 return bdev_io_opt(bd);
0879 }
0880
0881 static struct sbc_ops iblock_sbc_ops = {
0882 .execute_rw = iblock_execute_rw,
0883 .execute_sync_cache = iblock_execute_sync_cache,
0884 .execute_write_same = iblock_execute_write_same,
0885 .execute_unmap = iblock_execute_unmap,
0886 };
0887
0888 static sense_reason_t
0889 iblock_parse_cdb(struct se_cmd *cmd)
0890 {
0891 return sbc_parse_cdb(cmd, &iblock_sbc_ops);
0892 }
0893
0894 static bool iblock_get_write_cache(struct se_device *dev)
0895 {
0896 return bdev_write_cache(IBLOCK_DEV(dev)->ibd_bd);
0897 }
0898
0899 static const struct target_backend_ops iblock_ops = {
0900 .name = "iblock",
0901 .inquiry_prod = "IBLOCK",
0902 .inquiry_rev = IBLOCK_VERSION,
0903 .owner = THIS_MODULE,
0904 .attach_hba = iblock_attach_hba,
0905 .detach_hba = iblock_detach_hba,
0906 .alloc_device = iblock_alloc_device,
0907 .configure_device = iblock_configure_device,
0908 .destroy_device = iblock_destroy_device,
0909 .free_device = iblock_free_device,
0910 .configure_unmap = iblock_configure_unmap,
0911 .plug_device = iblock_plug_device,
0912 .unplug_device = iblock_unplug_device,
0913 .parse_cdb = iblock_parse_cdb,
0914 .set_configfs_dev_params = iblock_set_configfs_dev_params,
0915 .show_configfs_dev_params = iblock_show_configfs_dev_params,
0916 .get_device_type = sbc_get_device_type,
0917 .get_blocks = iblock_get_blocks,
0918 .get_alignment_offset_lbas = iblock_get_alignment_offset_lbas,
0919 .get_lbppbe = iblock_get_lbppbe,
0920 .get_io_min = iblock_get_io_min,
0921 .get_io_opt = iblock_get_io_opt,
0922 .get_write_cache = iblock_get_write_cache,
0923 .tb_dev_attrib_attrs = sbc_attrib_attrs,
0924 };
0925
0926 static int __init iblock_module_init(void)
0927 {
0928 return transport_backend_register(&iblock_ops);
0929 }
0930
0931 static void __exit iblock_module_exit(void)
0932 {
0933 target_backend_unregister(&iblock_ops);
0934 }
0935
0936 MODULE_DESCRIPTION("TCM IBLOCK subsystem plugin");
0937 MODULE_AUTHOR("nab@Linux-iSCSI.org");
0938 MODULE_LICENSE("GPL");
0939
0940 module_init(iblock_module_init);
0941 module_exit(iblock_module_exit);