Skip to content

Commit 7062995

Browse files
rolanddgregkh
authored andcommitted
target: Report correct response length for some commands
commit 2426bd4 upstream. When an initiator sends an allocation length bigger than what its command consumes, the target should only return the actual response data and set the residual length to the unused part of the allocation length. Add a helper function that command handlers (INQUIRY, READ CAPACITY, etc) can use to do this correctly, and use this code to get the correct residual for commands that don't use the full initiator allocation in the handlers for READ CAPACITY, READ CAPACITY(16), INQUIRY, MODE SENSE and REPORT LUNS. This addresses a handful of failures as reported by Christophe with the Windows Certification Kit: http://permalink.gmane.org/gmane.linux.scsi.target.devel/6515 Signed-off-by: Roland Dreier <roland@purestorage.com> Tested-by: Christophe Vu-Brugier <cvubrugier@yahoo.fr> Signed-off-by: Nicholas Bellinger <nab@linux-iscsi.org> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent cc74a0e commit 7062995

4 files changed

Lines changed: 26 additions & 5 deletions

File tree

‎drivers/target/target_core_sbc.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ sbc_emulate_readcapacity(struct se_cmd *cmd)
8181
transport_kunmap_data_sg(cmd);
8282
}
8383

84-
target_complete_cmd(cmd, GOOD);
84+
target_complete_cmd_with_length(cmd, GOOD, 8);
8585
return 0;
8686
}
8787

@@ -137,7 +137,7 @@ sbc_emulate_readcapacity_16(struct se_cmd *cmd)
137137
transport_kunmap_data_sg(cmd);
138138
}
139139

140-
target_complete_cmd(cmd, GOOD);
140+
target_complete_cmd_with_length(cmd, GOOD, 32);
141141
return 0;
142142
}
143143

‎drivers/target/target_core_spc.c‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -721,6 +721,7 @@ spc_emulate_inquiry(struct se_cmd *cmd)
721721
unsigned char *buf;
722722
sense_reason_t ret;
723723
int p;
724+
int len = 0;
724725

725726
buf = kzalloc(SE_INQUIRY_BUF, GFP_KERNEL);
726727
if (!buf) {
@@ -742,13 +743,15 @@ spc_emulate_inquiry(struct se_cmd *cmd)
742743
}
743744

744745
ret = spc_emulate_inquiry_std(cmd, buf);
746+
len = buf[4] + 5;
745747
goto out;
746748
}
747749

748750
for (p = 0; p < ARRAY_SIZE(evpd_handlers); ++p) {
749751
if (cdb[2] == evpd_handlers[p].page) {
750752
buf[1] = cdb[2];
751753
ret = evpd_handlers[p].emulate(cmd, buf);
754+
len = get_unaligned_be16(&buf[2]) + 4;
752755
goto out;
753756
}
754757
}
@@ -765,7 +768,7 @@ spc_emulate_inquiry(struct se_cmd *cmd)
765768
kfree(buf);
766769

767770
if (!ret)
768-
target_complete_cmd(cmd, GOOD);
771+
target_complete_cmd_with_length(cmd, GOOD, len);
769772
return ret;
770773
}
771774

@@ -1103,7 +1106,7 @@ static sense_reason_t spc_emulate_modesense(struct se_cmd *cmd)
11031106
transport_kunmap_data_sg(cmd);
11041107
}
11051108

1106-
target_complete_cmd(cmd, GOOD);
1109+
target_complete_cmd_with_length(cmd, GOOD, length);
11071110
return 0;
11081111
}
11091112

@@ -1279,7 +1282,7 @@ sense_reason_t spc_emulate_report_luns(struct se_cmd *cmd)
12791282
buf[3] = (lun_count & 0xff);
12801283
transport_kunmap_data_sg(cmd);
12811284

1282-
target_complete_cmd(cmd, GOOD);
1285+
target_complete_cmd_with_length(cmd, GOOD, 8 + lun_count * 8);
12831286
return 0;
12841287
}
12851288
EXPORT_SYMBOL(spc_emulate_report_luns);

‎drivers/target/target_core_transport.c‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -703,6 +703,23 @@ void target_complete_cmd(struct se_cmd *cmd, u8 scsi_status)
703703
}
704704
EXPORT_SYMBOL(target_complete_cmd);
705705

706+
void target_complete_cmd_with_length(struct se_cmd *cmd, u8 scsi_status, int length)
707+
{
708+
if (scsi_status == SAM_STAT_GOOD && length < cmd->data_length) {
709+
if (cmd->se_cmd_flags & SCF_UNDERFLOW_BIT) {
710+
cmd->residual_count += cmd->data_length - length;
711+
} else {
712+
cmd->se_cmd_flags |= SCF_UNDERFLOW_BIT;
713+
cmd->residual_count = cmd->data_length - length;
714+
}
715+
716+
cmd->data_length = length;
717+
}
718+
719+
target_complete_cmd(cmd, scsi_status);
720+
}
721+
EXPORT_SYMBOL(target_complete_cmd_with_length);
722+
706723
static void target_add_to_state_list(struct se_cmd *cmd)
707724
{
708725
struct se_device *dev = cmd->se_dev;

‎include/target/target_core_backend.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ int transport_subsystem_register(struct se_subsystem_api *);
5959
void transport_subsystem_release(struct se_subsystem_api *);
6060

6161
void target_complete_cmd(struct se_cmd *, u8);
62+
void target_complete_cmd_with_length(struct se_cmd *, u8, int);
6263

6364
sense_reason_t spc_parse_cdb(struct se_cmd *cmd, unsigned int *size);
6465
sense_reason_t spc_emulate_report_luns(struct se_cmd *cmd);

0 commit comments

Comments
 (0)