target: Fix VERIFY and WRITE VERIFY command parsing
diff mbox

Message ID 1D08B61A9CF0974AA09887BE32D889DA0E2C0B@ULS-OP-MBXIP03.sdcorp.global.sandisk.com
State New, archived
Headers show

Commit Message

Bart Van Assche March 10, 2017, 7:42 p.m. UTC
Use the value of the BYTCHK field to determine the size of the
Data-Out buffer. For VERIFY, honor the VRPROTECT, DPO and FUA
fields. This patch avoids that LIO complains about a mismatch
between the expected transfer length and the SCSI CDB length
if the value of the BYTCHK field is 0.

Signed-off-by: Bart Van Assche <bart.vanassche@sandisk.com>
---
 drivers/target/target_core_sbc.c | 54 +++++++++++++++++++++++++++++++++++++---
 1 file changed, 50 insertions(+), 4 deletions(-)

Comments

Nicholas A. Bellinger March 19, 2017, 12:23 a.m. UTC | #1
On Fri, 2017-03-10 at 19:42 +0000, Bart Van Assche wrote:
> Use the value of the BYTCHK field to determine the size of the
> Data-Out buffer. For VERIFY, honor the VRPROTECT, DPO and FUA
> fields. This patch avoids that LIO complains about a mismatch
> between the expected transfer length and the SCSI CDB length
> if the value of the BYTCHK field is 0.
> 
> Signed-off-by: Bart Van Assche <bart.vanassche@sandisk.com>
> ---
>  drivers/target/target_core_sbc.c | 54 +++++++++++++++++++++++++++++++++++++---
>  1 file changed, 50 insertions(+), 4 deletions(-)
> 
> diff --git a/drivers/target/target_core_sbc.c b/drivers/target/target_core_sbc.c
> index 68d8aef7ab78..0f6f98491f5c 100644
> --- a/drivers/target/target_core_sbc.c
> +++ b/drivers/target/target_core_sbc.c
> @@ -828,6 +828,47 @@ sbc_check_dpofua(struct se_device *dev, struct se_cmd *cmd, unsigned char *cdb)
>  	return 0;
>  }
>  
> +/**
> + * sbc_parse_verify - parse VERIFY and WRITE VERIFY commands
> + * @cmd:     (in)  structure that describes the SCSI command to be parsed.
> + * @sectors: (out) Number of logical blocks on the storage medium that will be
> + *           affected by the SCSI command.
> + * @bufflen: (out) Expected length of the SCSI Data-Out buffer.
> + */
> +static sense_reason_t sbc_parse_verify(struct se_cmd *cmd, int *sectors,
> +				       u32 *bufflen)
> +{
> +	struct se_device *dev = cmd->se_dev;
> +	u8 *cdb = cmd->t_task_cdb;
> +	u8 bytchk = (cdb[1] >> 1) & 3;
> +	sense_reason_t ret;
> +
> +	*sectors = transport_get_sectors_10(cdb);
> +	cmd->t_task_lba = transport_lba_32(cdb);
> +
> +	if (sbc_check_dpofua(dev, cmd, cdb))
> +		return TCM_INVALID_CDB_FIELD;
> +
> +	ret = sbc_check_prot(dev, cmd, cdb, *sectors, true);
> +	if (ret)
> +		return ret;
> +
> +	switch (bytchk) {
> +	case 0:
> +		*bufflen = 0;
> +		break;
> +	case 1:
> +		*bufflen = sbc_get_size(cmd, *sectors);
> +		cmd->se_cmd_flags |= SCF_SCSI_DATA_CDB;
> +		break;
> +	default:
> +		pr_err("Unsupported BYTCHK value %d for SCSI opcode %#x\n",
> +		       bytchk, cdb[0]);
> +		return TCM_INVALID_CDB_FIELD;
> +	}
> +	return TCM_NO_SENSE;
> +}
> +
>  sense_reason_t
>  sbc_parse_cdb(struct se_cmd *cmd, struct sbc_ops *ops)
>  {
> @@ -895,7 +936,6 @@ sbc_parse_cdb(struct se_cmd *cmd, struct sbc_ops *ops)
>  		cmd->execute_cmd = sbc_execute_rw;
>  		break;
>  	case WRITE_10:
> -	case WRITE_VERIFY:
>  		sectors = transport_get_sectors_10(cdb);
>  		cmd->t_task_lba = transport_lba_32(cdb);
>  
> @@ -909,6 +949,12 @@ sbc_parse_cdb(struct se_cmd *cmd, struct sbc_ops *ops)
>  		cmd->se_cmd_flags |= SCF_SCSI_DATA_CDB;
>  		cmd->execute_cmd = sbc_execute_rw;
>  		break;
> +	case WRITE_VERIFY:
> +		ret = sbc_parse_verify(cmd, &sectors, &size);
> +		if (ret)
> +			return ret;
> +		cmd->execute_cmd = sbc_execute_rw;
> +		goto check_lba;
>  	case WRITE_12:
>  		sectors = transport_get_sectors_12(cdb);
>  		cmd->t_task_lba = transport_lba_32(cdb);
> @@ -1105,9 +1151,9 @@ sbc_parse_cdb(struct se_cmd *cmd, struct sbc_ops *ops)
>  			return ret;
>  		break;
>  	case VERIFY:
> -		size = 0;
> -		sectors = transport_get_sectors_10(cdb);
> -		cmd->t_task_lba = transport_lba_32(cdb);
> +		ret = sbc_parse_verify(cmd, &sectors, &size);
> +		if (ret)
> +			return ret;
>  		cmd->execute_cmd = sbc_emulate_noop;
>  		goto check_lba;
>  	case REZERO_UNIT:

The patch looks OK to address the warning, but as-is it conflicts with
the bug-fix for VERIFY_16 here:

https://git.kernel.org/pub/scm/linux/kernel/git/nab/target-pending.git/commit/?id=13603685c1f12c67a7a2427f00b63f39a2b6f7c9

Please re-submit atop target-pending/master.

--
To unsubscribe from this list: send the line "unsubscribe target-devel" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

Patch
diff mbox

diff --git a/drivers/target/target_core_sbc.c b/drivers/target/target_core_sbc.c
index 68d8aef7ab78..0f6f98491f5c 100644
--- a/drivers/target/target_core_sbc.c
+++ b/drivers/target/target_core_sbc.c
@@ -828,6 +828,47 @@  sbc_check_dpofua(struct se_device *dev, struct se_cmd *cmd, unsigned char *cdb)
 	return 0;
 }
 
+/**
+ * sbc_parse_verify - parse VERIFY and WRITE VERIFY commands
+ * @cmd:     (in)  structure that describes the SCSI command to be parsed.
+ * @sectors: (out) Number of logical blocks on the storage medium that will be
+ *           affected by the SCSI command.
+ * @bufflen: (out) Expected length of the SCSI Data-Out buffer.
+ */
+static sense_reason_t sbc_parse_verify(struct se_cmd *cmd, int *sectors,
+				       u32 *bufflen)
+{
+	struct se_device *dev = cmd->se_dev;
+	u8 *cdb = cmd->t_task_cdb;
+	u8 bytchk = (cdb[1] >> 1) & 3;
+	sense_reason_t ret;
+
+	*sectors = transport_get_sectors_10(cdb);
+	cmd->t_task_lba = transport_lba_32(cdb);
+
+	if (sbc_check_dpofua(dev, cmd, cdb))
+		return TCM_INVALID_CDB_FIELD;
+
+	ret = sbc_check_prot(dev, cmd, cdb, *sectors, true);
+	if (ret)
+		return ret;
+
+	switch (bytchk) {
+	case 0:
+		*bufflen = 0;
+		break;
+	case 1:
+		*bufflen = sbc_get_size(cmd, *sectors);
+		cmd->se_cmd_flags |= SCF_SCSI_DATA_CDB;
+		break;
+	default:
+		pr_err("Unsupported BYTCHK value %d for SCSI opcode %#x\n",
+		       bytchk, cdb[0]);
+		return TCM_INVALID_CDB_FIELD;
+	}
+	return TCM_NO_SENSE;
+}
+
 sense_reason_t
 sbc_parse_cdb(struct se_cmd *cmd, struct sbc_ops *ops)
 {
@@ -895,7 +936,6 @@  sbc_parse_cdb(struct se_cmd *cmd, struct sbc_ops *ops)
 		cmd->execute_cmd = sbc_execute_rw;
 		break;
 	case WRITE_10:
-	case WRITE_VERIFY:
 		sectors = transport_get_sectors_10(cdb);
 		cmd->t_task_lba = transport_lba_32(cdb);
 
@@ -909,6 +949,12 @@  sbc_parse_cdb(struct se_cmd *cmd, struct sbc_ops *ops)
 		cmd->se_cmd_flags |= SCF_SCSI_DATA_CDB;
 		cmd->execute_cmd = sbc_execute_rw;
 		break;
+	case WRITE_VERIFY:
+		ret = sbc_parse_verify(cmd, &sectors, &size);
+		if (ret)
+			return ret;
+		cmd->execute_cmd = sbc_execute_rw;
+		goto check_lba;
 	case WRITE_12:
 		sectors = transport_get_sectors_12(cdb);
 		cmd->t_task_lba = transport_lba_32(cdb);
@@ -1105,9 +1151,9 @@  sbc_parse_cdb(struct se_cmd *cmd, struct sbc_ops *ops)
 			return ret;
 		break;
 	case VERIFY:
-		size = 0;
-		sectors = transport_get_sectors_10(cdb);
-		cmd->t_task_lba = transport_lba_32(cdb);
+		ret = sbc_parse_verify(cmd, &sectors, &size);
+		if (ret)
+			return ret;
 		cmd->execute_cmd = sbc_emulate_noop;
 		goto check_lba;
 	case REZERO_UNIT: