-
Notifications
You must be signed in to change notification settings - Fork 11
Race condition #92
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
tommymcguiver
wants to merge
3
commits into
kolesa-team:main
Choose a base branch
from
tommymcguiver:unmap-remove-patch-race
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Race condition #92
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,6 +7,7 @@ use Data::Dumper qw( Dumper ); # DEBUG | |
|
|
||
| use IO::File (); | ||
| use File::Path (); | ||
| use Fcntl qw(O_WRONLY); | ||
|
|
||
| use PVE::JSONSchema (); | ||
| use PVE::Network (); | ||
|
|
@@ -513,9 +514,31 @@ sub get_device_size { | |
| sub device_op { | ||
| my ( $device_path, $op, $value ) = @_; | ||
|
|
||
| open( my $fh, '>', $device_path . '/' . $op ) or $fatal->( "Could not open file \"$device_path/$op\" for writing.", undef ); | ||
| print $fh $value; | ||
| close( $fh ); | ||
| my $path = $device_path . '/' . $op; | ||
|
|
||
| # Open for writing, no buffering, and handle errors explicitly | ||
| sysopen( my $fh, $path, O_WRONLY ) or do { | ||
| $logger->( P_WARN, "Cannot open $path for writing: $!", undef ); | ||
| return; | ||
| }; | ||
|
|
||
| # The Write: Use a newline as many drivers require '\n' to trigger the store function | ||
| my $payload = "$value\n"; | ||
| my $result = syswrite( $fh, $payload ); | ||
|
|
||
| # Handle the error specifically | ||
| if ( !defined $result ) { | ||
| my $err = $!; | ||
| close( $fh ); # Close regardless of error | ||
|
|
||
| if ( $err =~ /Invalid argument/i ) { | ||
| $logger->( P_WARN, "Kernel rejected '$value' for $path (EINVAL). Device may be busy, mounted, or driver doesn't support this transition.", undef ); | ||
| return; | ||
| } | ||
| $logger->( P_WARN, "Critical failure writing to $path: $err", undef ); | ||
| } | ||
|
|
||
| close( $fh ) or $logger->( P_WARN, "Failed to close file \"$path\" (write may have succeeded).", undef ); | ||
| } | ||
|
|
||
| sub block_device_action { | ||
|
|
@@ -529,10 +552,19 @@ sub block_device_action { | |
| } | ||
| $device = $1; # untaint | ||
| my $device_path = '/sys/block/' . $device . '/device'; | ||
| if ( $action eq 'remove' ) { | ||
| $logger->( P_DEBUG, "Removing device: $device", undef ); | ||
| if ( $action eq 'offline' ) { | ||
| $logger->( P_DEBUG, "Make device offline: $device", undef ); | ||
| exec_command( [ 'blockdev', '--flushbufs', '/dev/' . $device ] ); | ||
| device_op( $device_path, 'state', 'offline' ); | ||
| device_op( $device_path, 'state', 'offline' ); | ||
| } elsif ( $action eq 'delete' ) { | ||
| $logger->( P_DEBUG, "Delete device: $device", undef ); | ||
|
|
||
| # Check state is offline before delete | ||
| my $state = file_read_firstline( $device_path . '/state' ); | ||
| if ( $state ne 'offline' ) { | ||
| $logger->( P_WARN, "Device $device is not offline (state: $state)", undef ); | ||
| } | ||
|
|
||
| device_op( $device_path, 'delete', '1' ); | ||
| } elsif ( $action eq 'rescan' ) { | ||
| $logger->( P_DEBUG, "Rescanning: $device", undef ); | ||
|
|
@@ -1817,8 +1849,51 @@ sub map_volume { | |
| return $path; | ||
| } | ||
|
|
||
| =head2 unmap_volume | ||
|
|
||
| Unmaps a volume from the system, handling device cleanup and multipath removal. | ||
|
|
||
| This method safely disconnects a volume by flushing caches, offlining devices, | ||
| removing multipath mappings if applicable, and cleaning up device nodes. | ||
|
|
||
| Parameters: | ||
| $class - The class instance (implicit first parameter) | ||
| $storeid - Storage identifier | ||
| $scfg - Storage configuration hash reference | ||
| $volname - Volume name to unmap | ||
| $deactivate_cb - Optional callback subroutine to execute after device offlining | ||
|
|
||
| The callback exists to prevent race conditions between offlining devices, | ||
| making the storage unavailable, and finally deleting the device nodes. | ||
| This prevents the udev checker from re-adding devices before final cleanup is complete. | ||
|
|
||
| Returns: | ||
| 1 on success, 0 if volume path doesn't exist or isn't a block device | ||
|
|
||
| The function performs the following operations: | ||
|
|
||
| =over | ||
|
|
||
| =item * Retrieves the device path and WWID for the volume | ||
|
|
||
| =item * Flushes filesystem and device buffers to ensure data integrity | ||
|
|
||
| =item * Waits for udev events to settle | ||
|
|
||
| =item * Removes multipath mapping if the device is multipath-enabled | ||
|
|
||
| =item * Offlines all slave devices | ||
|
|
||
| =item * Executes the deactivation callback if provided | ||
|
|
||
| =item * Deletes the slave device nodes | ||
|
|
||
| =back | ||
|
|
||
| =cut | ||
|
|
||
| sub unmap_volume { | ||
| my ( $class, $storeid, $scfg, $volname, $snapname ) = @_; | ||
| my ( $class, $storeid, $scfg, $volname, $deactivate_cb ) = @_; | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
sub unmap_volume {
my ($cfg, $volid, $snapname) = @_;
my ($storeid, $volname) = parse_volume_id($volid);
my $scfg = storage_config($cfg, $storeid);
my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
return $plugin->unmap_volume($storeid, $scfg, $volname, $snapname);
}You should not change its signature. |
||
| $logger->( P_DEBUG, "unmap_volume", $scfg ); | ||
|
|
||
| my ( $path, $wwid ) = $class->purestorage_get_wwn( $scfg, $volname ); | ||
|
|
@@ -1846,8 +1921,14 @@ sub unmap_volume { | |
| $logger->( P_DEBUG, "Device \"$wwid\" is not a multipath device. Skipping multipath removal.", $scfg ); | ||
| } | ||
|
|
||
| # Iterate through slaves and remove each device | ||
| block_device_action( 'remove', @slaves ); | ||
| # Iterate through slaves and offline each device | ||
| block_device_action( 'offline', @slaves ); | ||
|
|
||
| if ( $deactivate_cb ) { | ||
| $deactivate_cb->(); | ||
| } | ||
|
|
||
| block_device_action( 'delete', @slaves ); | ||
|
|
||
| $logger->( P_DEBUG, "Device \"$wwid\" is removed.", $scfg ); | ||
| return 1; | ||
|
|
@@ -1867,9 +1948,11 @@ sub deactivate_volume { | |
| my ( $class, $storeid, $scfg, $volname, $snapname, $cache ) = @_; | ||
| $logger->( P_DEBUG, "deactivate_volume", $scfg ); | ||
|
|
||
| $class->unmap_volume( $storeid, $scfg, $volname, $snapname ); | ||
| my $deactivate_cb = sub { | ||
| $class->purestorage_volume_connection( $storeid, $scfg, $volname, 0 ); | ||
| }; | ||
|
|
||
| $class->purestorage_volume_connection( $storeid, $scfg, $volname, 0 ); | ||
| $class->unmap_volume( $storeid, $scfg, $volname, $deactivate_cb ); | ||
|
|
||
| $logger->( P_INFO, "Volume \"$volname\" is deactivated.", $scfg ); | ||
|
|
||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
close( $ fh )could be called twice.