Files
gotgt/pkg/scsi/target.go
Lei Xue 93e1476a0f feat: implement cmd management for targets, LUNs, and TPGTs (fixes #36)
- Fix target delete URL path mismatch (/targets/ -> /target/)
- Implement target create/delete server handlers with proper validation
- Add DeleteTarget method with force flag and mutex locking to SCSITargetService
- Implement full LU management: create/list/delete through CLI, client, and server
- Add TPGT list command to show target portal group tags
- Add unit tests for target/LU router handlers and SCSI service

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-14 20:30:47 +08:00

141 lines
3.6 KiB
Go

/*
Copyright 2016 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 scsi
import (
"encoding/binary"
"fmt"
"github.com/google/uuid"
"github.com/gostor/gotgt/pkg/api"
log "github.com/sirupsen/logrus"
)
func (s *SCSITargetService) NewSCSITarget(tid int, driverName, name string) (*api.SCSITarget, error) {
s.mutex.Lock()
defer s.mutex.Unlock()
var target = &api.SCSITarget{
Name: name,
TID: tid,
TargetPortGroups: []*api.TargetPortGroup{},
ITNexus: make(map[uuid.UUID]*api.ITNexus),
}
tpg := &api.TargetPortGroup{GroupID: 0, TargetPortGroup: []*api.SCSITargetPort{}}
s.Targets = append(s.Targets, target)
target.Devices = GetTargetLUNMap(target.Name)
target.LUN0 = NewLUN0()
target.TargetPortGroups = append(target.TargetPortGroups, tpg)
return target, nil
}
// DeleteTarget removes a target by name. If force is false and there are active sessions, it returns an error.
func (s *SCSITargetService) DeleteTarget(name string, force bool) error {
s.mutex.Lock()
defer s.mutex.Unlock()
for i, t := range s.Targets {
if t.Name == name {
if !force && len(t.ITNexus) > 0 {
return fmt.Errorf("target %s has %d active sessions, use force to remove", name, len(t.ITNexus))
}
s.Targets = append(s.Targets[:i], s.Targets[i+1:]...)
DelTargetLUNMap(name)
return nil
}
}
return fmt.Errorf("target %q not found", name)
}
func (s *SCSITargetService) RereadTargetLUNMap() {
s.mutex.Lock()
defer s.mutex.Unlock()
for _, tgt := range s.Targets {
tgt.Devices = GetTargetLUNMap(tgt.Name)
}
}
func FindTargetGroup(target *api.SCSITarget, relPortID uint16) uint16 {
for _, tpg := range target.TargetPortGroups {
for _, port := range tpg.TargetPortGroup {
if port.RelativeTargetPortID == relPortID {
return tpg.GroupID
}
}
}
return 0
}
func FindTargetPort(target *api.SCSITarget, relPortID uint16) *api.SCSITargetPort {
for _, tpg := range target.TargetPortGroups {
for _, port := range tpg.TargetPortGroup {
if port.RelativeTargetPortID == relPortID {
return port
}
}
}
return nil
}
func AddITNexus(target *api.SCSITarget, itnexus *api.ITNexus) bool {
var ret bool = true
target.ITNexusMutex.Lock()
defer target.ITNexusMutex.Unlock()
if _, ok := target.ITNexus[itnexus.ID]; !ok {
target.ITNexus[itnexus.ID] = itnexus
ret = true
} else {
ret = false
}
return ret
}
func RemoveITNexus(target *api.SCSITarget, itnexus *api.ITNexus) {
target.ITNexusMutex.Lock()
defer target.ITNexusMutex.Unlock()
delete(target.ITNexus, itnexus.ID)
}
func deviceReserve(cmd *api.SCSICommand) error {
var lu *api.SCSILu
lun := binary.LittleEndian.Uint64(cmd.Lun[:])
for tgtLUN, lunDev := range cmd.Target.Devices {
if tgtLUN == lun {
lu = lunDev
break
}
}
if lu == nil {
log.Errorf("invalid target and lun %d %d", cmd.Target.TID, lun)
return nil
}
if lu.ReserveID != uuid.Nil && lu.ReserveID == cmd.ITNexusID {
log.Errorf("already reserved %d, %d", lu.ReserveID, cmd.ITNexusID)
return fmt.Errorf("already reserved")
}
lu.ReserveID = cmd.ITNexusID
return nil
}
func deviceRelease(tid int, itn uuid.UUID, lun uint64, force bool) error {
// TODO
return nil
}