-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
fs: implement tokio::fs::symlink via io_uring #7844
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
tahmid-23
wants to merge
1
commit into
tokio-rs:master
Choose a base branch
from
tahmid-23:tahmid-23/uring-symlink
base: master
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.
+240
−0
Open
Changes from all commits
Commits
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
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 symlink; | ||
| 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,54 @@ | ||
| use super::utils::cstr; | ||
|
|
||
| use crate::runtime::driver::op::{CancelData, Cancellable, Completable, CqeResult, Op}; | ||
|
|
||
| use io_uring::{opcode, types}; | ||
| use std::ffi::CString; | ||
| use std::io; | ||
| use std::io::Error; | ||
| use std::path::Path; | ||
|
|
||
| #[derive(Debug)] | ||
| pub(crate) struct Symlink { | ||
| /// 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. | ||
| #[allow(dead_code)] | ||
| original: CString, | ||
| /// 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. | ||
| #[allow(dead_code)] | ||
| link: CString, | ||
| } | ||
|
|
||
| impl Completable for Symlink { | ||
| type Output = io::Result<()>; | ||
|
|
||
| fn complete(self, cqe: CqeResult) -> Self::Output { | ||
| cqe.result.map(|_| ()) | ||
| } | ||
|
|
||
| fn complete_with_error(self, err: Error) -> Self::Output { | ||
| Err(err) | ||
| } | ||
| } | ||
|
|
||
| impl Cancellable for Symlink { | ||
| fn cancel(self) -> CancelData { | ||
| CancelData::Symlink(self) | ||
| } | ||
| } | ||
|
|
||
| impl Op<Symlink> { | ||
| /// Submit a request to create a symbolic link. | ||
| pub(crate) fn symlink(original: &Path, link: &Path) -> io::Result<Self> { | ||
| let original = cstr(original)?; | ||
| let link = cstr(link)?; | ||
|
|
||
| let symlink_op = | ||
| opcode::SymlinkAt::new(types::Fd(libc::AT_FDCWD), original.as_ptr(), link.as_ptr()) | ||
| .build(); | ||
|
|
||
| // SAFETY: Parameters are valid for the entire duration of the operation | ||
| Ok(unsafe { Op::new(symlink_op, Symlink { original, link }) }) | ||
| } | ||
| } |
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,168 @@ | ||||||
| //! Uring symlink operations tests. | ||||||
|
|
||||||
| #![cfg(all( | ||||||
| tokio_unstable, | ||||||
| feature = "io-uring", | ||||||
| feature = "rt", | ||||||
| feature = "fs", | ||||||
| target_os = "linux" | ||||||
| ))] | ||||||
|
|
||||||
| use futures::FutureExt; | ||||||
| use std::future::poll_fn; | ||||||
| use std::future::Future; | ||||||
| use std::path::PathBuf; | ||||||
| use std::pin::pin; | ||||||
| use std::sync::mpsc; | ||||||
| use std::task::Poll; | ||||||
| use std::time::Duration; | ||||||
| use tempfile::TempDir; | ||||||
| use tokio::runtime::{Builder, Runtime}; | ||||||
| use tokio::task::JoinSet; | ||||||
| use tokio_test::assert_pending; | ||||||
| use tokio_util::task::TaskTracker; | ||||||
|
|
||||||
| fn multi_rt(n: usize) -> Box<dyn Fn() -> Runtime> { | ||||||
| Box::new(move || { | ||||||
| Builder::new_multi_thread() | ||||||
| .worker_threads(n) | ||||||
| .enable_all() | ||||||
| .build() | ||||||
| .unwrap() | ||||||
| }) | ||||||
| } | ||||||
|
|
||||||
| fn current_rt() -> Box<dyn Fn() -> Runtime> { | ||||||
| Box::new(|| Builder::new_current_thread().enable_all().build().unwrap()) | ||||||
| } | ||||||
|
|
||||||
| fn rt_combinations() -> Vec<Box<dyn Fn() -> Runtime>> { | ||||||
| vec![ | ||||||
| current_rt(), | ||||||
| multi_rt(1), | ||||||
| multi_rt(2), | ||||||
| multi_rt(8), | ||||||
| multi_rt(64), | ||||||
| multi_rt(256), | ||||||
| ] | ||||||
| } | ||||||
|
|
||||||
| #[test] | ||||||
| fn shutdown_runtime_while_performing_io_uring_ops() { | ||||||
| fn run(rt: Runtime) { | ||||||
| let (done_tx, done_rx) = mpsc::channel(); | ||||||
| let (workdir, target) = create_tmp_dir(); | ||||||
|
|
||||||
| rt.spawn(async move { | ||||||
| // spawning a bunch of uring operations. | ||||||
| for i in 0..usize::MAX { | ||||||
| let link = workdir.path().join(format!("{i}")); | ||||||
| let target = target.clone(); | ||||||
| tokio::spawn(async move { | ||||||
| let mut fut = pin!(tokio::fs::symlink(target, &link)); | ||||||
|
|
||||||
| poll_fn(|cx| { | ||||||
| assert_pending!(fut.as_mut().poll(cx)); | ||||||
| Poll::<()>::Pending | ||||||
| }) | ||||||
| .await; | ||||||
|
|
||||||
| fut.await.unwrap(); | ||||||
| }); | ||||||
|
|
||||||
| // Avoid busy looping. | ||||||
| tokio::task::yield_now().await; | ||||||
| } | ||||||
| }); | ||||||
|
|
||||||
| std::thread::spawn(move || { | ||||||
| rt.shutdown_timeout(Duration::from_millis(300)); | ||||||
| done_tx.send(()).unwrap(); | ||||||
| }); | ||||||
|
|
||||||
| done_rx.recv().unwrap(); | ||||||
| } | ||||||
|
|
||||||
| for rt in rt_combinations() { | ||||||
| run(rt()); | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| #[test] | ||||||
| fn symlink_many_files() { | ||||||
| fn run(rt: Runtime) { | ||||||
| let (workdir, target) = create_tmp_dir(); | ||||||
|
|
||||||
| rt.block_on(async move { | ||||||
| const N_LINKS: usize = 10_000; | ||||||
|
|
||||||
| let tracker = TaskTracker::new(); | ||||||
|
|
||||||
| for i in 0..N_LINKS { | ||||||
| let target = target.clone(); | ||||||
| let link = workdir.path().join(format!("{i}")); | ||||||
| tracker.spawn(async move { | ||||||
| tokio::fs::symlink(&target, &link).await.unwrap(); | ||||||
| }); | ||||||
| } | ||||||
| tracker.close(); | ||||||
| tracker.wait().await; | ||||||
|
|
||||||
| let mut resolve_tasks = JoinSet::new(); | ||||||
| for i in 0..N_LINKS { | ||||||
| let link = workdir.path().join(format!("{i}")); | ||||||
| resolve_tasks.spawn(async move { tokio::fs::read_link(&link).await.unwrap() }); | ||||||
| } | ||||||
|
|
||||||
| while let Some(resolve_result) = resolve_tasks.join_next().await { | ||||||
| assert_eq!(&resolve_result.unwrap(), &target); | ||||||
| } | ||||||
| }); | ||||||
| } | ||||||
|
|
||||||
| for rt in rt_combinations() { | ||||||
| run(rt()); | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| #[tokio::test] | ||||||
| async fn cancel_op_future() { | ||||||
| let (workdir, target) = create_tmp_dir(); | ||||||
|
|
||||||
| let (tx, mut rx) = tokio::sync::mpsc::unbounded_channel(); | ||||||
| let handle = tokio::spawn(async move { | ||||||
| poll_fn(|cx| { | ||||||
| let link = workdir.path().join("link"); | ||||||
| let fut = tokio::fs::symlink(&target, &link); | ||||||
|
|
||||||
| // If io_uring is enabled (and not falling back to the thread pool), | ||||||
| // the first poll should return Pending. | ||||||
| let _pending = pin!(fut).poll_unpin(cx); | ||||||
|
Member
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. Let's assert that:
Suggested change
|
||||||
|
|
||||||
| tx.send(()).unwrap(); | ||||||
|
|
||||||
| Poll::<()>::Pending | ||||||
| }) | ||||||
| .await; | ||||||
| }); | ||||||
|
|
||||||
| // Wait for the first poll | ||||||
| rx.recv().await.unwrap(); | ||||||
|
|
||||||
| handle.abort(); | ||||||
|
|
||||||
| let res = handle.await.unwrap_err(); | ||||||
| assert!(res.is_cancelled()); | ||||||
| } | ||||||
|
|
||||||
| fn create_tmp_dir() -> (TempDir, PathBuf) { | ||||||
| let workdir = tempfile::tempdir().unwrap(); | ||||||
| let target = workdir.path().join("target"); | ||||||
| std::fs::OpenOptions::new() | ||||||
| .create_new(true) | ||||||
| .write(true) | ||||||
| .open(&target) | ||||||
| .unwrap(); | ||||||
|
|
||||||
| (workdir, target) | ||||||
| } | ||||||
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.
otherwise
current_rt()won't do anything.