hc
2024-02-20 102a0743326a03cd1a1202ceda21e175b7d3575c
kernel/drivers/target/target_core_iblock.c
....@@ -1,3 +1,4 @@
1
+// SPDX-License-Identifier: GPL-2.0-or-later
12 /*******************************************************************************
23 * Filename: target_core_iblock.c
34 *
....@@ -7,20 +8,6 @@
78 * (c) Copyright 2003-2013 Datera, Inc.
89 *
910 * Nicholas A. Bellinger <nab@kernel.org>
10
- *
11
- * This program is free software; you can redistribute it and/or modify
12
- * it under the terms of the GNU General Public License as published by
13
- * the Free Software Foundation; either version 2 of the License, or
14
- * (at your option) any later version.
15
- *
16
- * This program is distributed in the hope that it will be useful,
17
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19
- * GNU General Public License for more details.
20
- *
21
- * You should have received a copy of the GNU General Public License
22
- * along with this program; if not, write to the Free Software
23
- * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
2411 *
2512 ******************************************************************************/
2613
....@@ -624,9 +611,8 @@
624611 bl += sprintf(b + bl, " ");
625612 if (bd) {
626613 bl += sprintf(b + bl, "Major: %d Minor: %d %s\n",
627
- MAJOR(bd->bd_dev), MINOR(bd->bd_dev), (!bd->bd_contains) ?
628
- "" : (bd->bd_holder == ib_dev) ?
629
- "CLAIMED: IBLOCK" : "CLAIMED: OS");
614
+ MAJOR(bd->bd_dev), MINOR(bd->bd_dev),
615
+ "CLAIMED: IBLOCK");
630616 } else {
631617 bl += sprintf(b + bl, "Major: 0 Minor: 0\n");
632618 }
....@@ -635,14 +621,15 @@
635621 }
636622
637623 static int
638
-iblock_alloc_bip(struct se_cmd *cmd, struct bio *bio)
624
+iblock_alloc_bip(struct se_cmd *cmd, struct bio *bio,
625
+ struct sg_mapping_iter *miter)
639626 {
640627 struct se_device *dev = cmd->se_dev;
641628 struct blk_integrity *bi;
642629 struct bio_integrity_payload *bip;
643630 struct iblock_dev *ib_dev = IBLOCK_DEV(dev);
644
- struct scatterlist *sg;
645
- int i, rc;
631
+ int rc;
632
+ size_t resid, len;
646633
647634 bi = bdev_get_integrity(ib_dev->ibd_bd);
648635 if (!bi) {
....@@ -650,31 +637,41 @@
650637 return -ENODEV;
651638 }
652639
653
- bip = bio_integrity_alloc(bio, GFP_NOIO, cmd->t_prot_nents);
640
+ bip = bio_integrity_alloc(bio, GFP_NOIO,
641
+ min_t(unsigned int, cmd->t_prot_nents, BIO_MAX_PAGES));
654642 if (IS_ERR(bip)) {
655643 pr_err("Unable to allocate bio_integrity_payload\n");
656644 return PTR_ERR(bip);
657645 }
658646
659
- bip->bip_iter.bi_size = (cmd->data_length / dev->dev_attrib.block_size) *
660
- dev->prot_length;
661
- bip->bip_iter.bi_sector = bio->bi_iter.bi_sector;
647
+ bip->bip_iter.bi_size = bio_integrity_bytes(bi, bio_sectors(bio));
648
+ /* virtual start sector must be in integrity interval units */
649
+ bip_set_seed(bip, bio->bi_iter.bi_sector >>
650
+ (bi->interval_exp - SECTOR_SHIFT));
662651
663652 pr_debug("IBLOCK BIP Size: %u Sector: %llu\n", bip->bip_iter.bi_size,
664653 (unsigned long long)bip->bip_iter.bi_sector);
665654
666
- for_each_sg(cmd->t_prot_sg, sg, cmd->t_prot_nents, i) {
655
+ resid = bip->bip_iter.bi_size;
656
+ while (resid > 0 && sg_miter_next(miter)) {
667657
668
- rc = bio_integrity_add_page(bio, sg_page(sg), sg->length,
669
- sg->offset);
670
- if (rc != sg->length) {
658
+ len = min_t(size_t, miter->length, resid);
659
+ rc = bio_integrity_add_page(bio, miter->page, len,
660
+ offset_in_page(miter->addr));
661
+ if (rc != len) {
671662 pr_err("bio_integrity_add_page() failed; %d\n", rc);
663
+ sg_miter_stop(miter);
672664 return -ENOMEM;
673665 }
674666
675
- pr_debug("Added bio integrity page: %p length: %d offset; %d\n",
676
- sg_page(sg), sg->length, sg->offset);
667
+ pr_debug("Added bio integrity page: %p length: %zu offset: %lu\n",
668
+ miter->page, len, offset_in_page(miter->addr));
669
+
670
+ resid -= len;
671
+ if (len < miter->length)
672
+ miter->consumed -= miter->length - len;
677673 }
674
+ sg_miter_stop(miter);
678675
679676 return 0;
680677 }
....@@ -686,12 +683,13 @@
686683 struct se_device *dev = cmd->se_dev;
687684 sector_t block_lba = target_to_linux_sector(dev, cmd->t_task_lba);
688685 struct iblock_req *ibr;
689
- struct bio *bio, *bio_start;
686
+ struct bio *bio;
690687 struct bio_list list;
691688 struct scatterlist *sg;
692689 u32 sg_num = sgl_nents;
693690 unsigned bio_cnt;
694
- int i, op, op_flags = 0;
691
+ int i, rc, op, op_flags = 0;
692
+ struct sg_mapping_iter prot_miter;
695693
696694 if (data_direction == DMA_TO_DEVICE) {
697695 struct iblock_dev *ib_dev = IBLOCK_DEV(dev);
....@@ -726,12 +724,16 @@
726724 if (!bio)
727725 goto fail_free_ibr;
728726
729
- bio_start = bio;
730727 bio_list_init(&list);
731728 bio_list_add(&list, bio);
732729
733730 refcount_set(&ibr->pending, 2);
734731 bio_cnt = 1;
732
+
733
+ if (cmd->prot_type && dev->dev_attrib.pi_prot_type)
734
+ sg_miter_start(&prot_miter, cmd->t_prot_sg, cmd->t_prot_nents,
735
+ op == REQ_OP_READ ? SG_MITER_FROM_SG :
736
+ SG_MITER_TO_SG);
735737
736738 for_each_sg(sgl, sg, sgl_nents, i) {
737739 /*
....@@ -741,6 +743,12 @@
741743 */
742744 while (bio_add_page(bio, sg_page(sg), sg->length, sg->offset)
743745 != sg->length) {
746
+ if (cmd->prot_type && dev->dev_attrib.pi_prot_type) {
747
+ rc = iblock_alloc_bip(cmd, bio, &prot_miter);
748
+ if (rc)
749
+ goto fail_put_bios;
750
+ }
751
+
744752 if (bio_cnt >= IBLOCK_MAX_BIO_PER_TASK) {
745753 iblock_submit_bios(&list);
746754 bio_cnt = 0;
....@@ -762,7 +770,7 @@
762770 }
763771
764772 if (cmd->prot_type && dev->dev_attrib.pi_prot_type) {
765
- int rc = iblock_alloc_bip(cmd, bio_start);
773
+ rc = iblock_alloc_bip(cmd, bio, &prot_miter);
766774 if (rc)
767775 goto fail_put_bios;
768776 }