fix several bugs during read/write operations

This commit is contained in:
Lei Xue
2016-09-25 18:44:34 +08:00
parent 06c70e605f
commit d6108f0a88
5 changed files with 97 additions and 32 deletions

View File

@@ -185,7 +185,7 @@ func parseHeader(data []byte) (*ISCSICommand, error) {
m.TaskTag = uint32(ParseUint(data[16:20])) m.TaskTag = uint32(ParseUint(data[16:20]))
switch m.OpCode { switch m.OpCode {
case OpSCSICmd, OpSCSITaskReq: case OpSCSICmd, OpSCSITaskReq:
m.LUN = [8]uint8{data[9]} m.LUN = [8]byte{data[9]}
m.ExpectedDataLen = uint32(ParseUint(data[20:24])) m.ExpectedDataLen = uint32(ParseUint(data[20:24]))
m.CmdSN = uint32(ParseUint(data[24:28])) m.CmdSN = uint32(ParseUint(data[24:28]))
m.Read = data[1]&0x40 == 0x40 m.Read = data[1]&0x40 == 0x40
@@ -194,7 +194,7 @@ func parseHeader(data []byte) (*ISCSICommand, error) {
m.ExpStatSN = uint32(ParseUint(data[28:32])) m.ExpStatSN = uint32(ParseUint(data[28:32]))
case OpSCSIResp: case OpSCSIResp:
case OpSCSIOut: case OpSCSIOut:
m.LUN = [8]uint8{data[9]} m.LUN = [8]byte{data[9]}
m.ExpStatSN = uint32(ParseUint(data[28:32])) m.ExpStatSN = uint32(ParseUint(data[28:32]))
m.DataSN = uint32(ParseUint(data[36:40])) m.DataSN = uint32(ParseUint(data[36:40]))
m.BufferOffset = uint32(ParseUint(data[40:44])) m.BufferOffset = uint32(ParseUint(data[40:44]))

View File

@@ -243,6 +243,17 @@ func (s *ISCSITargetService) iscsiExecLogin(conn *iscsiConnection) error {
RawData: util.MarshalKVText([]util.KeyValue{ RawData: util.MarshalKVText([]util.KeyValue{
{"HeaderDigest", "None"}, {"HeaderDigest", "None"},
{"DataDigest", "None"}, {"DataDigest", "None"},
{"ImmediateData", "Yes"},
{"InitialR2T", "Yes"},
{"MaxBurstLength", "262144"},
{"FirstBurstLength", "65536"},
{"DefaultTime2Wait", "2"},
{"DefaultTime2Retain", "0"},
{"MaxOutstandingR2T", "1"},
{"IFMarker", "No"},
{"OFMarker", "No"},
{"DataPDUInOrder", "Yes"},
{"DataSequenceInOrder", "Yes"},
}), }),
} }
pairs := util.ParseKVText(cmd.RawData) pairs := util.ParseKVText(cmd.RawData)
@@ -332,7 +343,7 @@ func (s *ISCSITargetService) iscsiExecText(conn *iscsiConnection) error {
} }
for _, t := range list { for _, t := range list {
result = append(result, util.KeyValue{"TargetName", t.Name}) result = append(result, util.KeyValue{"TargetName", t.Name})
result = append(result, util.KeyValue{"TargetAddress", "172.16.69.169:3260,1"}) result = append(result, util.KeyValue{"TargetAddress", "172.16.69.1:3260,1"})
//result = append(result, util.KeyValue{"TargetAddress", "127.0.0.1:3260,1"}) //result = append(result, util.KeyValue{"TargetAddress", "127.0.0.1:3260,1"})
} }
} }
@@ -495,6 +506,7 @@ func (s *ISCSITargetService) scsiCommandHandler(conn *iscsiConnection) (err erro
} }
task.scmd.OutSDBBuffer.Buffer.Write(conn.req.RawData) task.scmd.OutSDBBuffer.Buffer.Write(conn.req.RawData)
if task.r2tCount > 0 { if task.r2tCount > 0 {
conn.session.ExpCmdSN += 1
// prepare to receive more data // prepare to receive more data
task.state = taskPending task.state = taskPending
conn.session.PendingTasks.Push(task) conn.session.PendingTasks.Push(task)
@@ -703,6 +715,7 @@ func (s *ISCSITargetService) iscsiTaskQueueHandler(task *iscsiTask) error {
// add this connection into queue and set this task as pending task // add this connection into queue and set this task as pending task
task.state = taskPending task.state = taskPending
sess.PendingTasks.Push(task) sess.PendingTasks.Push(task)
return fmt.Errorf("pending")
} }
return nil return nil

View File

@@ -48,6 +48,7 @@ func NewSCSILu(lun uint64, target *api.SCSITarget) (*api.SCSILu, error) {
lu.File = f lu.File = f
lu.DeviceProtocol.InitLu(lu) lu.DeviceProtocol.InitLu(lu)
lu.Attrs.Online = true lu.Attrs.Online = true
lu.Attrs.Lbppbe = 3
return lu, nil return lu, nil
} }

View File

@@ -477,11 +477,17 @@ sense:
} }
func SBCReadCapacity16(host int, cmd *api.SCSICommand) api.SAMStat { func SBCReadCapacity16(host int, cmd *api.SCSICommand) api.SAMStat {
data := &bytes.Buffer{} var (
data.Write(util.MarshalUint64(1)) data = &bytes.Buffer{}
data.Write(util.MarshalUint32(1 << cmd.Device.BlockShift)) bshift = cmd.Device.BlockShift
size = cmd.Device.Size >> bshift
)
data.Write(util.MarshalUint64(uint64(size - 1)))
binary.Write(data, binary.BigEndian, uint32(1<<bshift))
val := (cmd.Device.Attrs.Lbppbe << 16) | cmd.Device.Attrs.LowestAlignedLBA val := (cmd.Device.Attrs.Lbppbe << 16) | cmd.Device.Attrs.LowestAlignedLBA
data.Write(util.MarshalUint32(uint32(val))) data.Write(util.MarshalUint32(uint32(val)))
binary.Write(data, binary.BigEndian, uint64(0))
binary.Write(data, binary.BigEndian, uint64(0))
cmd.InSDBBuffer.Buffer = data cmd.InSDBBuffer.Buffer = data
return api.SAMStatGood return api.SAMStatGood
} }

View File

@@ -1,5 +1,5 @@
/* /*
Copyright 2015 The GoStor Authors All rights reserved. Copyright 2016 The GoStor Authors All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License"); Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License. you may not use this file except in compliance with the License.
@@ -22,6 +22,7 @@ import (
"encoding/binary" "encoding/binary"
"fmt" "fmt"
"reflect" "reflect"
"unsafe"
"github.com/golang/glog" "github.com/golang/glog"
"github.com/gostor/gotgt/pkg/api" "github.com/gostor/gotgt/pkg/api"
@@ -130,40 +131,84 @@ func SPCLuOnline(lu *api.SCSILu) error {
} }
func SPCInquiry(host int, cmd *api.SCSICommand) api.SAMStat { func SPCInquiry(host int, cmd *api.SCSICommand) api.SAMStat {
buf := &bytes.Buffer{} var (
var b byte = 0x75 buf = &bytes.Buffer{}
data []byte = []byte{}
b byte = 0x75
scb []byte = cmd.SCB.Bytes()
pcode byte = scb[2]
evpd bool = false
)
if scb[1]&0x01 > 0 {
evpd = true
}
if reflect.DeepEqual(util.MarshalUint64(cmd.Device.Lun)[0:7], cmd.Lun[0:7]) { if reflect.DeepEqual(util.MarshalUint64(cmd.Device.Lun)[0:7], cmd.Lun[0:7]) {
b = (uint8(0) & 0x7) << 5 b = (uint8(0) & 0x7) << 5
b |= uint8(0) & 0x1f b |= uint8(0) & 0x1f
} }
buf.WriteByte(b) if cmd.Device.Lun != *(*uint64)(unsafe.Pointer(&cmd.Lun)) {
b = 0 goto sense
buf.WriteByte(b) }
buf.WriteByte(byte(1)) if evpd {
b = 0x02 if pcode == 0x0 {
buf.WriteByte(b) buf.WriteByte(b)
buf.WriteByte(0x00) b = 0
// byte 5 buf.WriteByte(b)
b = 0 buf.WriteByte(b)
b |= byte(1) << 4 & 0x30 buf.WriteByte(b)
buf.WriteByte(b) buf.WriteByte(b)
// byte 6 buf.WriteByte(b)
b = 0 } else if pcode == 0xb0 {
buf.WriteByte(b) buf.WriteByte(b)
buf.WriteByte(0x02) buf.WriteByte(0xb0)
buf.Write([]byte{'1', '1', 'c', 'a', 'n', 's'}) buf.WriteByte(0x00)
buf.WriteByte(0x00) buf.WriteByte(0x3c)
buf.WriteByte(0x00) buf.WriteByte(0x00)
buf.Write([]byte{'c', 'o', 'f', 'f', 'e', 'e'}) buf.WriteByte(0x80)
for i := 0; i < 10; i++ {
for i := 0; i < 58; i++ {
buf.WriteByte(0x00)
}
} else {
buf.WriteByte(b)
buf.WriteByte(0xb0)
buf.WriteByte(0x00)
buf.WriteByte(0x00)
buf.WriteByte(0x00)
}
} else {
buf.WriteByte(b)
b = 0
buf.WriteByte(b)
buf.WriteByte(byte(1))
b = 0x02
buf.WriteByte(b)
buf.WriteByte(0x00)
// byte 5
b = 0
b |= byte(1) << 4 & 0x30
buf.WriteByte(b)
// byte 6
b = 0
buf.WriteByte(b)
buf.WriteByte(0x02)
buf.Write([]byte{'1', '1', 'c', 'a', 'n', 's'})
buf.WriteByte(0x00)
buf.WriteByte(0x00)
buf.Write([]byte{'c', 'o', 'f', 'f', 'e', 'e'})
for i := 0; i < 10; i++ {
buf.WriteByte(0x00)
}
buf.Write([]byte{'1', '.', '0'})
buf.WriteByte(0x00) buf.WriteByte(0x00)
} }
buf.Write([]byte{'1', '.', '0'}) data = buf.Bytes()
buf.WriteByte(0x00)
data := buf.Bytes()
data[4] = byte(len(data) - 4) data[4] = byte(len(data) - 4)
cmd.InSDBBuffer.Buffer = bytes.NewBuffer(data) cmd.InSDBBuffer.Buffer = bytes.NewBuffer(data)
return api.SAMStatGood return api.SAMStatGood
sense:
BuildSenseData(cmd, ILLEGAL_REQUEST, ASC_INVALID_FIELD_IN_CDB)
return api.SAMStatCheckCondition
} }
func SPCReportLuns(host int, cmd *api.SCSICommand) api.SAMStat { func SPCReportLuns(host int, cmd *api.SCSICommand) api.SAMStat {