@@ -147,7 +147,7 @@ func (d BlockVolumeNodeDriver) NodeStageVolume(ctx context.Context, req *csi.Nod
147147 defer d .volumeLocks .Release (req .VolumeId )
148148
149149 if ! isRawBlockVolume {
150- isMounted , oErr := mountHandler .DeviceOpened (devicePath )
150+ isMounted , oErr := mountHandler .IsMounted (devicePath , req . StagingTargetPath )
151151 if oErr != nil {
152152 logger .With (zap .Error (oErr )).Error ("getting error to get the details about volume is already mounted or not." )
153153 return nil , status .Error (codes .Internal , oErr .Error ())
@@ -197,6 +197,10 @@ func (d BlockVolumeNodeDriver) NodeStageVolume(ctx context.Context, req *csi.Nod
197197 devicePath , err = disk .WaitForDevicePathToExist (ctx , scsiInfo , logger )
198198 if err != nil {
199199 logger .With (zap .Error (err )).Error ("Failed to get /dev/disk/by-path device path for iscsi volume." )
200+ err = mountHandler .ISCSILogoutOnFailure ()
201+ if err != nil {
202+ return nil , status .Error (codes .Internal , "Failed to iscsi logout after timeout on waiting for device path to exist" )
203+ }
200204 return nil , status .Error (codes .InvalidArgument , "Failed to get device path for iscsi volume" )
201205 }
202206 }
@@ -215,12 +219,20 @@ func (d BlockVolumeNodeDriver) NodeStageVolume(ctx context.Context, req *csi.Nod
215219 err := csi_util .CreateFilePath (logger , stagingTargetFilePath )
216220 if err != nil {
217221 logger .With (zap .Error (err )).Error ("failed to create the stagingTargetFile." )
222+ err = mountHandler .ISCSILogoutOnFailure ()
223+ if err != nil {
224+ return nil , status .Error (codes .Internal , "Failed to iscsi logout after mount failure" )
225+ }
218226 return nil , status .Error (codes .Internal , err .Error ())
219227 }
220228 options := []string {"bind" } // Append the "bind" option if it is a raw block volume
221229 err = mountHandler .Mount (devicePath , stagingTargetFilePath , "" , options )
222230 if err != nil {
223231 logger .With (zap .Error (err )).Error ("failed to bind mount raw block volume to stagingTargetFile" )
232+ err = mountHandler .ISCSILogoutOnFailure ()
233+ if err != nil {
234+ return nil , status .Error (codes .Internal , "Failed to iscsi logout after mount failure" )
235+ }
224236 return nil , status .Error (codes .Internal , err .Error ())
225237 }
226238 return & csi.NodeStageVolumeResponse {}, nil
@@ -238,6 +250,10 @@ func (d BlockVolumeNodeDriver) NodeStageVolume(ctx context.Context, req *csi.Nod
238250 exists = false
239251 } else {
240252 logger .With (zap .Error (err )).Errorf ("failed to check if stagingTargetPath %q exists" , req .StagingTargetPath )
253+ err = mountHandler .ISCSILogoutOnFailure ()
254+ if err != nil {
255+ return nil , status .Error (codes .Internal , "Failed to iscsi logout after failure to check if staging path exists" )
256+ }
241257 message := fmt .Sprintf ("failed to check if stagingTargetPath %q exists" , req .StagingTargetPath )
242258 return nil , status .Error (codes .Internal , message )
243259 }
@@ -249,6 +265,10 @@ func (d BlockVolumeNodeDriver) NodeStageVolume(ctx context.Context, req *csi.Nod
249265 if ! exists {
250266 if err := os .MkdirAll (req .StagingTargetPath , 0750 ); err != nil {
251267 logger .With (zap .Error (err )).Error ("Failed to create StagingTargetPath directory" )
268+ err = mountHandler .ISCSILogoutOnFailure ()
269+ if err != nil {
270+ return nil , status .Error (codes .Internal , "Failed to iscsi logout after failure to create StagingTargetPath directory" )
271+ }
252272 return nil , status .Error (codes .Internal , "Failed to create StagingTargetPath directory" )
253273 }
254274 }
@@ -270,6 +290,10 @@ func (d BlockVolumeNodeDriver) NodeStageVolume(ctx context.Context, req *csi.Nod
270290 if existingFs != "" && existingFs != fsType {
271291 returnError := fmt .Sprintf ("FS Type mismatch detected. The existing fs type on the volume: %q doesn't match the requested fs type: %q. Please change fs type in PV to match the existing fs type." , existingFs , fsType )
272292 logger .Error (returnError )
293+ err = mountHandler .ISCSILogoutOnFailure ()
294+ if err != nil {
295+ return nil , status .Error (codes .Internal , "Failed to iscsi logout after failure due to FS Type mismatch" )
296+ }
273297 return nil , status .Error (codes .Internal , returnError )
274298 }
275299
@@ -278,6 +302,10 @@ func (d BlockVolumeNodeDriver) NodeStageVolume(ctx context.Context, req *csi.Nod
278302 err = mountHandler .FormatAndMount (devicePath , req .StagingTargetPath , fsType , options )
279303 if err != nil {
280304 logger .With (zap .Error (err )).Error ("failed to format and mount volume to staging path." )
305+ err = mountHandler .ISCSILogoutOnFailure ()
306+ if err != nil {
307+ return nil , status .Error (codes .Internal , "Failed to iscsi logout after mount failure" )
308+ }
281309 return nil , status .Error (codes .Internal , err .Error ())
282310 }
283311 logger .With ("devicePath" , devicePath , "fsType" , fsType , "attachmentType" , attachment ).
@@ -379,10 +407,10 @@ func (d BlockVolumeNodeDriver) NodeUnstageVolume(ctx context.Context, req *csi.N
379407 if ! isRawBlockVolume {
380408 isMounted , oErr := mountHandler .DeviceOpened (devicePath )
381409 if oErr != nil {
382- logger .With (zap .Error (oErr )).Error ("getting error to get the details about volume is already mounted or not." )
410+ logger .With (zap .Error (oErr )).Error ("getting error to get the details about volume is already unmounted or not." )
383411 return nil , status .Error (codes .Internal , oErr .Error ())
384412 } else if ! isMounted {
385- logger .Info ("volume is already unmounted on the staging path." )
413+ logger .Info ("volume is already unmounted from the staging path." )
386414 return & csi.NodeUnstageVolumeResponse {}, nil
387415 }
388416 }
0 commit comments