-
Notifications
You must be signed in to change notification settings - Fork 25
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
Use LOOP_CONFIGURE ioctl for attaching to loop devices #57
Open
player-two
wants to merge
1
commit into
mdaffin:master
Choose a base branch
from
player-two:loop-configure
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.
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 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 |
---|---|---|
|
@@ -38,8 +38,8 @@ | |
//! ld.detach().unwrap(); | ||
//! ``` | ||
use crate::bindings::{ | ||
loop_info64, LOOP_CLR_FD, LOOP_CTL_GET_FREE, LOOP_SET_CAPACITY, LOOP_SET_FD, LOOP_SET_STATUS64, | ||
LO_FLAGS_AUTOCLEAR, LO_FLAGS_PARTSCAN, LO_FLAGS_READ_ONLY, | ||
loop_config, loop_info64, LOOP_CLR_FD, LOOP_CONFIGURE, LOOP_CTL_GET_FREE, LOOP_SET_CAPACITY, | ||
LOOP_SET_FD, LOOP_SET_STATUS64, LO_FLAGS_AUTOCLEAR, LO_FLAGS_PARTSCAN, LO_FLAGS_READ_ONLY, | ||
}; | ||
#[cfg(feature = "direct_io")] | ||
use bindings::LOOP_SET_DIRECT_IO; | ||
|
@@ -50,6 +50,8 @@ use std::{ | |
io, | ||
os::unix::prelude::*, | ||
path::{Path, PathBuf}, | ||
thread::sleep, | ||
time::Duration, | ||
}; | ||
|
||
#[allow(non_camel_case_types)] | ||
|
@@ -231,8 +233,47 @@ impl LoopDevice { | |
self.attach_fd_with_loop_info(bf, info) | ||
} | ||
|
||
/// Attach the loop device to a fd with `loop_info`. | ||
/// Attach the loop device to a fd with `loop_info64`. | ||
fn attach_fd_with_loop_info(&self, bf: impl AsRawFd, info: loop_info64) -> io::Result<()> { | ||
let cfg = loop_config { | ||
fd: bf.as_raw_fd().try_into().unwrap(), | ||
info, | ||
..Default::default() | ||
}; | ||
|
||
// First attempt the `LOOP_CONFIGURE` ioctl, which makes the operation | ||
// atomic because the device is created with a single ioctl. | ||
let mut retries = 0; | ||
loop { | ||
let result = unsafe { | ||
ioctl( | ||
self.device.as_raw_fd() as c_int, | ||
LOOP_CONFIGURE as IoctlRequest, | ||
&cfg, | ||
) | ||
}; | ||
|
||
match ioctl_to_error(result) { | ||
Err(err) => match err.raw_os_error() { | ||
// According to the `losetup` source code, these conditions | ||
// trigger the fallback logic. | ||
Some(libc::EINVAL) | Some(libc::ENOTTY) | Some(libc::ENOSYS) => break, | ||
Some(libc::EAGAIN) => { | ||
// Use the same max retries and sleep duration from the | ||
// `losetup` source. | ||
if retries >= 10 { | ||
return Err(err); | ||
} | ||
retries += 1; | ||
sleep(Duration::from_micros(25000)); | ||
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. Is there a specific reason the value? Depending on the use case 25ms can be in acceptable long time. |
||
continue; | ||
} | ||
_ => return Err(err), | ||
}, | ||
Ok(_) => return Ok(()), | ||
}; | ||
} | ||
|
||
// Attach the file | ||
ioctl_to_error(unsafe { | ||
ioctl( | ||
|
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.
Using
LOOP_CONFIGURE
will cause problems with toolchains that do not contain that define and the libc headers are generated from a kernel priorLOOP_CONFIGURE
. The devicemapper crate had similar problems with features added over the years.LOOP_CONFIGURE
is great but must be guarded by a feature.I'm not sure if it's possible to reliably detect the kernel, libc header version etc... from a build.rs.
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.
Just checked a build for
aarch64-linux-android
withcross
:I don't know the exact version of the libc headers used in the NDK here but it's notorious outdated ;-)