diff --git a/scst/src/dev_handlers/scst_vdisk.c b/scst/src/dev_handlers/scst_vdisk.c index ccf79030e..ee031d3a2 100644 --- a/scst/src/dev_handlers/scst_vdisk.c +++ b/scst/src/dev_handlers/scst_vdisk.c @@ -3201,10 +3201,10 @@ static enum compl_status_e vdisk_exec_write_same(struct vdisk_cmd_params *p) TRACE_ENTRY(); - if (unlikely(cmd->cdb[1] & 1)) { + if (unlikely(cmd->cdb[1] & 0x10)) { TRACE_DBG("%s", "ANCHOR not supported"); scst_set_invalid_field_in_cdb(cmd, 1, - SCST_INVAL_FIELD_BIT_OFFS_VALID | 0); + SCST_INVAL_FIELD_BIT_OFFS_VALID | 4); goto out; } @@ -3216,6 +3216,8 @@ static enum compl_status_e vdisk_exec_write_same(struct vdisk_cmd_params *p) } if (unlikely(cmd->data_len <= 0)) { + PRINT_ERROR("WRITE SAME: refused data_len = %#llx", + cmd->data_len); scst_set_invalid_field_in_cdb(cmd, cmd->len_off, 0); goto out; } diff --git a/scst/src/scst_lib.c b/scst/src/scst_lib.c index 1b17d3828..9c08164aa 100644 --- a/scst/src/scst_lib.c +++ b/scst/src/scst_lib.c @@ -7550,22 +7550,47 @@ static int get_cdb_info_lba_8_len_4(struct scst_cmd *cmd, return 0; } +static int get_cdb_info_write_same(struct scst_cmd *cmd, + const struct scst_sdbops *sdbops, + const bool ndob) +{ + const bool anchor = (cmd->cdb[1] >> 4) & 1; + const bool unmap = (cmd->cdb[1] >> 3) & 1; + + if (!unmap && (anchor || ndob)) { + PRINT_ERROR("Received invalid %s command (UNMAP = %d;" + " ANCHOR = %d; NDOB = %d)", + scst_get_opcode_name(cmd), unmap, anchor, ndob); + scst_set_invalid_field_in_cdb(cmd, 1, + SCST_INVAL_FIELD_BIT_OFFS_VALID | (ndob ? 0 : 4)); + return 1; + } + + if (ndob) { + cmd->bufflen = 0; + cmd->data_direction = SCST_DATA_NONE; + } else { + cmd->bufflen = 1; + cmd->data_direction = SCST_DATA_WRITE; + } + + return 0; +} + static int get_cdb_info_write_same10(struct scst_cmd *cmd, const struct scst_sdbops *sdbops) { cmd->lba = get_unaligned_be32(cmd->cdb + sdbops->info_lba_off); - cmd->bufflen = 1; cmd->data_len = get_unaligned_be16(cmd->cdb + sdbops->info_len_off); - return 0; + return get_cdb_info_write_same(cmd, sdbops, false); } static int get_cdb_info_write_same16(struct scst_cmd *cmd, const struct scst_sdbops *sdbops) { cmd->lba = get_unaligned_be64(cmd->cdb + sdbops->info_lba_off); - cmd->bufflen = 1; cmd->data_len = get_unaligned_be32(cmd->cdb + sdbops->info_len_off); - return 0; + return get_cdb_info_write_same(cmd, sdbops, cmd->cdb[1] & 1 /*NDOB*/); } static int get_cdb_info_compare_and_write(struct scst_cmd *cmd,