-
Notifications
You must be signed in to change notification settings - Fork 4
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
Implement dm-sworndisk with core crate #35
Conversation
@@ -171,23 +183,91 @@ impl ReqQueue { | |||
bio.end(); | |||
} | |||
|
|||
fn read_unaligned(&self, bio: &Bio) { | |||
let start_bytes = bio.start_sector() * (BLOCK_SIZE / BLOCK_SECTORS); | |||
let end_bytes = start_bytes + bio.len(); |
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.
When we come across this kind of read/write_unaligned, we need a block range iterator abstraction. Fortunatelly I have wrote one before. Maybe we can add this to /core/util
?
/// Given a range and iterate sub-range for each block.
#[derive(Clone, Debug)]
pub struct BlockRangeIter {
pub begin: usize,
pub end: usize,
pub block_size: usize,
}
/// Describe the range for one block.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct BlockRange {
pub block_id: Bid,
pub begin: usize,
pub end: usize,
pub block_size: usize,
}
impl Iterator for BlockRangeIter {
type Item = BlockRange;
fn next(&mut self) -> Option<<Self as Iterator>::Item> {
// Exit condition
if self.begin >= self.end {
return None;
}
// Construct sub-range of next block
let sub_range = {
let block_id = Bid::from_byte_offset(self.begin);
let begin = self.begin % self.block_size;
let end = if block_id == Bid::from_byte_offset(self.end) {
self.end % self.block_size
} else {
self.block_size
};
let block_size = self.block_size;
BlockRange {
block_id,
begin,
end,
block_size,
}
};
self.begin += sub_range.len();
Some(sub_range)
}
fn size_hint(&self) -> (usize, Option<usize>) {
let exact_size = {
let begin_bid = Bid::from_byte_offset(self.begin);
let end_bid = Bid::from_byte_offset(self.end);
if self.end % self.block_size == 0 {
(end_bid - begin_bid.to_raw()).to_raw() as _
} else {
(end_bid - begin_bid.to_raw()).to_raw() as usize + 1
}
};
(exact_size, Some(exact_size))
}
}
impl ExactSizeIterator for BlockRangeIter {
fn len(&self) -> usize {
let (lower, upper) = self.size_hint();
debug_assert!(upper == Some(lower));
lower
}
}
impl BlockRange {
/// Return block length.
pub fn len(&self) -> usize {
self.end - self.begin
}
/// Whether the range covers a whole block.
pub fn is_full(&self) -> bool {
self.len() == self.block_size
}
/// Whether the range is empty.
pub fn is_empty(&self) -> bool {
self.len() == 0
}
/// Describe the begin offset of this block from the whole range.
pub fn origin_begin(&self) -> usize {
self.block_id.to_raw() as usize * self.block_size + self.begin
}
/// Describe the end offset of this block from the whole range.
pub fn origin_end(&self) -> usize {
self.block_id.to_raw() as usize * self.block_size + self.end
}
}
} | ||
|
||
let mut buf = Buf::alloc(1).unwrap(); | ||
pr_info!("----write begin"); |
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.
Is printing the common manifestation in Linux's unit tests? Since in Rust asserts are enough to represent that a test case passed or not.
8820a21
to
fc87a36
Compare
linux/bindings/src/lib.rs
Outdated
#![feature(unsize)] | ||
|
||
mod bindings_generated { | ||
include!("./bindings_generated.rs"); | ||
include!("./bindings_helpers_generated.rs"); | ||
} | ||
|
||
// Vendor for `alloc::collections::btree`. | ||
pub mod btree; |
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.
This module is copied from Rust for Linux? Copying this amount of code directly from other projects is unmaintainable.
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.
LGTM. Thanks for the contribution!
No description provided.