-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Implemented io-uring Op<Statx> and applied to read_uring and fs::try_exists
#8080
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
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
acf35a7
implement Op<Statx> and use it to implement fs::try_exists
vrtgs ae51fab
fix statx on unavailable platforms
vrtgs d4686ae
complete using only io-uring operations for read_uring
vrtgs 3edeeff
Implemented io-uring::Op<Statx> and apply it accordingly to read_urin…
asder8215 7e008e6
Added test for io uring statx operations. Checks for cancellations, s…
asder8215 91fe47e
Removed musl as supported platform for io_uring statx operations, as …
asder8215 5631da6
Removed pending checks for cancel_op_future since io_uring not availa…
asder8215 25642ec
Removed redundant cfg attributes on functions, added STATX_BTIME flag…
asder8215 f1e6d2f
Uncommented stat_permission_denied test and make sure it doesn't run …
asder8215 383724f
Statx fd leak drop test added and added STATX_BTIME to file_metadata
asder8215 3a24159
Remove unnecessary utils function, refactored code in statx and statx…
asder8215 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,5 @@ | ||
| pub(crate) mod open; | ||
| pub(crate) mod read; | ||
| pub(crate) mod statx; | ||
| pub(crate) mod utils; | ||
| pub(crate) mod write; |
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,161 @@ | ||
| #![cfg(all( | ||
| tokio_unstable, | ||
| feature = "io-uring", | ||
| feature = "rt", | ||
| feature = "fs", | ||
| // libc::statx is only supported on these platforms | ||
| // FIXME: Add musl target env when our minimum supported | ||
| // rust version is 1.93. To clarify, statx support is | ||
| // introduced to musl in 1.25 as mentioned officially here: | ||
| // https://musl.libc.org/releases.html. | ||
| // However, rustup target_env building for *-linux-musl | ||
| // uses 1.25 musl on all *-linux-musl platforms starting | ||
| // in 1.93 stable rust version. | ||
| // https://blog.rust-lang.org/2025/12/05/Updating-musl-1.2.5/ | ||
| any(target_env = "gnu", target_os = "android") | ||
| ))] | ||
|
|
||
| use crate::fs::File; | ||
| use crate::io::uring::utils::cstr; | ||
| use crate::runtime::driver::op::{CancelData, Cancellable, Completable, CqeResult, Op}; | ||
| use io_uring::{opcode, types}; | ||
| use libc::statx; | ||
| use std::ffi::{CStr, CString}; | ||
| use std::fmt::{Debug, Formatter}; | ||
| use std::io; | ||
| use std::mem::MaybeUninit; | ||
| use std::os::fd::AsRawFd; | ||
| use std::path::Path; | ||
|
|
||
| pub(crate) struct Metadata(statx); | ||
|
|
||
| impl Metadata { | ||
| /// Returns the size of the file, in bytes, this metadata is for. | ||
| pub(crate) fn len(&self) -> u64 { | ||
| self.0.stx_size | ||
| } | ||
| } | ||
|
|
||
| impl Debug for Metadata { | ||
| fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { | ||
| let mut debug = f.debug_struct("Metadata"); | ||
| debug.field("len", &self.len()); | ||
| debug.finish_non_exhaustive() | ||
| } | ||
| } | ||
|
|
||
| #[derive(Debug)] | ||
| pub(crate) struct Statx { | ||
|
asder8215 marked this conversation as resolved.
|
||
| /// This field will be read by the kernel during the operation, so we | ||
| /// need to ensure it is valid for the entire duration of the operation. | ||
| _path: CString, | ||
| buffer: Box<MaybeUninit<statx>>, | ||
| } | ||
|
|
||
| impl Completable for Statx { | ||
| type Output = io::Result<Metadata>; | ||
|
|
||
| fn complete(self, cqe: CqeResult) -> Self::Output { | ||
| // SAFETY: On success, we always receive 0, which should guarantee | ||
| // that the information about a file is stored inside the | ||
| // statx buffer. On failure, we'll receive an Error value. | ||
| // Refer to man page description and return value: | ||
| // https://man7.org/linux/man-pages/man2/statx.2.html | ||
| cqe.result | ||
| .map(|_| Metadata(unsafe { *self.buffer.as_ptr() })) | ||
| } | ||
|
|
||
| fn complete_with_error(self, error: io::Error) -> Self::Output { | ||
| Err(error) | ||
| } | ||
| } | ||
|
|
||
| impl Cancellable for Statx { | ||
| fn cancel(self) -> CancelData { | ||
| CancelData::Statx(self) | ||
| } | ||
| } | ||
|
|
||
| impl Op<Statx> { | ||
| /// Submit a request to retrieve a file's status. | ||
| #[inline] | ||
| fn statx(path: &Path, flags: i32) -> io::Result<Op<Statx>> { | ||
| let path = cstr(path)?; | ||
| let mut buffer = Box::new(MaybeUninit::<statx>::uninit()); | ||
|
|
||
| let statx_op = opcode::Statx::new( | ||
| types::Fd(libc::AT_FDCWD), | ||
| path.as_ptr(), | ||
| buffer.as_mut_ptr().cast(), | ||
| ) | ||
| .flags(flags) | ||
| .mask(libc::STATX_BASIC_STATS | libc::STATX_BTIME) | ||
| .build(); | ||
|
|
||
| // SAFETY: Parameters are valid for the entire duration of the operation | ||
| Ok(unsafe { | ||
| Op::new( | ||
| statx_op, | ||
| Statx { | ||
| _path: path, | ||
| buffer, | ||
| }, | ||
| ) | ||
| }) | ||
| } | ||
|
|
||
| /// Retrieves the metadata information of the given path, following symlinks | ||
| /// if the path provided points to a symlink location. | ||
| #[inline] | ||
| pub(crate) fn metadata(path: &Path) -> io::Result<Op<Statx>> { | ||
| Op::statx(path, libc::AT_STATX_SYNC_AS_STAT) | ||
| } | ||
|
|
||
| /// Retrieves the metadata information of the given file | ||
| pub(crate) fn file_metadata(file: &File) -> io::Result<Op<Statx>> { | ||
| let mut buffer = Box::new(MaybeUninit::<statx>::uninit()); | ||
| let empty_path: &'static CStr = c""; | ||
|
|
||
| // io-uring was introduced in linux 5.1 | ||
| // pass in an empty path instead of null to target the file descriptor | ||
| // status as specified by man: | ||
| // https://man7.org/linux/man-pages/man2/statx.2.html | ||
| let statx_op = opcode::Statx::new( | ||
| types::Fd(file.as_raw_fd()), | ||
|
asder8215 marked this conversation as resolved.
|
||
| // it should be fine to pass in `empty_path` whose lifetime | ||
| // does not exceed the `file_metadata()` function as a ptr here | ||
| // because we want to stat the dirfd not this pathname | ||
| empty_path.as_ptr(), | ||
| buffer.as_mut_ptr().cast(), | ||
| ) | ||
| .flags(libc::AT_STATX_SYNC_AS_STAT | libc::AT_EMPTY_PATH) | ||
| .mask(libc::STATX_BASIC_STATS | libc::STATX_BTIME) | ||
| .build(); | ||
|
|
||
| // SAFETY: Parameters are valid for the entire duration of the operation | ||
| Ok(unsafe { | ||
| Op::new( | ||
| statx_op, | ||
| Statx { | ||
| _path: empty_path.into(), | ||
| buffer, | ||
| }, | ||
| ) | ||
| }) | ||
| } | ||
|
|
||
| // TODO: Once `Metadata::from_statx` is stabilized, we can use use this function | ||
| // to enable io-uring support on `tokio::fs::symlink_metadata`. | ||
| // See this PR for more detail: https://github.com/tokio-rs/tokio/pull/8080 | ||
| // See `Metadata::from_statx` tracking issue to see progress: | ||
| // https://github.com/rust-lang/rust/issues/156268 | ||
| /// Retrieves the metadata information of the given path without following symlinks. | ||
| #[inline] | ||
| #[allow(dead_code)] | ||
|
asder8215 marked this conversation as resolved.
|
||
| pub(crate) fn symlink_metadata(path: &Path) -> io::Result<Op<Statx>> { | ||
| Op::statx( | ||
| path, | ||
| libc::AT_STATX_SYNC_AS_STAT | libc::AT_SYMLINK_NOFOLLOW, | ||
| ) | ||
| } | ||
| } | ||
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.
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.