[PATCH 1/3] block: Fix blk_rq_payload_bytes()

Bart Van Assche
2018-06-26T00:10:07Z

The SCSI sd driver converts a block layer request into a SCSI WRITE
SAME command in the following cases:
1. REQ_OP_DISCARD if the sd driver has been configured to translate
this request type into WRITE SAME.
2. REQ_OP_WRITE_SAME.
The SCSI sd driver sets RQF_SPECIAL_PAYLOAD in case (1) but not in
case (2). Make sure that blk_rq_payload_bytes() handles both cases
correctly.

Fixes: 2e3258ecfaeb ("block: add blk_rq_payload_bytes")
Signed-off-by: Bart Van Assche <bart.vanassche@wdc.com>
Cc: Christoph Hellwig <hch@lst.de>
Cc: Ming Lei <ming.lei@redhat.com>
---
include/linux/blkdev.h | 14 +++++++++-----
1 file changed, 9 insertions(+), 5 deletions(-)

diff --git a/include/linux/blkdev.h b/include/linux/blkdev.h
index dcf5e0990bfa..b7519a5c1002 100644
--- a/include/linux/blkdev.h
+++ b/include/linux/blkdev.h
@@ -1091,15 +1091,19 @@ static inline unsigned int blk_rq_zone_is_seq(struct request *rq)
#endif /* CONFIG_BLK_DEV_ZONED */

/*
- * Some commands like WRITE SAME have a payload or data transfer size which
- * is different from the size of the request. Any driver that supports such
- * commands using the RQF_SPECIAL_PAYLOAD flag needs to use this helper to
- * calculate the data transfer size.
+ * Some commands like DISCARD and WRITE SAME have a payload size which is
+ * different from the number of bytes affected on the storage medium. Any
+ * driver that supports such commands needs to use this helper to calculate
+ * the data buffer size.
*/
-static inline unsigned int blk_rq_payload_bytes(struct request *rq)
+static inline unsigned int blk_rq_payload_bytes(const struct request *rq)
{
if (rq->rq_flags & RQF_SPECIAL_PAYLOAD)
return rq->special_vec.bv_len;
+ if (req_op(rq) == REQ_OP_WRITE_SAME) {
+ WARN_ON_ONCE(rq->bio->bi_vcnt != 1);
+ return rq->bio->bi_io_vec->bv_len;
+ }
return blk_rq_bytes(rq);
}

--
2.17.1