Separate SegmentSlice from Segment

This commit is contained in:
Zhang Junyang
2024-09-04 10:54:56 +08:00
committed by Tate, Hongliang Tian
parent d930829866
commit 909639fd70
19 changed files with 434 additions and 214 deletions

View File

@ -1,9 +1,10 @@
// SPDX-License-Identifier: MPL-2.0
use align_ext::AlignExt;
use aster_util::segment_slice::SegmentSlice;
use int_to_c_enum::TryFromInt;
use ostd::{
mm::{Frame, Infallible, Segment, VmReader, VmWriter},
mm::{Frame, Infallible, VmReader, VmWriter},
sync::WaitQueue,
};
@ -366,7 +367,7 @@ pub enum BioStatus {
#[derive(Debug, Clone)]
pub struct BioSegment {
/// The contiguous pages on which this segment resides.
pages: Segment,
pages: SegmentSlice,
/// The starting offset (in bytes) within the first page.
/// The offset should always be aligned to the sector size and
/// must not exceed the size of a single page.
@ -381,9 +382,8 @@ const SECTOR_SIZE: u16 = super::SECTOR_SIZE as u16;
impl<'a> BioSegment {
/// Constructs a new `BioSegment` from `Segment`.
pub fn from_segment(segment: Segment, offset: usize, len: usize) -> Self {
pub fn from_segment(segment: SegmentSlice, offset: usize, len: usize) -> Self {
assert!(offset + len <= segment.nbytes());
Self {
pages: segment.range(frame_range(&(offset..offset + len))),
offset: AlignedUsize::<SECTOR_SIZE>::new(offset % super::BLOCK_SIZE).unwrap(),
@ -396,7 +396,7 @@ impl<'a> BioSegment {
assert!(offset + len <= super::BLOCK_SIZE);
Self {
pages: Segment::from(frame),
pages: SegmentSlice::from(frame),
offset: AlignedUsize::<SECTOR_SIZE>::new(offset).unwrap(),
len: AlignedUsize::<SECTOR_SIZE>::new(len).unwrap(),
}
@ -418,7 +418,7 @@ impl<'a> BioSegment {
}
/// Returns the contiguous pages on which this segment resides.
pub fn pages(&self) -> &Segment {
pub fn pages(&self) -> &SegmentSlice {
&self.pages
}

View File

@ -1,7 +1,8 @@
// SPDX-License-Identifier: MPL-2.0
use aster_util::segment_slice::SegmentSlice;
use ostd::mm::{
FallibleVmRead, FallibleVmWrite, Frame, FrameAllocOptions, Segment, VmIo, VmReader, VmWriter,
FallibleVmRead, FallibleVmWrite, Frame, FrameAllocOptions, VmIo, VmReader, VmWriter,
};
use super::{
@ -16,7 +17,11 @@ use crate::prelude::*;
// TODO: Add API to submit bio with multiple segments in scatter/gather manner.
impl dyn BlockDevice {
/// Synchronously reads contiguous blocks starting from the `bid`.
pub fn read_blocks(&self, bid: Bid, segment: &Segment) -> Result<BioStatus, BioEnqueueError> {
pub fn read_blocks(
&self,
bid: Bid,
segment: &SegmentSlice,
) -> Result<BioStatus, BioEnqueueError> {
let bio = create_bio_from_segment(BioType::Read, bid, segment);
let status = bio.submit_and_wait(self)?;
Ok(status)
@ -26,7 +31,7 @@ impl dyn BlockDevice {
pub fn read_blocks_async(
&self,
bid: Bid,
segment: &Segment,
segment: &SegmentSlice,
) -> Result<BioWaiter, BioEnqueueError> {
let bio = create_bio_from_segment(BioType::Read, bid, segment);
bio.submit(self)
@ -46,7 +51,11 @@ impl dyn BlockDevice {
}
/// Synchronously writes contiguous blocks starting from the `bid`.
pub fn write_blocks(&self, bid: Bid, segment: &Segment) -> Result<BioStatus, BioEnqueueError> {
pub fn write_blocks(
&self,
bid: Bid,
segment: &SegmentSlice,
) -> Result<BioStatus, BioEnqueueError> {
let bio = create_bio_from_segment(BioType::Write, bid, segment);
let status = bio.submit_and_wait(self)?;
Ok(status)
@ -56,7 +65,7 @@ impl dyn BlockDevice {
pub fn write_blocks_async(
&self,
bid: Bid,
segment: &Segment,
segment: &SegmentSlice,
) -> Result<BioWaiter, BioEnqueueError> {
let bio = create_bio_from_segment(BioType::Write, bid, segment);
bio.submit(self)
@ -96,7 +105,8 @@ impl VmIo for dyn BlockDevice {
let segment = FrameAllocOptions::new(num_blocks as usize)
.uninit(true)
.alloc_contiguous()?;
let bio_segment = BioSegment::from_segment(segment, offset % BLOCK_SIZE, read_len);
let bio_segment =
BioSegment::from_segment(segment.into(), offset % BLOCK_SIZE, read_len);
(
Bio::new(
@ -147,7 +157,7 @@ impl VmIo for dyn BlockDevice {
.skip(offset % BLOCK_SIZE)
.write_fallible(reader)
.map_err(|(e, _)| e)?;
let bio_segment = BioSegment::from_segment(segment, offset % BLOCK_SIZE, len);
let bio_segment = BioSegment::from_segment(segment.into(), offset % BLOCK_SIZE, len);
Bio::new(
BioType::Write,
Sid::from_offset(offset),
@ -188,7 +198,7 @@ impl dyn BlockDevice {
.writer()
.skip(offset % BLOCK_SIZE)
.write(&mut buf.into());
let bio_segment = BioSegment::from_segment(segment, offset % BLOCK_SIZE, len);
let bio_segment = BioSegment::from_segment(segment.into(), offset % BLOCK_SIZE, len);
Bio::new(
BioType::Write,
Sid::from_offset(offset),
@ -203,7 +213,7 @@ impl dyn BlockDevice {
}
// TODO: Maybe we should have a builder for `Bio`.
fn create_bio_from_segment(type_: BioType, bid: Bid, segment: &Segment) -> Bio {
fn create_bio_from_segment(type_: BioType, bid: Bid, segment: &SegmentSlice) -> Bio {
let bio_segment = BioSegment::from_segment(segment.clone(), 0, segment.nbytes());
Bio::new(
type_,