Skip to content

Commit 2426bd4

Browse files
rolanddNicholas Bellinger
authored andcommitted
target: Report correct response length for some commands
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> Cc: stable@vger.kernel.org # 3.10+ Signed-off-by: Nicholas Bellinger <nab@linux-iscsi.org>
1 parent c52716d commit 2426bd4

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
@@ -716,6 +716,7 @@ spc_emulate_inquiry(struct se_cmd *cmd)
716716
unsigned char *buf;
717717
sense_reason_t ret;
718718
int p;
719+
int len = 0;
719720

720721
buf = kzalloc(SE_INQUIRY_BUF, GFP_KERNEL);
721722
if (!buf) {
@@ -737,13 +738,15 @@ spc_emulate_inquiry(struct se_cmd *cmd)
737738
}
738739

739740
ret = spc_emulate_inquiry_std(cmd, buf);
741+
len = buf[4] + 5;
740742
goto out;
741743
}
742744

743745
for (p = 0; p < ARRAY_SIZE(evpd_handlers); ++p) {
744746
if (cdb[2] == evpd_handlers[p].page) {
745747
buf[1] = cdb[2];
746748
ret = evpd_handlers[p].emulate(cmd, buf);
749+
len = get_unaligned_be16(&buf[2]) + 4;
747750
goto out;
748751
}
749752
}
@@ -760,7 +763,7 @@ spc_emulate_inquiry(struct se_cmd *cmd)
760763
kfree(buf);
761764

762765
if (!ret)
763-
target_complete_cmd(cmd, GOOD);
766+
target_complete_cmd_with_length(cmd, GOOD, len);
764767
return ret;
765768
}
766769

@@ -1098,7 +1101,7 @@ static sense_reason_t spc_emulate_modesense(struct se_cmd *cmd)
10981101
transport_kunmap_data_sg(cmd);
10991102
}
11001103

1101-
target_complete_cmd(cmd, GOOD);
1104+
target_complete_cmd_with_length(cmd, GOOD, length);
11021105
return 0;
11031106
}
11041107

@@ -1274,7 +1277,7 @@ sense_reason_t spc_emulate_report_luns(struct se_cmd *cmd)
12741277
buf[3] = (lun_count & 0xff);
12751278
transport_kunmap_data_sg(cmd);
12761279

1277-
target_complete_cmd(cmd, GOOD);
1280+
target_complete_cmd_with_length(cmd, GOOD, 8 + lun_count * 8);
12781281
return 0;
12791282
}
12801283
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)