Skip to content
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

refactor(bdev/lock): add range lock trait #51

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
191 changes: 109 additions & 82 deletions src/bdev_desc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,88 +147,6 @@ where
}
}

/// Gains exclusive access over a block range, and returns
/// a lock object that must be used to unlock the range.
pub async fn lock_lba_range(
&self,
range: LbaRange,
) -> Result<LbaRangeLock<BdevData>, BdevDescError> {
let (s, r) = oneshot::channel::<i32>();

let ctx = Box::new(LockContext {
range,
ch: self.io_channel()?,
sender: Some(s),
});

unsafe {
let rc = bdev_lock_lba_range(
self.as_ptr(),
ctx.ch.legacy_as_ptr(),
ctx.range.offset,
ctx.range.len,
Some(LockContext::<BdevData>::lba_op_completion_cb),
ctx.as_ref() as *const _ as *mut c_void,
);
if rc != 0 {
return Err(BdevDescError::LbaLock {
source: nix::errno::from_i32(rc),
bdev_name: self.bdev().name().to_owned(),
});
}
}

// Wait for the lock to complete
let rc = r.await.unwrap();
if rc != 0 {
return Err(BdevDescError::LbaLock {
source: nix::errno::from_i32(rc),
bdev_name: self.bdev().name().to_owned(),
});
}

Ok(LbaRangeLock {
ctx,
})
}

/// Releases exclusive access over a block range.
pub async fn unlock_lba_range(
&self,
mut lock: LbaRangeLock<BdevData>,
) -> Result<(), BdevDescError> {
let (s, r) = oneshot::channel::<i32>();
lock.ctx.sender = Some(s);

unsafe {
let rc = bdev_unlock_lba_range(
self.as_ptr(),
lock.ctx.ch.legacy_as_ptr(),
lock.ctx.range.offset,
lock.ctx.range.len,
Some(LockContext::<BdevData>::lba_op_completion_cb),
lock.ctx.as_ref() as *const _ as *mut c_void,
);
if rc != 0 {
return Err(BdevDescError::LbaUnlock {
source: nix::errno::from_i32(rc),
bdev_name: self.bdev().name().to_owned(),
});
}
}

// Wait for the unlock to complete
let rc = r.await.unwrap();
if rc != 0 {
return Err(BdevDescError::LbaUnlock {
source: nix::errno::from_i32(rc),
bdev_name: self.bdev().name().to_owned(),
});
}

Ok(())
}

/// Returns a pointer to the underlying `spdk_bdev_desc` structure.
pub(crate) fn as_ptr(&self) -> *mut spdk_bdev_desc {
self.inner
Expand Down Expand Up @@ -375,3 +293,112 @@ impl<T: BdevOps> LockContext<T> {
pub struct LbaRangeLock<T: BdevOps> {
ctx: Box<LockContext<T>>,
}

/// Allows gaining exclusive access over a block range.
/// # Warning
/// Due to the lack of async drop the range lock must be
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if we can still release in the drop method, just not wait for it - and then the caller may decide to either wait or just allow the drop to do it if it doesn't care about waiting for unlock to complete, wdyt @dsavitskiy ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As I understand this drop part - since we have nothing custom to do in Drop, and the allocated heap memory will be anyway freed by default drop, it seems ok to wait for unlock to complete.

/// explicitly released.
#[async_trait::async_trait(?Send)]
pub trait LbaRangeLocker {
type RangeLock;

/// Gains exclusive access over a block range, and returns
/// a lock object that must be used to unlock the range.
async fn lock_lba_range(
&self,
range: LbaRange,
) -> Result<Self::RangeLock, BdevDescError>;

/// Releases exclusive access over a block range.
async fn unlock_lba_range(
&self,
mut lock: Self::RangeLock,
) -> Result<(), BdevDescError>;
}

#[async_trait::async_trait(?Send)]
impl<BdevData: BdevOps> LbaRangeLocker for BdevDesc<BdevData> {
type RangeLock = LbaRangeLock<BdevData>;

/// Gains exclusive access over a block range, and returns
/// a lock object that must be used to unlock the range.
async fn lock_lba_range(
&self,
range: LbaRange,
) -> Result<LbaRangeLock<BdevData>, BdevDescError> {
let (s, r) = oneshot::channel::<i32>();

let ctx = Box::new(LockContext {
range,
ch: self.io_channel()?,
sender: Some(s),
});

unsafe {
let rc = bdev_lock_lba_range(
self.as_ptr(),
ctx.ch.legacy_as_ptr(),
ctx.range.offset,
ctx.range.len,
Some(LockContext::<BdevData>::lba_op_completion_cb),
ctx.as_ref() as *const _ as *mut c_void,
);
if rc != 0 {
return Err(BdevDescError::LbaLock {
source: nix::errno::from_i32(rc),
bdev_name: self.bdev().name().to_owned(),
});
}
}

// Wait for the lock to complete
let rc = r.await.unwrap();
if rc != 0 {
return Err(BdevDescError::LbaLock {
source: nix::errno::from_i32(rc),
bdev_name: self.bdev().name().to_owned(),
});
}

Ok(LbaRangeLock {
ctx,
})
}

/// Releases exclusive access over a block range.
async fn unlock_lba_range(
&self,
mut lock: LbaRangeLock<BdevData>,
) -> Result<(), BdevDescError> {
let (s, r) = oneshot::channel::<i32>();
lock.ctx.sender = Some(s);

unsafe {
let rc = bdev_unlock_lba_range(
self.as_ptr(),
lock.ctx.ch.legacy_as_ptr(),
lock.ctx.range.offset,
lock.ctx.range.len,
Some(LockContext::<BdevData>::lba_op_completion_cb),
lock.ctx.as_ref() as *const _ as *mut c_void,
);
if rc != 0 {
return Err(BdevDescError::LbaUnlock {
source: nix::errno::from_i32(rc),
bdev_name: self.bdev().name().to_owned(),
});
}
}

// Wait for the unlock to complete
let rc = r.await.unwrap();
if rc != 0 {
return Err(BdevDescError::LbaUnlock {
source: nix::errno::from_i32(rc),
bdev_name: self.bdev().name().to_owned(),
});
}

Ok(())
}
}
9 changes: 8 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,14 @@ pub use crate::{
bdev::Bdev,
bdev_async::{BdevAsyncCallContext, BdevStats},
bdev_builder::BdevBuilder,
bdev_desc::{BdevDesc, BdevDescError, BdevEvent, LbaRange, LbaRangeLock},
bdev_desc::{
BdevDesc,
BdevDescError,
BdevEvent,
LbaRange,
LbaRangeLock,
LbaRangeLocker,
},
bdev_io::BdevIo,
bdev_iter::{BdevGlobalIter, BdevModuleIter},
bdev_module::{
Expand Down
Loading