From 12a648b5f3accbfab5c295fbdbfa9353a49352e9 Mon Sep 17 00:00:00 2001 From: Lei Xue Date: Thu, 15 Jun 2017 08:16:43 +0800 Subject: [PATCH] fix sense data issue --- pkg/port/iscsit/cmd.go | 20 +++++++++++++++++++- pkg/port/iscsit/conn.go | 11 ++++++----- pkg/port/iscsit/iscsid.go | 10 +++++++--- pkg/scsi/scsi.go | 9 ++++----- 4 files changed, 36 insertions(+), 14 deletions(-) diff --git a/pkg/port/iscsit/cmd.go b/pkg/port/iscsit/cmd.go index 2778c38..c02659a 100644 --- a/pkg/port/iscsit/cmd.go +++ b/pkg/port/iscsit/cmd.go @@ -1,3 +1,19 @@ +/* +Copyright 2017 The GoStor Authors All rights reserved. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + package iscsit import ( @@ -260,8 +276,10 @@ func (m *ISCSICommand) scsiCmdRespBytes() []byte { buf.WriteByte(byte(m.SCSIResponse)) buf.WriteByte(byte(m.Status)) + buf.WriteByte(0x00) + buf.Write(util.MarshalUint64(uint64(len(m.RawData)))[5:]) // 5-8 // Skip through to byte 16 - for i := 0; i < 3*4; i++ { + for i := 0; i < 8; i++ { buf.WriteByte(0x00) } buf.Write(util.MarshalUint64(uint64(m.TaskTag))[4:]) diff --git a/pkg/port/iscsit/conn.go b/pkg/port/iscsit/conn.go index 44942ab..f2d84b7 100644 --- a/pkg/port/iscsit/conn.go +++ b/pkg/port/iscsit/conn.go @@ -1,5 +1,5 @@ /* -Copyright 2016 The GoStor Authors All rights reserved. +Copyright 2017 The GoStor Authors All rights reserved. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. @@ -174,7 +174,10 @@ func (conn *iscsiConnection) buildRespPackage(oc OpCode, task *iscsiTask) error resp.HasStatus = true scmd := task.scmd resp.Status = scmd.Result - if scmd.Direction == api.SCSIDataRead || scmd.Direction == api.SCSIDataWrite { + if scmd.Result != 0 && scmd.SenseBuffer != nil { + length := util.MarshalUint32(uint32(scmd.SenseLength)) + resp.RawData = append(length[2:4], scmd.SenseBuffer.Bytes()...) + } else if scmd.Direction == api.SCSIDataRead || scmd.Direction == api.SCSIDataWrite { if scmd.InSDBBuffer.Buffer != nil { buf := scmd.InSDBBuffer.Buffer.Bytes() resp.RawData = buf @@ -182,9 +185,7 @@ func (conn *iscsiConnection) buildRespPackage(oc OpCode, task *iscsiTask) error resp.RawData = []byte{} } } - if scmd.Result != 0 && scmd.SenseBuffer != nil { - resp.RawData = scmd.SenseBuffer.Bytes() - } + case OpNoopIn, OpReject: resp.OpCode = oc resp.Final = true diff --git a/pkg/port/iscsit/iscsid.go b/pkg/port/iscsit/iscsid.go index ed988b9..524ab28 100644 --- a/pkg/port/iscsit/iscsid.go +++ b/pkg/port/iscsit/iscsid.go @@ -549,7 +549,7 @@ func (s *ISCSITargetDriver) scsiCommandHandler(conn *iscsiConnection) (err error if err = s.iscsiTaskQueueHandler(task); err != nil { return } else { - if scmd.Direction == api.SCSIDataRead { + if scmd.Direction == api.SCSIDataRead && scmd.SenseBuffer == nil { conn.buildRespPackage(OpSCSIIn, task) } else { conn.buildRespPackage(OpSCSIResp, task) @@ -667,7 +667,7 @@ func (s *ISCSITargetDriver) iscsiTaskQueueHandler(task *iscsiTask) error { return err } log.Debugf("add task(%d) into task queue", task.cmd.CmdSN) - // add this connection into queue and set this task as pending task + // add this task into queue and set it as a pending task sess.PendingTasksMutex.Lock() task.state = taskPending sess.PendingTasks.Push(task) @@ -732,7 +732,11 @@ func (s *ISCSITargetDriver) iscsiExecTask(task *iscsiTask) error { } else { // abort this task log.Debugf("abort the task[%v]", stask.tag) - stask.scmd.Result = api.SAM_STAT_TASK_ABORTED + if stask.scmd == nil { + stask.scmd = &api.SCSICommand{Result: api.SAM_STAT_TASK_ABORTED} + } + stask.conn = task.conn + log.Debugf("stask.conn: %#v", stask.conn) stask.conn.buildRespPackage(OpSCSIResp, stask) stask.conn.rxTask = nil s.handler(DATAOUT, stask.conn) diff --git a/pkg/scsi/scsi.go b/pkg/scsi/scsi.go index e3c5199..3aa31e7 100644 --- a/pkg/scsi/scsi.go +++ b/pkg/scsi/scsi.go @@ -1,5 +1,5 @@ /* -Copyright 2016 The GoStor Authors All rights reserved. +Copyright 2017 The GoStor Authors All rights reserved. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. @@ -151,10 +151,9 @@ func BuildSenseData(cmd *api.SCSICommand, key byte, asc SCSISubError) { } senseBuffer.WriteByte((byte(asc) >> 8) & 0xff) senseBuffer.WriteByte(byte(asc) & 0xff) - senseBuffer.WriteByte(0x00) - senseBuffer.WriteByte(0x00) - senseBuffer.WriteByte(0x00) - senseBuffer.WriteByte(0x00) + for i := 0; i < 4; i++ { + senseBuffer.WriteByte(0x00) + } cmd.SenseLength = length + 8 } if ok {