Skip to content

Commit 1f212a7

Browse files
committed
fix(initiator): persist SubsystemNQN after NVMe/TCP connect
Persist the discovered SubsystemNQN immediately after a successful discover-and-connect operation. This keeps initiator state aligned with the actual connected NVMe/TCP target even when controllerName validation fails afterwards. Without this update, reload/load/cleanup paths may use a stale SubsystemNQN and fail to locate or disconnect the correct NVMe device. Longhorn 7124 Signed-off-by: Derek Su <derek.su@suse.com>
1 parent 2cb1cc0 commit 1f212a7

2 files changed

Lines changed: 44 additions & 4 deletions

File tree

pkg/initiator/initiator.go

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -423,11 +423,9 @@ func (i *Initiator) StartNvmeTCPInitiator(transportAddress, transportServiceID s
423423
if err != nil {
424424
return dmDeviceIsBusy, errors.Wrapf(err, "failed to discover and connect NVMe/TCP target %s:%s", transportAddress, transportServiceID)
425425
}
426-
if controllerName == "" {
427-
return dmDeviceIsBusy, fmt.Errorf("controller name is empty after discovering and connecting NVMe/TCP target %s:%s", transportAddress, transportServiceID)
426+
if err := i.recordConnectedNVMeTCPInfo(subsystemNQN, controllerName); err != nil {
427+
return dmDeviceIsBusy, errors.Wrapf(err, "invalid connected NVMe/TCP target state %s:%s", transportAddress, transportServiceID)
428428
}
429-
i.NVMeTCPInfo.SubsystemNQN = subsystemNQN
430-
i.NVMeTCPInfo.ControllerName = controllerName
431429

432430
i.logger.Info("Loading NVMe/TCP initiator device info")
433431
err = i.waitAndLoadNVMeDeviceInfoWithoutLock(transportAddress, transportServiceID)
@@ -639,6 +637,25 @@ func (i *Initiator) waitAndLoadNVMeDeviceInfoWithoutLock(transportAddress, trans
639637
return nil
640638
}
641639

640+
func (i *Initiator) recordConnectedNVMeTCPInfo(subsystemNQN, controllerName string) error {
641+
if i.NVMeTCPInfo == nil {
642+
return fmt.Errorf("nvmeTCPInfo is nil")
643+
}
644+
645+
// Persist the discovered subsystem immediately after a successful connect.
646+
// Later reload/load/cleanup paths use SubsystemNQN to locate or disconnect
647+
// the NVMe device, so it must stay in sync even if controllerName validation fails.
648+
if subsystemNQN != "" {
649+
i.NVMeTCPInfo.SubsystemNQN = subsystemNQN
650+
}
651+
if controllerName == "" {
652+
return fmt.Errorf("controller name is empty")
653+
}
654+
655+
i.NVMeTCPInfo.ControllerName = controllerName
656+
return nil
657+
}
658+
642659
func (i *Initiator) discoverAndConnectNVMeTCPTarget(transportAddress, transportServiceID string, maxRetries int, retryInterval time.Duration) (subsystemNQN, controllerName string, err error) {
643660
if i.NVMeTCPInfo == nil {
644661
return "", "", fmt.Errorf("nvmeTCPInfo is nil")

pkg/initiator/initiator_test.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,29 @@ func (s *InitiatorTestSuite) TestLoadEndpointForNvmeTcpFrontendNilInfo(c *C) {
179179
c.Assert(err.Error(), Matches, ".*nvmeTCPInfo is nil.*")
180180
}
181181

182+
func (s *InitiatorTestSuite) TestRecordConnectedNVMeTCPInfoUpdatesSubsystemBeforeControllerValidation(c *C) {
183+
i := &Initiator{
184+
NVMeTCPInfo: &NVMeTCPInfo{
185+
SubsystemNQN: "nqn.old",
186+
ControllerName: "nvme-old",
187+
},
188+
}
189+
190+
err := i.recordConnectedNVMeTCPInfo("nqn.new", "")
191+
c.Assert(err, NotNil)
192+
c.Assert(err.Error(), Equals, "controller name is empty")
193+
c.Assert(i.NVMeTCPInfo.SubsystemNQN, Equals, "nqn.new")
194+
c.Assert(i.NVMeTCPInfo.ControllerName, Equals, "nvme-old")
195+
}
196+
197+
func (s *InitiatorTestSuite) TestRecordConnectedNVMeTCPInfoNilInfo(c *C) {
198+
i := &Initiator{}
199+
200+
err := i.recordConnectedNVMeTCPInfo("nqn.new", "nvme1")
201+
c.Assert(err, NotNil)
202+
c.Assert(err.Error(), Equals, "nvmeTCPInfo is nil")
203+
}
204+
182205
func (s *InitiatorTestSuite) TestIsNamespaceExist(c *C) {
183206
i := &Initiator{}
184207
c.Assert(i.isNamespaceExist([]string{"nvme0n1"}), Equals, false)

0 commit comments

Comments
 (0)