linux-block.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "Alexander V. Buev" <a.buev@yadro.com>
To: <linux-block@vger.kernel.org>
Cc: Jens Axboe <axboe@kernel.dk>, Christoph Hellwig <hch@lst.de>,
	"Martin K . Petersen" <martin.petersen@oracle.com>,
	Mikhail Malygin <m.malygin@yadro.com>, <linux@yadro.com>,
	"Alexander V. Buev" <a.buev@yadro.com>
Subject: [PATCH 3/3] block: fops: handle IOCB_USE_PI in direct IO
Date: Thu, 28 Oct 2021 14:24:06 +0300	[thread overview]
Message-ID: <20211028112406.101314-4-a.buev@yadro.com> (raw)
In-Reply-To: <20211028112406.101314-1-a.buev@yadro.com>

Check that the size of integrity data correspond to device integrity
profile and data size. Split integrity data to the different bio's
in case of to big orginal bio (together with normal data).
Correct offset/size checking at blkdev layer in read/write_iter
functions.

Signed-off-by: Alexander V. Buev <a.buev@yadro.com>
---
 block/fops.c | 71 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 71 insertions(+)

diff --git a/block/fops.c b/block/fops.c
index 1e970c247e0e..74040314f5f3 100644
--- a/block/fops.c
+++ b/block/fops.c
@@ -197,12 +197,39 @@ static ssize_t __blkdev_direct_IO(struct kiocb *iocb, struct iov_iter *iter,
 	struct blk_plug plug;
 	struct blkdev_dio *dio;
 	struct bio *bio;
+	struct iovec *pi_iov, _pi_iov;
 	bool is_poll = (iocb->ki_flags & IOCB_HIPRI) != 0;
 	bool is_read = (iov_iter_rw(iter) == READ), is_sync;
 	loff_t pos = iocb->ki_pos;
 	blk_qc_t qc = BLK_QC_T_NONE;
 	int ret = 0;
 
+	if (iocb->ki_flags & IOCB_USE_PI) {
+		struct blk_integrity *bi = blk_get_integrity(bdev->bd_disk);
+		unsigned int intervals;
+
+		/* Last iovec contains protection information. */
+		if (!iter->nr_segs)
+			return -EINVAL;
+
+		iter->nr_segs--;
+		pi_iov = (struct iovec *)(iter->iov + iter->nr_segs);
+
+		/* TODO: seems iter is in charge of this check ? */
+		if (pi_iov->iov_len > iter->count)
+			return -EINVAL;
+
+		iter->count -= pi_iov->iov_len;
+
+		intervals = bio_integrity_intervals(bi, iter->count >> 9);
+		if (unlikely(intervals * bi->tuple_size > pi_iov->iov_len)) {
+			pr_err("Integrity & data size mismatch data=%lu integrity=%u intervals=%u tupple=%u",
+				iter->count, (unsigned int)pi_iov->iov_len,
+				intervals, bi->tuple_size);
+				return -EINVAL;
+		}
+	}
+
 	if ((pos | iov_iter_alignment(iter)) &
 	    (bdev_logical_block_size(bdev) - 1))
 		return -EINVAL;
@@ -258,6 +285,17 @@ static ssize_t __blkdev_direct_IO(struct kiocb *iocb, struct iov_iter *iter,
 		dio->size += bio->bi_iter.bi_size;
 		pos += bio->bi_iter.bi_size;
 
+		/* in case we can't add all data to one bio */
+		/* we must split integrity too */
+
+		if (iocb->ki_flags & IOCB_USE_PI) {
+			struct blk_integrity *bi = bdev_get_integrity(bio->bi_bdev);
+
+			_pi_iov.iov_base =  pi_iov->iov_base;
+			_pi_iov.iov_base += bio_integrity_bytes(bi, (dio->size-bio->bi_iter.bi_size) >> 9);
+			_pi_iov.iov_len  = bio_integrity_bytes(bi, bio->bi_iter.bi_size >> 9);
+		}
+
 		nr_pages = bio_iov_vecs_to_alloc(iter, BIO_MAX_VECS);
 		if (!nr_pages) {
 			bool polled = false;
@@ -267,6 +305,16 @@ static ssize_t __blkdev_direct_IO(struct kiocb *iocb, struct iov_iter *iter,
 				polled = true;
 			}
 
+			/* Add protection information to bio */
+			if (iocb->ki_flags & IOCB_USE_PI) {
+				ret = bio_integrity_add_pi_iovec(bio, &_pi_iov);
+				if (ret) {
+					bio->bi_status = BLK_STS_IOERR;
+					bio_endio(bio);
+					break;
+				}
+			}
+
 			qc = submit_bio(bio);
 
 			if (polled)
@@ -288,6 +336,16 @@ static ssize_t __blkdev_direct_IO(struct kiocb *iocb, struct iov_iter *iter,
 			atomic_inc(&dio->ref);
 		}
 
+
+		if (iocb->ki_flags & IOCB_USE_PI) {
+			ret = bio_integrity_add_pi_iovec(bio, &_pi_iov);
+			if (ret) {
+				bio->bi_status = BLK_STS_IOERR;
+				bio_endio(bio);
+				break;
+			}
+		}
+
 		submit_bio(bio);
 		bio = bio_alloc(GFP_KERNEL, nr_pages);
 	}
@@ -509,6 +567,12 @@ static ssize_t blkdev_write_iter(struct kiocb *iocb, struct iov_iter *from)
 	if ((iocb->ki_flags & (IOCB_NOWAIT | IOCB_DIRECT)) == IOCB_NOWAIT)
 		return -EOPNOTSUPP;
 
+	if (iocb->ki_flags & IOCB_USE_PI) {
+		if (from->nr_segs < 2)
+			return -EINVAL;
+		size += from->iov[from->nr_segs-1].iov_len;
+	}
+
 	size -= iocb->ki_pos;
 	if (iov_iter_count(from) > size) {
 		shorted = iov_iter_count(from) - size;
@@ -537,6 +601,13 @@ static ssize_t blkdev_read_iter(struct kiocb *iocb, struct iov_iter *to)
 		return 0;
 
 	size -= pos;
+
+	if (iocb->ki_flags & IOCB_USE_PI) {
+		if (to->nr_segs < 2)
+			return -EINVAL;
+		size += to->iov[to->nr_segs-1].iov_len;
+	}
+
 	if (iov_iter_count(to) > size) {
 		shorted = iov_iter_count(to) - size;
 		iov_iter_truncate(to, size);
-- 
2.33.0


  parent reply	other threads:[~2021-10-28 11:24 UTC|newest]

Thread overview: 24+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-10-28 11:24 [PATCH 0/3] implement direct IO with integrity Alexander V. Buev
2021-10-28 11:24 ` [PATCH 1/3] block: bio-integrity: add PI iovec to bio Alexander V. Buev
2021-10-28 15:16   ` Christoph Hellwig
2021-10-29  0:11   ` Chaitanya Kulkarni
2021-10-29  4:27   ` Martin K. Petersen
2021-10-29 10:59     ` Alexander V. Buev
2021-10-29  8:40   ` kernel test robot
2021-10-29  8:53   ` kernel test robot
2021-10-29  9:48   ` kernel test robot
2021-10-28 11:24 ` [PATCH 2/3] block: io_uring: add IO_WITH_PI flag to SQE Alexander V. Buev
2021-10-28 11:24 ` Alexander V. Buev [this message]
2021-10-28 15:17   ` [PATCH 3/3] block: fops: handle IOCB_USE_PI in direct IO Christoph Hellwig
2021-10-29  9:04   ` kernel test robot
2021-10-28 15:13 ` [PATCH 0/3] implement direct IO with integrity Jens Axboe
2021-10-28 15:18   ` Christoph Hellwig
2021-10-28 15:20     ` Jens Axboe
2021-10-28 15:44       ` Mikhail Malygin
2021-10-28 15:50         ` Jens Axboe
2021-10-28 15:56           ` Pavel Begunkov
2021-10-28 16:22             ` Jens Axboe
2021-10-28 17:11               ` Pavel Begunkov
2021-10-28 17:45                 ` Jens Axboe
2021-10-29  3:39       ` Martin K. Petersen
2021-10-28 15:25   ` Jens Axboe

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20211028112406.101314-4-a.buev@yadro.com \
    --to=a.buev@yadro.com \
    --cc=axboe@kernel.dk \
    --cc=hch@lst.de \
    --cc=linux-block@vger.kernel.org \
    --cc=linux@yadro.com \
    --cc=m.malygin@yadro.com \
    --cc=martin.petersen@oracle.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).