storage/nvmed: Pass NvmeNamespace by value and remove the separate nsid arguments

This commit is contained in:
bjorn3
2025-03-07 20:46:33 +01:00
parent aacf09cef4
commit ebdfc89e7a
2 changed files with 18 additions and 23 deletions
+8 -11
View File
@@ -170,7 +170,7 @@ pub struct NvmeRegs {
cmbsz: Mmio<u32>,
}
#[derive(Debug)]
#[derive(Copy, Clone, Debug)]
pub struct NvmeNamespace {
pub id: u32,
pub blocks: u64,
@@ -641,8 +641,7 @@ impl Nvme {
fn namespace_rw(
&self,
namespace: &NvmeNamespace,
nsid: u32,
namespace: NvmeNamespace,
lba: u64,
blocks_1: u16,
write: bool,
@@ -666,9 +665,9 @@ impl Nvme {
let mut cmd = NvmeCmd::default();
let comp = self.submit_and_complete_command(1, |cid| {
cmd = if write {
NvmeCmd::io_write(cid, nsid, lba, blocks_1, ptr0, ptr1)
NvmeCmd::io_write(cid, namespace.id, lba, blocks_1, ptr0, ptr1)
} else {
NvmeCmd::io_read(cid, nsid, lba, blocks_1, ptr0, ptr1)
NvmeCmd::io_read(cid, namespace.id, lba, blocks_1, ptr0, ptr1)
};
cmd.clone()
});
@@ -683,8 +682,7 @@ impl Nvme {
pub fn namespace_read(
&self,
namespace: &NvmeNamespace,
nsid: u32,
namespace: NvmeNamespace,
mut lba: u64,
buf: &mut [u8],
) -> Result<Option<usize>> {
@@ -698,7 +696,7 @@ impl Nvme {
assert!(blocks > 0);
assert!(blocks <= 0x1_0000);
self.namespace_rw(namespace, nsid, lba, (blocks - 1) as u16, false)?;
self.namespace_rw(namespace, lba, (blocks - 1) as u16, false)?;
chunk.copy_from_slice(&buffer_guard[..chunk.len()]);
@@ -710,8 +708,7 @@ impl Nvme {
pub fn namespace_write(
&self,
namespace: &NvmeNamespace,
nsid: u32,
namespace: NvmeNamespace,
mut lba: u64,
buf: &[u8],
) -> Result<Option<usize>> {
@@ -727,7 +724,7 @@ impl Nvme {
buffer_guard[..chunk.len()].copy_from_slice(chunk);
self.namespace_rw(namespace, nsid, lba, (blocks - 1) as u16, true)?;
self.namespace_rw(namespace, lba, (blocks - 1) as u16, true)?;
lba += blocks as u64;
}
+10 -12
View File
@@ -35,14 +35,14 @@ impl AsRef<NvmeNamespace> for DiskWrapper {
}
impl DiskWrapper {
fn pt(disk: &mut NvmeNamespace, nvme: &Nvme) -> Option<PartitionTable> {
fn pt(disk: NvmeNamespace, nvme: &Nvme) -> Option<PartitionTable> {
let bs = match disk.block_size {
512 => LogicalBlockSize::Lb512,
4096 => LogicalBlockSize::Lb4096,
_ => return None,
};
struct Device<'a> {
disk: &'a mut NvmeNamespace,
disk: NvmeNamespace,
nvme: &'a Nvme,
offset: u64,
}
@@ -74,7 +74,7 @@ impl DiskWrapper {
let blksize = self.disk.block_size;
let size_in_blocks = self.disk.blocks;
let disk = &mut self.disk;
let disk = self.disk;
let nvme = &mut self.nvme;
let read_block = |block: u64, block_bytes: &mut [u8]| {
@@ -83,7 +83,7 @@ impl DiskWrapper {
}
loop {
match nvme
.namespace_read(disk, disk.id, block, block_bytes)
.namespace_read(disk, block, block_bytes)
.map_err(|err| io::Error::from_raw_os_error(err.errno))?
{
Some(bytes) => {
@@ -122,9 +122,9 @@ impl DiskWrapper {
.ok()
.flatten()
}
fn new(mut inner: NvmeNamespace, nvme: &Nvme) -> Self {
fn new(inner: NvmeNamespace, nvme: &Nvme) -> Self {
Self {
pt: Self::pt(&mut inner, nvme),
pt: Self::pt(inner, nvme),
inner,
}
}
@@ -372,7 +372,7 @@ impl SchemeBlock for DiskScheme {
let disk = self.disks.get_mut(&number).ok_or(Error::new(EBADF))?;
let block_size = disk.as_ref().block_size;
self.nvme
.namespace_read(disk.as_ref(), disk.as_ref().id, offset / block_size, buf)
.namespace_read(*disk.as_ref(), offset / block_size, buf)
}
Handle::Partition(disk_num, part_num) => {
let disk = self.disks.get_mut(&disk_num).ok_or(Error::new(EBADF))?;
@@ -392,8 +392,7 @@ impl SchemeBlock for DiskScheme {
let abs_block = part.start_lba + rel_block;
self.nvme
.namespace_read(disk.as_ref(), disk.as_ref().id, abs_block, buf)
self.nvme.namespace_read(*disk.as_ref(), abs_block, buf)
}
}
}
@@ -411,7 +410,7 @@ impl SchemeBlock for DiskScheme {
let disk = self.disks.get_mut(&number).ok_or(Error::new(EBADF))?;
let block_size = disk.as_ref().block_size;
self.nvme
.namespace_write(disk.as_ref(), disk.as_ref().id, offset / block_size, buf)
.namespace_write(*disk.as_ref(), offset / block_size, buf)
}
Handle::Partition(disk_num, part_num) => {
let disk = self.disks.get_mut(&disk_num).ok_or(Error::new(EBADF))?;
@@ -431,8 +430,7 @@ impl SchemeBlock for DiskScheme {
let abs_block = part.start_lba + rel_block;
self.nvme
.namespace_write(disk.as_ref(), disk.as_ref().id, abs_block, buf)
self.nvme.namespace_write(*disk.as_ref(), abs_block, buf)
}
}
}