Skip to content

Commit 3359227

Browse files
wenchao-haomartinkpetersen
authored andcommitted
scsi: scsi_debug: Set command result and sense data if error is injected
If a fail command error is injected, set the command's status and sense data then finish this SCSI command. Set SCSI command's status and sense data format: +--------+------+-------------------------------------------------------+ | Column | Type | Description | +--------+------+-------------------------------------------------------+ | 1 | u8 | Error type, fixed to 0x2 | +--------+------+-------------------------------------------------------+ | 2 | s32 | Error Count | | | | 0: the rule will be ignored | | | | positive: the rule will always take effect | | | | negative: the rule takes effect n times where -n is | | | | the value given. Ignored after n times | +--------+------+-------------------------------------------------------+ | 3 | x8 | SCSI command opcode, 0xff for all commands | +--------+------+-------------------------------------------------------+ | 4 | x8 | Host byte in scsi_cmd::status | | | | [scsi_cmd::status has 32 bits holding these 3 bytes] | +--------+------+-------------------------------------------------------+ | 5 | x8 | Driver byte in scsi_cmd::status | +--------+------+-------------------------------------------------------+ | 6 | x8 | SCSI Status byte in scsi_cmd::status | +--------+------+-------------------------------------------------------+ | 7 | x8 | SCSI Sense Key in scsi_cmnd | +--------+------+-------------------------------------------------------+ | 8 | x8 | SCSI ASC in scsi_cmnd | +--------+------+-------------------------------------------------------+ | 9 | x8 | SCSI ASCQ in scsi_cmnd | +--------+------+-------------------------------------------------------+ Examples: error=/sys/kernel/debug/scsi_debug/0:0:0:1/error echo "2 -10 0x88 0 0 0x2 0x3 0x11 0x0" >${error} will make device's read command return with media error with additional sense of "Unrecovered read error" (UNC): Acked-by: Douglas Gilbert <[email protected]> Signed-off-by: Wenchao Hao <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Martin K. Petersen <[email protected]>
1 parent 33bccf5 commit 3359227

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed

drivers/scsi/scsi_debug.c

+53
Original file line numberDiff line numberDiff line change
@@ -7797,6 +7797,48 @@ static int sdebug_fail_queue_cmd(struct scsi_cmnd *cmnd)
77977797
return 0;
77987798
}
77997799

7800+
static int sdebug_fail_cmd(struct scsi_cmnd *cmnd, int *retval,
7801+
struct sdebug_err_inject *info)
7802+
{
7803+
struct scsi_device *sdp = cmnd->device;
7804+
struct sdebug_dev_info *devip = (struct sdebug_dev_info *)sdp->hostdata;
7805+
struct sdebug_err_inject *err;
7806+
unsigned char *cmd = cmnd->cmnd;
7807+
int ret = 0;
7808+
int result;
7809+
7810+
if (devip == NULL)
7811+
return 0;
7812+
7813+
rcu_read_lock();
7814+
list_for_each_entry_rcu(err, &devip->inject_err_list, list) {
7815+
if (err->type == ERR_FAIL_CMD &&
7816+
(err->cmd == cmd[0] || err->cmd == 0xff)) {
7817+
if (!err->cnt) {
7818+
rcu_read_unlock();
7819+
return 0;
7820+
}
7821+
7822+
ret = !!err->cnt;
7823+
rcu_read_unlock();
7824+
goto out_handle;
7825+
}
7826+
}
7827+
rcu_read_unlock();
7828+
7829+
return 0;
7830+
7831+
out_handle:
7832+
if (err->cnt < 0)
7833+
err->cnt++;
7834+
mk_sense_buffer(cmnd, err->sense_key, err->asc, err->asq);
7835+
result = err->status_byte | err->host_byte << 16 | err->driver_byte << 24;
7836+
*info = *err;
7837+
*retval = schedule_resp(cmnd, devip, result, NULL, 0, 0);
7838+
7839+
return ret;
7840+
}
7841+
78007842
static int scsi_debug_queuecommand(struct Scsi_Host *shost,
78017843
struct scsi_cmnd *scp)
78027844
{
@@ -7817,6 +7859,7 @@ static int scsi_debug_queuecommand(struct Scsi_Host *shost,
78177859
bool has_wlun_rl;
78187860
bool inject_now;
78197861
int ret = 0;
7862+
struct sdebug_err_inject err;
78207863

78217864
scsi_set_resid(scp, 0);
78227865
if (sdebug_statistics) {
@@ -7869,6 +7912,16 @@ static int scsi_debug_queuecommand(struct Scsi_Host *shost,
78697912
return ret;
78707913
}
78717914

7915+
if (sdebug_fail_cmd(scp, &ret, &err)) {
7916+
scmd_printk(KERN_INFO, scp,
7917+
"fail command 0x%x with hostbyte=0x%x, "
7918+
"driverbyte=0x%x, statusbyte=0x%x, "
7919+
"sense_key=0x%x, asc=0x%x, asq=0x%x\n",
7920+
opcode, err.host_byte, err.driver_byte,
7921+
err.status_byte, err.sense_key, err.asc, err.asq);
7922+
return ret;
7923+
}
7924+
78727925
if (unlikely(inject_now && !atomic_read(&sdeb_inject_pending)))
78737926
atomic_set(&sdeb_inject_pending, 1);
78747927

0 commit comments

Comments
 (0)