block: Replace specific bitmap implementation with trait

Replace the specific Bitmap implementation from the type signature used
for functions that take memory. This allows more flexibility when using
these functions in particular when these functions are used by the
vhost-user-block backend. An updated vhost-user-backend crate requires
extra constraints on the Bitmap implementation used (it must support
BitmapReplace which AtomicBitmap does not.)

Signed-off-by: Rob Bradford <rbradford@rivosinc.com>
(cherry picked from commit 6c0dedd560)
This commit is contained in:
Rob Bradford 2024-05-01 09:24:48 +01:00
parent c2d3d015d8
commit 8632dda669

View File

@ -58,16 +58,14 @@ use versionize_derive::Versionize;
use virtio_bindings::virtio_blk::*; use virtio_bindings::virtio_blk::*;
use virtio_queue::DescriptorChain; use virtio_queue::DescriptorChain;
use vm_memory::{ use vm_memory::{
bitmap::AtomicBitmap, bitmap::Bitmap, ByteValued, Bytes, GuestAddress, GuestMemory, bitmap::Bitmap, ByteValued, Bytes, GuestAddress, GuestMemory, GuestMemoryError,
GuestMemoryError, GuestMemoryLoadGuard, GuestMemoryLoadGuard,
}; };
use vm_virtio::{AccessPlatform, Translatable}; use vm_virtio::{AccessPlatform, Translatable};
use vmm_sys_util::aio; use vmm_sys_util::aio;
use vmm_sys_util::eventfd::EventFd; use vmm_sys_util::eventfd::EventFd;
use vmm_sys_util::{ioctl_io_nr, ioctl_ioc_nr}; use vmm_sys_util::{ioctl_io_nr, ioctl_ioc_nr};
type GuestMemoryMmap = vm_memory::GuestMemoryMmap<AtomicBitmap>;
const SECTOR_SHIFT: u8 = 9; const SECTOR_SHIFT: u8 = 9;
pub const SECTOR_SIZE: u64 = 0x01 << SECTOR_SHIFT; pub const SECTOR_SIZE: u64 = 0x01 << SECTOR_SHIFT;
@ -197,8 +195,8 @@ pub enum RequestType {
Unsupported(u32), Unsupported(u32),
} }
pub fn request_type( pub fn request_type<B: Bitmap + 'static>(
mem: &GuestMemoryMmap, mem: &vm_memory::GuestMemoryMmap<B>,
desc_addr: GuestAddress, desc_addr: GuestAddress,
) -> result::Result<RequestType, Error> { ) -> result::Result<RequestType, Error> {
let type_ = mem.read_obj(desc_addr).map_err(Error::GuestMemory)?; let type_ = mem.read_obj(desc_addr).map_err(Error::GuestMemory)?;
@ -211,7 +209,10 @@ pub fn request_type(
} }
} }
fn sector(mem: &GuestMemoryMmap, desc_addr: GuestAddress) -> result::Result<u64, Error> { fn sector<B: Bitmap + 'static>(
mem: &vm_memory::GuestMemoryMmap<B>,
desc_addr: GuestAddress,
) -> result::Result<u64, Error> {
const SECTOR_OFFSET: usize = 8; const SECTOR_OFFSET: usize = 8;
let addr = match mem.checked_offset(desc_addr, SECTOR_OFFSET) { let addr = match mem.checked_offset(desc_addr, SECTOR_OFFSET) {
Some(v) => v, Some(v) => v,
@ -241,8 +242,8 @@ pub struct Request {
} }
impl Request { impl Request {
pub fn parse( pub fn parse<B: Bitmap + 'static>(
desc_chain: &mut DescriptorChain<GuestMemoryLoadGuard<GuestMemoryMmap>>, desc_chain: &mut DescriptorChain<GuestMemoryLoadGuard<vm_memory::GuestMemoryMmap<B>>>,
access_platform: Option<&Arc<dyn AccessPlatform>>, access_platform: Option<&Arc<dyn AccessPlatform>>,
) -> result::Result<Request, Error> { ) -> result::Result<Request, Error> {
let hdr_desc = desc_chain let hdr_desc = desc_chain
@ -333,11 +334,11 @@ impl Request {
Ok(req) Ok(req)
} }
pub fn execute<T: Seek + Read + Write>( pub fn execute<T: Seek + Read + Write, B: Bitmap + 'static>(
&self, &self,
disk: &mut T, disk: &mut T,
disk_nsectors: u64, disk_nsectors: u64,
mem: &GuestMemoryMmap, mem: &vm_memory::GuestMemoryMmap<B>,
serial: &[u8], serial: &[u8],
) -> result::Result<u32, ExecuteError> { ) -> result::Result<u32, ExecuteError> {
disk.seek(SeekFrom::Start(self.sector << SECTOR_SHIFT)) disk.seek(SeekFrom::Start(self.sector << SECTOR_SHIFT))
@ -390,9 +391,9 @@ impl Request {
Ok(len) Ok(len)
} }
pub fn execute_async( pub fn execute_async<B: Bitmap + 'static>(
&mut self, &mut self,
mem: &GuestMemoryMmap, mem: &vm_memory::GuestMemoryMmap<B>,
disk_nsectors: u64, disk_nsectors: u64,
disk_image: &mut dyn AsyncIo, disk_image: &mut dyn AsyncIo,
serial: &[u8], serial: &[u8],