From 03329430b457b4e631ee5d3f51a66ce9affc96ab Mon Sep 17 00:00:00 2001 From: bjorn3 <17426603+bjorn3@users.noreply.github.com> Date: Fri, 7 Mar 2025 20:34:33 +0100 Subject: [PATCH] storage/driver-block: Handle block_read read buffer internally --- storage/driver-block/src/lib.rs | 31 ++++++++++--------------------- storage/nvmed/src/scheme.rs | 11 +++-------- storage/virtio-blkd/src/scheme.rs | 5 +---- 3 files changed, 14 insertions(+), 33 deletions(-) diff --git a/storage/driver-block/src/lib.rs b/storage/driver-block/src/lib.rs index 2d40c69d57..316478f61a 100644 --- a/storage/driver-block/src/lib.rs +++ b/storage/driver-block/src/lib.rs @@ -6,7 +6,6 @@ use partitionlib::{LogicalBlockSize, PartitionTable}; /// Split the read operation into a series of block reads. /// `read_fn` will be called with a block number to be read, and a buffer to be filled. -/// The buffer must be large enough to hold `blksize` of data. /// `read_fn` must return a full block of data. /// Result will be the number of bytes read. // FIXME make private once nvmed uses the DiskWrapper defined in this crate @@ -14,7 +13,6 @@ pub fn block_read( offset: u64, blksize: u32, buf: &mut [u8], - block_bytes: &mut [u8], mut read_fn: impl FnMut(u64, &mut [u8]) -> Result<(), Error>, ) -> Result { // TODO: Yield sometimes, perhaps after a few blocks or something. @@ -32,6 +30,9 @@ pub fn block_read( let blk_size = usize::try_from(blksize).expect("blksize larger than usize"); let mut total_read = 0; + let mut block_bytes = [0u8; 4096]; + let block_bytes = &mut block_bytes[..blk_size]; + while curr_buf.len() > 0 { // TODO: Async/await? I mean, shouldn't AHCI be async? @@ -72,13 +73,12 @@ impl DiskWrapper { Ok(512) => LogicalBlockSize::Lb512, _ => return None, }; - struct Device<'a, 'b> { + struct Device<'a> { disk: &'a mut dyn Disk, offset: u64, - block_bytes: &'b mut [u8], } - impl<'a, 'b> Seek for Device<'a, 'b> { + impl<'a> Seek for Device<'a> { fn seek(&mut self, from: SeekFrom) -> io::Result { let size = i64::try_from(self.disk.size()).or(Err(io::Error::new( io::ErrorKind::Other, @@ -97,7 +97,7 @@ impl DiskWrapper { } } // TODO: Perhaps this impl should be used in the rest of the scheme. - impl<'a, 'b> Read for Device<'a, 'b> { + impl<'a> Read for Device<'a> { fn read(&mut self, buf: &mut [u8]) -> io::Result { let blksize = self .disk @@ -115,7 +115,6 @@ impl DiskWrapper { match disk.read(block, block_bytes) { Ok(Some(bytes)) => { assert_eq!(bytes, block_bytes.len()); - assert_eq!(bytes, blksize as usize); return Ok(()); } Ok(None) => { @@ -126,26 +125,16 @@ impl DiskWrapper { } } }; - let bytes_read = - block_read(self.offset, blksize, buf, self.block_bytes, read_block)?; + let bytes_read = block_read(self.offset, blksize, buf, read_block)?; self.offset += bytes_read as u64; Ok(bytes_read) } } - let mut block_bytes = [0u8; 4096]; - - partitionlib::get_partitions( - &mut Device { - disk, - offset: 0, - block_bytes: &mut block_bytes[..bs.into()], - }, - bs, - ) - .ok() - .flatten() + partitionlib::get_partitions(&mut Device { disk, offset: 0 }, bs) + .ok() + .flatten() } pub fn new(mut disk: Box) -> Self { diff --git a/storage/nvmed/src/scheme.rs b/storage/nvmed/src/scheme.rs index 493a08293b..977d03328b 100644 --- a/storage/nvmed/src/scheme.rs +++ b/storage/nvmed/src/scheme.rs @@ -41,14 +41,13 @@ impl DiskWrapper { 4096 => LogicalBlockSize::Lb4096, _ => return None, }; - struct Device<'a, 'b> { + struct Device<'a> { disk: &'a mut NvmeNamespace, nvme: &'a Nvme, offset: u64, - block_bytes: &'b mut [u8], } - impl<'a, 'b> Seek for Device<'a, 'b> { + impl<'a> Seek for Device<'a> { fn seek(&mut self, from: io::SeekFrom) -> io::Result { let size_u = self.disk.blocks * self.disk.block_size; let size = i64::try_from(size_u).or(Err(io::Error::new( @@ -70,7 +69,7 @@ impl DiskWrapper { } } - impl<'a, 'b> Read for Device<'a, 'b> { + impl<'a> Read for Device<'a> { fn read(&mut self, buf: &mut [u8]) -> io::Result { let blksize = self.disk.block_size; let size_in_blocks = self.disk.blocks; @@ -105,7 +104,6 @@ impl DiskWrapper { .try_into() .expect("Unreasonable block size above 2^32 bytes"), buf, - self.block_bytes, read_block, )?; self.offset += bytes_read as u64; @@ -113,14 +111,11 @@ impl DiskWrapper { } } - let mut block_bytes = [0u8; 4096]; - partitionlib::get_partitions( &mut Device { disk, nvme, offset: 0, - block_bytes: &mut block_bytes[..bs.into()], }, bs, ) diff --git a/storage/virtio-blkd/src/scheme.rs b/storage/virtio-blkd/src/scheme.rs index 609f5f5059..8880779c59 100644 --- a/storage/virtio-blkd/src/scheme.rs +++ b/storage/virtio-blkd/src/scheme.rs @@ -124,7 +124,6 @@ impl<'a> DiskScheme<'a> { struct VirtioShim<'a, 'b> { scheme: &'b DiskScheme<'a>, offset: u64, - block_bytes: &'b mut [u8], } impl<'a, 'b> Read for VirtioShim<'a, 'b> { @@ -156,8 +155,7 @@ impl<'a> DiskScheme<'a> { }; let bytes_read = - driver_block::block_read(self.offset, 512, buf, self.block_bytes, read_block) - .unwrap(); + driver_block::block_read(self.offset, 512, buf, read_block).unwrap(); self.offset += bytes_read as u64; Ok(bytes_read) } @@ -188,7 +186,6 @@ impl<'a> DiskScheme<'a> { let mut shim = VirtioShim { scheme: &this, offset: 0, - block_bytes: &mut [0u8; 4096], }; //driver_block::DiskWrapper::new(disk)