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

Implement dm-sworndisk with core crate #35

Merged
merged 3 commits into from
Jun 30, 2024
Merged

Conversation

cqs21
Copy link
Contributor

@cqs21 cqs21 commented May 7, 2024

No description provided.

linux/bindings/src/btree/append.rs Outdated Show resolved Hide resolved
core/src/os/linux/mod.rs Show resolved Hide resolved
@@ -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();
Copy link
Contributor

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
    }
}

linux/dm-sworndisk/src/lib.rs Outdated Show resolved Hide resolved
}

let mut buf = Buf::alloc(1).unwrap();
pr_info!("----write begin");
Copy link
Contributor

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.

@cqs21 cqs21 force-pushed the integration branch 2 times, most recently from 8820a21 to fc87a36 Compare May 22, 2024 07:38
#![feature(unsize)]

mod bindings_generated {
include!("./bindings_generated.rs");
include!("./bindings_helpers_generated.rs");
}

// Vendor for `alloc::collections::btree`.
pub mod btree;
Copy link
Collaborator

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.

Copy link
Collaborator

@tatetian tatetian left a 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!

@tatetian tatetian merged commit f977a0e into asterinas:main Jun 30, 2024
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants