Skip to content

Commit 4ed2197

Browse files
Consider the correct target id for FCP LUN scan
1 parent b299be8 commit 4ed2197

File tree

10 files changed

+170
-92
lines changed

10 files changed

+170
-92
lines changed

mocks/mock_utils/mock_devices/mock_devices_client.go

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

utils/adaptors.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,12 @@ func GetDeviceInfoForFCPLUN(
2525
}
2626

2727
return &models.ScsiDeviceInfo{
28-
Host: deviceInfo.Host,
29-
Channel: deviceInfo.Channel,
30-
Target: deviceInfo.Target,
31-
LUN: deviceInfo.LUN,
28+
ScsiDeviceAddress: models.ScsiDeviceAddress{
29+
Host: deviceInfo.Host,
30+
Channel: deviceInfo.Channel,
31+
Target: deviceInfo.Target,
32+
LUN: deviceInfo.LUN,
33+
},
3234
Devices: deviceInfo.Devices,
3335
MultipathDevice: deviceInfo.MultipathDevice,
3436
WWNN: deviceInfo.WWNN,

utils/devices/devices.go

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"fmt"
1313
"os"
1414
"path/filepath"
15+
"strconv"
1516
"strings"
1617
"time"
1718

@@ -66,7 +67,7 @@ type Devices interface {
6667
GetLunSerial(ctx context.Context, path string) (string, error)
6768
GetMultipathDeviceUUID(multipathDevicePath string) (string, error)
6869
GetLUKSDeviceForMultipathDevice(multipathDevice string) (string, error)
69-
ScanTargetLUN(ctx context.Context, lunID int, hosts []int) error
70+
ScanTargetLUN(ctx context.Context, deviceAddresses []models.ScsiDeviceAddress) error
7071
CloseLUKSDevice(ctx context.Context, devicePath string) error
7172
EnsureLUKSDeviceClosedWithMaxWaitLimit(ctx context.Context, luksDevicePath string) error
7273
EnsureLUKSDeviceClosed(ctx context.Context, devicePath string) error
@@ -812,8 +813,8 @@ func (c *Client) GetLUKSDeviceForMultipathDevice(multipathDevice string) (string
812813

813814
// ScanTargetLUN scans a single LUN or all the LUNs on an iSCSI target to discover it.
814815
// If all the LUNs are to be scanned please pass -1 for lunID.
815-
func (c *Client) ScanTargetLUN(ctx context.Context, lunID int, hosts []int) error {
816-
fields := LogFields{"hosts": hosts, "lunID": lunID}
816+
func (c *Client) ScanTargetLUN(ctx context.Context, deviceAddresses []models.ScsiDeviceAddress) error {
817+
fields := LogFields{"deviceAddresses": deviceAddresses}
817818
Logc(ctx).WithFields(fields).Debug(">>>> iscsi.scanTargetLUN")
818819
defer Logc(ctx).WithFields(fields).Debug("<<<< iscsi.scanTargetLUN")
819820

@@ -822,16 +823,11 @@ func (c *Client) ScanTargetLUN(ctx context.Context, lunID int, hosts []int) erro
822823
err error
823824
)
824825

825-
// By default, scan for all the LUNs
826-
scanCmd := "0 0 -"
827-
if lunID >= 0 {
828-
scanCmd = fmt.Sprintf("0 0 %d", lunID)
829-
}
830-
831826
c.ListAllDevices(ctx)
832-
for _, hostNumber := range hosts {
827+
for _, deviceAddress := range deviceAddresses {
828+
scanCmd := fmt.Sprintf("%s %s %s", deviceAddress.Channel, deviceAddress.Target, deviceAddress.LUN)
833829

834-
filename := fmt.Sprintf(c.chrootPathPrefix+"/sys/class/scsi_host/host%d/scan", hostNumber)
830+
filename := fmt.Sprintf(c.chrootPathPrefix+"/sys/class/scsi_host/host%s/scan", deviceAddress.Host)
835831
if f, err = c.osFs.OpenFile(filename, os.O_APPEND|os.O_WRONLY, 0o200); err != nil {
836832
Logc(ctx).WithField("file", filename).Warning("Could not open file for writing.")
837833
return err
@@ -857,9 +853,18 @@ func (c *Client) ScanTargetLUN(ctx context.Context, lunID int, hosts []int) erro
857853
Logc(ctx).WithFields(LogFields{
858854
"scanCmd": scanCmd,
859855
"scanFile": filename,
860-
"host": hostNumber,
856+
"host": deviceAddress.Host,
861857
}).Debug("Invoked SCSI scan for host.")
858+
862859
}
863860

864861
return nil
865862
}
863+
864+
// convertToDeviceAddressValue converts the device address value to string.
865+
func convertToDeviceAddressValue(value int) string {
866+
if value < 0 {
867+
return "-"
868+
}
869+
return strconv.Itoa(value)
870+
}

utils/devices/devices_test.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,9 @@ func TestClient_scanTargetLUN(t *testing.T) {
114114
const lunID = 0
115115
const host1 = 1
116116
const host2 = 2
117+
const lunIDStr = "0"
118+
const host1Str = "1"
119+
const host2Str = "2"
117120

118121
tests := map[string]parameters{
119122
"scan files not present": {
@@ -185,7 +188,10 @@ func TestClient_scanTargetLUN(t *testing.T) {
185188
t.Run(name, func(t *testing.T) {
186189
client := NewDetailed(nil, afero.Afero{Fs: params.getFileSystemUtils()}, NewDiskSizeGetter())
187190

188-
err := client.ScanTargetLUN(context.TODO(), lunID, []int{host1, host2})
191+
deviceAddresses := make([]models.ScsiDeviceAddress, 0)
192+
deviceAddresses = append(deviceAddresses, models.ScsiDeviceAddress{Host: host1Str, LUN: lunIDStr})
193+
deviceAddresses = append(deviceAddresses, models.ScsiDeviceAddress{Host: host2Str, LUN: lunIDStr})
194+
err := client.ScanTargetLUN(context.TODO(), deviceAddresses)
189195
if params.assertError != nil {
190196
params.assertError(t, err)
191197
}

utils/fcp/fcp.go

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,10 @@ import (
1717
"github.com/cenkalti/backoff/v4"
1818

1919
"github.com/netapp/trident/internal/fiji"
20+
"github.com/netapp/trident/pkg/collection"
2021

2122
. "github.com/netapp/trident/logging"
23+
2224
"github.com/netapp/trident/utils/devices"
2325
"github.com/netapp/trident/utils/devices/luks"
2426
"github.com/netapp/trident/utils/errors"
@@ -641,7 +643,9 @@ func (client *Client) GetDeviceInfoForLUN(
641643
}).Debug("Found SCSI device.")
642644

643645
info := &models.ScsiDeviceInfo{
644-
LUN: strconv.Itoa(lunID),
646+
ScsiDeviceAddress: models.ScsiDeviceAddress{
647+
LUN: strconv.Itoa(lunID),
648+
},
645649
MultipathDevice: multipathDevice,
646650
Devices: devicesForLUN,
647651
DevicePaths: paths,
@@ -770,9 +774,13 @@ func (client *Client) waitForDeviceScan(ctx context.Context, hostSessionMap []ma
770774
Logc(ctx).WithFields(fields).Debug(">>>> fcp.waitForDeviceScan")
771775
defer Logc(ctx).WithFields(fields).Debug("<<<< fcp.waitForDeviceScan")
772776

773-
hosts := make([]int, 0)
774-
var hostNum int
777+
Logc(ctx).WithField("hostSessionMap", hostSessionMap).Debug("Built FCP host/session map.")
778+
779+
deviceAddresses := make([]models.ScsiDeviceAddress, 0)
780+
var hostNum, targetID int
775781
var err error
782+
783+
// Construct the host and target lists required for the scan.
776784
for _, hostNumber := range hostSessionMap {
777785
for host := range hostNumber {
778786
re := regexp.MustCompile(`rport-(\d+):`)
@@ -783,11 +791,33 @@ func (client *Client) waitForDeviceScan(ctx context.Context, hostSessionMap []ma
783791
continue
784792
}
785793
}
786-
hosts = append(hosts, hostNum)
794+
795+
// Read the target IDs from the sysfs path.
796+
targetIDFile := fmt.Sprintf("/sys/class/fc_host/host%d/device/%s/fc_remote_ports/%s/scsi_target_id",
797+
hostNum, host, host)
798+
if targetIDRaw, err := os.ReadFile(targetIDFile); err != nil {
799+
Logc(ctx).WithField("targetIDFile", targetIDFile).Error("Could not find the target ID file")
800+
} else {
801+
targetIDStr := strings.TrimSpace(string(targetIDRaw))
802+
if targetID, err = strconv.Atoi(targetIDStr); err != nil {
803+
Logc(ctx).WithField("targetID", targetIDStr).Error("Could not parse target ID")
804+
continue
805+
}
806+
807+
deviceAddress := models.ScsiDeviceAddress{
808+
Host: strconv.Itoa(hostNum),
809+
Channel: models.ScanAllSCSIDeviceAddress,
810+
Target: strconv.Itoa(targetID),
811+
LUN: strconv.Itoa(lunID),
812+
}
813+
if !collection.Contains(deviceAddresses, deviceAddress) {
814+
deviceAddresses = append(deviceAddresses, deviceAddress)
815+
}
816+
}
787817
}
788818
}
789819

790-
if err := client.deviceClient.ScanTargetLUN(ctx, lunID, hosts); err != nil {
820+
if err := client.deviceClient.ScanTargetLUN(ctx, deviceAddresses); err != nil {
791821
Logc(ctx).WithField("scanError", err).Error("Could not scan for new LUN.")
792822
}
793823

utils/iscsi.go

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -483,12 +483,17 @@ func InitiateScanForLun(ctx context.Context, lunID int, iSCSINodeName string) er
483483
}
484484

485485
Logc(ctx).WithField("hostSessionMap", hostSessionMap).Debug("Built iSCSI host/session map.")
486-
hosts := make([]int, 0)
486+
deviceAddresses := make([]models.ScsiDeviceAddress, 0)
487487
for hostNumber := range hostSessionMap {
488-
hosts = append(hosts, hostNumber)
488+
deviceAddresses = append(deviceAddresses, models.ScsiDeviceAddress{
489+
Host: strconv.Itoa(hostNumber),
490+
Channel: models.ScanSCSIDeviceAddressZero,
491+
Target: models.ScanSCSIDeviceAddressZero,
492+
LUN: strconv.Itoa(lunID),
493+
})
489494
}
490495

491-
if err := iSCSIScanTargetLUN(ctx, lunID, hosts); err != nil {
496+
if err := iSCSIScanTargetLUN(ctx, deviceAddresses); err != nil {
492497
Logc(ctx).WithField("scanError", err).Error("Could not scan for new LUN.")
493498
}
494499

@@ -502,21 +507,26 @@ func InitiateScanForAllLUNs(ctx context.Context, iSCSINodeName string) error {
502507
Logc(ctx).WithFields(fields).Debug(">>>> iscsi.InitiateScanForAllLUNs")
503508
defer Logc(ctx).WithFields(fields).Debug("<<<< iscsi.InitiateScanForAllLUNs")
504509

505-
// Setting lunID to -1 so that all the LUNs are scanned.
506-
lunID := -1
510+
// Setting lunID to - so that all the LUNs are scanned.
511+
lunID := models.ScanAllSCSIDeviceAddress
507512

508513
hostSessionMap := IscsiUtils.GetISCSIHostSessionMapForTarget(ctx, iSCSINodeName)
509514
if len(hostSessionMap) == 0 {
510515
return fmt.Errorf("no iSCSI hosts found for target %s", iSCSINodeName)
511516
}
512517

513518
Logc(ctx).WithField("hostSessionMap", hostSessionMap).Debug("Built iSCSI host/session map.")
514-
hosts := make([]int, 0)
519+
deviceAddresses := make([]models.ScsiDeviceAddress, 0)
515520
for hostNumber := range hostSessionMap {
516-
hosts = append(hosts, hostNumber)
521+
deviceAddresses = append(deviceAddresses, models.ScsiDeviceAddress{
522+
Host: strconv.Itoa(hostNumber),
523+
Channel: models.ScanSCSIDeviceAddressZero,
524+
Target: models.ScanSCSIDeviceAddressZero,
525+
LUN: lunID,
526+
})
517527
}
518528

519-
if err := iSCSIScanTargetLUN(ctx, lunID, hosts); err != nil {
529+
if err := iSCSIScanTargetLUN(ctx, deviceAddresses); err != nil {
520530
Logc(ctx).WithError(err).Error("Could not scan for new LUN.")
521531
}
522532

@@ -882,8 +892,8 @@ func LoginISCSITarget(ctx context.Context, publishInfo *models.VolumePublishInfo
882892
return iscsiClient.LoginTarget(ctx, publishInfo, portal)
883893
}
884894

885-
func iSCSIScanTargetLUN(ctx context.Context, lunID int, hosts []int) error {
886-
return devicesClient.ScanTargetLUN(ctx, lunID, hosts)
895+
func iSCSIScanTargetLUN(ctx context.Context, deviceAddresses []models.ScsiDeviceAddress) error {
896+
return devicesClient.ScanTargetLUN(ctx, deviceAddresses)
887897
}
888898

889899
func IsISCSISessionStale(ctx context.Context, sessionNumber string) bool {

utils/iscsi/iscsi.go

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -792,12 +792,12 @@ func (client *Client) GetDeviceInfoForLUN(
792792
}).Debug("Found SCSI device.")
793793

794794
info := &models.ScsiDeviceInfo{
795-
LUN: strconv.Itoa(lunID),
796-
MultipathDevice: multipathDevice,
797-
Devices: devicesForLUN,
798-
DevicePaths: paths,
799-
Filesystem: fsType,
800-
IQN: iSCSINodeName,
795+
ScsiDeviceAddress: models.ScsiDeviceAddress{LUN: strconv.Itoa(lunID)},
796+
MultipathDevice: multipathDevice,
797+
Devices: devicesForLUN,
798+
DevicePaths: paths,
799+
Filesystem: fsType,
800+
IQN: iSCSINodeName,
801801
}
802802

803803
return info, nil
@@ -921,12 +921,17 @@ func (client *Client) waitForDeviceScan(ctx context.Context, hostSessionMap map[
921921
Logc(ctx).WithFields(fields).Debug(">>>> iscsi.waitForDeviceScan")
922922
defer Logc(ctx).WithFields(fields).Debug("<<<< iscsi.waitForDeviceScan")
923923

924-
hosts := make([]int, 0)
924+
deviceAddresses := make([]models.ScsiDeviceAddress, 0)
925925
for hostNumber := range hostSessionMap {
926-
hosts = append(hosts, hostNumber)
926+
deviceAddresses = append(deviceAddresses, models.ScsiDeviceAddress{
927+
Host: strconv.Itoa(hostNumber),
928+
Channel: models.ScanSCSIDeviceAddressZero,
929+
Target: models.ScanSCSIDeviceAddressZero,
930+
LUN: strconv.Itoa(lunID),
931+
})
927932
}
928933

929-
if err := client.devices.ScanTargetLUN(ctx, lunID, hosts); err != nil {
934+
if err := client.devices.ScanTargetLUN(ctx, deviceAddresses); err != nil {
930935
Logc(ctx).WithField("scanError", err).Error("Could not scan for new LUN.")
931936
}
932937

@@ -2152,10 +2157,12 @@ func (c *Client) GetISCSIDevices(ctx context.Context, getCredentials bool) ([]*m
21522157
}).Debug("Found iSCSI device.")
21532158

21542159
device := &models.ScsiDeviceInfo{
2155-
Host: hostNum,
2156-
Channel: busNum,
2157-
Target: deviceNum,
2158-
LUN: lunNum,
2160+
ScsiDeviceAddress: models.ScsiDeviceAddress{
2161+
Host: hostNum,
2162+
Channel: busNum,
2163+
Target: deviceNum,
2164+
LUN: lunNum,
2165+
},
21592166
Devices: slaveDevices,
21602167
MultipathDevice: multipathDevice,
21612168
IQN: targetIQN,

utils/iscsi/iscsi_linux_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ tcp: [4] 127.0.0.2:3260,1029 ` + targetIQN + ` (non-flash)`
8686
mockDevices.EXPECT().WaitForDevice(context.TODO(), "/dev/dm-0").Return(nil)
8787
mockDevices.EXPECT().GetMultipathDeviceUUID("dm-0").Return("mpath-53594135475a464a3847314d3930354756483748", nil)
8888
mockDevices.EXPECT().GetLunSerial(context.TODO(), "/dev/sda").Return(vpdpg80Serial, nil).Times(3)
89-
mockDevices.EXPECT().ScanTargetLUN(context.TODO(), 0, []int{0})
89+
mockDevices.EXPECT().ScanTargetLUN(context.TODO(), ScsiScanZeros)
9090
mockDevices.EXPECT().FindMultipathDeviceForDevice(context.TODO(), "sda").Return("dm-0").Times(2)
9191
mockDevices.EXPECT().VerifyMultipathDeviceSize(context.TODO(), "dm-0", "sda").Return(int64(0), true,
9292
nil)
@@ -179,7 +179,7 @@ tcp: [4] 127.0.0.2:3260,1029 ` + targetIQN + ` (non-flash)`
179179
mockDevices.EXPECT().WaitForDevice(context.TODO(), "/dev/dm-0").Return(nil)
180180
mockDevices.EXPECT().GetMultipathDeviceUUID("dm-0").Return("mpath-53594135475a464a3847314d3930354756483748", nil)
181181
mockDevices.EXPECT().GetLunSerial(context.TODO(), "/dev/sda").Return(vpdpg80Serial, nil).Times(3)
182-
mockDevices.EXPECT().ScanTargetLUN(context.TODO(), 0, []int{0})
182+
mockDevices.EXPECT().ScanTargetLUN(context.TODO(), ScsiScanZeros)
183183
mockDevices.EXPECT().FindMultipathDeviceForDevice(context.TODO(), "sda").Return("dm-0").Times(2)
184184
mockDevices.EXPECT().VerifyMultipathDeviceSize(context.TODO(), "dm-0", "sda").Return(int64(0), true,
185185
nil)

0 commit comments

Comments
 (0)