Repository navigation
ttrpc: attach Windows physical and virtual disks via registered handles #5158
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
Closed
Daman Mulye (damanm24)
wants to merge
16
commits into
microsoft:main
from
damanm24:wsl-upstream-physical-disks
Closed
Changes from 4 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
622ebc7
disk_file: accept client-provided Windows physical-disk handles
damanm24 d78e039
disk_file: merge latest main into physical disk passthrough branch
ba16a99
disk_file: merge main and use resource passing for disk handles
08a215d
vmm_tests: add ttrpc physical disk handle-passing test
d8d99fb
feedback
61fdf48
disk_file: open physical disks explicitly and reject overlapped handles
e172946
Merge branch 'main' into wsl-upstream-physical-disks
damanm24 3940c92
Refactor disk mounting logic in handle_passing.rs
damanm24 906c2d4
disk_physical: move Windows physical disk support out of disk_file
5fa4f45
re-work
ab656f1
fixes
4e5e11d
disk_physical: avoid unresolved rustdoc link on non-Windows builds
7b06f54
disk_physical: address review feedback
bbe6601
ttrpc: accept client-opened virtual disks via registered handles
aab70f4
disk_physical: check the device's buffer alignment requirement
82493c7
ttrpc: limit this change to client-opened virtual disks
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
Some comments aren't visible on the classic Files Changed page.
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
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
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
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
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
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
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
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 |
|---|---|---|
| @@ -0,0 +1,101 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT License. | ||
|
|
||
| //! Windows block device (for example `\\.\PhysicalDriveN`) metadata queries. | ||
|
|
||
| // UNSAFETY: Calling DeviceIoControl with properly sized input and output buffers. | ||
| #![expect(unsafe_code)] | ||
|
|
||
| use std::fs; | ||
| use std::io; | ||
| use std::mem::size_of; | ||
| use std::os::windows::io::AsRawHandle; | ||
| use windows_sys::Win32::Foundation::ERROR_INVALID_FUNCTION; | ||
| use windows_sys::Win32::Foundation::ERROR_NOT_SUPPORTED; | ||
| use windows_sys::Win32::System::IO::DeviceIoControl; | ||
| use windows_sys::Win32::System::Ioctl::GET_LENGTH_INFORMATION; | ||
| use windows_sys::Win32::System::Ioctl::IOCTL_DISK_GET_LENGTH_INFO; | ||
| use windows_sys::Win32::System::Ioctl::IOCTL_STORAGE_QUERY_PROPERTY; | ||
| use windows_sys::Win32::System::Ioctl::PropertyStandardQuery; | ||
| use windows_sys::Win32::System::Ioctl::STORAGE_ACCESS_ALIGNMENT_DESCRIPTOR; | ||
| use windows_sys::Win32::System::Ioctl::STORAGE_PROPERTY_QUERY; | ||
| use windows_sys::Win32::System::Ioctl::StorageAccessAlignmentProperty; | ||
|
|
||
| /// Size and sector geometry of a block device. | ||
| pub(crate) struct DeviceGeometry { | ||
| pub disk_size: u64, | ||
| pub sector_size: u32, | ||
| pub physical_sector_size: u32, | ||
| } | ||
|
|
||
| /// Returns true if `err` indicates that the handle is a device that does not | ||
| /// support file metadata queries. | ||
| pub(crate) fn is_device_error(err: &io::Error) -> bool { | ||
| err.raw_os_error() == Some(ERROR_INVALID_FUNCTION as i32) | ||
| } | ||
|
|
||
| fn ioctl<I, O: Default>(file: &fs::File, code: u32, input: Option<&I>) -> io::Result<O> { | ||
| let mut output = O::default(); | ||
| let mut returned = 0; | ||
| let (in_ptr, in_len) = input.map_or((std::ptr::null(), 0), |i| { | ||
| (std::ptr::from_ref(i).cast(), size_of::<I>() as u32) | ||
| }); | ||
| // SAFETY: the input and output pointers are valid for the specified | ||
| // lengths for the duration of the synchronous call. | ||
| let ok = unsafe { | ||
| DeviceIoControl( | ||
| file.as_raw_handle(), | ||
| code, | ||
| in_ptr, | ||
| in_len, | ||
| std::ptr::from_mut(&mut output).cast(), | ||
| size_of::<O>() as u32, | ||
| &mut returned, | ||
| std::ptr::null_mut(), | ||
| ) | ||
| }; | ||
| if ok == 0 { | ||
| return Err(io::Error::last_os_error()); | ||
| } | ||
| if (returned as usize) < size_of::<O>() { | ||
| return Err(io::Error::from_raw_os_error(ERROR_NOT_SUPPORTED as i32)); | ||
| } | ||
| Ok(output) | ||
| } | ||
|
|
||
| /// Queries the size and sector sizes of a block device. | ||
| pub(crate) fn query_device_geometry(file: &fs::File) -> io::Result<DeviceGeometry> { | ||
| let length: GET_LENGTH_INFORMATION = ioctl::<(), _>(file, IOCTL_DISK_GET_LENGTH_INFO, None)?; | ||
| let query = STORAGE_PROPERTY_QUERY { | ||
| PropertyId: StorageAccessAlignmentProperty, | ||
| QueryType: PropertyStandardQuery, | ||
| AdditionalParameters: [0], | ||
| }; | ||
| let (sector_size, physical_sector_size) = match ioctl::<_, STORAGE_ACCESS_ALIGNMENT_DESCRIPTOR>( | ||
| file, | ||
| IOCTL_STORAGE_QUERY_PROPERTY, | ||
| Some(&query), | ||
| ) { | ||
| Ok(alignment) => ( | ||
| alignment.BytesPerLogicalSector, | ||
| alignment.BytesPerPhysicalSector, | ||
| ), | ||
| Err(_) => (512, 512), | ||
| }; | ||
| let sector_size = if sector_size.is_power_of_two() && sector_size >= 512 { | ||
| sector_size | ||
| } else { | ||
| 512 | ||
| }; | ||
| let physical_sector_size = | ||
| if physical_sector_size.is_power_of_two() && physical_sector_size >= sector_size { | ||
| physical_sector_size | ||
| } else { | ||
| sector_size | ||
| }; | ||
| Ok(DeviceGeometry { | ||
| disk_size: length.Length as u64, | ||
| sector_size, | ||
| physical_sector_size, | ||
| }) | ||
| } |
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.